· 6 years ago · Oct 10, 2019, 01:48 PM
1<?php
2set_time_limit(0);
3define('DBASE', 'u273253230_bpg');
4define('USER', 'capital');
5define('PASS', 'B3:B&q~Nxqa&(9HG');
6define('API_URL', 'https://api.telegram.org/bot479123085:AAF5dm_zmiwzQquFXdArd6nvW86AXk-pXWs/');
7
8error_reporting(E_ALL);
9ini_set('display_errors', 1);
10
11class Cron {
12
13 private $list;
14 private $dbcon;
15
16 function __construct() {
17 $options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4'];
18 $this->dbcon = new PDO('mysql:host=localhost;port=3306;dbname='.DBASE, USER, PASS, $options);
19 $stmt = $this->dbcon->prepare("SELECT id_chat FROM users WHERE 1"); // status_off = 1
20 $stmt->execute();
21 $this->list = $stmt->fetchAll();
22 $this->update();
23 }
24
25 private function update() {
26 $stmt = $this->dbcon->prepare("SELECT * FROM crontask WHERE name='notifications'");
27 $stmt->execute();
28 $task = $stmt->fetch();
29 $stmt3 = $this->dbcon->prepare("SELECT * FROM crontask WHERE name='author'");
30 $stmt3->execute();
31 $author= $stmt3->fetch();
32 $author=$author['content'];
33 $content = $task['content'];
34 if ($task['additional'] != null || $task['additional'] != '') {
35 $additional = $task['additional'];
36 if ($additional == 'NO'){
37 $caption = false;
38 } else {
39 $caption = true;
40 }
41 } else {
42 $additional = null;
43 }
44 $count = 0;
45 if ($task['status'] == 1) {
46 $sendArray = array(
47 "chat_id" => $author,
48 "text" => "Кол-во: ".count($this->list)
49 );
50 $result = $this->apiRequest("sendMessage", $sendArray);
51 $stmt2 = $this->dbcon->prepare("UPDATE crontask SET status = 0 WHERE name='notifications'");
52 $stmt2->execute();
53 $items = count($this->list);
54 for ($i = 0; $i < $items; $i++) {
55 $id = $this->list[$i]['id_chat'];
56 if ($additional == null) {
57 $sendArray = array(
58 "chat_id" => $id,
59 "text" => $content
60 );
61 $result = $this->apiRequest("sendMessage", $sendArray);
62 } else {
63 $sendArray = array(
64 "chat_id" => $id,
65 "photo" => $content
66 );
67 if ($caption) {
68 $sendArray['caption'] = $additional;
69 }
70 $result = $this->apiRequest("sendPhoto", $sendArray);
71 }
72 if (isset($result['message_id']) == false) {
73 echo $id." ".json_encode($result)."\r\n";
74 $stmt3 = $this->dbcon->prepare("UPDATE users SET status_off = 0 WHERE id_chat = :user");
75 $stmt3->bindValue(':user', $id);
76 $stmt3->execute();
77 $count++;
78 } else {
79 echo $i." ".time()."\r\n";
80 $stmt3 = $this->dbcon->prepare("UPDATE users SET status_off = 1 WHERE id_chat = :user");
81 $stmt3->bindValue(':user', $id);
82 $stmt3->execute();
83 }
84 if ($i % 1000 == 0) {
85 $sendArray = array(
86 "chat_id" => $author,
87 "text" => "Отправлено: ".$i
88 );
89 $result = $this->apiRequest("sendMessage", $sendArray);
90 }
91 if ($i % 30 == 0) {
92 usleep(100000);
93 }
94 $stmt3 = $this->dbcon->prepare("UPDATE crontask SET content = content + 1 WHERE name='process'");
95 $stmt3->execute();
96 }
97 sleep(5);
98 $sendArray = array(
99 "chat_id" => $author,
100 "text" => "Кол-во: ".count($this->list)."\n\nНе получили: ".$count
101 );
102 $result = $this->apiRequest("sendMessage", $sendArray);
103 }
104 }
105 function exec_curl_request($handle) {
106 $response = curl_exec($handle);
107
108 if ($response === false) {
109 $errno = curl_errno($handle);
110 $error = curl_error($handle);
111 error_log("Curl returned error $errno: $error\n");
112 curl_close($handle);
113 return false;
114 }
115
116 $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
117 curl_close($handle);
118
119 if ($http_code >= 500) {
120 sleep(10);
121 return $response;
122 } elseif (($http_code == 400) || ($http_code == 403)){
123 $response = json_decode($response, true);
124 return array($response['error_code'],$response['description']);
125 } else if ($http_code != 200) {
126 $response = json_decode($response, true);
127 error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
128 if ($http_code == 401) {
129 throw new Exception('Invalid access token provided');
130 }
131 return $response;
132 } else {
133 $response = json_decode($response, true);
134 if (isset($response['description'])) {
135 error_log("Request was successfull: {$response['description']}\n");
136 }
137 $response = $response['result'];
138 }
139
140 return $response;
141 }
142
143 // Api request
144 function apiRequest($method, $parameters) {
145 if (!is_string($method)) {
146 error_log("Method name must be a string\n");
147 return false;
148 }
149
150 if (!$parameters) {
151 $parameters = array();
152 } else if (!is_array($parameters)) {
153 error_log("Parameters must be an array\n");
154 return false;
155 }
156
157 foreach ($parameters as $key => &$val) {
158 if (!is_numeric($val) && !is_string($val)) {
159 $val = json_encode($val);
160 }
161 }
162 $url = API_URL.$method.'?'.http_build_query($parameters);
163
164 $handle = curl_init($url);
165 curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
166 curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 0);
167 curl_setopt($handle, CURLOPT_TIMEOUT, 0);
168
169 return $this->exec_curl_request($handle);
170 }
171}
172
173new Cron();
174?>