| Current Path : /var/www/html/nadineandmiabeauty/admin/classes/ |
| Current File : /var/www/html/nadineandmiabeauty/admin/classes/api.php |
<?php
require_once('pdocrudhandler.php');
class api extends pdocrudhandler{
private $mendetoryParamsAgainstEachMethod;
//Adding blog information into database
public function addComment($data){
$table = config::getAppConfig()['table']['comment'];
$insertResponse = $this->insert($table,array('name' => $data['name'],'email' => $data['email'],'subject' => $data['subject'],'comment' => $data['comment'],'blog_id' => $data['blog_id']));
$response = $insertResponse;
return $response;
}
public function subscribe($data){
$table = config::getAppConfig()['table']['subscriber'];
$res = $this->select($table,array('email'), "WHERE email = ?", array($data['email']));
if($res['rowsAffected'] == 0){
$insertResponse = $this->insert($table,array('email' => $data['email']));
$response = $insertResponse;
}else{
$res = $this->update($table,array('active' => 1), 'WHERE email = ?', array($data['email']));
$response = $res;
}
return $response;
}
public function Pagesubscribe($data){
$table = config::getAppConfig()['table']['subscriber'];
$insertResponse = $this->insert($table,array('email' => $data['pageemail']));
$response = $insertResponse;
return $response;
}
public function updateUrl($data){
$table = config::getAppConfig()['table']['url'];
$insertResponse = $this->update($table,array('url' => $data['url']),'where id = ?', [2]);
$response = $insertResponse;
return $response;
}
public function curlPost($url,$data, $token){
$ch = curl_init();
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($data)),
'authorization: Bearer ' . $token
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$json = curl_exec($ch);
curl_close($ch);
$response = json_decode($json,true);
return $response;
}
public function curlGet($url,$token){
$headers = array("Authorization: Bearer ".$token);
$ch = curl_init();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$response = json_decode($json,true);
return $response;
}
public function curlDelete($url,$token){
$headers = array("Authorization: Bearer ".$token);
$ch = curl_init();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$response = json_decode($json,true);
return $response;
}
public function curlPut($url,$data, $token){
$ch = curl_init();
$header = array(
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($data)),
'authorization: Bearer ' . $token
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$json = curl_exec($ch);
curl_close($ch);
$response = json_decode($json,true);
return $response;
}
public function validateRequestsParams(){
$this->mendetoryParamsAgainstEachMethod = array(
'addComment' => array('name','email','subject','comment','blog_id'),
'subscribe' => array('email'),
'Pagesubscribe' => array('email'),
);
return $this->mendetoryParamsAgainstEachMethod;
}
}
$obj = new api();
$_pdo = new pdocrudhandler();
$validApiConsumers = config::getConsumerNamesAgainstApiKeys();
if(isset($_REQUEST['auth']) && $_REQUEST['auth'] != '' && isset($validApiConsumers[$_REQUEST['auth']])){
$consumerName = $validApiConsumers[$_REQUEST['auth']];
}else{
$consumerName = 'Invalid';
}
$action = (isset($_REQUEST['action'])) ? $_REQUEST['action'] : '';
$mendetoryParam = $obj->validateRequestsParams();
if(isset($_REQUEST['action']) && isset($mendetoryParam[$action]) && isset($_REQUEST['auth']) && $_REQUEST['auth'] != ''){
$data = array();
$missingFields = array();
$invalidFields = array();
$validApiKeys = config::getValidApiKeys();
if (!in_array($_REQUEST['auth'], $validApiKeys)) {
$invalidFields[] = "auth";
}
if (count($mendetoryParam[$_REQUEST['action']]) > 0) {
foreach($mendetoryParam[$_REQUEST['action']] as $value){
if(!isset($_REQUEST[$value]) || $_REQUEST[$value] == ''){
$missingFields[] = $value;
}
}
}
if(count($missingFields) > 0 || count($invalidFields) > 0){
$data['status'] = 'false';
$data['error'] = 'Required parameter(s) missing or have invalid value(s)';
$data['missingParameters'] = implode(',',$missingFields);
$data['invalidParameters'] = implode(',',$invalidFields);
if(!isset($_REQUEST['response_type']) || $_REQUEST['response_type'] == 'json'){
echo json_encode($data,true);
}else{
print_r($data);
}
}else{
$data = $_REQUEST;
$action = (string)$_REQUEST['action'];
$response = $obj->$action($data);
if(!isset($_REQUEST['response_type']) || $_REQUEST['response_type'] == 'json'){
echo json_encode($response);
}else{
echo "<pre>";
print_r($response);
}
}
}else{
echo json_encode(array('status'=>false,'error'=>'Invalid action or auth'));
}
?>