Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(0);
- define(ROOT, trim(__FILE__, 'sender.php'));
- class Sender {
- protected $telegram, $config, $date;
- public function __construct() {
- require_once('recaptchalib.php');
- // Get a key from https://www.google.com/recaptcha/admin/create
- $publickey = "6Le00iQUAAAAAONEMEAPA0CdPrYEi1X842_NO6Bt";
- $privatekey = "6Le00iQUAAAAAF2YIYSzrrA43MtAYQ2CwXROyzou";
- # the response from reCAPTCHA
- $resp = null;
- # the error code from reCAPTCHA, if any
- $error = null;
- # was there a reCAPTCHA response?
- if ($_POST["recaptcha_response_field"]) {
- $resp = recaptcha_check_answer ($privatekey,
- $_SERVER["REMOTE_ADDR"],
- $_POST["recaptcha_challenge_field"],
- $_POST["recaptcha_response_field"]);
- if ($resp->is_valid) {
- $config = $this->config('config.ini');
- $date = new DateTime;
- $date->setTimezone(new DateTimeZone($config['timezone']));
- $ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' ? true : false;
- //if(!$this->ajax) {
- //die('Access danied');
- //}
- //var_dump($this->config);
- if(isset($_POST['action']) and $_POST['action'] === 'sendMail') {
- $name = isset($_POST['fio']) ? $_POST['fio'] : 'Не указано';
- $phone = isset($_POST['phone']) ? $_POST['phone'] : 'Не указано';
- $email = isset($_POST['email']) ? $_POST['email'] : 'Не указано';
- $deadLine = isset($_POST['deadLine']) ? $_POST['deadLine'] : 'Не указано';
- $description = isset($_POST['description']) ? $_POST['description'] : 'Не указано';
- $pages = isset($_POST['pagesAll']) ? $_POST['pagesAll'] : 'Не указано';
- $predmet = isset($_POST['predmet']) ? $_POST['predmet'] : 'Не указано';
- $text = isset($_POST['text']) ? $_POST['text'] : 'Не указано';
- $typejob = isset($_POST['typejob']) ? $_POST['typejob'] : 'Не указано';
- $body =
- 'На сайте ' . $config['home_title'] . " было отправлено сообщение\n\n\n" .
- 'Имя: ' . $name . "\n" .
- 'Контакты: email - ' . $email . " / телефон - " . $phone . "\n" .
- 'Вид работы: ' . $typejob . "\n" .
- 'Предмет: ' . $predmet . "\n" .
- 'Тема работы: ' . $description . "\n" .
- 'Кол-во страниц: ' . $pages . "\n" .
- 'Сроки сдачи: ' . $deadLine . "\n" .
- 'Доп. информация: ' . $text . "\n\n\n" .
- 'Отправлено в: ' . $date->format('Y-m-d H:i:s') . "\n";
- $mail = new Mailer($config);
- $mail->from = $_POST['email'];
- $mail->send($config['admin_mail'], $typejob . " : " . $predmet, $body);
- if($mail->send_error) {
- echo json_encode(array('status' => 'error', 'message' => $mail->smtp_msg));
- } else {
- echo json_encode(array('status' => 'success', 'message' => 'Сообщение успешно отправлено'));
- $telegram = new Telegram($config['token']);
- $telegram->sendMessage(array('chat_id' => $config['chat_id'], 'text' => $body));
- if(isset($config['chat_id2']) and !empty($config['chat_id2'])) {
- $telegram->sendMessage(array('chat_id' => $config['chat_id2'], 'text' => $body));
- }
- }
- }
- } else {
- # set the error code so that we can display it
- $error = $resp->error;
- }
- else {
- echo json_encode(array('status' => 'error', 'message' => 'Введите каптчу'));
- }
- }
- protected function config($file) {
- if(file_exists(ROOT . $file)) {
- return parse_ini_file(ROOT . $file);
- } else {
- die('Config file not found');
- }
- }
- }
- class Telegram {
- private $bot_id = "";
- private $data = array();
- private $updates = array();
- public function __construct($bot_id) {
- $this->bot_id = $bot_id;
- $this->data = $this->getData();
- }
- public function endpoint($api, array $content, $post = true) {
- $url = 'https://api.telegram.org/bot' . $this->bot_id . '/' . $api;
- if ($post)
- $reply = $this->sendAPIRequest($url, $content);
- else
- $reply = $this->sendAPIRequest($url, array(), false);
- return json_decode($reply, true);
- }
- public function getMe() {
- return $this->endpoint("getMe", array(), false);
- }
- public function respondSuccess() {
- http_response_code(200);
- return json_encode(array("status" => "success"));
- }
- public function sendMessage(array $content) {
- return $this->endpoint("sendMessage", $content);
- }
- public function forwardMessage(array $content) {
- return $this->endpoint("forwardMessage", $content);
- }
- public function sendPhoto(array $content) {
- return $this->endpoint("sendPhoto", $content);
- }
- public function sendAudio(array $content) {
- return $this->endpoint("sendAudio", $content);
- }
- public function sendDocument(array $content) {
- return $this->endpoint("sendDocument", $content);
- }
- public function sendSticker(array $content) {
- return $this->endpoint("sendSticker", $content);
- }
- public function sendVideo(array $content) {
- return $this->endpoint("sendVideo", $content);
- }
- public function sendVoice(array $content) {
- return $this->endpoint("sendVoice", $content);
- }
- public function sendLocation(array $content) {
- return $this->endpoint("sendLocation", $content);
- }
- public function sendVenue(array $content) {
- return $this->endpoint("sendVenue", $content);
- }
- public function sendContact(array $content) {
- return $this->endpoint("sendContact", $content);
- }
- public function sendChatAction(array $content) {
- return $this->endpoint("sendChatAction", $content);
- }
- public function getUserProfilePhotos(array $content) {
- return $this->endpoint("getUserProfilePhotos", $content);
- }
- public function getFile($file_id) {
- $content = array('file_id' => $file_id);
- return $this->endpoint("getFile", $content);
- }
- public function kickChatMember(array $content) {
- return $this->endpoint("kickChatMember", $content);
- }
- public function leaveChat(array $content) {
- return $this->endpoint("leaveChat", $content);
- }
- public function unbanChatMember(array $content) {
- return $this->endpoint("unbanChatMember", $content);
- }
- public function getChat(array $content) {
- return $this->endpoint("getChat", $content);
- }
- public function getChatAdministrators(array $content) {
- return $this->endpoint("getChatAdministrators", $content);
- }
- public function getChatMembersCount(array $content) {
- return $this->endpoint("getChatMembersCount", $content);
- }
- public function getChatMember(array $content) {
- return $this->endpoint("getChatMember", $content);
- }
- public function answerInlineQuery(array $content) {
- return $this->endpoint("answerInlineQuery", $content);
- }
- public function setGameScore(array $content) {
- return $this->endpoint("setGameScore", $content);
- }
- public function answerCallbackQuery(array $content) {
- return $this->endpoint("answerCallbackQuery", $content);
- }
- public function editMessageText(array $content) {
- return $this->endpoint("editMessageText", $content);
- }
- public function editMessageCaption(array $content) {
- return $this->endpoint("editMessageCaption", $content);
- }
- public function editMessageReplyMarkup(array $content) {
- return $this->endpoint("editMessageReplyMarkup", $content);
- }
- public function downloadFile($telegram_file_path, $local_file_path) {
- $file_url = "https://api.telegram.org/file/bot" . $this->bot_id . "/" . $telegram_file_path;
- $in = fopen($file_url, "rb");
- $out = fopen($local_file_path, "wb");
- while ($chunk = fread($in, 8192)) {
- fwrite($out, $chunk, 8192);
- }
- fclose($in);
- fclose($out);
- }
- public function setWebhook($url, $certificate = "") {
- if ($certificate == "") {
- $content = array('url' => $url);
- } else {
- $content = array('url' => $url, 'certificate' => $certificate);
- }
- return $this->endpoint("setWebhook", $content);
- }
- public function getData() {
- if (empty($this->data)) {
- $rawData = file_get_contents("php://input");
- return json_decode($rawData, true);
- } else {
- return $this->data;
- }
- }
- public function setData(array $data) {
- $this->data = $data;
- }
- public function Text() {
- return $this->data["message"] ["text"];
- }
- public function ChatID() {
- return $this->data["message"]["chat"]["id"];
- }
- public function MessageID() {
- return $this->data["message"]["message_id"];
- }
- public function ReplyToMessageID() {
- return $this->data["message"]["reply_to_message"]["message_id"];
- }
- public function ReplyToMessageFromUserID() {
- return $this->data["message"]["reply_to_message"]["forward_from"]["id"];
- }
- public function Inline_Query() {
- return $this->data["inline_query"];
- }
- public function Callback_Query() {
- return $this->data["callback_query"];
- }
- public function Callback_ID() {
- return $this->data["callback_query"]["id"];
- }
- public function Callback_Data() {
- return $this->data["callback_query"]["data"];
- }
- public function Callback_Message() {
- return $this->data["callback_query"]["message"];
- }
- public function Callback_ChatID() {
- return $this->data["callback_query"]["message"]["chat"]["id"];
- }
- public function Date() {
- return $this->data["message"]["date"];
- }
- public function FirstName() {
- return $this->data["message"]["from"]["first_name"];
- }
- public function LastName() {
- return $this->data["message"]["from"]["last_name"];
- }
- public function Username() {
- return $this->data["message"]["from"]["username"];
- }
- public function Location() {
- return $this->data["message"]["location"];
- }
- public function UpdateID() {
- return $this->data["update_id"];
- }
- public function UpdateCount() {
- return count($this->updates["result"]);
- }
- public function messageFromGroup() {
- if ($this->data["message"]["chat"]["type"] == "private") {
- return false;
- }
- return true;
- }
- public function messageFromGroupTitle() {
- if ($this->data["message"]["chat"]["type"] != "private") {
- return $this->data["message"]["chat"]["title"];
- }
- return null;
- }
- public function buildKeyBoard(array $options, $onetime = false, $resize = false, $selective = true) {
- $replyMarkup = array(
- 'keyboard' => $options,
- 'one_time_keyboard' => $onetime,
- 'resize_keyboard' => $resize,
- 'selective' => $selective
- );
- $encodedMarkup = json_encode($replyMarkup, true);
- return $encodedMarkup;
- }
- public function buildInlineKeyBoard(array $options) {
- $replyMarkup = array(
- 'inline_keyboard' => $options,
- );
- $encodedMarkup = json_encode($replyMarkup, true);
- return $encodedMarkup;
- }
- public function buildInlineKeyboardButton($text, $url = "", $callback_data = "", $switch_inline_query = "", $switch_inline_query_current_chat = "", $callback_game = "") {
- $replyMarkup = array(
- 'text' => $text
- );
- if ($url != "") {
- $replyMarkup['url'] = $url;
- } else if ($callback_data != "") {
- $replyMarkup['callback_data'] = $callback_data;
- } else if ($switch_inline_query != "") {
- $replyMarkup['switch_inline_query'] = $switch_inline_query;
- } else if ($switch_inline_query_current_chat != "") {
- $replyMarkup['switch_inline_query_current_chat'] = $switch_inline_query_current_chat;
- } else if ($callback_game != "") {
- $replyMarkup['callback_game'] = $callback_game;
- }
- return $replyMarkup;
- }
- public function buildKeyboardButton($text, $request_contact = false, $request_location = false) {
- $replyMarkup = array(
- 'text' => $text,
- 'request_contact' => $request_contact,
- 'request_location' => $request_location
- );
- if ($url != "") {
- $replyMarkup['url'] = $url;
- } else if ($callback_data != "") {
- $replyMarkup['callback_data'] = $callback_data;
- } else if ($switch_inline_query != "") {
- $replyMarkup['switch_inline_query'] = $switch_inline_query;
- }
- return $replyMarkup;
- }
- public function buildKeyBoardHide($selective = true) {
- $replyMarkup = array(
- 'hide_keyboard' => true,
- 'selective' => $selective
- );
- $encodedMarkup = json_encode($replyMarkup, true);
- return $encodedMarkup;
- }
- public function buildForceReply($selective = true) {
- $replyMarkup = array(
- 'force_reply' => true,
- 'selective' => $selective
- );
- $encodedMarkup = json_encode($replyMarkup, true);
- return $encodedMarkup;
- }
- public function getUpdates($offset = 0, $limit = 100, $timeout = 0, $update = true) {
- $content = array('offset' => $offset, 'limit' => $limit, 'timeout' => $timeout);
- $this->updates = $this->endpoint("getUpdates", $content);
- if ($update) {
- if(count($this->updates["result"]) >= 1) { //for CLI working.
- $last_element_id = $this->updates["result"][count($this->updates["result"]) - 1]["update_id"] + 1;
- $content = array('offset' => $last_element_id, 'limit' => "1", 'timeout' => $timeout);
- $this->endpoint("getUpdates", $content);
- }
- }
- return $this->updates;
- }
- public function serveUpdate($update) {
- $this->data = $this->updates["result"][$update];
- }
- private function sendAPIRequest($url, array $content, $post = true) {
- if (isset($content['chat_id'])) {
- $url = $url . "?chat_id=" . $content['chat_id'];
- unset($content['chat_id']);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- if ($post) {
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
- }
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- }
- if (!function_exists('curl_file_create')) {
- function curl_file_create($filename, $mimetype = '', $postname = '') {
- return "@$filename;filename="
- . ($postname ? : basename($filename))
- . ($mimetype ? ";type=$mimetype" : '');
- }
- }
- class Mailer {
- var $site_name = "";
- var $from = "";
- var $to = "";
- var $subject = "";
- var $message = "";
- var $header = "";
- var $error = "";
- var $bcc = array ();
- var $mail_headers = "";
- var $html_mail = 0;
- var $charset = 'utf-8';
- var $smtp_fp = FALSE;
- var $smtp_msg = "";
- var $smtp_port = "";
- var $smtp_host = "localhost";
- var $smtp_user = "";
- var $smtp_pass = "";
- var $smtp_code = "";
- var $send_error = FALSE;
- var $eol = "\n";
- var $mail_method = 'php';
- public function prepare($config, $is_html = false) {
- $this->mail_method = $config['mail_metod'];
- $this->from = $config['admin_mail'];
- $this->charset = $config['charset'];
- $this->site_name = $config['home_title'];
- $this->smtp_host = $config['smtp_host'];
- $this->smtp_port = intval( $config['smtp_port'] );
- $this->smtp_user = $config['smtp_user'];
- $this->smtp_pass = $config['smtp_pass'];
- $this->html_mail = $is_html;
- }
- public function compile_headers() {
- $this->subject = "=?" . $this->charset . "?b?" . base64_encode( $this->subject ) . "?=";
- $from = "=?" . $this->charset . "?b?" . base64_encode( $this->site_name ) . "?=";
- if( $this->html_mail ) {
- $this->mail_headers .= "MIME-Version: 1.0" . $this->eol;
- $this->mail_headers .= "Content-type: text/html; charset=\"" . $this->charset . "\"" . $this->eol;
- } else {
- $this->mail_headers .= "MIME-Version: 1.0" . $this->eol;
- $this->mail_headers .= "Content-type: text/plain; charset=\"" . $this->charset . "\"" . $this->eol;
- }
- if( $this->mail_method != 'smtp' ) {
- if( count( $this->bcc ) ) {
- $this->mail_headers .= "Bcc: " . implode( ",", $this->bcc ) . $this->eol;
- }
- } else {
- $this->mail_headers .= "Subject: " . $this->subject . $this->eol;
- if( $this->to ) {
- $this->mail_headers .= "To: " . $this->to . $this->eol;
- }
- }
- $this->mail_headers .= "From: \"" . $from . "\" <" . $this->from . ">" . $this->eol;
- $this->mail_headers .= "Return-Path: <" . $this->from . ">" . $this->eol;
- $this->mail_headers .= "X-Priority: 3" . $this->eol;
- $this->mail_headers .= "X-MSMail-Priority: Normal" . $this->eol;
- $this->mail_headers .= "X-Mailer: DLE PHP" . $this->eol;
- }
- public function send($to, $subject, $message) {
- $this->to = preg_replace( "/[ \t]+/", "", $to );
- $this->from = preg_replace( "/[ \t]+/", "", $this->from );
- $this->to = preg_replace( "/,,/", ",", $this->to );
- $this->from = preg_replace( "/,,/", ",", $this->from );
- if( $this->mail_method != 'smtp' )
- $this->to = preg_replace( "#\#\[\]'\"\(\):;/\$!£%\^&\*\{\}#", "", $this->to );
- else
- $this->to = '<' . preg_replace( "#\#\[\]'\"\(\):;/\$!£%\^&\*\{\}#", "", $this->to ) . '>';
- $this->from = preg_replace( "#\#\[\]'\"\(\):;/\$!£%\^&\*\{\}#", "", $this->from );
- $this->subject = $subject;
- $this->message = $message;
- $this->message = str_replace( "\r", "", $this->message );
- $this->compile_headers();
- if( ($this->to) and ($this->from) and ($this->subject) ) {
- if( $this->mail_method != 'smtp' ) {
- if( ! @mail( $this->to, $this->subject, $this->message, $this->mail_headers ) ) {
- $this->smtp_msg = "PHP Mail Error.";
- $this->send_error = true;
- }
- } else {
- $this->smtp_send();
- }
- }
- $this->mail_headers = "";
- }
- public function smtp_get_line() {
- $this->smtp_msg = "";
- while ( $line = fgets( $this->smtp_fp, 515 ) ) {
- $this->smtp_msg .= $line;
- if( substr( $line, 3, 1 ) == " " ) {
- break;
- }
- }
- }
- public function smtp_send() {
- $this->smtp_fp = @fsockopen( $this->smtp_host, intval( $this->smtp_port ), $errno, $errstr, 30 );
- if( ! $this->smtp_fp ) {
- $this->smtp_error( "Could not open a socket to the SMTP server" );
- return;
- }
- $this->smtp_get_line();
- $this->smtp_code = substr( $this->smtp_msg, 0, 3 );
- if( $this->smtp_code == 220 ) {
- $data = $this->smtp_crlf_encode( $this->mail_headers . "\n" . $this->message );
- $this->smtp_send_cmd( "HELO " . $this->smtp_host );
- if( $this->smtp_code != 250 ) {
- $this->smtp_error( "HELO" );
- return;
- }
- if( $this->smtp_user and $this->smtp_pass ) {
- $this->smtp_send_cmd( "AUTH LOGIN" );
- if( $this->smtp_code == 334 ) {
- $this->smtp_send_cmd( base64_encode( $this->smtp_user ) );
- if( $this->smtp_code != 334 ) {
- $this->smtp_error( "Username not accepted from the server" );
- return;
- }
- $this->smtp_send_cmd( base64_encode( $this->smtp_pass ) );
- if( $this->smtp_code != 235 ) {
- $this->smtp_error( "Password not accepted from the SMTP server" );
- return;
- }
- } else {
- $this->smtp_error( "This SMTP server does not support authorisation" );
- return;
- }
- }
- $this->smtp_send_cmd( "MAIL FROM:<" . $this->from . ">" );
- if( $this->smtp_code != 250 ) {
- $this->smtp_error( "Incorrect FROM address: $this->from" );
- return;
- }
- $to_array = array ( $this->to );
- if( count( $this->bcc ) ) {
- foreach ( $this->bcc as $bcc ) {
- if( preg_match( "/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/", str_replace( " ", "", $bcc ) ) ) {
- $to_array[] = "<".$bcc.">";
- }
- }
- }
- foreach ( $to_array as $to_email ) {
- $this->smtp_send_cmd( "RCPT TO:" . $to_email );
- if( $this->smtp_code != 250 ) {
- $this->smtp_error( "Incorrect email address: $to_email" );
- return;
- break;
- }
- }
- $this->smtp_send_cmd( "DATA" );
- if( $this->smtp_code == 354 ) {
- fputs( $this->smtp_fp, $data . "\r\n" );
- } else {
- $this->smtp_error( "Error on write to SMTP server" );
- return;
- }
- $this->smtp_send_cmd( "." );
- if( $this->smtp_code != 250 ) {
- $this->smtp_error();
- return;
- }
- $this->smtp_send_cmd( "quit" );
- if( $this->smtp_code != 221 ) {
- $this->smtp_error();
- return;
- }
- @fclose( $this->smtp_fp );
- } else {
- $this->smtp_error( "SMTP service unaviable" );
- return;
- }
- }
- public function smtp_send_cmd($cmd) {
- $this->smtp_msg = "";
- $this->smtp_code = "";
- fputs( $this->smtp_fp, $cmd . "\r\n" );
- $this->smtp_get_line();
- $this->smtp_code = substr( $this->smtp_msg, 0, 3 );
- return $this->smtp_code == "" ? FALSE : TRUE;
- }
- public function smtp_error($err = "") {
- $this->smtp_msg = $err;
- $this->send_error = true;
- return;
- }
- public function smtp_crlf_encode($data) {
- $data .= "\n";
- $data = str_replace( "\n", "\r\n", str_replace( "\r", "", $data ) );
- $data = str_replace( "\n.\r\n", "\n. \r\n", $data );
- return $data;
- }
- }
- new Sender();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement