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