Api.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /****************************
  3. * File : Api.php
  4. * Author : Cygnusa
  5. * @2018
  6. *
  7. * Main Api Class
  8. *
  9. ****************************/
  10. require_once("appconfig.php");
  11. require_once("Rest.inc.php");
  12. require_once("global.php");
  13. require_once("projectfunctions.php");
  14. class API extends REST {
  15. public $data = "";
  16. public $db = NULL;
  17. //CONFIG
  18. public $site_settings = NULL;
  19. private $role_administrator =1;
  20. private $role_admin =2;
  21. private $role_user =3;
  22. public $api_url = NULL;
  23. public $function_name = NULL;
  24. public $params=NULL;
  25. public $funcparams=NULL;
  26. public $lang=NULL;
  27. public $device_type=NULL;
  28. public $current_user_data =NULL;
  29. public $current_user_id=NULL;
  30. public $current_user_role=NULL;
  31. public $current_access_token=NULL;
  32. public $current_smpin=NULL;
  33. public $current_emp_code=NULL;
  34. public $current_page=0;
  35. public $manager_error=NULL;
  36. public $dse_roles=array("8,9,10,11,12,13");
  37. public function __construct(){
  38. parent::__construct();
  39. }
  40. /* /////////////////////////////// DEVICE VALIDATION ////////////////////////////// */
  41. private function validateDevice($device_type){
  42. if(defined('DEVICE_TYPE'))
  43. {
  44. $device_arr=explode("|",DEVICE_TYPE); if(!(is_array($device_arr)&& in_array($device_type,$device_arr))) $this->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
  45. }else {
  46. $this->response(array('response'=>'failed', 'message' => 'PAGE NOT FOUND'), 404);
  47. }
  48. }
  49. /* ///////////////////////////////////////////////////////////////////////////////////// */
  50. /* /////////////////////////////// LANGUAGE VALIDATION ////////////////////////////// */
  51. private function validateLanguage($lang){
  52. /*
  53. $query_lang=$this->getQueryValue("SELECT language_id FROM ".TABLE_MASTER_LANGUAGE." WHERE language_code='".$lang."' LIMIT 1");
  54. if(count($query_lang)<0)$this->response(array('response'=>'failed', 'message' => 'INVALID LANGUAGE'), 404);
  55. *
  56. */
  57. if(strtolower($lang)!='en_us')$this->response(array('response'=>'failed', 'message' => 'INVALID LANGUAGE'), 404);
  58. return true;
  59. }
  60. /* ///////////////////////////////////////////////////////////////////////////////////// */
  61. /* /////////////////////////////// DATABASE CONNECTION ////////////////////////////// */
  62. private function dbConnect(){
  63. try{
  64. $this->db = new PDO("mysql:host=".HOST.";dbname=".DBNAME.";charset=utf8",DBUSER,DBPASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  65. $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  66. }catch(PDOException $e){
  67. $this->response(array('response'=>'failed', 'message' => 'DB NOT CONNECTED'), 503);
  68. exit(1);
  69. }
  70. }
  71. /* ///////////////////////////////////////////////////////////////////////////////////// */
  72. /*
  73. * Public method for access api.
  74. * This method dynamically call the method based on the query string
  75. *
  76. */
  77. /* /////////////////////////////// PROCESS ////////////////////////////// */
  78. public function processApi(){
  79. $api_request=explode("_api/",urldecode($_SERVER['REQUEST_URI']));
  80. $this->api_url=$api_request[1];
  81. //CHECK URL API EXITS
  82. if(!isset($api_request[1]) || ($api_request[1]==NULL || $api_request[1]=='' || strpos($api_request[1], '/') === false)){
  83. $this->response(array('response'=>'failed','message' => 'PAGE NOT FOUND'),404);
  84. }else{
  85. //CHECK URL SPLIT
  86. $request_function=explode("/",$api_request[1]);
  87. if(!count($request_function)>3) $this->response(array('response'=>'failed','message'=>'PAGE NOT FOUND'),404);
  88. //CHECK DEVICE
  89. $this->device_type=trim(strtolower($request_function[0]));
  90. $this->validateDevice($this->device_type);
  91. //CONNNECT TO DB
  92. $this->dbConnect();
  93. //CHECK LANGUAGE
  94. $this->lang=trim($request_function[1]);
  95. $this->validateLanguage($this->lang);
  96. $function=strtolower(trim(str_replace("/","", $request_function[2])));
  97. $func_namearr=explode("?",$function);
  98. $this->function_name=$func_namearr[0];
  99. //FOR SINGLE CONTROLLER WITH FILTER CALL
  100. $this->funcparams=array_slice($func_namearr,1);
  101. $this->params=($request_function[2]==NULL || $request_function[2]=='')?NULL:array_slice($request_function,3);
  102. }
  103. $this->call_controller();
  104. }
  105. /* ///////////////////////////////////////////////////////////////////////////////////// */
  106. /* /////////////////////////////// CALL CONTROLLER ////////////////////////////// */
  107. private function call_controller()
  108. {
  109. if(file_exists(APPLICATION_PATH.CONTROLLER_PATH.DIRECTORY_SEPARATOR.strtolower($this->function_name).".php"))
  110. {
  111. $qry_params=explode ("?",$this->params[0]);
  112. try{
  113. spl_autoload_register('my_autoloader');
  114. $classname=$this->function_name;
  115. $obj = new $classname($this);
  116. }catch(Exception $e){
  117. $this->response(array('response'=>'failed','message'=>$e->getMessage()),500);
  118. }
  119. }else{
  120. // If the method not exist with in this class, response would be "Page not found".
  121. $this->response(array('response'=>'failed','message'=>'PAGE NOT FOUND'),404);
  122. }
  123. }
  124. /* ///////////////////////////////////////////////////////////////////////////////////// */
  125. /* MYSQL MAIN FUNCTION */
  126. /* /////////////////////////////// EXCECUTE QUERY ////////////////////////////// */
  127. public function executeQuery($str) {
  128. try
  129. {
  130. $result = $this->db->query($str);
  131. return $result;
  132. }
  133. catch (PDOException $e) {
  134. $this->response(array('response'=>'failed','message'=>"PHP CODE ERROR ".$e->getMessage()),500);
  135. }
  136. catch(Exception $e){
  137. $this->response(array('response'=>'failed','message'=>"PHP CODE ERROR ".$e->getMessage()),500);
  138. }
  139. }
  140. /* ///////////////////////////////////////////////////////////////////////////////////// */
  141. /* /////////////////////////////// QUERY VALUE ////////////////////////////// */
  142. public function getQueryValue($str) {
  143. try
  144. {
  145. $result = $this->executeQuery($str);
  146. $result_value = $result->fetchAll(PDO::FETCH_OBJ);
  147. return $result_value;
  148. }
  149. catch (PDOException $e) {
  150. $this->response(array('response'=>'failed','message'=>"MYSQL ERROR ".$e->getMessage()),500);
  151. }
  152. catch(Exception $e){
  153. $this->response(array('response'=>'failed','message'=>"PHP CODE ERROR ".$e->getMessage()),500);
  154. }
  155. }
  156. /* ///////////////////////////////////////////////////////////////////////////////////// */
  157. /* /////////////////////////////// SELECT QUERY ////////////////////////////// */
  158. public function selectQuery($table_name, $field_name, $condition, $limitations='')
  159. {
  160. try{
  161. if($field_name == "") $field_name = "*";
  162. $select_query_str = "SELECT $field_name FROM $table_name";
  163. if($condition!="") $select_query_str .= " where $condition";
  164. if($limitations!="") $select_query_str .= " limit $limitations";
  165. $query_obj=$this->executeQuery($select_query_str);
  166. $num_rows = $query_obj->rowCount($query_obj);
  167. $result_value=array();
  168. if($num_rows>0)
  169. {
  170. $result_value = $query_obj->fetchAll(PDO::FETCH_OBJ);
  171. }
  172. return array('nr' => $num_rows,'result'=>$result_value);
  173. }catch (PDOException $e) {
  174. $this->response(array('response'=>'failed','message'=>"MYSQL ERROR ".$e->getMessage()),500);
  175. }
  176. catch(Exception $e){
  177. $this->response(array('response'=>'failed','message'=>"PHP CODE ERROR ".$e->getMessage()),500);
  178. }
  179. }
  180. /* ///////////////////////////////////////////////////////////////////////////////////// */
  181. /* /////////////////////////////// QUERY COUNT ////////////////////////////// */
  182. public function getQueryCount($str) {
  183. try
  184. {
  185. $result = $this->executeQuery($str);
  186. $result_value = $result->rowCount();
  187. return $result_value;
  188. }
  189. catch (PDOException $e) {
  190. $this->response(array('response'=>'failed','message'=>"MYSQL ERROR ".$e->getMessage()),500);
  191. }
  192. catch(Exception $e){
  193. $this->response(array('response'=>'failed','message'=>"PHP CODE ERROR ".$e->getMessage()),500);
  194. }
  195. }
  196. /* ///////////////////////////////////////////////////////////////////////////////////// */
  197. /* /////////////////////////////// LAST INSERT ID ////////////////////////////// */
  198. public function lastInsertId(){
  199. return $this->db->lastInsertId();
  200. }
  201. /* ///////////////////////////////////////////////////////////////////////////////////// */
  202. }
  203. // Initiiate Library
  204. $api = new API;
  205. $api->processApi();
  206. ?>