[ PHPXref.com ] [ Generated: Sun Jul 20 18:02:19 2008 ] [ Group-Office 2.15-FINAL-7 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/ -> functions.inc (source)

   1  <?php
   2  /**
   3   * @copyright Intermesh 2004
   4   * @author Merijn Schering <mschering@intermesh.nl>
   5   * @version $Revision: 1.202 $ $Date: 2006/05/31 09:32:48 $
   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_once($GO_CONFIG->root_path.'functions_compat.inc');
  14   
  15   /**
  16   * This file holds global functions for use inside Group-Office
  17   *
  18   * @package Framework
  19   * @author   Merijn Schering <mschering@intermesh.nl>
  20   * @since    Group-Office 1.0
  21   */
  22  
  23  
  24   
  25   function quoted_printable_encode($str)
  26   {
  27           global $GO_CONFIG;
  28           require_once($GO_CONFIG->class_path."mail/phpmailer/class.phpmailer.php");        
  29          $mail = new PHPMailer();    
  30          
  31          return trim($mail->EncodeQP ($str));
  32   
  33   }
  34   
  35   
  36   	function array_to_form($array, $prefix='', $suffix='')
  37       {
  38           $form = '';
  39          foreach($array as $key => $value)
  40          {
  41              if(is_array($value))
  42              {
  43                  $form .= array_to_form($value, $prefix.$key.$suffix.'[',']');
  44              }else
  45              {
  46                  $input = new input('hidden', $prefix.smart_stripslashes($key).$suffix, smart_stripslashes($value),false);
  47                  $form .= $input->get_html();
  48              }
  49          }
  50          return $form;
  51      }
  52  
  53   /**
  54   * Creates a directory recursively
  55   *
  56   * @param    string $path
  57   * @access public
  58   * @return bool True on success
  59   */
  60  
  61  function mkdir_recursive($path) {
  62    global $GO_CONFIG;
  63  
  64    $dirs_to_create = array ();
  65    while (!is_writable($path)) {
  66      $dirs_to_create[] = $path;
  67      if(dirname($path) == $path)
  68      {
  69           exit('Failed to create '.var_export($dirs_to_create, true));
  70      }
  71      $path = dirname($path);
  72    }
  73    $old_umask = umask(000);    
  74    while ($dir = array_pop($dirs_to_create)) {
  75      if (@ !mkdir($dir, $GO_CONFIG->create_mode)) {
  76        die("Failed creating required directory ".$dir);
  77      }
  78    }
  79    umask($old_umask);
  80    return true;
  81  }
  82  
  83  /**
  84   * Creates a JUpload java applet that can upload multiple files using HTTP
  85   *
  86   * @param    string $post_url The URL to the script that handles the uploaded files
  87   * @access public
  88   * @return void
  89   */
  90  
  91  function jupload($post_url, $width, $height) {
  92    global $GO_CONFIG, $jupload_post_url, $jupload_width, $jupload_height;
  93    $jupload_post_url = $post_url;
  94    $jupload_width = $width;
  95    $jupload_height = $height;
  96  
  97    require_once ($GO_CONFIG->control_path.'JUpload/jupload.inc');
  98  }
  99  
 100  
 101  
 102  /**
 103   * Add a log entry to syslog if enabled in config.php
 104   *
 105   * @param    int $level The log level. See sys_log() of the PHP docs
 106   * @param    string $message The log message
 107   * @access public
 108   * @return void
 109   */
 110  function go_log($level, $message) {
 111    global $GO_CONFIG;
 112    if ($GO_CONFIG->log) {
 113      $messages = str_split($message, 500);
 114      for ($i = 0; $i < count($messages); $i ++) {
 115        syslog($level, $messages[$i]);
 116      }
 117    }
 118  }
 119  
 120  /**
 121   * Grab an e-mail address out of a string
 122   *
 123   * @param    int $level The log level. See sys_log() of the PHP docs
 124   * @param    string $message The log message
 125   * @access public
 126   * @return void
 127   */
 128  function get_email_from_string($email) {
 129    if (preg_match("/(\b)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,4})\b/i", $email, $matches)) {
 130      return $matches[0];
 131    } else {
 132      return false;
 133    }
 134  }
 135  
 136  function get_name_from_string($string) {
 137    if (preg_match('/([\D]*|[\D]*[\040][\D]*)/i', $string, $matches)) {
 138      $matches[0] = str_replace('"', '', $matches[0]);
 139      return $matches[0];
 140    } else {
 141      return $string;
 142    }
 143  }
 144  
 145  function get_filetype_image($extension) {
 146    global $GO_THEME;
 147  
 148    if (isset ($GO_THEME->filetypes[$extension])) {
 149      return $GO_THEME->filetypes[$extension];
 150    } else {
 151      return $GO_THEME->filetypes['unknown'];
 152    }
 153  }
 154  
 155  function get_filetype_description($extension) {
 156    global $filetypes;
 157  
 158    if (isset ($filetypes[$extension])) {
 159      return $filetypes[$extension];
 160    } else {
 161      return $filetypes['unknown'];
 162    }
 163  }
 164  
 165  
 166  
 167    function get_timezone_offset($time = 0) {
 168      if ($time == 0)
 169        $time = time();
 170  
 171      if (date('I', $time) == 0) {
 172        return $_SESSION['GO_SESSION']['timezone'];
 173      } else {
 174        return $_SESSION['GO_SESSION']['timezone'] + $_SESSION['GO_SESSION']['DST'];
 175      }
 176    }
 177  
 178  /**
 179   * Adds paramaters to an URL
 180   *
 181   * @param    string $url
 182   * @param    string $params
 183   * @access public
 184   * @return string
 185   */
 186  
 187  function add_params_to_url($url, $params) {
 188    if (strpos($url, '?') === false) {
 189      $url .= '?'.$params;
 190    } else {
 191      $url .= '&'.$params;
 192    }
 193    return $url;
 194  }
 195  
 196  /**
 197   * Get's all queries from an SQL dump file in an array
 198   *
 199   * @param    string $file The absolute path to the SQL file
 200   * @access public
 201   * @return array An array of SQL strings
 202   */
 203  
 204  function get_sql_queries($file) {
 205    $sql = '';
 206    $queries = array ();
 207    if ($handle = fopen($file, "r")) {
 208      while (!feof($handle)) {
 209        $buffer = trim(fgets($handle, 4096));
 210        if ($buffer != '' && substr($buffer, 0, 1) != '#' && substr($buffer, 0, 1) != '-') {
 211      $sql .= $buffer;
 212        }
 213      }
 214      fclose($handle);
 215    } else {
 216      die("Could not read SQL dump file $file!");
 217    }
 218    $length = strlen($sql);
 219    $in_string = false;
 220    $start = 0;
 221    $escaped = false;
 222    for ($i = 0; $i < $length; $i ++) {
 223      $char = $sql[$i];
 224      if ($char == '\'' && !$escaped) {
 225        $in_string = !$in_string;
 226      }
 227      if ($char == ';' && !$in_string) {
 228        $offset = $i - $start;
 229        $queries[] = substr($sql, $start, $offset);
 230  
 231        $start = $i +1;
 232      }
 233      if ($char == '\\') {
 234        $escaped = true;
 235      } else {
 236        $escaped = false;
 237      }
 238    }
 239    return $queries;
 240  }
 241  
 242  /**
 243   * Converts a Group-Office date to unix time.
 244   *
 245   * A Group-Office date is formated by user preference.
 246   *
 247   * @param    string $date_string The date string formated in the user's preference
 248   * @access public
 249   * @return int unix timestamp
 250   */
 251  
 252  function date_to_unixtime($date_string) {
 253    $date_string = trim($date_string);
 254    if ($date_string != '') {
 255  
 256      $datetime_array = explode(' ', $date_string);
 257  
 258      $date = isset ($datetime_array[0]) ? 
 259          $datetime_array[0] : 
 260          '0000'.$_SESSION['GO_SESSION']['date_seperator'].
 261          '00'.$_SESSION['GO_SESSION']['date_seperator'].'00';
 262  
 263      $date_array = explode($_SESSION['GO_SESSION']['date_seperator'], $datetime_array[0]);
 264      $year = isset ($date_array[2]) ? $date_array[2] : 0;
 265      
 266      $format = str_replace($_SESSION['GO_SESSION']['date_seperator'],'',$_SESSION['GO_SESSION']['date_format']);
 267      
 268      $year_pos = strpos($format, 'Y');
 269      $month_pos = strpos($format, 'm');
 270      $day_pos = strpos($format, 'd');
 271  
 272      $year = isset ($date_array[$year_pos]) ? $date_array[$year_pos] : 0;
 273      $month = isset ($date_array[$month_pos]) ? $date_array[$month_pos] : 0;
 274      $day = isset ($date_array[$day_pos]) ? $date_array[$day_pos] : 0;
 275      
 276      $time = isset ($datetime_array[1]) ? $datetime_array[1] : '00:00:00';
 277      $time_array = explode(':', $time);
 278  
 279      $hour = isset ($time_array[0]) ? $time_array[0] : 0;
 280      $min = isset ($time_array[1]) ? $time_array[1] : 0;
 281      $sec = isset ($time_array[2]) ? $time_array[2] : 0;
 282  
 283      return adodb_mktime($hour, $min, $sec, $month, $day, $year);
 284    }
 285    return false;
 286  }
 287  
 288  function get_dateformat($format, $seperator)
 289  {
 290      $newformat = '';
 291      $end = strlen($format)-1;
 292      for($i=0;$i<$end;$i++)
 293      {
 294          $newformat .= $format[$i].$seperator;
 295      }
 296      $newformat .= $format[$i];
 297      return $newformat;
 298  }
 299  
 300  /**
 301   * Convert a Group-Office date to MySQL date format
 302   *
 303   * A Group-Office date is formated by user preference.
 304   *
 305   * @param    string $date_string The Group-Office date string
 306   * @param    bool $with_time The output sting should contain time too
 307   * @access public
 308   * @return int unix timestamp
 309   */
 310  
 311  function date_to_db_date($date_string, $with_time = false) {
 312      if(empty($date_string))
 313      {
 314          return '';
 315      }
 316    $time = date_to_unixtime($date_string);
 317    $date_format = $with_time ? 'Y-m-d H:i' : 'Y-m-d';
 318       return adodb_date($date_format, $time);
 319  }
 320  
 321  
 322  /**
 323   * Convert a MySQL date to Group-Office date.
 324   *
 325   * A Group-Office date is formated by user preference.
 326   *
 327   * @param    string     $date_string The MySQL date string
 328   * @access public
 329   * @return string Group-Office date string
 330   */
 331  
 332  function db_date_to_date($date_string) {
 333    if ($date_string == '' || $date_string == '0000-00-00' || $date_string == '0000-00-00 00:00') {
 334      return '';
 335    }
 336    $datetime_array = explode(' ', $date_string);
 337  
 338    $date = isset ($datetime_array[0]) ? $datetime_array[0] : '0000-00-00';
 339  
 340    $date_array = explode('-', $datetime_array[0]);
 341  
 342    $year = isset ($date_array[0]) ? $date_array[0] : 0;
 343    $month = isset ($date_array[1]) ? $date_array[1] : 0;
 344    $day = isset ($date_array[2]) ? $date_array[2] : 0;
 345  
 346    $date = adodb_date($_SESSION['GO_SESSION']['date_format'], adodb_mktime(0, 0, 0, $month, $day, $year));
 347  
 348    if(isset ($datetime_array[1]))
 349      {
 350          $time_array = explode(':', $datetime_array[1]);
 351      
 352          $hour = isset ($time_array[0]) ? $time_array[0] : 0;
 353          $min = isset ($time_array[1]) ? $time_array[1] : 0;
 354          $sec = isset ($time_array[2]) ? $time_array[2] : 0;    
 355          
 356          $date .= ' '.adodb_date($_SESSION['GO_SESSION']['time_format'], adodb_mktime($hour, $min, $sec));
 357      }
 358  
 359      return $date;
 360  }
 361  /*
 362  function php_to_pear_date_format($date_format)
 363  {
 364      $date_format = str_replace('G', 'H', $date_format);
 365      $date_format = str_replace('i', 'm', $date_format);
 366      
 367      $pear_date_format = '';    
 368    for($i=0;$i<strlen($date_format);$i++)
 369    {
 370        if($date_format[$i] != $_SESSION['GO_SESSION']['date_seperator'] && $date_format[$i]!= ' ' && $date_format[$i]!= ':')
 371        {
 372            $pear_date_format .= '%';
 373        }
 374        $pear_date_format .= $date_format[$i];
 375    }
 376    return $pear_date_format;
 377  }
 378  
 379  function db_date_to_date($date_string) {
 380    if ($date_string == '') {
 381      return '';
 382    }
 383      
 384    $date_format = (strlen($date_string) > 10) ? 
 385        $_SESSION['GO_SESSION']['date_format'].' '.$_SESSION['GO_SESSION']['time_format'] : 
 386            $_SESSION['GO_SESSION']['date_format'];    
 387    
 388  
 389    $date = new go_date($date_string);
 390    return $date->format(php_to_pear_date_format($date_format));
 391  }
 392  */
 393  /**
 394  * Convert a MySQL date to unix time format
 395  *
 396  * @param    int            $date_string The MySQL date string
 397  * @access public
 398  * @return int unix timestamp
 399  */
 400  
 401  function db_date_to_unixtime($date_string) {
 402      $datetime_array = explode(' ', $date_string);
 403  
 404      $date = isset ($datetime_array[0]) ? $datetime_array[0] : '0000-00-00';
 405  
 406      $date_array = explode('-', $datetime_array[0]);
 407  
 408      $year = isset ($date_array[0]) ? $date_array[0] : 0;
 409      $month = isset ($date_array[1]) ? $date_array[1] : 0;
 410      $day = isset ($date_array[2]) ? $date_array[2] : 0;
 411  
 412      $time = isset ($datetime_array[1]) ? $datetime_array[1] : '00:00:00';
 413      $time_array = explode(':', $time);
 414  
 415      $hour = isset ($time_array[0]) ? $time_array[0] : 0;
 416      $min = isset ($time_array[1]) ? $time_array[1] : 0;
 417      $sec = isset ($time_array[2]) ? $time_array[2] : 0;
 418  
 419      return adodb_mktime($hour, $min, $sec, $month, $day, $year);
 420  }
 421  
 422  function get_timestamp($gmt_time)
 423  {
 424      return date($_SESSION['GO_SESSION']['date_format'].' '.
 425          $_SESSION['GO_SESSION']['time_format'], 
 426          gmt_to_local_time($gmt_time));
 427  }
 428  
 429  /**
 430  * Convert a unix time stamp to MySQL date format
 431  *
 432  * @param    int         $date_string The MySQL date string
 433  * @access public
 434  * @return string MySQL timestamp
 435  */
 436  
 437  function unixtime_to_db_date($unixtime) {
 438      return adodb_date('Y-m-d');
 439  }
 440  
 441  /**
 442  * Return only the contents of the body tag from a HTML page
 443  *
 444  * @param    string $html A HTML formatted string
 445  * @access public
 446  * @return string HTML formated string
 447  */
 448  
 449  function get_html_body($html) {
 450      $to_removed_array = array ("'<html[^>]*>'si", "'</html>'si", "'<body[^>]*>'si", "'</body>'si", "'<head[^>]*>.*?</head>'si", "'<style[^>]*>.*?</style>'si", "'<object[^>]*>.*?</object>'si",);
 451  
 452      //$html = str_replace("\r", "", $html);
 453      //$html = str_replace("\n", "", $html);
 454  
 455      $html = preg_replace($to_removed_array, '', $html);
 456      return $html;
 457  
 458  }
 459  
 460  /**
 461  * Get the user's local time using the timezone offset preference.
 462  *
 463  * @param    string $server_time The time according to the server. 
 464  *                    If empty time() is used.
 465  * @access public
 466  * @return int unix timestamp
 467  */
 468  
 469  function get_time($server_time = 0) {
 470      if ($server_time == '0') {
 471          $server_time = time();
 472      }
 473  
 474      $server_timezone_offset = date('Z', $server_time);
 475  
 476      $time = $server_time + ((get_timezone_offset($server_time) * 3600) - $server_timezone_offset);
 477      return $time;
 478  }
 479  
 480  /**
 481  * Get current GMT time.
 482  *
 483  * @param    string $server_time The server time
 484  *
 485  * @access public
 486  * @return int unix timestamp
 487  */
 488  
 489  function server_to_gmt_time($server_time)
 490  {
 491      $server_time -= get_server_timezone_offset($server_time);
 492      return $server_time;
 493  }
 494  
 495  function get_server_timezone_offset($time=0)
 496  {    
 497      if($time == 0) $time = time();
 498       return adodb_date('Z', $time);
 499  }
 500  
 501  /**
 502  * Get current GMT time.
 503  *
 504  * @param    string $local_time The local time of the logged in user.
 505  *                    If empty the current time in GMT will be returned.
 506  * @access public
 507  * @return int unix timestamp
 508  */
 509  function get_gmt_time($local_time = 0) {
 510      if ($local_time == 0) {
 511          $time = time() - get_server_timezone_offset($local_time);
 512      } else {
 513          $timezone_offset = get_timezone_offset($local_time);
 514          
 515          //echo 'Local time: '.date('Y-m-d G:i', $local_time).'<br />';
 516          //echo 'Timezone offset: '.$timezone_offset.'<br />';
 517              
 518          $time = $local_time - ($timezone_offset * 3600);
 519          //echo 'GMT time: '.date('Y-m-d G:i', $time).'<hr />';
 520      }
 521      return $time;
 522  }
 523  
 524  function gmt_to_local_time($gmt_time) {
 525      $timezone_offset = get_timezone_offset($gmt_time);
 526      $local_time = $gmt_time + ($timezone_offset * 3600);
 527  
 528      return $local_time;
 529  }
 530  
 531  function local_to_gmt_time($local_time) {
 532      $timezone_offset = get_timezone_offset($local_time);
 533      $gmt_time = $local_time - ($timezone_offset * 3600);
 534  
 535      return $gmt_time;
 536  }
 537  
 538  
 539  /**
 540  * Give it a full name and it tries to determine the First, Middle and Lastname
 541  *
 542  * @param    string $full_name A full name
 543  * @access public
 544  * @return array array with keys first, middle and last
 545  */
 546  
 547  function split_name($full_name) {
 548      $name_arr = explode(' ', $full_name);
 549  
 550      $name['first'] = $full_name;
 551      $name['middle'] = '';
 552      $name['last'] = '';
 553      $count = count($name_arr);
 554      $last_index = $count -1;
 555      for ($i = 0; $i < $count; $i ++) {
 556          switch ($i) {
 557              case 0 :
 558                  $name['first'] = $name_arr[$i];
 559                  break;
 560  
 561              case $last_index :
 562                  $name['last'] = $name_arr[$i];
 563                  break;
 564  
 565              default :
 566                  $name['middle'] .= $name_arr[$i].' ';
 567                  break;
 568          }
 569      }
 570      $name['middle'] = trim($name['middle']);
 571      return $name;
 572  }
 573  
 574  /**
 575  * Prints an iframe containing a dialog to modify an Access Control List
 576  *
 577  * @param    int $acl_id The acl_id to modify
 578  * @access public
 579  * @return void
 580  */
 581  function print_acl($acl_id) {
 582      echo get_acl($acl_id);
 583  }
 584  
 585  function get_acl($acl_id)
 586  {
 587      global $GO_CONFIG;
 588      return  '<iframe style="display:block;" width="350"; height="340;" border="0" frameborder="0" '.'scrolling="no" src="'.$GO_CONFIG->control_url.'acl.php?acl_id='.$acl_id.'"></iframe>';
 589  }
 590  
 591  /**
 592  * Get the current server time in microseconds
 593  *
 594  * @access public
 595  * @return int
 596  */
 597  function getmicrotime() {
 598      list ($usec, $sec) = explode(" ", microtime());
 599      return ((float) $usec + (float) $sec);
 600  }
 601  
 602  /**
 603  * Returns an array with browser information
 604  *
 605  * @access public
 606  * @return array Array contains keys name, version and subversion
 607  */
 608  function detect_browser() {
 609      if (eregi('msie ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
 610          $browser['version'] = $log_version[1];
 611          $browser['name'] = 'MSIE';
 612      }
 613      elseif (eregi('opera/([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
 614          $browser['version'] = $log_version[1];
 615          $browser['name'] = 'OPERA';
 616      }
 617      elseif (eregi('mozilla/([0-9].[0-9]{1,2}).*gecko/([0-9]+)', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
 618          $browser['version'] = $log_version[1];
 619          $browser['name'] = 'MOZILLA';
 620          $browser['subversion'] = $log_version[2];
 621      }
 622      elseif (eregi('netscape/([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
 623          $browser['version'] = $log_version[1];
 624          $browser['name'] = 'NETSCAPE';
 625      }
 626      elseif (eregi('safari/([0-9]+.[0-9]+)', $_SERVER['HTTP_USER_AGENT'], $log_version)) {
 627          $browser['version'] = $log_version[1];
 628          $browser['name'] = 'SAFARI';
 629      } else {
 630          $browser['version'] = 0;
 631          $browser['name'] = 'OTHER';
 632      }
 633      return $browser;
 634  }
 635  
 636  /**
 637  * Get the regex used for validating an email address
 638  * Requires the Top Level Domain to be between 2 and 6 alphanumeric chars
 639  *
 640  * @param    none
 641  * @access    public
 642  * @return    string
 643  */
 644  function get_email_validation_regex() {
 645      return "^([a-z0-9]+)([._-]([a-z0-9]+))*[@]([a-z0-9]+)([._-]([a-z0-9]+))*[.]([a-z0-9]){2,6}$";
 646  }
 647  
 648  /**
 649  * Check if an email adress is in a valid format
 650  *
 651  * @param    string $email E-mail address
 652  * @access public
 653  * @return bool
 654  */
 655  function validate_email($email) {
 656      return eregi(get_email_validation_regex(), $email);
 657  }
 658  
 659  /**
 660  * Checks for empty string and returns stripe when empty
 661  *
 662  * @param    string $input Any string
 663  * @access public
 664  * @return string
 665  */
 666  function empty_to_stripe($input) {
 667      if ($input == "") {
 668          return "-";
 669      } else {
 670          return $input;
 671      }
 672  }
 673  
 674  /**
 675  * Build mailto link based on user's preference
 676  * 
 677  * creates a mail to link based on the users settings to use 
 678  * his own mail client or the Group-Office mail client
 679  *
 680  * @param    string $email The reciever's address
 681  * @param    string $name The reciever's name
 682  * @param    string $class The CSS class of the link
 683  * @param    bool $full_link Return a full link or just the href attribute
 684  * @param    int $contact_id The contact id of the reciever
 685  * @access public
 686  * @return string URL to open e-mail client
 687  */
 688  function mail_to($email, $name = "", $class = "normal", $full_link = true, $contact_id = 0, $company_id = 0) {
 689      global $GO_CONFIG, $GO_SECURITY, $GO_MODULES, $strEmailTo, $charset;
 690  
 691      $email = validate_email(get_email_from_string($email)) ? $email : '';
 692  
 693      $module = isset ($GO_MODULES->modules['email']) ? $GO_MODULES->modules['email'] : false;
 694  
 695      $email = htmlspecialchars($email, ENT_QUOTES);
 696  
 697      if ($name == "")
 698          $name = $email;
 699  
 700      if ($module['read_permission']) {
 701          if ($full_link == true) {
 702              return "<a class=\"".$class."\" title=\"".$strEmailTo.stripslashes($email)."\" href='javascript:popup(\"".$module['url']."send.php?mail_to=".$email."&contact_id=".$contact_id."&company_id=".$company_id."\",\"".$GO_CONFIG->composer_width."\",\"".$GO_CONFIG->composer_height."\")'>".$name."</a>";
 703          } else {
 704              return 'javascript:openPopup("_blank", "'.$module['url'].'send.php?mail_to='.$email.'&contact_id='.$contact_id.'&company_id='.$company_id.'","'.$GO_CONFIG->composer_width.'","'.$GO_CONFIG->composer_height.'", "no")';
 705          }
 706  
 707      } else {
 708          $email = get_email_from_string($email);
 709          if ($full_link == true) {
 710              return "<a class=\"".$class."\" href=\"mailto:".$email."\" title=\"".$strEmailTo.$email."\">".$name."</a>";
 711          } else {
 712              return 'mailto:'.$email;
 713          }
 714      }
 715  }
 716  
 717  function get_mail_to_href($email='', $contact_id = 0, $company_id = 0)
 718  {
 719      global $GO_MODULES, $GO_CONFIG;
 720      
 721      if (isset($GO_MODULES->modules['email']) && $GO_MODULES->modules['email']['read_permission']) {
 722          return "javascript:openPopup('_blank', '".$GO_MODULES->modules['email']['url'].
 723              "send.php?mail_to=".htmlentities($email, ENT_QUOTES)."&contact_id=".
 724              $contact_id."&company_id=".$company_id."','".
 725              $GO_CONFIG->composer_width."','".$GO_CONFIG->composer_height."','no')";    
 726      }else
 727      {
 728          return 'mailto:'.get_email_from_string($email);
 729      }
 730  }
 731  /**
 732  * Return a formatted address string
 733  * 
 734  * @param    array $object User or contact
 735  * @access public
 736  * @return string Address formatted
 737  */
 738  function address_format($object, $linebreak = '<br />') {
 739      if (isset ($object['name'])) {
 740          $name = $object['name'];
 741      } else {
 742          $middle_name = $object['middle_name'] == '' ? '' : $object['middle_name'].' ';
 743  
 744          if ($object['title'] != '' && $object['initials'] != '') {
 745              $name = $object['title'].' '.$object['initials'].' '.$middle_name.$object['last_name'];
 746          } else {
 747              $name = $object['first_name'].' '.$middle_name.$object['last_name'];
 748          }
 749      }
 750  
 751      $address = $name.$linebreak;
 752  
 753      if ($object['address'] != '') {
 754          $address .= $object['address'];
 755          if (isset ($object['address_no'])) {
 756              $address .= ' '.$object['address_no'];
 757          }
 758          $address .= $linebreak;
 759      }
 760      if ($object['zip'] != '') {
 761          $address .= $object['zip'].' ';
 762      }
 763      if ($object['city'] != '') {
 764          $address .= $object['city'].$linebreak;
 765      }
 766      if ($object['country'] != '') {
 767          $address .= $object['country'].$linebreak;
 768      }
 769      return $address;
 770  
 771  }
 772  
 773  /**
 774  * Return an URL to a user profile page
 775  * 
 776  * @param    int $user_id The user_id
 777  * @param    string $linktext The text to display
 778  * @param    string $class The CSS class of the link
 779  * @param    string $return_to The URL to return to when Back is clicked
 780  * @access public
 781  * @return string URL to a user profile page
 782  */
 783  function show_profile($user_id, $linktext = '', $class = 'normal', $return_to = '') {
 784      global $strShowProfile, $strProtectedUser, $GO_CONFIG, $GO_USERS, $go_security_class;
 785  
 786      if ($return_to == '') {
 787          $return_to = $_SERVER['REQUEST_URI'];
 788      }
 789  
 790      $GO_SECURITY = new $go_security_class();
 791      $GO_MODULES = new GO_MODULES();
 792  
 793      if ($linktext == '') {
 794          $profile = $GO_USERS->get_user($user_id);
 795          $middle_name = $profile['middle_name'] == '' ? '' : $profile['middle_name'].' ';
 796          $linktext = $profile['first_name'].' '.$middle_name.$profile['last_name'];
 797      }
 798      
 799      if ($GO_SECURITY->user_is_visible($user_id)) {
 800          if(isset($GO_MODULES->modules['users']) && $GO_MODULES->modules['users']['read_permission'])
 801          {
 802              $link = '<a href="'.$GO_MODULES->modules['users']['url'].'edit_user.php?user_id='.$user_id.'&return_to='.urlencode($return_to).'" class="'.$class.'" title="'.$strShowProfile.'">';
 803              $link .= $linktext."</a>\n";
 804          }else
 805          {
 806              $link = '<a href="'.$GO_CONFIG->control_url.'user.php?id='.$user_id.'&return_to='.urlencode($return_to).'" class="'.$class.'" title="'.$strShowProfile.'">';
 807              $link .= $linktext."</a>\n";
 808          }
 809      } else {
 810          $link = $linktext;
 811      }
 812  
 813      
 814      return $link;
 815  }
 816  
 817  /**
 818  * Return an URL to a project
 819  * 
 820  * @param    int $project_id The project_id
 821  * @param    string $linktext The text to display
 822  * @param    string $return_to The URL to return to when Back is clicked
 823  * @access public
 824  * @return string URL to project page
 825  */
 826  function show_project($project_id, $linktext = '', $return_to = '') {
 827      global $GO_CONFIG, $GO_SECURITY, $GO_MODULES;
 828  
 829      if ($return_to == '') {
 830          $return_to = $_SERVER['REQUEST_URI'];
 831      }
 832  
 833      $link = '';
 834  
 835      $projects_module = isset ($GO_MODULES->modules['projects']) ? $GO_MODULES->modules['projects'] : false;
 836      if ($projects_module && $projects_module['read_permission']) {
 837          require_once ($GO_MODULES->modules['projects']['class_path'].'projects.class.inc');
 838          $projects = new projects();
 839  
 840          if ($project = $projects->get_project($project_id)) {
 841              if ($linktext == '') {
 842                  $linktext = $project['name'];
 843              }
 844  
 845              $link = "<a ".$style." href=\"".$projects_module['url']."project.php?project_id=".$project_id."&return_to=".urlencode($return_to)."\" class=\"normal\">";
 846              $link .= $linktext."</a>\n";
 847          }
 848      }
 849      return $link;
 850  }
 851  
 852  /**
 853  * Return an URL to a contact profile page
 854  * 
 855  * @param    int $contact_id The contact_id
 856  * @param    string $linktext The text to display
 857  * @param    string $return_to The URL to return to when Back is clicked
 858  * @access public
 859  * @return string URL to Contact profile page
 860  */
 861  function show_contact($contact_id, $linktext = '', $return_to = '') {
 862      global $strShowProfile, $strProtectedUser, $GO_CONFIG, $GO_SECURITY, $GO_MODULES;
 863  
 864      if ($return_to == '') {
 865          $return_to = $_SERVER['REQUEST_URI'];
 866      }
 867  
 868      $link = '';
 869  
 870      $ab_module = isset ($GO_MODULES->modules['addressbook']) ? $GO_MODULES->modules['addressbook'] : false;
 871      if ($ab_module) {
 872          require_once ($GO_MODULES->modules['addressbook']['class_path'].'addressbook.class.inc');
 873          $ab = new addressbook();
 874          
 875          
 876          if ($contact = $ab->get_contact($contact_id)) {
 877              if ($ab->f('color') != '') {
 878                  $style = ' style="color: #'.$ab->f('color').';"';
 879              } else {
 880                  $style = '';
 881              }
 882  
 883              if ($linktext == '') {
 884                  $middle_name = $contact['middle_name'] == '' ? '' : $contact['middle_name'].' ';
 885                  $linktext = $contact['first_name'].' '.$middle_name.$contact['last_name'];
 886              }
 887              
 888              if($ab_module['read_permission'] &&     
 889                  ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $contact['acl_read']) ||    
 890                  $GO_SECURITY->has_permission($GO_SECURITY->user_id, $contact['acl_write'])))
 891              {
 892                  $link = "<a ".$style." href=\"".$ab_module['url']."contact.php?contact_id=".$contact_id."&return_to=".urlencode($return_to)."\" class=\"normal\" title=\"".$strShowProfile."\">";
 893                  $link .= $linktext."</a>\n";
 894              }else
 895              {
 896                  $link .= '<span '.$style.'>'.$linktext.'</a>';
 897              }
 898          }
 899      }
 900      return $link;
 901  }
 902  
 903  /**
 904  * Return an URL to a company profile page
 905  * 
 906  * @param    int $company_id The company_id
 907  * @param    string $linktext The text to display
 908  * @param    string $return_to The URL to return to when Back is clicked
 909  * @access public
 910  * @return string URL to company profile page
 911  */
 912  
 913  function show_company($company_id, $linktext = '', $return_to = '') {
 914      global $strShowProfile, $GO_CONFIG, $GO_SECURITY, $GO_MODULES;
 915  
 916      if ($return_to == '') {
 917          $return_to = $_SERVER['REQUEST_URI'];
 918      }
 919  
 920      $link = '';
 921      $ab_module = isset ($GO_MODULES->modules['addressbook']) ? $GO_MODULES->modules['addressbook'] : false;
 922      if ($ab_module) {
 923          require_once ($ab_module['class_path'].'addressbook.class.inc');
 924          $ab = new addressbook();
 925  
 926          if ($company = $ab->get_company($company_id)) {
 927              if ($linktext == '') {
 928                  $linktext = $company['name'];
 929              }
 930  
 931              if($ab_module['read_permission'] && 
 932                  ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $company['acl_read']) || 
 933                  $GO_SECURITY->has_permission($GO_SECURITY->user_id, $company['acl_write'])))
 934              {
 935                  $link = "<a href=\"".$ab_module['url']."company.php?company_id=".$company_id."&return_to=".urlencode($return_to)."\" class=\"normal\" title=\"".$strShowProfile."\">";
 936                  $link .= $linktext."</a>\n";
 937              }else
 938              {
 939                  $link .= $linktext;
 940              }
 941          }
 942      }
 943      return $link;
 944  }
 945  
 946  /**
 947  * Return an URL to a user or contact profile page
 948  * 
 949  * @param    string $email The e-mail address to search
 950  * @param    string $linktext The text to display
 951  * @param    string $return_to The URL to return to when Back is clicked
 952  * @access public
 953  * @return string URL to a user profile page
 954  */
 955  function show_profile_by_email($email, $linktext = '', $return_to = '') {
 956      global $GO_CONFIG, $GO_SECURITY, $strShowProfile, $strAddContact, $GO_MODULES, $GO_USERS;
 957  
 958      $linktext = ($linktext == '') ? get_name_from_string($email) : $linktext;
 959      $email = get_email_from_string($email);
 960  
 961      if ($return_to == '') {
 962          $return_to = $_SERVER['REQUEST_URI'];
 963      }
 964  
 965      $ab_module = isset ($GO_MODULES->modules['addressbook']) ? $GO_MODULES->modules['addressbook'] : false;
 966      if ($ab_module) {
 967          require_once ($GO_MODULES->modules['addressbook']['class_path'].'addressbook.class.inc');
 968          $ab = new addressbook();
 969      }
 970  
 971      if ($ab_module && $contact = $ab->get_contact_by_email($email, $GO_SECURITY->user_id)) {
 972          if ($contact['color'] != '') {
 973              $style = ' style="color: #'.$contact['color'].';"';
 974          } else {
 975              $style = '';
 976          }
 977          if($ab_module['read_permission'] && 
 978          ($GO_SECURITY->has_permission($GO_SECURITY->user_id, $contact['acl_read']) || 
 979          $GO_SECURITY->has_permission($GO_SECURITY->user_id, $contact['acl_write'])))
 980          {
 981              $link = "<a ".$style." href=\"".$GO_MODULES->modules['addressbook']['url']."contact.php?contact_id=".$contact['id']."&return_to=".urlencode($return_to)."\" class=\"normal\" title=\"".$strShowProfile."\">";
 982              $link .= $linktext."</a>\n";
 983          }else
 984          {
 985              $link = '<span '.$style.'>'.$linktext.'</a>';
 986          }
 987      } else {
 988          $user = $GO_USERS->get_user_by_email($email);
 989          
 990          if ($user && $GO_SECURITY->user_is_visible($GO_SECURITY->user_id)) {
 991              $link = '<a href="'.$GO_CONFIG->control_url.'user.php?id='.$user['id'].'&return_to='.urlencode($return_to).'" title="'.$strShowProfile.'">';
 992              $link .= $linktext."</a>\n";
 993          }    elseif ($ab_module && $ab_module['read_permission']) {
 994              $name = split_name($linktext);
 995              $link = "<a href=\"".$ab_module['url']."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=\"".$strAddContact."\">";
 996              $link .= $linktext."</a>\n";
 997          } else {
 998              $link = $linktext;
 999          }
1000      }
1001  
1002      return $link;
1003  }
1004  
1005  /**
1006  * Check precence of invalid characters
1007  * 
1008  * @param    string $input The string to check
1009  * @param    array $invalid_chars An array of invalid charcters
1010  * @access public
1011  * @return bool
1012  */
1013  function validate_input($input, $invalid_chars = "") {
1014      if ($invalid_chars == "") {
1015          $invalid_chars[] = "\"";
1016          $invalid_chars[] = "/";
1017          $invalid_chars[] = "?";
1018          $invalid_chars[] = "&";
1019      }
1020  
1021      for ($i = 0; $i < count($invalid_chars); $i ++) {
1022          if (strchr($input, $invalid_chars[$i])) {
1023              return false;
1024          }
1025      }
1026      return true;
1027  }
1028  
1029  /**
1030  * Format a number by using the user preferences
1031  * 
1032  * @param    int $number The number
1033  * @param    int $decimals Number of decimals to display
1034  * @access public
1035  * @return string
1036  */
1037  
1038  function format_number($number, $decimals=2)
1039  {
1040      return number_format($number, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
1041  }
1042  
1043  /**
1044  * Format a number by using the user preferences
1045  * 
1046  * @param    int $number The number
1047  * @param    int $decimals Number of decimals to display
1048  * @access public
1049  * @return string
1050  */
1051  
1052  function number_to_phpnumber($number)
1053  {
1054      $number = str_replace($_SESSION['GO_SESSION']['thousands_seperator'],'', $number);
1055      return str_replace($_SESSION['GO_SESSION']['decimal_seperator'],'.',$number);
1056  }
1057  
1058  /**
1059  * Format a size to a human readable format.
1060  * 
1061  * @param    int $size The size in bytes
1062  * @param    int $decimals Number of decimals to display
1063  * @access public
1064  * @return string
1065  */
1066  
1067  function format_size($size, $decimals = 1) {
1068      switch ($size) {
1069          case ($size > 1073741824) :
1070              $size = number_format($size / 1073741824, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
1071              $size .= " GB";
1072              break;
1073  
1074          case ($size > 1048576) :
1075              $size = number_format($size / 1048576, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
1076              $size .= " MB";
1077              break;
1078  
1079          case ($size > 1024) :
1080              $size = number_format($size / 1024, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
1081              $size .= " KB";
1082              break;
1083  
1084          default :
1085              number_format($size, $decimals, $_SESSION['GO_SESSION']['decimal_seperator'], $_SESSION['GO_SESSION']['thousands_seperator']);
1086              $size .= " bytes";
1087              break;
1088      }
1089      return $size;
1090  }
1091  
1092  /**
1093  * Chop long strings with 3 dots
1094  *
1095  * Chops of the string after a given length and puts three dots behind it
1096  * function editted by Tyler Gee to make it chop at whole words
1097  * 
1098  * @param    string $string The string to chop
1099  * @param    int $maxlength The maximum number of characters in the string
1100  * @access public
1101  * @return string
1102  */
1103  
1104  function cut_string($string, $maxlength, $cut_whole_words = true) {
1105      if (strlen($string) > $maxlength) {
1106          $temp = substr($string, 0, $maxlength -3);
1107          if ($cut_whole_words) {
1108              if ($pos = strrpos($temp, ' ')) {
1109                  return substr($temp, 0, $pos).'...';
1110              } else {
1111                  return $temp = substr($string, 0, $maxlength -3).'...';
1112              }
1113          } else {
1114              return $temp.'...';
1115          }
1116  
1117      } else {
1118          return $string;
1119      }
1120  }
1121  
1122  /**
1123  * Convert an enriched formated string to HTML format
1124  *
1125  * @param    string $enriched Enriched formatted string
1126  * @access public
1127  * @return string HTML formated string
1128  */
1129  function enriched_to_html($enriched, $convert_links=true) {
1130      global $GO_CONFIG, $GO_MODULES;
1131  
1132      // We add space at the beginning and end of the string as it will
1133      // make some regular expression checks later much easier (so we
1134      // don't have to worry about start/end of line characters)
1135      $enriched = ' '.$enriched.' ';
1136  
1137      // Get color parameters into a more useable format.
1138      $enriched = preg_replace('/<color><param>([\da-fA-F]+),([\da-fA-F]+),([\da-fA-F]+)<\/param>/Uis', '<color r=\1 g=\2 b=\3>', $enriched);
1139      $enriched = preg_replace('/<color><param>(red|blue|green|yellow|cyan|magenta|black|white)<\/param>/Uis', '<color n=\1>', $enriched);
1140  
1141      // Get font family parameters into a more useable format.
1142      $enriched = preg_replace('/<fontfamily><param>(\w+)<\/param>/Uis', '<fontfamily f=\1>', $enriched);
1143  
1144      // Single line breaks become spaces, double line breaks are a
1145      // real break. This needs to do <nofill> tracking to be
1146      // compliant but we don't want to deal with state at this
1147      // time, so we fake it some day we should rewrite this to
1148      // handle <nofill> correctly.
1149      $enriched = preg_replace('/([^\n])\r\n([^\r])/', '\1 \2', $enriched);
1150      $enriched = preg_replace('/(\r\n)\r\n/', '\1', $enriched);
1151  
1152      // We try to protect against bad stuff here.
1153      $enriched = @ htmlspecialchars($enriched, ENT_QUOTES);
1154  
1155      // Now convert the known tags to html. Try to remove any tag
1156      // parameters to stop people from trying to pull a fast one
1157      $enriched = preg_replace('/(?<!&lt;)&lt;bold.*&gt;(.*)&lt;\/bold&gt;/Uis', '<span style="font-weight: bold">\1</span>', $enriched);
1158      $enriched = preg_replace('/(?<!&lt;)&lt;italic.*&gt;(.*)&lt;\/italic&gt;/Uis', '<span style="font-style: italic">\1</span>', $enriched);
1159      $enriched = preg_replace('/(?<!&lt;)&lt;underline.*&gt;(.*)&lt;\/underline&gt;/Uis', '<span style="text-decoration: underline">\1</span>', $enriched);
1160      $enriched = preg_replace_callback('/(?<!&lt;)&lt;color r=([\da-fA-F]+) g=([\da-fA-F]+) b=([\da-fA-F]+)&gt;(.*)&lt;\/color&gt;/Uis', 'colorize', $enriched);
1161      $enriched = preg_replace('/(?<!&lt;)&lt;color n=(red|blue|green|yellow|cyan|magenta|black|white)&gt;(.*)&lt;\/color&gt;/Uis', '<span style="color: \1">\2</span>', $enriched);
1162      $enriched = preg_replace('/(?<!&lt;)&lt;fontfamily&gt;(.*)&lt;\/fontfamily&gt;/Uis', '\1', $enriched);
1163      $enriched = preg_replace('/(?<!&lt;)&lt;fontfamily f=(\w+)&gt;(.*)&lt;\/fontfamily&gt;/Uis', '<span style="font-family: \1">\2</span>', $enriched);
1164      $enriched = preg_replace('/(?<!&lt;)&lt;smaller.*&gt;/Uis', '<span style="font-size: smaller">', $enriched);
1165      $enriched = preg_replace('/(?<!&lt;)&lt;\/smaller&gt;/Uis', '</span>', $enriched);
1166      $enriched = preg_replace('/(?<!&lt;)&lt;bigger.*&gt;/Uis', '<span style="font-size: larger">', $enriched);
1167      $enriched = preg_replace('/(?<!&lt;)&lt;\/bigger&gt;/Uis', '</span>', $enriched);
1168      $enriched = preg_replace('/(?<!&lt;)&lt;fixed.*&gt;(.*)&lt;\/fixed&gt;/Uis', '<font face="fixed">\1</font>', $enriched);
1169      $enriched = preg_replace('/(?<!&lt;)&lt;center.*&gt;(.*)&lt;\/center&gt;/Uis', '<div align="center">\1</div>', $enriched);
1170      $enriched = preg_replace('/(?<!&lt;)&lt;flushleft.*&gt;(.*)&lt;\/flushleft&gt;/Uis', '<div align="left">\1</div>', $enriched);
1171      $enriched = preg_replace('/(?<!&lt;)&lt;flushright.*&gt;(.*)&lt;\/flushright&gt;/Uis', '<div align="right">\1</div>', $enriched);
1172      $enriched = preg_replace('/(?<!&lt;)&lt;flushboth.*&gt;(.*)&lt;\/flushboth&gt;/Uis', '<div align="justify">\1</div>', $enriched);
1173      $enriched = preg_replace('/(?<!&lt;)&lt;paraindent.*&gt;(.*)&lt;\/paraindent&gt;/Uis', '<blockquote>\1</blockquote>', $enriched);
1174      $enriched = preg_replace('/(?<!&lt;)&lt;excerpt.*&gt;(.*)&lt;\/excerpt&gt;/Uis', '<blockquote>\1</blockquote>', $enriched);
1175  
1176      // Now we remove the leading/trailing space we added at the
1177      // start.
1178      $enriched = preg_replace('/^ (.*) $/s', '\1', $enriched);
1179  
1180      $module = $GO_MODULES->modules['email'];
1181  
1182      if($convert_links)
1183      {
1184          $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);
1185          if ($_SESSION['GO_SESSION']['mail_client'] == 1) {
1186              $enriched = preg_replace("/(\A|\s)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,3})\b/i", "\\1<a href=\"javascript:top.main.popup('".$module['url']."send.php?mail_to=\\2\\3\\4\\5','".$GO_CONFIG->composer_width."','".$GO_CONFIG->composer_height."')\" class=\"blue\">\\2\\3\\4\\5</a>", $enriched);
1187          } else {
1188              $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);
1189          }
1190      }
1191  
1192      $enriched = nl2br($enriched);
1193      $enriched = str_replace("\r", "", $enriched);
1194      $enriched = str_replace("\n", "", $enriched);
1195  
1196      return $enriched;
1197  
1198  }
1199  
1200  /**
1201  * Used by enriched_to_html() to create colors
1202  *
1203  * @param    array $colors
1204  * @access private
1205  * @return string HTML formatted string
1206  */
1207  function colorize($colors) {
1208      for ($i = 1; $i < 4; $i ++) {
1209          $colors[$i] = sprintf('%02X', round(hexdec($colors[$i]) / 255));
1210      }
1211      return '<span style="color: #'.$colors[1].$colors[2].$colors[3].'">'.$colors[4].'</span>';
1212  }
1213  
1214  /**
1215  * Convert plain text to HTML
1216  *
1217  * @param    string $text Plain text string
1218  * @access public
1219  * @return string HTML formatted string
1220  */
1221  function text_to_html($text, $convert_links=true) {
1222      global $GO_CONFIG, $GO_MODULES;
1223  
1224      $module = isset ($GO_MODULES->modules['email']) ? $GO_MODULES->modules['email'] : false;
1225  
1226      $text = htmlspecialchars($text);    
1227      if($convert_links)
1228      {
1229          $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);
1230          if ($_SESSION['GO_SESSION']['mail_client'] == 1) {
1231              $text = preg_replace("/(\A|\s)([\w\.\-]+)(@)([\w\.-]+)([A-Za-z]{2,3})\b/i", "\\1<a href=\"javascript:top.main.popup('".$module['url']."send.php?mail_to=\\2\\3\\4\\5','".$GO_CONFIG->composer_width."','".$GO_CONFIG->composer_height."')\" class=\"blue\">\\2\\3\\4\\5</a>", $text);
1232          } else {
1233              $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);
1234          }
1235      }
1236      $text = nl2br($text);
1237      $text = str_replace("\r", "", $text);
1238      $text = str_replace("\n", "", $text);
1239  
1240      return ($text);
1241  
1242  }
1243  
1244  /**
1245  * Convert Dangerous HTML to safe HTML for display inside of Group-Office
1246  *
1247  * This also removes everything outside the body and replaces mailto links
1248  *
1249  * @param    string $text Plain text string
1250  * @access public
1251  * @return string HTML formatted string
1252  */
1253  function convert_html($html) {
1254  
1255      $html = str_replace("\r", '', $html);
1256      $html = str_replace("\n",' ', $html);
1257      
1258      $to_removed_array = array (
1259      "'<html>'si",
1260      "'</html>'si",
1261      "'<body[^>]*>'si",
1262      "'</body>'si",
1263      "'<head[^>]*>.*?</head>'si",
1264      "'<style[^>]*>.*?</style>'si",
1265      "'<script[^>]*>.*?</script>'si",
1266      "'<object[^>]*>.*?</object>'si",
1267      "'<embed[^>]*>.*?</embed>'si",
1268      "'<applet[^>]*>.*?</applet>'si",
1269      "'<form[^>]*>'si",
1270      "'<input[^>]*>'si",
1271      "'<select[^>]*>.*?</select>'si",
1272      "'<textarea[^>]*>.*?</textarea>'si",
1273      "'</form>'si"
1274      );
1275  
1276      $html = preg_replace($to_removed_array, '', $html);
1277      $html = preg_replace("/([\"']?)javascript:/i", "$1removed_script:", $html);
1278      
1279      return $html;
1280  }
1281  
1282  /**
1283  * Change HTML links to Group-Office links. For example mailto: links will call
1284  * the Group-Office e-mail module if installed.
1285  *
1286  *
1287  * @param    string $text Plain text string
1288  * @access public
1289  * @return string HTML formatted string
1290  */
1291  
1292  function convert_links($html)
1293  {
1294      global $GO_CONFIG, $GO_MODULES;
1295      
1296      $html = str_replace("\r", '', $html);
1297      $html = str_replace("\n",' ', $html);
1298      
1299      if ($GO_MODULES->modules['email'] && $GO_MODULES->modules['email']['read_permission']) {
1300          $html = preg_replace("/(href=([\"']?)mailto:)([\w\.\-]+)(@)([\w\.\-\"]+)\b/i", 
1301              "href=\"javascript:top.main.popup('".$GO_MODULES->modules['email'] ['url'].
1302              "send.php?mail_to=$3$4$5','".$GO_CONFIG->composer_width."','".
1303              $GO_CONFIG->composer_height."')\" class=\"blue\"", $html);
1304      }
1305      //$html = preg_replace("/\b((href=([\"']?)http(s?):\/\/))([\w\.\?\=\&]+)([\/\w+\.\?\=\&\:\~]+)([\"']?)/i",
1306      //    "target=$3_blank$3 class=$3blue$3 href=$3http$4://$5$6$7", $html);
1307  
1308      $regexp="/<a.+?href=([\"']?)(.+?)>/i";
1309      $html = preg_replace($regexp, "<a target=$1_blank$1 class=$1blue$1 href=$1$2$3>", $html);
1310  
1311      $regexp="/<a.+?href=([\"']?)".str_replace('/','\\/', $GO_CONFIG->full_url)."(.+?)>/i";
1312      $html = preg_replace($regexp, "<a target=$1main$1 class=$1blue$1 href=$1".$GO_CONFIG->host."$2$3>", $html);
1313      
1314      return $html;
1315  }
1316  
1317  /**
1318  * Get all e-mail addresses inside a string
1319  *
1320  * @param    string $addr The comma seperated string
1321  * @access public
1322  * @return array Array of e-mail addresses
1323  */
1324  function get_addresses_from_string($address_string) {
1325      $in_address = false;
1326      $address = '';
1327      $addresses = array ();
1328      $length = strlen($address_string);
1329      for ($i = 0; $i < $length; $i ++) {
1330          $char = $address_string[$i];
1331          if ($char == '>') {
1332              if ($in_address) {
1333                  $in_address = false;
1334                  $addresses[] = trim($address);
1335                  $address = '';
1336              }
1337          }
1338          elseif ($char == '<') {
1339              $in_address = true;
1340          }
1341          elseif ($in_address) {
1342              $address .= $char;
1343          }
1344      }
1345      return $addresses;
1346  }
1347  
1348  /**
1349  * Get CRLF based on OS (Deprecated
1350  *
1351  * @param    string $smtp
1352  * @access public
1353  * @return string CRLF
1354  */
1355  function get_crlf($smtp = "") {
1356      $crlf = stristr(PHP_OS, 'Windows') ? "\r\n" : "\n";
1357  
1358      if ($smtp != "")
1359          $crlf = $smtp ? "\r\n" : $crlf;
1360  
1361      //return ($crlf);
1362      return "\r\n";
1363  }
1364  
1365  /**
1366  * Unescapes a slashed string if magic_quotes_gpc is on
1367  *
1368  * @param    string $string
1369  * @access public
1370  * @return string Stripped of slashes
1371  */
1372  function smart_stripslashes($string) {
1373      if (get_magic_quotes_gpc()) {
1374          $string = stripslashes($string);
1375      }
1376      return $string;
1377  }
1378  
1379  /**
1380  * Escapes a string with slashes if magic_quotes_gpc is off
1381  *
1382  * @param    string $string
1383  * @access public
1384  * @return string Stripped of slashes
1385  */
1386  function smart_addslashes($string) {
1387      if (!get_magic_quotes_gpc()) {
1388          $string = addslashes($string);
1389      }
1390      return $string;
1391  }
1392  
1393  /**
1394  * Quotes a string with >
1395  *
1396  * @param    string $text
1397  * @access public
1398  * @return string A string quoted with >
1399  */
1400  function quote($text) {
1401      $text = "> ".ereg_replace("\n", "\n> ", trim($text));
1402      return ($text);
1403  }
1404  
1405  /**
1406  * Sends an e-mail using the global mail settings from Group-Office.php
1407  *
1408  * @param    string $email_to The e-mail reciepent
1409  * @param    string $email_from The e-mail sender
1410  * @param    string $subject The e-mail subject
1411  * @param    string $body The e-mail body
1412  * @param    string $priority The priotity of the e-mail message
1413  * @param    string $body_ctype The the mime type of the body
1414                   (text/PLAIN or text/HTML)
1415  * @access public
1416  * @return bool Returns true if the message was successfully sent
1417  */
1418  function sendmail($email_to, $email_from, $name_from, $subject, $body, $priority = '3', $body_ctype = 'text/PLAIN') {
1419      global $GO_CONFIG, $GO_MODULES, $GO_SECURITY, $charset, $php_mailer_lang;
1420  
1421      require_once ($GO_CONFIG->class_path."mail/phpmailer/class.phpmailer.php");
1422      require_once ($GO_CONFIG->class_path."mail/phpmailer/class.smtp.php");
1423      require_once ($GO_CONFIG->class_path."html2text.class.inc");
1424  
1425      $mail = new PHPMailer();
1426      $mail->PluginDir = $GO_CONFIG->class_path.'mail/phpmailer/';
1427      $mail->SetLanguage($php_mailer_lang, $GO_CONFIG->class_path.'mail/phpmailer/language/');
1428  
1429      switch ($GO_CONFIG->mailer) {
1430          case 'smtp' :
1431              $mail->Host = $GO_CONFIG->smtp_server;
1432              $mail->Port = $GO_CONFIG->smtp_port;
1433              
1434              if(!empty($GO_CONFIG->smtp_username))
1435              {
1436                  $mail->SMTPAuth=true;
1437                  $mail->Username=$GO_CONFIG->smtp_username;            
1438                  $mail->Password = $GO_CONFIG->smtp_password;
1439              }
1440              
1441              $mail->IsSMTP();
1442              break;
1443          case 'qmail' :
1444              $mail->IsQmail();
1445              break;
1446          case 'sendmail' :
1447              $mail->IsSendmail();
1448              break;
1449          case 'mail' :
1450              $mail->IsMail();
1451              break;
1452      }
1453      $mail->Priority = $priority;
1454      $mail->Sender = $email_from;
1455      $mail->From = $email_from;
1456      $mail->FromName = $name_from;
1457      $mail->AddReplyTo($email_from, $name_from);
1458      $mail->WordWrap = 76;
1459      $html_message = strtolower($body_ctype) == 'text/html' ? true : false;
1460      $mail->IsHTML($html_message);
1461      $mail->Subject = smart_stripslashes(trim($subject));
1462  
1463      require_once ($GO_CONFIG->class_path.'mail/RFC822.class.inc');
1464      $RFC822 = new RFC822();
1465  
1466      $mail_to_array = $RFC822->parse_address_list($email_to);
1467  
1468      foreach ($mail_to_array as $to_address) {
1469          $mail->AddAddress($to_address['email'], $to_address['personal']);
1470      }
1471      
1472      if(isset($GO_MODULES->modules['email']) && $GO_MODULES->modules['email']['read_permission'])
1473      {
1474          require_once($GO_MODULES->modules['email']['class_path'].'email.class.inc');
1475          $email= new email();
1476          $em_settings = $email->get_settings($GO_SECURITY->user_id);
1477          $mail_charset = $em_settings['charset'];
1478      }else
1479      {
1480          $mail_charset = $charset;
1481      }
1482      
1483      if(function_exists('iconv') && $mail_charset != $charset)
1484      {
1485          $mail->CharSet=$mail_charset;
1486      }
1487  
1488      if ($html_message) {
1489          if($mail->CharSet!=$charset)
1490          {
1491              $html_mail_head = str_replace($charset, $mail->CharSet, $html_mail_head);
1492          }    
1493          $mail->Body = smart_stripslashes($body);
1494          $h2t = & new html2text($body);
1495          $mail->AltBody = $h2t->get_text();
1496  
1497      } else {
1498          $mail->Body = smart_stripslashes($body);
1499      }
1500  
1501      if($mail->CharSet!=$charset)
1502      {
1503          $mail->recode($charset);
1504      }
1505  
1506      return $mail->Send();
1507  }
1508  
1509  /**
1510  * returns a link poping up a open file dialog
1511  *
1512  * A handler file needs to be created to do something with the file to open
1513  *
1514  * @param    string $GO_HANDLER The handler file to submit to
1515  * @param    string $GO_FILTER Filter certain file types to display
1516  * @param    string $link_text The text to display in the link
1517  * @param    string $class CSS class file to use in the link
1518  * @param    bool $full_link Return a full link or retrun only an URL.
1519  * @access public
1520  * @return string URL or link
1521  */
1522  function show_open($GO_HANDLER, $GO_FILTER, $link_text = "", $class = "normal", $fulllink = true) {
1523      global $GO_CONFIG, $GO_MODULES;
1524  
1525      $module = $GO_MODULES->modules['filesystem'];
1526  
1527      if ($fulllink) {
1528          $link = "<a class=\"".$class."\" href=\"javascript:popup('".$module['url']."index.php?GO_HANDLER=".urlencode($GO_HANDLER)."&GO_FILTER=".$GO_FILTER."&mode=popup','600','400')\">".$link_text."</a>";
1529      } else {
1530          $link = "javascript:popup('".$module['url']."index.php?GO_HANDLER=".urlencode($GO_HANDLER)."&GO_FILTER=".$GO_FILTER."&mode=popup','600','400')";
1531      }
1532      return $link;
1533  }
1534  
1535  /**
1536  * returns a link poping up a save file dialog
1537  *
1538  * A temporarely file to save must be passed
1539  *
1540  * @param    string $tmp_file The temporary file location
1541  * @param    string $link_text The text to display in the link
1542  * @param    string $class CSS class file to use in the link
1543  * @param    bool $full_link Return a full link or retrun only an URL.
1544  * @access public
1545  * @return string URL or link
1546  */
1547  
1548  function show_save($tmpfile, $link_text = "", $class = "normal", $fulllink = true) {
1549      global $GO_CONFIG, $GO_MODULES;
1550  
1551      $module = $GO_MODULES->modules['filesystem'];
1552  
1553      if ($fulllink) {
1554          $link = "<a class=\"".$class."\" href=\"javascript:popup('".$module['url']."save.php?SET_HANDLER=".urlencode($tmpfile)."','600','400')\">".$link_text."</a>";
1555      } else {
1556          $link = "javascript:popup('".$module['url']."save.php?GO_HANDLER=".urlencode($tmpfile)."','400','450')";
1557      }
1558      return $link;
1559  }
1560  
1561  /**
1562  * Return a filename without extension
1563  *
1564  * @param    string $filename The complete filename
1565  * @access public
1566  * @return string A filename without the extension
1567  */
1568  function strip_extension($filename) {
1569      $pos = strrpos($filename, '.');
1570      if ($pos) {
1571          $filename = substr($filename, 0, $pos);
1572      }
1573      return $filename;
1574  }
1575  
1576  /**
1577  * Returns the extension of a filename
1578  *
1579  * @param    string $filename The complete filename
1580  * @access public
1581  * @return string  The extension of a filename
1582  */
1583  function get_extension($filename) {
1584      $extension = '';
1585      $pos = strrpos($filename, '.');
1586      if ($pos) {
1587          $extension = substr($filename, $pos +1, strlen($filename));
1588      }
1589      return strtolower($extension);
1590  }
1591  
1592  /**
1593  * Returns an array with RGB color (e.g. R128 G126 B124) from a hex html color code (e.g. #F7F7F7)
1594  *
1595  * @param  string $html_color HTML color code: e.g. #FFFFFF
1596  * @access public
1597  * @return array RGB color code: e.g. R:102 G:102 B:102
1598  */
1599  function hex2dec($html_color) {
1600      $R = hexdec(substr($html_color, 1, 2));
1601      $G = hexdec(substr($html_color, 3, 2));
1602      $B = hexdec(substr($html_color, 5, 2));
1603      $color = array ();
1604      $color['R'] = $R;
1605      $color['G'] = $G;
1606      $color['B'] = $B;
1607      return $color;
1608  }
1609  
1610  
1611  function mime_content_type_by_extension($extension) {
1612      global $GO_CONFIG;
1613      $file = file($GO_CONFIG->mime_types_file);
1614      foreach ($file as $line) {
1615          rtrim($line);
1616          if (preg_match('/^\#/', $line))
1617              continue;
1618          $elms = preg_split('/\s+/', $line);
1619          $mime = array_shift($elms);
1620          foreach ($elms as $elm) {
1621              if ($elm == $extension) {
1622                  return $mime;
1623              }
1624          }
1625      }
1626      return 'application/OCTET-STREAM';
1627  }
1628  
1629  function enc_utf8($str) {
1630      //some mail clients create encoded strings such: =?iso-8859-1?Q? "Andr=E9=20Mc=20Intyre" ?=
1631      //containing space values inside, but they mustn't. The space values have to be removed before
1632      //they are going to be converted to utf8.
1633      
1634      if (preg_match("/=\?/", $str)) {
1635          $str = str_replace(" ", "", $str);
1636      }
1637      return imap_utf8($str);
1638  /*
1639      $text = '';
1640      if($elements = imap_mime_header_decode($str))
1641      {
1642          foreach($elements as $element)
1643          {
1644              $text .= $element->text;
1645          }
1646          return utf8_encode($text);
1647      }    */
1648  }
1649  /**
1650   * Formats a name in Group-Office
1651   * 
1652   * @param string $sort_name string Vlaue can be last_name or first_name
1653   * @return string base64 encoded string
1654   */
1655  function format_name($last, $first = '', $middle = '', $sort_name='') {
1656      
1657      $sort_name = $sort_name == '' ? $_SESSION['GO_SESSION']['sort_name'] : $sort_name;
1658      
1659      if ($sort_name== 'last_name') {
1660          $name =     !empty ($last) ? $last : '';
1661          if(!empty($last) && !empty($first))
1662          {
1663              $name .= ', ';
1664          }        
1665          $name .= !empty ($first) ? $first : '';
1666          $name .= !empty ($middle) ? ' '.$middle : '';
1667      } else {
1668          $name = !empty ($first) ? $first : ' ';
1669          $name .= !empty ($middle) ? ' '.$middle.' ' : ' ';
1670          $name .= $last;
1671      }
1672  
1673      return $name;
1674  }
1675  
1676  /**
1677   * Converts string from base64
1678   * 
1679   * @param string $s string
1680   * @return string base64 encoded string
1681   */
1682  function decodeBASE64($string) {
1683      $B64Values = array(
1684              'A' =>  0, 'B' =>  1, 'C' =>  2, 'D' =>  3, 'E' =>  4, 'F' =>  5,
1685              'G' =>  6, 'H' =>  7, 'I' =>  8, 'J' =>  9, 'K' => 10, 'L' => 11,
1686              'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
1687              'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
1688              'Y' => 24, 'Z' => 25,
1689              'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
1690              'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
1691              'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
1692              's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
1693              'y' => 50, 'z' => 51,
1694              '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
1695              '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
1696              );
1697      $p = 0;
1698      $d = '';
1699      $unicodeNullByteToggle = 0;
1700      for ($i = 0, $len = strlen($string); $i < $len; $i++) {
1701          $c = $string[$i];
1702          if ($p == 0) {
1703              $t = $B64Values[$c];
1704              $p = 1;
1705          } elseif ($p == 1) {
1706              if ($unicodeNullByteToggle) {
1707                  $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
1708                  $unicodeNullByteToggle = 0;
1709              } else {
1710                  $unicodeNullByteToggle = 1;
1711              }
1712              $t = ($B64Values[$c] & 15);
1713              $p = 2;
1714          } elseif ($p == 2) {
1715              if ($unicodeNullByteToggle) {
1716                  $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
1717                  $unicodeNullByteToggle = 0;
1718              } else {
1719                  $unicodeNullByteToggle = 1;
1720              }
1721              $t = ($B64Values[$c] & 3);
1722              $p = 3;
1723          } elseif ($p == 3) {
1724              if ($unicodeNullByteToggle) {
1725                  $d = $d . chr(($t << 6) + $B64Values[$c]);
1726                  $unicodeNullByteToggle = 0;
1727              } else {
1728                  $unicodeNullByteToggle = 1;
1729              }
1730              $t = ($B64Values[$c] & 3);
1731              $p = 0;
1732          }
1733      }
1734      return $d;
1735  }
1736  /**
1737   * Converts string to base64
1738   * @param string $s string
1739   * @return string base64 encoded string
1740   */
1741  function encodeBASE64($string) {
1742      $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
1743      $p = 0;     // phase: 1 / 2 / 3 / 1 / 2 / 3...
1744      $e = '';    // base64-encoded string
1745      //foreach($s as $c) {
1746      for ($i = 0; $i < strlen($string); $i++) {
1747          $c = $string[$i];
1748          if ($p == 0) {
1749              $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
1750              $t = (ord($c) & 3);
1751              $p = 1;
1752          } elseif ($p == 1) {
1753              $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
1754              $t = (ord($c) & 15);
1755              $p = 2;
1756          } elseif ($p == 2) {
1757              $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
1758              $e = $e . $B64Chars[ord($c) & 63];
1759              $p = 0;
1760          }
1761      }
1762      //
1763      // flush buffer
1764      //
1765      if ($p == 1) {
1766          $e = $e . $B64Chars[$t << 4];
1767      } elseif ($p == 2) {
1768          $e = $e . $B64Chars[$t << 2];
1769      }
1770      return $e;
1771  }
1772  
1773  	function encode_imap_path($str, $delimiter='.')
1774      {
1775          $encoded = array();
1776          $mailboxes = explode($delimiter, $str);
1777          foreach($mailboxes as $mailbox)
1778          {
1779              $encoded[]=$this->utf7_encode($mailbox);
1780          }    
1781          return implode($delimiter, $encoded);
1782      }
1783      
1784  	function decode_imap_path($str, $delimiter='.')
1785      {
1786          $decoded = array();
1787          $mailboxes = explode($delimiter, $str);
1788          foreach($mailboxes as $mailbox)
1789          {
1790              $decoded[]=$this->utf7_decode($mailbox);
1791          }    
1792          return implode($delimiter, $decoded);
1793      }
1794  
1795  	function utf7_imap_encode($input)
1796      {
1797          global $charset;
1798          if (extension_loaded('iconv')) {
1799              if($converted_input = iconv($charset, 'UTF-7', $input))
1800              {        
1801                  $input=preg_replace("/&/","&-",$converted_input);            
1802                  $input=preg_replace("/\+A/","&A",$input);            
1803                  $input=preg_replace("/&ACY /","&- ",$input);
1804                  $input=preg_replace("/&ACY/","&",$input);
1805                  $input=preg_replace("/&AF8(-)?/","_",$input);
1806                  $input=preg_replace("/&ADs(-)?/",";",$input);
1807            return $input;
1808          }
1809          }
1810          return @imap_utf7_encode(utf8_decode($input));
1811      }
1812      
1813  
1814      
1815  	function utf7_imap_decode($input)
1816      {
1817          global $charset;
1818  
1819          if (extension_loaded('iconv')) {
1820              $input=preg_replace("/&A/","+A",$input);
1821              $input=preg_replace("/&-/","&",$input);
1822              
1823              if($converted_input = iconv('UTF-7', $charset, $input))
1824              {
1825                  return $converted_input;
1826              }
1827          }
1828          return utf8_encode(@imap_utf7_decode($input));    
1829      }
1830  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]