dolibarr 20.0.0
wrapper.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2009-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
30if (!defined('NOREQUIRESOC')) {
31 define('NOREQUIRESOC', '1');
32}
33if (!defined('NOREQUIRETRAN')) {
34 define('NOREQUIRETRAN', '1');
35}
36if (!defined('NOTOKENRENEWAL')) {
37 define('NOTOKENRENEWAL', '1');
38}
39if (!defined('NOREQUIREMENU')) {
40 define('NOREQUIREMENU', '1');
41}
42if (!defined('NOREQUIREHTML')) {
43 define('NOREQUIREHTML', '1');
44}
45if (!defined('NOREQUIREAJAX')) {
46 define('NOREQUIREAJAX', '1');
47}
48
55function llxHeader()
56{
57 print '<html>'."\n";
58 print '<head>'."\n";
59 print '<title>Asterisk redirection from Dolibarr...</title>'."\n";
60 print '</head>'."\n";
61}
62
69function llxFooter()
70{
71 print "\n".'</html>'."\n";
72}
73
74require_once '../main.inc.php';
75require_once DOL_DOCUMENT_ROOT.'/core/lib/functions.lib.php';
76require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
77
78
79// Security check
80if (!isModEnabled('clicktodial')) {
82 exit;
83}
84
85
86// Define Asterisk setup
87if (!getDolGlobalString('ASTERISK_HOST')) {
88 $conf->global->ASTERISK_HOST = "127.0.0.1";
89}
90if (!getDolGlobalString('ASTERISK_TYPE')) {
91 $conf->global->ASTERISK_TYPE = "SIP/";
92}
93if (!getDolGlobalString('ASTERISK_INDICATIF')) {
94 $conf->global->ASTERISK_INDICATIF = "0";
95}
96if (!getDolGlobalString('ASTERISK_PORT')) {
97 $conf->global->ASTERISK_PORT = 5038;
98}
99if (getDolGlobalString('ASTERISK_INDICATIF') == 'NONE') {
100 $conf->global->ASTERISK_INDICATIF = '';
101}
102if (!getDolGlobalString('ASTERISK_CONTEXT')) {
103 $conf->global->ASTERISK_CONTEXT = "from-internal";
104}
105if (!getDolGlobalString('ASTERISK_WAIT_TIME')) {
106 $conf->global->ASTERISK_WAIT_TIME = "30";
107}
108if (!getDolGlobalString('ASTERISK_PRIORITY')) {
109 $conf->global->ASTERISK_PRIORITY = "1";
110}
111if (!getDolGlobalString('ASTERISK_MAX_RETRY')) {
112 $conf->global->ASTERISK_MAX_RETRY = "2";
113}
114
115
116$login = GETPOST('login', 'alphanohtml');
117$password = GETPOST('password', 'none');
118$caller = GETPOST('caller', 'alphanohtml');
119$called = GETPOST('called', 'alphanohtml');
120
121// Sanitize input data to avoid to use the wrapper to inject malicious paylod into asterisk
122$login = preg_replace('/[\n\r]/', '', $login);
123$password = preg_replace('/[\n\r]/', '', $password);
124$caller = preg_replace('/[\n\r]/', '', $caller);
125$called = preg_replace('/[\n\r]/', '', $called);
126
127// IP address of Asterisk server
128$strHost = getDolGlobalString('ASTERISK_HOST');
129
130// Specify the type of extension through which your extension is connected.
131// ex: SIP/, IAX2/, ZAP/, etc
132$channel = getDolGlobalString('ASTERISK_TYPE');
133
134// Outgoing call sign
135$prefix = getDolGlobalString('ASTERISK_INDICATIF');
136
137// Asterisk Port
138$port = getDolGlobalString('ASTERISK_PORT');
139
140// Context ( generalement from-internal )
141$strContext = getDolGlobalString('ASTERISK_CONTEXT');
142
143// Waiting time before hanging up
144$strWaitTime = getDolGlobalString('ASTERISK_WAIT_TIME');
145
146// Priority
147$strPriority = getDolGlobalString('ASTERISK_PRIORITY');
148
149// Number of call attempts
150$strMaxRetry = getDolGlobalString('ASTERISK_MAX_RETRY');
151
152
153/*
154 * View
155 */
156
157llxHeader();
158
159$sql = "SELECT s.nom as name FROM ".MAIN_DB_PREFIX."societe as s";
160$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."socpeople as sp ON sp.fk_soc = s.rowid";
161$sql .= " WHERE s.entity IN (".getEntity('societe').")";
162$sql .= " AND (s.phone='".$db->escape($called)."'";
163$sql .= " OR sp.phone='".$db->escape($called)."'";
164$sql .= " OR sp.phone_perso='".$db->escape($called)."'";
165$sql .= " OR sp.phone_mobile='".$db->escape($called)."')";
166$sql .= $db->plimit(1);
167
168dol_syslog('click to dial search information with phone '.$called, LOG_DEBUG);
169$resql = $db->query($sql);
170if ($resql) {
171 $obj = $db->fetch_object($resql);
172 if ($obj) {
173 $found = $obj->name;
174 } else {
175 $found = 'Not found';
176 }
177 $db->free($resql);
178} else {
179 dol_print_error($db, 'Error');
180 $found = 'Error';
181}
182
183$number = strtolower($called);
184$pos = strpos($number, "local");
185if (!empty($number)) {
186 if ($pos === false) {
187 $errno = 0;
188 $errstr = 0;
189 $strCallerId = "Dolibarr caller $found <".strtolower($number).">";
190 $oSocket = @fsockopen($strHost, (int) $port, $errno, $errstr, 10);
191 if (!$oSocket) {
192 print '<body>'."\n";
193 $txt = "Failed to execute fsockopen($strHost, $port, \$errno, \$errstr, 10)<br>\n";
194 print $txt;
195 dol_syslog($txt, LOG_ERR);
196 $txt = $errstr." (".$errno.")<br>\n";
197 print $txt;
198 dol_syslog($txt, LOG_ERR);
199 print '</body>'."\n";
200 } else {
201 $txt = "Call Asterisk dialer for caller: ".$caller.", called: ".$called." clicktodiallogin: ".$login;
202 dol_syslog($txt);
203 print '<body onload="history.go(-1);">'."\n";
204 print '<!-- '.$txt.' -->';
205 fwrite($oSocket, "Action: login\r\n");
206 fwrite($oSocket, "Events: off\r\n");
207 fwrite($oSocket, "Username: $login\r\n");
208 fwrite($oSocket, "Secret: $password\r\n\r\n");
209 fwrite($oSocket, "Action: originate\r\n");
210 fwrite($oSocket, "Channel: ".$channel.$caller."\r\n");
211 fwrite($oSocket, "WaitTime: $strWaitTime\r\n");
212 fwrite($oSocket, "CallerId: $strCallerId\r\n");
213 fwrite($oSocket, "Exten: ".$prefix.$number."\r\n");
214 fwrite($oSocket, "Context: $strContext\r\n");
215 fwrite($oSocket, "Priority: $strPriority\r\n\r\n");
216 fwrite($oSocket, "Action: Logoff\r\n\r\n");
217 sleep(2);
218 fclose($oSocket);
219 print '</body>'."\n";
220 }
221 }
222} else {
223 print 'Bad parameters in URL. Must be '.dol_escape_htmltag($_SERVER['PHP_SELF']).'?caller=99999&called=99999&login=xxxxx&password=xxxxx';
224}
225
226// End of page
227llxFooter();
228$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.