$ composer require "mesilov/bitrix24-php-sdk: ^0.2.0" "defuse/php-encryption: ^2.0"
<?php /** * . * * 24 */ error_reporting(E_ALL & ~E_NOTICE); require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/lib.php'; if (null === $_REQUEST['DOMAIN'] || null === $_REQUEST['member_id'] || null === $_REQUEST['AUTH_ID'] || null === $_REQUEST['REFRESH_ID']) { die(' 24'); } $params = AddMessageToBitrix24Task::load(); if (0 === count($params)) { $params = [ // ( ) 'B24_APPLICATION_ID' => '<CLIENT_ID>', // ( ) 'B24_APPLICATION_SECRET' => '<CLIENT_SECRET>', // ( ) 'B24_APPLICATION_SCOPE' => ['task'], //URL ( ) 'B24_REDIRECT_URI' => 'https://<APP_DOMAIN>/app.php', // 'DOMAIN' => $_REQUEST['DOMAIN'], // 'MEMBER_ID' => $_REQUEST['member_id'], // 'AUTH_ID' => $_REQUEST['AUTH_ID'], // 'REFRESH_ID' => $_REQUEST['REFRESH_ID'], ]; // AddMessageToBitrix24Task::save($params); } //, if (AddMessageToBitrix24Task::check()) { // 24 $params = AddMessageToBitrix24Task::load(); $result = ' .<br>'; $result .= ' hook.php :<br>'; $result .= $params['KEY']; } else { $result = ' c .<br>'; } die($result);
<?php /** * */ error_reporting(E_ALL & ~E_NOTICE); require __DIR__ . '/vendor/autoload.php'; require __DIR__ . '/lib.php'; // $params = AddMessageToBitrix24Task::load(); try { // $key = $params['B24_APPLICATION_ID'] . $params['MEMBER_ID'] . $params['B24_APPLICATION_SECRET']; // if (AddMessageToBitrix24Task::decrypt($_REQUEST['key']) !== $key) { die(' '); } } catch (Exception $e) { die(' '); } if (!is_numeric($_REQUEST['task'])) { die(' '); } if (null === $_REQUEST['message'] || '' === trim($_REQUEST['message'])) { die(' '); } try { // Bitrix24 $bx24 = AddMessageToBitrix24Task::getBX24Instance($params); // $result = AddMessageToBitrix24Task::add($bx24, $_REQUEST['task'], $_REQUEST['message']); die($result); } catch (Exception $e) { die(' '); }
<?php /** * */ use Defuse\Crypto\Crypto; use Defuse\Crypto\Key; /** * 24 * * Class AddMessageToBitrix24Task */ class AddMessageToBitrix24Task { /** * @var string . . */ private static $config = __DIR__ . '/../bx24.auth'; /** * @var string */ private static $safeKey; /** * * * @param string $var * * @return string * * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public static function encrypt($var) { return Crypto::encrypt($var, self::getKey()); } /** * * * @param string $var * * @return string * * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public static function decrypt($var) { return Crypto::decrypt($var, self::getKey()); } /** * * * @return Key * * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public static function getKey() { if (null === self::$safeKey) { $params = self::load(); // - if (null === $params || null === $params['PRIVATE_KEY']) { self::$safeKey = Key::createNewRandomKey()->saveToAsciiSafeString(); } else { self::$safeKey = $params['PRIVATE_KEY']; } } return Key::loadFromAsciiSafeString(self::$safeKey); } /** * Bitrix24 * * @param array $params 24 * * @return \Bitrix24\Bitrix24 24 * * @throws \Bitrix24\Bitrix24Exception * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public static function getBX24Instance(array $params) { $bx24 = new \Bitrix24\Bitrix24(false); $bx24->setApplicationScope($params['B24_APPLICATION_SCOPE']); $bx24->setApplicationId($params['B24_APPLICATION_ID']); $bx24->setApplicationSecret($params['B24_APPLICATION_SECRET']); $bx24->setRedirectUri($params['B24_REDIRECT_URI']); $bx24->setDomain($params['DOMAIN']); $bx24->setMemberId($params['MEMBER_ID']); $bx24->setAccessToken($params['AUTH_ID']); $bx24->setRefreshToken($params['REFRESH_ID']); // if ($bx24->isAccessTokenExpire()) { // $temp = $bx24->getNewAccessToken(); // $params['AUTH_ID'] = $temp['access_token']; $params['REFRESH_ID'] = $temp['refresh_token']; $bx24->setAccessToken($params['AUTH_ID']); $bx24->setRefreshToken($params['REFRESH_ID']); // self::save($params); } return $bx24; } /** * * * @param \Bitrix24\Bitrix24 $bx24 24 * @param int $task * @param string $message * * @return string */ public static function add(\Bitrix24\Bitrix24 $bx24, $task, $message) { $str = ''; try { // $bx24->call( 'task.item.getdata', [ 'TASKID' => $task ] ); $str .= ' #' . $task . ' ' . $bx24->getDomain() . ' ' . PHP_EOL; // $bx24->call( 'task.commentitem.add', [ 'TASKID' => $task, 'FIELDS' => [ 'POST_MESSAGE' => $message ] ] ); $str .= ' '; } catch (Exception $e) { $str .= ' '; } return $str; } /** * * * @param array $params * * @return bool * * @throws \Defuse\Crypto\Exception\BadFormatException * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException */ public static function save(array $params) { // $params['KEY'] = AddMessageToBitrix24Task::encrypt($params['B24_APPLICATION_ID'] . $params['MEMBER_ID'] . $params['B24_APPLICATION_SECRET']); // $params['PRIVATE_KEY'] = self::$safeKey; // $result = json_encode($params, JSON_UNESCAPED_UNICODE); return file_put_contents(self::$config, $result) > 0; } /** * * * @return array */ public static function load() { if (!file_exists(self::$config)) { return []; } // $params = file_get_contents(self::$config); return json_decode($params, true); } /** * , 24. * * @return bool */ public static function check() { try { $params = AddMessageToBitrix24Task::load(); $bx24 = self::getBX24Instance($params); $result = $bx24->call('app.info'); return $result['result']['CODE'] === $params['B24_APPLICATION_ID']; } catch (\Exception $e) { return false; } } }
<?php /** * hg commit */ // 24 define('BX24_APP_URL', 'https://<APP_DOMAIN>/app.php'); // . define('KEY', '<ACCESS_KEY>'); echo ' Mercurial, ' . PHP_EOL; if(!function_exists('shell_exec')){ echo ': «shell_exec» ' . PHP_EOL; echo ' ' . PHP_EOL; exit(0); } if(!function_exists('curl_exec')){ echo ': «curl_exec» ' . PHP_EOL; echo ' ' . PHP_EOL; exit(0); } // Mercurial $hg = $_SERVER['HG']; if (!is_file($hg) || !is_executable($hg)) { echo ': Mercurial' . PHP_EOL; echo ' ' . PHP_EOL; exit(0); } echo ' ' . PHP_EOL; // , Mercurial $hostname = trim(shell_exec('hostname -f')); // $pwd = $_SERVER['PWD']; // Mercurial $branch = shell_exec("$hg branch"); // $log = trim(shell_exec("$hg log -l 1")); // $matches = null; $user = preg_match('/user:\s+(?<user>\S.*)/ium', $log, $matches) ? $matches['user'] : 'unknown'; // $summary = preg_match('/summary:\s+(?<summary>\S.*)/ium', $log, $matches) ? $matches['summary'] : ''; // $files = trim(shell_exec("$hg st -amr")); // $filesCount = substr_count($files, PHP_EOL); // Mercurial echo ' ' . PHP_EOL; $task = 0; if (preg_match('/^task[#\@\$](?<id>\d+)/iu', $branch, $matches)) { $task = (int)$matches['id']; } elseif (preg_match('/^task[#\@\$](?<id>\d+)/iu', $summary, $matches)) { $task = (int)$matches['id']; } // , if ($task <= 0) { echo ' ' . PHP_EOL; echo ' Enter, : '; $count = fscanf(STDIN, "%d\n", $task); if ($count <= 0) { echo ' .' . PHP_EOL; echo ' ' . PHP_EOL; echo ' ' . PHP_EOL; exit(0); } } echo ' ' . PHP_EOL; $message = <<<EOT $user $pwd $hostname: $summary ====== : $log ( $filesCount ): $files . EOT; // $postData = [ 'message' => $message, 'task' => $task, 'key' => KEY ]; // $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, BX24_APP_URL); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); $result = curl_exec($ch); echo $result . PHP_EOL; echo ' ' . PHP_EOL;
[hooks] commit = php -f /home/bitrix/hghook-commit-to-bx24.php
. hghook-commit-to-bx24.php : <ACCESS_KEY>
Header set X-Frame-Options SAMEORIGIN
Header unset X-Frame-Options
[bitrix@dhcppc5 www]$ hg st M composer.json [bitrix@dhcppc5 www]$ hg ci -u testuser -m 'task#62 #1 : - -' Mercurial, #62 <PORTAL_NAME>.bitrix24.ru [bitrix@dhcppc5 www]$
[bitrix@dhcppc5 www]$ hg ci -u testuser -m ' #2 : - -' Mercurial, Enter, : 62 #62 <PORTAL_NAME>.bitrix24.ru [bitrix@dhcppc5 www]$
[bitrix@dhcppc5 www]$ hg ci -u testuser -m ' #3 : - -' Mercurial, Enter, : [bitrix@dhcppc5 www]$
[bitrix@dhcppc5 www]$ hg ci -u testuser -m 'task$65445642 #3 : - -' Mercurial, [bitrix@dhcppc5 www]$
Source: https://habr.com/ru/post/303718/
All Articles