home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / email / folders.php < prev    next >
PHP Script  |  2004-03-08  |  14KB  |  426 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13. require("../../Group-Office.php");
  14. require($GO_CONFIG->class_path."imap.class.inc");
  15. require($GO_CONFIG->class_path."email.class.inc");
  16. require($GO_LANGUAGE->get_language_file('email'));
  17. $mail = new imap();
  18. $email = new email();
  19.  
  20. $GO_SECURITY->authenticate();
  21. $GO_MODULES->authenticate('email');
  22.  
  23. $task = isset($_REQUEST['task']) ? $_REQUEST['task'] : '';
  24. $return_to = (isset($_REQUEST['return_to']) && $_REQUEST['return_to'] != '') ? $_REQUEST['return_to'] : $_SERVER['HTTP_REFERER'];
  25. $link_back = (isset($_REQUEST['link_back']) && $_REQUEST['link_back'] != '') ? $_REQUEST['link_back'] : $_SERVER['REQUEST_URI'];
  26. $account_id = isset($_REQUEST['account_id']) ? $_REQUEST['account_id'] : 0;
  27.  
  28. $account = $email->get_account($account_id);
  29. if ($account && $mail->open($account['host'], $account['type'],$account['port'],$account['username'],$GO_CRYPTO->decrypt($account['password'])))
  30. {
  31.     if ($task == 'create_folder')
  32.     {
  33.         $name = trim($_POST['name']);
  34.  
  35.         if ($name == '')
  36.         {
  37.             $feedback = '<p class="Error">'.$error_missing_field.'</p>';
  38.         }else
  39.         {
  40.             //$delimiter = (strstr($parent_folder_name, $delimiter) == $delimiter) ?  $delimiter : '';
  41.             if ($_POST['parent_folder_name'] != '' && substr($_POST['parent_folder_name'], -1) != $_POST['delimiter'])
  42.             {
  43.                 $_POST['parent_folder_name'] .= $_POST['delimiter'];
  44.             }
  45.             $mail->create_folder($_POST['parent_folder_name'].$name);
  46.         }
  47.     }
  48.  
  49.     if (isset($_REQUEST['delete_folder']))
  50.     {
  51.         if ($mail->delete_folder($_REQUEST['delete_folder'], $account['mbroot']))
  52.         {
  53.             //(cyrus imap) if folder still exists then don't delete it from the database,
  54.             //because it contains at least one child mailbox
  55.             if (!is_array($mail->get_mailboxes($_REQUEST['delete_folder'])))
  56.             {
  57.                 $email->delete_folder($account['id'], $_REQUEST['delete_folder']);
  58.             }
  59.         }
  60.     }
  61.  
  62.     $edit_name = isset($_REQUEST['edit_name']) ? $_REQUEST['edit_name'] : '';
  63.  
  64.     if ($task =='save')
  65.     {
  66.         $subscribed = $mail->get_subscribed();
  67.         $subscribed_names = array();
  68.         if (isset($_POST['use']))
  69.         {
  70.             while($mailbox = array_shift($subscribed))
  71.             {
  72.                 $subscribed_names[] = $mailbox['name'];
  73.                 if (!in_array($mailbox['name'], $_POST['use']))
  74.                 {
  75.                     if($mail->unsubscribe($mailbox['name']))
  76.                     {
  77.                         $email->unsubscribe($account['id'], $mailbox['name']);
  78.                     }
  79.                 }
  80.             }
  81.  
  82.             for ($i=0;$i<count($_POST['use']);$i++)
  83.             {
  84.                 if (!in_array($_POST['use'][$i], $subscribed_names) && $_POST['use'][$i] != "INBOX")
  85.                 {
  86.                     if($mail->subscribe($_POST['use'][$i]))
  87.                     {
  88.                         $email->subscribe($account['id'], $_POST['use'][$i]);
  89.                     }
  90.                 }
  91.             }
  92.         }else
  93.         {
  94.             while($mailbox = array_shift($subscribed))
  95.             {
  96.                 if($mail->unsubscribe($mailbox['name']))
  97.                 {
  98.                     $email->unsubscribe($account['id'], $mailbox['name']);
  99.                 }
  100.             }
  101.         }
  102.  
  103.         $sent = isset($_POST['sent']) ? $_POST['sent'] : '';
  104.         $spam = isset($_POST['spam']) ? $_POST['spam'] : '';
  105.         $trash = isset($_POST['trash']) ? $_POST['trash'] : '';
  106.  
  107.         $email->update_folders($account['id'], $sent, $spam, $trash);
  108.  
  109.         if (isset($_POST['new_name']))
  110.         {
  111.             $new_name = trim($_POST['new_name']);
  112.             if ($new_name == '')
  113.             {
  114.                 $feedback = '<p class="Error">'.$error_missing_field.'</p>';
  115.             }else
  116.             {
  117.                 if ($mail->rename_folder($_POST['old_name'], $_POST['location'].$new_name))
  118.                 {
  119.                     $email->rename_folder($account_id, $_POST['old_name'], $_POST['location'].addslashes($new_name));
  120.                 }
  121.             }
  122.         }
  123.  
  124.     }
  125.  
  126.     if (isset($_POST['close']) && $_POST['close'] == 'true')
  127.     {
  128.         header('Location: '.$return_to);
  129.         exit();
  130.     }
  131.  
  132.     require($GO_THEME->theme_path."header.inc");
  133.  
  134.     echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" name="email_client">';
  135.     echo '<input type="hidden" name="task" value="" />';
  136.     echo '<input type="hidden" name="close" value="false" />';
  137.     echo '<input type="hidden" name="return_to" value="'.$return_to.'" />';
  138.     echo '<input type="hidden" name="link_back" value="'.$link_back.'" />';
  139.     echo '<input type="hidden" name="account_id" value="'.$account_id.'" />';
  140.  
  141.     $tabtable = new tabtable('folders_list', $ml_folders, '600', '300', '100', '', true);
  142.     $tabtable->print_head();
  143.  
  144.  
  145.     //synchronise Group-Office with the IMAP server
  146.     $subscribed = $mail->get_subscribed();
  147.     $mailboxes = $mail->get_mailboxes();
  148.     $email->synchronise($account['id'], $mailboxes, $subscribed);
  149.  
  150.     //get all the folders and the subscribed folders as an array
  151.     //and add all missing subscribed folders to Group-Office
  152.     $mailboxes = array();
  153.     $mailboxes = $mail->get_mailboxes($account['mbroot']);
  154.  
  155.     //get all the Group-Office folders as an array
  156.  
  157.     $account = $email->get_account($account['id']);
  158.  
  159.     $email->get_all_folders($account['id']);
  160.     $go_mailboxes = array();
  161.     //folders that can have children
  162.     $parent_folders = array();
  163.     while ($email->next_record())
  164.     {
  165.         $go_mailboxes[] = $email->Record;
  166.         $delimiter = $email->f('delimiter');
  167.         //if it can have children add it to the parent_folders array
  168.         if (!($email->f('attributes')&LATT_NOINFERIORS))
  169.         {
  170.             $parent_folders[] = $email->f('name');
  171.         }
  172.     }
  173.     $mcount = count($go_mailboxes);
  174.  
  175.     ?>
  176.     <table border="0" cellpadding="0" cellspacing="8" class="normal">
  177.     <tr>
  178.         <td>
  179.         <?php if (isset($feedback)) echo $feedback; ?>
  180.         <table border="0" cellpadding="2" cellspacing="0">
  181.         <tr>
  182.             <td colspan="2">
  183.             <table border="0">
  184.             <tr>
  185.                 <td nowrap><?php echo $ml_sent_items; ?>:</td>
  186.                 <td>
  187.                 <?php
  188.                 $dropbox=new dropbox();
  189.                 $dropbox->add_value('', $ml_disable);
  190.                 for ($i=0;$i<$mcount;$i++)
  191.                 {
  192.                     if ($go_mailboxes[$i]['attributes'] != LATT_NOSELECT)
  193.                     {
  194.                         $dropbox->add_value($go_mailboxes[$i]['name'], $go_mailboxes[$i]['name']);
  195.                     }
  196.                 }
  197.                 $dropbox->print_dropbox('sent', $account['sent']);
  198.                 ?>
  199.                 </td>
  200.             </tr>
  201.             <tr>
  202.                 <td nowrap><?php echo $ml_spam; ?>:</td>
  203.                 <td>
  204.                 <?php
  205.                 $dropbox=new dropbox();
  206.                 $dropbox->add_value('', $ml_disable);
  207.                 for ($i=0;$i<$mcount;$i++)
  208.                 {
  209.                     if ($go_mailboxes[$i]['attributes'] != LATT_NOSELECT)
  210.                     {
  211.                         $dropbox->add_value($go_mailboxes[$i]['name'], $go_mailboxes[$i]['name']);
  212.                     }
  213.                 }
  214.                 $dropbox->print_dropbox('spam', $account['spam']);
  215.                 ?>
  216.                 </td>
  217.             </tr>
  218.             <tr>
  219.                 <td nowrap><?php echo $ml_trash; ?>:</td>
  220.                 <td>
  221.                 <?php
  222.                 $dropbox=new dropbox();
  223.                 $dropbox->add_value('', $ml_disable);
  224.                 for ($i=0;$i<$mcount;$i++)
  225.                 {
  226.                     if ($go_mailboxes[$i]['attributes'] != LATT_NOSELECT)
  227.                     {
  228.                         $dropbox->add_value($go_mailboxes[$i]['name'], $go_mailboxes[$i]['name']);
  229.                     }
  230.                 }
  231.                 $dropbox->print_dropbox('trash', $account['trash']);
  232.                 ?>
  233.                 </td>
  234.             </tr>
  235.             </table>
  236.             </td>
  237.         </tr>
  238.         <tr>
  239.             <td> </td>
  240.         </tr>
  241.  
  242.         <tr>
  243.             <td>
  244.             <?php echo $em_new_folder; ?>:
  245.             </td>
  246.             <td>
  247.             <?php $name = isset($_POST['name']) ? $_POST['name'] : ''; ?>
  248.             <input type="text" class="textbox" name="name" value="<?php echo $name; ?>" maxlength="100" size="30" />
  249.             </td>
  250.             <?php
  251.             $delimiter = isset($delimiter) ? $delimiter : '/';
  252.             echo '<input type="hidden" name="delimiter" value="'.$delimiter.'" />';
  253.             echo '<td>'.$ml_inside.'</td>';
  254.             echo '<td>';
  255.             $parent_folder_name = isset($parent_folder_name) ? $parent_folder_name : '';
  256.             $dropbox=new dropbox();
  257.             $dropbox->add_value($account['mbroot'],$ml_root_mailbox);
  258.             $dropbox->add_arrays($parent_folders,$parent_folders);
  259.             $dropbox->print_dropbox('parent_folder_name',$parent_folder_name);
  260.             echo '</td>';
  261.             ?>
  262.             <td>
  263.             <?php
  264.             $button = new button($cmdOk, "javascript:_save('create_folder', 'false')");
  265.             ?>
  266.             </td>
  267.         </tr>
  268.         </table>
  269.         <br />
  270.         <table border="0" cellpadding="0" cellspacing="0">
  271.         <tr>
  272.             <td><h3><?php echo $strName; ?>  </h3></td>
  273.             <td><h3><?php echo $ml_use; ?>  </h3></td>
  274.             <td>       </td>
  275.             <td> </td>
  276.         </tr>
  277.         <tr>
  278.             <td><img border="0" src="<?php echo $GO_THEME->images['newmail']; ?>" /></td>
  279.             <td colspan="5"> </td>
  280.         </tr>
  281.         <?php
  282.  
  283.         $fm_image['folder'] = '<img src="'.$GO_THEME->images['folderclosed'].'" border="0" height="22" width="24" />';
  284.         $fm_image['emptynode'] = '<img src="'.$GO_THEME->images['emptynode'].'" border="0" height="22" width="16" />';
  285.         $fm_image['emptylastnode'] = '<img src="'.$GO_THEME->images['emptylastnode'].'" border="0" height="22" width="16" />';
  286.         $fm_image['blank'] = '<img src="'.$GO_THEME->images['blank'].'" border="0" height="22" width="16" />';
  287.         $fm_image['vertline'] = '<img src="'.$GO_THEME->images['vertline'].'" border="0" height="22" width="16" />';
  288.  
  289.  
  290.         $sublevel = 0; // number of levels deep
  291.         $lastfolder = '';
  292.         $prefix = array();
  293.         for ($a=0;$a<$mcount;$a++)
  294.         {
  295.             $sublevel = substr_count($go_mailboxes[$a]['name'], $go_mailboxes[$a]['delimiter']) + 1;
  296.  
  297.             if ($sublevel > 1)
  298.             {
  299.                 $folder_name = substr($go_mailboxes[$a]['name'],strrpos($go_mailboxes[$a]['name'], $go_mailboxes[$a]['delimiter'])+1);
  300.             }else
  301.             {
  302.                 $folder_name = $go_mailboxes[$a]['name'];
  303.             }
  304.  
  305.             //check if this is the last node on this level
  306.             $node = $fm_image['emptylastnode'];
  307.             $prefix[$sublevel] = $fm_image['blank'];
  308.             if (isset($go_mailboxes[$a+1]['name']) && strtolower($go_mailboxes[$a+1]['name']) != 'inbox')
  309.             {
  310.                 for ($i=$a+1;$i<$mcount;$i++)
  311.                 {
  312.                     $next_sublevel = substr_count($go_mailboxes[$i]['name'], $go_mailboxes[$a]['delimiter']) + 1;
  313.  
  314.                     if ($next_sublevel < $sublevel)
  315.                     {
  316.                         $prefix[$sublevel] = $fm_image['blank'];
  317.                         $node = $fm_image['emptylastnode'];
  318.                         break;
  319.                     }
  320.  
  321.                     if ($next_sublevel == $sublevel)
  322.                     {
  323.                         $prefix[$sublevel] = $fm_image['vertline'];
  324.                         $node = $fm_image['emptynode'];
  325.                         break;
  326.                     }
  327.  
  328.                 }
  329.             }
  330.  
  331.             $image = $GO_THEME->images['folder'];
  332.  
  333.             echo '<tr><td><table border="0" cellpadding="0" cellspacing="0">';
  334.             echo '<tr><td>';
  335.             for ($i=1;$i<$sublevel;$i++)
  336.             {
  337.                 if(array_key_exists($i, $prefix)) echo $prefix[$i];
  338.             }
  339.  
  340.             echo $node;
  341.             echo '</td><td><img src="'.$image.'" border="0" /></td>';
  342.             if ($edit_name == $go_mailboxes[$a]['name'])
  343.             {
  344.                 if ($pos = strrpos($go_mailboxes[$a]['name'], $go_mailboxes[$a]['delimiter']))
  345.                 {
  346.                     $location = substr($go_mailboxes[$a]['name'],0,$pos+1);
  347.                 }else
  348.                 {
  349.                     $location = '';
  350.                 }
  351.                 echo '<input type="hidden" name="location" value="'.$location.'" />';
  352.                 echo '<td> <input class="textbox" type="text" name="new_name" value="'.$folder_name.'" /><input type="hidden" name="old_name" value="'.$go_mailboxes[$a]['name'].'" /></td></tr>';
  353.             }else
  354.             {
  355.                 echo "<td> ".$folder_name."</td></tr>";
  356.             }
  357.  
  358.             if ($go_mailboxes[$a]['attributes']&LATT_NOSELECT)
  359.                 $disabled = ' disabled';
  360.             else
  361.                 $disabled = '';
  362.  
  363.             echo '</table></td>';
  364.             if ($go_mailboxes[$a]['subscribed'] == 1)
  365.                 $checked = 'checked';
  366.             else
  367.                 $checked = '';
  368.  
  369.             echo '<td align="center"><input type="checkbox" name="use[]" value="'.$go_mailboxes[$a]['name'].'" '.$checked.' /></td>';
  370.             echo '<td><a href="'.$_SERVER['PHP_SELF'].'?account_id='.$account_id.'&return_to='.urlencode($return_to).'&link_back='.urlencode($link_back).'&edit_name='.urlencode($go_mailboxes[$a]['name']).'" title="'.$strEdit.' '.$go_mailboxes[$a]['name'].'"><img src="'.$GO_THEME->images['edit'].'" border="0" /></a></td>';
  371.             echo "<td><a href='javascript:confirm_action(\"".$_SERVER['PHP_SELF']."?account_id=".$account_id."&return_to=".urlencode($return_to)."&delete_folder=".urlencode($go_mailboxes[$a]['name'])."\",\"".rawurlencode($strDeletePrefix."'".$folder_name."'".$strDeleteSuffix)."\")' title=\"".$strDeleteItem." '".$go_mailboxes[$a]['name']."'\"><img src=\"".$GO_THEME->images['delete']."\" border=\"0\"></a></td>";
  372.             echo '</tr>';
  373.             $lastfolder = $go_mailboxes[$a]['name'];
  374.         }
  375.  
  376.  
  377.         echo '<tr><td colspan="6"><br />';
  378.         $button = new button($cmdOk, "javascript:_save('save', 'true');");
  379.         echo '  ';
  380.         $button = new button($cmdApply, "javascript:_save('save', 'false');");
  381.         echo '  ';
  382.         if ($edit_name != '')
  383.         {
  384.             $button = new button($cmdCancel,'javascript:document.location=\''.$link_back.'\'');
  385.         }else
  386.         {
  387.             $button = new button($cmdClose,'javascript:document.location=\''.$return_to.'\'');
  388.         }
  389.         echo '</td></tr>';
  390.         echo "</td></tr></table>";
  391.         echo "</td></tr></table>";
  392.         $mail->close();
  393. }else
  394. {
  395.     require($GO_THEME->theme_path."header.inc");
  396.  
  397.     echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" name="email_client">';
  398.     echo '<input type="hidden" name="task" value="" />';
  399.     echo '<input type="hidden" name="close" value="false" />';
  400.     echo '<input type="hidden" name="return_to" value="'.$return_to.'" />';
  401.     echo '<input type="hidden" name="link_back" value="'.$link_back.'" />';
  402.     echo '<input type="hidden" name="account_id" value="'.$account_id.'" />';
  403.  
  404.     $tabtable = new tabtable('folders_list', $ml_folders, '600', '300', '100', '', true);
  405.     $tabtable->print_head();
  406.  
  407.     echo '<br /><p class="Error">'.$ml_connect_failed.' \''.$account['host'].'\' '.$ml_at_port.': '.$account['port'].'</p>';
  408.     echo '<p class="Error">'.imap_last_error().'</p><br />'.$ml_solve_error;
  409.     unset($_SESSION['email_id']);
  410. }
  411. ?>
  412. <script type="text/javascript" language="javascript">
  413. function _save(task, close)
  414. {
  415.     document.forms[0].task.value = task;
  416.     document.forms[0].close.value = close;
  417.     document.forms[0].submit();
  418. }
  419. </script>
  420.  
  421. <?php
  422. $tabtable->print_foot();
  423. echo '</form>';
  424. require($GO_THEME->theme_path."footer.inc");
  425. ?>
  426.