setup.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * setup.php -- Sent Subfolders Setup File
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This is a standard Squirrelmail-1.2 API for plugins.
  9. *
  10. * $Id$
  11. */
  12. define('SMPREF_SENT_SUBFOLDERS_DISABLED', 0);
  13. define('SMPREF_SENT_SUBFOLDERS_YEARLY', 1);
  14. define('SMPREF_SENT_SUBFOLDERS_QUARTERLY', 2);
  15. define('SMPREF_SENT_SUBFOLDERS_MONTHLY', 3);
  16. define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
  17. function squirrelmail_plugin_init_sent_subfolders() {
  18. /* Standard initialization API. */
  19. global $squirrelmail_plugin_hooks;
  20. /* The hooks to make the sent subfolders display correctly. */
  21. $squirrelmail_plugin_hooks
  22. ['check_handleAsSent_result']['sent_subfolders'] =
  23. 'sent_subfolders_check_handleAsSent';
  24. /* The hooks to automatically update sent subfolders. */
  25. $squirrelmail_plugin_hooks
  26. ['left_main_before']['sent_subfolders'] =
  27. 'sent_subfolders_update_sentfolder';
  28. $squirrelmail_plugin_hooks
  29. ['compose_send']['sent_subfolders'] =
  30. 'sent_subfolders_update_sentfolder';
  31. /* The hook to load the sent subfolders prefs. */
  32. $squirrelmail_plugin_hooks
  33. ['loading_prefs']['sent_subfolders'] =
  34. 'sent_subfolders_load_prefs';
  35. /* The hooks to handle sent subfolders options. */
  36. $squirrelmail_plugin_hooks
  37. ['optpage_loadhook_folder']['sent_subfolders'] =
  38. 'sent_subfolders_optpage_loadhook_folders';
  39. }
  40. function sent_subfolders_check_handleAsSent() {
  41. global $handleAsSent_result, $sent_subfolders_base,
  42. $use_sent_subfolders, $delimiter;
  43. $sent_subfolders_base = 'INBOX.Sent';
  44. $args = func_get_arg(0);
  45. /* Only check the folder string if we have been passed a mailbox. */
  46. if ($use_sent_subfolders && (count($args) > 1)) {
  47. /* Chop up the folder strings as needed. */
  48. $base_str = $sent_subfolders_base . $delimiter;
  49. $mbox_str = substr($args[1], 0, strlen($base_str));
  50. /* Perform the comparison. */
  51. $handleAsSent_result =
  52. ( $handleAsSent_result
  53. || ($base_str == $mbox_str)
  54. || ($sent_subfolders_base == $args[1])
  55. );
  56. }
  57. }
  58. function sent_subfolders_load_prefs() {
  59. global $use_sent_subfolders, $data_dir, $username,
  60. $sent_subfolders_setting, $sent_subfolders_base;
  61. $use_sent_subfolders = getPref
  62. ($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
  63. $sent_subfolders_setting = getPref
  64. ($data_dir, $username, 'sent_subfolders_setting', SMPREF_SENT_SUBFOLDERS_DISABLED);
  65. $sent_subfolders_base = getPref
  66. ($data_dir, $username, 'sent_subfolders_base', SMPREF_NONE);
  67. }
  68. function sent_subfolders_optpage_loadhook_folders() {
  69. global $optpage_data, $username, $key, $imapServerAddress, $imapPort;
  70. /* Get some imap data we need later. */
  71. $imapConnection =
  72. sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
  73. $boxes = sqimap_mailbox_list($imapConnection);
  74. sqimap_logout($imapConnection);
  75. /* Load the Sent Subfolder Options into an array. */
  76. $optgrp = _("Sent Subfolders Options");
  77. $optvals = array();
  78. $optvals[] = array(
  79. 'name' => 'sent_subfolders_setting',
  80. 'caption' => _("Use Sent Subfolders"),
  81. 'type' => SMOPT_TYPE_STRLIST,
  82. 'refresh' => SMOPT_REFRESH_FOLDERLIST,
  83. 'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED => _("Disabled"),
  84. SMPREF_SENT_SUBFOLDERS_MONTHLY => _("Monthly"),
  85. SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
  86. SMPREF_SENT_SUBFOLDERS_YEARLY => _("Yearly")),
  87. 'save' => 'save_option_sent_subfolders_setting'
  88. );
  89. $sent_subfolders_base_values = array();
  90. foreach ($boxes as $folder) {
  91. if (strtolower($folder['unformatted']) != 'inbox') {
  92. $real_value = $folder['unformatted-dm'];
  93. $disp_value = str_replace(' ', '&nbsp;', $folder['formatted']);
  94. $sent_subfolders_base_values[$real_value] = $disp_value;
  95. }
  96. }
  97. $optvals[] = array(
  98. 'name' => 'sent_subfolders_base',
  99. 'caption' => _("Base Sent Folder"),
  100. 'type' => SMOPT_TYPE_STRLIST,
  101. 'refresh' => SMOPT_REFRESH_FOLDERLIST,
  102. 'posvals' => $sent_subfolders_base_values
  103. );
  104. /* Add our option data to the global array. */
  105. $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS] = $optgrp;
  106. $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS] = $optvals;
  107. }
  108. function save_option_sent_subfolders_setting($option) {
  109. global $data_dir, $username, $use_sent_subfolders;
  110. /* Set use_sent_subfolders as either on or off. */
  111. if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED) {
  112. setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_OFF);
  113. } else {
  114. setPref($data_dir, $username, 'use_sent_subfolders', SMPREF_ON);
  115. setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
  116. }
  117. /* Now just save the option as normal. */
  118. save_option($option);
  119. }
  120. function sent_subfolders_update_sentfolder() {
  121. global $sent_folder, $delimiter, $auto_create_special, $auto_create_done;
  122. global $sent_subfolders_base, $sent_subfolders_setting;
  123. global $username, $data_dir, $key, $imapServerAddress, $imapPort;
  124. global $use_sent_subfolders, $move_to_sent, $imap_server_type;
  125. if ($use_sent_subfolders || $move_to_sent) {
  126. $year = date('Y');
  127. $month = date('m');
  128. $quarter = sent_subfolder_getQuarter($month);
  129. /*
  130. Regarding the structure we've got three main possibilities.
  131. One sent holder. level 0.
  132. Multiple year holders with messages in it. level 1.
  133. Multiple year folders with holders in it. level 2.
  134. */
  135. if( $imap_server_type == 'uw' ) {
  136. $cnd_delimiter = '';
  137. } else {
  138. $cnd_delimiter = $delimiter;
  139. }
  140. switch ($sent_subfolders_setting) {
  141. case SMPREF_SENT_SUBFOLDERS_YEARLY:
  142. $level = 1;
  143. $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
  144. . $year;
  145. break;
  146. case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
  147. $level = 2;
  148. $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
  149. . $year
  150. . $delimiter . $quarter;
  151. $year_folder = $sent_subfolders_base
  152. . $year;
  153. break;
  154. case SMPREF_SENT_SUBFOLDERS_MONTHLY:
  155. $level = 2;
  156. $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
  157. . $year
  158. . $delimiter . $month;
  159. $year_folder = $sent_subfolders_base . $year;
  160. break;
  161. case SMPREF_SENT_SUBFOLDERS_DISABLED:
  162. default:
  163. $level = 0;
  164. $sent_subfolder = $sent_folder;
  165. $year_folder = $sent_folder;
  166. }
  167. /* If this folder is NOT the current sent folder, update stuff. */
  168. if ($sent_subfolder != $sent_folder) {
  169. /* First, update the sent folder. */
  170. setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
  171. setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
  172. $sent_folder = $sent_subfolder;
  173. $move_to_sent = SMPREF_ON;
  174. /* Auto-create folders, if they do not yet exist. */
  175. if ($sent_folder != 'none') {
  176. /* Create the imap connection. */
  177. $ic = sqimap_login
  178. ($username, $key, $imapServerAddress, $imapPort, 10);
  179. /* Auto-create the year folder, if it does not yet exist. */
  180. if (!sqimap_mailbox_exists($ic, $year_folder)) {
  181. sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
  182. } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
  183. sqimap_subscribe($ic, $year_folder);
  184. }
  185. /* Auto-create the subfolder, if it does not yet exist. */
  186. if (!sqimap_mailbox_exists($ic, $sent_folder)) {
  187. sqimap_mailbox_create($ic, $sent_folder, '');
  188. } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
  189. sqimap_subscribe($ic, $sent_subfolder);
  190. }
  191. /* Close the imap connection. */
  192. sqimap_logout($ic);
  193. }
  194. }
  195. }
  196. }
  197. function sent_subfolder_getQuarter($month) {
  198. switch ($month) {
  199. case '01':
  200. case '02':
  201. case '03':
  202. $result = '1';
  203. break;
  204. case '04':
  205. case '05':
  206. case '06':
  207. $result = '2';
  208. break;
  209. case '07':
  210. case '08':
  211. case '09':
  212. $result = '3';
  213. break;
  214. case '10':
  215. case '11':
  216. case '12':
  217. $result = '4';
  218. break;
  219. default:
  220. $result = 'ERR';
  221. }
  222. /* Return the current quarter. */
  223. return ('Q' . $result);
  224. }
  225. ?>