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