/var/www/myprog.com.ua/admin/controllers
Edit: /var/www/myprog.com.ua/admin/controllers/class.api.php (7852B)
errors[0] = 'OK';
$this->errors[1] = 'Unknown error occured';
$this->errors[2] = 'Application is disabled. Enable your application';
$this->errors[3] = 'Unknown method passed';
$this->errors[4] = 'Incorrect signature';
$this->errors[5] = 'Authorization method failed';
$this->errors[6] = 'Invalid request';
$this->errors[7] = 'Empty database';
$this->errors[8] = 'User authorization required';
$this->method_no_autorized[] = 'ping';
$this->method_no_autorized[] = 'login';
$this->method_no_autorized[] = 'restore_pass';
//$this->client_id = 0;
$this->su = false; // Superuser ['menu_admin']
}
public function index($url = 0){
//print_r($url);exit;
$url = explode('/',substr($_SERVER['REQUEST_URI'],1));
if (isset($url[2])) {//если есть параметр метода
$key = $value = array();
for($i=1, $count=count($url);$i<$count;$i++){
if($i%2==0){
$value[] = $url[$i];
}else{
$key[] = $url[$i];
}
}
if(count($key)!=count($value)){
$this->error(6);
exit;
}
$param = array_combine($key,$value);
$sig = isset($param['sig'])?$param['sig']:0;
$method = isset($param['method'])?$param['method']:0;
$client_key = isset($param['client_key'])?$param['client_key']:0;
$ip = $_SERVER['REMOTE_ADDR'];
unset($param['sig']);
ksort($param);
//print_r($param);exit;
$query = '';
foreach($param as $k => $v){
$query.=$k.'='.$v;
}
$apikey = API_KEY;
if($apikey==null)$this->error(2,$param);
//$apikey = e270b9a4510e46e5d21c763fe5dca4e2 (Android DEV)
$test_sig = md5($query.$apikey);
if($test_sig!=$sig&&!API_NO_AUTH){
$this->error(4,$param);
exit;
}
// Авторизация клиента по ключу
$this->client_id = $this->model->getClientIdByClientKey($client_key);
if($this->client_id>0){
$client_authorized = true;
//session::set('id_user',$this->client_id);
if(isset($this->view->perm['menu_admin'])){
$this->su = true;
}
}else{
$client_authorized = false;
}
if(!$client_authorized){
if (!in_array($method, $this->method_no_autorized)) {
$this->error(8);
exit;
}
}
$json['response']['client_authorized'] = $client_authorized;
if (method_exists($this, $method)) {
unset($param['method']);
unset($param['random']);
$post = '';
foreach($_POST as $k => $v){
$post.=$k.'='.$v.' ';
}
$this->model->database->log('API CALL '.$method." POST: ".$post."\r\n",get_class($this).'->'.__FUNCTION__);
$this->{$method}( $param ); //загружаем методs с передаными параметрами
}else{
$this->model->database->log('API ERROR CALL '.$method." POST: ".implode(' | ',$_POST)."\r\n",get_class($this).'->'.__FUNCTION__);
$this->error(3,$param);
return false;
}
} else {
$this->error(6);
exit;
}
}
public function error($error=0,$param=0){
$json['error'] = $error;
$json['error_msg'] = $this->errors[$error];
$json['request'] = $param;
echo $this->model->json_encode__($json);
exit;
}
// ====================== FUNCTIONS ========================= //
public function ping($param=0){
$json['error'] = 0;
$json['response']['method'] = 'ping';
$json['response']['server_title'] = APP_NAME;
echo $this->model->json_encode__($json);
return false;
}
public function login($param=0){
$client_key = $this->model->login();
if($client_key!=null){
$this->client_id = $this->model->getClientIdByClientKey($client_key);
$this->view->perm = $this->model->getPerm($this->client_id);
if(isset($this->view->perm['menu_admin'])){
$this->su = true;
}
$conf = $this->model->getConf();
$json['error'] = 0;
$json['response']['method'] = 'login';
//$json['response']['client_id'] = $this->client_id;
$json['response']['client_key'] = $client_key;
$json['response']['client_permissions'] = $this->view->perm;
$json['response']['transfer_incoming_type'] = TRANSFER_INCOMING_TYPE;
$json['response']['transfer_consumption_type'] = TRANSFER_CONSUMPTION_TYPE;
echo $this->model->json_encode__($json);
}else{
$this->error(5);
}
return false;
}
public function get_bills($param=0){
$bills = $this->model->get_bills($this->client_id,$this->su);
if(!is_array($bills)){
$bills = array();
}
$json['error'] = 0;
$json['response']['method'] = 'get_bills';
$json['response']['client_id'] = $this->client_id;
$json['response']['bills'] = $bills;
echo $this->model->json_encode__($json);
return false;
}
public function get_orders($param=0){
$orders = $this->model->get_orders($this->client_id,$this->su);
if(!is_array($orders)){
$orders = array();
}
$temp = $this->model->getOrderTypes(1,$this->client_id);
foreach($temp as $v){
if($v['owner_id']==0){
//$v['name'] = '12345';
$incoming_types[] = $v;
foreach($temp as $v2){
if($v['id']==$v2['owner_id']){
$v2['name'] = '⤷ '.$v2['name']; // ╰
$incoming_types[] = $v2;
}
}
}
}
$temp = $this->model->getOrderTypes(0,$this->client_id);
foreach($temp as $v){
if($v['owner_id']==0){
$consumption_types[] = $v;
foreach($temp as $v2){
if($v['id']==$v2['owner_id']){
$v2['name'] = '⤷ '.$v2['name']; // ╰
$consumption_types[] = $v2;
}
}
}
}
$json['error'] = 0;
$json['response']['method'] = 'get_orders';
$json['response']['orders'] = $orders;
$json['response']['company_bills'] = $this->model->getCompanyBills();
$json['response']['contractors'] = $this->model->getContractors($this->client_id);
$json['response']['incoming_types'] = $incoming_types;
$json['response']['consumption_types'] = $consumption_types;
echo $this->model->json_encode__($json);
return false;
}
public function order_create($param=0){
list($error,$error_msg,$order_id) = $this->model->order_create($this->client_id,$this->su);
if($error>0){
$this->errors[$error] = $error_msg;
$this->error($error);
return false;
}
$json['error'] = 0;
$json['response']['method'] = 'order_create';
$json['response']['order_id'] = $order_id;
echo $this->model->json_encode__($json);
return false;
}
public function test123($param=0){
$json['error'] = 0;
$this->loadModel('user',true);
session::set('id_user',$param['user_id']);
$city_id = isset($_POST['city_id'])?$_POST['city_id']:0;
session::set('city_id',$city_id);
$this->getCollection('user')->addorder(true);
$order_id = $this->getCollection('user')->create_order(true);
$json['response']['method'] = 'order_create';
$json['response']['order_id'] = $order_id;
echo $this->model->json_encode__($json);
return false;
}
}