MadelineProto/src/danog/MadelineProto/TL/Conversion/BotAPI.php

758 lines
33 KiB
PHP
Raw Normal View History

<?php
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
/**
* BotAPI module.
*
* This file is part of MadelineProto.
* MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
* You should have received a copy of the GNU General Public License along with MadelineProto.
* If not, see <http://www.gnu.org/licenses/>.
*
* @author Daniil Gentili <daniil@daniil.it>
2020-02-17 14:13:46 +01:00
* @copyright 2016-2020 Daniil Gentili <daniil@daniil.it>
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
* @license https://opensource.org/licenses/AGPL-3.0 AGPLv3
*
2019-10-31 15:07:35 +01:00
* @link https://docs.madelineproto.xyz MadelineProto documentation
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
*/
2018-02-24 17:54:39 +01:00
namespace danog\MadelineProto\TL\Conversion;
2020-02-09 16:24:57 +01:00
use danog\Decoder\FileId;
2019-05-31 17:43:49 +02:00
use danog\MadelineProto\Logger;
2020-02-12 18:16:08 +01:00
use danog\MadelineProto\MTProtoTools\PeerHandler;
2019-05-31 17:43:49 +02:00
2020-02-09 16:24:57 +01:00
use const danog\Decoder\TYPES_IDS;
trait BotAPI
{
2019-12-28 19:00:19 +01:00
private function htmlEntityDecode(string $stuff): string
{
2019-09-02 17:08:36 +02:00
return \html_entity_decode(\preg_replace('#< *br */? *>#', "\n", $stuff));
}
/**
* Get Telegram UTF-8 length of string.
*
* @param string $text Text
*
* @return int
*/
2019-12-28 16:45:00 +01:00
public static function mbStrlen(string $text): int
2018-03-03 17:56:23 +01:00
{
2018-03-03 17:55:45 +01:00
$length = 0;
2019-09-02 17:08:36 +02:00
$textlength = \strlen($text);
for ($x = 0; $x < $textlength; $x++) {
2019-09-02 17:08:36 +02:00
$char = \ord($text[$x]);
2020-01-31 19:29:43 +01:00
if (($char & 0xc0) != 0x80) {
2018-03-03 17:55:45 +01:00
$length += 1 + ($char >= 0xf0);
}
}
return $length;
}
/**
* Telegram UTF-8 multibyte substring.
*
* @param string $text Text to substring
* @param integer $offset Offset
* @param ?int $length Length
*
* @return string
*/
2019-12-28 16:45:00 +01:00
public static function mbSubstr(string $text, int $offset, $length = null): string
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
{
2019-12-28 16:45:00 +01:00
$mb_text_length = self::mbStrlen($text);
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
if ($offset < 0) {
$offset = $mb_text_length + $offset;
}
if ($length < 0) {
2020-01-31 19:29:43 +01:00
$length = $mb_text_length - $offset + $length;
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
} elseif ($length === null) {
$length = $mb_text_length - $offset;
}
$new_text = '';
$current_offset = 0;
$current_length = 0;
2019-09-02 17:08:36 +02:00
$text_length = \strlen($text);
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
for ($x = 0; $x < $text_length; $x++) {
2019-09-02 17:08:36 +02:00
$char = \ord($text[$x]);
2020-01-31 19:29:43 +01:00
if (($char & 0xc0) != 0x80) {
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
$current_offset += 1 + ($char >= 0xf0);
if ($current_offset > $offset) {
$current_length += 1 + ($char >= 0xf0);
}
}
if ($current_offset > $offset) {
if ($current_length <= $length) {
$new_text .= $text[$x];
}
}
}
return $new_text;
}
/**
* Telegram UTF-8 multibyte split.
*
* @param string $text Text
* @param integer $length Length
*
2019-12-28 19:00:19 +01:00
* @return array
*/
2019-12-28 19:00:19 +01:00
public static function mbStrSplit(string $text, int $length): array
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
{
2019-09-02 17:08:36 +02:00
$tlength = \mb_strlen($text, 'UTF-8');
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
$result = [];
for ($x = 0; $x < $tlength; $x += $length) {
2019-09-02 17:08:36 +02:00
$result[] = \mb_substr($text, $x, $length, 'UTF-8');
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
}
return $result;
}
private function parseButtons($rows)
{
$newrows = [];
$key = 0;
$button_key = 0;
foreach ($rows as $row) {
$newrows[$key] = ['_' => 'keyboardButtonRow', 'buttons' => []];
foreach ($row as $button) {
$newrows[$key]['buttons'][$button_key] = ['_' => 'keyboardButton', 'text' => $button['text']];
if (isset($button['url'])) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonUrl';
$newrows[$key]['buttons'][$button_key]['url'] = $button['url'];
} elseif (isset($button['callback_data'])) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonCallback';
$newrows[$key]['buttons'][$button_key]['data'] = $button['callback_data'];
} elseif (isset($button['switch_inline_query'])) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonSwitchInline';
$newrows[$key]['buttons'][$button_key]['same_peer'] = false;
$newrows[$key]['buttons'][$button_key]['query'] = $button['switch_inline_query'];
} elseif (isset($button['switch_inline_query_current_chat'])) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonSwitchInline';
$newrows[$key]['buttons'][$button_key]['same_peer'] = true;
$newrows[$key]['buttons'][$button_key]['query'] = $button['switch_inline_query_current_chat'];
} elseif (isset($button['callback_game'])) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonGame';
$newrows[$key]['buttons'][$button_key]['text'] = $button['callback_game'];
} elseif (isset($button['request_contact']) && $button['request_contact']) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonRequestPhone';
} elseif (isset($button['request_location']) && $button['request_location']) {
$newrows[$key]['buttons'][$button_key]['_'] = 'keyboardButtonRequestGeoLocation';
}
$button_key++;
}
$key++;
}
return $newrows;
}
private function parseReplyMarkup($markup)
{
if (isset($markup['force_reply']) && $markup['force_reply']) {
$markup['_'] = 'replyKeyboardForceReply';
unset($markup['force_reply']);
}
if (isset($markup['remove_keyboard']) && $markup['remove_keyboard']) {
$markup['_'] = 'replyKeyboardHide';
unset($markup['remove_keyboard']);
}
if (isset($markup['keyboard'])) {
$markup['_'] = 'replyKeyboardMarkup';
if (isset($markup['resize_keyboard'])) {
$markup['resize'] = $markup['resize_keyboard'];
unset($markup['resize_keyboard']);
}
if (isset($markup['one_time_keyboard'])) {
$markup['single_use'] = $markup['one_time_keyboard'];
unset($markup['one_time_keyboard']);
}
2019-10-29 21:33:23 +01:00
$markup['rows'] = $this->parseButtons($markup['keyboard']);
unset($markup['keyboard']);
}
if (isset($markup['inline_keyboard'])) {
$markup['_'] = 'replyInlineMarkup';
2019-10-29 21:33:23 +01:00
$markup['rows'] = $this->parseButtons($markup['inline_keyboard']);
unset($markup['inline_keyboard']);
}
return $markup;
}
/**
* Convert MTProto parameters to bot API parameters.
*
2020-02-09 16:24:57 +01:00
* @param array $data Data
*
* @return \Generator<array>
*/
2020-02-09 16:24:57 +01:00
public function MTProtoToBotAPI(array $data): \Generator
{
$newd = [];
if (!isset($data['_'])) {
foreach ($data as $key => $element) {
2020-02-09 16:24:57 +01:00
$newd[$key] = (yield from $this->MTProtoToBotAPI($element));
}
return $newd;
}
switch ($data['_']) {
case 'updateShortSentMessage':
$newd['message_id'] = $data['id'];
$newd['date'] = $data['date'];
$newd['text'] = $data['request']['body']['message'];
if ($data['out']) {
2020-01-31 19:49:58 +01:00
$newd['from'] = (yield from $this->getPwrChat($this->authorization['user']));
}
$newd['chat'] = yield from $this->getPwrChat($data['request']['body']['peer']);
if (isset($data['entities'])) {
2020-02-09 16:24:57 +01:00
$newd['entities'] = yield from $this->MTProtoToBotAPI($data['entities']);
}
if (isset($data['media'])) {
2020-02-09 16:24:57 +01:00
$newd += yield from $this->MTProtoToBotAPI($data['media']);
}
return $newd;
2020-02-09 16:24:57 +01:00
case 'updates':
$data = \array_values(\array_filter($data['updates'], fn (array $update) => $update['_'] !== 'updateMessageID'))[0];
// no break
case 'updateNewChannelMessage':
case 'updateNewMessage':
2020-01-31 19:29:43 +01:00
return yield from $this->MTProtoToBotAPI($data['message']);
case 'message':
$newd['message_id'] = $data['id'];
$newd['date'] = $data['date'];
$newd['text'] = $data['message'];
$newd['post'] = $data['post'];
$newd['silent'] = $data['silent'];
if (isset($data['from_id'])) {
2020-01-31 19:49:58 +01:00
$newd['from'] = (yield from $this->getPwrChat($data['from_id']));
}
2020-10-01 18:02:54 +02:00
$newd['chat'] = (yield from $this->getPwrChat($data['peer_id']));
if (isset($data['entities'])) {
2020-02-09 16:24:57 +01:00
$newd['entities'] = (yield from $this->MTProtoToBotAPI($data['entities']));
}
if (isset($data['views'])) {
$newd['views'] = $data['views'];
}
if (isset($data['edit_date'])) {
$newd['edit_date'] = $data['edit_date'];
}
if (isset($data['via_bot_id'])) {
2020-01-31 19:49:58 +01:00
$newd['via_bot'] = (yield from $this->getPwrChat($data['via_bot_id']));
}
if (isset($data['fwd_from']['from_id'])) {
2020-01-31 19:49:58 +01:00
$newd['forward_from'] = (yield from $this->getPwrChat($data['fwd_from']['from_id']));
}
if (isset($data['fwd_from']['channel_id'])) {
2020-02-12 18:16:08 +01:00
try {
$newd['forward_from_chat'] = yield from $this->getPwrChat(PeerHandler::toSupergroup($data['fwd_from']['channel_id']));
} catch (\Throwable $e) {
}
}
if (isset($data['fwd_from']['date'])) {
$newd['forward_date'] = $data['fwd_from']['date'];
}
if (isset($data['fwd_from']['channel_post'])) {
$newd['forward_from_message_id'] = $data['fwd_from']['channel_post'];
}
if (isset($data['media'])) {
2020-02-09 16:24:57 +01:00
$newd = \array_merge($newd, yield from $this->MTProtoToBotAPI($data['media']));
}
return $newd;
case 'messageEntityMention':
unset($data['_']);
$data['type'] = 'mention';
return $data;
case 'messageEntityHashtag':
unset($data['_']);
$data['type'] = 'hashtag';
return $data;
case 'messageEntityBotCommand':
unset($data['_']);
$data['type'] = 'bot_command';
return $data;
case 'messageEntityUrl':
unset($data['_']);
$data['type'] = 'url';
return $data;
case 'messageEntityEmail':
unset($data['_']);
$data['type'] = 'email';
return $data;
case 'messageEntityBold':
unset($data['_']);
$data['type'] = 'bold';
return $data;
case 'messageEntityItalic':
unset($data['_']);
$data['type'] = 'italic';
return $data;
case 'messageEntityCode':
unset($data['_']);
$data['type'] = 'code';
return $data;
case 'messageEntityPre':
unset($data['_']);
$data['type'] = 'pre';
return $data;
case 'messageEntityTextUrl':
unset($data['_']);
$data['type'] = 'text_url';
return $data;
case 'messageEntityMentionName':
unset($data['_']);
$data['type'] = 'text_mention';
2020-01-31 19:49:58 +01:00
$data['user'] = (yield from $this->getPwrChat($data['user_id']));
unset($data['user_id']);
return $data;
case 'messageMediaPhoto':
if (isset($data['caption'])) {
$res['caption'] = $data['caption'];
}
$res['photo'] = [];
foreach ($data['photo']['sizes'] as $key => $photo) {
2019-09-02 17:08:36 +02:00
if (\in_array($photo['_'], ['photoCachedSize', 'photoSize'])) {
2020-02-09 16:24:57 +01:00
$res['photo'][$key] = $this->photosizeToBotAPI($photo, $data['photo']);
2019-06-01 13:13:59 +02:00
}
}
return $res;
case 'messageMediaEmpty':
return [];
case 'messageMediaDocument':
$type_name = 'document';
$res = [];
2019-09-02 17:08:36 +02:00
if (isset($data['document']['thumbs']) && $data['document']['thumbs'] && \in_array(\end($data['document']['thumbs'])['_'], ['photoCachedSize', 'photoSize'])) {
2020-02-09 16:24:57 +01:00
$res['thumb'] = $this->photosizeToBotAPI(\end($data['document']['thumbs']), $data['document'], true);
}
foreach ($data['document']['attributes'] as $attribute) {
switch ($attribute['_']) {
case 'documentAttributeFilename':
2019-09-02 17:08:36 +02:00
$pathinfo = \pathinfo($attribute['file_name']);
2020-02-09 16:24:57 +01:00
$res['ext'] = isset($pathinfo['extension']) ? '.'.$pathinfo['extension'] : '';
$res['file_name'] = $pathinfo['filename'];
break;
case 'documentAttributeAudio':
$audio = $attribute;
2020-02-09 16:24:57 +01:00
$type_name = $attribute['voice'] ? 'voice' :'audio';
$res['duration'] = $attribute['duration'];
if (isset($attribute['performer'])) {
$res['performer'] = $attribute['performer'];
}
if (isset($attribute['title'])) {
$res['title'] = $attribute['title'];
}
if (isset($attribute['waveform'])) {
$res['title'] = $attribute['waveform'];
}
break;
case 'documentAttributeVideo':
$type_name = $attribute['round_message'] ? 'video_note' : 'video';
$res['width'] = $attribute['w'];
$res['height'] = $attribute['h'];
$res['duration'] = $attribute['duration'];
break;
case 'documentAttributeImageSize':
$res['width'] = $attribute['w'];
$res['height'] = $attribute['h'];
break;
case 'documentAttributeAnimated':
2020-02-09 16:24:57 +01:00
$type_name = 'animation';
$res['animated'] = true;
break;
case 'documentAttributeHasStickers':
$res['has_stickers'] = true;
break;
case 'documentAttributeSticker':
$type_name = 'sticker';
$res['mask'] = $attribute['mask'];
$res['emoji'] = $attribute['alt'];
$res['sticker_set'] = $attribute['stickerset'];
if (isset($attribute['mask_coords'])) {
$res['mask_coords'] = $attribute['mask_coords'];
}
break;
}
}
if (isset($audio) && isset($audio['title']) && !isset($res['file_name'])) {
$res['file_name'] = $audio['title'];
if (isset($audio['performer'])) {
2020-02-09 16:24:57 +01:00
$res['file_name'] .= ' - '.$audio['performer'];
}
}
if (!isset($res['file_name'])) {
$res['file_name'] = $data['document']['access_hash'];
}
2020-02-09 16:24:57 +01:00
$res['file_name'] .= '_'.$data['document']['id'];
if (isset($res['ext'])) {
$res['file_name'] .= $res['ext'];
unset($res['ext']);
} else {
2019-10-29 21:33:23 +01:00
$res['file_name'] .= $this->getExtensionFromMime($data['document']['mime_type']);
}
$res['file_size'] = $data['document']['size'];
$res['mime_type'] = $data['document']['mime_type'];
2020-02-09 16:24:57 +01:00
$fileId = new FileId;
$fileId->setId($data['document']['id']);
$fileId->setAccessHash($data['document']['access_hash']);
$fileId->setFileReference($data['document']['file_reference'] ?? '');
$fileId->setDcId($data['document']['dc_id']);
$fileId->setType(TYPES_IDS[$type_name]);
$res['file_id'] = (string) $fileId;
$res['file_unique_id'] = $fileId->getUniqueBotAPI();
return [$type_name => $res, 'caption' => $data['caption'] ?? ''];
default:
2019-09-02 17:08:36 +02:00
throw new Exception(\sprintf(\danog\MadelineProto\Lang::$current_lang['botapi_conversion_error'], $data['_']));
}
}
/**
* Convert bot API parameters to MTProto parameters.
*
* @param array $arguments Arguments
*
* @return \Generator<array>
*/
public function botAPIToMTProto(array $arguments): \Generator
{
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
foreach (self::BOTAPI_PARAMS_CONVERSION as $bot => $mtproto) {
if (isset($arguments[$bot]) && !isset($arguments[$mtproto])) {
$arguments[$mtproto] = $arguments[$bot];
//unset($arguments[$bot]);
}
}
if (isset($arguments['reply_markup'])) {
2019-10-29 21:33:23 +01:00
$arguments['reply_markup'] = $this->parseReplyMarkup($arguments['reply_markup']);
}
if (isset($arguments['parse_mode'])) {
2020-01-31 19:29:43 +01:00
$arguments = (yield from $this->parseMode($arguments));
}
return $arguments;
}
2020-01-31 19:29:43 +01:00
private function parseNode($node, &$entities, &$new_message, &$offset): \Generator
{
switch ($node->nodeName) {
case 'br':
$new_message .= "\n";
$offset++;
break;
2019-06-24 15:46:38 +02:00
case 's':
case 'strike':
case 'del':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
2019-06-24 15:46:38 +02:00
$entities[] = ['_' => 'messageEntityStrike', 'offset' => $offset, 'length' => $length];
$new_message .= $text;
$offset += $length;
break;
case 'u':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
2019-06-24 15:46:38 +02:00
$entities[] = ['_' => 'messageEntityUnderline', 'offset' => $offset, 'length' => $length];
$new_message .= $text;
$offset += $length;
break;
case 'blockquote':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
2019-06-24 15:46:38 +02:00
$entities[] = ['_' => 'messageEntityBlockquote', 'offset' => $offset, 'length' => $length];
$new_message .= $text;
$offset += $length;
break;
case 'b':
case 'strong':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
$entities[] = ['_' => 'messageEntityBold', 'offset' => $offset, 'length' => $length];
$new_message .= $text;
$offset += $length;
break;
case 'i':
case 'em':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
$entities[] = ['_' => 'messageEntityItalic', 'offset' => $offset, 'length' => $length];
$new_message .= $text;
$offset += $length;
break;
case 'code':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
$entities[] = ['_' => 'messageEntityCode', 'offset' => $offset, 'length' => $length];
$new_message .= $text;
$offset += $length;
break;
case 'pre':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
$language = $node->getAttribute('language');
if ($language === null) {
$language = '';
}
$entities[] = ['_' => 'messageEntityPre', 'offset' => $offset, 'length' => $length, 'language' => $language];
$new_message .= $text;
$offset += $length;
break;
case 'p':
foreach ($node->childNodes as $node) {
2020-01-31 19:29:43 +01:00
yield from $this->parseNode($node, $entities, $new_message, $offset);
2017-05-19 21:34:57 +02:00
}
break;
case 'a':
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
$href = $node->getAttribute('href');
2020-01-31 19:29:43 +01:00
if (\preg_match('|mention:(.*)|', $href, $matches) || \preg_match('|tg://user\\?id=(.*)|', $href, $matches)) {
2020-01-31 19:49:58 +01:00
$mention = (yield from $this->getInfo($matches[1]));
if (!isset($mention['InputUser'])) {
throw new \danog\MadelineProto\Exception(\danog\MadelineProto\Lang::$current_lang['peer_not_in_db']);
}
$entities[] = ['_' => 'inputMessageEntityMentionName', 'offset' => $offset, 'length' => $length, 'user_id' => $mention['InputUser']];
2019-09-02 17:08:36 +02:00
} elseif (\preg_match('|buttonurl:(.*)|', $href)) {
if (!isset($entities['buttons'])) {
$entities['buttons'] = [];
}
2019-09-02 17:08:36 +02:00
if (\strpos(\substr($href, -4), '|:new|') !== false) {
$entities['buttons'][] = ['_' => 'keyboardButtonUrl', 'text' => $text, 'url' => \str_replace(['buttonurl:', ':new'], '', $href), 'new' => true];
} else {
2019-09-02 17:08:36 +02:00
$entities['buttons'][] = ['_' => 'keyboardButtonUrl', 'text' => $text, 'url' => \str_replace('buttonurl:', '', $href)];
}
break;
2017-05-19 21:34:57 +02:00
} else {
$entities[] = ['_' => 'messageEntityTextUrl', 'offset' => $offset, 'length' => $length, 'url' => $href];
2017-05-19 21:34:57 +02:00
}
$new_message .= $text;
$offset += $length;
2017-05-19 21:34:57 +02:00
break;
default:
2019-10-29 21:33:23 +01:00
$text = $this->htmlEntityDecode($node->textContent);
$length = $this->mbStrlen($text);
$new_message .= $text;
$offset += $length;
break;
}
}
2019-12-28 18:44:56 +01:00
/**
2019-12-28 19:16:02 +01:00
* Convert markdown and HTML messages.
2019-12-28 18:44:56 +01:00
*
* @param array $arguments Arguments
*
* @internal
*
2019-12-28 18:53:37 +01:00
* @return \Generator<array>
2019-12-28 18:44:56 +01:00
*/
2019-12-28 18:53:37 +01:00
public function parseMode(array $arguments): \Generator
{
2020-01-16 20:23:39 +01:00
if (($arguments['message'] ?? '') === '' || !isset($arguments['parse_mode'])) {
2018-07-09 21:45:09 +02:00
return $arguments;
}
if (!(\is_string($arguments['message']) || \is_object($arguments['message']) && \method_exists($arguments['message'], '__toString'))) {
throw new Exception('Messages can only be strings');
}
if (isset($arguments['parse_mode']['_'])) {
2019-09-02 17:08:36 +02:00
$arguments['parse_mode'] = \str_replace('textParseMode', '', $arguments['parse_mode']['_']);
}
2019-09-02 17:08:36 +02:00
if (\stripos($arguments['parse_mode'], 'markdown') !== false) {
$arguments['message'] = \Parsedown::instance()->line($arguments['message']);
$arguments['parse_mode'] = 'HTML';
}
2019-09-02 17:08:36 +02:00
if (\stripos($arguments['parse_mode'], 'html') !== false) {
$new_message = '';
2019-10-29 21:33:23 +01:00
$arguments['message'] = \trim($this->htmlFixtags($arguments['message']));
2018-03-03 00:22:15 +01:00
$dom = new \DOMDocument();
2019-09-02 17:08:36 +02:00
$dom->loadHTML(\mb_convert_encoding($arguments['message'], 'HTML-ENTITIES', 'UTF-8'));
2018-03-03 00:22:15 +01:00
if (!isset($arguments['entities'])) {
$arguments['entities'] = [];
}
$offset = 0;
2018-03-03 00:22:15 +01:00
foreach ($dom->getElementsByTagName('body')->item(0)->childNodes as $node) {
2020-01-31 19:29:43 +01:00
yield from $this->parseNode($node, $arguments['entities'], $new_message, $offset);
2018-03-03 00:22:15 +01:00
}
if (isset($arguments['entities']['buttons'])) {
2019-10-29 21:33:23 +01:00
$arguments['reply_markup'] = $this->buildRows($arguments['entities']['buttons']);
2018-03-03 00:22:15 +01:00
unset($arguments['entities']['buttons']);
}
unset($arguments['parse_mode']);
$arguments['message'] = $new_message;
}
return $arguments;
}
2019-12-28 18:44:56 +01:00
/**
2019-12-28 19:16:02 +01:00
* Split too long message into chunks.
2019-12-28 18:44:56 +01:00
*
* @param array $args Arguments
*
* @internal
*
* @return \Generator
*/
public function splitToChunks($args): \Generator
2017-04-21 13:14:21 +02:00
{
2020-01-31 19:29:43 +01:00
$args = (yield from $this->parseMode($args));
2018-07-11 16:39:53 +02:00
if (!isset($args['entities'])) {
$args['entities'] = [];
}
Merge alpha into master (async, huge bugfixes and more) (#546) * Implement async and lots of bugfixes * Implement more async * Implement async, implement bugfixes for the connection module, for the datacenter module, huge bugfixes, huge perfomance improvements, media DCs for https, advanced selecting, custom var_dump, totally rewritten IOLoop and response mechanism, promises, improvements to the TL parser, custom mb_substr * Apply fixes from StyleCI * Bugfixes * Apply fixes from StyleCI * Bugfixes, implement combined promises * Apply fixes from StyleCI * Support passing method arguments as callable * Starting to write async upload logic * Apply fixes from StyleCI * Start implementing async file upload * Apply fixes from StyleCI * bugfix * Apply fixes from StyleCI * Start rewriting connection module * Add PHP file docblocks for all classes * Start working on new async stream API * Finish writing stream API * More stream API fixes * Apply fixes from StyleCI * Rewrite DataCenter and Connection modules * Clean up stream API documentation * Fixes * Apply fixes from StyleCI * Add referenced parameter to get length of buffer to read in getReadBuffer API * Moved all MessageHandler code in the Connection module, added a PHP version warning in the phar * Start fixing reads * Fix all protocol stream wrappers * Apply fixes from StyleCI * Implement disconnection, and remove end function * Working async RPC * Implement async file upload * Bugfix * Method recall bugfixes * Bugfixes * Trait bugfixes * Fix FIFO buffer * Bugfixes and speedtests * Async logging * Implement websocket streams * Implement loop API, signal API, clean closing and start changing layer * Small magna, websocket and HTTP fixes * Clean up loop API * Improved stack traces, 2FA and async * Login fixes * Added instructions for manual verification * Small fixes * More app info improvements * More app info improvements * TL and 2FA fixes * Update to layer 89 * More bugfixes * Implement broken media reporting * Remove debug comments * PHP 7.2 backwards compatibility * Bugfixes * Async key generation * Some simplifications * Transport fixes * Cleanup * async API * Performance fixes * Fixes to async API * Bugfixes * Implement one-time async loop * Authorization and logging fixes * Update to layer 91 * 7to5 fix * Null coalesce conversion * Implement socks5 proxy * Implement HTTP proxy * Fixes to HTTP proxy * MTProxy and socks5 fixes * Disable PHP 5 conversion * Proxies have higher priority * Avoid error handling in vendor * Override composer dependencies * Fix travis build * Final composer fixes * Proxy logic fixes * Fix get_updates update handling * Do not use parallel file driver if not supported * Refactor loader and implement HTTP fixes * Suppress errors in loader * HTTP and authorization fixes * HTTP fixes * Improved peer management * Use HTTP protocol on altervista * Small bugfixes * Minor fixes * Docufix * Docufix * Legacy fixes * Fix message queue * Avoid updating if using MTProxy * Improve logs and examples * Trim final newlines while converting parse mode * Reimplement noResponse flag * Async combined event handler and APIFactory fixes * Actually return config * Case-insensitive methods * Bugfix * Apply fixes from StyleCI (#545) * MTProxy fixes * PHP 5 warning * Improved PHP 5 warning * Use <br> along with newlines in web logs * Update docs
2018-12-26 20:51:14 +01:00
$max_length = isset($args['media']) ? $this->config['caption_length_max'] : $this->config['message_length_max'];
2019-05-31 17:43:49 +02:00
$max_entity_length = 100;
$max_entity_size = 8110;
2017-05-20 15:46:05 +02:00
$text_arr = [];
foreach ($this->multipleExplodeKeepDelimiters(["\n"], $args['message']) as $word) {
2019-09-02 17:08:36 +02:00
if (\mb_strlen($word, 'UTF-8') > $max_length) {
2019-10-29 21:33:23 +01:00
foreach ($this->mbStrSplit($word, $max_length) as $vv) {
2017-05-20 15:46:46 +02:00
$text_arr[] = $vv;
}
} else {
$text_arr[] = $word;
}
2017-05-20 15:46:05 +02:00
}
2019-09-02 17:08:36 +02:00
$multiple_args_base = \array_merge($args, ['entities' => [], 'parse_mode' => 'text', 'message' => '']);
2019-05-31 17:43:49 +02:00
$multiple_args = [$multiple_args_base];
2017-04-21 13:14:21 +02:00
$i = 0;
foreach ($text_arr as $word) {
2020-02-09 16:24:57 +01:00
if ($this->mbStrlen($multiple_args[$i]['message'].$word) <= $max_length) {
$multiple_args[$i]['message'] .= $word;
2017-04-21 13:14:21 +02:00
} else {
$i++;
$multiple_args[$i] = $multiple_args_base;
$multiple_args[$i]['message'] .= $word;
2017-04-21 13:14:21 +02:00
}
}
$i = 0;
$offset = 0;
2019-09-02 17:08:36 +02:00
for ($k = 0; $k < \count($args['entities']); $k++) {
2019-05-31 15:31:23 +02:00
$entity = $args['entities'][$k];
do {
2019-10-29 21:33:23 +01:00
while ($entity['offset'] > $offset + $this->mbStrlen($multiple_args[$i]['message'])) {
$offset += $this->mbStrlen($multiple_args[$i]['message']);
$i++;
}
$entity['offset'] -= $offset;
2019-10-29 21:33:23 +01:00
if ($entity['offset'] + $entity['length'] > $this->mbStrlen($multiple_args[$i]['message'])) {
$newentity = $entity;
2019-10-29 21:33:23 +01:00
$newentity['length'] = $entity['length'] - ($this->mbStrlen($multiple_args[$i]['message']) - $entity['offset']);
$entity['length'] = $this->mbStrlen($multiple_args[$i]['message']) - $entity['offset'];
2020-01-31 19:29:43 +01:00
$offset += $entity['length'];
//$this->mbStrlen($multiple_args[$i]['message']);
$newentity['offset'] = $offset;
2019-10-29 21:33:23 +01:00
$prev_length = $this->mbStrlen($multiple_args[$i]['message']);
2019-09-02 17:08:36 +02:00
$multiple_args[$i]['message'] = \rtrim($multiple_args[$i]['message']);
2019-10-29 21:33:23 +01:00
$diff = $prev_length - $this->mbStrlen($multiple_args[$i]['message']);
2019-05-31 15:31:23 +02:00
if ($diff) {
$entity['length'] -= $diff;
foreach ($args['entities'] as $key => &$eentity) {
if ($key > $k) {
$eentity['offset'] -= $diff;
}
}
}
$multiple_args[$i]['entities'][] = $entity;
$i++;
$entity = $newentity;
continue;
2019-09-02 17:08:36 +02:00
}
2019-10-29 21:33:23 +01:00
$prev_length = $this->mbStrlen($multiple_args[$i]['message']);
2019-09-02 17:08:36 +02:00
$multiple_args[$i]['message'] = \rtrim($multiple_args[$i]['message']);
2019-10-29 21:33:23 +01:00
$diff = $prev_length - $this->mbStrlen($multiple_args[$i]['message']);
2019-09-02 17:08:36 +02:00
if ($diff) {
$entity['length'] -= $diff;
foreach ($args['entities'] as $key => &$eentity) {
if ($key > $k) {
$eentity['offset'] -= $diff;
2019-05-31 15:31:23 +02:00
}
}
}
2019-09-02 17:08:36 +02:00
$multiple_args[$i]['entities'][] = $entity;
break;
} while (true);
}
2019-05-31 17:43:49 +02:00
$total = 0;
foreach ($multiple_args as $args) {
2019-09-02 17:08:36 +02:00
if (\count($args['entities']) > $max_entity_length) {
$total += \count($args['entities']) - $max_entity_length;
2019-05-31 17:43:49 +02:00
}
$c = 0;
foreach ($args['entities'] as $entity) {
if (isset($entity['url'])) {
2019-09-02 17:08:36 +02:00
$c += \strlen($entity['url']);
2019-05-31 17:43:49 +02:00
}
}
2019-06-05 21:09:25 +02:00
if ($c >= $max_entity_size) {
2019-06-04 14:55:58 +02:00
$this->logger->logger('Entity size limit possibly exceeded, you may get an error indicating that the entities are too long. Reduce the number of entities and/or size of the URLs used.', Logger::FATAL_ERROR);
2019-05-31 17:43:49 +02:00
}
}
if ($total) {
2020-01-31 19:29:43 +01:00
$this->logger->logger("Too many entities, {$total} entities will be truncated", Logger::FATAL_ERROR);
2019-05-31 17:43:49 +02:00
}
return $multiple_args;
}
private function multipleExplodeKeepDelimiters($delimiters, $string)
2017-04-21 13:14:21 +02:00
{
2019-09-02 17:08:36 +02:00
$initialArray = \explode(\chr(1), \str_replace($delimiters, \chr(1), $string));
2017-04-21 13:14:21 +02:00
$finalArray = [];
2018-02-19 12:48:43 +01:00
$delimOffset = 0;
2017-04-21 13:14:21 +02:00
foreach ($initialArray as $item) {
2019-10-29 21:33:23 +01:00
$delimOffset += $this->mbStrlen($item);
//if ($this->mbStrlen($item) > 0) {
2020-02-09 16:24:57 +01:00
$finalArray[] = $item.($delimOffset < $this->mbStrlen($string) ? $string[$delimOffset] : '');
2018-07-11 16:39:38 +02:00
//}
2018-02-19 12:48:43 +01:00
$delimOffset++;
2017-04-21 13:14:21 +02:00
}
return $finalArray;
}
private function htmlFixtags($text)
{
2019-06-05 21:09:25 +02:00
$diff = 0;
2020-01-31 19:29:43 +01:00
\preg_match_all('#(.*?)(<(\\bu\\b|\\bs\\b|\\ba\\b|\\bb\\b|\\bstrong\\b|\\bblockquote\\b|\\bstrike\\b|\\bdel\\b|\\bem\\b|i|\\bcode\\b|\\bpre\\b)[^>]*>)(.*?)([<]\\s*/\\s*\\3[>])#is', $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);
if ($matches) {
2019-05-31 15:31:23 +02:00
foreach ($matches as $match) {
2019-09-02 17:08:36 +02:00
if (\trim($match[1][0]) != '') {
$mod = \htmlentities($match[1][0]);
$temp = \substr($text, 0, $match[1][1] + $diff);
2019-06-05 21:09:25 +02:00
$temp .= $mod;
2019-09-02 17:08:36 +02:00
$temp .= \substr($text, $match[1][1] + $diff + \strlen($match[1][0]));
$diff += \strlen($mod) - \strlen($match[1][0]);
2019-05-31 15:31:23 +02:00
$text = $temp;
}
2019-09-02 17:08:36 +02:00
$mod = \htmlentities($match[4][0]);
$temp = \substr($text, 0, $match[4][1] + $diff);
2019-06-05 21:09:25 +02:00
$temp .= $mod;
2019-09-02 17:08:36 +02:00
$temp .= \substr($text, $match[4][1] + $diff + \strlen($match[4][0]));
$diff += \strlen($mod) - \strlen($match[4][0]);
2019-05-31 15:31:23 +02:00
$text = $temp;
}
2019-06-05 21:09:25 +02:00
$diff = 0;
2020-01-31 19:29:43 +01:00
\preg_match_all('#<a\\s*href=("|\')(.+?)("|\')\\s*>#is', $text, $matches, PREG_OFFSET_CAPTURE);
2019-05-31 15:31:23 +02:00
foreach ($matches[2] as $match) {
2019-09-02 17:08:36 +02:00
$mod = \htmlentities($match[0]);
$temp = \substr($text, 0, $match[1] + $diff);
2019-06-05 21:09:25 +02:00
$temp .= $mod;
2019-09-02 17:08:36 +02:00
$temp .= \substr($text, $match[1] + $diff + \strlen($match[0]));
$diff += \strlen($mod) - \strlen($match[0]);
2019-05-31 15:31:23 +02:00
$text = $temp;
}
return $text;
}
2019-09-02 17:08:36 +02:00
return \htmlentities($text);
}
private function buildRows($button_list)
2017-05-19 21:34:57 +02:00
{
$end = false;
$rows = [];
$buttons = [];
$cols = 0;
foreach ($button_list as $button) {
2017-05-19 21:35:03 +02:00
if (isset($button['new'])) {
2019-09-02 17:08:36 +02:00
if (\count($buttons) == 0) {
2017-05-19 21:35:03 +02:00
$buttons[] = $button;
} else {
$row = ['_' => 'keyboardButtonRow', 'buttons' => $buttons];
$rows[] = $row;
$buttons = [$button];
}
} else {
2017-05-19 21:34:57 +02:00
$buttons[] = $button;
$end = true;
}
}
if ($end) {
$row = ['_' => 'keyboardButtonRow', 'buttons' => $buttons];
$rows[] = $row;
}
return ['_' => 'replyInlineMarkup', 'rows' => $rows];
}
2018-02-24 17:54:39 +01:00
}