solodroid

The Stream API order by alphabet

Nov 13th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.63 KB | None | 0 0
  1. <?php
  2.  
  3. require_once("Rest.inc.php");
  4.  
  5.     class API extends REST {
  6.  
  7.         public $data = "";
  8.         const demo_version = false;
  9.  
  10.         private $db     = NULL;
  11.         private $mysqli = NULL;
  12.         public function __construct() {
  13.             // Init parent contructor
  14.             parent::__construct();
  15.             // Initiate Database connection
  16.             $this->dbConnect();
  17.         }
  18.  
  19.         /*
  20.          *  Connect to Database
  21.         */
  22.         private function dbConnect() {
  23.             require_once ("../includes/config.php");
  24.             $this->mysqli = new mysqli($host, $user, $pass, $database);
  25.             $this->mysqli->query('SET CHARACTER SET utf8');
  26.         }
  27.  
  28.         /*
  29.          * Dynmically call the method based on the query string
  30.          */
  31.         public function processApi() {
  32.             $func = strtolower(trim(str_replace("/","",$_REQUEST['x'])));
  33.             if((int)method_exists($this,$func) > 0)
  34.                 $this->$func();
  35.             else
  36.                 $this->response('Ooops, no method found!',404); // If the method not exist with in this class "Page not found".
  37.         }
  38.  
  39.         /* Api Checker */
  40.         private function checkConnection() {
  41.             if (mysqli_ping($this->mysqli)) {
  42.                 //echo "Responses : Congratulations, database successfully connected.";
  43.                 $respon = array(
  44.                     'status' => 'ok', 'database' => 'connected'
  45.                 );
  46.                 $this->response($this->json($respon), 200);
  47.             } else {
  48.                 $respon = array(
  49.                     'status' => 'failed', 'database' => 'not connected'
  50.                 );
  51.                 $this->response($this->json($respon), 404);
  52.             }
  53.         }
  54.  
  55.         private function get_posts() {
  56.  
  57.             include "../includes/config.php";
  58.             $setting_qry    = "SELECT * FROM tbl_fcm_api_key where id = '1'";
  59.             $setting_result = mysqli_query($connect, $setting_qry);
  60.             $settings_row   = mysqli_fetch_assoc($setting_result);
  61.             $api_key    = $settings_row['api_key'];
  62.  
  63.             if (isset($_GET['api_key'])) {
  64.  
  65.                 $access_key_received = $_GET['api_key'];
  66.  
  67.                 if ($access_key_received == $api_key) {
  68.  
  69.                     if($this->get_request_method() != "GET") $this->response('',406);
  70.                     $limit = isset($this->_request['count']) ? ((int)$this->_request['count']) : 10;
  71.                     $page = isset($this->_request['page']) ? ((int)$this->_request['page']) : 1;
  72.  
  73.                     $offset = ($page * $limit) - $limit;
  74.                     $count_total = $this->get_count_result("SELECT COUNT(DISTINCT n.id) FROM tbl_channel n");
  75.  
  76.                     $query = "SELECT distinct
  77.                                 n.id AS 'channel_id',
  78.                                 n.category_id,
  79.                                 n.channel_name,
  80.                                 n.channel_image,
  81.                                 n.channel_url,
  82.                                 n.channel_description,
  83.                                
  84.                                 c.category_name
  85.                                
  86.                             FROM
  87.                                 tbl_channel n,
  88.                                 tbl_category c
  89.                                
  90.                             WHERE
  91.                                 n.category_id = c.cid ORDER BY n.channel_name ASC LIMIT $limit OFFSET $offset";
  92.  
  93.                     $post = $this->get_list_result($query);
  94.                     $count = count($post);
  95.                     $respon = array(
  96.                         'status' => 'ok', 'count' => $count, 'count_total' => $count_total, 'pages' => $page, 'posts' => $post
  97.                     );
  98.                     $this->response($this->json($respon), 200);
  99.  
  100.                 } else {
  101.                     die ('Oops, API Key is Incorrect!');
  102.                 }
  103.             } else {
  104.                 die ('Forbidden, API Key is Required!');
  105.             }
  106.  
  107.         }
  108.  
  109.         private function get_category_index() {
  110.  
  111.             include "../includes/config.php";
  112.             $setting_qry    = "SELECT * FROM tbl_fcm_api_key where id = '1'";
  113.             $setting_result = mysqli_query($connect, $setting_qry);
  114.             $settings_row   = mysqli_fetch_assoc($setting_result);
  115.             $api_key    = $settings_row['api_key'];
  116.  
  117.             if (isset($_GET['api_key'])) {
  118.  
  119.                 $access_key_received = $_GET['api_key'];
  120.  
  121.                 if ($access_key_received == $api_key) {
  122.  
  123.                     if($this->get_request_method() != "GET") $this->response('',406);
  124.                     $count_total = $this->get_count_result("SELECT COUNT(DISTINCT cid) FROM tbl_category");
  125.  
  126.                     $query = "SELECT distinct
  127.                                 cid,
  128.                                 category_name,
  129.                                 category_image
  130.                                
  131.                             FROM
  132.                                 tbl_category ORDER BY category_name ASC";
  133.  
  134.                     $news = $this->get_list_result($query);
  135.                     $count = count($news);
  136.                     $respon = array(
  137.                         'status' => 'ok', 'count' => $count, 'categories' => $news
  138.                     );
  139.                     $this->response($this->json($respon), 200);
  140.  
  141.                 } else {
  142.                     die ('Oops, API Key is Incorrect!');
  143.                 }
  144.             } else {
  145.                 die ('Forbidden, API Key is Required!');
  146.             }
  147.  
  148.         }
  149.  
  150.         private function get_category_posts() {
  151.  
  152.             include "../includes/config.php";
  153.             $setting_qry    = "SELECT * FROM tbl_fcm_api_key where id = '1'";
  154.             $setting_result = mysqli_query($connect, $setting_qry);
  155.             $settings_row   = mysqli_fetch_assoc($setting_result);
  156.             $api_key    = $settings_row['api_key'];
  157.  
  158.             if (isset($_GET['api_key'])) {
  159.  
  160.                 $access_key_received = $_GET['api_key'];
  161.  
  162.                 if ($access_key_received == $api_key) {
  163.  
  164.                     $id = $_GET['id'];
  165.  
  166.                     if($this->get_request_method() != "GET") $this->response('',406);
  167.                     $limit = isset($this->_request['count']) ? ((int)$this->_request['count']) : 10;
  168.                     $page = isset($this->_request['page']) ? ((int)$this->_request['page']) : 1;
  169.  
  170.                     $offset = ($page * $limit) - $limit;
  171.                     $count_total = $this->get_count_result("SELECT COUNT(DISTINCT id) FROM tbl_channel WHERE category_id = '$id'");
  172.  
  173.                     $query = "SELECT distinct
  174.                                 cid,
  175.                                 category_name,
  176.                                 category_image
  177.                                
  178.                             FROM
  179.                                 tbl_category
  180.  
  181.                             WHERE
  182.                                 cid = '$id'
  183.  
  184.                             ORDER BY cid DESC";
  185.  
  186.                     $query2 = "SELECT distinct
  187.                                 n.id AS 'channel_id',
  188.                                 n.category_id,
  189.                                 n.channel_name,
  190.                                 n.channel_image,
  191.                                 n.channel_url,
  192.                                 n.channel_description,
  193.                                
  194.                                 c.category_name
  195.                                
  196.                             FROM
  197.                                 tbl_channel n,
  198.                                 tbl_category c
  199.                                
  200.                             WHERE
  201.                                 n.category_id = c.cid AND c.cid = '$id' ORDER BY n.channel_name ASC LIMIT $limit OFFSET $offset";
  202.  
  203.                     $category = $this->get_category_result($query);
  204.                     $post = $this->get_list_result($query2);
  205.                     $count = count($post);
  206.                     $respon = array(
  207.                         'status' => 'ok', 'count' => $count, 'count_total' => $count_total, 'pages' => $page, 'category' => $category, 'posts' => $post
  208.                     );
  209.                     $this->response($this->json($respon), 200);
  210.  
  211.                 } else {
  212.                     die ('Oops, API Key is Incorrect!');
  213.                 }
  214.             } else {
  215.                 die ('Forbidden, API Key is Required!');
  216.             }
  217.  
  218.         }
  219.  
  220.         private function get_search_results() {
  221.  
  222.             include "../includes/config.php";
  223.             $setting_qry    = "SELECT * FROM tbl_fcm_api_key where id = '1'";
  224.             $setting_result = mysqli_query($connect, $setting_qry);
  225.             $settings_row   = mysqli_fetch_assoc($setting_result);
  226.             $api_key    = $settings_row['api_key'];
  227.  
  228.             if (isset($_GET['api_key'])) {
  229.  
  230.                 $access_key_received = $_GET['api_key'];
  231.  
  232.                 if ($access_key_received == $api_key) {
  233.  
  234.                     $search = $_GET['search'];
  235.  
  236.                     if($this->get_request_method() != "GET") $this->response('',406);
  237.                     $limit = isset($this->_request['count']) ? ((int)$this->_request['count']) : 10;
  238.                     $page = isset($this->_request['page']) ? ((int)$this->_request['page']) : 1;
  239.  
  240.                     $offset = ($page * $limit) - $limit;
  241.                     $count_total = $this->get_count_result("SELECT COUNT(DISTINCT n.id) FROM tbl_channel n, tbl_category c WHERE n.category_id = c.cid AND (n.channel_name LIKE '%$search%' OR n.channel_description LIKE '%$search%')");
  242.  
  243.                     $query = "SELECT distinct
  244.                                 n.id AS 'channel_id',
  245.                                 n.category_id,
  246.                                 n.channel_name,
  247.                                 n.channel_image,
  248.                                 n.channel_url,
  249.                                 n.channel_description,
  250.                                
  251.                                 c.category_name
  252.                                
  253.                             FROM
  254.                                 tbl_channel n,
  255.                                 tbl_category c
  256.                                
  257.                             WHERE n.category_id = c.cid AND (n.channel_name LIKE '%$search%' OR n.channel_description LIKE '%$search%')
  258.  
  259.                             LIMIT $limit OFFSET $offset";
  260.  
  261.                     $post = $this->get_list_result($query);
  262.                     $count = count($post);
  263.                     $respon = array(
  264.                         'status' => 'ok', 'count' => $count, 'count_total' => $count_total, 'pages' => $page, 'posts' => $post
  265.                     );
  266.                     $this->response($this->json($respon), 200);
  267.  
  268.                 } else {
  269.                     die ('Oops, API Key is Incorrect!');
  270.                 }
  271.             } else {
  272.                 die ('Forbidden, API Key is Required!');
  273.             }
  274.  
  275.         }
  276.  
  277.         //don't edit all the code below
  278.         private function get_list($query) {
  279.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  280.             if($r->num_rows > 0) {
  281.                 $result = array();
  282.                 while($row = $r->fetch_assoc()) {
  283.                     $result[] = $row;
  284.                 }
  285.                 $this->response($this->json($result), 200); // send user details
  286.             }
  287.             $this->response('',204);    // If no records "No Content" status
  288.         }
  289.  
  290.         private function get_list_result($query) {
  291.             $result = array();
  292.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  293.             if($r->num_rows > 0) {
  294.                 while($row = $r->fetch_assoc()) {
  295.                     $result[] = $row;
  296.                 }
  297.             }
  298.             return $result;
  299.         }
  300.  
  301.         private function get_object_result($query) {
  302.             $result = array();
  303.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  304.             if($r->num_rows > 0) {
  305.                 while($row = $r->fetch_assoc()) {
  306.                     $result = $row;
  307.                 }
  308.             }
  309.             return $result;
  310.         }
  311.  
  312.         private function get_category_result($query) {
  313.             $result = array();
  314.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  315.             if($r->num_rows > 0) {
  316.                 while($row = $r->fetch_assoc()) {
  317.                     $result = $row;
  318.                 }
  319.             }
  320.             return $result;
  321.         }
  322.  
  323.         private function get_one($query) {
  324.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  325.             if($r->num_rows > 0) {
  326.                 $result = $r->fetch_assoc();
  327.                 $this->response($this->json($result), 200); // send user details
  328.             }
  329.             $this->response('',204);    // If no records "No Content" status
  330.         }
  331.  
  332.         private function get_count($query) {
  333.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  334.             if($r->num_rows > 0) {
  335.                 $result = $r->fetch_row();
  336.                 $this->response($result[0], 200);
  337.             }
  338.             $this->response('',204);    // If no records "No Content" status
  339.         }
  340.  
  341.         private function get_count_result($query) {
  342.             $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  343.             if($r->num_rows > 0) {
  344.                 $result = $r->fetch_row();
  345.                 return $result[0];
  346.             }
  347.             return 0;
  348.         }
  349.  
  350.         private function post_one($obj, $column_names, $table_name) {
  351.             $keys       = array_keys($obj);
  352.             $columns    = '';
  353.             $values     = '';
  354.             foreach($column_names as $desired_key) { // Check the recipe received. If blank insert blank into the array.
  355.               if(!in_array($desired_key, $keys)) {
  356.                 $$desired_key = '';
  357.                 } else {
  358.                     $$desired_key = $obj[$desired_key];
  359.                 }
  360.                 $columns    = $columns.$desired_key.',';
  361.                 $values     = $values."'".$this->real_escape($$desired_key)."',";
  362.             }
  363.             $query = "INSERT INTO ".$table_name."(".trim($columns,',').") VALUES(".trim($values,',').")";
  364.             //echo "QUERY : ".$query;
  365.             if(!empty($obj)) {
  366.                 //$r = $this->mysqli->query($query) or trigger_error($this->mysqli->error.__LINE__);
  367.                 if ($this->mysqli->query($query)) {
  368.                     $status = "success";
  369.                 $msg        = $table_name." created successfully";
  370.                 } else {
  371.                     $status = "failed";
  372.                 $msg        = $this->mysqli->error.__LINE__;
  373.                 }
  374.                 $resp = array('status' => $status, "msg" => $msg, "data" => $obj);
  375.                 $this->response($this->json($resp),200);
  376.             } else {
  377.                 $this->response('',204);    //"No Content" status
  378.             }
  379.         }
  380.  
  381.         private function post_update($id, $obj, $column_names, $table_name) {
  382.             $keys = array_keys($obj[$table_name]);
  383.             $columns = '';
  384.             $values = '';
  385.             foreach($column_names as $desired_key){ // Check the recipe received. If key does not exist, insert blank into the array.
  386.               if(!in_array($desired_key, $keys)) {
  387.                 $$desired_key = '';
  388.                 } else {
  389.                     $$desired_key = $obj[$table_name][$desired_key];
  390.                 }
  391.                 $columns = $columns.$desired_key."='".$this->real_escape($$desired_key)."',";
  392.             }
  393.  
  394.             $query = "UPDATE ".$table_name." SET ".trim($columns,',')." WHERE id=$id";
  395.             if(!empty($obj)) {
  396.                 // $r = $this->mysqli->query($query) or die($this->mysqli->error.__LINE__);
  397.                 if ($this->mysqli->query($query)) {
  398.                     $status = "success";
  399.                     $msg    = $table_name." update successfully";
  400.                 } else {
  401.                     $status = "failed";
  402.                     $msg    = $this->mysqli->error.__LINE__;
  403.                 }
  404.                 $resp = array('status' => $status, "msg" => $msg, "data" => $obj);
  405.                 $this->response($this->json($resp),200);
  406.             } else {
  407.                 $this->response('',204);    // "No Content" status
  408.             }
  409.         }
  410.  
  411.         private function delete_one($id, $table_name) {
  412.             if($id > 0) {
  413.                 $query="DELETE FROM ".$table_name." WHERE id = $id";
  414.                 if ($this->mysqli->query($query)) {
  415.                     $status = "success";
  416.                 $msg        = "One record " .$table_name." successfully deleted";
  417.                 } else {
  418.                     $status = "failed";
  419.                 $msg        = $this->mysqli->error.__LINE__;
  420.                 }
  421.                 $resp = array('status' => $status, "msg" => $msg);
  422.                 $this->response($this->json($resp),200);
  423.             } else {
  424.                 $this->response('',204);    // If no records "No Content" status
  425.             }
  426.         }
  427.  
  428.         private function responseInvalidParam() {
  429.             $resp = array("status" => 'Failed', "msg" => 'Invalid Parameter' );
  430.             $this->response($this->json($resp), 200);
  431.         }
  432.  
  433.         /* ==================================== End of API utilities ==========================================
  434.          * ====================================================================================================
  435.          */
  436.  
  437.         /* Encode array into JSON */
  438.         private function json($data) {
  439.             if(is_array($data)) {
  440.                 return json_encode($data, JSON_NUMERIC_CHECK);
  441.             }
  442.         }
  443.  
  444.         /* String mysqli_real_escape_string */
  445.         private function real_escape($s) {
  446.             return mysqli_real_escape_string($this->mysqli, $s);
  447.         }
  448.     }
  449.  
  450.     // Initiate Library
  451.     $api = new API;
  452.     $api->processApi();
  453. ?>
Add Comment
Please, Sign In to add comment