[903] | 1 | <?php |
---|
| 2 | class Functions{ |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | function CallVoipConnect($params){ |
---|
| 6 | // se for um ramal. |
---|
| 7 | |
---|
| 8 | if($params['typePhone'] == 'ramal'){ |
---|
| 9 | isset($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number']) ? $fromNumber = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number']: $fromNumber = $_SESSION['phpgw_info']['user']['preferences']['contactcenter']['telephone_number'] ; |
---|
| 10 | //return ":".$this->verifica_sip().":".$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['SIP']; |
---|
| 11 | if ($fromNumber == $params['to']){ |
---|
| 12 | return $this->getLang("You can't call yourself"); |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | if (preg_match('/^\([0-9]{2}\)[0-9]{4}\-[0-9]{4}$/',$fromNumber) == 0){ |
---|
| 16 | return $this->getLang("you extension number is incorect"); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | if (preg_match('/^\([0-9]{2}\)[0-9]{4}\-[0-9]{4}$/',$params['to']) == 0){ |
---|
| 20 | return $this->getLang("The extension ".$params['to']." is incorrect."); |
---|
| 21 | } |
---|
| 22 | if ($this->verifica_sip()){ |
---|
| 23 | $tipo="sip"; |
---|
| 24 | $fromNumber = substr($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number'],1,2).$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['SIP']; |
---|
| 25 | }else{ |
---|
| 26 | $tipo="ramal"; |
---|
| 27 | $fromNumber = substr($fromNumber,1,2).substr($fromNumber,strlen($fromNumber) - 4, strlen($fromNumber) - 1); |
---|
| 28 | } |
---|
| 29 | $toNumber = $params['to'] ? substr($params['to'],1,2).substr($params['to'],strlen($params['to']) - 4, strlen($params['to']) - 1) : 0; |
---|
| 30 | //return $fromNumber.$toNumber; |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | //se convencional |
---|
| 34 | if($params['typePhone'] == 'com'){ |
---|
| 35 | $fromNumber = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number']; |
---|
| 36 | if(!$fromNumber) |
---|
| 37 | return false; |
---|
| 38 | $fromNumber = substr($fromNumber,strlen($fromNumber) - 4, strlen($fromNumber) - 1); |
---|
| 39 | $toNumber = $params['to'] ? substr($params['to'],strlen($params['to']) - 4, strlen($params['to']) - 1) : 0; |
---|
| 40 | |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | // Se for celular, passa os digitos do nï¿œmero e conecta com outro VoIP Server..... |
---|
| 44 | if($params['typePhone'] == 'mob'){ |
---|
| 45 | $toNumber = $params['to']; |
---|
| 46 | |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | $url = "&acao=liga&tipo=".$tipo."&ramal=".$fromNumber."&numero=".$toNumber; |
---|
| 51 | |
---|
| 52 | $resp = $this->connectVoip($url); |
---|
| 53 | |
---|
| 54 | return $this->getLang("Calling extension: ").$resp; |
---|
| 55 | } |
---|
| 56 | function set_followme($params){ |
---|
| 57 | $ramal = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number']; |
---|
| 58 | $ramal = substr($ramal,1,2).substr($ramal,strlen($ramal) - 4, strlen($ramal) - 1); |
---|
| 59 | $url="&acao=set_followme&ramal=".$ramal.'&siga='.$params['follow']; |
---|
| 60 | $resp = $this->connectVoip($url); |
---|
| 61 | return $resp; |
---|
| 62 | } |
---|
| 63 | function get_followme($params){ |
---|
| 64 | $ramal = $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number']; |
---|
| 65 | $ramal = substr($ramal,1,2).substr($ramal,strlen($ramal) - 4, strlen($ramal) - 1); |
---|
| 66 | $url="&acao=get_followme&ramal=".$ramal; |
---|
| 67 | $resp = $this->connectVoip($url); |
---|
| 68 | if(trim($resp) == "FALSE") |
---|
| 69 | return FALSE; |
---|
| 70 | $telefones = unserialize($resp); |
---|
| 71 | |
---|
| 72 | foreach($telefones as $tel) |
---|
| 73 | { |
---|
| 74 | $Ultimosquatro = substr($tel,-4); |
---|
| 75 | if($Ultimosquatro == substr($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number'],-4)) |
---|
| 76 | $resposta['exten'] = $tel; |
---|
| 77 | elseif($Ultimosquatro == substr($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['mobile'],-4)) |
---|
| 78 | $resposta['mobile'] = $tel; |
---|
| 79 | elseif($tel == $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['SIP']) |
---|
| 80 | $resposta['sip'] = $tel; |
---|
| 81 | else |
---|
| 82 | $resposta['other'] = $tel; |
---|
| 83 | } |
---|
| 84 | return $resposta; |
---|
| 85 | |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | function verifica_sip(){ |
---|
| 89 | if(isset($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['SIP'])){ |
---|
| 90 | $ramal = substr($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['telephone_number'],1,2).$_SESSION['phpgw_info']['user']['preferences']['expressoMail']['SIP']; |
---|
| 91 | $url="&acao=verifica_sip&ramal=".$ramal; |
---|
| 92 | $resp = $this->connectVoip($url); |
---|
| 93 | if(trim($resp) == "true") |
---|
| 94 | return TRUE; |
---|
| 95 | } |
---|
| 96 | return FALSE; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | function connectVoip($param){ |
---|
| 100 | |
---|
| 101 | $voipServer= $_SESSION['phpgw_info']['expressomail']['server']['voip_server']; |
---|
| 102 | $voipUrl = $_SESSION['phpgw_info']['expressomail']['server']['voip_url']; |
---|
| 103 | $voipPort = $_SESSION['phpgw_info']['expressomail']['server']['voip_port']; |
---|
| 104 | if(!$voipServer || !$voipUrl || !$voipPort){ |
---|
| 105 | return false;} |
---|
| 106 | $url = "http://".$voipServer.":".$voipPort.$voipUrl."?magic=1333&".$param; |
---|
| 107 | $sMethod = 'GET '; |
---|
| 108 | $crlf = "\r\n"; |
---|
| 109 | $sRequest = " HTTP/1.1" . $crlf; |
---|
| 110 | $sRequest .= "Host: localhost" . $crlf; |
---|
| 111 | $sRequest .= "Accept: */* " . $crlf; |
---|
| 112 | $sRequest .= "Connection: Close" . $crlf . $crlf; |
---|
| 113 | $sRequest = $sMethod . $url . $sRequest; |
---|
| 114 | $sockHttp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
---|
| 115 | if (!$sockHttp) { |
---|
| 116 | return "socket_create falhou"; |
---|
| 117 | } |
---|
| 118 | $resSocketConnect = socket_connect($sockHttp, $voipServer, $voipPort); |
---|
| 119 | if (!$resSocketConnect) { |
---|
| 120 | return "socket_connect falhou"; |
---|
| 121 | } |
---|
| 122 | $resSocketWrite = socket_write($sockHttp, $sRequest, strlen($sRequest)); |
---|
| 123 | if (!$resSocketWrite) { |
---|
| 124 | return "socket_write falhou"; |
---|
| 125 | } |
---|
| 126 | $sResponse = ''; |
---|
| 127 | while ($sRead = socket_read($sockHttp, 512)) { |
---|
| 128 | $sResponse .= $sRead; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | socket_close($qsockHttp); |
---|
| 132 | $pos = strpos($sResponse, $crlf . $crlf); |
---|
| 133 | $resp = substr($sResponse, $pos + 2 * strlen($crlf)); |
---|
| 134 | return $resp; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | function getDirContents($dir){ |
---|
| 138 | //$dir = dirname($dir); |
---|
| 139 | |
---|
| 140 | if (!is_dir($dir)) { |
---|
| 141 | return 'Error: Cannot load files in :'. $dir.' !'; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | if ($root=@opendir($dir)) { |
---|
| 145 | |
---|
| 146 | while ($file=readdir($root)) { |
---|
| 147 | if($file!="." && $file!="..") |
---|
| 148 | $files[]=$dir."/".$file; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | return $files; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | function getFilesJs($includeFiles = '', $update_version = ''){ |
---|
| 157 | |
---|
| 158 | $files = $this -> getDirContents('js'); |
---|
| 159 | $str_files = ''; |
---|
| 160 | |
---|
| 161 | if($includeFiles) { |
---|
| 162 | $includeFiles = explode(",",trim($includeFiles)); |
---|
| 163 | // Bug fixed for array_search function |
---|
| 164 | $includeFiles[count($includeFiles)] = $includeFiles[0]; |
---|
| 165 | $includeFiles[0] = null; |
---|
| 166 | // End Bug fixed. |
---|
| 167 | } |
---|
| 168 | for($i = 0; $i < count($files); $i++) { |
---|
| 169 | if(count(explode('.js',$files[$i])) > 1) { |
---|
| 170 | if($includeFiles && array_search(trim($files[$i]),$includeFiles)){ |
---|
| 171 | $str_files .= "<script src='".$files[$i]."?".$update_version."' type='text/javascript'></script>"; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | return $str_files; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | function getReturnExecuteForm(){ |
---|
| 180 | $response = $_SESSION['response']; |
---|
| 181 | $_SESSION['response'] = null; |
---|
| 182 | return $response; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | function getLang($key){ |
---|
| 186 | if ($_SESSION['phpgw_info']['expressomail']['lang'][$key]) |
---|
| 187 | return $_SESSION['phpgw_info']['expressomail']['lang'][$key]; |
---|
| 188 | else |
---|
| 189 | return ($key . '*'); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | function get_preferences() { |
---|
| 193 | return $_SESSION['phpgw_info']['user']['preferences']['expressoMail']; |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | ?> |
---|