PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ticker_v2_notfinished/class.Ticker.inc.php

http://flaimo-php.googlecode.com/
PHP | 266 lines | 223 code | 27 blank | 16 comment | 25 complexity | 5d2332ac68036bdea0f16f572f90896b MD5 | raw file
  1. <?php
  2. include_once 'class.TickerBase.inc.php';
  3. include_once 'class.TickerMessage.inc.php';
  4. class Ticker extends TickerBase {
  5. private $list_size;
  6. private $messages;
  7. private $messagelist;
  8. private $conn;
  9. private $db_table = 'ticker_backup';
  10. CONST COOKIE_NAME = 'ticker_comment';
  11. private $mail_conn_string;
  12. function __construct($list_size = 6) {
  13. if (parent::readINIsettings() === FALSE) {
  14. die ("Couldn't load ticker settings");
  15. } // end if
  16. $this->setListSize($list_size);
  17. } // end constructor
  18. private function setListSize($size = 6) {
  19. $this->list_size = (int) $size;
  20. if (isset($this->messagelist)) {
  21. unset($this->messagelist);
  22. } // end if
  23. if (isset($this->messages)) {
  24. unset($this->messages);
  25. } // end if
  26. } // end function
  27. protected function isValidAuthor($header = '') {
  28. if (strlen(trim($GLOBALS[TICKER_GLOBALNAME]['Settings']['whitelist'])) == 0) {
  29. return (boolean) TRUE; // return true if whitelist is empty
  30. } else {
  31. @$headerstring = (string) $header;
  32. if (strlen(trim($headerstring)) === 0) {
  33. return (boolean) FALSE;
  34. } // end if
  35. unset($headerstring);
  36. }// end if
  37. /* get e-mail address from mail header and compare it with */
  38. $from = $header->fromaddress . ' ' . $header->from[0]->mailbox . '@' . $header->from[0]->host;
  39. /* convert whitelist to an array */
  40. if (!is_array($GLOBALS[TICKER_GLOBALNAME]['Settings']['whitelist'])) {
  41. $GLOBALS[TICKER_GLOBALNAME]['Settings']['whitelist'] = (array) explode(',', $GLOBALS[TICKER_GLOBALNAME]['Settings']['whitelist'])
  42. }// end if
  43. foreach ($GLOBALS[TICKER_GLOBALNAME]['Settings']['whitelist'] as $white) {
  44. if (strpos($from, $white) != FALSE) {
  45. return (boolean) TRUE; // return true if the e-mail was found in the whitelist
  46. } // end if
  47. } // end foreach
  48. unset($whitelist);
  49. unset($from);
  50. return (boolean) FALSE;
  51. } // end function
  52. protected function isValidMail(&$header, &$body) {
  53. $start_string =& $GLOBALS[TICKER_GLOBALNAME]['Settings']['start_string'];
  54. $start_string_length = (int) strlen($start_string);
  55. if (($start_string_length === 0) || (isset($header->subject) && $header->subject == $start_string) || (substr_count($body, $start_string) > 0)) {
  56. return (boolean) TRUE;
  57. } // end if
  58. unset($start_string_length);
  59. return (boolean) FALSE;
  60. } // end function
  61. private function createMailConnString() {
  62. if (!isset($this->mail_conn_string)) {
  63. $mailbox_setting =& $GLOBALS[TICKER_GLOBALNAME]['Mailbox'];
  64. $port =& $mailbox_setting['mail_server_port'];
  65. $mail_type =& $mailbox_setting['mail_server_type'];
  66. $folder =& $mailbox_setting['folder'];
  67. $type = (string) (($mail_type == 'POP3') ? 'pop3' : 'imap');
  68. $conn_string = '{' . $mailbox_setting['mail_server'];
  69. if ($mailbox_setting['mail_server_ssl'] === TRUE) {
  70. if ($mailbox_setting['mail_server_sc'] === TRUE) {
  71. $conn_string .= ':' . $port . '/' . $type . '/ssl/novalidate-cert}';
  72. } else {
  73. $conn_string .= ':' . $port . '/' . $type . '/ssl}' . $folder;
  74. }// end if
  75. } else {
  76. $conn_string .= ':' . $port . '}' . $folder;
  77. }// end if
  78. $this->mail_conn_string = (string) $conn_string;
  79. } // end if
  80. } // end function
  81. protected function addTickerMessage($message = '', $author = '') {
  82. /* if message is empty don't add message */
  83. if (strlen(trim($message)) === 0 || $this->hasMessageWritten() === TRUE) {
  84. return (boolean) FALSE;
  85. } // end if
  86. $settings =& $GLOBALS[TICKER_GLOBALNAME]['Settings'];
  87. /* if name is given, add it to the message string */
  88. $message = (string) $settings['start_string'] . $message;
  89. if (strlen(trim($author)) > 0) {
  90. $message .= ' &#x2013; ' . $author;
  91. } // end if
  92. $this->createMailConnString();
  93. $mbox = imap_open($this->mail_conn_string, $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'],
  94. $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['password']) or die ('Error: ' . imap_last_error());
  95. /* add message to the mailbox */
  96. imap_append($mbox,
  97. $this->mail_conn_string,
  98. 'From: ' . $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'] . "\r\n" .
  99. 'To: ' . $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'] . "\r\n" .
  100. 'Subject: ' . $settings['start_string'] . "\r\n\r\n" .
  101. $message . "\r\n"
  102. );
  103. imap_close($mbox);
  104. unset($message);
  105. /*
  106. mail($GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'],
  107. $GLOBALS[TICKER_GLOBALNAME]['Settings']['start_string'],
  108. $message,
  109. 'From: ' . $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'] . "\r\n" . 'X-Mailer: PHP/' . phpversion());
  110. */
  111. /* set cookie and session that user has written a message (prevent flooding) */
  112. $_SESSION[COOKIE_NAME] = time() + $settings['spamprotection'];
  113. $_COOKIE[COOKIE_NAME] = time() + $settings['spamprotection'];
  114. setcookie(COOKIE_NAME, time(), time()+ $settings['spamprotection']);
  115. //$this->setMessages();
  116. return (boolean) TRUE;
  117. } // end function
  118. protected function hasMessageWritten() {
  119. if ((isset($_SESSION[COOKIE_NAME]) && time() < $_SESSION[COOKIE_NAME]) || isset($_COOKIE[COOKIE_NAME])) {
  120. return (boolean) TRUE;
  121. } // end if
  122. return (boolean) FALSE;
  123. } // end function
  124. protected function setMessages() {
  125. if (!isset($this->messages)) {
  126. $this->createMailConnString();
  127. /* create mailbox connection */
  128. $mbox = imap_open($this->mail_conn_string, $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'], $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['password']) or die ('Error: ' . imap_last_error());
  129. $settings =& $GLOBALS[TICKER_GLOBALNAME]['Settings'];
  130. $start_string =& $settings['start_string'];
  131. $start_string_length = (int) strlen($start_string);
  132. $headers = imap_headers($mbox);
  133. $count_mails = (int) count($headers);
  134. unset($headers);
  135. $counter = (int) $this->list_size;
  136. $this->messages = (array) array();
  137. if ($count_mails > 0) { // if mailbox is not empty
  138. for ($i = $count_mails; $i > 0; $i--) {
  139. $header = imap_header($mbox, $i);
  140. $body = imap_body($mbox, $i);
  141. /* if mail does not fit the rules continue with next
  142. (and maybe delete it) */
  143. if ($this->isValidAuthor($header) === FALSE || $this->isValidMail(&$header, &$body) === FALSE) {
  144. if ($settings['delete_false_mails'] == TRUE) {
  145. imap_delete($mbox, $i);
  146. } // end if
  147. continue;
  148. } // end if
  149. /* only go though the fist XX mails
  150. the rest will be deleted or archived */
  151. if ($counter <= 0) { // do this for all mails
  152. if ($settings['db_backup'] == TRUE) {
  153. $tm_backup = new TickerMessage($i, $body, $header, imap_fetchstructure($mbox, $i));
  154. $this->backupMessage($tm_backup);
  155. } // end if
  156. if ($settings['delete_old_mails'] == TRUE) {
  157. imap_delete($mbox, $i);
  158. } // end if
  159. continue;
  160. } // end if
  161. /* create now message object and stick it into an array */
  162. $tm = new TickerMessage($i, $body, $header, imap_fetchstructure($mbox, $i));
  163. $this->messages[$i] = $tm;
  164. $counter--;
  165. } // end for
  166. unset($header);
  167. unset($body);
  168. unset($tm);
  169. } // end if
  170. imap_expunge($mbox);
  171. imap_close($mbox);
  172. } // end if
  173. } // end function
  174. private function setConnection() {
  175. if (!isset($this->conn)) {
  176. $settings =& $GLOBALS[TICKER_GLOBALNAME]['DB'];
  177. $this->conn = mysql_pconnect($settings['host'], $settings['user'], $settings['password']) or die ('Connection not possible! => ' . mysql_error());
  178. mysql_select_db($settings['database']) or die ('Couldn\'t connect to "' . $this->database . '" => ' . mysql_error());
  179. $this->db_table =& $settings['translation_table'];
  180. } // end if
  181. } // end function
  182. private function backupMessage(&$tm) {
  183. $this->setConnection();
  184. $sql = 'INSERT INTO ' . $this->db_table . '(date, message)';
  185. $sql .= ' VALUES (' . $tm->getTimestamp() . ',"' . addslashes($tm->getText()) . '");';
  186. $result = mysql_query($sql, $this->conn) or die ('Request not possible! SQL Statement: ' . $sql . ' / ' . mysql_error());
  187. } // end function
  188. protected function setMessageList() {
  189. if (!isset($this->messagelist)) {
  190. $this->setMessages();
  191. $this->messagelist = (array) array_keys($this->messages);
  192. } // end if
  193. } // end function
  194. public function getMessages() {
  195. $this->setMessages();
  196. if (isset($this->messages)) {
  197. return (array) $this->messages;
  198. } else {
  199. return (boolean) FALSE;
  200. } // end if
  201. } // end if
  202. public function getMessage($id = -1) {
  203. $this->setMessages();
  204. if (array_key_exists($id, $this->messages)) {
  205. return (object) $this->messages[$id];
  206. } else {
  207. return (boolean) FALSE;
  208. } // end if
  209. } // end if
  210. public function getMessageList() {
  211. $this->setMessageList();
  212. return (array) $this->messagelist;
  213. } // end function
  214. public function getListSize() {
  215. return (int) $this->list_size;
  216. } // end function
  217. public function getStartString() {
  218. return (string) $GLOBALS[TICKER_GLOBALNAME]['Settings']['start_string'];
  219. } // end function
  220. public function getEndString() {
  221. return (string) $GLOBALS[TICKER_GLOBALNAME]['Settings']['end_string'];
  222. } // end function
  223. public function getEmail() {
  224. return (string) $GLOBALS[TICKER_GLOBALNAME]['Mailbox']['email'];
  225. } // end function
  226. public function getMaxMessageSize() {
  227. return (int) $GLOBALS[TICKER_GLOBALNAME]['Settings']['max_length'];
  228. } // end function
  229. } // end class Ticker
  230. ?>