Source for file functions.php

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

Documentation generated on Mon, 13 Jan 2020 04:22:34 +0100 by phpDocumentor 1.4.3