Source for file class.Mailbox.inc

Documentation is available at class.Mailbox.inc

  1. <?php //-*-php-*-
  2. /* ******************************************************************** **
  3. ** Copyright notice **
  4. ** **
  5. ** (c) 1995-2003 PHPOpenChat Development Team **
  6. ** http://phpopenchat.sourceforge.net/ **
  7. ** **
  8. ** All rights reserved **
  9. ** **
  10. ** This script is part of the PHPOpenChat project. The PHPOpenChat **
  11. ** project is free software; you can redistribute it and/or modify **
  12. ** it under the terms of the GNU General Public License as published by **
  13. ** the Free Software Foundation; either version 2 of the License, or **
  14. ** (at your option) any later version. **
  15. ** **
  16. ** The GNU General Public License can be found at **
  17. ** http://www.gnu.org/copyleft/gpl.html. **
  18. ** A copy is found in the textfile GPL and important notices to the **
  19. ** license from the team is found in the textfile LICENSE distributed **
  20. ** with these scripts. **
  21. ** **
  22. ** This script is distributed in the hope that it will be useful, **
  23. ** but WITHOUT ANY WARRANTY; without even the implied warranty of **
  24. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
  25. ** GNU General Public License for more details. **
  26. ** **
  27. ** This copyright notice MUST APPEAR in all copies of the script! **
  28. ** ******************************************************************** */
  29.  
  30. //Get default values
  31. require_once(POC_BASE.'/config.inc.php');
  32. require_once(POC_INCLUDE_PATH.'/class.Logger.inc');
  33.  
  34. /**
  35. * Implements a Mailbox within the chat
  36. *
  37. * @package phpopenchat
  38. * @author Michael Oertel <michael@ortelius.de>
  39. * @access public
  40. * @version $Id: class.Mailbox.inc,v 1.16 2003/08/06 12:19:36 letreo Exp $
  41. */
  42. class Mailbox {
  43. /**
  44. * @var object
  45. * @access private
  46. * @see connect()
  47. */
  48. var $db;
  49. /**
  50. * @var integer
  51. * @see connect()
  52. * @see disconnect()
  53. */
  54. var $connection_count = 0;
  55. /**
  56. * @var array
  57. */
  58. var $mailbox = array();
  59. /**
  60. * @var array
  61. */
  62. var $mails = array();
  63. /**
  64. * @var array
  65. */
  66. var $subjects = array();
  67. /**
  68. * @var array
  69. */
  70. var $bodies = array();
  71. /**
  72. * @var array
  73. */
  74. var $times = array();
  75. /**
  76. * @var array
  77. */
  78. var $recipients = array();
  79. /**
  80. * @var string
  81. */
  82. var $type = '';
  83. /**
  84. * @var integer
  85. */
  86. var $unread_mail_count = 0;
  87. /**
  88. * Constructor
  89. *
  90. * @param $box string
  91. * @desc Implements a mailbox
  92. */
  93. function Mailbox( $box_type )
  94. {
  95. $this->type = $box_type;
  96. switch( $box_type )
  97. {
  98. case'inbox':
  99. $this->connect();
  100. $rs = $this->db->Execute( 'SELECT MAIL FROM poc_mails WHERE RECIPIENT=\''.$_SESSION['chatter']->get_nick().'\' AND TRASHED_BY_RECIPIENT=0');
  101. $this->disconnect();
  102. break;
  103. case'outbox':
  104. $this->connect();
  105. $rs = $this->db->Execute( 'SELECT MAIL FROM poc_mails WHERE SENDER=\''.$_SESSION['chatter']->get_nick().'\' AND TRASHED_BY_SENDER=0 AND NOT(TRASHED_BY_RECIPIENT=1 AND RECIPIENT=\''.$_SESSION['chatter']->get_nick().'\')');
  106. $this->disconnect();
  107. break;
  108. case'trash':
  109. $this->connect();
  110. $rs = $this->db->Execute( 'SELECT MAIL FROM poc_mails WHERE (SENDER=\''.$_SESSION['chatter']->get_nick().'\' OR RECIPIENT=\''.$_SESSION['chatter']->get_nick().'\') AND (TRASHED_BY_SENDER = 1 OR TRASHED_BY_RECIPIENT = 1)');
  111. $this->disconnect();
  112. break;
  113. default:
  114. die('Please specify the type of mailbox! Type can be "inbox", "outbox" or "trash"');
  115. }
  116. while( !$rs->EOF ) {
  117. $this->mailbox[] = unserialize( $rs->fields[0] );
  118. $rs->MoveNext();
  119. }
  120. $rs->Close();
  121. $this->_load_mails();
  122. }
  123.  
  124. /**
  125. * Connect to the database
  126. *
  127. * Establish a database connection
  128. *
  129. * @access public
  130. */
  131. function connect()
  132. {
  133. if( ++$this->connection_count > 1 )
  134. return null;
  135.  
  136. //create a database object
  137. $this->db = &NewADOConnection( DATABASE_DRIVER );
  138. if( USE_PCONNECT )
  139. $this->db->PConnect( DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_TABLESPACE );
  140. else
  141. $this->db->Connect( DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_TABLESPACE );
  142.  
  143. return true;
  144. }
  145.  
  146. /**
  147. * Disconnect the database
  148. *
  149. * @access public
  150. * @see connect()
  151. */
  152. function disconnect()
  153. {
  154. if( --$this->connection_count == 0 )
  155. {
  156. $this->db->Close();
  157. /*
  158. * NOTE: a db->Close() will be not enough! without setting $this->db to null,
  159. * PHP 4.2.1 can't serialize the object $line ( see Channel_Buffer_DB::put_line() )
  160. * who contains a chatter object.
  161. */
  162. $this->db = null;
  163. return true;
  164. }
  165. return null;
  166. }
  167.  
  168. /**
  169. * Loads all mail objects into an arrays
  170. *
  171. * @access private
  172. * @return boolean
  173. * @desc Loads all mail objects into an arrays
  174. */
  175. function _load_mails()
  176. {
  177. if( count($this->mailbox) == 0 )
  178. return false;
  179. //clear array of mails
  180. $this->mails = array();
  181. reset( $this->mailbox );
  182. do
  183. {
  184. $mail = current($this->mailbox);
  185. if( !is_object($mail) ) continue;
  186. if( !$mail->is_red_by_recipient() )
  187. $this->unread_mail_count++;
  188. $this->mails[] = $mail;
  189. }while( next($this->mailbox) );
  190. unset($mail);
  191.  
  192. return true;
  193. }
  194.  
  195. /**
  196. * Provides the fill level of mailbox
  197. *
  198. * @access public
  199. * @return boolean
  200. */
  201. function is_empty()
  202. {
  203. return count( $this->mails ) == 0;
  204. }
  205.  
  206. /**
  207. * Provides the count of mails in the current mailbox
  208. *
  209. * @access public
  210. * @return integer
  211. */
  212. function get_mail_count()
  213. {
  214. return count( $this->mails );
  215. }
  216.  
  217. /**
  218. * Provides the count of *new* mails in the current mailbox
  219. *
  220. * @access public
  221. * @return integer
  222. */
  223. function get_unread_mail_count()
  224. {
  225. return $this->unread_mail_count;
  226. }
  227.  
  228. /**
  229. * Provides the type of mailbox
  230. *
  231. * @access public
  232. * @return string
  233. */
  234. function get_type()
  235. {
  236. return $this->type;
  237. }
  238.  
  239. /**
  240. * Provides an mail-object of current mailbox
  241. *
  242. * @access public
  243. * @return array
  244. */
  245. function get_current_mail( $key )
  246. {
  247. if( isset($this->mails[ $key ]) )
  248. return $this->mails[ $key ];
  249. else
  250. die('get_current_mail(): No mail with this index found! Key: '.$key.'array count: '.count($this->mails));
  251. }
  252.  
  253. /**
  254. * Deletes an mail-object in current mailbox
  255. *
  256. * @access public
  257. * @return boolean
  258. */
  259. function delete_mail( $key )
  260. {
  261. if( isset($this->mails[ $key ]) )
  262. unset( $this->mails[ $key ] );
  263. else
  264. die('delete_mail: No mail with this index found!');
  265. }
  266.  
  267. /**
  268. * Provides the name of sender or recipient of given mail
  269. *
  270. * @access public
  271. * @return string
  272. * @param object
  273. */
  274. function get_chatter( $mail, $chatter )
  275. {
  276. if( $mail->get_recipient() == $chatter )
  277. {
  278. $sender = $mail->get_sender();
  279. return $sender->get_nick();
  280. }
  281. else
  282. return $mail->get_recipient();
  283. }
  284. }
  285. ?>

Documentation generated on Tue, 29 Jun 2004 14:42:10 +0200 by phpDocumentor 1.3.0RC3