/tine20/Felamimail/Model/Folder.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM · PHP · 224 lines · 97 code · 21 blank · 106 comment · 8 complexity · 6f1631af739703816f61108a8a5f0554 MD5 · raw file

  1. <?php
  2. /**
  3. * class to hold Folder data
  4. *
  5. * @package Felamimail
  6. * @subpackage Model
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @author Philipp Schüle <p.schuele@metaways.de>
  9. * @copyright Copyright (c) 2009-2011 Metaways Infosystems GmbH (http://www.metaways.de)
  10. *
  11. * @todo rename unreadcount -> unseen
  12. */
  13. /**
  14. * class to hold Folder data
  15. *
  16. * @package Felamimail
  17. * @subpackage Model
  18. *
  19. * @property string account_id
  20. * @property string localname
  21. * @property string globalname
  22. */
  23. class Felamimail_Model_Folder extends Tinebase_Record_Abstract
  24. {
  25. /**
  26. * imap status: ok
  27. *
  28. */
  29. const IMAP_STATUS_OK = 'ok';
  30. /**
  31. * imap status: disconnected
  32. *
  33. */
  34. const IMAP_STATUS_DISCONNECT = 'disconnect';
  35. /**
  36. * cache status: empty
  37. *
  38. */
  39. const CACHE_STATUS_EMPTY = 'empty';
  40. /**
  41. * cache status: complete
  42. *
  43. */
  44. const CACHE_STATUS_COMPLETE = 'complete';
  45. /**
  46. * cache status: updating
  47. *
  48. */
  49. const CACHE_STATUS_UPDATING = 'updating';
  50. /**
  51. * cache status: incomplete
  52. *
  53. */
  54. const CACHE_STATUS_INCOMPLETE = 'incomplete';
  55. /**
  56. * cache status: invalid
  57. *
  58. */
  59. const CACHE_STATUS_INVALID = 'invalid';
  60. /**
  61. * meta folder trash constant
  62. */
  63. const FOLDER_TRASH = '_trash_';
  64. /**
  65. * meta folder sent constant
  66. */
  67. const FOLDER_SENT = '_sent_';
  68. /**
  69. * meta folder drafts constant
  70. */
  71. const FOLDER_DRAFTS = '_drafts_';
  72. /**
  73. * meta folder templates constant
  74. */
  75. const FOLDER_TEMPLATES = '_templates_';
  76. /**
  77. * key in $_validators/$_properties array for the field which
  78. * represents the identifier
  79. *
  80. * @var string
  81. */
  82. protected $_identifier = 'id';
  83. /**
  84. * application the record belongs to
  85. *
  86. * @var string
  87. */
  88. protected $_application = 'Felamimail';
  89. /**
  90. * list of zend validator
  91. *
  92. * this validators get used when validating user generated content with Zend_Input_Filter
  93. *
  94. * @var array
  95. */
  96. protected $_validators = array(
  97. 'id' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
  98. 'localname' => array(Zend_Filter_Input::ALLOW_EMPTY => false),
  99. 'globalname' => array(Zend_Filter_Input::ALLOW_EMPTY => false), // global name is the path from root folder
  100. 'parent' => array(Zend_Filter_Input::ALLOW_EMPTY => true), // global name of parent folder
  101. 'account_id' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 'default'),
  102. 'delimiter' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
  103. 'is_selectable' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 1),
  104. 'has_children' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  105. 'recent' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  106. 'system_folder' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  107. // imap values
  108. 'imap_status' => array(
  109. Zend_Filter_Input::ALLOW_EMPTY => true,
  110. Zend_Filter_Input::DEFAULT_VALUE => self::CACHE_STATUS_EMPTY,
  111. 'InArray' => array(
  112. self::IMAP_STATUS_OK,
  113. self::IMAP_STATUS_DISCONNECT,
  114. )
  115. ),
  116. 'imap_uidvalidity' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  117. 'imap_totalcount' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  118. 'imap_timestamp' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
  119. // cache values
  120. 'cache_status' => array(
  121. Zend_Filter_Input::ALLOW_EMPTY => true,
  122. Zend_Filter_Input::DEFAULT_VALUE => self::CACHE_STATUS_EMPTY,
  123. 'InArray' => array(
  124. self::CACHE_STATUS_EMPTY,
  125. self::CACHE_STATUS_COMPLETE,
  126. self::CACHE_STATUS_INCOMPLETE,
  127. self::CACHE_STATUS_UPDATING
  128. )
  129. ),
  130. 'cache_uidvalidity' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  131. 'cache_totalcount' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  132. 'cache_recentcount' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  133. 'cache_unreadcount' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  134. 'cache_timestamp' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
  135. 'cache_job_lowestuid' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  136. 'cache_job_startuid' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  137. 'cache_job_actions_estimate' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  138. 'cache_job_actions_done' => array(Zend_Filter_Input::ALLOW_EMPTY => true, Zend_Filter_Input::DEFAULT_VALUE => 0),
  139. 'quota_usage' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
  140. 'quota_limit' => array(Zend_Filter_Input::ALLOW_EMPTY => true),
  141. );
  142. /**
  143. * name of fields containing datetime or or an array of datetime information
  144. *
  145. * @var array list of datetime fields
  146. */
  147. protected $_datetimeFields = array(
  148. 'cache_timestamp',
  149. 'imap_timestamp',
  150. );
  151. /**
  152. * encode foldername given by user (convert to UTF7-IMAP)
  153. *
  154. * @param string $_folderName
  155. * @return string
  156. */
  157. public static function encodeFolderName($_folderName)
  158. {
  159. if (extension_loaded('mbstring')) {
  160. $result = mb_convert_encoding($_folderName, "UTF7-IMAP", "utf-8");
  161. } else if (extension_loaded('imap')) {
  162. $result = imap_utf7_encode(iconv('utf-8', 'ISO-8859-1', $_folderName));
  163. } else {
  164. // fallback
  165. $result = replaceSpecialChars($_folderName);
  166. }
  167. return $result;
  168. }
  169. /**
  170. * decode foldername given by IMAP server (convert from UTF7-IMAP to UTF8)
  171. *
  172. * @param string $_folderName
  173. * @return string
  174. */
  175. public static function decodeFolderName($_folderName)
  176. {
  177. if (extension_loaded('mbstring')) {
  178. $result = mb_convert_encoding($_folderName, "utf-8", "UTF7-IMAP");
  179. } else if (extension_loaded('imap')) {
  180. $result = iconv('ISO-8859-1', 'utf-8', imap_utf7_decode($_folderName));
  181. } else {
  182. // fallback
  183. $result = replaceSpecialChars($_folderName);
  184. }
  185. return $result;
  186. }
  187. /**
  188. * extract localname and parent globalname
  189. *
  190. * @param string $_folderName
  191. * @return array
  192. */
  193. public static function extractLocalnameAndParent($_folderName, $_delimiter)
  194. {
  195. $globalNameParts = explode($_delimiter, $_folderName);
  196. $localname = array_pop($globalNameParts);
  197. $parent = (count($globalNameParts) > 0) ? implode($_delimiter, $globalNameParts) : '';
  198. return array(
  199. 'localname' => $localname,
  200. 'parent' => $parent,
  201. );
  202. }
  203. }