home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / functions.inc < prev    next >
Encoding:
Text File  |  2004-03-08  |  31.5 KB  |  987 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. /////////////////////////////////////////////////////////////////////////////////
  14. /////////////////////      Group-Office common functions       //////////////////
  15. /////////////////////////////////////////////////////////////////////////////////
  16.  
  17. function date_to_unixtime($date_string)
  18. {
  19.     $date_string = str_replace('/', '-', $date_string);
  20.  
  21.     $datetime_array = explode(' ', $date_string);
  22.  
  23.     $date = isset($datetime_array[0]) ? $datetime_array[0] : '00-00-0000';
  24.  
  25.     $date_array = explode('-',$datetime_array[0]);
  26.     $year = isset($date_array[2]) ? $date_array[2] : 0;
  27.  
  28.     if ($_SESSION['GO_SESSION']['date_format'] == "m-d-Y")
  29.     {
  30.         $month = isset($date_array[0]) ? $date_array[0] : 0;
  31.         $day = isset($date_array[1]) ? $date_array[1] : 0;
  32.     }else
  33.     {
  34.         $month = isset($date_array[1]) ? $date_array[1] : 0;
  35.         $day = isset($date_array[0]) ? $date_array[0] : 0;
  36.     }
  37.  
  38.     $time = isset($datetime_array[1]) ? $datetime_array[1] : '00:00:00';
  39.     $time_array = explode(':', $time);
  40.  
  41.     $hour = isset($time_array[0]) ? $time_array[0] : 0;
  42.     $min = isset($time_array[1]) ? $time_array[1] : 0;
  43.     $sec = isset($time_array[2]) ? $time_array[2] : 0;
  44.  
  45.  
  46.     return mktime($hour,$min,$sec,$month, $day, $year);
  47. }
  48.  
  49. function date_to_db_date($date_string, $with_time=false)
  50. {
  51.     $date_string = str_replace('/', '-', $date_string);
  52.  
  53.     $datetime_array = explode(' ', $date_string);
  54.  
  55.     $date = isset($datetime_array[0]) ? $datetime_array[0] : '00-00-0000';
  56.  
  57.     $date_array = explode('-',$datetime_array[0]);
  58.     $year = isset($date_array[2]) ? $date_array[2] : 0;
  59.  
  60.     if ($_SESSION['GO_SESSION']['date_format'] == "m-d-Y")
  61.     {
  62.         $month = isset($date_array[0]) ? $date_array[0] : 0;
  63.         $day = isset($date_array[1]) ? $date_array[1] : 0;
  64.     }else
  65.     {
  66.         $month = isset($date_array[1]) ? $date_array[1] : 0;
  67.         $day = isset($date_array[0]) ? $date_array[0] : 0;
  68.     }
  69.  
  70.     $time = isset($datetime_array[1]) ? $datetime_array[1] : '00:00:00';
  71.     $time_array = explode(':', $time);
  72.  
  73.     $hour = isset($time_array[0]) ? $time_array[0] : 0;
  74.     $min = isset($time_array[1]) ? $time_array[1] : 0;
  75.     $sec = isset($time_array[2]) ? $time_array[2] : 0;
  76.  
  77.     $date_format = $with_time ? 'Y-m-d H:i' : 'Y-m-d';
  78.  
  79.     $db_date = "$year-$month-$day";
  80.     if ($with_time)
  81.     {
  82.         $db_date .= ' '.date($_SESSION['GO_SESSION']['timeformat'], mktime($hour, $min, $sec));
  83.     }
  84.  
  85.     return $db_date;
  86. }
  87.  
  88. function db_date_to_date($date_string, $with_time=false)
  89. {
  90.     $datetime_array = explode(' ', $date_string);
  91.  
  92.     $date = isset($datetime_array[0]) ? $datetime_array[0] : '00-00-0000';
  93.  
  94.     $date_array = explode('-',$datetime_array[0]);
  95.  
  96.     $year = isset($date_array[0]) ? $date_array[0] : 0;
  97.     $month = isset($date_array[1]) ? $date_array[1] : 0;
  98.     $day = isset($date_array[2]) ? $date_array[2] : 0;
  99.  
  100.     $time = isset($datetime_array[1]) ? $datetime_array[1] : '00:00:00';
  101.     $time_array = explode(':', $time);
  102.  
  103.     $hour = isset($time_array[0]) ? $time_array[0] : 0;
  104.     $min = isset($time_array[1]) ? $time_array[1] : 0;
  105.     $sec = isset($time_array[2]) ? $time_array[2] : 0;
  106.  
  107.     if ($_SESSION['GO_SESSION']['date_format'] == "m-d-Y")
  108.     {
  109.         $date = "$month-$day-$year";
  110.     }else
  111.     {
  112.         $date = "$day-$month-$year";
  113.     }
  114.  
  115.     if ($with_time)
  116.     {
  117.         $date .= ' '.date($_SESSION['GO_SESSION']['timeformat'], mktime($hour, $min, $sec));
  118.     }
  119.  
  120.     return $date;
  121.  
  122. }
  123.  
  124. function db_date_to_unixtime($date_string)
  125. {
  126.     $datetime_array = explode(' ', $date_string);
  127.  
  128.     $date = isset($datetime_array[0]) ? $datetime_array[0] : '00-00-0000';
  129.  
  130.     $date_array = explode('-',$datetime_array[0]);
  131.  
  132.     $year = isset($date_array[0]) ? $date_array[0] : 0;
  133.     $month = isset($date_array[1]) ? $date_array[1] : 0;
  134.     $day = isset($date_array[2]) ? $date_array[2] : 0;
  135.  
  136.     $time = isset($datetime_array[1]) ? $datetime_array[1] : '00:00:00';
  137.     $time_array = explode(':', $time);
  138.  
  139.     $hour = isset($time_array[0]) ? $time_array[0] : 0;
  140.     $min = isset($time_array[1]) ? $time_array[1] : 0;
  141.     $sec = isset($time_array[2]) ? $time_array[2] : 0;
  142.  
  143.     return mktime($hour,$min,$sec,$month, $day, $year);
  144. }
  145.  
  146. function get_html_body($html)
  147. {
  148.     $to_removed_array = array (
  149.                 "'<html[^>]*>'si",
  150.                 "'</html>'si",
  151.                 "'<body[^>]*>'si",
  152.                 "'</body>'si",
  153.                 "'<head[^>]*>.*?</head>'si",
  154.                 "'<style[^>]*>.*?</style>'si",
  155.                 "'<object[^>]*>.*?</object>'si",
  156.             );
  157.     $html = preg_replace($to_removed_array, '', $html);
  158.     return $html;
  159.  
  160. }
  161. function get_time($server_time=0)
  162. {
  163.     if ($server_time == '0')
  164.     {
  165.         $server_time = time();
  166.     }
  167.  
  168.     $server_timezone_offset = date('Z');
  169.     $time = $server_time+(($_SESSION['GO_SESSION']['timezone']*3600)-$server_timezone_offset);
  170.     return $time;
  171. }
  172.  
  173.  
  174. function get_gmt_time()
  175. {
  176.     $server_timezone_offset = date('Z');
  177.     $time = time()-$server_timezone_offset;
  178.     return $time;
  179. }
  180.  
  181. function split_name($full_name)
  182. {
  183.     $name_arr = explode(' ', $full_name);
  184.  
  185.     $name['first'] = $full_name;
  186.     $name['middle'] = '';
  187.     $name['last'] = $full_name;
  188.     $count = count($name_arr);
  189.     $last_index = $count-1;
  190.     for($i=0;$i<$count;$i++)
  191.     {
  192.         switch ($i)
  193.         {
  194.             case 0:
  195.                 $name['first'] = $name_arr[$i];
  196.             break;
  197.  
  198.             case $last_index:
  199.                 $name['last'] = $name_arr[$i];
  200.             break;
  201.  
  202.             default:
  203.                 $name['middle'] .= $name_arr[$i].' ';
  204.             break;
  205.         }
  206.     }
  207.     $name['middle'] = trim($name['middle']);
  208.     return $name;
  209. }
  210.  
  211. function print_acl($acl_id)
  212. {
  213.     global $GO_CONFIG;
  214.     echo '<iframe width="350"; height="320" border="0" frameborder="0" scrolling="no" src="'.$GO_CONFIG->control_url.'acl.php?acl_id='.$acl_id.'"></iframe>';
  215. }
  216. function getmicrotime(){
  217.     list($usec, $sec) = explode(" ",microtime());
  218.     return ((float)$usec + (float)$sec);
  219. }
  220.  
  221. function detect_browser()
  222. {
  223.     if(eregi('msie ([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version))
  224.     {
  225.         $browser['version']=$log_version[1];
  226.         $browser['name']='MSIE';
  227.     }elseif(eregi('opera/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version))
  228.     {
  229.         $browser['version']=$log_version[1];
  230.         $browser['name']='OPERA';
  231.     }elseif(eregi('mozilla/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version))
  232.     {
  233.         $browser['version']=$log_version[1];
  234.         $browser['name']='MOZILLA';
  235.     }elseif(eregi('netscape/([0-9].[0-9]{1,2})',$_SERVER['HTTP_USER_AGENT'],$log_version))
  236.     {
  237.         $browser['version']=$log_version[1];
  238.         $browser['name']='NETSCAPE';
  239.     }else
  240.     {
  241.         $browser['version']=0;
  242.         $browser['name']='OTHER';
  243.     }
  244.     return $browser;
  245. }
  246.  
  247. function validate_email($email)
  248. {
  249.     return eregi("^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2}([a-z0-9])?([a-z0-9])?$",$email);
  250. }
  251.  
  252. //checks for empty string and returns stripe when empty
  253. function empty_to_stripe($input)
  254. {
  255.     if ($input == "")
  256.     {
  257.         return "-";
  258.     }else
  259.     {
  260.         return $input;
  261.     }
  262. }
  263.  
  264. //creates a mail to link based on the users settings to use
  265. //his own mail client or the Group-Office mail client
  266. function mail_to($email, $name="", $class="normal", $full_link=true, $contact_id=0)
  267. {
  268.     if (validate_email($email))
  269.     {
  270.         global $GO_CONFIG, $GO_SECURITY, $GO_MODULES, $strEmailTo;
  271.  
  272.         $module = $GO_MODULES->get_module('email');
  273.  
  274.         if ($name == "")
  275.                 $name = $email;
  276.  
  277.         if ($_SESSION['GO_SESSION']['mail_client'] == 1 && $module = $GO_MODULES->get_module('email'))
  278.         {
  279.             if ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_read']) || $GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_write']))
  280.             {
  281.                 if ($full_link == true)
  282.                 {
  283.                     return "<a class=\"".$class."\" title=\"".$strEmailTo.$email."\" href='javascript:popup(\"".$GO_CONFIG->host.$module['path']."send.php?mail_to=".$email."&contact_id=".$contact_id."\",\"".$GO_CONFIG->composer_width."\",\"".$GO_CONFIG->composer_height."\")'>".$name."</a>";
  284.                 }else
  285.                 {
  286.                     return 'javascript:popup("'.$GO_CONFIG->host.$module['path'].'send.php?mail_to='.$email.'&contact_id='.$contact_id.'","'.$GO_CONFIG->composer_width.'","'.$GO_CONFIG->composer_height.'")';
  287.                 }
  288.             }
  289.         }
  290.  
  291.         if ($full_link == true)
  292.         {
  293.             return "<a class=\"".$class."\" href=\"mailto:".$email."\" title=\"".$strEmailTo.$email."\">".$name."</a>";
  294.         }else
  295.         {
  296.             return 'mailto:'.$email;
  297.         }
  298.     }
  299. }
  300.  
  301. //Creates a link that pops up a users profile
  302. function show_profile($user_id, $linktext='', $class='normal', $return_to='')
  303. {
  304.     global $strShowProfile,  $strProtectedUser,  $GO_CONFIG;
  305.  
  306.     if ($return_to == '')
  307.     {
  308.         $return_to = $_SERVER['REQUEST_URI'];
  309.     }
  310.  
  311.     $GO_SECURITY = new GO_SECURITY();
  312.     $GO_MODULES = new GO_MODULES();
  313.  
  314.     require_once($GO_CONFIG->class_path."addressbook.class.inc");
  315.     $ab = new addressbook();
  316.  
  317.     if ($linktext == '')
  318.     {
  319.         require_once($GO_CONFIG->class_path."users.class.inc");
  320.         $users = new users();
  321.         $profile = $users->get_user($user_id);
  322.         $middle_name = $profile['middle_name'] == '' ? '' : $profile['middle_name'].' ';
  323.         $linktext = $profile['first_name'].' '.$middle_name.$profile['last_name'];
  324.     }
  325.     $module = $GO_MODULES->get_module('addressbook');
  326.     if ($module && ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_read']) || $GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_write'])) && $contact = $ab->user_is_contact($GO_SECURITY->user_id, $user_id))
  327.     {
  328.         if($contact['color'] != '')
  329.         {
  330.             $style = ' style="color: #'.$contact['color'].';"';
  331.         }else
  332.         {
  333.             $style = '';
  334.         }
  335.         //$link = "<a ".$style." href='javascript:popup(\"".$GO_CONFIG->host.$module['path']."show_contact.php?contact_id=".$contact['id']."\",\"750\",\"550\")' class=\"".$class."\" title=\"".$strShowProfile."\">";
  336.         $link = "<a ".$style." href=\"".$GO_CONFIG->host.$module['path']."contact.php?contact_id=".$contact['id']."&return_to=".urlencode($return_to)."\" class=\"".$class."\" title=\"".$strShowProfile."\">";
  337.         $link .= $linktext."</a>\n";
  338.  
  339.     }else
  340.     {
  341.         if ($GO_SECURITY->user_is_visible($user_id))
  342.         {
  343.             //$link = "<a href='javascript:popup(\"".$GO_CONFIG->control_url."user.php?id=".$user_id."&return_to=".$return_to."\",\"750\",\"550\")' class=\"".$class."\" title=\"".$strShowProfile."\">";
  344.             $link = '<a href="'.$GO_CONFIG->control_url.'user.php?id='.$user_id.'&return_to='.urlencode($return_to).'" class="'.$class.'" title="'.$strShowProfile.'">';
  345.             $link .= $linktext."</a>\n";
  346.         }else
  347.         {
  348.             $link = $linktext;
  349.         }
  350.  
  351.     }
  352.     return $link;
  353. }
  354.  
  355. function show_contact($contact_id, $linktext='', $return_to='')
  356. {
  357.     global $strShowProfile, $strProtectedUser, $GO_CONFIG, $GO_SECURITY, $GO_MODULES;
  358.  
  359.     if ($return_to == '')
  360.     {
  361.         $return_to = $_SERVER['REQUEST_URI'];
  362.     }
  363.  
  364.     $module = $GO_MODULES->get_module('addressbook');
  365.     if ($module && ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_read']) || $GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_write'])))
  366.     {
  367.         require_once($GO_CONFIG->class_path."addressbook.class.inc");
  368.         $ab = new addressbook();
  369.  
  370.         $contact = $ab->get_contact($contact_id);
  371.  
  372.         if($ab->f('color') != '')
  373.         {
  374.             $style = ' style="color: #'.$ab->f('color').';"';
  375.         }else
  376.         {
  377.             $style = '';
  378.         }
  379.  
  380.         if ($linktext == '')
  381.         {
  382.             $middle_name = $contact['middle_name'] == '' ? '' : $contact['middle_name'].' ';
  383.             $linktext = $contact['first_name'].' '.$middle_name.$contact['last_name'];
  384.         }
  385.         $contact_id=$contact['id'];
  386.  
  387.         $link = "<a ".$style." href=\"".$GO_CONFIG->host.$module['path']."contact.php?contact_id=".$contact_id."&return_to=".urlencode($return_to)."\" class=\"normal\" title=\"".$strShowProfile."\">";
  388.         //$link = "<a".$style." href='javascript:popup(\"".$GO_CONFIG->host.$module['path']."show_contact.php?contact_id=".$contact_id."\",\"750\",\"550\")' class=\"normal\" title=\"".$strShowProfile."\">";
  389.         $link .= $linktext."</a>\n";
  390.     }else
  391.     {
  392.         $link = '';
  393.     }
  394.     return $link;
  395. }
  396.  
  397.  
  398. function show_profile_by_email($email, $linktext = '', $return_to='')
  399. {
  400.     if ($return_to == '')
  401.     {
  402.         $return_to = $_SERVER['REQUEST_URI'];
  403.     }
  404.     if ($linktext == '') $linktext = $email;
  405.  
  406.     global $GO_CONFIG, $GO_SECURITY, $strShowProfile, $strAddContact, $GO_MODULES;
  407.  
  408.     require_once($GO_CONFIG->class_path."users.class.inc");
  409.     require_once($GO_CONFIG->class_path."addressbook.class.inc");
  410.     $ab = new addressbook();
  411.     $users = new users();
  412.     $module = $GO_MODULES->get_module('addressbook');
  413.  
  414.     $has_contacts_module = ($module && ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_read']) || $GO_SECURITY->has_permission($GO_SECURITY->user_id, $module['acl_write'])));
  415.  
  416.     if ($has_contacts_module && $contact = $ab->get_contact_profile_by_email($email, $GO_SECURITY->user_id))
  417.     {
  418.         if($contact['color'] != '')
  419.         {
  420.             $style = ' style="color: #'.$contact['color'].';"';
  421.         }else
  422.         {
  423.             $style = '';
  424.         }
  425.         //$link = "<a".$style." href='javascript:popup(\"".$GO_CONFIG->host.$module['path']."show_contact.php?contact_id=".$contact['id']."\",\"750\",\"550\")' class=\"normal\" title=\"".$strShowProfile."\">";
  426.         $link = "<a ".$style." href=\"".$GO_CONFIG->host.$module['path']."contact.php?contact_id=".$contact['id']."&return_to=".urlencode($return_to)."\" class=\"normal\" title=\"".$strShowProfile."\">";
  427.         $link .= $linktext."</a>\n";
  428.     }else
  429.     {
  430.         $users_id = $users->get_user_id_by_email($email);
  431.         if ($users_id && $GO_SECURITY->user_is_visible($users_id))
  432.         {
  433.             $link = "<a href='javascript:popup(\"".$GO_CONFIG->control_url."user.php?id=".$users_id."\",\"750\",\"500\")' class=\"normal\" title=\"".$strShowProfile."\">";
  434.             $link .= $linktext."</a>\n";
  435.         }elseif($has_contacts_module)
  436.         {
  437.             $name = split_name($linktext);
  438.             //$link = "<a href=\"javascript:popup('".$GO_CONFIG->host.$module['path']."add_contact.php?email=".$email."&name=".urlencode($linktext)."','750','550')\" class=\"normal\" title=\"".$strAddContact."\">".$linktext."</a>";
  439.             $link = "<a href=\"".$GO_CONFIG->host.$module['path']."contact.php?email=".$email."&first_name=".urlencode($name['first'])."&middle_name=".urlencode($name['middle'])."&last_name=".urlencode($name['last'])."&return_to=".urlencode($return_to)."\" class=\"normal\" title=\"".$strShowProfile."\">";
  440.             $link .= $linktext."</a>\n";
  441.         }else
  442.         {
  443.             $link = $linktext;
  444.         }
  445.     }
  446.  
  447.     return $link;
  448. }
  449.  
  450. //checks input for invalid characters
  451. function validate_input($input, $invalid_chars = "")
  452. {
  453.     if ($invalid_chars == "")
  454.     {
  455.         $invalid_chars[] = "\"";
  456.         $invalid_chars[] = "/";
  457.         $invalid_chars[] = "?";
  458.         $invalid_chars[] = "&";
  459.     }
  460.  
  461.     for ($i=0; $i<count($invalid_chars);$i++)
  462.     {
  463.         if (strchr($input,$invalid_chars[$i]))
  464.         {
  465.             return false;
  466.         }
  467.     }
  468.     return true;
  469. }
  470.  
  471. //checks the size and formats it to KB, MB or bytes depending on the size given in bytes.
  472. //Also uses user settings to format the number.
  473. function format_size($size, $decimals = 1)
  474. {
  475.     switch ($size)
  476.     {
  477.         case ($size > 1073741824):
  478.             $size = number_format($size / 1073741824, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
  479.             $size .= " GB";
  480.         break;
  481.  
  482.         case ($size > 1048576):
  483.             $size = number_format($size / 1048576, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
  484.             $size .= " MB";
  485.         break;
  486.  
  487.         case ($size > 1024):
  488.             $size = number_format($size / 1024, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
  489.             $size .= " KB";
  490.         break;
  491.  
  492.         default:
  493.             number_format($size, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
  494.             $size .= " bytes";
  495.         break;
  496.     }
  497.     return $size;
  498. }
  499.  
  500. /*
  501. function cut_string($string, $maxlength)
  502. {
  503.     if (strlen($string) > $maxlength)
  504.     {
  505.         return substr($string,0,$maxlength-3)."...";
  506.     }else
  507.     {
  508.         return $string;
  509.     }
  510. }
  511. */
  512. //chops of the string after a given length and puts three dots behind it
  513. //function editted by Tyler Gee to make it chop at whole words
  514. function cut_string($string, $maxlength)
  515. {
  516.     if (strlen($string) > $maxlength)
  517.     {
  518.         $temp = substr($string,0,$maxlength-3);
  519.         if ($pos = strrpos($temp, ' '))
  520.         {
  521.             return substr($temp,0,$pos).'...';
  522.         }else
  523.         {
  524.             return $temp = substr($string,0,$maxlength-3).'...';
  525.         }
  526.  
  527.     }else
  528.     {
  529.         return $string;
  530.     }
  531. }
  532.  
  533. function enriched_to_html($enriched)
  534. {
  535.     global $GO_CONFIG, $GO_MODULES;
  536.  
  537.         // We add space at the beginning and end of the string as it will
  538.         // make some regular expression checks later much easier (so we
  539.         // don't have to worry about start/end of line characters)
  540.         $enriched = ' ' . $enriched . ' ';
  541.  
  542.         // Get color parameters into a more useable format.
  543.         $enriched = preg_replace('/<color><param>([\da-fA-F]+),([\da-fA-F]+),([\da-fA-F]+)<\/param>/Uis', '<color r=\1 g=\2 b=\3>', $enriched);
  544.         $enriched = preg_replace('/<color><param>(red|blue|green|yellow|cyan|magenta|black|white)<\/param>/Uis', '<color n=\1>', $enriched);
  545.  
  546.         // Get font family parameters into a more useable format.
  547.         $enriched = preg_replace('/<fontfamily><param>(\w+)<\/param>/Uis', '<fontfamily f=\1>', $enriched);
  548.  
  549.         // Single line breaks become spaces, double line breaks are a
  550.         // real break. This needs to do <nofill> tracking to be
  551.         // compliant but we don't want to deal with state at this
  552.         // time, so we fake it some day we should rewrite this to
  553.         // handle <nofill> correctly.
  554.         $enriched = preg_replace('/([^\n])\r\n([^\r])/', '\1 \2', $enriched);
  555.         $enriched = preg_replace('/(\r\n)\r\n/', '\1', $enriched);
  556.  
  557.         // We try to protect against bad stuff here.
  558.         $enriched = @htmlspecialchars($enriched, ENT_QUOTES, $charset);
  559.  
  560.         // Now convert the known tags to html. Try to remove any tag
  561.         // parameters to stop people from trying to pull a fast one
  562.         $enriched = preg_replace('/(?<!<)<bold.*>(.*)<\/bold>/Uis', '<span style="font-weight: bold">\1</span>', $enriched);
  563.         $enriched = preg_replace('/(?<!<)<italic.*>(.*)<\/italic>/Uis', '<span style="font-style: italic">\1</span>', $enriched);
  564.         $enriched = preg_replace('/(?<!<)<underline.*>(.*)<\/underline>/Uis', '<span style="text-decoration: underline">\1</span>', $enriched);
  565.         $enriched = preg_replace_callback('/(?<!<)<color r=([\da-fA-F]+) g=([\da-fA-F]+) b=([\da-fA-F]+)>(.*)<\/color>/Uis', 'colorize', $enriched);
  566.         $enriched = preg_replace('/(?<!<)<color n=(red|blue|green|yellow|cyan|magenta|black|white)>(.*)<\/color>/Uis', '<span style="color: \1">\2</span>', $enriched);
  567.         $enriched = preg_replace('/(?<!<)<fontfamily>(.*)<\/fontfamily>/Uis', '\1', $enriched);
  568.         $enriched = preg_replace('/(?<!<)<fontfamily f=(\w+)>(.*)<\/fontfamily>/Uis', '<span style="font-family: \1">\2</span>', $enriched);
  569.         $enriched = preg_replace('/(?<!<)<smaller.*>/Uis', '<span style="font-size: smaller">', $enriched);
  570.         $enriched = preg_replace('/(?<!<)<\/smaller>/Uis', '</span>', $enriched);
  571.         $enriched = preg_replace('/(?<!<)<bigger.*>/Uis', '<span style="font-size: larger">', $enriched);
  572.         $enriched = preg_replace('/(?<!<)<\/bigger>/Uis', '</span>', $enriched);
  573.         $enriched = preg_replace('/(?<!<)<fixed.*>(.*)<\/fixed>/Uis', '<font face="fixed">\1</font>', $enriched);
  574.         $enriched = preg_replace('/(?<!<)<center.*>(.*)<\/center>/Uis', '<div align="center">\1</div>', $enriched);
  575.         $enriched = preg_replace('/(?<!<)<flushleft.*>(.*)<\/flushleft>/Uis', '<div align="left">\1</div>', $enriched);
  576.         $enriched = preg_replace('/(?<!<)<flushright.*>(.*)<\/flushright>/Uis', '<div align="right">\1</div>', $enriched);
  577.         $enriched = preg_replace('/(?<!<)<flushboth.*>(.*)<\/flushboth>/Uis', '<div align="justify">\1</div>', $enriched);
  578.         $enriched = preg_replace('/(?<!<)<paraindent.*>(.*)<\/paraindent>/Uis', '<blockquote>\1</blockquote>', $enriched);
  579.         $enriched = preg_replace('/(?<!<)<excerpt.*>(.*)<\/excerpt>/Uis', '<blockquote>\1</blockquote>', $enriched);
  580.  
  581.         // Now we remove the leading/trailing space we added at the
  582.         // start.
  583.         $enriched = preg_replace('/^ (.*) $/s', '\1', $enriched);
  584.  
  585.         $module = $GO_MODULES->get_module('email');
  586.  
  587.         $enriched = preg_replace("/(?:^|\b)(((http(s?):\/\/)|(www\.-))([\w\.-]+)([,:;%#&\/?=\w+\.\-@]+))(?:\b|$)/is", "<a href=\"http$4://$5$6$7\" target=\"_blank\" class=\"blue\">$1</a>", $enriched);
  588.         if ($_SESSION['GO_SESSION']['mail_client'] == 1)
  589.         {
  590.             $enriched = preg_replace("/(\A|\s)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,3})\b/i","\\1<a href=\"javascript:popup('".$GO_CONFIG->host.$module['path']."send.php?mail_to=\\2\\3\\4\\5','650','500')\" class=\"blue\">\\2\\3\\4\\5</a>",$enriched);
  591.         }else
  592.         {
  593.             $enriched = preg_replace("/(\A|\s)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,3})\b/i","\\1<a href=\"mailto:\\2\\3\\4\\5\" class=\"blue\">\\2\\3\\4\\5</a>",$enriched);
  594.         }
  595.  
  596.         $enriched = nl2br($enriched);
  597.         $enriched = str_replace("\r", "", $enriched);
  598.         $enriched = str_replace("\n", "", $enriched);
  599.  
  600.         return $enriched;
  601.  
  602. }
  603.  
  604. function colorize($colors)
  605. {
  606.     for ($i = 1; $i < 4; $i++) {
  607.         $colors[$i] = sprintf('%02X', round(hexdec($colors[$i]) / 255));
  608.     }
  609.     return '<span style="color: #' . $colors[1] . $colors[2] . $colors[3] . '">' . $colors[4] . '</span>';
  610. }
  611.  
  612.  
  613. //turns plain text into html with url's and email adresses parsed into links
  614. function text_to_html($text, $wordwrap="120")
  615. {
  616.     global $GO_CONFIG, $GO_MODULES;
  617.  
  618.     $module = $GO_MODULES->get_module('email');
  619.  
  620.     $text = htmlspecialchars($text);
  621.     $text = preg_replace("/(?:^|\b)(((http(s?):\/\/)|(www\.-))([\w\.-]+)([,:;%#&\/?=\w+\.\-@]+))(?:\b|$)/is", "<a href=\"http$4://$5$6$7\" target=\"_blank\" class=\"blue\">$1</a>", $text);
  622.     if ($_SESSION['GO_SESSION']['mail_client'] == 1)
  623.     {
  624.         $text = preg_replace("/(\A|\s)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,3})\b/i","\\1<a href=\"javascript:popup('".$GO_CONFIG->host.$module['path']."send.php?mail_to=\\2\\3\\4\\5','650','500')\" class=\"blue\">\\2\\3\\4\\5</a>",$text);
  625.     }else
  626.     {
  627.         $text = preg_replace("/(\A|\s)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,3})\b/i","\\1<a href=\"mailto:\\2\\3\\4\\5\" class=\"blue\">\\2\\3\\4\\5</a>",$text);
  628.     }
  629.  
  630.     $text = nl2br($text);
  631.     $text = str_replace("\r", "", $text);
  632.     $text = str_replace("\n", "", $text);
  633.  
  634.     return ($text);
  635.  
  636. }
  637.  
  638. //strips unwanted html tags and converts links to suit Group-Office
  639. function convert_html($html, $remove = true)
  640. {
  641.     global $GO_CONFIG, $GO_MODULES;
  642.  
  643.     //$html = str_replace("\r", "", $html);
  644.     //$html = str_replace("\n", "", $html);
  645.  
  646.     $module = $GO_MODULES->get_module('email');
  647.  
  648.     if ($remove)
  649.     {
  650.         $to_removed_array = array (
  651.             "'<html>'si",
  652.             "'</html>'si",
  653.             "'<body[^>]*>'si",
  654.             "'</body>'si",
  655.             "'<head[^>]*>.*?</head>'si",
  656.             "'<style[^>]*>.*?</style>'si",
  657.             "'<script[^>]*>.*?</script>'si",
  658.             "'<object[^>]*>.*?</object>'si",
  659.             "'<embed[^>]*>.*?</embed>'si",
  660.             "'<applet[^>]*>.*?</applet>'si",
  661.             "'<mocha[^>]*>.*?</mocha>'si"
  662.         );
  663.         $html= preg_replace($to_removed_array, '', $html);
  664.         //$html = preg_replace("|href=\"(.*)script:|i", 'href="removed_script:', $html);
  665.         //$html = preg_replace("|<([^>]*)java|i", '<removed_java_tag', $html);
  666.         //$html = preg_replace("|<([^>]*)&{.*}([^>]*)>|i", "<&{;}\\3>", $html);
  667.         //$html = preg_replace("|<([^>]*)mocha:([^>]*)>|i", "<removed_mocha:\\2>",$html);
  668.     }
  669.  
  670.     if ($_SESSION['GO_SESSION']['mail_client'] == 1)
  671.     {
  672.         $html = preg_replace("/(href=\"mailto:)([\w\.\-]+)(@)([\w\.\-\"]+)\b/i","href=\"javascript:popup('".$GO_CONFIG->host.$module['path']."send.php?mail_to=$2$3$4','650','500')\" class=\"blue\"",$html);
  673.     }
  674.  
  675.     $html = preg_replace("/\b((href=\"http(s?):\/\/))([\w\.\?\=\&]+)([\/\w+\.\?\=\&\:\~]+)\b/i"," target=\"_blank\" class=\"blue\" href=\"http$3://$4$5$6", $html);
  676.     $html = preg_replace("/\b((href='http(s?):\/\/))([\w\.\?\=\&]+)([\/\w+\.\?\=\&\:\~]+)\b/i"," target=\"_blank\" class=\"blue\" href='http$3://$4$5$6", $html);
  677.  
  678.     return ($html);
  679. }
  680.  
  681.  
  682. function cut_address($addr, $charset)
  683. {
  684.     $addr = smartstrip($addr);
  685.  
  686.     $addresses = array();
  687.     $token = '';
  688.     $quote_esc = false;
  689.     for ($i = 0; $i < strlen($addr); $i++) {
  690.         $c = substr($addr, $i, 1);
  691.  
  692.         if($c == '"') {
  693.             $quote_esc = !$quote_esc;
  694.         }
  695.  
  696.         if($c == ',' || $c == ';') {
  697.             if(!$quote_esc) {
  698.                 $token = trim($token);
  699.                 if($token != '') {
  700.                     $addresses[] = $token;
  701.                 }
  702.                 $token = '';
  703.                 continue;
  704.             }
  705.         }
  706.  
  707.         $token .= $c;
  708.     }
  709.     if(!$quote_esc) {
  710.         $token = trim($token);
  711.         if($token != '') {
  712.             $addresses[] = $token;
  713.         }
  714.     }
  715.     return ($addresses);
  716. }
  717.  
  718. function get_addresses_from_string($address_string)
  719. {
  720.     $in_address = false;
  721.     $address = '';
  722.     $addresses = array();
  723.     $length = strlen($address_string);
  724.     for($i=0;$i<$length;$i++)
  725.     {
  726.         $char = $address_string[$i];
  727.         if ($char == '>')
  728.         {
  729.             if ($in_address)
  730.             {
  731.                 $in_address = false;
  732.                 $addresses[] = trim($address);
  733.                 $address = '';
  734.             }
  735.         }elseif($char == '<')
  736.         {
  737.             $in_address=true;
  738.         }elseif($in_address)
  739.         {
  740.             $address .= $char;
  741.         }
  742.     }
  743.     return $addresses;
  744. }
  745.  
  746. function get_crlf($smtp = "")
  747. {
  748.     $crlf = stristr(PHP_OS, 'Windows') ? "\r\n" : "\n";
  749.  
  750.         if ($smtp != "")
  751.             $crlf = $smtp ? "\r\n" : $crlf;
  752.  
  753.     //return ($crlf);
  754.     return "\r\n";
  755. }
  756.  
  757.  
  758. function smartstrip($string)
  759. {
  760.     if(get_magic_quotes_gpc()) {
  761.         $string = stripslashes($string);
  762.     }
  763.     return $string;
  764. }
  765.  
  766. function smart_addslashes($string)
  767. {
  768.     if(!get_magic_quotes_gpc()) {
  769.         $string = addslashes($string);
  770.     }
  771.     return $string;
  772. }
  773.  
  774.  
  775. function quote($text)
  776. {
  777.     $text = "> " . ereg_replace("\n", "\n> ", trim($text));
  778.     return($text);
  779. }
  780.  
  781. function sendmail($email_to, $email_from, $name_from, $subject, $body, $priority = '3', $body_ctype = 'text/PLAIN')
  782. {
  783.     global $GO_CONFIG, $charset, $php_mailer_lang;
  784.  
  785.     require_once($GO_CONFIG->class_path."phpmailer/class.phpmailer.php");
  786.     require_once($GO_CONFIG->class_path."phpmailer/class.smtp.php");
  787.     require_once($GO_CONFIG->class_path."html2text.class.inc");
  788.  
  789.     $mail = new PHPMailer();
  790.     $mail->PluginDir = $GO_CONFIG->class_path.'phpmailer/';
  791.     $mail->SetLanguage($php_mailer_lang, $GO_CONFIG->class_path.'phpmailer/language/');
  792.  
  793.     if ($GO_CONFIG->smtp_server != '' && $GO_CONFIG->smtp_port != '')
  794.     {
  795.         $mail->IsSMTP();
  796.         $mail->Host = $GO_CONFIG->smtp_server;
  797.         $mail->Port = $GO_CONFIG->smtp_port;
  798.     }
  799.     $mail->Priority = $priority;
  800.     $mail->From     = $email_from;
  801.     $mail->FromName = $name_from;
  802.     $mail->AddReplyTo($email_from, $name_from);
  803.     $mail->WordWrap = 50;
  804.     $html_message = strtolower($body_ctype) == 'text/html' ? true : false;
  805.     $mail->IsHTML($html_message);
  806.     $mail->Subject = smartstrip(trim($subject));
  807.  
  808.     $mail_to_array = cut_address(trim($email_to), $charset);
  809.     while ($to_address = array_shift($mail_to_array))
  810.     {
  811.         $mail->AddAddress($to_address);
  812.     }
  813.  
  814.     if ($html_message)
  815.     {
  816.         $mail->Body = smartstrip($body);
  817.         $h2t =& new html2text($body);
  818.         $mail->AltBody  = $h2t->get_text();
  819.     }else
  820.     {
  821.         $mail->Body = smartstrip($body);
  822.     }
  823.  
  824.     return $mail->Send();
  825. }
  826.  
  827. //returns a link poping up a open file dialog. A HANDLER FILE MUST BE CREATED TO DO SOMETHING WITH THE FILE SELECTED
  828. function show_open($GO_HANDLER, $GO_FILTER, $link_text="", $class="normal", $fulllink=true)
  829. {
  830.         global $GO_CONFIG, $GO_MODULES;
  831.  
  832.         $module = $GO_MODULES->get_module('filesystem');
  833.  
  834.         if ($fulllink)
  835.         {
  836.             $link = "<a class=\"".$class."\" href=\"javascript:popup('".$GO_CONFIG->host.$module['path']."index.php?GO_HANDLER=".urlencode($GO_HANDLER)."&GO_FILTER=".$GO_FILTER."&mode=popup','600','400')\">".$link_text."</a>";
  837.         }else
  838.         {
  839.             $link = "javascript:popup('".$GO_CONFIG->host.$module['path']."index.php?GO_HANDLER=".urlencode($GO_HANDLER)."&GO_FILTER=".$GO_FILTER."&mode=popup','600','400')";
  840.         }
  841.         return $link;
  842. }
  843.  
  844. //returns a link poping up a save file dialog. A temporarely file to save must be passed
  845. //optionally a creator file can be specified. This file will simply be included in the save script.
  846. //this is added to create the temporary file
  847.  
  848. function show_save($tmpfile, $link_text="", $class="normal", $fulllink=true)
  849. {
  850.     global $GO_CONFIG, $GO_MODULES;
  851.  
  852.     $module = $GO_MODULES->get_module('files');
  853.  
  854.     if ($fulllink)
  855.     {
  856.         $link = "<a class=\"".$class."\" href=\"javascript:popup('".$GO_CONFIG->host.$module['path']."save.php?SET_HANDLER=".urlencode($tmpfile)."','600','400')\">".$link_text."</a>";
  857.     }else
  858.     {
  859.         $link = "javascript:popup('".$GO_CONFIG->host.$module['path']."save.php?GO_HANDLER=".urlencode($tmpfile)."','400','450')";
  860.     }
  861.     return $link;
  862. }
  863. function strip_extension($filename)
  864. {
  865.     $pos = strrpos($filename,'.');
  866.     if ($pos)
  867.     {
  868.         $filename = substr($filename,0,$pos);
  869.     }
  870.     return $filename;
  871. }
  872.  
  873. function get_extension($filename)
  874. {
  875.     $extension = '';
  876.     $pos = strrpos($filename,'.');
  877.     if ($pos)
  878.     {
  879.         $extension= substr($filename,$pos+1,strlen($filename));
  880.     }
  881.     return $extension;
  882. }
  883.  
  884. //gets the filetype
  885. function filemime($path)
  886. {
  887.     global $GO_CONFIG;
  888.     require_once($GO_CONFIG->class_path.'filetypes.class.inc');
  889.     $filetypes = new filetypes();
  890.  
  891.     if(!$type = $filetypes->get_type($filetypes->get_extension(basename($path))))
  892.     {
  893.         $type = $filetypes->add_type($filetypes->get_extension(basename($path)));
  894.     }
  895.  
  896.     if ($type)
  897.     {
  898.         return $type['mime'];
  899.     }else
  900.     {
  901.         return false;
  902.     }
  903. }
  904.  
  905. //gets the file type in a friendly way like text/HTML will be HTML document
  906. function filemimefriendly($path)
  907. {
  908.     global $GO_CONFIG;
  909.     require_once($GO_CONFIG->class_path.'filetypes.class.inc');
  910.     $filetypes = new filetypes();
  911.  
  912.     if(!$type = $filetypes->get_type(get_extension(basename($path))))
  913.     {
  914.         $type = $filetypes->add_type(get_extension(basename($path)));
  915.     }
  916.  
  917.     if ($type)
  918.     {
  919.         return $type['friendly'];
  920.     }else
  921.     {
  922.         return false;
  923.     }
  924. }
  925.  
  926. function path_to_url($path)
  927. {
  928.     global $GO_CONFIG;
  929.     if ($path = str_replace($GO_CONFIG->root_path, $GO_CONFIG->host, $path))
  930.     {
  931.         return $path;
  932.     }
  933.     return false;
  934. }
  935.  
  936.  
  937. // Find a system program.  Do path checking
  938. function find_program ($program) {
  939.     $path = array('/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
  940.     while ($this_path = current($path)) {
  941.         if (is_executable("$this_path/$program")) {
  942.             return "$this_path/$program";
  943.         }
  944.         next($path);
  945.     }
  946.     return;
  947. }
  948.  
  949.  
  950. // Execute a system program. return a trim()'d result.
  951. // does very crude pipe checking.  you need ' | ' for it to work
  952. // ie $program = execute_program('netstat', '-anp | grep LIST');
  953. // NOT $program = execute_program('netstat', '-anp|grep LIST');
  954. function execute_program ($program, $args = '') {
  955.     $buffer = '';
  956.     $program = find_program($program);
  957.  
  958.     if (!$program) { return; }
  959.  
  960.     // see if we've gotten a |, if we have we need to do patch checking on the cmd
  961.     if ($args) {
  962.         $args_list = split(' ', $args);
  963.         for ($i = 0; $i < count($args_list); $i++) {
  964.             if ($args_list[$i] == '|') {
  965.                 $cmd = $args_list[$i + 1];
  966.                 $new_cmd = find_program($cmd);
  967.                 $args = ereg_replace("\| $cmd", "| $new_cmd", $args);
  968.             }
  969.         }
  970.     }
  971.  
  972.     // we've finally got a good cmd line.. execute it
  973.     if ($fp = popen("$program $args", 'r')) {
  974.         while (!feof($fp)) {
  975.             $buffer .= fgets($fp, 4096);
  976.         }
  977.         return trim($buffer);
  978.     }
  979. }
  980.  
  981. function array_csort($marray, $column) {
  982.   foreach ($marray as $row) {
  983.     $sortarr[] = $row[$column];
  984.   }
  985.   array_multisort($sortarr, $marray);
  986.   return $marray;
  987. }
  988.