Joywork/webhooks/webhook.php
2026-05-22 21:21:54 +03:00

172 lines
5.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once($_SERVER['DOCUMENT_ROOT']."/config.php");
header('Content-Type: application/json');
/*ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_STRICT & ~E_NOTICE);*/
$pdo = new MysqlPdo(hst, ndb, user, pass);
$sql = "SET NAMES 'utf8'";
$pdo->query($sql);
$content = file_get_contents('php://input');
$data = json_decode($content, true);
//var_dump($data);
if(!empty($data)){
$token_user = $data['token_user'];
$token_agency = $data['token_agency'];
$webhook = new WebHook($pdo);
$auth = $webhook->auth($data['token_user'], $data['token_agency']);
if(!isset($auth['error'])){
$agency_id = $webhook->get_agencyId();
$params = [];
$params['name'] = 'Не указано';
$params['about'] = '';
$params['phone'] = '';
$params['funnel_id'] = 0;
$params['funnel_req_id'] = 0;
$params['type_id'] = 2;
$params['object_id'] = 0;
$params['source_id'] = 0;
$params['activite_id'] = 0;
if($agency_id == 8826){
$webhook->set_source(1814);
$params['funnel_req_id'] = 841;
}
if(!empty($data['phone'])){
$params['phone'] = $data['phone'];
}
if(!empty($data['name'])){
$params['name'] = $data['name'];
}
if(!empty($data['about'])){
$params['about'] = $data['about'];
}
if(!empty($data['source_id'])){
$webhook->set_source((int)$data['source_id']);
}
if(!empty($data['activite_id'])){
$webhook->set_activites((int)$data['activite_id']);
}
if(!empty($data['funnel_id'])){
$params['funnel_id'] = (int)$data['funnel_id'];
}
if(!empty($data['funnel_req_id'])){
$params['funnel_req_id'] = (int)$data['funnel_req_id'];
}
if(!empty($data['object_id'])){
$params['object_id'] = (int)$data['object_id'];
}
if(!empty($data['type_id'])){
$params['type_id'] = (int)$data['type_id'];
}
if(!empty($data['general_req'])){
$params['general_req'] = (int)$data['general_req'];
}
if(!empty($data['action'])){
$params['action'] = $data['action'];
}
$res = $webhook->set_params($params);
//echo json_encode($res);
if(isset($res['error'])){
echo json_encode($res);
die();
} else if($res['action']){
$action = array();
switch ($res['action']) {
case 'get_types_req':{
$action = $webhook->get_types_req();
break;
}
case 'get_funnels' : {
$action = $webhook->get_funnels();
break;
}
case 'get_sources' : {
$action = $webhook->get_sources();
break;
}
case 'get_activites' : {
$action = $webhook->get_activites();
break;
}
case 'get_employees' : {
$action = $webhook->get_employees();
break;
}
case 'create_object' : {
if(!isset($data['params']) || empty($data['params']))
{
$error_arr = array('error'=>'no parameters specified');
echo json_encode($error_arr);
die();
}
$params = json_decode($data['params'], true);
$webhook->set_params_object($params);
if(isset($data['photo'])){
$webhook->set_photos(json_decode($data['photo'], true));
}
$action = $webhook->create_object();
break;
}
case 'get_objects' : {
if(!isset($data['size']) || empty($data['size']) || (int)$data['size'] == 0)
{
$error_arr = array('error'=>'no parameter size');
echo json_encode($error_arr);
die();
}
$size = (int)$data['size'];
$page = 0;
if(isset($data['page']))
$page = (int)$data['page'];
$action = $webhook->get_objects($size, $page);
break;
}
case 'get_objects_ids' : {
$objects = $data['ids'];
if(!is_array($objects)){
$error_arr = array('error'=>'empty objects ids');
echo json_encode($error_arr);
die();
}
$action = $webhook->get_objects_ids($objects);
break;
}
}
echo json_encode($action);
exit();
}
$webhook->save_client();
$webhook->create_req();
$req_id = (int)$webhook->get_req_id();
$client_id = (int)$webhook->get_client_id();
echo json_encode(array('done'=>array('result'=>'ok','req_id'=>$req_id,'client_id'=>$client_id)));
} else {
echo json_encode($auth);
die();
}
} else {
$res['error'] = "empty data";
echo json_encode($res);
}