Source for file setup.php

Documentation is available at setup.php

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

Documentation generated on Sat, 07 Oct 2006 16:13:33 +0300 by phpDocumentor 1.3.0RC6