message)) { if (isset($row->message->from->username)) { if (DEBUG) botlog('[II] ' . $row->message->from->username . ' has written «' . $row->message->text . '»' . "\n"); if (! isCommand($row->message)) { if (in_array($row->message->from->username, BOT_ALLOWED_USERS)) { if (DEBUG) botlog( ' Telegram: forwarding to channel... '); $r = forwardChannel($row->message); if (DEBUG) botlog(($r ? 'done' : 'error') . "\n"); if (DEBUG) botlog(' Mail: mailing to address... '); $r = sendEmail($row->message); if (DEBUG) botlog(($r ? 'done' : 'not done') . "\n"); } else if (DEBUG) botlog('[EE] User ' . $row->message->from->username . ' not authorized!' . "\n"); } else if (DEBUG) botlog('[WW] Command not allowed' . "\n"); } else if (DEBUG) botlog('[EE] from->username not set!' . "\n"); } echo '{}' . "\n"; botlog('[II] End at ' . date('Y-m-d H:i:s') . ' ====================' . "\n"); /* ===== END of MAIN PROGRAM ===== */ /* ===== FUNCTIONS ===== */ function forwardChannel($message) { $query = API_URL . API_TOKEN . '/forwardMessage?' . 'chat_id=' . urlencode(API_CHANNEL_ID) . '&from_chat_id=' . urlencode($message->chat->id) . '&message_id=' . urlencode($message->message_id); if (DEBUG) botlog($query); return file_get_contents($query); } function sendEmail($message) { $link = isLink($message); if ($link) { $title = getHtmlTitle($link); if ($title) { $subject = $title; $text = mb_convert_encoding($message->text, 'UTF-8'); $headers = 'From: ' . MAIL_FROM_ADDR . "\n" . 'Content-Type: text/plain; charset=UTF-8'; if (mail(MAIL_TO_ADDR, $subject, $text, $headers)) return true; } } return false; } /* * Retrieves link content, then, if it is HTML, returns the title (if any), else returns false * http://stackoverflow.com/questions/399332/fastest-way-to-retrieve-a-title-in-php */ function getHtmlTitle($link) { $html = file_get_contents($link); if ($html) { if (! preg_match('/