setup.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * setup.php -- Sent Subfolders Setup File
  4. *
  5. * This is a standard SquirrelMail 1.2 API for plugins.
  6. *
  7. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package plugins
  11. * @subpackage sent_subfolders
  12. */
  13. /**
  14. */
  15. define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
  16. define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
  17. define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
  18. define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
  19. define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
  20. /**
  21. * Adds plugin to SquirrelMail's hooks
  22. */
  23. function squirrelmail_plugin_init_sent_subfolders() {
  24. /* Standard initialization API. */
  25. global $squirrelmail_plugin_hooks;
  26. /* The hooks to make the sent subfolders display correctly. */
  27. $squirrelmail_plugin_hooks
  28. ['check_handleAsSent_result']['sent_subfolders'] =
  29. 'sent_subfolders_check_handleAsSent';
  30. /* The hooks to automatically update sent subfolders. */
  31. $squirrelmail_plugin_hooks
  32. ['left_main_before']['sent_subfolders'] =
  33. 'sent_subfolders_update_sentfolder';
  34. $squirrelmail_plugin_hooks
  35. ['compose_send']['sent_subfolders'] =
  36. 'sent_subfolders_update_sentfolder';
  37. /* The hook to load the sent subfolders prefs. */
  38. $squirrelmail_plugin_hooks
  39. ['loading_prefs']['sent_subfolders'] =
  40. 'sent_subfolders_load_prefs';
  41. /* The hooks to handle sent subfolders options. */
  42. $squirrelmail_plugin_hooks
  43. ['optpage_loadhook_folder']['sent_subfolders'] =
  44. 'sent_subfolders_optpage_loadhook_folders';
  45. /* mark base sent folder as special mailbox */
  46. $squirrelmail_plugin_hooks
  47. ['special_mailbox']['sent_subfolders'] =
  48. 'sent_subfolders_special_mailbox';
  49. }
  50. function sent_subfolders_check_handleAsSent() {
  51. global $handleAsSent_result, $sent_subfolders_base,
  52. $use_sent_subfolders;
  53. // FIXME: hardcoded folder
  54. $sent_subfolders_base = 'INBOX.Sent';
  55. $args = func_get_arg(0);
  56. sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
  57. /* Only check the folder string if we have been passed a mailbox. */
  58. if ($use_sent_subfolders && (count($args) > 1)) {
  59. /* Chop up the folder strings as needed. */
  60. $base_str = $sent_subfolders_base . $delimiter;
  61. $mbox_str = substr($args[1], 0, strlen($base_str));
  62. /* Perform the comparison. */
  63. $handleAsSent_result =
  64. ( $handleAsSent_result
  65. || ($base_str == $mbox_str)
  66. || ($sent_subfolders_base == $args[1])
  67. );
  68. }
  69. }
  70. /**
  71. * Loads sent_subfolders settings
  72. */
  73. function sent_subfolders_load_prefs() {
  74. global $use_sent_subfolders, $data_dir, $username,
  75. $sent_subfolders_setting, $sent_subfolders_base;
  76. $use_sent_subfolders = getPref
  77. ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
  78. $sent_subfolders_setting = getPref
  79. ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
  80. $sent_subfolders_base = getPref
  81. ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
  82. }
  83. /**
  84. * Adds sent_subfolders options in folder preferences
  85. */
  86. function sent_subfolders_optpage_loadhook_folders() {
  87. global $username, $optpage_data, $imapServerAddress, $imapPort, $show_contain_subfolders_option;
  88. /* Get some imap data we need later. */
  89. $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
  90. $boxes = sqimap_mailbox_list($imapConnection);
  91. sqimap_logout($imapConnection);
  92. /* Load the Sent Subfolder Options into an array. */
  93. $optgrp = _("Sent Subfolders Options");
  94. $optvals = array();
  95. $optvals[] = array(
  96. 'name' => 'sent_subfolders_setting',
  97. 'caption' => _("Use Sent Subfolders"),
  98. 'type' => SMOPT_TYPE_STRLIST,
  99. 'refresh' => SMOPT_REFRESH_FOLDERLIST,
  100. 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
  101. SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
  102. SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
  103. SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
  104. 'save' => 'save_option_sent_subfolders_setting'
  105. );
  106. $filtered_folders=array_filter($boxes, "filter_folders");
  107. $sent_subfolders_base_values = array('whatever'=>$filtered_folders);
  108. $optvals[] = array(
  109. 'name' => 'sent_subfolders_base',
  110. 'caption' => _("Base Sent Folder"),
  111. 'type' => SMOPT_TYPE_FLDRLIST,
  112. 'refresh' => SMOPT_REFRESH_FOLDERLIST,
  113. 'posvals' => $sent_subfolders_base_values,
  114. 'folder_filter' => 'noinferiors'
  115. );
  116. if ($show_contain_subfolders_option) {
  117. $optvals[] = array(
  118. 'name' => 'sent_subfolders_warning',
  119. 'caption' => _("Warning"),
  120. 'type' => SMOPT_TYPE_COMMENT,
  121. 'comment' => _("There are some restrictions in Sent Subfolder options.")
  122. );
  123. }
  124. /* Add our option data to the global array. */
  125. $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
  126. $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
  127. }
  128. /**
  129. * Defines folder filtering rules
  130. *
  131. * Callback function that should exclude some folders from folder listing.
  132. * @param array $fldr list of folders. See sqimap_mailbox_list
  133. * @return boolean returns true, if folder has to included in folder listing
  134. * @access private
  135. */
  136. function filter_folders($fldr) {
  137. return strtolower($fldr['unformatted'])!='inbox';
  138. }
  139. /**
  140. * Saves sent_subfolder_options
  141. */
  142. function save_option_sent_subfolders_setting($option) {
  143. global $data_dir, $username, $use_sent_subfolders;
  144. /* Set use_sent_subfolders as either on or off. */
  145. if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
  146. setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
  147. } else {
  148. setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
  149. setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
  150. }
  151. /* Now just save the option as normal. */
  152. save_option($option);
  153. }
  154. /**
  155. * Update sent_subfolders settings
  156. *
  157. * function updates default sent folder value and
  158. * creates required imap folders
  159. */
  160. function sent_subfolders_update_sentfolder() {
  161. global $sent_folder, $username;
  162. global $sent_subfolders_base, $sent_subfolders_setting;
  163. global $data_dir, $imapServerAddress, $imapPort;
  164. global $use_sent_subfolders, $move_to_sent;
  165. sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
  166. if ($use_sent_subfolders || $move_to_sent) {
  167. $year = date('Y');
  168. $month = date('m');
  169. $quarter = sent_subfolder_getQuarter($month);
  170. /**
  171. * Regarding the structure we've got three main possibilities.
  172. * One sent holder. level 0.
  173. * Multiple year holders with messages in it. level 1.
  174. * Multiple year folders with holders in it. level 2.
  175. */
  176. switch ($sent_subfolders_setting) {
  177. case SMPREF_SENT_SUBFOLDERS_YEARLY:
  178. $level = 1;
  179. $sent_subfolder = $sent_subfolders_base . $delimiter
  180. . $year;
  181. break;
  182. case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
  183. $level = 2;
  184. $sent_subfolder = $sent_subfolders_base . $delimiter
  185. . $year
  186. . $delimiter . $quarter;
  187. $year_folder = $sent_subfolders_base . $delimiter
  188. . $year;
  189. break;
  190. case SMPREF_SENT_SUBFOLDERS_MONTHLY:
  191. $level = 2;
  192. $sent_subfolder = $sent_subfolders_base . $delimiter
  193. . $year
  194. . $delimiter . $month;
  195. $year_folder = $sent_subfolders_base. $delimiter . $year;
  196. break;
  197. case SMPREF_SENT_SUBFOLDERS_DISABLED:
  198. default:
  199. $level = 0;
  200. $sent_subfolder = $sent_folder;
  201. $year_folder = $sent_folder;
  202. }
  203. /* If this folder is NOT the current sent folder, update stuff. */
  204. if ($sent_subfolder != $sent_folder) {
  205. /* Auto-create folders, if they do not yet exist. */
  206. if ($sent_subfolder != 'none') {
  207. /* Create the imap connection. */
  208. $ic = sqimap_login($username, false, $imapServerAddress, $imapPort, 10);
  209. $boxes = false;
  210. /**
  211. * If sent_subfolder can't store messages (noselect) ||
  212. * year_folder can't store subfolders (noinferiors) in level=2 setup ||
  213. * subfolder_base can't store subfolders (noinferiors), setup is broken
  214. */
  215. if (sqimap_mailbox_is_noselect($ic,$sent_subfolder,$boxes) ||
  216. ($level==2 && sqimap_mailbox_is_noinferiors($ic,$year_folder,$boxes)) ||
  217. sqimap_mailbox_is_noinferiors($ic,$sent_subfolders_base,$boxes)) {
  218. error_box(_("Sent Subfolders plugin is misconfigured."));
  219. } else {
  220. if ($level==2) {
  221. /* Auto-create the year folder, if it does not yet exist. */
  222. if (!sqimap_mailbox_exists($ic, $year_folder)) {
  223. sqimap_mailbox_create($ic, $year_folder, 'noselect');
  224. // TODO: safety check for imap servers that can't create subfolders
  225. } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
  226. sqimap_subscribe($ic, $year_folder);
  227. }
  228. }
  229. /* Auto-create the subfolder, if it does not yet exist. */
  230. if (!sqimap_mailbox_exists($ic, $sent_subfolder)) {
  231. sqimap_mailbox_create($ic, $sent_subfolder, '');
  232. } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
  233. sqimap_subscribe($ic, $sent_subfolder);
  234. }
  235. /* Update sent_folder setting. */
  236. //setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
  237. //setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
  238. $sent_folder = $sent_subfolder;
  239. $move_to_sent = SMPREF_ON;
  240. }
  241. /* Close the imap connection. */
  242. sqimap_logout($ic);
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * Sets quarter subfolder names
  249. *
  250. * @param string $month numeric month
  251. * @return string quarter name (Q + number)
  252. */
  253. function sent_subfolder_getQuarter($month) {
  254. switch ($month) {
  255. case '01':
  256. case '02':
  257. case '03':
  258. $result = '1';
  259. break;
  260. case '04':
  261. case '05':
  262. case '06':
  263. $result = '2';
  264. break;
  265. case '07':
  266. case '08':
  267. case '09':
  268. $result = '3';
  269. break;
  270. case '10':
  271. case '11':
  272. case '12':
  273. $result = '4';
  274. break;
  275. default:
  276. $result = 'ERR';
  277. }
  278. /* Return the current quarter. */
  279. return ('Q' . $result);
  280. }
  281. /**
  282. * detects if mailbox is part of sent_subfolders
  283. *
  284. * @param string $mb imap folder name
  285. * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
  286. */
  287. function sent_subfolders_special_mailbox($mb) {
  288. global $data_dir, $username;
  289. sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
  290. $use_sent_subfolders = getPref
  291. ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
  292. $sent_subfolders_base = getPref($data_dir, $username, 'sent_subfolders_base', 'na');
  293. /**
  294. * If sent_subfolders are used and mailbox is equal to subfolder base
  295. * or mailbox matches subfolder base + delimiter.
  296. */
  297. if ($use_sent_subfolders == SMPREF_ON &&
  298. ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base . $delimiter) ) ) {
  299. return 1;
  300. }
  301. return 0;
  302. }