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-2006 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,v 1.14.2.6 2006/02/03 22:27:52 jervfors Exp $
  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.     $sent_subfolders_base 'INBOX.Sent';
  65.     $args func_get_arg(0);
  66.     sqgetGlobalVar('delimiter'$delimiterSQ_SESSION);
  67.  
  68.     /* Only check the folder string if we have been passed a mailbox. */
  69.     if ($use_sent_subfolders && (count($args1)) {
  70.         /* Chop up the folder strings as needed. */
  71.         $base_str $sent_subfolders_base $delimiter;
  72.         $mbox_str substr($args[1]0strlen($base_str));
  73.  
  74.         /* Perform the comparison. */
  75.         $handleAsSent_result =
  76.             $handleAsSent_result
  77.             || ($base_str == $mbox_str)
  78.             || ($sent_subfolders_base == $args[1])
  79.             );
  80.     }
  81. }
  82.  
  83. /**
  84.  * Loads sent_subfolders settings
  85.  */
  86.     global $use_sent_subfolders$data_dir$username,
  87.            $sent_subfolders_setting$sent_subfolders_base;
  88.  
  89.     $use_sent_subfolders getPref
  90.     ($data_dir$username'use_sent_subfolders'SMPREF_OFF);
  91.  
  92.     $sent_subfolders_setting getPref
  93.     ($data_dir$username'sent_subfolders_setting'SMPREF_SENT_SUBFOLDERS_DISABLED);
  94.  
  95.     $sent_subfolders_base getPref
  96.     ($data_dir$username'sent_subfolders_base'SMPREF_NONE);
  97. }
  98.  
  99. /**
  100.  * Adds sent_subfolders options in folder preferences
  101.  */
  102.     global $optpage_data$imapServerAddress$imapPort;
  103.  
  104.     sqgetGlobalVar('username'$usernameSQ_SESSION);
  105.     sqgetGlobalVar('key'$keySQ_COOKIE);
  106.  
  107.     /* Get some imap data we need later. */
  108.     $imapConnection =
  109.         sqimap_login($username$key$imapServerAddress$imapPort0);
  110.     $boxes sqimap_mailbox_list($imapConnection);
  111.     sqimap_logout($imapConnection);
  112.  
  113.     /* Load the Sent Subfolder Options into an array. */
  114.     $optgrp _("Sent Subfolders Options");
  115.     $optvals array();
  116.  
  117.     $optvals[array(
  118.         'name'    => 'sent_subfolders_setting',
  119.         'caption' => _("Use Sent Subfolders"),
  120.         'type'    => SMOPT_TYPE_STRLIST,
  121.         'refresh' => SMOPT_REFRESH_FOLDERLIST,
  122.         'posvals' => array(SMPREF_SENT_SUBFOLDERS_DISABLED  => _("Disabled"),
  123.                         SMPREF_SENT_SUBFOLDERS_MONTHLY   => _("Monthly"),
  124.                         SMPREF_SENT_SUBFOLDERS_QUARTERLY => _("Quarterly"),
  125.                         SMPREF_SENT_SUBFOLDERS_YEARLY    => _("Yearly")),
  126.         'save'    => 'save_option_sent_subfolders_setting'
  127.     );
  128.  
  129.     $filtered_folders=array_filter($boxes"filter_folders");
  130.     $sent_subfolders_base_values array('whatever'=>$filtered_folders);
  131.  
  132.     $optvals[array(
  133.         'name'    => 'sent_subfolders_base',
  134.         'caption' => _("Base Sent Folder"),
  135.         'type'    => SMOPT_TYPE_FLDRLIST,
  136.         'refresh' => SMOPT_REFRESH_FOLDERLIST,
  137.         'posvals' => $sent_subfolders_base_values
  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;
  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.     else {
  166.         setPref($data_dir$username'use_sent_subfolders'SMPREF_ON);
  167.         setPref($data_dir$username'move_to_sent'SMPREF_ON);
  168.     }
  169.  
  170.     /* Now just save the option as normal. */
  171.     save_option($option);
  172. }
  173.  
  174. /**
  175.  * Update sent_subfolders settings
  176.  *
  177.  * function updates default sent folder value and
  178.  * creates required imap folders
  179.  */
  180.     global $sent_folder$auto_create_special$auto_create_done;
  181.     global $sent_subfolders_base$sent_subfolders_setting;
  182.     global $data_dir$imapServerAddress$imapPort;
  183.     global $use_sent_subfolders$move_to_sent$imap_server_type;
  184.  
  185.     sqgetGlobalVar('username'$usernameSQ_SESSION);
  186.     sqgetGlobalVar('key'$keySQ_COOKIE);
  187.     sqgetGlobalVar('delimiter'$delimiterSQ_SESSION);
  188.     
  189.     if ($use_sent_subfolders || $move_to_sent{
  190.         $year date('Y');
  191.         $month date('m');
  192.         $quarter sent_subfolder_getQuarter($month);
  193.  
  194.         /*
  195.             Regarding the structure we've got three main possibilities.
  196.             One sent holder. level 0.
  197.             Multiple year holders with messages in it. level 1.
  198.             Multiple year folders with holders in it. level 2.
  199.         */
  200. /*
  201.         if( $imap_server_type == 'uw' ) {
  202.             $cnd_delimiter = '';
  203.         } else {
  204.             $cnd_delimiter = $delimiter;
  205.         }
  206. */        
  207.         $cnd_delimiter $delimiter;
  208.                                         
  209.         switch ($sent_subfolders_setting{
  210.         case SMPREF_SENT_SUBFOLDERS_YEARLY:
  211.             $level 1;
  212.             $sent_subfolder $sent_subfolders_base $cnd_delimiter
  213.                             . $year;
  214.             break;
  215.         case SMPREF_SENT_SUBFOLDERS_QUARTERLY:
  216.             $level 2;
  217.             $sent_subfolder $sent_subfolders_base $cnd_delimiter 
  218.                             . $year
  219.                             . $delimiter $quarter;
  220.             $year_folder $sent_subfolders_base
  221.                             . $year;
  222.             break;
  223.         case SMPREF_SENT_SUBFOLDERS_MONTHLY:
  224.             $level 2;
  225.             $sent_subfolder $sent_subfolders_base $cnd_delimiter
  226.                             . $year
  227.                             . $delimiter $month;
  228.             $year_folder $sent_subfolders_base $year;
  229.             break;
  230.         case SMPREF_SENT_SUBFOLDERS_DISABLED:
  231.         default:
  232.             $level 0;
  233.             $sent_subfolder $sent_folder;
  234.             $year_folder $sent_folder;
  235.         }
  236.  
  237.         /* If this folder is NOT the current sent folder, update stuff. */
  238.         if ($sent_subfolder != $sent_folder{
  239.             /* First, update the sent folder. */
  240.  
  241.             setPref($data_dir$username'sent_folder'$sent_subfolder);
  242.             setPref($data_dir$username'move_to_sent'SMPREF_ON);
  243.             $sent_folder $sent_subfolder;
  244.             $move_to_sent SMPREF_ON;
  245.  
  246.             /* Auto-create folders, if they do not yet exist. */
  247.             if ($sent_folder != 'none'{
  248.                 /* Create the imap connection. */
  249.                 $ic sqimap_login
  250.                 ($username$key$imapServerAddress$imapPort10);
  251.  
  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($level==1)?'':'noselect');
  255.                 else if (!sqimap_mailbox_is_subscribed($ic$year_folder)) {
  256.                     sqimap_subscribe($ic$year_folder);
  257.                 }
  258.  
  259.                 /* Auto-create the subfolder, if it does not yet exist. */
  260.                 if (!sqimap_mailbox_exists($ic$sent_folder)) {
  261.                     sqimap_mailbox_create($ic$sent_folder'');
  262.                 else if (!sqimap_mailbox_is_subscribed($ic$sent_subfolder)) {
  263.                     sqimap_subscribe($ic$sent_subfolder);
  264.                 }
  265.  
  266.                 /* Close the imap connection. */
  267.                 sqimap_logout($ic);
  268.             }
  269.         }
  270.     }
  271. }
  272.  
  273. /**
  274.  * Sets quarter subfolder names
  275.  *
  276.  * @param string $month numeric month
  277.  * @return string quarter name (Q + number)
  278.  */
  279. function sent_subfolder_getQuarter($month{
  280.     switch ($month{
  281.         case '01':
  282.         case '02':
  283.         case '03':
  284.             $result '1';
  285.             break;
  286.         case '04':
  287.         case '05':
  288.         case '06':
  289.             $result '2';
  290.             break;
  291.         case '07':
  292.         case '08':
  293.         case '09':
  294.             $result '3';
  295.             break;
  296.         case '10':
  297.         case '11':
  298.         case '12':
  299.             $result '4';
  300.             break;
  301.         default:
  302.             $result 'ERR';
  303.     }
  304.  
  305.     /* Return the current quarter. */
  306.     return ('Q' $result);
  307. }
  308.  
  309. /**
  310.  * detects if mailbox is part of sent_subfolders
  311.  *
  312.  * @param string $mb imap folder name
  313.  * @return boolean 1 - is part of sent_subfolders, 0 - is not part of sent_subfolders
  314.  */
  315.     global $data_dir$username;
  316.  
  317.     $use_sent_subfolders getPref
  318.         ($data_dir$username'use_sent_subfolders'SMPREF_OFF);
  319.     $sent_subfolders_base getPref($data_dir$username'sent_subfolders_base''na');
  320.  
  321.     if ($use_sent_subfolders == SMPREF_ON && 
  322.     ($mb == $sent_subfolders_base || stristr($mb,$sent_subfolders_base) ) ) {
  323.     return 1;
  324.     }
  325.     return 0;
  326. }
  327. ?>

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