Advertisement
Gravehard

Untitled

Oct 10th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.35 KB | None | 0 0
  1. <?php
  2. require_once("Rest.inc.php");
  3.  
  4. class API extends REST {
  5.  
  6. public $data = "";
  7. const demo_version = false;
  8.  
  9. const DB_SERVER = "localhost";
  10. const DB_USER = "gamescas_app2";
  11. const DB_PASSWORD = "asd654321";
  12. const DB = "gamescas_app2";
  13. const GOOGLE_API_KEY = "AIzaSyBiB9CCaRZLfRP4bXXXXXXXXXXXXXXXXX";
  14.  
  15. private $db = NULL;
  16. private $mysqli = NULL;
  17. public function __construct(){
  18. parent::__construct(); // Init parent contructor
  19. $this->dbConnect(); // Initiate Database connection
  20. }
  21.  
  22. /* Connect to Database */
  23. private function dbConnect(){
  24. $this->mysqli = new mysqli(self::DB_SERVER, self::DB_USER, self::DB_PASSWORD, self::DB);
  25. }
  26.  
  27. /* Dynmically call the method based on the query string */
  28. public function processApi(){
  29. $func = strtolower(trim(str_replace("/","",$_REQUEST['x'])));
  30. if((int)method_exists($this,$func) > 0)
  31. $this->$func();
  32. else
  33. $this->response('processApi - method not exist',404); // If the method not exist with in this class "Page not found".
  34. }
  35. /* Api Checker */
  36. private function checkResponse(){
  37. if (mysqli_ping($this->mysqli)){
  38. echo "Database Connection : Success";
  39. }else {
  40. echo "Database Connection : Error";
  41. }
  42. }
  43.  
  44. /*
  45. * API USED BY ANDROID CLIENT -------------------------------------------------------------------------------------------------------
  46. */
  47.  
  48. //use start android LAZY_LOAD = false
  49. private function getApiClientData(){
  50. if($this->get_request_method() != "GET"){
  51. $this->response('',406);
  52. }
  53. $query_p = "SELECT * FROM place p ORDER BY p.last_update DESC;";
  54. $query_pc = "SELECT * FROM place_category;";
  55. $query_i = "SELECT DISTINCT * FROM images;";
  56. $p = $this->mysqli->query($query_p) or die($this->mysqli->error.__LINE__);
  57. $pc = $this->mysqli->query($query_pc) or die($this->mysqli->error.__LINE__);
  58. $i = $this->mysqli->query($query_i) or die($this->mysqli->error.__LINE__);
  59. $result["places"] = array();
  60. $result["place_category"] = array();
  61. $result["images"] = array();
  62. while($row = $p->fetch_assoc()){
  63. $result["places"][] = $row;
  64. }
  65. while($row = $pc->fetch_assoc()){
  66. $result["place_category"][] = $row;
  67. }
  68. while($row = $i->fetch_assoc()){
  69. $result["images"][] = $row;
  70. }
  71. $this->response($this->json($result), 200); // send user details
  72. }
  73.  
  74. //use start version 3.0 for android LAZY_LOAD = true
  75. private function getApiClientDataDraft(){
  76. if($this->get_request_method() != "GET"){
  77. $this->response('',406);
  78. }
  79. $query_p = "SELECT p.place_id, p.name, p.image, p.lat, p.lng, p.last_update FROM place p ORDER BY p.last_update DESC";
  80. $query_pc = "SELECT * FROM place_category;";
  81. $query_i = "SELECT DISTINCT * FROM images;";
  82. $p = $this->mysqli->query($query_p) or die($this->mysqli->error.__LINE__);
  83. $pc = $this->mysqli->query($query_pc) or die($this->mysqli->error.__LINE__);
  84. $i = $this->mysqli->query($query_i) or die($this->mysqli->error.__LINE__);
  85. $result["places"] = array();
  86. $result["place_category"] = array();
  87. $result["images"] = array();
  88. while($row = $p->fetch_assoc()){
  89. $result["places"][] = $row;
  90. }
  91. while($row = $pc->fetch_assoc()){
  92. $result["place_category"][] = $row;
  93. }
  94. while($row = $i->fetch_assoc()){
  95. $result["images"][] = $row;
  96. }
  97. $this->response($this->json($result), 200); // send user details
  98. }
  99.  
  100. /*
  101. * TABLE USERS TRANSACTION --------------------------------------------------------------------------------------------------------------
  102. */
  103. private function login(){
  104. if($this->get_request_method() != "POST"){
  105. $this->response('',406);
  106. }
  107. $customer = json_decode(file_get_contents("php://input"),true);
  108. $username = $customer["username"];
  109. $password = $customer["password"];
  110. if(!empty($username) and !empty($password)){ // empty checker
  111. $query="SELECT id, name, username, email FROM users WHERE password = '".md5($password)."' AND username = '$username' LIMIT 1";
  112. $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  113. if($r->num_rows > 0) {
  114. $result = $r->fetch_assoc();
  115. $this->response($this->json($result), 200);
  116. }
  117. $this->response('', 204); // If no records "No Content" status
  118. }
  119. $error = array('status' => "Failed", "msg" => "Invalid Email address or Password");
  120. $this->response($this->json($error), 400);
  121. }
  122.  
  123. private function users(){
  124. if($this->get_request_method() != "GET"){
  125. $this->response('',406);
  126. }
  127. $id = (int)$this->_request['id'];
  128. $query="SELECT id, name, username, email FROM users WHERE id=$id";
  129. $this->get_one($query);
  130. }
  131.  
  132. private function updateUsers(){
  133. if($this->get_request_method() != "POST"){
  134. $this->response('',406);
  135. }
  136. if(self::demo_version){
  137. $m = array('status' => "failed", "msg" => "Ops, this is demo version", "data" => null);
  138. $this->response($this->json($m),200);
  139. }
  140.  
  141. $users = json_decode(file_get_contents("php://input"),true);
  142. $id = (int)$users['id'];
  143. $password = $users['users']['password'];
  144. if($password == '*****'){
  145. $column_names = array('id', 'name', 'username', 'email');
  146. }else{
  147. $users['users']['password'] = md5($password);
  148. $column_names = array('id', 'name', 'username', 'email', 'password');
  149. }
  150. $table_name = 'users';
  151. $pk = 'id';
  152. $this->post_update($id, $users, $pk, $column_names, $table_name);
  153. }
  154.  
  155. private function insertUser(){
  156. if($this->get_request_method() != "POST"){
  157. $this->response('',406);
  158. }
  159. if(self::demo_version){
  160. $m = array('status' => "failed", "msg" => "Ops, this is demo version", "data" => null);
  161. $this->response($this->json($m),200);
  162. }
  163. $users = json_decode(file_get_contents("php://input"),true);
  164. $users['password'] = md5($users['password']);
  165. $column_names = array('name', 'username', 'email', 'password');
  166. $table_name = 'users';
  167. $pk = 'id';
  168. $this->post_one($users, $pk, $column_names, $table_name);
  169. }
  170.  
  171. /*
  172. * TABLE PLACES TRANSACTION ---------------------------------------------------------------------------------------------------------
  173. */
  174. private function getPlaces(){
  175. if($this->get_request_method() != "GET"){
  176. $this->response('',406);
  177. }
  178. $param = "";
  179. if(isset($this->_request['cat_id'])){
  180. $param = $this->_request['cat_id'];
  181. }
  182. if($param != ""){
  183. $cat_id = (int)$param;
  184. $query = "SELECT DISTINCT p.* FROM place p, category c WHERE p.place_id IN
  185. (SELECT pc.place_id FROM place_category pc WHERE pc.cat_id=$cat_id) ORDER BY p.last_update DESC";
  186. }else{
  187. $query = "SELECT * FROM place p ORDER BY p.last_update DESC";
  188. }
  189. $this->get_list($query);
  190. }
  191.  
  192. private function getPlace(){
  193. if($this->get_request_method() != "GET"){
  194. $this->response('',406);
  195. }
  196. $place_id = (int)$this->_request['place_id'];
  197. $query="SELECT * FROM place p WHERE p.place_id=$place_id";
  198. $this->get_one($query);
  199. }
  200.  
  201. private function insertPlace(){
  202. if($this->get_request_method() != "POST"){
  203. $this->response('',406);
  204. }
  205. $place = json_decode(file_get_contents("php://input"),true);
  206. $column_names = array('name', 'image', 'address', 'phone','website','description','lat','lng','last_update');
  207. $table_name = 'place';
  208. $pk = 'place_id';
  209. $this->post_one($place, $pk, $column_names, $table_name);
  210. }
  211.  
  212. private function updatePlace(){
  213. if($this->get_request_method() != "POST"){
  214. $this->response('',406);
  215. }
  216. $place = json_decode(file_get_contents("php://input"),true);
  217. $place_id = (int)$place['place_id'];
  218. $column_names = array('name', 'image', 'address', 'phone','website','description','lat','lng','last_update');
  219. $table_name = 'place';
  220. $pk = 'place_id';
  221. $this->post_update($place_id, $place, $pk, $column_names, $table_name);
  222. }
  223.  
  224. private function deletePlace(){
  225. if($this->get_request_method() != "DELETE"){
  226. $this->response('',406);
  227. }
  228. $place_id = (int)$this->_request['place_id'];
  229. $table_name = 'place';
  230. $pk = 'place_id';
  231. $this->delete_one($place_id, $pk, $table_name);
  232. }
  233.  
  234. private function getPlaceCount(){
  235. if($this->get_request_method() != "GET"){
  236. $this->response('',406);
  237. }
  238. $param = "";
  239. if(isset($this->_request['cat_id'])){
  240. $param = $this->_request['cat_id'];
  241. }
  242. if($param != ""){
  243. $cat_id = (int)$param;
  244. $query = "SELECT COUNT(DISTINCT p.place_id) FROM place p, category c WHERE p.place_id IN
  245. (SELECT pc.place_id FROM place_category pc WHERE pc.cat_id=$cat_id)";
  246. }else{
  247. $query="SELECT COUNT(p.place_id) FROM place p";
  248. }
  249. $this->get_count($query);
  250. }
  251.  
  252. private function getPlacesByPage(){
  253. if($this->get_request_method() != "GET"){
  254. $this->response('',406);
  255. }
  256. $limit = (int)$this->_request['limit'];
  257. $offset = ((int)$this->_request['page']) - 1;
  258.  
  259. $param = "";
  260. if(isset($this->_request['cat_id'])){
  261. $param = $this->_request['cat_id'];
  262. }
  263. if($param != ""){
  264. $cat_id = (int)$param;
  265. $query = "SELECT DISTINCT p.* FROM place p, category c WHERE p.place_id IN
  266. (SELECT pc.place_id FROM place_category pc WHERE pc.cat_id=$cat_id)
  267. ORDER BY p.last_update DESC LIMIT $limit OFFSET $offset";
  268. }else{
  269. $query="SELECT DISTINCT * FROM place p ORDER BY p.last_update DESC LIMIT $limit OFFSET $offset";
  270. }
  271.  
  272. $this->get_list($query);
  273. }
  274.  
  275. /*
  276. * TABLE CATEGORY TRANSACTION ----------------------------------------------------------------------------------------------------------
  277. */
  278. private function getCategories(){
  279. if($this->get_request_method() != "GET"){
  280. $this->response('',406);
  281. }
  282. $query="SELECT * FROM category c ORDER BY c.cat_id ASC";
  283. $this->get_list($query);
  284. }
  285.  
  286. private function getCategory(){
  287. if($this->get_request_method() != "GET"){
  288. $this->response('',406);
  289. }
  290. $cat_id = (int)$this->_request['cat_id'];
  291. $query="SELECT distinct * FROM category c WHERE c.cat_id=$cat_id";
  292. $this->get_one($query);
  293. }
  294.  
  295. private function getCategoriesByPlaceId(){
  296. if($this->get_request_method() != "GET"){
  297. $this->response('',406);
  298. }
  299. $place_id = (int)$this->_request['place_id'];
  300. $query = "SELECT DISTINCT c.* FROM category c WHERE c.cat_id IN (SELECT pc.cat_id FROM place_category pc WHERE pc.place_id=$place_id);";
  301. $this->get_list($query);
  302. }
  303.  
  304. /*
  305. * TABLE PLACE_CATEGORY TRANSACTION ----------------------------------------------------------------------------------------------------------
  306. */
  307. private function getPlaceCategories(){
  308. if($this->get_request_method() != "GET"){
  309. $this->response('',406);
  310. }
  311. $query="SELECT * FROM place_category;";
  312. $this->get_list($query);
  313. }
  314.  
  315. private function placeCategoriesByPlaceId(){
  316. if($this->get_request_method() != "GET"){
  317. $this->response('',406);
  318. }
  319. $place_id = (int)$this->_request['place_id'];
  320. $query="SELECT * FROM place_category WHERE place_id=".$place_id;
  321. $this->get_list($query);
  322. }
  323.  
  324. private function insertPlaceCategories(){
  325. if($this->get_request_method() != "POST"){
  326. $this->response('',406);
  327. }
  328. $place_category = json_decode(file_get_contents("php://input"),true);
  329. $column_names = array('place_id', 'cat_id');
  330. $table_name = 'place_category';
  331. try {
  332. $query="DELETE FROM ".$table_name." WHERE place_id = ".$place_category[0]['place_id'];
  333. $this->mysqli->query($query);
  334. } catch(Exception $e) {}
  335. $this->post_array($place_category, $column_names, $table_name);
  336. }
  337.  
  338. /*
  339. * TABLE IMAGES TRANSACTION ----------------------------------------------------------------------------------------------------------
  340. */
  341. private function getImages(){
  342. if($this->get_request_method() != "GET"){
  343. $this->response('',406);
  344. }
  345. $query="SELECT DISTINCT * FROM images;";
  346. $this->get_list($query);
  347. }
  348.  
  349. private function imagesByPlaceId(){
  350. if($this->get_request_method() != "GET"){
  351. $this->response('',406);
  352. }
  353. $place_id = (int)$this->_request['place_id'];
  354. $query="SELECT DISTINCT * FROM images i WHERE i.place_id=$place_id";
  355. $this->get_list($query);
  356. }
  357.  
  358. private function insertImages(){
  359. if($this->get_request_method() != "POST"){
  360. $this->response('',406);
  361. }
  362. $images = json_decode(file_get_contents("php://input"),true);
  363. $column_names = array('place_id', 'name');
  364. $table_name = 'images';
  365. try {
  366. $query="DELETE FROM ".$table_name." WHERE place_id = ".$images[0]['place_id'];
  367. $this->mysqli->query($query);
  368. } catch(Exception $e) {}
  369. $this->post_array($images, $column_names, $table_name);
  370. }
  371.  
  372. private function deleteImage(){
  373. if($this->get_request_method() != "DELETE"){
  374. $this->response('',406);
  375. }
  376. $_name = $this->_request['name'];
  377. $table_name = 'images';
  378. $pk = 'name';
  379. $target_file = "../../uploads/place/" . $_name;
  380. if(file_exists($target_file)){
  381. unlink($target_file);
  382. }
  383. $this->delete_one_str($_name, $pk, $table_name);
  384. }
  385.  
  386. /*
  387. * TABLE GCM TRANSACTION ------------------------------------------------------------------------------------------------------
  388. */
  389. private function gcms(){
  390. if($this->get_request_method() != "GET"){
  391. $this->response('',406);
  392. }
  393. $query="SELECT DISTINCT g.id, g.device, g.email, g.version, g.regid, g.date_create FROM gcm g ORDER BY g.id DESC";
  394. $this->get_list($query);
  395. }
  396.  
  397. private function allGcmId(){
  398. if($this->get_request_method() != "GET"){
  399. $this->response('',406);
  400. }
  401. $query="SELECT DISTINCT g.regid FROM gcm g";
  402. $this->get_list($query);
  403. }
  404.  
  405. private function getGcmCount(){
  406. if($this->get_request_method() != "GET"){
  407. $this->response('',406);
  408. }
  409. $query="SELECT COUNT(DISTINCT g.regid) FROM gcm g ORDER BY g.id DESC";
  410. $this->get_count($query);
  411. }
  412.  
  413. private function getGcmByPage(){
  414. if($this->get_request_method() != "GET"){
  415. $this->response('',406);
  416. }
  417. $limit = (int)$this->_request['limit'];
  418. $offset = ((int)$this->_request['page']) - 1;
  419. $query="SELECT DISTINCT * FROM gcm g ORDER BY g.id DESC LIMIT $limit OFFSET $offset";
  420. $this->get_list($query);
  421. }
  422.  
  423. private function insertGcm(){
  424. if($this->get_request_method() != "POST"){
  425. $this->response('',406);
  426. }
  427. $gcm = json_decode(file_get_contents("php://input"),true);
  428. $device = $gcm['device'];
  429. $email = $gcm['email'];
  430. $regid = $gcm['regid'];
  431.  
  432. $column_names = array('device', 'email', 'version', 'regid', 'date_create');
  433. $table_name = 'gcm';
  434. $pk = 'id';
  435. $query="SELECT DISTINCT g.id FROM gcm g WHERE g.regid='$regid' OR ( g.device='$device' AND g.email='$email' )";
  436. $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  437. if($r->num_rows > 0){ // update
  438. $result = $r->fetch_assoc();
  439. $id = (int)$result['id'];
  440. $new_gcm['id'] = $id;
  441. $new_gcm['gcm'] = $gcm;
  442. $this-> post_update($id, $new_gcm, $pk, $column_names, $table_name);
  443. }else{ // insert
  444. $this->post_one($gcm, $pk, $column_names, $table_name);
  445. }
  446. }
  447.  
  448. private function sendNotif() {
  449. if($this->get_request_method() != "POST"){
  450. $this->response('',406);
  451. }
  452. $body = json_decode(file_get_contents("php://input"),true);
  453. $registatoin_ids = $body['registatoin_ids'];
  454. $notif_title = $body['data']['title'];
  455. $notif_content = $body['data']['content'];
  456.  
  457. $gcmRegIds = array();
  458. $i = 0;
  459. // split gcm reg id per 1000 item
  460. foreach($registatoin_ids as $reg_id){
  461. $i++;
  462. $gcmRegIds[floor($i/1000)][] = $reg_id;
  463. }
  464. // send notif per 1000 items
  465. $pushStatus = array();
  466. foreach($gcmRegIds as $val){
  467. $pushStatus[] = $this->sendPushNotification($val, $notif_title, $notif_content);
  468. }
  469.  
  470. $success_count = 0;
  471. $failure_count = 0;
  472. foreach($pushStatus as $s){
  473. if(!empty($s['success'])) $success_count = $success_count + $s['success'];
  474. if(!empty($s['failure'])) $failure_count = $failure_count + ($s['failure']);
  475. }
  476.  
  477. $obj_data = array();
  478. if(!empty($pushStatus)){
  479. $obj_data['success'] = $success_count;
  480. $obj_data['failure'] = $failure_count;
  481. $resp['data'] = $obj_data;
  482. $this->response($this->json($resp), 200);
  483. }else{
  484. $this->response('',204); // "No Content" status
  485. }
  486.  
  487. }
  488.  
  489. private function sendPushNotification($registatoin_ids, $title, $content){
  490. // Set POST variables
  491. $url = 'https://android.googleapis.com/gcm/send';
  492. $fields = array(
  493. 'registration_ids' => $registatoin_ids,
  494. 'data' => array( 'title' => $title, 'content' => $content, )
  495. );
  496. $api_key = self::GOOGLE_API_KEY;
  497. $headers = array( 'Authorization: key='.$api_key, 'Content-Type: application/json' );
  498. // Open connection
  499. $ch = curl_init();
  500.  
  501. // Set the url, number of POST vars, POST data
  502. curl_setopt($ch, CURLOPT_URL, $url);
  503. curl_setopt($ch, CURLOPT_POST, true);
  504. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  505. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  506.  
  507. // Disabling SSL Certificate support temporarly
  508. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  509. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->json($fields));
  510. // Execute post
  511. $result = curl_exec($ch);
  512. if ($result === FALSE) { die('Curl failed: ' . curl_error($ch)); }
  513. // Close connection
  514. curl_close($ch);
  515. $result_data = json_decode($result);
  516. $result_arr = array();
  517. $result_arr['success'] = $result_data->success;
  518. $result_arr['failure'] = $result_data->failure;
  519. return $result_arr;
  520. }
  521.  
  522. /*
  523. * ========================================================================================================================
  524. * ===================================== API utilities # DO NOT EDIT ======================================================
  525. */
  526.  
  527. private function get_list($query){
  528. $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  529. if($r->num_rows > 0){
  530. $result = array();
  531. while($row = $r->fetch_assoc()){
  532. $result[] = $row;
  533. }
  534. $this->response($this->json($result), 200); // send user details
  535. }
  536. $this->response('',204); // If no records "No Content" status
  537. }
  538.  
  539. private function get_one($query){
  540. $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  541. if($r->num_rows > 0) {
  542. $result = $r->fetch_assoc();
  543. $this->response($this->json($result), 200); // send user details
  544. }
  545. $this->response('',204); // If no records "No Content" status
  546. }
  547.  
  548. private function get_count($query){
  549. $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  550. if($r->num_rows > 0) {
  551. $result = $r->fetch_row();
  552. $this->response($result[0], 200);
  553. }
  554. $this->response('',204); // If no records "No Content" status
  555. }
  556.  
  557. private function post_one($obj, $pk, $column_names, $table_name){
  558. $keys = array_keys($obj);
  559. $columns = '';
  560. $values = '';
  561. foreach($column_names as $desired_key){
  562. if(!in_array($desired_key, $keys)) {
  563. $$desired_key = '';
  564. }else{
  565. $$desired_key = $obj[$desired_key];
  566. }
  567. $columns = $columns.$desired_key.',';
  568. $values = $values."'".$this->real_escape($$desired_key)."',";
  569. }
  570. $query = "INSERT INTO ".$table_name."(".trim($columns,',').") VALUES(".trim($values,',').")";
  571. if(!empty($obj)){
  572. if ($this->mysqli->query($query)) {
  573. // retrive row after insert
  574. $last_id = $this->mysqli->insert_id;
  575. $get_query = "SELECT * FROM ".$table_name." WHERE ".$pk."=".$last_id;
  576. $r = $this->mysqli->query($get_query) or die($this->mysqli->error.__LINE__);
  577. if($r->num_rows > 0) {
  578. $obj = $r->fetch_assoc();
  579. }
  580. $status = "success";
  581. $msg = $table_name." created successfully";
  582. } else {
  583. $status = "failed";
  584. $msg = $this->mysqli->error.__LINE__;
  585. }
  586. $resp = array('status' => $status, "msg" => $msg, "data" => $obj);
  587. $this->response($this->json($resp),200);
  588. }else{
  589. $this->response('',204); //"No Content" status
  590. }
  591. }
  592.  
  593. private function post_array($obj_array, $column_names, $table_name){
  594. $query = "";
  595. for ($i = 0; $i < count($obj_array); $i++) {
  596. $obj = $obj_array[$i];
  597. $keys = array_keys($obj);
  598. $columns = '';
  599. $values = '';
  600. foreach($column_names as $desired_key){
  601. if(!in_array($desired_key, $keys)) {
  602. $$desired_key = '';
  603. }else{
  604. $$desired_key = $obj[$desired_key];
  605. }
  606. $columns = $columns.$desired_key.',';
  607. $values = $values."'".$this->real_escape($$desired_key)."',";
  608. }
  609. $query .= "INSERT INTO ".$table_name."(".trim($columns,',').") VALUES(".trim($values,',').");";
  610. }
  611. if(!empty($obj_array)){
  612. if ($this->mysqli->multi_query($query)) {
  613. $status = "success";
  614. $msg = $table_name." created successfully";
  615. } else {
  616. $status = "failed";
  617. $msg = $this->mysqli->error.__LINE__;
  618. }
  619. $resp = array('status' => $status, "msg" => $msg, "data" => $obj_array);
  620. $this->response($this->json($resp),200);
  621. }else{
  622. $this->response('',204); //"No Content" status
  623. }
  624. }
  625.  
  626. private function post_update($id, $obj, $pk, $column_names, $table_name){
  627. $keys = array_keys($obj[$table_name]);
  628. $columns = '';
  629. $values = '';
  630. foreach($column_names as $desired_key){ // Check the recipe received. If key does not exist, insert blank into the array.
  631. if(!in_array($desired_key, $keys)) {
  632. $$desired_key = '';
  633. }else{
  634. $$desired_key = $obj[$table_name][$desired_key];
  635. }
  636. $columns = $columns.$desired_key."='".$this->real_escape($$desired_key)."',";
  637. }
  638. $query = "UPDATE ".$table_name." SET ".trim($columns,',')." WHERE ".$pk."=$id";
  639. if(!empty($obj)){
  640. // $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  641. if ($this->mysqli->query($query)) {
  642. $status = "success";
  643. $msg = $table_name." update successfully";
  644. } else {
  645. $status = "failed";
  646. $msg = $this->mysqli->error.__LINE__;
  647. }
  648. $resp = array('status' => $status, "msg" => $msg, "data" => $obj);
  649. $this->response($this->json($resp),200);
  650. }else{
  651. $this->response('',204); // "No Content" status
  652. }
  653. }
  654.  
  655. private function delete_one($id, $pk, $table_name){
  656. $query="DELETE FROM ".$table_name." WHERE ".$pk." = $id";
  657. if ($this->mysqli->query($query)) {
  658. $status = "success";
  659. $msg = "One record " .$table_name." successfully deleted";
  660. } else {
  661. $status = "failed";
  662. $msg = $this->mysqli->error.__LINE__;
  663. }
  664. $resp = array('status' => $status, "msg" => $msg);
  665. $this->response($this->json($resp),200);
  666. }
  667.  
  668. private function delete_one_str($pkval, $pk, $table_name){
  669. $query="DELETE FROM ".$table_name." WHERE ".$pk." = '$pkval'";
  670. if ($this->mysqli->query($query)) {
  671. $status = "success";
  672. $msg = "One record " .$table_name." successfully deleted";
  673. } else {
  674. $status = "failed";
  675. $msg = $this->mysqli->error.__LINE__;
  676. }
  677. $resp = array('status' => $status, "msg" => $msg);
  678. $this->response($this->json($resp),200);
  679. }
  680.  
  681. /* ==================================== End of API utilities ==========================================
  682. * ====================================================================================================
  683. */
  684.  
  685. /*Encode array into JSON */
  686. private function json($data){
  687. if(is_array($data)){
  688. return json_encode($data, JSON_NUMERIC_CHECK);
  689. }
  690. }
  691.  
  692. /* String mysqli_real_escape_string */
  693. private function real_escape($s){
  694. return mysqli_real_escape_string($this->mysqli, $s);
  695. }
  696.  
  697. }
  698.  
  699. // Initiiate Library
  700.  
  701. $api = new API;
  702. $api->processApi();
  703. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement