setup.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /*
  136. if( $imap_server_type == 'uw' ) {
  137. $cnd_delimiter = '';
  138. } else {
  139. $cnd_delimiter = $delimiter;
  140. }
  141. */
  142. $cnd_delimiter = $delimiter;
  143. switch ($sent_subfolders_setting) {
  144. case SMPREF_SENT_SUBFOLDERS_YEARLY:
  145. $level = 1;
  146. $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
  147. . $year;
  148. break;
  149. case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
  150. $level = 2;
  151. $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
  152. . $year
  153. . $delimiter . $quarter;
  154. $year_folder = $sent_subfolders_base
  155. . $year;
  156. break;
  157. case SMPREF_SENT_SUBFOLDERS_MONTHLY:
  158. $level = 2;
  159. $sent_subfolder = $sent_subfolders_base . $cnd_delimiter
  160. . $year
  161. . $delimiter . $month;
  162. $year_folder = $sent_subfolders_base . $year;
  163. break;
  164. case SMPREF_SENT_SUBFOLDERS_DISABLED:
  165. default:
  166. $level = 0;
  167. $sent_subfolder = $sent_folder;
  168. $year_folder = $sent_folder;
  169. }
  170. /* If this folder is NOT the current sent folder, update stuff. */
  171. if ($sent_subfolder != $sent_folder) {
  172. /* First, update the sent folder. */
  173. setPref($data_dir, $username, 'sent_folder', $sent_subfolder);
  174. setPref($data_dir, $username, 'move_to_sent', SMPREF_ON);
  175. $sent_folder = $sent_subfolder;
  176. $move_to_sent = SMPREF_ON;
  177. /* Auto-create folders, if they do not yet exist. */
  178. if ($sent_folder != 'none') {
  179. /* Create the imap connection. */
  180. $ic = sqimap_login
  181. ($username, $key, $imapServerAddress, $imapPort, 10);
  182. /* Auto-create the year folder, if it does not yet exist. */
  183. if (!sqimap_mailbox_exists($ic, $year_folder)) {
  184. sqimap_mailbox_create($ic, $year_folder, ($level==1)?'':'noselect');
  185. } else if (!sqimap_mailbox_is_subscribed($ic, $year_folder)) {
  186. sqimap_subscribe($ic, $year_folder);
  187. }
  188. /* Auto-create the subfolder, if it does not yet exist. */
  189. if (!sqimap_mailbox_exists($ic, $sent_folder)) {
  190. sqimap_mailbox_create($ic, $sent_folder, '');
  191. } else if (!sqimap_mailbox_is_subscribed($ic, $sent_subfolder)) {
  192. sqimap_subscribe($ic, $sent_subfolder);
  193. }
  194. /* Close the imap connection. */
  195. sqimap_logout($ic);
  196. }
  197. }
  198. }
  199. }
  200. function sent_subfolder_getQuarter($month) {
  201. switch ($month) {
  202. case '01':
  203. case '02':
  204. case '03':
  205. $result = '1';
  206. break;
  207. case '04':
  208. case '05':
  209. case '06':
  210. $result = '2';
  211. break;
  212. case '07':
  213. case '08':
  214. case '09':
  215. $result = '3';
  216. break;
  217. case '10':
  218. case '11':
  219. case '12':
  220. $result = '4';
  221. break;
  222. default:
  223. $result = 'ERR';
  224. }
  225. /* Return the current quarter. */
  226. return ('Q' . $result);
  227. }
  228. ?>