From ee08ac0a64ea6092b7626d3039e896309b1b18d9 Mon Sep 17 00:00:00 2001 From: CB9TOIIIA Date: Fri, 5 May 2017 22:20:38 +0300 Subject: [PATCH] add megaplan --- mod_simplecallback/crm/index.html | 1 + mod_simplecallback/crm/megaplan/Request.php | 340 ++++++++++++++++++ .../crm/megaplan/RequestInfo.php | 89 +++++ mod_simplecallback/crm/megaplan/index.html | 1 + mod_simplecallback/helper.php | 64 ++++ mod_simplecallback/mod_simplecallback.xml | 63 +++- updatemysimplecallback.xml | 4 +- 7 files changed, 559 insertions(+), 3 deletions(-) create mode 100644 mod_simplecallback/crm/index.html create mode 100644 mod_simplecallback/crm/megaplan/Request.php create mode 100644 mod_simplecallback/crm/megaplan/RequestInfo.php create mode 100644 mod_simplecallback/crm/megaplan/index.html diff --git a/mod_simplecallback/crm/index.html b/mod_simplecallback/crm/index.html new file mode 100644 index 0000000..fa6d84e --- /dev/null +++ b/mod_simplecallback/crm/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/mod_simplecallback/crm/megaplan/Request.php b/mod_simplecallback/crm/megaplan/Request.php new file mode 100644 index 0000000..05d6d0f --- /dev/null +++ b/mod_simplecallback/crm/megaplan/Request.php @@ -0,0 +1,340 @@ +accessId = $AccessId; + $this->secretKey = $SecretKey; + $this->host = $Host; + $this->https = $Https; + $this->timeout = $Timeout; + } +//===========================================================================}}} +//{{{ useHttps +/** + * Устанавливает нужно ли использовать https-соединение + * @since 20.12.2010 13:43 + * @author megaplan + * @param bool $Https true + */ + public function useHttps( $Https = true ) + { + $this->https = $Https; + } +//===========================================================================}}} +//{{{ setOutputFile +/** + * Устанавливает путь к файлу, в который будет записан всё содержимое ответа + * @since 20.12.2010 13:46 + * @author megaplan + * @param string $FilePath Путь к файлу + */ + public function setOutputFile( $FilePath ) + { + $this->outputFile = $FilePath; + } +//===========================================================================}}} +//{{{ get +/** + * Отправляет GET-запрос + * @since 31.03.2010 20:17 + * @author megaplan + * @param string $Uri + * @param array $Params GET-параметры + * @return string Ответ на запрос + */ + public function get( $Uri, array $Params = NULL ) + { + $date = new DateTime(); + + $Uri = $this->processUri( $Uri, $Params ); + + $request = SdfApi_RequestInfo::create( 'GET', $this->host, $Uri, array( 'Date' => $date->format( 'r' ) ) ); + + return $this->send( $request ); + } +//===========================================================================}}} +//{{{ post +/** + * Отправляет POST-запрос + * @since 19.05.2010 16:27 + * @author megaplan + * @param string $Uri + * @param array $Params GET-параметры + * @return string Ответ на запрос + */ + public function post( $Uri, array $Params = NULL ) + { + $date = new DateTime(); + + $Uri = $this->processUri( $Uri ); + + $headers = array( + 'Date' => $date->format( 'r' ), + 'Post-Fields' => $Params, + 'Content-Type' => 'application/x-www-form-urlencoded' + ); + + $request = SdfApi_RequestInfo::create( 'POST', $this->host, $Uri, $headers ); + + return $this->send( $request ); + } +//===========================================================================}}} +//{{{ processUri +/** + * Собирает строку запроса из URI и параметров + * @since 05.04.2010 14:26 + * @author megaplan + * @param string $Uri URI + * @param array $Params Параметры запроса + * @return string + */ + public function processUri( $Uri, array $Params = NULL ) + { + $part = parse_url( $Uri ); + + if ( ! preg_match( "/\.[a-z]+$/u", $part['path'] ) ) { + $part['path'] .= '.easy'; + } + + $Uri = $part['path']; + + if ( $Params ) + { + if ( ! empty( $part['query'] ) ) { + parse_str( $part['query'], $Params ); + } + $Uri .= '?'.http_build_query( $Params ); + } + elseif ( ! empty( $part['query'] ) ) { + $Uri .= '?' . $part['query']; + } + + return $Uri; + } +//===========================================================================}}} +//{{{ send +/** + * Осуществляет отправку запроса + * @since 01.04.2010 14:53 + * @author megaplan + * @param SdfApi_RequestInfo $Request Параметры запроса + * @return string Ответ на запрос + */ + protected function send( SdfApi_RequestInfo $Request ) + { + $signature = self::calcSignature( $Request, $this->secretKey ); + + $headers = array( + 'Date: '.$Request->Date, + 'X-Authorization: '.$this->accessId . ':' . $signature, + 'Accept: application/json' + ); + if ( $Request->ContentType ) { + $headers[] = 'Content-Type: '.$Request->ContentType; + } + if ( $Request->ContentMD5 ) { + $headers[] = 'Content-MD5: '.$Request->ContentMD5; + } + + $url = 'http' . ( $this->https ? 's' : '' ) . '://' . $this->host . $Request->Uri; + + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); + curl_setopt( $ch, CURLOPT_USERAGENT, __CLASS__ ); + curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, $Request->Method ); + + if ( $Request->Method == 'POST' ) + { + curl_setopt( $ch, CURLOPT_POST, true ); + if ( $Request->PostFields ) + { + $postFields = is_array( $Request->PostFields ) ? http_build_query( $Request->PostFields ) : $Request->PostFields; + curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields ); + } + } + + curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); + + if ( $this->https ) + { + curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 ); + curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); + } + + if ( $this->outputFile ) + { + $fh = fopen( $this->outputFile, 'wb' ); + curl_setopt( $ch, CURLOPT_FILE, $fh ); + } + + curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ); + curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $this->timeout ); + curl_setopt( $ch, CURLOPT_TIMEOUT, $this->timeout ); + + if ( $this->outputFile ) + { + curl_exec( $ch ); + $this->result = NULL; + fclose( $fh ); + } + else { + $this->result = curl_exec( $ch ); + } + + $this->info = curl_getinfo( $ch ); + $this->error = curl_error( $ch ); + + curl_close( $ch ); + + return $this->result; + } +//===========================================================================}}} +//{{{ calcSignature +/** + * Вычисляет подпись запроса + * @since 31.03.2010 20:21 + * @author megaplan + * @param SdfApi_RequestInfo $Request Параметры запроса + * @param string $SecretKey Секретный ключ + * @return string Подпись запроса + */ + public static function calcSignature( SdfApi_RequestInfo $Request, $SecretKey ) + { + $stringToSign = $Request->Method . "\n" . + $Request->ContentMD5 . "\n" . + $Request->ContentType . "\n" . + $Request->Date . "\n" . + $Request->Host . $Request->Uri; + + $signature = base64_encode( self::hashHmac( 'sha1', $stringToSign, $SecretKey ) ); + + return $signature; + } +//===========================================================================}}} +//{{{ hashHmac +/** + * Клон функции hash_hmac + * @since 14.05.2010 + * @author megaplan + * @param string $Algo алгоритм, по которому производится шифрование + * @param string $Data строка для шифрования + * @param string $Key ключ + * @param boolean $RawOutput + * @return string + */ + public static function hashHmac( $Algo, $Data, $Key, $RawOutput = false ) + { + if ( function_exists( 'hash_hmac' ) ) { + return hash_hmac( $Algo, $Data, $Key, $RawOutput ); + } + $Algo = strtolower( $Algo ); + $pack = 'H' . strlen( $Algo( 'test' ) ); + $size = 64; + $opad = str_repeat( chr( 0x5C ), $size ); + $ipad = str_repeat( chr( 0x36 ), $size ); + + if ( strlen( $Key ) > $size ){ + $Key = str_pad( pack( $pack, $Algo( $Key ) ), $size, chr( 0x00 ) ); + } else { + $Key = str_pad( $Key, $size, chr( 0x00 ) ); + } + + for ( $i = 0; $i < strlen( $Key ) - 1; $i++ ) { + $opad[$i] = $opad[$i] ^ $Key[$i]; + $ipad[$i] = $ipad[$i] ^ $Key[$i]; + } + + $output = $Algo( $opad.pack( $pack, $Algo( $ipad.$Data ) ) ); + + return ( $RawOutput ) ? pack( $pack, $output ) : $output; + } +//===========================================================================}}} +//{{{ getResult +/** + * Возвращает результат последнего запроса + * @since 07.10.2010 17:45 + * @author megaplan + * @return mixed + */ + public function getResult() + { + return $this->result; + } +//===========================================================================}}} +//{{{ getInfo +/** + * Возвращает информацию о последнем запросе + * @since 07.10.2010 17:52 + * @author megaplan + * @param string $Param Параметр запроса (если не указан, возвращается вся информация) + * @return mixed + */ + public function getInfo( $Param = NULL ) + { + if ( $Param ) { + return isset( $this->info[$Param] ) ? $this->info[$Param] : NULL; + } + else { + return $this->info; + } + } +//===========================================================================}}} +//{{{ getError +/** + * Возвращает последнюю ошибку запроса + * @since 14.10.2010 12:58:23 + * @author megaplan + * @return string + */ + public function getError() + { + return $this->error; + } +//===========================================================================}}} + +} +/*============================================================================* + * END OF SdfApi_Request * + *=========================================================================}}}*/ diff --git a/mod_simplecallback/crm/megaplan/RequestInfo.php b/mod_simplecallback/crm/megaplan/RequestInfo.php new file mode 100644 index 0000000..aa8495e --- /dev/null +++ b/mod_simplecallback/crm/megaplan/RequestInfo.php @@ -0,0 +1,89 @@ + $Method, + 'Host' => $Host, + 'Uri' => $Uri + ); + + // фильтруем заголовки + $validHeaders = array_intersect_key( $Headers, array_flip( self::$acceptedHeaders ) ); + $params = array_merge( $params, $validHeaders ); + + $request = new self( $params ); + + return $request; + } +//===========================================================================}}} +//{{{ __construct +/** + * Создает объект + * @since 01.04.2010 13:59 + * @author megaplan + * @param array $Params Параметры запроса + */ + protected function __construct( array $Params ) + { + $this->params = $Params; + } +//===========================================================================}}} +//{{{ __get +/** + * Возвращает параметры запроса + * @since 01.04.2010 14:26 + * @author megaplan + * @param string $Name + * @return string + */ + public function __get( $Name ) + { + $Name = preg_replace( "/([a-z]{1})([A-Z]{1})/u", '$1-$2', $Name ); + + if ( ! empty( $this->params[$Name] ) ) { + return $this->params[$Name]; + } + else { + return ''; + } + } +//===========================================================================}}} + +} +/*============================================================================* + * END OF SdfApi_RequestInfo * + *=========================================================================}}}*/ \ No newline at end of file diff --git a/mod_simplecallback/crm/megaplan/index.html b/mod_simplecallback/crm/megaplan/index.html new file mode 100644 index 0000000..fa6d84e --- /dev/null +++ b/mod_simplecallback/crm/megaplan/index.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/mod_simplecallback/helper.php b/mod_simplecallback/helper.php index 67162a4..cdb2e8f 100644 --- a/mod_simplecallback/helper.php +++ b/mod_simplecallback/helper.php @@ -130,6 +130,15 @@ public static function getAjax() $amocrm_crm_custom_fields_task_type = (int)$params->get('simplecallback_amocrm_crm_custom_fields_task_type'); $amocrm_crm_custom_fields_complete_till = (int)$params->get('simplecallback_amocrm_crm_custom_fields_complete_till'); + $megaplan_enabled = $params->get('simplecallback_megaplan_enabled'); + $megaplan_host = $params->get('simplecallback_megaplan_host'); + $megaplan_login = $params->get('simplecallback_megaplan_login'); + $megaplan_password = $params->get('simplecallback_megaplan_password'); + $megaplan_deadline = $params->get('simplecallback_megaplan_deadline'); + $megaplan_responsible = $params->get('simplecallback_megaplan_responsible'); + $megaplan_severity = $params->get('simplecallback_megaplan_severity'); + $megaplan_middleName = $params->get('simplecallback_megaplan_middleName'); + $vk_access_token = $params->get('simplecallback_vk_access_token'); $vk_group_id = $params->get('simplecallback_vk_group_id'); $vk_topic_id = $params->get('simplecallback_vk_topic_id'); @@ -576,6 +585,61 @@ function vkpush($method, $post = false) { } + if ($megaplan_enabled === '1') { + + require_once dirname(__FILE__) . '/crm/megaplan/Request.php'; + + // Авторизуемся в Мегаплане + $request = new SdfApi_Request( '', '', $megaplan_host, true ); + $response = json_decode( + $request->get( + '/BumsCommonApiV01/User/authorize.api', + array( + 'Login' => $megaplan_login, + 'Password' => md5( $megaplan_password ) + ) + ) + ); + + // Получаем AccessId и SecretKey + $accessId = $response->data->AccessId; + $secretKey = $response->data->SecretKey; + + // Переподключаемся с полученными AccessId и SecretKey + unset( $request ); + $request = new SdfApi_Request( $accessId, $secretKey, $megaplan_host, true ); + + // Создаем задачу + $raw = $request->post('/BumsCrmApiV01/Contractor/save.api',array( + 'Model[FirstName]' => $name, + 'Model[TypePerson]' => "human", + 'Model[LastName]' => " ", + 'Model[MiddleName]' => $megaplan_middleName, + 'Model[Email]' => $emailclient, + 'Model[Phones]' => array("ph_m{$phone}\tSimpleCallback"), + 'Model[Responsible]' => $megaplan_responsible, + ) ); + + // Чистый форматированный JSON (ответ сервера с ID созданной задачи) + $queryContact = json_decode($raw, true); + + $NewCustomerId = $queryContact['data']['contractor']['Id']; + + $dateDeadline = date('d.m.Y', strtotime($megaplan_deadline)); + + $raw = $request->post('/BumsTaskApiV01/Task/create.api',array( + 'Model[Name]' => $subject, + 'Model[DeadlineDate]' => $dateDeadline, + 'Model[Responsible]' => $megaplan_responsible, + 'Model[Customer]' => $NewCustomerId, + 'Model[Severity]' => $megaplan_severity, + 'Model[Statement]' => $message, + ) ); + + // Чистый форматированный JSON (ответ сервера с ID созданной задачи) + $query = json_decode($raw, true); + + } echo json_encode(array( 'success' => true, 'error' => false, diff --git a/mod_simplecallback/mod_simplecallback.xml b/mod_simplecallback/mod_simplecallback.xml index 76aefec..acab8c7 100644 --- a/mod_simplecallback/mod_simplecallback.xml +++ b/mod_simplecallback/mod_simplecallback.xml @@ -4,7 +4,7 @@ Eugene Kopylov cb9toiiia@gmail.com https://github.com/CB9TOIIIA/simplecallback_fork_cb9t - 1.2.5 + 1.3 24.02.2015 Для настройки и добавление новых возможностей (мои контакты): Skype: cb9t.ru / Telegram: @CB9TOIIIA / Почта: cb9toiiia@gmail.com
]]> @@ -19,6 +19,7 @@ README.md language lib + crm css @@ -794,6 +795,66 @@ class="text" label="<b> <a href="https://seosm.ru/" target="_blank" > Заказать интеграцию (или доработать модуль) </a> </b> "/> + + + + + + + + + + + + + + + + + + + mod_simplecallback
mod_simplecallback module - 1.2.5 + 1.3 https://github.com/CB9TOIIIA/simplecallback_fork_cb9t/ - + stable