cleaning code

This commit is contained in:
giuliof 2017-12-16 23:58:36 +01:00
parent 32f8887145
commit b35d8831a3
2 changed files with 137 additions and 323 deletions

347
main.php
View File

@ -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." $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); ORDER BY DateTime ASC LIMIT 1 OFFSET $offset") or error("Can't make the query, SQL error ".$sql->error);
//while($row = $query->fetch_assoc()) { //while($row = $query->fetch_assoc()) {
$row = $query->fetch_assoc(); $row = $query->fetch_assoc();
$mc->set($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']) $mc->set($chatID.MC_DELETE_SCHEDULED_ID, $row['ID'])
or $mc->replace($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']); or $mc->replace($chatID.MC_DELETE_SCHEDULED_ID, $row['ID']);
//~ $query = API_URL . API_TOKEN . '/forwardMessage?' . //~ $query = API_URL . API_TOKEN . '/forwardMessage?' .
//~ 'chat_id=' . urlencode($chatID) . //~ 'chat_id=' . urlencode($chatID) .
//~ '&from_chat_id=' . urlencode($row['ChatID']) . //~ '&from_chat_id=' . urlencode($row['ChatID']) .
//~ '&message_id=' . urlencode($row['MessageID']); //~ '&message_id=' . urlencode($row['MessageID']);
//~ $answer = file_get_contents($query); //~ $answer = file_get_contents($query);
//~ $answer = json_decode($answer); //~ $answer = json_decode($answer);
$sql->close(); $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<i>$row[Author]</i>"; // 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);
$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<i>$row[Author]</i>"; // 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; break;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END STATE_IDLE * END STATE_IDLE
@ -293,16 +271,11 @@ if (isset ($row->callback_query)) {
'day' => null '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 (); $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?"; $text="Quando vuoi inviare il messaggio?";
$reply = json_encode($container);
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
//~ "&message_id=".$inlineID . //~ "&message_id=".$inlineID .
//~ "&text=".urlencode($text). //~ "&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"); $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 // Display time inline keyboard
$container = array(); $kbd = new InlineKbd;
$riga = array();
$firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']? $firstHour = date('Ynj') == $currentTab['year'].$currentTab['month'].$currentTab['day']?
date('G')+1 : 8; date('G')+1 : 8;
for ($i = 8; $i <= 21; $i++) { for ($i = 8; $i <= 21; $i++) {
if ($i < $firstHour) { if ($i < $firstHour)
$riga[] = array( $kbd->insertItem(" ", null);
"text" => " ", else
"callback_data" => "null" $kbd->insertItem("$i".":00", $i);
);
}
else {
$riga[] = array(
"text" => "$i".":00",
"callback_data" => "$i"
);
}
if ($i == 14) { if ($i == 14)
$container[] = $riga; $kbd->pushLine();
$riga = array();
}
} }
$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?"; $text="A che ora vuoi inviare il messaggio?";
$reply = json_encode($container); $reply = json_encode($container);
@ -421,19 +380,15 @@ if (isset ($row->callback_query)) {
//~ "&text=".urlencode($text)."&reply_markup=".$reply; //~ "&text=".urlencode($text)."&reply_markup=".$reply;
//~ file_get_contents($url); //~ 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(); $mc->replace($chatID.MC_STATUS, STATE_WAIT_TIME) or die();
break; break;
} }
// If answer is < or > remain in current state and update calendar // If answer is < or > remain in current state and update calendar
$container = getCalendarTab($currentTab['month'], $currentTab['year']); $reply = getCalendarTab($currentTab['month'], $currentTab['year']);
$resp = array(
"inline_keyboard" => $container);
$reply = json_encode($resp);
//~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID). //~ $url = API_URL.API_TOKEN."/editMessageText?chat_id=".($chatID).
//~ "&message_id=".$inlineID . //~ "&message_id=".$inlineID .
@ -471,61 +426,10 @@ if (isset ($row->callback_query)) {
$currentTab = $mc->get($chatID.MC_DATE_MSG); $currentTab = $mc->get($chatID.MC_DATE_MSG);
$msg = $mc->get($chatID.MC_FORWARD_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)."</$parse_char>";
$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) // add to database (mysql)
$sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB); $sql = new mysqli('localhost', MYSQL_USER, MYSQL_PASSWORD, MYSQL_DB);
@ -562,10 +466,6 @@ if (isset ($row->callback_query)) {
$text = "Programmazione avvenuta con successo"; $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); editMessageText($chatID, $inlineID, $text);
break; break;
@ -583,10 +483,6 @@ elseif (isset($row->message)) {
// Check if it was thrown by admitted users // Check if it was thrown by admitted users
in_array($row->message->from->username, BOT_ALLOWED_USERS) or not_authorized($chatID); in_array($row->message->from->username, BOT_ALLOWED_USERS) or not_authorized($chatID);
////////
////$status = getStatus($chatID);
////////////////
// Finally, check actual state and do things // Finally, check actual state and do things
$status = $mc->get($chatID.MC_STATUS); $status = $mc->get($chatID.MC_STATUS);
if (!$status) { if (!$status) {
@ -606,18 +502,15 @@ elseif (isset($row->message)) {
switch ($command['command']) { switch ($command['command']) {
case '/help': case '/help':
$resp = array("remove_keyboard" => true); case '/start':
$resp = json_encode($resp); // Send some explainations about this bot
sendMessage($chatID,$WELCOME_MESSAGE);
$query = API_URL . API_TOKEN . '/sendmessage?' .
'chat_id=' . urlencode($chatID) .
'&text='.urlencode($WELCOME_MESSAGE).
'&reply_markup='.$resp;
file_get_contents($query);
break; break;
case '/edit': case '/edit':
// not yet implemented // 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; break;
case '/list': case '/list':
@ -642,37 +535,17 @@ elseif (isset($row->message)) {
$sql->close(); $sql->close();
$container = array( $kbd = new InlineKbd;
array( $kbd->insertItem($row['DateTimeFormat'], "null");
array( $kbd->pushLine();
"text" => $row['DateTimeFormat'], $kbd->insertItem(" ","null");
"callback_data" => "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'] != ''){ if ($row['Text'] != ''){
$text="$row[Text]\n<i>$row[Author]</i>"; // NOME CANALE ".API_CHANNEL_ID."?"; $text="$row[Text]\n<i>$row[Author]</i>"; // NOME CANALE ".API_CHANNEL_ID."?";
@ -681,7 +554,7 @@ elseif (isset($row->message)) {
$text="Non testo puro"; $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); $answer = json_decode($answer);
// Should forward the message if is not pure text // Should forward the message if is not pure text
//forwardMessage($chatID, $row['ChatID'], $row['MessageID']); //forwardMessage($chatID, $row['ChatID'], $row['MessageID']);
@ -695,6 +568,7 @@ elseif (isset($row->message)) {
break; break;
default: default:
sendMessage($chatID,"Non è un comando valido, leggi i comandi digitando /help");
warning("$command[command] is not a valid command"); warning("$command[command] is not a valid command");
} }
} }
@ -702,41 +576,18 @@ elseif (isset($row->message)) {
// you probably want to forward a message? // you probably want to forward a message?
// Check what kind of message is this // Check what kind of message is this
$container[] = array( $kbd = new InlineKbd;
array( $kbd->insertItem("SI ".$EMOJI_THUMBSUP,MSG_YES);
"text" => "SI ".$EMOJI_THUMBSUP, $kbd->insertItem("NO ".$EMOJI_THUMBSDOWN,MSG_NO);
"callback_data" => MSG_YES $kbd->insertItem("Programma ".$EMOJI_CLOCK,MSG_SCHEDULE);
), $kbd->pushLine();
array(
"text" => "NO ".$EMOJI_THUMBSDOWN,
"callback_data" => MSG_NO
),
array(
"text" => "Programma ".$EMOJI_CLOCK,
"callback_data" => MSG_SCHEDULE
)
);
// A photo // This bot accepts only text, images, videos and locations
if (isset($row->message->photo)) { if (!(isset($row->message->photo) or isset($row->message->location)
// at the moment photo are forwardable but no schedulable or isset($row->message->video) or isset($row->message->text) )) {
}
// 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 {
sendMessage($chatID, "Questa roba non va bene"); sendMessage($chatID, "Questa roba non va bene");
error("Not supported content"); error("Not supported content");
die(); // Program stops here
} }
$mc->set($chatID.MC_FORWARD_MSG, $row) $mc->set($chatID.MC_FORWARD_MSG, $row)
@ -745,29 +596,19 @@ elseif (isset($row->message)) {
// SetUp inline messages (OK, NO, SCHEDULE) // 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."?"; $text="Vuoi condividere sul canale?"; // NOME CANALE ".API_CHANNEL_ID."?";
$query = API_URL.API_TOKEN."/sendmessage?chat_id=".($row->message->chat->id). $query = sendMessage($row->message->chat->id,
"&text=".urlencode($text). $text,
"&reply_markup=".$reply; null, false, false, null,
$answer = file_get_contents($query); $kbd->getKeyboard());
//if (DEBUG) botlog("\nINLINE ANSWER\n\n".$answer);
$answer = json_decode($answer); //~ $answer = file_get_contents($query);
$answer = json_decode($query);
$inlineID = $answer->result->message_id; $inlineID = $answer->result->message_id;
if (DEBUG) info("Callback message id: $inlineID"); 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->replace($chatID.MC_STATUS, STATE_MSG_ANSWER);
$mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID); $mc->set($chatID.MC_INLINE_ID, $inlineID) or $mc->replace($chatID.MC_INLINE_ID, $inlineID);

View File

@ -140,23 +140,13 @@
// Makes the calendar inline keyboard // Makes the calendar inline keyboard
function getCalendarTab ($month, $year) { function getCalendarTab ($month, $year) {
$container = array(); $kbd = new InlineKbd;
$riga = array();
$days = array("L", "M", "M", "G", "V", "S", "D"); $days = array("L", "M", "M", "G", "V", "S", "D");
for ($i = 0; $i < 7; $i++) { for ($i = 0; $i < 7; $i++)
$element = array( $kbd->insertItem("$days[$i]", "null");
"text" => "$days[$i]", $kbd->pushLine();
"callback_data" => "null"
);
$riga[] = $element;
}
$container[] = $riga;
$riga = array();
$daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year); $daysInMonth = cal_days_in_month (CAL_GREGORIAN , $month , $year);
$date = $year."-".$month; $date = $year."-".$month;
@ -166,75 +156,36 @@
$firstDay = date("N", strtotime($date.'-'.$startingDay)); $firstDay = date("N", strtotime($date.'-'.$startingDay));
// blank buttons // blank buttons
for ($i = 1; $i < $firstDay; $i++) { for ($i = 1; $i < $firstDay; $i++)
$element = array( $kbd->insertItem(" ", "null");
"text" => " ",
"callback_data" => "null"
);
$riga[] = $element;
}
for ($numDay = $startingDay; $numDay <= $daysInMonth; $numDay++, $i++) { for ($numDay = $startingDay; $numDay <= $daysInMonth; $numDay++, $i++) {
//if ($today <= $numDay) { $kbd->insertItem("$numDay", "$numDay");
$element = array(
"text" => "$numDay",
"callback_data" => "$numDay"
);
//}
//else {
/* $element = array(
"text" => " ",
"callback_data" => "null"
);*/
//}
$riga[] = $element;
if ($i >= 7) { if ($i >= 7) {
$container[] = $riga; $kbd->pushLine();
$riga = array();
$i = 0; $i = 0;
} }
} }
// empty buttons // empty buttons
if ($i < 7) {
for (; $i > 1 and $i <= 7; $i++) { for (; $i > 1 and $i <= 7; $i++)
$element = array( $kbd->insertItem(" ", "null");
"text" => " ", $kbd->pushLine();
"callback_data" => "null"
);
$riga[] = $element;
}
if (!empty($riga)) {
$container[] = $riga;
} }
$container[] = array( if ($isCurrentMonth)
$isCurrentMonth ? $kbd->insertItem(" ", "null");
array( else
"text" => " ", $kbd->insertItem("<", "<");
"callback_data" => "null" $kbd->insertItem("$EMOJI_X Annulla",MSG_ABORT);
) $kbd->insertItem(">", ">");
:
array(
"text" => "<",
"callback_data" => "<"
),
array(
"text" => "$EMOJI_X Annulla",
"callback_data" => MSG_ABORT
),
array(
"text" => ">",
"callback_data" => ">"
));
return $container; $kbd->pushLine();
return $kbd->getKeyboard();
} }
@ -257,4 +208,26 @@
die(); 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);
}
}
?> ?>