Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- define('DBASE', 'u273253230_bpg');
- define('USER', 'capital');
- define('PASS', 'B3:B&q~Nxqa&(9HG');
- define('API_URL', 'https://api.telegram.org/bot479123085:AAF5dm_zmiwzQquFXdArd6nvW86AXk-pXWs/');
- error_reporting(E_ALL);
- ini_set('display_errors', 1);
- class Cron {
- private $list;
- private $dbcon;
- function __construct() {
- $options = [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4'];
- $this->dbcon = new PDO('mysql:host=localhost;port=3306;dbname='.DBASE, USER, PASS, $options);
- $stmt = $this->dbcon->prepare("SELECT id_chat FROM users WHERE 1"); // status_off = 1
- $stmt->execute();
- $this->list = $stmt->fetchAll();
- $this->update();
- }
- private function update() {
- $stmt = $this->dbcon->prepare("SELECT * FROM crontask WHERE name='notifications'");
- $stmt->execute();
- $task = $stmt->fetch();
- $stmt3 = $this->dbcon->prepare("SELECT * FROM crontask WHERE name='author'");
- $stmt3->execute();
- $author= $stmt3->fetch();
- $author=$author['content'];
- $content = $task['content'];
- if ($task['additional'] != null || $task['additional'] != '') {
- $additional = $task['additional'];
- if ($additional == 'NO'){
- $caption = false;
- } else {
- $caption = true;
- }
- } else {
- $additional = null;
- }
- $count = 0;
- if ($task['status'] == 1) {
- $sendArray = array(
- "chat_id" => $author,
- "text" => "Кол-во: ".count($this->list)
- );
- $result = $this->apiRequest("sendMessage", $sendArray);
- $stmt2 = $this->dbcon->prepare("UPDATE crontask SET status = 0 WHERE name='notifications'");
- $stmt2->execute();
- $items = count($this->list);
- for ($i = 0; $i < $items; $i++) {
- $id = $this->list[$i]['id_chat'];
- if ($additional == null) {
- $sendArray = array(
- "chat_id" => $id,
- "text" => $content
- );
- $result = $this->apiRequest("sendMessage", $sendArray);
- } else {
- $sendArray = array(
- "chat_id" => $id,
- "photo" => $content
- );
- if ($caption) {
- $sendArray['caption'] = $additional;
- }
- $result = $this->apiRequest("sendPhoto", $sendArray);
- }
- if (isset($result['message_id']) == false) {
- echo $id." ".json_encode($result)."\r\n";
- $stmt3 = $this->dbcon->prepare("UPDATE users SET status_off = 0 WHERE id_chat = :user");
- $stmt3->bindValue(':user', $id);
- $stmt3->execute();
- $count++;
- } else {
- echo $i." ".time()."\r\n";
- $stmt3 = $this->dbcon->prepare("UPDATE users SET status_off = 1 WHERE id_chat = :user");
- $stmt3->bindValue(':user', $id);
- $stmt3->execute();
- }
- if ($i % 1000 == 0) {
- $sendArray = array(
- "chat_id" => $author,
- "text" => "Отправлено: ".$i
- );
- $result = $this->apiRequest("sendMessage", $sendArray);
- }
- if ($i % 30 == 0) {
- usleep(100000);
- }
- $stmt3 = $this->dbcon->prepare("UPDATE crontask SET content = content + 1 WHERE name='process'");
- $stmt3->execute();
- }
- sleep(5);
- $sendArray = array(
- "chat_id" => $author,
- "text" => "Кол-во: ".count($this->list)."\n\nНе получили: ".$count
- );
- $result = $this->apiRequest("sendMessage", $sendArray);
- }
- }
- function exec_curl_request($handle) {
- $response = curl_exec($handle);
- if ($response === false) {
- $errno = curl_errno($handle);
- $error = curl_error($handle);
- error_log("Curl returned error $errno: $error\n");
- curl_close($handle);
- return false;
- }
- $http_code = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
- curl_close($handle);
- if ($http_code >= 500) {
- sleep(10);
- return $response;
- } elseif (($http_code == 400) || ($http_code == 403)){
- $response = json_decode($response, true);
- return array($response['error_code'],$response['description']);
- } else if ($http_code != 200) {
- $response = json_decode($response, true);
- error_log("Request has failed with error {$response['error_code']}: {$response['description']}\n");
- if ($http_code == 401) {
- throw new Exception('Invalid access token provided');
- }
- return $response;
- } else {
- $response = json_decode($response, true);
- if (isset($response['description'])) {
- error_log("Request was successfull: {$response['description']}\n");
- }
- $response = $response['result'];
- }
- return $response;
- }
- // Api request
- function apiRequest($method, $parameters) {
- if (!is_string($method)) {
- error_log("Method name must be a string\n");
- return false;
- }
- if (!$parameters) {
- $parameters = array();
- } else if (!is_array($parameters)) {
- error_log("Parameters must be an array\n");
- return false;
- }
- foreach ($parameters as $key => &$val) {
- if (!is_numeric($val) && !is_string($val)) {
- $val = json_encode($val);
- }
- }
- $url = API_URL.$method.'?'.http_build_query($parameters);
- $handle = curl_init($url);
- curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 0);
- curl_setopt($handle, CURLOPT_TIMEOUT, 0);
- return $this->exec_curl_request($handle);
- }
- }
- new Cron();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement