clipbucket/upload/api/api.php

101 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2014-03-26 11:00:36 +00:00
<?php
require_once("Rest.inc.php");
class API extends REST
{
public $data = "";
const DB_SERVER = "localhost";
const DB_USER = "root";
const DB_PASSWORD = "";
const DB = "rest";
private $db = NULL;
public function __construct()
{
parent::__construct();// Init parent contructor
//$this->dbConnect();// Initiate Database connection
}
//Database connection
private function dbConnect()
{
2017-12-05 12:55:43 +05:00
$this->db = mysqli_connect(self::DB_SERVER,self::DB_USER,self::DB_PASSWORD);
2014-03-26 11:00:36 +00:00
if($this->db)
2017-12-06 11:41:30 +05:00
mysqli_select_db($this->db,self::DB);
2014-03-26 11:00:36 +00:00
}
//Public method for access api.
//This method dynmically call the method based on the query string
public function processApi()
{
$func = strtolower(trim(str_replace("/","",$_REQUEST['mode'])));
if((int)method_exists($this,$func) > 0)
$this->$func();
else
$this->response('',404);
// If the method not exist with in this class, response would be "Page not found".
}
private function users()
{
// Cross validation if the request method is GET else it will return "Not Acceptable" status
if($this->get_request_method() != "GET")
{
$this->response('',406);
}
2017-12-05 12:55:43 +05:00
$sql = mysqli_query($this->db ,"SELECT user_id, user_fullname, user_email FROM users WHERE user_status = 1");
if(mysqli_num_rows($sql) > 0)
2014-03-26 11:00:36 +00:00
{
$result = array();
2017-12-05 12:55:43 +05:00
while($rlt = mysqli_fetch_array($sql,MYSQLI_ASSOC))
2014-03-26 11:00:36 +00:00
{
$result[] = $rlt;
}
// If success everythig is good send header as "OK" and return list of users in JSON format
$this->response($this->json($result), 200);
}
$this->response('',204); // If no records "No Content" status
}
private function deleteUser()
{
if($this->get_request_method() != "DELETE"){
$this->response('',406);
}
$id = (int)$this->_request['id'];
if($id > 0)
{
2017-12-05 12:55:43 +05:00
mysqli_query($this->db,"DELETE FROM users WHERE user_id = $id");
2014-03-26 11:00:36 +00:00
2017-12-06 11:41:30 +05:00
if (mysqli_affected_rows($this->db) > 0)
2014-03-26 11:00:36 +00:00
$success = array('status' => "Success", "msg" => "Successfully one record deleted.");
else
$success = array('status' => "Failure", "msg" => "No such id exist in database");
$this->response($this->json($success),200);
}
else
{
$this->response('',204); // If no records "No Content" status
}
}
//Encode array into JSON
private function json($data)
{
if(is_array($data))
{
return json_encode($data);
}
}
}
// Initiiate Library
$api = new API;
$api->processApi();
?>