setup.php 11 KB

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