1
- <?php
1
+ <?php
2
2
/**
3
- * Asterisk SugarCRM Integration
3
+ * Asterisk SugarCRM Integration
4
4
* (c) KINAMU Business Solutions AG 2009
5
- *
5
+ *
6
6
* Parts of this code are (c) 2006. RustyBrick, Inc. http://www.rustybrick.com/
7
- * Parts of this code are (c) 2008 vertico software GmbH
7
+ * Parts of this code are (c) 2008 vertico software GmbH
8
8
* Parts of this code are (c) 2009 abcona e. K. Angelo Malaguarnera E-Mail [email protected]
9
9
* http://www.sugarforge.org/projects/yaai/
10
- *
10
+ *
11
11
* This program is free software; you can redistribute it and/or modify it under
12
12
* the terms of the GNU General Public License version 3 as published by the
13
13
* Free Software Foundation with the addition of the following permission added
14
14
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
15
15
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
16
16
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
17
- *
17
+ *
18
18
* This program is distributed in the hope that it will be useful, but WITHOUT
19
19
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20
20
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21
21
* details.
22
- *
22
+ *
23
23
* You should have received a copy of the GNU General Public License along with
24
24
* this program; if not, see http://www.gnu.org/licenses or write to the Free
25
25
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26
26
* 02110-1301 USA.
27
- *
27
+ *
28
28
* You can contact KINAMU Business Solutions AG at [email protected]
29
- *
29
+ *
30
30
* The interactive user interfaces in modified source and object code versions
31
31
* of this program must display Appropriate Legal Notices, as required under
32
32
* Section 5 of the GNU General Public License version 3.
33
- *
33
+ *
34
34
*/
35
35
if (!defined ('sugarEntry ' ) || !sugarEntry) die ('Not A Valid Entry Point ' );
36
36
37
37
require_once ('include/utils.php ' );
38
38
require_once ('include/export_utils.php ' );
39
+ //require_once('callinize_db.php');
39
40
40
41
global $ sugar_config ;
41
42
global $ locale ;
89
90
//format Phone Number
90
91
$ number = $ _REQUEST ['phoneNr ' ];
91
92
$ prefix = $ sugar_config ['asterisk_prefix ' ];
92
- $ number = str_replace ("+ " , "00 " , $ number );
93
+ $ number = str_replace ("+1 " , "" , $ number );
93
94
$ number = str_replace (array ("( " , ") " , " " , "- " , "/ " , ". " ), "" , $ number );
94
95
$ number = $ prefix .$ number ;
95
96
echo "Originate Params: Number: $ number, Channel: $ channel, Context: $ context, Exten: $ number... \n" ;
96
97
97
98
98
99
$ socket = fsockopen ($ server , $ port , $ errno , $ errstr , 20 );
99
-
100
+
100
101
if (!$ socket ) {
101
102
echo "errstr ( $ errno) <br> \n" ;
102
-
103
+
103
104
} else {
104
105
105
106
// $result = ReadResponse($socket);
106
107
// echo "AMI Header: " . $result;
107
108
108
109
// log on to Asterisk
109
- fputs ($ socket , "Action: Login \r\n" );
110
+ fputs ($ socket , "Action: Login \r\n" );
110
111
fputs ($ socket , $ Username );
111
112
fputs ($ socket , $ Secret );
112
113
fputs ($ socket , "Events: off \r\n" );
113
- fputs ($ socket , "\r\n" );
114
+ fputs ($ socket , "\r\n" );
114
115
$ result = ReadResponse ($ socket );
115
116
echo ("Login Response: " . $ result . "\n" );
116
117
117
118
118
119
// dial number
119
- fputs ($ socket , "Action: originate \r\n" );
120
- fputs ($ socket , "Channel: " . $ channel ."\r\n" );
121
- fputs ($ socket , "Context: " . $ context ."\r\n" );
122
- fputs ($ socket , "Exten: " . $ number . "\r\n" );
123
- fputs ($ socket , "Priority: 1 \r\n" );
124
- fputs ($ socket , "Callerid: " . $ _REQUEST ['phoneNr ' ] ."\r\n" );
120
+ fputs ($ socket , "Action: originate \r\n" );
121
+ fputs ($ socket , "Channel: " . $ channel ."\r\n" );
122
+ fputs ($ socket , "Context: " . $ context ."\r\n" );
123
+ fputs ($ socket , "Exten: " . $ number . "\r\n" );
124
+ fputs ($ socket , "Priority: 1 \r\n" );
125
+ fputs ($ socket , "Callerid: " . $ _REQUEST ['phoneNr ' ] ."\r\n" );
126
+ fputs ($ socket , "Account: CLICKTODIAL- " . formatPhoneNumberToE164 ($ _REQUEST ['phoneNr ' ]) . "- " . $ _REQUEST ['module ' ] . "- " . $ _REQUEST ['contactId ' ] . "\r\n" );
125
127
fputs ($ socket , "Variable: CALLERID(number)= " . $ extension . "\r\n\r\n" );
126
128
127
129
// You will not get an originate response unless you wait for the phone to be answered... so it's impractical to wait.
130
132
echo "Originate Response: " . $ result . "\n" ;
131
133
132
134
fputs ($ socket , "Action: Logoff \r\n\r\n" );
133
- fputs ($ socket , "\r\n" );
134
-
135
+ fputs ($ socket , "\r\n" );
136
+
135
137
$ result = ReadResponse ($ socket );
136
138
echo ("Logout Response: " . $ result );
137
139
142
144
//var_dump($context);
143
145
//var_dump($number);
144
146
//sleep(1);
145
-
147
+
146
148
// close socket
147
149
fclose ($ socket );
148
150
}
@@ -175,21 +177,6 @@ function ReadResponse($socket, $timeout = 500000) {
175
177
return $ retVal ;
176
178
}
177
179
178
- /**
179
- * Performs an async get request (doesn't wait for response)
180
- * Note: One limitation of this approach is it will not work if server does any URL rewriting
181
- */
182
- function gitimg_log ($ event ) {
183
- $ host = "gitimg.com " ;
184
- $ path = "/rs/track/blak3r/yaai-stats/ $ event/increment " ;
185
- $ fp = fsockopen ($ host ,80 , $ errno , $ errstr , 30 );
186
- $ out = "GET " . $ path . " HTTP/1.1 \r\n" ;
187
- $ out .= "Host: " . $ host . "\r\n" ;
188
- $ out .= "Connection: Close \r\n\r\n" ;
189
- fwrite ($ fp , $ out );
190
- fclose ($ fp );
191
- }
192
-
193
180
/**
194
181
* Another var_dump() alternative, for debugging use.
195
182
*
@@ -206,18 +193,51 @@ function printr($data, $exit = TRUE) {
206
193
// exit;
207
194
// }
208
195
}
209
-
196
+
210
197
function logLine ($ str )
211
198
{
212
- // print($str);
213
-
199
+ // print($str);
200
+
214
201
// if logging is enabled.
215
- if ( !empty ($ sugar_config ['asterisk_log_file ' ]) )
202
+ if ( !empty ($ sugar_config ['asterisk_log_file ' ]) )
216
203
{
217
- $ myFile = $ sugar_config ['asterisk_log_file ' ]; // Might be a performance issue being here...
204
+ $ myFile = $ sugar_config ['asterisk_log_file ' ]; // Might be a performance issue being here...
218
205
$ fh = fopen ($ myFile , 'a ' ) or die ("can't open file " );
219
206
fwrite ($ fh , $ str );
220
207
fclose ($ fh );
221
208
}
222
209
}
210
+
211
+ /**
212
+ * Helper method for turning any number into an e164 number
213
+ *
214
+ * @param string $number The number you want to convert
215
+ * @return string
216
+ */
217
+ function formatPhoneNumberToE164 ($ number ) {
218
+
219
+ // get rid of any non (digit, + character)
220
+ $ phone = preg_replace ('/[^0-9+]/ ' , '' , $ number );
221
+
222
+ // validate intl 10
223
+ if (preg_match ('/^\+([2-9][0-9]{9})$/ ' , $ phone , $ matches )) {
224
+ return "+ {$ matches [1 ]}" ;
225
+ }
226
+
227
+ // validate US DID
228
+ if (preg_match ('/^\+?1?([2-9][0-9]{9})$/ ' , $ phone , $ matches )) {
229
+ return "+1 {$ matches [1 ]}" ;
230
+ }
231
+
232
+ // validate INTL DID
233
+ if (preg_match ('/^\+?([2-9][0-9]{8,14})$/ ' , $ phone , $ matches )) {
234
+ return "+ {$ matches [1 ]}" ;
235
+ }
236
+
237
+ // premium US DID
238
+ if (preg_match ('/^\+?1?([2-9]11)$/ ' , $ phone , $ matches )) {
239
+ return "+1 {$ matches [1 ]}" ;
240
+ }
241
+ }
242
+
223
243
?>
0 commit comments