From b35d8831a350e7c140d0b44f259d3cc80c270978 Mon Sep 17 00:00:00 2001 From: giuliof Date: Sat, 16 Dec 2017 23:58:36 +0100 Subject: [PATCH] cleaning code --- main.php | 347 +++++++++++++++-------------------------------------- shared.php | 113 +++++++---------- 2 files changed, 137 insertions(+), 323 deletions(-) diff --git a/main.php b/main.php index 63a8aa9..29b3839 100644 --- a/main.php +++ b/main.php @@ -146,71 +146,49 @@ if (isset ($row->callback_query)) { $query = $sql->query("SELECT DATE_FORMAT(DateTime,'%d/%m/%Y ore %H') as DateTimeFormat,Text,Author,ID FROM ".MYSQL_TABLE." ORDER BY DateTime ASC LIMIT 1 OFFSET $offset") or error("Can't make the query, SQL error ".$sql->error); - //while($row = $query->fetch_assoc()) { - $row = $query->fetch_assoc(); - $mc->set($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']) - or $mc->replace($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']); - //~ $query = API_URL . API_TOKEN . '/forwardMessage?' . - //~ 'chat_id=' . urlencode($chatID) . - //~ '&from_chat_id=' . urlencode($row['ChatID']) . - //~ '&message_id=' . urlencode($row['MessageID']); - //~ $answer = file_get_contents($query); - //~ $answer = json_decode($answer); - $sql->close(); - - $container = array( - array( - array( - "text" => $row['DateTimeFormat'], - "callback_data" => "null" - ))); - - $container[] = array( - $offset > 0? - array( - "text" => "<", - "callback_data" => "<" - ) - : - array( - "text" => " ", - "callback_data" => "null" - ), - array( - "text" => "$EMOJI_THUMBSDOWN Canc", - "callback_data" => MSG_DELETE - ), - $offset < $numbers-1 ? - array( - "text" => ">", - "callback_data" => ">" - ) - : - array( - "text" => " ", - "callback_data" => "null" - ) - ); - - $resp = array( - "inline_keyboard" => $container); - $reply = json_encode($resp); - - $text="$row[Text]\n$row[Author]"; // NOME CANALE ".API_CHANNEL_ID."?"; - - //~ $query = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). - //~ "&message_id=".$inlineID. - //~ "&text=".urlencode($text). - //~ "&parse_mode=".urlencode("Markdown"). - //~ "&reply_markup=".$reply; - //~ $answer = file_get_contents($query); - $answer = editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); - $answer = json_decode($answer); + //while($row = $query->fetch_assoc()) { + $row = $query->fetch_assoc(); + $mc->set($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']) + or $mc->replace($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']); + //~ $query = API_URL . API_TOKEN . '/forwardMessage?' . + //~ 'chat_id=' . urlencode($chatID) . + //~ '&from_chat_id=' . urlencode($row['ChatID']) . + //~ '&message_id=' . urlencode($row['MessageID']); + //~ $answer = file_get_contents($query); + //~ $answer = json_decode($answer); + $sql->close(); - $mc->set($chatID.MC_LIST_NUMBER, $offset) - or $mc->replace($chatID.MC_LIST_NUMBER, $offset); + + $kbd = new InlineKbd; + $kbd->insertItem($row['DateTimeFormat'], "null"); + $kbd->pushLine(); + if ($offset > 0) + $kbd->insertItem("<","<"); + else + $kbd->insertItem(" ","null"); + $kbd->insertItem("$EMOJI_THUMBSDOWN Canc",MSG_DELETE); + if ($offset < $numbers-1) + $kbd->insertItem(">",">"); + else + $kbd->insertItem(" ","null"); + $kbd->pushLine(); - die(); + $text="$row[Text]\n$row[Author]"; // NOME CANALE ".API_CHANNEL_ID."?"; + + //~ $query = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). + //~ "&message_id=".$inlineID. + //~ "&text=".urlencode($text). + //~ "&parse_mode=".urlencode("Markdown"). + //~ "&reply_markup=".$reply; + //~ $answer = file_get_contents($query); + $answer = editMessageText($chatID, $inlineID, $text, + "HTML", false, $kbd->getKeyboard() ); + $answer = json_decode($answer); + + $mc->set($chatID.MC_LIST_NUMBER, $offset) + or $mc->replace($chatID.MC_LIST_NUMBER, $offset); + + die(); break; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * END STATE_IDLE @@ -293,16 +271,11 @@ if (isset ($row->callback_query)) { 'day' => null ); - $container = getCalendarTab($currentTab['month'], $currentTab['year']); + $reply = getCalendarTab($currentTab['month'], $currentTab['year']); $mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or die (); - $container = array( - "inline_keyboard" => $container); - $text="Quando vuoi inviare il messaggio?"; - $reply = json_encode($container); - //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ "&message_id=".$inlineID . //~ "&text=".urlencode($text). @@ -378,41 +351,27 @@ if (isset ($row->callback_query)) { $mc->set($chatID.MC_DATE_MSG, $currentTab) or $mc->replace($chatID.MC_DATE_MSG, $currentTab) or die ("Failed to save data at Memcached server"); // Display time inline keyboard - $container = array(); - $riga = array(); - + $kbd = new InlineKbd; + $firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']? date('G')+1 : 8; for ($i = 8; $i <= 21; $i++) { - if ($i < $firstHour) { - $riga[] = array( - "text" => " ", - "callback_data" => "null" - ); - } - else { - $riga[] = array( - "text" => "$i".":00", - "callback_data" => "$i" - ); - } + if ($i < $firstHour) + $kbd->insertItem(" ", null); + else + $kbd->insertItem("$i".":00", $i); - if ($i == 14) { - $container[] = $riga; - $riga = array(); - } + if ($i == 14) + $kbd->pushLine(); } - $container[] = $riga; + $kbd->pushLine(); + + $kbd->insertItem("$EMOJI_X Annulla", MSG_ABORT); + + $kbd->pushLine(); - $container[] = array(array( - "text" => "$EMOJI_X Annulla", - "callback_data" => MSG_ABORT - )); - $container = array( - "inline_keyboard" => $container); - $text="A che ora vuoi inviare il messaggio?"; $reply = json_encode($container); @@ -421,19 +380,15 @@ if (isset ($row->callback_query)) { //~ "&text=".urlencode($text)."&reply_markup=".$reply; //~ file_get_contents($url); - editMessageText($chatID, $inlineID, $text, "HTML", false, $reply); + editMessageText($chatID, $inlineID, $text, "HTML", + false, $kbd->getKeyboard()); $mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die(); break; } // If answer is < or > remain in current state and update calendar - $container = getCalendarTab($currentTab['month'], $currentTab['year']); - - $resp = array( - "inline_keyboard" => $container); - - $reply = json_encode($resp); + $reply = getCalendarTab($currentTab['month'], $currentTab['year']); //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ "&message_id=".$inlineID . @@ -471,61 +426,10 @@ if (isset ($row->callback_query)) { $currentTab = $mc->get($chatID.MC_DATE_MSG); $msg = $mc->get($chatID.MC_FORWARD_MSG); - // BEGIN TextEscaper - // - - // 1- replace all * and _ with \* and \_ - //~ $text = $msg->message->text; - //~ $text = str_replace(array("_","*"), array("\_","\*"), $text); - - // 2- insert text formatting - if (isset($msg->message->entities)) - { - $elements = array(); - $string_pointer = 0; - // split text for each entity - foreach ($msg->message->entities as $value) { - switch($value->type) { - case "italic": - $parse_char = 'i'; - break; - case "bold": - $parse_char = 'b'; - break; - case "code": - $parse_char = 'code'; - break; - default: - continue 2; // exit from switch and continue foreach - } - - if (DEBUG) { - info("Offset: $value->offset\nLength: $value->length"); - } - - $no_format_string = mb_substr($msg->message->text, $string_pointer, $value->offset-$string_pointer); - if (DEBUG) info("Stringa no format: $no_format_string"); - $elements[] = htmlspecialchars($no_format_string); - - $format_string = mb_substr($msg->message->text, $value->offset, $value->length); - if (DEBUG) info("Stringa format: $format_string"); - $elements[] = "<$parse_char>".htmlspecialchars($format_string).""; - - $string_pointer = $value->offset+$value->length; - } - if ($string_pointer != mb_strlen($msg->message->text)) { - $no_format_string = mb_substr($msg->message->text, $string_pointer); - $elements[] = htmlspecialchars($no_format_string); - } - $text = implode($elements); - } - else - $text = htmlspecialchars($msg->message->text); - - - - // END TextEscaper + // Message preview in scheduling list is not formatted! + $text = htmlspecialchars($msg->message->text); + // add to database (mysql) $sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); @@ -562,10 +466,6 @@ if (isset ($row->callback_query)) { $text = "Programmazione avvenuta con successo"; - //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). - //~ "&message_id=".$inlineID . - //~ "&text=".urlencode($text); - //~ file_get_contents($url); editMessageText($chatID, $inlineID, $text); break; @@ -583,10 +483,6 @@ elseif (isset($row->message)) { // Check if it was thrown by admitted users in_array($row->message->from->username, BOT_ALLOWED_USERS) or not_authorized($chatID); - //////// - ////$status = getStatus($chatID); - //////////////// - // Finally, check actual state and do things $status = $mc->get($chatID.MC_STATUS); if (!$status) { @@ -606,18 +502,15 @@ elseif (isset($row->message)) { switch ($command['command']) { case '/help': - $resp = array("remove_keyboard" => true); - $resp = json_encode($resp); - - $query = API_URL . API_TOKEN . '/sendmessage?' . - 'chat_id=' . urlencode($chatID) . - '&text='.urlencode($WELCOME_MESSAGE). - '&reply_markup='.$resp; - file_get_contents($query); + case '/start': + // Send some explainations about this bot + sendMessage($chatID,$WELCOME_MESSAGE); break; case '/edit': // not yet implemented + sendMessage($chatID, "Bravo! Hai trovato un comando non ancora implementato. Apri il codice su GitHub e lavoraci su", + "HTML", false, false, null, $reply); break; case '/list': @@ -642,37 +535,17 @@ elseif (isset($row->message)) { $sql->close(); - $container = array( - array( - array( - "text" => $row['DateTimeFormat'], - "callback_data" => "null" - ))); + $kbd = new InlineKbd; + $kbd->insertItem($row['DateTimeFormat'], "null"); + $kbd->pushLine(); + $kbd->insertItem(" ","null"); + $kbd->insertItem("$EMOJI_THUMBSDOWN Canc", MSG_DELETE); + if ($numbers > 1) + $kbd->insertItem(">", ">"); + else + $kbd->insertItem("","null"); + $kbd->pushLine(); - $container[] = array( - array( - "text" => " ", - "callback_data" => "null" - ), - array( - "text" => "$EMOJI_THUMBSDOWN Canc", - "callback_data" => MSG_DELETE - ), - $numbers > 1 ? - array( - "text" => ">", - "callback_data" => ">" - ) - : - array( - "text" => " ", - "callback_data" => "null" - ) - ); - - $resp = array( - "inline_keyboard" => $container); - $reply = json_encode($resp); if ($row['Text'] != ''){ $text="$row[Text]\n$row[Author]"; // NOME CANALE ".API_CHANNEL_ID."?"; @@ -681,7 +554,7 @@ elseif (isset($row->message)) { $text="Non testo puro"; - $answer = sendMessage($chatID, $text, "HTML", false, false, null, $reply); + $answer = sendMessage($chatID, $text, "HTML", false, false, null, $kbd->getKeyboard()); $answer = json_decode($answer); // Should forward the message if is not pure text //forwardMessage($chatID, $row['ChatID'], $row['MessageID']); @@ -695,6 +568,7 @@ elseif (isset($row->message)) { break; default: + sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help"); warning("$command[command] is not a valid command"); } } @@ -702,41 +576,18 @@ elseif (isset($row->message)) { // you probably want to forward a message? // Check what kind of message is this - $container[] = array( - array( - "text" => "SI ".$EMOJI_THUMBSUP, - "callback_data" => MSG_YES - ), - array( - "text" => "NO ".$EMOJI_THUMBSDOWN, - "callback_data" => MSG_NO - ), - array( - "text" => "Programma ".$EMOJI_CLOCK, - "callback_data" => MSG_SCHEDULE - ) - ); + $kbd = new InlineKbd; + $kbd->insertItem("SI ".$EMOJI_THUMBSUP,MSG_YES); + $kbd->insertItem("NO ".$EMOJI_THUMBSDOWN,MSG_NO); + $kbd->insertItem("Programma ".$EMOJI_CLOCK,MSG_SCHEDULE); + $kbd->pushLine(); - // A photo - if (isset($row->message->photo)) { - // at the moment photo are forwardable but no schedulable - } - // A location or a video - elseif (isset($row->message->location) - or isset($row->message->video)) { - sendMessage($chatID, "Presto saprò inviare questo tipo di post"); - warning("Not yet supported content"); - die(); - } - // A common text message - elseif (isset($row->message->text)) { - //.... nothing - } - // else... this is not supported - else { + // This bot accepts only text, images, videos and locations + if (!(isset($row->message->photo) or isset($row->message->location) + or isset($row->message->video) or isset($row->message->text) )) { sendMessage($chatID, "Questa roba non va bene"); error("Not supported content"); - die(); + // Program stops here } $mc->set($chatID.MC_FORWARD_MSG, $row) @@ -745,29 +596,19 @@ elseif (isset($row->message)) { // SetUp inline messages (OK, NO, SCHEDULE) - $resp = array( - "inline_keyboard" => $container); - $reply = json_encode($resp); - $text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?"; - $query = API_URL.API_TOKEN."/sendmessage?chat_id=".($row->message->chat->id). - "&text=".urlencode($text). - "&reply_markup=".$reply; - $answer = file_get_contents($query); - //if (DEBUG) botlog("\nINLINE ANSWER\n\n".$answer); - $answer = json_decode($answer); + $query = sendMessage($row->message->chat->id, + $text, + null, false, false, null, + $kbd->getKeyboard()); + + //~ $answer = file_get_contents($query); + $answer = json_decode($query); $inlineID = $answer->result->message_id; if (DEBUG) info("Callback message id: $inlineID"); - // DEBUGGING strlen - $mi_pigli_per_il_culo = mb_convert_encoding($row->message->text, 'UTF-16', 'UTF-8'); - if (DEBUG) if(isset($row->message->text)) {info("String length: ".mb_strlen($row->message->text, "UTF-8")); - info("String encoding: ".mb_detect_encoding($row->message->text)); - info("String length: ".mb_strlen($mi_pigli_per_il_culo, "UTF-16")); - info("String encoding: ".mb_detect_encoding($mi_pigli_per_il_culo)); - } $mc->replace($chatID.MC_STATUS, STATE_MSG_ANSWER); $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID); diff --git a/shared.php b/shared.php index d1903ba..2dd0f1e 100644 --- a/shared.php +++ b/shared.php @@ -140,23 +140,13 @@ // Makes the calendar inline keyboard function getCalendarTab ($month, $year) { - $container = array(); - $riga = array(); + $kbd = new InlineKbd; $days = array("L", "M", "M", "G", "V", "S", "D"); - for ($i = 0; $i < 7; $i++) { - $element = array( - "text" => "$days[$i]", - "callback_data" => "null" - ); - - $riga[] = $element; - } - - - $container[] = $riga; - $riga = array(); + for ($i = 0; $i < 7; $i++) + $kbd->insertItem("$days[$i]", "null"); + $kbd->pushLine(); $daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year); $date = $year."-".$month; @@ -166,75 +156,36 @@ $firstDay = date("N", strtotime($date.'-'.$startingDay)); // blank buttons - for ($i = 1; $i < $firstDay; $i++) { - $element = array( - "text" => " ", - "callback_data" => "null" - ); - - $riga[] = $element; - } + for ($i = 1; $i < $firstDay; $i++) + $kbd->insertItem(" ", "null"); for ($numDay = $startingDay; $numDay <= $daysInMonth; $numDay++, $i++) { - //if ($today <= $numDay) { - $element = array( - "text" => "$numDay", - "callback_data" => "$numDay" - ); - //} - //else { - /* $element = array( - "text" => " ", - "callback_data" => "null" - );*/ - //} - - $riga[] = $element; + $kbd->insertItem("$numDay", "$numDay"); if ($i >= 7) { - $container[] = $riga; - $riga = array(); + $kbd->pushLine(); $i = 0; } } // empty buttons - - for (; $i > 1 and $i <= 7; $i++) { - $element = array( - "text" => " ", - "callback_data" => "null" - ); - - $riga[] = $element; - } - - if (!empty($riga)) { - $container[] = $riga; + if ($i < 7) { + for (; $i > 1 and $i <= 7; $i++) + $kbd->insertItem(" ", "null"); + $kbd->pushLine(); } - $container[] = array( - $isCurrentMonth ? - array( - "text" => " ", - "callback_data" => "null" - ) - : - array( - "text" => "<", - "callback_data" => "<" - ), - array( - "text" => "$EMOJI_X Annulla", - "callback_data" => MSG_ABORT - ), - array( - "text" => ">", - "callback_data" => ">" - )); + if ($isCurrentMonth) + $kbd->insertItem(" ", "null"); + else + $kbd->insertItem("<", "<"); + $kbd->insertItem("$EMOJI_X Annulla",MSG_ABORT); + $kbd->insertItem(">", ">"); - return $container; + $kbd->pushLine(); + + return $kbd->getKeyboard(); } @@ -257,4 +208,26 @@ die(); } + + + class InlineKbd + { + private $keyboard = array(); + private $currentLine = array(); + + public function pushLine() { + $this->keyboard[] = $this->currentLine; + $this->currentLine = array(); + } + public function insertItem($text, $callback_data) { + $this->currentLine[] = array( + "text" => $text, + "callback_data" => $callback_data + ); + } + public function getKeyboard() { + $kbd = array("inline_keyboard" => $this->keyboard); + return json_encode($kbd); + } + } ?>