source: trunk/expressoMail1_2/inc/class.functions.inc.php @ 7673

Revision 7673, 7.3 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
RevLine 
[2]1<?php
[5509]2                /***************************************************************************
3                * Expresso Livre                                                           *
4                * http://www.expressolivre.org                                             *
5                * --------------------------------------------                             *
6                *  This program is free software; you can redistribute it and/or modify it *
7                *  under the terms of the GNU General Public License as published by the   *
8                *  Free Software Foundation; either version 2 of the License, or (at your  *
9                *  option) any later version.                                              *
10                \**************************************************************************/
11               
[2]12        class Functions{
[3493]13
14                // get the offset against GMT.
15                function CalculateDateOffset()
16                {
17
[6714]18                   
19                    $zones = $this->getTimezones();
[6848]20                    $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['timezone'] = isset($GLOBALS['phpgw_info']['user']['preferences']['expressoMail']['timezone']) ? $GLOBALS['phpgw_info']['user']['preferences']['expressoMail']['timezone'] : sprintf("%s", array_search("America/Sao_Paulo", $zones));
[6714]21
[3493]22                    $timezone_index = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['timezone'];
23                    $user_timezone = $zones[$timezone_index];
[6714]24
[3493]25                    $tz = new DateTimeZone($user_timezone);
26                    $gmt = new DateTimeZone("Etc/GMT+0");
27                    $gmttime = new DateTime("now", $gmt);
28                    $offset = $tz->getOffset($gmttime);
29                    return $offset;
30                }
31
32                function getTimezones()
33                {
34                    $zones = timezone_identifiers_list();
35                    $friendly_zones = array();
36                    foreach ($zones as $zone)
37                    {
38                        if (preg_match("/^(Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)\/.*$/", $zone))
39                        {
[7500]40                           $friendly_zones[$zone] = $zone;// array_push($friendly_zones,  $zone);
[3493]41                        }
42
43                    }
44                    return $friendly_zones;
45                }
46
[72]47                function CallVoipConnect($params){
48                        $fromNumber = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number'];
49                        if(!$fromNumber)
50                                return false;
51                        $fromNumber = substr($fromNumber,strlen($fromNumber) - 4, strlen($fromNumber) - 1);
52                        $toNumber       = $params['to'] ? substr($params['to'],strlen($params['to']) - 4, strlen($params['to']) - 1) : 0;
53                       
54                        $voipServer     = $_SESSION['phpgw_info']['expressomail']['server']['voip_server'];
55                        $voipUrl        = $_SESSION['phpgw_info']['expressomail']['server']['voip_url'];
56                        $voipPort       = $_SESSION['phpgw_info']['expressomail']['server']['voip_port'];
57                       
58                        // Se for celular, passa os digitos do número e conecta com outro VoIP Server.....
59                        if($params['typePhone'] == 'mob'){
60                                $toNumber       = $params['to'];
61                                $voipServer     = $_SESSION['phpgw_info']['expressomail']['server']['voip_server'];
62                                $voipUrl        = $_SESSION['phpgw_info']['expressomail']['server']['voip_url'];
63                                $voipPort       = $_SESSION['phpgw_info']['expressomail']['server']['voip_port'];                               
64                        }       
65                               
66                        if(!$voipServer || !$voipUrl || !$voipPort)
67                                return false;
68                        $url            = "http://".$voipServer.":".$voipPort.$voipUrl."?magic=1333&acao=liga&ramal=".$fromNumber."&numero=".$toNumber;                 
69                        $sMethod = 'GET ';
70            $crlf = "\r\n";
71            $sRequest = " HTTP/1.1" . $crlf;
72            $sRequest .= "Host: localhost" . $crlf;
73            $sRequest .= "Accept: */* " . $crlf;
74            $sRequest .= "Connection: Close" . $crlf . $crlf;           
75            $sRequest = $sMethod . $url . $sRequest;   
76            $sockHttp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);           
77            if (!$sockHttp)  {
78                return false;
79            }
80            $resSocketConnect = socket_connect($sockHttp, $voipServer, $voipPort);
81            if (!$resSocketConnect) {
82                return false;
83            }
84            $resSocketWrite = socket_write($sockHttp, $sRequest, strlen($sRequest));
85            if (!$resSocketWrite) {
86                return false;
87            }   
88            $sResponse = '';   
89            while ($sRead = socket_read($sockHttp, 512)) {
90                $sResponse .= $sRead;
91            }           
92           
93            socket_close($sockHttp);           
94            $pos = strpos($sResponse, $crlf . $crlf);
95            return substr($sResponse, $pos + 2 * strlen($crlf));                                                                       
96                }
97
[2]98                function getDirContents($dir){
99                        //$dir = dirname($dir);
100       
101                        if (!is_dir($dir))      {
102                                        return 'Error: Cannot load files in :'. $dir.' !';
103                        }
104                       
105                        if ($root=@opendir($dir))       {
106                       
107                       while ($file=readdir($root))     {                 
108                           if($file!="." && $file!="..")
109                                   $files[]=$dir."/".$file;           
110                       }
111                       
112                        }
113                       
114                   return $files;
115                }
116                               
[56]117                function getFilesJs($includeFiles = '', $update_version = ''){
[2]118                       
119                        $files = $this -> getDirContents('js');
120                        $str_files = '';
121                       
[56]122                        if($includeFiles) {
123                                $includeFiles = explode(",",trim($includeFiles));
[2]124                                // Bug fixed for array_search function
[56]125                                $includeFiles[count($includeFiles)] = $includeFiles[0];
126                                $includeFiles[0] = null;
127                                // End Bug fixed.
[7673]128                        }
129            $files_count = count($files);
130                        for($i = 0; $i < $files_count; ++$i) {
[2]131                                if(count(explode('.js',$files[$i])) > 1) {
[56]132                                        if($includeFiles  && array_search(trim($files[$i]),$includeFiles)){     
[2]133                                                $str_files .= "<script src='".$files[$i]."?".$update_version."' type='text/javascript'></script>";                                             
134                                        }
135                                }
136                        }
137                       
138                        return $str_files;
139                }                                                               
140
141                function getReturnExecuteForm(){
142                        $response = $_SESSION['response'];
143                        $_SESSION['response'] = null;
144                        return $response;
145                }
[1526]146                function getLang($key){
147                        if ($_SESSION['phpgw_info']['expressomail']['lang'][$key])
[1572]148                                return $_SESSION['phpgw_info']['expressomail']['lang'][$key];                   
149                        else
150                                return ($key . '*');
[2]151                }
[51]152
153                function get_preferences()      {
154                        return $_SESSION['phpgw_info']['user']['preferences']['expressoMail'];
155                }
[1384]156                // Unicode Conversor: convert everything from UTF-8 into an NCR[Numeric Character Reference]
157                function utf8_to_ncr($content)  {
158            $result = "";           
159            while ($strlen = mb_strlen($content)) {
160                $c = mb_substr($content, 0, 1, "UTF-8");
161                $h = ord($c{0});   
162                    if ($h <= 0x7F || $h < 0xC2) {
[3018]163                            // fixing curly brackets
164                            if($h == 0x7B || $h == 0x7D)
165                                    $result .= "&#" . $h . ";";
166                            else
167                                    $result .= $c;
168                    }
[1384]169                        else if ($h <= 0xDF) {
170                        $h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
171                        $result .= "&#" . $h . ";";
172                    } else if ($h <= 0xEF) {
173                        $h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
174                        $result .= "&#" . $h . ";";
175                    } else if ($h <= 0xF4) {
176                        $h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
177                        $result .= "&#" . $h . ";";
178                    }               
179                $content = mb_substr( $content, 1, $strlen, "UTF-8");
180            }
181            return mb_convert_encoding($result,"UTF-8");
182        }
183    }   
[1037]184?>
Note: See TracBrowser for help on using the repository browser.