functions.php 12 KB

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