Skip to content

Commit f753fbf

Browse files
committed
Supress some errors while getting update data in polling
1 parent a6a9eef commit f753fbf

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

files/bot.errorhandler.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ public static function register($error_bot, $error_admin, $show_data = true) {
1414
self::$admin = self::$first_admin = $error_admin;
1515
self::$show_data = $show_data;
1616

17-
$json = @file_get_contents('php://input');
18-
self::$data = @json_decode($json, true);
17+
$json = \file_get_contents('php://input');
18+
self::$data = null;
19+
if ($json) {
20+
self::$data = \json_decode($json, true);
21+
}
1922

20-
set_error_handler(['\phgram\BotErrorHandler', 'error_handler']);
21-
set_exception_handler(['\phgram\BotErrorHandler', 'exception_handler']);
22-
register_shutdown_function(['\phgram\BotErrorHandler', 'shutdown_handler']);
23+
\set_error_handler(['\phgram\BotErrorHandler', 'error_handler']);
24+
\set_exception_handler(['\phgram\BotErrorHandler', 'exception_handler']);
25+
\register_shutdown_function(['\phgram\BotErrorHandler', 'shutdown_handler']);
2326
}
2427

2528
// for restoring the handlers
2629
public static function destruct () {
27-
restore_error_handler();
28-
restore_exception_handler();
30+
\restore_error_handler();
31+
\restore_exception_handler();
2932
}
3033

3134
// for restoring self::$bot and self::$admin to the initial values
@@ -39,13 +42,13 @@ public static function call(string $method, array $args = []) {
3942
$bot = self::$bot;
4043
$url = "https://api.telegram.org/bot{$bot}/{$method}";
4144

42-
$ch = curl_init($url);
43-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
44-
curl_setopt($ch, CURLOPT_POST, TRUE);
45-
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
46-
$result = curl_exec($ch);
47-
curl_close($ch);
48-
return @json_decode($result, true);
45+
$ch = \curl_init($url);
46+
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
47+
\curl_setopt($ch, CURLOPT_POST, TRUE);
48+
\curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
49+
$result = \curl_exec($ch);
50+
\curl_close($ch);
51+
return \json_decode($result ?: '[]', true);
4952
}
5053

5154
// returns the error type name by code
@@ -72,9 +75,9 @@ public static function get_error_type($code) {
7275

7376
// handle errors
7477
public static function error_handler($error_type, $error_message, $error_file, $error_line, $error_args) {
75-
if (error_reporting() === 0 && self::$verbose != true) return false;
78+
if (\error_reporting() === 0 && self::$verbose != true) return false;
7679

77-
$str = htmlspecialchars("{$error_message} in {$error_file} on line {$error_line}");
80+
$str = \htmlspecialchars("{$error_message} in {$error_file} on line {$error_line}");
7881
$str .= "\nView:\n". phgram_pretty_debug(2);
7982

8083
if (self::$show_data) {
@@ -96,7 +99,7 @@ public static function error_handler($error_type, $error_message, $error_file, $
9699
$chat_mention = isset($chat['username'])? "<a href='t.me/{$chat['username']}/{$message_id}'>@{$chat['username']}</a>" : "<i>{$chat['title']}</i>";
97100
}
98101

99-
$str .= htmlspecialchars("\n\n\"{$text}\", ").
102+
$str .= \htmlspecialchars("\n\n\"{$text}\", ").
100103
($sender? "sent by <a href='tg://user?id={$sender}'>{$sender_name}</a>, " : '').
101104
($chat? "in {$chat_id} ({$chat_mention})." : '')." Update type: '{$type}'.";
102105
}
@@ -105,7 +108,7 @@ public static function error_handler($error_type, $error_message, $error_file, $
105108
$str .= "\nError type: {$error_type}.";
106109

107110
$error_log_str = "{$error_type}: {$error_message} in {$error_file} on line {$error_line}";
108-
error_log($error_log_str);
111+
\error_log($error_log_str);
109112

110113
self::log($str);
111114

@@ -114,7 +117,7 @@ public static function error_handler($error_type, $error_message, $error_file, $
114117

115118
// handle exceptions
116119
public static function exception_handler($e) {
117-
$str = htmlspecialchars("{$e->getMessage()} in {$e->getFile()} on line {$e->getline()}");
120+
$str = \htmlspecialchars("{$e->getMessage()} in {$e->getFile()} on line {$e->getline()}");
118121
$str .= "\nView:\n". phgram_pretty_debug(2);
119122

120123
if (self::$show_data) {
@@ -141,7 +144,7 @@ public static function exception_handler($e) {
141144
($chat? "in {$chat_id} ({$chat_mention})." : '')." Update type: '{$type}'.";
142145
}
143146
$error_log_str = "Exception: {$e->getMessage()} in {$e->getFile()} on line {$e->getline()}";
144-
error_log($error_log_str);
147+
\error_log($error_log_str);
145148

146149
self::log($str);
147150

@@ -162,26 +165,26 @@ public static function log($text, $type = 'ERR') {
162165
$params = ['chat_id' => self::$admin, 'text' => $text, 'parse_mode' => 'html'];
163166
$method = 'sendMessage';
164167

165-
if (mb_strlen($text) > 4096) {
166-
$text = substr($text, 0, 20400); # 20480 = 20MB (limit of BotAPI)
168+
if (\mb_strlen($text) > 4096) {
169+
$text = \substr($text, 0, 20400); # 20480 = 20MB (limit of BotAPI)
167170
$logname = 'BEHlog_'.time().'.txt';
168171

169-
file_put_contents($logname, $text);
172+
\file_put_contents($logname, $text);
170173

171174
$method = 'sendDocument';
172-
$document = curl_file_create(realpath($logname));
175+
$document = \curl_file_create(realpath($logname));
173176
$document->postname = $type.'_report.txt';
174177
$params['document'] = $document;
175178
}
176179

177-
if (is_array(self::$admin)) {
180+
if (\is_array(self::$admin)) {
178181
foreach (self::$admin as $admin) {
179182
$params['chat_id'] = $admin;
180183
self::call($method, $params);
181184
}
182185
} else {
183186
self::call($method, $params);
184187
}
185-
if (isset($logname)) unlink($logname);
188+
if (isset($logname)) \unlink($logname);
186189
}
187-
}
190+
}

files/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
namespace phgram;
3-
const version = '2020-02-21T16:47:46-03:00'; # actually a date
3+
const version = '2020-04-05T13:30:09-03:00'; # actually a date
44

55
require_once 'misc.functions.php';
66
require_once 'bot.errorhandler.php';

phgram.phar

74 Bytes
Binary file not shown.

phgram.phar.gz

43 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)