static function aliases($tv = NULL) { $tv = funcs::utfToAscii($tv); $dane = file(self::$aliases); $return = array(); foreach ($dane as $line) { $line = trim($line); if (empty($line) or substr($line, 0, 1) == '#') { continue; } $line = explode("\t", $line); for ($i = 0; $i < count($line); $i++) { if ($tv !== NULL and funcs::utfToAscii($line[$i]) == $tv) { return $line[0]; } else { $return[$line[$i]] = $line[0]; } } } if ($tv !== NULL) { return FALSE; } else { return $return; } }
/** * Na podstawie nieprzetworzonej wiadomości ({@link BotMessage::$rawText}) * metoda ustawia wszystkie pola klasy. * @param string $value Nieprzetworzona wiadomość */ function setText($value) { $this->rawText = $value; $value = trim($value); $this->text = funcs::utfToAscii($value); $this->command = funcs::utfToAscii(trim(strtok($value, " \t\r\n"))); $this->args = trim(strtok('')); }
function imieniny($msg, $params) { $arg = funcs::utfToAscii($msg->args); if (empty($arg)) { return new BotMsg('Nie podano imienia!<br />' . "\n" . '<br />' . "\n" . '<u>Przykłady:</u><br />' . "\n" . 'imieniny Adama<br />' . "\n" . 'imieniny Ewy'); } include './data/data/imieniny.php'; if (!isset($imiona[$arg])) { return new BotMsg('Nie znaleziono imienia w bazie. Pamiętaj, by podać imię w dopełniaczu liczby pojedynczej!<br />' . "\n" . '<br />' . "\n" . '<u>Przykłady:</u><br />' . "\n" . 'imieniny Adama<br />' . "\n" . 'imieniny Ewy'); } $txt = array(); foreach ($imiona[$arg] as $dzien) { $dzien = explode('.', $dzien); $txt[] = $dzien[0] . ' ' . self::$miesiace[$dzien[1]]; } return new BotMsg('Imieniny ' . ucfirst($arg) . ' są ' . implode(', ', $txt)); }
function handle($msg, $params) { $args = trim($msg->args); if (empty($args)) { return new BotMsg('Funkcja <b>synonimy</b> wymaga argumentu.<br />' . '<br />' . "\n" . '<u>Przykłady:</u><br />' . "\n" . 'synonimy abecadło<br />' . "\n" . 'synonimy wyspa'); } $args = funcs::utfToAscii($args); $fp = fopen('./data/synonimy/thesaurus.res', 'r'); list($rows, $len) = explode('x', trim(fgets($fp, 50))); $start = ftell($fp); $l = 0; $p = $rows - 1; while ($l < $p) { //echo 'L: '.$l.' - P: '.$p."\n"; $s = floor(($l + $p) / 2); $poz = $start + $s * $len; fseek($fp, $poz); $line = fread($fp, $len); $word = strtok($line, ';'); $cmp = strcmp($word, $args); if ($cmp >= 0) { //echo 'Right'."\n"; $p = $s; } else { //echo 'Left'."\n"; $l = $s + 1; } } $poz = $start + $l * $len; fseek($fp, $poz); $i = 1; $return = ''; while (TRUE) { $line = fread($fp, $len); $word = strtok($line, ';'); if ($word != $args) { break; } $return .= '<br />' . "\n" . $i++ . '. ' . htmlspecialchars(rtrim(substr(strstr($line, ';'), 1))); } if (!empty($return)) { return new BotMsg('Znalezione synonimy:' . $return); } else { return new BotMsg('Nie znaleziono synonimów podanego słowa'); } }
static function cmd_kurs($nazwa, $argument) { $argument = funcs::utfToAscii(trim($argument)); if (isset(self::$name2iso[$argument])) { $argument = self::$name2iso[$argument]; } else { $argument = strtoupper($argument); } $waluty_A = unserialize(file_get_contents('./data/kurs/A_kursy.txt')); if (empty($argument)) { $waluty_C = unserialize(file_get_contents('./data/kurs/C_kursy.txt')); $txt = 'Kursy średnie walut obcych z dnia ' . $waluty_A['i_n_f_o']['data'] . ' (tabela NBP nr ' . $waluty_A['i_n_f_o']['tabela'] . ') oraz kursy kupna i sprzedaży z dnia ' . $waluty_C['i_n_f_o']['data'] . ' (tabela NBP nr ' . $waluty_C['i_n_f_o']['tabela'] . ')' . "\n"; foreach ($waluty_C as $kod => $dane) { if ($kod == 'i_n_f_o') { continue; } $txt .= "\n" . $dane['ilosc'] . ' ' . $kod . ' => ' . $waluty_A[$kod]['kurs'] . ' PLN'; $txt .= ' (kupno: ' . $dane['kupno'] . ' PLN, sprzedaż: ' . $dane['sprzedaz'] . ' PLN)'; } GGapi::putText($txt); return TRUE; } if (isset($waluty_A[$argument])) { $txt = 'Kursy średnie walut obcych z dnia ' . $waluty_A['i_n_f_o']['data'] . ' (tabela NBP nr ' . $waluty_A['i_n_f_o']['tabela'] . ')'; $waluty_C = unserialize(file_get_contents('./data/kurs/C_kursy.txt')); if (isset($waluty_C[$argument])) { $txt .= ' oraz kursy kupna i sprzedaży z dnia ' . $waluty_C['i_n_f_o']['data'] . ' (tabela NBP nr ' . $waluty_C['i_n_f_o']['tabela'] . ')'; } $txt .= "\n\n" . $waluty_A[$argument]['ilosc'] . ' ' . $argument . ' => ' . $waluty_A[$argument]['kurs'] . ' PLN'; if (isset($waluty_C[$argument])) { $txt .= ' (kupno: ' . $waluty_C[$argument]['kupno'] . ' PLN, sprzedaż: ' . $waluty_C[$argument]['sprzedaz'] . ' PLN)'; } GGapi::putText($txt); return TRUE; } else { $waluty_B = unserialize(file_get_contents('./data/kurs/B_kursy.txt')); if (!isset($waluty_B[$argument])) { GGapi::putText('Nie znaleziono żądanej waluty. Sprawdź, czy kod waluty jest zgodny z ISO 4217.' . "\n\n"); GGapi::putRichText('Przykłady', FALSE, FALSE, TRUE); GGapi::putRichText("\n" . 'kurs USD' . "\n" . 'kurs EUR'); return FALSE; } GGapi::putText('Kursy średnie walut obcych z dnia ' . $waluty_B['i_n_f_o']['data'] . ' (tabela NBP nr ' . $waluty_B['i_n_f_o']['tabela'] . ')' . "\n\n" . $waluty_B[$argument]['ilosc'] . ' ' . $argument . ' => ' . $waluty_B[$argument]['kurs'] . ' PLN'); return TRUE; } }
private function init() { if (is_file(BOT_TOPDIR . '/cache/functions.sqlite')) { $this->PDO = new PDO('sqlite:' . BOT_TOPDIR . '/cache/functions.sqlite'); $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING); return; } $functions = array(); $modules = glob(BOT_TOPDIR . '/modules/*', GLOB_ONLYDIR); foreach ($modules as $dir) { if (!is_file($dir . '/init.php')) { continue; } $class = (include_once $dir . '/init.php'); if (!$class || !class_exists($class, FALSE)) { continue; } $init = new $class(); if (!$init instanceof BotModuleInit) { continue; } $row = $init->register(); if (!is_array($row)) { $row = array(); } foreach ($row as $name => &$value) { if (!is_array($value)) { unset($row[$name]); continue; } foreach ($value as &$val) { $val['dir'] = '/modules/' . basename($dir) . '/'; $val['init'] = $class; $val['file'] = $val['file']; } $name2 = funcs::utfToAscii(strtolower($name)); if ($name != $name2) { if (isset($row[$name2])) { $row[$name2] = array_merge_recursive($row[$name2], $row[$name]); } else { $row[$name2] = $row[$name]; } unset($row[$name]); } } $functions = array_merge_recursive($functions, $row); } try { $this->PDO = new PDO('sqlite:' . BOT_TOPDIR . '/cache/functions.sqlite'); $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->PDO->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_TO_STRING); $this->PDO->query('CREATE TABLE functions ( name VARCHAR(50), priority INT, dir VARCHAR(255), init VARCHAR(255), file VARCHAR(255), class VARCHAR(255), method VARCHAR(255), params TEXT, PRIMARY KEY ( name ASC, priority ASC ) )'); $this->PDO->query('CREATE INDEX file ON functions (dir, file)'); $st = $this->PDO->prepare('INSERT INTO functions (name, priority, dir, init, file, class, method, params) VALUES (?, ?, ?, ?, ?, ?, ?, ?)'); $this->PDO->beginTransaction(); foreach ($functions as $name => $funcs) { $name = funcs::utfToAscii(strtolower($name)); foreach ($funcs as $priority => $func) { if (!isset($func['params'])) { $func['params'] = NULL; } $st->execute(array($name, $priority, $func['dir'], $func['init'], $func['file'], $func['class'], $func['method'], serialize($func['params']))); } } $this->PDO->commit(); } catch (Exception $e) { @unlink(BOT_TOPDIR . '/cache/functions.sqlite'); throw $e; } }
$plik = file('./thesaurus.txt'); $sqlite = sqlite_open('thesaurus.sqlite') or die(sqlite_error_string(sqlite_last_error($sqlite))); sqlite_query($sqlite, 'CREATE TABLE thesaurus (slowo varchar(255), podobne TEXT)') or die(sqlite_error_string(sqlite_last_error($sqlite))); $len = $rows = 0; foreach ($plik as $line) { if (substr($line, 0, 1) == '#') { continue; } $line = explode(';', iconv('ISO-8859-2', 'UTF-8', rtrim($line))); foreach ($line as $words) { $similar = array(); foreach ($line as $word) { if ($word == $words) { continue; } $similar[] = $word; } $similar = implode(', ', $similar); $len = max($len, strlen($words) + strlen($similar) + 1); $rows++; sqlite_query($sqlite, 'INSERT INTO thesaurus (slowo, podobne) VALUES (\'' . funcs::utfToAscii($words) . '\', \'' . $similar . '\')') or die(sqlite_error_string(sqlite_last_error($sqlite))); } } unset($plik); $fp = fopen('thesaurus.res', 'w'); fwrite($fp, $rows . 'x' . $len . "\n"); $data = sqlite_unbuffered_query($sqlite, 'SELECT * FROM thesaurus ORDER BY slowo ASC'); while ($en = sqlite_fetch_array($data, SQLITE_ASSOC)) { fwrite($fp, str_pad($en['slowo'] . ';' . $en['podobne'], $len, "")); } fclose($fp);
/** * Na podstawie podanej daty zwraca uniksowy znacznik czasu * @param string $date Data w formacie "naturalnym". Np. jutro, 1 stycznia, piątek * @return int Uniksowy znacznik czasu - liczba sekund od północy 1 stycznia 1970 * @see http://www.php.net/date */ static function parse_date($date) { $date = funcs::utfToAscii($date); switch ($date) { case 'jutro': return strtotime('tomorrow'); case 'za tydzien': return strtotime('+1 week'); case 'pojutrze': case 'po jutrze': return strtotime('tomorrow +1 day'); case 'dzis': return mktime(0, 0, 0); case 'teraz': case '': return time(); case 'wczoraj': return strtotime('yesterday'); case 'przedwczoraj': case 'przed wczoraj': return strtotime('yesterday -1 days'); case 'poniedzialek': return strtotime('monday'); case 'wtorek': return strtotime('tuesday'); case 'sroda': return strtotime('wednesday'); case 'czwartek': return strtotime('thursday'); case 'piatek': return strtotime('friday'); case 'sobota': return strtotime('saturday'); case 'niedziela': return strtotime('sunday'); } if (substr($date, 0, 2) == 'za') { $czego = array('sekunde' => 'second', 'sekund' => 'seconds', 'sekundy' => 'seconds', 'minuta' => 'minute', 'minut' => 'minutes', 'minuty' => 'minutes', 'godzine' => 'hour', 'godzin' => 'hours', 'godziny' => 'hours', 'dzien' => 'day', 'dni' => 'days', 'miesiac' => 'month', 'miesiecy' => 'months', 'miesiace' => 'months', 'rok' => 'year', 'lat' => 'years', 'lata' => 'years'); $date = trim(substr($date, 2)); $ile = array('zero' => 0, 'jeden' => 1, 'dwa' => 2, 'trzy' => 3, 'cztery' => 4, 'piec' => 5, 'szesc' => 6, 'siedem' => 7, 'osiem' => 8, 'dziewiec' => 9, 'dziesiec' => 10); foreach ($ile as $key => $num) { if (substr($date, 0, strlen($key)) == $key) { $ile = $num; $done = TRUE; $date = trim(substr($date, strlen($key))); break; } } if ($done) { } elseif (!is_numeric(substr($date, 0, 1))) { $ile = 1; } else { $ile = (int) $date; $date = trim(substr($date, strlen($ile))); } $czego = $czego[$date]; if (!$czego) { return FALSE; } return strtotime('+' . $ile . ' ' . $czego); } if (preg_match('/([0-9]{1,2})\\.([0-9]{1,2}).([0-9]{2,4})/', $date, $test)) { return mktime(0, 0, 0, $test[2], $test[1], $test[3]); } elseif (preg_match('/([0-9]{1,2})\\.([0-9]{1,2})/', $date, $test)) { return mktime(0, 0, 0, $test[2], $test[1]); } elseif (preg_match('/([0-9]{1,2})\\-([0-9]{1,2})\\-([0-9]{2,4})/', $date, $test)) { return mktime(0, 0, 0, $test[2], $test[1], $test[3]); } elseif (preg_match('/([0-9]{1,2})\\s+([a-z]{3,13})\\s+([0-9]{2,4})/', $date, $test) || preg_match('/([0-9]{1,2})\\s+([a-z]{3,13})/', $date, $test)) { $array = array('styczen' => 1, 'stycznia' => 1, 'luty' => 2, 'lutego' => 2, 'marzec' => 3, 'marca' => 3, 'kwiecien' => 4, 'kwietnia' => 4, 'maj' => 5, 'maja' => 5, 'czerwiec' => 6, 'czerwca' => 6, 'lipiec' => 7, 'lipca' => 7, 'sierpien' => 8, 'sierpnia' => 8, 'wrzesien' => 9, 'wrzesnia' => 9, 'pazdziernik' => 10, 'pazdziernika' => 10, 'listopad' => 11, 'listopada' => 11, 'grudzien' => 12, 'grudnia' => 12); if (!isset($array[$test[2]])) { return FALSE; } if (!$test[3]) { $test[3] = date('Y'); } return mktime(0, 0, 0, $array[$test[2]], $test[1], $test[3]); } else { return strtotime($date); } }
function handle($msg, $params) { $arg = funcs::utfToAscii($msg->args); $msg->session->setClass('kino'); if (empty($arg)) { $arg = $msg->session->kino; if (empty($arg)) { return new BotMsg('Podaj nazwę miejscowości i kina.<br />' . "\n" . '<br />' . "\n" . '<u>Przykłady:</u><br />' . "\n" . 'kino Kraków<br />' . "\n" . 'kino Kraków Multikino'); } } else { $arg2 = $msg->session->kino; } /* MIASTO */ $miasta = self::getMiasta(); $miasto_num = $miasto_nazw = ''; if (!$miasta) { return new BotMsg('Przepraszamy, wystąpił bład przy pobieraniu listy miejscowości.'); } foreach ($miasta as $miasto => $numer) { $szukaj = funcs::utfToAscii($miasto); if (($pos = strpos($arg, $szukaj)) !== FALSE) { $miasto_nazw = htmlspecialchars($miasto); $miasto_num = $numer; $arg = trim(str_replace(' ', ' ', substr($arg, 0, $pos) . substr($arg, $pos + strlen($szukaj)))); break; } } if ($miasto_num === '' && !empty($arg2)) { foreach ($miasta as $miasto => $numer) { $szukaj = funcs::utfToAscii($miasto); if (($pos = strpos($arg2, $szukaj)) !== FALSE) { $miasto_nazw = htmlspecialchars($miasto); $miasto_num = $numer; $arg2 = trim(str_replace(' ', ' ', substr($arg2, 0, $pos) . substr($arg2, $pos + strlen($szukaj)))); break; } } } if ($miasto_num === '') { $txt = 'Wybrane miasto nie został odnalezione. Obsługiwane miejscowości:'; $miasto = 'Warszawa'; foreach ($miasta as $miasto => $num) { $txt .= '<br />' . "\n" . htmlspecialchars($miasto); } $txt .= '<br />' . "\n" . '<br />' . "\n" . '<u>Przykład:</u><br />' . "\n" . 'kino ' . htmlspecialchars($miasto); return new BotMsg($txt); } /* KIEDY */ $tydzien = array('niedziela', 'poniedzialek', 'wtorek', 'sroda', 'czwartek', 'piatek', 'sobota'); $data = array('dzis' => '', 'teraz' => '', 'jutro' => '1', 'pojutrze' => '2', 'po jutrze' => '2'); for ($i = 0; $i < 3; $i++) { $data[date('d.m', strtotime('+' . $i . ' day'))] = $i ? $i : ''; $data[date('j.m', strtotime('+' . $i . ' day'))] = $i ? $i : ''; } $czas = ''; foreach ($data as $known => $d) { if (($pos = strpos($arg, $known)) !== FALSE) { $czas = $d; $arg = trim(str_replace(' ', ' ', substr($arg, 0, $pos) . substr($arg, $pos + strlen($known)))); break; } } /* KINO */ $kina = self::getKina($miasto_num, $czas); $kino_num = $kino_nazw = ''; if (!$kina) { return new BotMsg('Przepraszamy, wystąpił bład przy pobieraniu listy kin.'); } if (empty($kina)) { return new BotMsg(($czas == '1' ? 'Jutro' : ($czas == '2' ? 'Pojutrze' : 'Dziś')) . ' żadne filmy nie są wyświetlane w podanym mieście.<br />' . "\n" . '<br />' . "\n" . '<u>Spróbuj też:</u><br />' . "\n" . 'kino ' . $miasto_nazw . ' ' . htmlspecialchars($arg) . ' ' . ($czas != '1' ? 'jutro' : ($czas != '2' ? 'pojutrze' : 'dziś')) . '<br />' . "\n" . 'kino ' . $miasto_nazw . ' ' . htmlspecialchars($arg) . ' ' . ($czas != '' ? 'dziś' : ($czas != '2' ? 'pojutrze' : 'dziś'))); } if (!empty($arg)) { foreach ($kina as $kino => $kino_id) { if (levenshtein(funcs::utfToAscii($kino), $arg, 1, 1, 0) < 2) { $kino_num = $kino_id; $kino_nazw = htmlspecialchars($kino); break; } } } if ($kino_num === '' && !empty($arg2)) { foreach ($kina as $kino => $kino_id) { if (levenshtein(funcs::utfToAscii($kino), $arg2, 1, 1, 0) < 2) { $kino_num = $kino_id; $kino_nazw = htmlspecialchars($kino); break; } } } if ($kino_num === '') { $txt = (!empty($arg) ? 'Podany obiekt nie został znaleziony. ' : '') . 'Dostępne kina w pasujących miastach:'; $kino = ''; foreach ($kina as $kino => $num) { $txt .= '<br />' . "\n" . $miasto_nazw . ' ' . htmlspecialchars($kino); } return new BotMsg($txt . '<br />' . "\n" . '<br />' . "\n" . '<u>Przykład:</u><br />' . "\n" . 'kino ' . $miasto_nazw . ' ' . htmlspecialchars($kino) . ' ' . ($czas == '1' ? 'jutro' : ($czas == '2' ? 'pojutrze' : 'dziś'))); } /* REPERTUAR */ $filmy = self::getKino($miasto_num, $kino_num, $czas); if (!$filmy) { return new BotMsg('Przepraszamy, wystąpił bład przy pobieraniu listy wyświelanych filmów.'); } $txt = '<b>Repertuar dla kina ' . $kino_nazw . ' (' . $miasto_nazw . ') na ' . ($czas == '1' ? 'jutro' : ($czas == '2' ? 'pojutrze' : 'dziś')) . ':</b>'; if (empty($filmy)) { $txt .= '<br />' . "\n" . 'Brak projekcji.'; } else { foreach ($filmy as $film) { $txt .= '<br />' . "\n" . htmlspecialchars($film[0]) . ' ' . htmlspecialchars($film[1]) . ($film[2] != '' ? ' (' . htmlspecialchars($film[2]) . ')' : ''); } } return new BotMsg($txt); }
static function cmd_set($name, $arg) { $arg = self::channel(funcs::utfToAscii($arg), TRUE); if (!$arg) { GGapi::putText('Wybrany kanał nie istnieje! Dostępne kanały: ' . "\n" . '- ' . implode("\n" . '- ', self::channels())); } else { database::add($_GET['from'], 'rss', 'kanal', $arg[1]); GGapi::putText('Kanał ' . $arg[3] . ' został ustawiony jako domyślny. Teraz zamiast:' . "\n" . 'rss ' . $arg[1] . "\n" . 'możesz wpisać samo' . "\n" . 'rss'); } }