home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / email.class.inc < prev    next >
Text File  |  2004-03-08  |  17KB  |  610 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. class email extends db
  13. {
  14.     var $last_error;
  15.  
  16.     function get_accounts($user_id)
  17.     {
  18.         $sql = "SELECT * FROM emAccounts WHERE user_id='$user_id' ORDER BY standard DESC";
  19.         $result = $this->query($sql);
  20.         return $this->num_rows($result);
  21.     }
  22.  
  23.     function add_account($user_id, $type, $host, $port, $mbroot, $username, $password, $name, $email, $signature, $sent, $spam, $trash, $auto_check=0)
  24.     {
  25.  
  26.         global $GO_CONFIG,$GO_CRYPTO;
  27.  
  28.         require_once($GO_CONFIG->class_path."imap.class.inc");
  29.  
  30.         $mail= new imap();
  31.         if ($mail->open($host, $type, $port, $username, $password))
  32.         {
  33.             $next_id = $this->nextid("emAccounts");
  34.             if ($next_id > 0)
  35.             {
  36.                 if ($this->get_accounts($user_id) == 0)
  37.                 {
  38.                     $default="1";
  39.                 }else
  40.                 {
  41.                     $default="0";
  42.                 }
  43.  
  44.                 $sql  = "INSERT INTO emAccounts (id, user_id, type, host, port, mbroot, username, password, name, email, signature, standard, auto_check) ";
  45.                 $sql .= "VALUES ('$next_id', '$user_id', '$type', '".smart_addslashes($host)."', '$port', '".smart_addslashes($mbroot)."', '".smart_addslashes($username)."', '".$GO_CRYPTO->encrypt($password)."', '".smart_addslashes($name)."', '".smart_addslashes($email)."', '".smart_addslashes($signature)."','$default' , '$auto_check')";
  46.                 $this->query($sql);
  47.                 $account_id = $next_id;
  48.             }
  49.  
  50.  
  51.             if ($account_id)
  52.             {
  53.                 if ($type=='imap')
  54.                 {
  55.                     $prefix = '';
  56.  
  57.                     $mailboxes =  $mail->get_mailboxes();
  58.                     $subscribed =  $mail->get_subscribed();
  59.  
  60.                     $mailbox_names = array();
  61.                     $this->synchronise($account_id, $subscribed, $mailboxes);
  62.  
  63.                     while($mailbox = array_shift($mailboxes))
  64.                     {
  65.                         if ($mbroot != '' && strtolower($mailbox['name']) == strtolower($mbroot))
  66.                         {
  67.                             if (substr($mbroot, -1) != $mailbox['delimiter'])
  68.                             {
  69.                                 $prefix = $mbroot.$mailbox['delimiter'];
  70.                             }
  71.                         }
  72.                         $mailbox_names[]=$mailbox['name'];
  73.                     }
  74.  
  75.                     $sent = $sent == '' ? '' : smart_addslashes($prefix.$sent);
  76.                     $spam = $spam == '' ? '' : smart_addslashes($prefix.$spam);
  77.                     $trash = $trash == '' ? '' : smart_addslashes($prefix.$trash);
  78.  
  79.                     $subscribed_names = array();
  80.  
  81.                     while($mailbox = array_shift($subscribed))
  82.                     {
  83.                         $subscribed_names[]=$mailbox['name'];
  84.                     }
  85.  
  86.                     $sent_folder = '';
  87.                     if ($sent != '')
  88.                     {
  89.                         //create sent items folder
  90.                         if (!in_array($sent, $mailbox_names))
  91.                         {
  92.                             if ($mail->create_folder($sent))
  93.                             {
  94.                                 $sent_folder = $sent;
  95.                             }
  96.                         }else
  97.                         {
  98.                             if (!in_array($sent, $subscribed_names))
  99.                             {
  100.                                 $mail->subscribe($sent);
  101.                             }
  102.                             $sent_folder = $sent;
  103.                         }
  104.                     }
  105.  
  106.                     $spam_folder = '';
  107.                     if ($spam != '')
  108.                     {
  109.                         //create spam folder
  110.                         if (!in_array($spam, $mailbox_names))
  111.                         {
  112.                             if($mail->create_folder($spam))
  113.                             {
  114.                                 $spam_folder = $spam;
  115.                             }
  116.                         }else
  117.                         {
  118.                             if (!in_array($spam, $subscribed_names))
  119.                             {
  120.                                 $mail->subscribe($spam);
  121.                             }
  122.                             $spam_folder = $spam;
  123.                         }
  124.                     }
  125.  
  126.                     $trash_folder = '';
  127.                     if ($trash != '')
  128.                     {
  129.                         //create trash folder
  130.                         if (!in_array($trash, $mailbox_names))
  131.                         {
  132.                             if ($mail->create_folder($trash))
  133.                             {
  134.                                 $trash_folder = $trash;
  135.                             }
  136.                         }else
  137.                         {
  138.                             if (!in_array($trash, $subscribed_names))
  139.                             {
  140.                                 $mail->subscribe($trash);
  141.                             }
  142.                             $trash_folder = $trash;
  143.                         }
  144.                     }
  145.  
  146.                     //update the special folders
  147.                     $this->update_folders($account_id, $sent_folder, $spam_folder, $trash_folder);
  148.  
  149.                     $mailboxes =  $mail->get_mailboxes();
  150.                     $subscribed =  $mail->get_subscribed();
  151.                     $this->synchronise($account_id, $mailboxes, $subscribed);
  152.  
  153.                     $mail->close();
  154.                 }
  155.                 return $account_id;
  156.             }else
  157.             {
  158.                 $this->last_error = "<p class=\"Error\">".$strSaveError."</p>";
  159.                 return false;
  160.             }
  161.         }else
  162.         {
  163.             $this->last_error = "<p class=\"Error\">".imap_last_error()."</p>";
  164.             return false;
  165.         }
  166.     }
  167.  
  168.     function update_account($account_id, $type, $host, $port, $mbroot, $username, $password, $name, $email, $signature, $sent, $spam, $trash, $auto_check=0)
  169.     {
  170.         global $GO_CONFIG,$GO_CRYPTO;
  171.  
  172.         require_once($GO_CONFIG->class_path."imap.class.inc");
  173.         $mail= new imap();
  174.  
  175.         $sql  = "UPDATE emAccounts SET type='$type', host='".smart_addslashes($host)."', port='$port', mbroot='".smart_addslashes($mbroot)."', username='".smart_addslashes($username)."', password='".$GO_CRYPTO->encrypt($password)."',";
  176.         $sql .= " name='".smart_addslashes($name)."', email='".smart_addslashes($email)."', signature='".smart_addslashes($signature)."', auto_check='$auto_check' WHERE id='$account_id'";
  177.  
  178.         if ($this->query($sql))
  179.         {
  180.             if ($mail->open($host, $type, $port, $username, $password))
  181.             {
  182.                 $mailboxes =  $mail->get_mailboxes();
  183.                 $subscribed =  $mail->get_subscribed();
  184.                 $this->synchronise($account_id, $subscribed, $mailboxes);
  185.  
  186.                 while($mailbox = array_shift($mailboxes))
  187.                 {
  188.                     $mailbox_names[]=$mailbox['name'];
  189.                     //echo $mailbox['name'].'<br>';
  190.  
  191.                 }
  192.  
  193.  
  194.                 while($mailbox = array_shift($subscribed))
  195.                 {
  196.                     $subscribed_names[]=$mailbox['name'];
  197.                 }
  198.                 $sent_folder = '';
  199.                 if ($sent != '')
  200.                 {
  201.                     //create sent items folder
  202.                     if (!in_array($sent, $mailbox_names))
  203.                     {
  204.                         if ($mail->create_folder($sent))
  205.                         {
  206.                             $sent_folder = $sent;
  207.                         }
  208.                     }else
  209.                     {
  210.                         if (!in_array($sent, $subscribed_names))
  211.                         {
  212.                             $mail->subscribe($sent);
  213.                         }
  214.                         $sent_folder = $sent;
  215.                     }
  216.                 }
  217.  
  218.                 $spam_folder = '';
  219.                 if ($spam != '')
  220.                 {
  221.                     //create spam folder
  222.                     if (!in_array($spam, $mailbox_names))
  223.                     {
  224.                         if($mail->create_folder($spam))
  225.                         {
  226.                             $spam_folder = $spam;
  227.                         }
  228.                     }else
  229.                     {
  230.                         if (!in_array($spam, $subscribed_names))
  231.                         {
  232.                             $mail->subscribe($spam);
  233.                         }
  234.                         $spam_folder = $spam;
  235.                     }
  236.                 }
  237.  
  238.                 $trash_folder = '';
  239.                 if ($trash != '')
  240.                 {
  241.                     //create trash folder
  242.                     if (!in_array($trash, $mailbox_names))
  243.                     {
  244.                         if ($mail->create_folder($trash))
  245.                         {
  246.                             $trash_folder = $trash;
  247.                         }
  248.                     }else
  249.                     {
  250.                         if (!in_array($trash, $subscribed_names))
  251.                         {
  252.                             $mail->subscribe($trash);
  253.                         }
  254.                         $trash_folder = $trash;
  255.                     }
  256.                 }
  257.  
  258.                 //update the special folders
  259.                 $this->update_folders($account_id, $sent_folder, $spam_folder, $trash_folder);
  260.  
  261.                 $mailboxes =  $mail->get_mailboxes();
  262.                 $subscribed =  $mail->get_subscribed();
  263.                 $this->synchronise($account_id, $mailboxes, $subscribed);
  264.  
  265.                 $mail->close();
  266.             }
  267.             return true;
  268.         }
  269.         return false;
  270.     }
  271.  
  272.     function update_password($host, $username, $password)
  273.     {
  274.         global $GO_CRYPTO;
  275.         $sql = "UPDATE emAccounts SET password='".$GO_CRYPTO->encrypt($password)."' WHERE username='$username' AND host='$host'";
  276.         return $this->query($sql);
  277.     }
  278.  
  279.     function update_folders($account_id, $sent, $spam, $trash)
  280.     {
  281.         $sql = "UPDATE emAccounts SET sent='".smart_addslashes($sent)."', spam='".smart_addslashes($spam)."',trash='".smart_addslashes($trash)."' WHERE id='$account_id'";
  282.         return $this->query($sql);
  283.     }
  284.  
  285.     function get_account($id = 0)
  286.     {
  287.         global $GO_CRYPTO;
  288.         if ($id == 0)
  289.         {
  290.             $sql = "SELECT * FROM emAccounts WHERE standard='1' AND user_id='".$_SESSION['GO_SESSION']['user_id']."'";
  291.         }else
  292.         {
  293.             $sql = "SELECT * FROM emAccounts WHERE id='$id'";
  294.         }
  295.  
  296.         $this->query($sql);
  297.         if ($this->next_record())
  298.         {
  299.             return $this->Record;
  300.         }else
  301.         {
  302.             return false;
  303.         }
  304.     }
  305.  
  306.     function is_default_account($id)
  307.     {
  308.         $sql = "SELECT * FROM emAccounts WHERE id='$id' AND standard='1'";
  309.         $this->query($sql);
  310.         if ($this->num_rows() == 1)
  311.         {
  312.             return true;
  313.         }else
  314.         {
  315.             return false;
  316.         }
  317.     }
  318.  
  319.     function delete_account($user_id, $id)
  320.     {
  321.         $default = $this->is_default_account($id);
  322.  
  323.         $sql = "DELETE FROM emAccounts WHERE id='$id' AND user_id='$user_id'";
  324.         if ($this->query($sql))
  325.         {
  326.             $sql = "DELETE FROM emFolders WHERE account_id='$id'";
  327.             $this->query($sql);
  328.             $sql = "DELETE FROM emFilters WHERE account_id='$id'";
  329.             $this->query($sql);
  330.         }
  331.         if ($default)
  332.         {
  333.             $this->get_accounts($user_id);
  334.             $this->next_record();
  335.             $this->set_as_default($this->f("id"), $user_id);
  336.         }
  337.         return true;
  338.     }
  339.  
  340.     function set_as_default($account_id, $user_id)
  341.     {
  342.         $sql = "UPDATE emAccounts SET standard='0' WHERE user_id='$user_id' AND standard='1'";
  343.         $this->query($sql);
  344.         $sql = "UPDATE emAccounts SET standard='1' WHERE id='$account_id'";
  345.         $this->query($sql);
  346.     }
  347.     /*
  348.     gets the subfolder of a folder id. Account id is only usefull for the root level where all folders have parent 0
  349.     */
  350.  
  351.     function get_folders($account_id, $folder_id=-1)
  352.     {
  353.         $sql = "SELECT * FROM emFolders WHERE account_id='$account_id' AND subscribed='1' ";
  354.         if ($folder_id > -1)
  355.         {
  356.             $sql .= "AND parent_id='$folder_id' ";
  357.         }
  358.         $sql .= "ORDER BY NAME ASC";
  359.  
  360.         $this->query($sql);
  361.         return $this->num_rows();
  362.     }
  363.  
  364.     function get_all_folders($account_id, $subscribed_only=false)
  365.     {
  366.         if ($subscribed_only)
  367.         {
  368.             $sql = "SELECT * FROM emFolders WHERE account_id='$account_id' AND subscribed='1' ORDER BY NAME ASC";
  369.         }else
  370.         {
  371.             $sql = "SELECT * FROM emFolders WHERE account_id='$account_id' ORDER BY NAME ASC";
  372.         }
  373.         $this->query($sql);
  374.         return $this->num_rows();
  375.     }
  376.  
  377.  
  378.     function add_folder($account_id, $name, $parent_id=0, $subscribed=1, $delimiter='/', $attributes=0)
  379.     {
  380.         $next_id = $this->nextid("emFolders");
  381.         if ($next_id > 0)
  382.         {
  383.             $sql = "INSERT INTO emFolders (id, parent_id, account_id, subscribed, name, attributes, delimiter) VALUES ('$next_id', '$parent_id', '$account_id', '$subscribed', '".smart_addslashes($name)."', '$attributes', '$delimiter')";
  384.             if ($this->query($sql))
  385.             {
  386.                 return $next_id;
  387.             }
  388.         }
  389.         return false;
  390.     }
  391.     function rename_folder($account_id, $old_name, $new_name)
  392.     {
  393.         $sql = "UPDATE emFolders SET name='".smart_addslashes($new_name)."' WHERE name='".smart_addslashes($old_name)."' AND account_id='$account_id'";
  394.         $this->query($sql);
  395.         $sql = "UPDATE emFilters SET folder='".smart_addslashes($new_name)."' WHERE folder='".smart_addslashes($old_name)."' AND account_id='$account_id'";
  396.         $this->query($sql);
  397.  
  398.     }
  399.  
  400.     function update_folder($folder_id, $parent_id, $subscribed, $attributes)
  401.     {
  402.         $sql = "UPDATE emFolders SET subscribed='$subscribed', parent_id='$parent_id', attributes='$attributes' WHERE id='$folder_id'";
  403.         $this->query($sql);
  404.     }
  405.  
  406.     function delete_folder($account_id, $name)
  407.     {
  408.         $sql = "DELETE FROM emFolders WHERE account_id='$account_id' AND name='".smart_addslashes($name)."'";
  409.         $this->query($sql);
  410.         $sql = "DELETE FROM emFilters WHERE account_id='$account_id' AND folder='".smart_addslashes($name)."'";
  411.         $this->query($sql);
  412.     }
  413.     function folder_exists($account_id, $name)
  414.     {
  415.         $sql = "SELECT id FROM emFolders WHERE name='".smart_addslashes($name)."' AND account_id='$account_id'";
  416.         $this->query($sql);
  417.         if ($this->next_record())
  418.         {
  419.             return $this->f("id");
  420.         }else
  421.         {
  422.             return false;
  423.         }
  424.     }
  425.  
  426.     function get_folder($account_id, $name)
  427.     {
  428.         $sql = "SELECT * FROM emFolders WHERE name='".smart_addslashes($name)."' AND account_id='$account_id'";
  429.         $this->query($sql);
  430.         if ($this->next_record())
  431.         {
  432.             return $this->Record;;
  433.         }else
  434.         {
  435.             return false;
  436.         }
  437.     }
  438.  
  439.     function subscribe($account_id, $name)
  440.     {
  441.         return $this->query("UPDATE emFolders SET subscribed='1' WHERE account_id='$account_id' AND name='".smart_addslashes($name)."'");
  442.     }
  443.  
  444.     function unsubscribe($account_id, $name)
  445.     {
  446.         return $this->query("UPDATE emFolders SET subscribed='0' WHERE account_id='$account_id' AND name='".smart_addslashes($name)."'");
  447.     }
  448.  
  449.     /*
  450.     Gets the parent_id from a folder path
  451.     */
  452.     function get_parent_id($account_id, $path, $delimiter)
  453.     {
  454.         if ($pos = strrpos($path, $delimiter))
  455.         {
  456.             $parent_name = substr($path, 0, $pos);
  457.             if ($parent_folder = $this->get_folder($account_id, $parent_name))
  458.             {
  459.                 return $parent_folder['id'];
  460.             }
  461.         }else
  462.         {
  463.             return 0;
  464.         }
  465.         return false;
  466.  
  467.     }
  468.  
  469.     /*
  470.     IMAP function to synchronise Group-Office folders with the IMAP server
  471.     */
  472.     function synchronise($account_id,$mailboxes, $subscribed)
  473.     {
  474.         //add the subscribed folders into a searchable array
  475.         $subscribed_names = array();
  476.         $mailbox_names = array();
  477.         while($mailbox = array_shift($subscribed))
  478.         {
  479.             $subscribed_names[]=$mailbox['name'];
  480.         }
  481.         //Add all missing folders to Group-Office
  482.         while($mailbox = array_shift($mailboxes))
  483.         {
  484.             $mailbox_names[] = $mailbox['name'];
  485.             $parent_id = $this->get_parent_id($account_id, $mailbox['name'], $mailbox['delimiter']);
  486.             $used = in_array($mailbox['name'], $subscribed_names);
  487.             $attributes = $mailbox['attributes'];
  488.             if ($this->get_folder($account_id,$mailbox['name']))
  489.             {
  490.                 if ($this->f('subscribed') != $used || $this->f('parent_id') != $parent_id || $this->f('attributes') != $attributes)
  491.                 {
  492.                     $this->update_folder($this->f('id'),$parent_id, $used, $attributes);
  493.                 }
  494.             }else
  495.             {
  496.                 $this->add_folder($account_id, $mailbox['name'],$parent_id, $used, $mailbox['delimiter'], $mailbox['attributes']);
  497.             }
  498.  
  499.         }
  500.  
  501.         //get all the Group-Office folders and delete the folders that no longer exist on the IMAP server
  502.         $this->get_all_folders($account_id);
  503.         $GO_mailboxes = array();
  504.         $emailobj = new email();
  505.         while ($this->next_record())
  506.         {
  507.             if (!in_array($this->f('name'), $mailbox_names))
  508.             {
  509.                 $emailobj->delete_folder($account_id, $this->f('name'));
  510.             }
  511.         }
  512.     }
  513.  
  514.     function get_filters($account_id)
  515.     {
  516.         $sql = "SELECT * FROM emFilters WHERE account_id='$account_id' ORDER BY priority DESC";
  517.         $this->query($sql);
  518.         return $this->num_rows();
  519.     }
  520.  
  521.     function add_filter($account_id, $field, $keyword, $folder)
  522.     {
  523.         $next_id = $this->nextid("emFilters");
  524.         if ($next_id > 0)
  525.         {
  526.             $sql = "INSERT INTO emFilters (id, account_id, field, keyword, folder, priority) VALUES ('$next_id','$account_id','".smart_addslashes($field)."','".smart_addslashes($keyword)."','".smart_addslashes($folder)."','$next_id')";
  527.             return $this->query($sql);
  528.         }else
  529.         {
  530.             return false;
  531.         }
  532.     }
  533.  
  534.     function update_filter($id, $field, $keyword, $folder)
  535.     {
  536.         $sql = "UPDATE emFilters SET field='$field', keyword='".smart_addslashes($keyword)."', folder='".smart_addslashes($folder)."'";
  537.         $this->query($sql);
  538.     }
  539.  
  540.     function delete_filter($id)
  541.     {
  542.         $sql = "DELETE FROM emFilters WHERE id='$id'";
  543.         $this->query($sql);
  544.     }
  545.  
  546.     function move_up($move_up_id, $move_dn_id, $move_up_pr, $move_dn_pr)
  547.     {
  548.         if ($move_up_pr == $move_dn_pr)
  549.             $move_up_pr++;
  550.  
  551.         $sql = "UPDATE emFilters SET priority='$move_up_pr' WHERE id='$move_up_id'";
  552.         $this->query($sql);
  553.  
  554.         $sql = "UPDATE emFilters SET priority='$move_dn_pr' WHERE id='$move_dn_id'";
  555.         $this->query($sql);
  556.     }
  557.  
  558.     function delete_user($user_id)
  559.     {
  560.         $del = new email;
  561.         $this->get_accounts($user_id);
  562.         while ($this->next_record())
  563.         {
  564.             $del->delete_account($user_id,$this->f("id"));
  565.         }
  566.     }
  567.  
  568.     function register_attachment($tmp_file, $filename, $filesize, $filemime='', $disposition='attachment', $content_id='')
  569.     {
  570.         global $GO_CONFIG,$GO_CRYPTO;
  571.  
  572.         $filename = smart_addslashes($filename);
  573.  
  574.         $tmp_file = smart_addslashes($tmp_file);
  575.  
  576.         if (!isset($_SESSION['attach_array']))
  577.         {
  578.             $_SESSION['attach_array'] = array();
  579.             $_SESSION['num_attach'] = 1;
  580.         }else
  581.         {
  582.             $_SESSION['num_attach']++;
  583.         }
  584.  
  585.         require_once($GO_CONFIG->class_path.'filetypes.class.inc');
  586.         $filetypes = new filetypes();
  587.  
  588.         if ($filename != '')
  589.         {
  590.             $extension = get_extension($filename);
  591.  
  592.             if(!$type = $filetypes->get_type($extension))
  593.             {
  594.                 $type = $filetypes->add_type($extension, $filemime);
  595.             }
  596.             $filemime = $type['mime'];
  597.         }
  598.  
  599.  
  600.         $_SESSION['attach_array'][$_SESSION['num_attach']]->file_name = $filename;
  601.         $_SESSION['attach_array'][$_SESSION['num_attach']]->tmp_file =  $tmp_file;
  602.         $_SESSION['attach_array'][$_SESSION['num_attach']]->file_size = $filesize;
  603.         $_SESSION['attach_array'][$_SESSION['num_attach']]->file_mime = $filemime;
  604.         $_SESSION['attach_array'][$_SESSION['num_attach']]->disposition = $disposition;
  605.           $_SESSION['attach_array'][$_SESSION['num_attach']]->content_id = $content_id;
  606.  
  607.     }
  608. }
  609. ?>
  610.