Source for file setup.php

Documentation is available at setup.php

  1. <?php
  2.  
  3. /**
  4.  * setup.php -- Sent Subfolders Setup File
  5.  *
  6.  * Copyright (c) 1999-2020 The SquirrelMail Project Team
  7.  * Licensed under the GNU GPL. For full terms see the file COPYING.
  8.  *
  9.  * This is a standard Squirrelmail-1.2 API for plugins.
  10.  *
  11.  * $Id: setup.php 14840 2020-01-07 07:42:38Z pdontthink $
  12.  * @package plugins
  13.  * @subpackage sent_subfolders
  14.  */
  15.  
  16. /** 
  17.  * 
  18.  */
  19. define('SMPREF_SENT_SUBFOLDERS_DISABLED',  0);
  20. define('SMPREF_SENT_SUBFOLDERS_YEARLY',    1);
  21. define('SMPREF_SENT_SUBFOLDERS_QUARTERLY'2);
  22. define('SMPREF_SENT_SUBFOLDERS_MONTHLY',   3);
  23. define('SMOPT_GRP_SENT_SUBFOLDERS','SENT_SUBFOLDERS');
  24.  
  25. /**
  26.  * Adds plugin to squirrelmail hooks
  27.  */
  28.     /* Standard initialization API. */
  29.     global $squirrelmail_plugin_hooks;
  30.  
  31.     /* The hooks to make the sent subfolders display correctly. */
  32.     $squirrelmail_plugin_hooks
  33.     ['check_handleAsSent_result']['sent_subfolders'=
  34.         'sent_subfolders_check_handleAsSent';
  35.  
  36.     /* The hooks to automatically update sent subfolders. */
  37.     $squirrelmail_plugin_hooks
  38.     ['left_main_before']['sent_subfolders'=
  39.         'sent_subfolders_update_sentfolder';
  40.  
  41.     $squirrelmail_plugin_hooks
  42.     ['compose_send']['sent_subfolders'=
  43.         'sent_subfolders_update_sentfolder';
  44.  
  45.     /* The hook to load the sent subfolders prefs. */
  46.     $squirrelmail_plugin_hooks
  47.     ['loading_prefs']['sent_subfolders'=
  48.         'sent_subfolders_load_prefs';
  49.  
  50.     /* The hooks to handle sent subfolders options. */
  51.     $squirrelmail_plugin_hooks
  52.     ['optpage_loadhook_folder']['sent_subfolders'=
  53.         'sent_subfolders_optpage_loadhook_folders';
  54.  
  55.     /* mark base sent folder as special mailbox */
  56.     $squirrelmail_plugin_hooks
  57.     ['special_mailbox']['sent_subfolders'
  58.     'sent_subfolders_special_mailbox';
  59. }
  60.  
  61.     global $handleAsSent_result$sent_subfolders_base,
  62.            $use_sent_subfolders;
  63.  
  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$sent_folder,
  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'$sent_folder);
  96. }
  97.  
  98. /**
  99.  * Adds sent_subfolders options in folder preferences
  100.  */
  101.     global $optpage_data$imapServerAddress$imapPort$imap_stream_options;
  102.  
  103.     sqgetGlobalVar('username'$usernameSQ_SESSION);
  104.     sqgetGlobalVar('key'$keySQ_COOKIE);
  105.  
  106.     /* Get some imap data we need later. */
  107.     $imapConnection =
  108.         sqimap_login($username$key$imapServerAddress$imapPort0$imap_stream_options);
  109.     $boxes sqimap_mailbox_list($imapConnection);
  110.     sqimap_logout($imapConnection);
  111.  
  112.     /* Load the Sent Subfolder Options into an array. */
  113.     $optgrp _("Sent Subfolders Options");
  114.     $optvals array();
  115.  
  116.     $optvals[array(
  117.         'name'    => 'sent_subfolders_setting',
  118.         'caption' => _("Use Sent Subfolders"),
  119.         'type'    => SMOPT_TYPE_STRLIST,
  120.         'refresh' => SMOPT_REFRESH_FOLDERLIST,
  121.         'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED  => _("Disabled"),
  122.                         SMPREF_SENT_SUBFOLDERS_MONTHLY   => _("Monthly"),
  123.                         SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
  124.                         SMPREF_SENT_SUBFOLDERS_YEARLY    => _("Yearly")),
  125.         'save'    => 'save_option_sent_subfolders_setting'
  126.     );
  127.  
  128.     $filtered_folders=array_filter($boxes"filter_folders");
  129.     $sent_subfolders_base_values array('whatever'=>$filtered_folders);
  130.  
  131.     $optvals[array(
  132.         'name'    => 'sent_subfolders_base',
  133.         'caption' => _("Base Sent Folder"),
  134.         'type'    => SMOPT_TYPE_FLDRLIST,
  135.         'refresh' => SMOPT_REFRESH_FOLDERLIST,
  136.         'posvals' => $sent_subfolders_base_values,
  137.         'save'    => 'save_option_sent_subfolders_base',
  138.     );
  139.  
  140.     /* Add our option data to the global array. */
  141.     $optpage_data['grps'][SMOPT_GRP_SENT_SUBFOLDERS$optgrp;
  142.     $optpage_data['vals'][SMOPT_GRP_SENT_SUBFOLDERS$optvals;
  143. }
  144.  
  145. /**
  146.  * Defines folder filtering rules
  147.  *
  148.  * Callback function that should exclude some folders from folder listing.
  149.  * @param array $fldr list of folders. See sqimap_mailbox_list
  150.  * @return boolean returns true, if folder has to included in folder listing
  151.  * @access private
  152.  */
  153. function filter_folders($fldr{
  154.     return strtolower($fldr['unformatted'])!='inbox';
  155. }
  156.  
  157. /**
  158.  * Saves sent_subfolder_options
  159.  */
  160.     global $data_dir$username$use_sent_subfolders$sent_subfolders_base;
  161.  
  162.     /* Set use_sent_subfolders as either on or off. */
  163.     if ($option->new_value == SMPREF_SENT_SUBFOLDERS_DISABLED{
  164.         setPref($data_dir$username'use_sent_subfolders'SMPREF_OFF);
  165.         // for lack of anything better
  166.         setPref($data_dir$username'sent_folder'$sent_subfolders_base);
  167.     else {
  168.         setPref($data_dir$username'use_sent_subfolders'SMPREF_ON);
  169.         setPref($data_dir$username'move_to_sent'SMPREF_ON);
  170.         $check_sent_subfolders_base getPref($data_dir$username'sent_subfolders_base''');
  171.         if ($check_sent_subfolders_base === ''{
  172.             setPref($data_dir$username'sent_subfolders_base'$sent_subfolders_base);
  173.         }
  174.     }
  175.  
  176.     /* Now just save the option as normal. */
  177.     save_option($option);
  178. }
  179.  
  180. /**
  181.  * Update sent_subfolders settings
  182.  *
  183.  * function updates default sent folder value and
  184.  * creates required imap folders
  185.  */
  186.     global $sent_folder$auto_create_special$auto_create_done;
  187.     global $sent_subfolders_base$sent_subfolders_setting;
  188.     global $data_dir$imapServerAddress$imapPort$imap_stream_options;
  189.     global $use_sent_subfolders$move_to_sent$imap_server_type;
  190.  
  191.     sqgetGlobalVar('username'$usernameSQ_SESSION);
  192.     sqgetGlobalVar('key'$keySQ_COOKIE);
  193.     sqgetGlobalVar('delimiter'$delimiterSQ_SESSION);
  194.     
  195.     if ($use_sent_subfolders || $move_to_sent{
  196.         $year date('Y');
  197.         $month date('m');
  198.         $quarter sent_subfolder_getQuarter($month);
  199.  
  200.         /*
  201.             Regarding the structure we've got three main possibilities.
  202.             One sent holder. level 0.
  203.             Multiple year holders with messages in it. level 1.
  204.             Multiple year folders with holders in it. level 2.
  205.         */
  206. /*
  207.         if( $imap_server_type == 'uw' ) {
  208.             $cnd_delimiter = '';
  209.         } else {
  210.             $cnd_delimiter = $delimiter;
  211.         }
  212. */        
  213.         $cnd_delimiter $delimiter;
  214.                                         
  215.         switch ($sent_subfolders_setting{
  216.         case SMPREF_SENT_SUBFOLDERS_YEARLY:
  217.             $level 1;
  218.             $sent_subfolder $sent_subfolders_base $cnd_delimiter
  219.                             . $year;
  220.             break;
  221.         case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
  222.             $level 2;
  223.             $sent_subfolder $sent_subfolders_base $cnd_delimiter 
  224.                             . $year
  225.                             . $delimiter $quarter;
  226.             $year_folder $sent_subfolders_base $cnd_delimiter
  227.                             . $year;
  228.             break;
  229.         case SMPREF_SENT_SUBFOLDERS_MONTHLY:
  230.             $level 2;
  231.             $sent_subfolder $sent_subfolders_base $cnd_delimiter
  232.                             . $year
  233.                             . $delimiter $month;
  234.             $year_folder $sent_subfolders_base $cnd_delimiter $year;
  235.             break;
  236.         case SMPREF_SENT_SUBFOLDERS_DISABLED:
  237.         default:
  238.             $level 0;
  239.             $sent_subfolder $sent_folder;
  240.             $year_folder $sent_folder;
  241.         }
  242.  
  243.         /* If this folder is NOT the current sent folder, update stuff. */
  244.         if ($sent_subfolder != $sent_folder{
  245.             /* First, update the sent folder. */
  246.  
  247.             setPref($data_dir$username'sent_folder'$sent_subfolder);
  248.             setPref($data_dir$username'move_to_sent'SMPREF_ON);
  249.             $sent_folder $sent_subfolder;
  250.             $move_to_sent SMPREF_ON;
  251.  
  252.             /* Auto-create folders, if they do not yet exist. */
  253.             if ($sent_folder != 'none'{
  254.                 /* Create the imap connection. */
  255.                 $ic sqimap_login
  256.                 ($username$key$imapServerAddress$imapPort10$imap_stream_options);
  257.  
  258.                 /* Auto-create the year folder, if it does not yet exist.
  259.                    (only for monthly/quarterly modes */
  260.                 if ($sent_subfolders_setting != SMPREF_SENT_SUBFOLDERS_YEARLY{
  261.                     if (!sqimap_mailbox_exists($ic$year_folder)) {
  262.                         sqimap_mailbox_create($ic$year_folder($level==1)?'':'noselect');
  263.                     else if (!sqimap_mailbox_is_subscribed($ic$year_folder)) {
  264.                         sqimap_subscribe($ic$year_folder);
  265.                     }
  266.                 }
  267.  
  268.                 /* Auto-create the subfolder, if it does not yet exist. */
  269.                 if (!sqimap_mailbox_exists($ic$sent_folder)) {
  270.                     sqimap_mailbox_create($ic$sent_folder'');
  271.                 else if (!sqimap_mailbox_is_subscribed($ic$sent_subfolder)) {
  272.                     sqimap_subscribe($ic$sent_subfolder);
  273.                 }
  274.  
  275.                 /* Close the imap connection. */
  276.                 sqimap_logout($ic);
  277.             }
  278.         }
  279.     }
  280. }
  281.  
  282. /**
  283.  * Update the folder settings/auto-create new subfolder
  284.  */
  285. function save_option_sent_subfolders_base($option{
  286.     // first save the option as normal
  287.     save_option($option);
  288.  
  289.     // now update folder settings and auto-create first subfolder if needed
  290. }
  291.  
  292. /**
  293.  * Sets quarter subfolder names
  294.  *
  295.  * @param string $month numeric month
  296.  * @return string quarter name (Q + number)
  297.  */
  298. function sent_subfolder_getQuarter($month{
  299.     switch ($month{
  300.         case '01':
  301.         case '02':
  302.         case '03':
  303.             $result '1';
  304.             break;
  305.         case '04':
  306.         case '05':
  307.         case '06':
  308.             $result '2';
  309.             break;
  310.         case '07':
  311.         case '08':
  312.         case '09':
  313.             $result '3';
  314.             break;
  315.         case '10':
  316.         case '11':
  317.         case '12':
  318.             $result '4';
  319.             break;
  320.         default:
  321.             $result 'ERR';
  322.     }
  323.  
  324.     /* Return the current quarter. */
  325.     return ('Q' $result);
  326. }
  327.  
  328. /**
  329.  * detects if mailbox is part of sent_subfolders
  330.  *
  331.  * @param string $mb imap folder name
  332.  * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
  333.  */
  334.     global $data_dir$username$sent_folder;
  335.  
  336.     sqgetGlobalVar('delimiter'$delimiterSQ_SESSION);
  337. /*
  338.     if( $imap_server_type == 'uw' ) {
  339.         $cnd_delimiter = '';
  340.     } else {
  341.         $cnd_delimiter = $delimiter;
  342.     }
  343. */        
  344.     $cnd_delimiter $delimiter;
  345.  
  346.     $use_sent_subfolders getPref
  347.         ($data_dir$username'use_sent_subfolders'SMPREF_OFF);
  348.     $sent_subfolders_base getPref($data_dir$username'sent_subfolders_base'$sent_folder);
  349.  
  350.     if ($use_sent_subfolders == SMPREF_ON && 
  351.     ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base$cnd_delimiter) ) ) {
  352.     return 1;
  353.     }
  354.     return 0;
  355. }

Documentation generated on Mon, 13 Jan 2020 04:25:19 +0100 by phpDocumentor 1.4.3