Documentation is available at lib-mobytsms.inc.php
1 <?php
2 /**
3 * Libreria per l'invio di SMS tramite il servizio SMS-Web offerto da Mobyt Srl
4 *
5 * - <b>Versione 1.2.0</b>
6 * - Aggiunto supporto alle nuova {@link mobytSms::setQualityAuto() qualità automatica}
7 * - Aggiunto supporto agli sms con {@link mobytSms::setQualityAutoNotify() notifica}
8 * - Aggiunto supporto agli sms {@link mobytWapPush Wap/Push}
9 * - <b>Versione 1.2.1</b>
10 * - Aggiunto supporto alle suonerie in {@link mobytRTTTL formato RTTTL}
11 * - <b>Versione 1.2.2</b>
12 * - Bugfix: la versione, utile per motivi di debug, non veniva inviata correttamente al server
13 * - <b>Versione 1.2.3</b>
14 * - Bugfix: corretto un problema nell'invio di messaggi Wap/Push
15 * - <b>Versione 1.2.4</b>
16 * - Aggiunta possibilità di ottenere il {@link mobytSms::getAvailableNotifies() numero di notifiche disponibili}
17 * - <b>Versione 1.2.5</b>
18 * - Aggiunto supporto per l'invio multiplo tramite SMS-Batch (FTP)
19 * - <b>Versione 1.3.0</b>
20 * - Classe aggiornata alla nuova documentazione
21 * - <b>Versione 1.3.1</b>
22 * - Aggiunto supporto ai messaggi Flash
23 * - Aggiunta compatibilità con register_long_arrays off (opzione PHP5) in sms-relay.php
24 * - <b>Versione 1.3.2</b>
25 * - Risolto problema nella scelta della qualità se la classe viene inclusa all'interno di una funzione
26 * - <b>Versione 1.3.3</b>
27 * - Aggiunto supporto al servizio {@link mobytMnc MNC (Mobyt Number Check)}
28 * - <b>Versione 1.4.0</b>
29 * - Aggiunto supporto al servizio {@link mobytMMS MMS}
30 * - <b>Versione 1.4.1</b>
31 * - Risolti problemi nell'utilizzo del servizio MNC
32 * - <b>Versione 1.4.2</b>
33 * - Risolti problemi nell'esempio MNC
34 * - <b>Versione 1.4.3</b>
35 * - Modificata spedizione FTP per consentire invio SMS con notifica
36 * - <b>Versione 1.4.4</b>
37 * - Aggiunto supporto per la ricezione MMS
38 *
39 * @version 1.4.4
40 * @package Mobyt-SmsWeb
41 * @author Matteo Beccati - matteo.beccati@mobyt.it
42 * Francesca Maccari - francesca.maccari@mobyt.it
43 * Marco Romano - marco.romano@mobyt.it
44 * @copyright (C) 2003-2007 Mobyt srl
45 * @license https://www.mobyt.it/bsd-license.html BSD License
46 *
47 */
48
49 /**#@+
50 * @access private
51 */
52 /**
53 * Versione della classe
54 */
55 define('MOBYT_PHPSMS_VERSION', '1.4.4');
56 /**
57 * Tipo di autenticazione basata su MD5, con password <b>non</b> inviata in chiaro
58 */
59 define('MOBYT_AUTH_MD5', 1);
60 /**
61 * Tipo di autenticazione basata su IP, con password inviata in chiaro
62 */
63 define('MOBYT_AUTH_PLAIN', 2);
64
65 /**
66 * Qualità messaggi in base al valore di default dell'account
67 */
68 define('MOBYT_QUALITY_DEFAULT', 0);
69 /**
70 * Qualità messaggi bassa (LQS)
71 */
72 define('MOBYT_QUALITY_LQS', 1);
73 /**
74 * Qualità messaggi media (MQS)
75 */
76 define('MOBYT_QUALITY_MQS', 2);
77 /**
78 * Qualità messaggi alta (HQS)
79 */
80 define('MOBYT_QUALITY_HQS', 3);
81 /**
82 * Qualità messaggi automatica
83 */
84 define('MOBYT_QUALITY_AUTO', 4);
85 /**
86 * Qualità messaggi automatica con notifica
87 */
88 define('MOBYT_QUALITY_AUTO_NY', 5);
89
90 /**
91 * Tipo operazione TEXT
92 */
93 define('MOBYT_OPERATION_TEXT', 0);
94 /**
95 * Tipo operazione RING
96 */
97 define('MOBYT_OPERATION_RING', 1);
98 /**
99 * Tipo operazione Logo Operatore
100 */
101 define('MOBYT_OPERATION_OLGO', 2);
102 /**
103 * Tipo operazione Logo Gruppo
104 */
105 define('MOBYT_OPERATION_GLGO', 3);
106 /**
107 * Tipo operazione 8 bit
108 */
109 define('MOBYT_OPERATION_8BIT', 4);
110 /**
111 * Tipo operazione Flash
112 */
113 define('MOBYT_OPERATION_FLASH', 5);
114
115 /**
116 * @global array Array di conversione per le qualità
117 */
118 $GLOBALS['mobyt_qty'] = array(
119 MOBYT_QUALITY_LQS => 'll',
120 MOBYT_QUALITY_MQS => 'l',
121 MOBYT_QUALITY_HQS => 'h',
122 MOBYT_QUALITY_AUTO => 'a',
123 MOBYT_QUALITY_AUTO_NY => 'a'
124 );
125
126 /**
127 * @global array Array di conversione per l'operazione
128 */
129 $GLOBALS['mobyt_ops'] = array(
130 MOBYT_OPERATION_TEXT => 'TEXT',
131 MOBYT_OPERATION_RING => 'RING',
132 MOBYT_OPERATION_OLGO => 'OLGO',
133 MOBYT_OPERATION_GLGO => 'GLGO',
134 MOBYT_OPERATION_8BIT => '8BIT',
135 MOBYT_OPERATION_FLASH => 'FLASH'
136 );
137 /**#@-*/
138
139 /**
140 * Classe per l'invio di SMS tramite il servizio SMS-Web
141 *
142 * Le impostazioni utilizzate di default sono:
143 * - Mittente: <b>"MobytSms"</b>
144 * - Autenticazione: <b>MD5</b>
145 * - Qualità: <b>Non impostata</b> - Il default è l'utilizzo della modalità automatica
146 *
147 * @package Mobyt-SmsWeb
148 * @example sms-single.php Invio di un singolo sms in alta qualità con autenticazione MD5
149 */
150 class mobytSms
151 {
152 /**#@+
153 * @access private
154 * @var string
155 */
156 var $auth = MOBYT_AUTH_MD5;
157 var $quality = MOBYT_QUALITY_DEFAULT;
158 var $operation = MOBYT_OPERATION_TEXT;
159 var $from;
160 var $login;
161 var $pwd;
162 var $udh;
163 /**#@-*/
164
165 /**
166 * @param string Username di accesso (Login)
167 * @param string Password dispositiva
168 * @param string Intestazione mittente
169 *
170 * @see setFrom
171 */
172 function mobytSms($login, $pwd, $from = 'MobytSms')
173 {
174 $this->login = $login;
175 $this->pwd = $pwd;
176 $this->setFrom($from);
177 }
178
179 /**
180 * Imposta intestazione mittente
181 *
182 * Il mittente può essere composto da un massimo di 11 caratteri alfanumerici o un numero telefonico
183 * con prefisso internazionale.
184 *
185 * @param string Intestazione mittente
186 */
187 function setFrom($from)
188 {
189 $this->from = substr($from, 0, 14);
190 }
191
192
193 /**
194 * Utilizza l'autenticazione di tipo MD5
195 */
196 function setAuthMd5()
197 {
198 $this->auth = MOBYT_AUTH_MD5;
199 }
200
201 /**
202 * Utilizza l'autenticazione con password in chiaro basata sull'IP
203 */
204 function setAuthPlain()
205 {
206 $this->auth = MOBYT_AUTH_PLAIN;
207 }
208
209
210 /**
211 * Imposta la qualità messaggi al default dell'account
212 */
213 function setQualityDefault()
214 {
215 $this->quality = MOBYT_QUALITY_DEFAULT;
216 }
217
218 /**
219 * Imposta la qualità messaggi come bassa
220 */
221 function setQualityLow()
222 {
223 $this->quality = MOBYT_QUALITY_LQS;
224 }
225
226 /**
227 * Imposta la qualità messaggi come media
228 */
229 function setQualityMedium()
230 {
231 $this->quality = MOBYT_QUALITY_MQS;
232 }
233
234 /**
235 * Imposta la qualità messaggi come alta
236 */
237 function setQualityHigh()
238 {
239 $this->quality = MOBYT_QUALITY_HQS;
240 }
241
242 /**
243 * Imposta la qualità messaggi automatica
244 */
245 function setQualityAuto()
246 {
247 $this->quality = MOBYT_QUALITY_AUTO;
248 }
249
250 /**
251 * Imposta la qualità messaggi automatica con notifica
252 *
253 * @example sms-single-notify.php Invio di un singolo sms con notifica
254 * @example sms-multi-notify.php Invio sms multipli con notifica
255 */
256 function setQualityAutoNotify()
257 {
258 $this->quality = MOBYT_QUALITY_AUTO_NY;
259 }
260
261 /**
262 * Imposta il tipo di messaggio a TEXT
263 */
264 function setOperationText()
265 {
266 $this->operation = MOBYT_OPERATION_TEXT;
267 }
268
269 /**
270 * Imposta il tipo di messaggio a RING (suoneria)
271 *
272 * L'invio di messaggi di tipo RING necessita l'invio in alta qualità o con notifica.
273 * Questa verrà impostata automaticamente, tranne nel caso in cui sia stata impostata la qualità automatica con notifica
274 *
275 * @example sms-single-ring.php Invio di un singolo sms in modalità RING
276 */
277 function setOperationRing()
278 {
279 $this->operation = MOBYT_OPERATION_RING;
280
281 if ($this->quality != MOBYT_QUALITY_AUTO_NY)
282 $this->setQualityHigh();
283 }
284
285 /**
286 * Imposta il tipo di messaggio a OLGO (logo operatore)
287 *
288 * L'invio di messaggi di tipo OLGO necessita l'invio in alta qualità o con notifica.
289 * Questa verrà impostata automaticamente, tranne nel caso in cui sia stata impostata la qualità automatica con notifica
290 */
291 function setOperationOlgo()
292 {
293 $this->operation = MOBYT_OPERATION_OLGO;
294
295 if ($this->quality != MOBYT_QUALITY_AUTO_NY)
296 $this->setQualityHigh();
297 }
298
299 /**
300 * Imposta il tipo di messaggio a GLGO (logo gruppo)
301 *
302 * L'invio di messaggi di tipo GLGO necessita l'invio in alta qualità o con notifica.
303 * Questa verrà impostata automaticamente, tranne nel caso in cui sia stata impostata la qualità automatica con notifica
304 */
305 function setOperationGlgo()
306 {
307 $this->operation = MOBYT_OPERATION_GLGO;
308
309 if ($this->quality != MOBYT_QUALITY_AUTO_NY)
310 $this->setQualityHigh();
311 }
312
313 /**
314 * Imposta il tipo di messaggio a 8 bit
315 *
316 * L'invio di messaggi di tipo 8BIT necessita l'invio in alta qualità o con notifica.
317 * Questa verrà impostata automaticamente, tranne nel caso in cui sia stata impostata la qualità automatica con notifica
318 *
319 * @param string UDH
320 */
321 function setOperation8Bit($udh)
322 {
323 $this->operation = MOBYT_OPERATION_8BIT;
324
325 $this->udh = $udh;
326
327 if ($this->quality != MOBYT_QUALITY_AUTO_NY)
328 $this->setQualityHigh();
329 }
330
331 /**
332 * Imposta il tipo di messaggio a FLASH
333 */
334 function setOperationFlash()
335 {
336 $this->operation = MOBYT_OPERATION_FLASH;
337 }
338
339
340
341 /**
342 * Controlla il credito disponibile espresso in Euro
343 *
344 * @returns mixed Un intero corrispondente al credito o <i>FALSE</i> in caso di errore
345 *
346 * @example sms-credit.php Controllo credito e messaggi disponibili
347 */
348 function getCredit()
349 {
350 $op = 'GETCREDIT';
351
352 $fields = array(
353 'operation' => $op,
354 'id' => $this->login,
355 'password' => $this->auth == MOBYT_AUTH_MD5 ? '' : $this->pwd,
356 'ticket' => $this->auth == MOBYT_AUTH_MD5 ? md5($this->login.$op.$this->pwd) : ''
357 );
358
359 if (preg_match('/^OK (\d+)/', $this->httpPost($fields), $m))
360 return intval($m[1]);
361
362 return false;
363 }
364
365
366
367 /**
368 * Controlla il numero approssimativo di messaggi disponibili
369 *
370 * <b>N.B.</b> Il numero di messaggi disponibile dipende dalla qualità con cui verranno inviati.
371 *
372 * @returns mixed Un intero corrispondente al numero di messaggi o <i>FALSE</i> in caso di errore
373 *
374 * @example sms-credit.php Controllo credito e messaggi disponibili
375 */
376 function getAvailableSms()
377 {
378 $op = 'GETMESS';
379
380 $fields = array(
381 'operation' => $op,
382 'id' => $this->login,
383 'password' => $this->auth == MOBYT_AUTH_MD5 ? '' : $this->pwd,
384 'ticket' => $this->auth == MOBYT_AUTH_MD5 ? md5($this->login.$op.$this->pwd) : ''
385 );
386
387 if (preg_match('/^OK (\d+)/', $this->httpPost($fields), $m))
388 return intval($m[1]);
389
390 return false;
391 }
392
393
394
395 /**
396 * Controlla il numero di notifiche disponibili
397 *
398 * @returns mixed Un intero corrispondente al numero di notifiche o <i>FALSE</i> in caso di errore
399 */
400 function getAvailableNotifies()
401 {
402 $op = 'GETNOTIFY';
403
404 $fields = array(
405 'operation' => $op,
406 'id' => $this->login,
407 'password' => $this->auth == MOBYT_AUTH_MD5 ? '' : $this->pwd,
408 'ticket' => $this->auth == MOBYT_AUTH_MD5 ? md5($this->login.$op.$this->pwd) : ''
409 );
410
411 if (preg_match('/^OK (\d+)/', $this->httpPost($fields), $m))
412 return intval($m[1]);
413
414 return false;
415 }
416
417 /**
418 * Invia un SMS
419 *
420 * Nel caso sia utilizzata la qualità automatica con notifica, serà necessario passare un identificatore univoco di max 20 caratteri numerici come terzo parametro. Qualora non venisse impostato, ne verrà generato uno casuale in maniera automatica, per permettere il corretto invio del messaggio.
421 *
422 * @param string Numero telefonico con prefisso internazionale (es. +393201234567)
423 * @param string Testo del messaggio (max 160 caratteri)
424 * @param string Identificatore univoco del messaggio da utilizzare nel caso sia richiesta la notifica
425 *
426 * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ...")
427 *
428 * @example sms-single.php Invio di un singolo sms in alta qualità con autenticazione MD5
429 */
430 function sendSms($rcpt, $text, $act = '')
431 {
432 global $mobyt_qty $mobyt_ops
433
434 $operation = isset($mobyt_ops[$this->operation]) ? $mobyt_ops[$this->operation] : 'TEXT';
435
436 $fields = array(
437 'operation' => $operation,
438 'from' => $this->from,
439 'rcpt' => $rcpt,
440 'data' => $text,
441 'id' => $this->
442 login );
443
444 if ($this->quality == MOBYT_QUALITY_AUTO_NY)
445 {
446 if ($act == '')
447 {
448 // Generate random act
449 while (strlen($act) < 16)
450 $act .= preg_replace('/[^0-9]/', '', md5(uniqid('', true)));
451
452 if (strlen($act) > 20)
453 $act = substr($act, 0, 20);
454 }
455
456 $fields['act'] = $act;
457 }
458
459 if ($this->quality != MOBYT_QUALITY_DEFAULT && isset($mobyt_qty[$this->quality]))
460 $fields['qty'] = $mobyt_qty[$this->quality];
461
462 if ($this->auth == MOBYT_AUTH_MD5)
463 {
464 $fields['password'] = '';
465 $fields['ticket'] = md5($this->login.$operation.$rcpt.$this->from.$text.$this->pwd);
466 }
467 else
468 {
469 $fields['password'] = $this->pwd;
470 $fields['ticket'] = '';
471 }
472
473 if ($this->operation == MOBYT_OPERATION_8BIT)
474 $fields['udh'] = $this->udh;
475
476 return trim($this->httpPost($fields));
477 }
478
479 /**
480 * Invia un SMS a più destinatari
481 *
482 * Nel caso sia utilizzata la qualità automatica con notifica, serà necessario passare un array associativo come primo parametro, le cui chiavi siano identificatori univoci di max 20 caratteri numerici.
483 *
484 * @example sms-multi.php Invio di un sms a più numeri in media qualità con autenticazione tramite password in chiaro
485 *
486 * @param array Array di numeri telefonici con prefisso internazionale (es. +393201234567)
487 * @param string Testo del messaggio (max 160 caratteri)
488 *
489 * @returns string Elenco di risposte ricevute dal gateway ("OK ..." o "KO ..."), separate da caratteri di "a capo" (\n)
490 */
491 function sendMultiSms($rcpts, $text)
492 {
493 global $mobyt_qty $mobyt_ops
494
495 if (!is_array($rcpts))
496 return $this->sendSms($rcpts, $text);
497
498
499 $operation = isset($mobyt_ops[$this->operation]) ? $mobyt_ops[$this->operation] : 'TEXT';
500
501 $fields = array(
502 'id' => $this->login,
503 'password' => $this->auth == MOBYT_AUTH_MD5 ? '' : $this->pwd,
504 'operation' => $operation,
505 'from' => $this->from,
506 'data' => $text
507 );
508
509 if ($this->quality != MOBYT_QUALITY_DEFAULT && isset($mobyt_qty[$this->quality]))
510 $fields['qty'] = $mobyt_qty[$this->quality];
511
512 if ($this->operation == MOBYT_OPERATION_8BIT)
513 $fileds['udh'] = $this->udh;
514
515 $ret = array();
516 foreach ($rcpts as $act => $rcpt)
517 {
518 $fields['rcpt'] = $rcpt;
519 $fields['ticket'] = $this->auth == MOBYT_AUTH_MD5 ?
520 md5($this->login.$operation.$rcpt.$this->from.$text.$this->pwd) :
521 '';
522
523 if ($this->quality == MOBYT_QUALITY_AUTO_NY)
524 $fields['act'] = $act;
525
526 $ret[] = trim($this->httpPost($fields));
527 }
528
529 return join("\n", $ret);
530 }
531
532 /**
533 * Send an HTTP POST request, choosing either cURL or fsockopen
534 *
535 * @access private
536 */
537 function httpPost($fields, $url = '/sms-gw/sendsmart')
538 {
539 $qs = array();
540 foreach ($fields as $k => $v)
541 $qs[] = $k.'='.urlencode($v);
542 $qs = join('&', $qs);
543
544 if (function_exists('curl_init'))
545 return mobytSms::httpPostCurl($qs, $url);
546
547 $errno = $errstr = '';
548 if ($fp = @fsockopen('smsweb.mobyt.it', 80, $errno, $errstr, 30))
549 {
550 fputs($fp, "POST ".$url." HTTP/1.0\r\n");
551 fputs($fp, "Host: smsweb.mobyt.it\r\n");
552 fputs($fp, "User-Agent: phpMobytSms/".MOBYT_PHPSMS_VERSION."\r\n");
553 fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
554 fputs($fp, "Content-Length: ".strlen($qs)."\r\n");
555 fputs($fp, "Connection: close\r\n");
556 fputs($fp, "\r\n".$qs);
557
558 $content = '';
559 while (!feof($fp))
560 $content .= fgets($fp, 1024);
561
562 fclose($fp);
563
564 return preg_replace("/^.*?\r\n\r\n/s", '', $content);
565 }
566
567 return false;
568 }
569
570 /**
571 * Send an HTTP POST request, through cURL
572 *
573 * @access private
574 */
575 function httpPostCurl($qs, $url)
576 {
577 if ($ch = @curl_init('http://smsweb.mobyt.it'.$url))
578 {
579 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
580 curl_setopt($ch, CURLOPT_USERAGENT, 'phpMobytSms/'.MOBYT_PHPSMS_VERSION.' (curl)');
581 curl_setopt($ch, CURLOPT_POST, 1);
582 curl_setopt($ch, CURLOPT_POSTFIELDS, $qs);
583
584 return curl_exec($ch);
585 }
586
587 return false;
588 }
589
590 /**
591 * Converti stringa in formato esadecimale OTA, per invio RING, 8BIT, ecc...
592 *
593 * @param string Stringa da convertire
594 *
595 * @return string
596 */
597 function stringToOTA($str)
598 {
599 $ret = '';
600
601 $len = strlen($str);
602 for ($x = 0; $x < $len; $x++)
603 $ret .= sprintf('%X', ord($str{$x}));
604
605 return $ret;
606 }
607 }
608
609
610
611 /**
612 * Classe per l'invio di SMS multipli tramite il servizio SMS-Batch
613 *
614 * Il servizio richiede una directory scrivibile dall'utente con cui gira l'interprete PHP e le funzioni FTP abilitate
615 *
616 * Le impostazioni utilizzate di default sono:
617 * - Mittente: <b>"MobytSms"</b>
618 * - Qualità: <b>Automatica</b>
619 * - Directory temporanea: <b>la directory impostata come <i>upload_tmp_dir</i> in php.ini</b>
620 *
621 * <b>N.B.</b> Il servizio supporta solo messaggi di testo e non supporta la notifica
622 *
623 * @package Mobyt-SmsWeb
624 * @example smsftp-multi.php Invio di messaggi multipli via SMS-Batch
625 */
626 class mobytSmsFtp extends mobytSms
627 {
628 /**#@+
629 * @access private
630 * @var boolean
631 */
632 var $has_ftp;
633 var $use_pasv = false;
634
635 /**
636 * @var string
637 */
638 var $cache_dir;
639 /**#@-*/
640
641 /**
642 * @param string Username di accesso (Login)
643 * @param string Password dispositiva
644 * @param string Intestazione mittente
645 *
646 * @see setFrom
647 */
648 function mobytSmsFtp($login, $pwd, $from = 'MobytSms')
649 {
650 $this->login = $login;
651 $this->pwd = $pwd;
652 $this->setFrom($from);
653
654 if (!function_exists('ftp_connect'))
655 die("Estensione FTP non disponibile");
656
657 $this->cache_dir = ini_get('upload_tmp_dir');
658 }
659
660 /**
661 * Imposta la directory per i file temporanei
662 *
663 * @param string Directory temporanea
664 */
665 function setTmpDir($dir)
666 {
667 $this->cache_dir = preg_replace('/[/\\\\]$/', '', $dir);
668 }
669
670 /**
671 * Utilizza la modalità passiva per il trasferimento
672 *
673 * @param boolean Abilita modalità passiva
674 */
675 function usePassive($pasv = true)
676 {
677 $this->use_pasv = $pasv;
678 }
679
680 /**#@+
681 * Disabilita alcune funzioni
682 *
683 * @access private
684 */
685
686 function batchOperationError()
687 {
688 trigger_error("E' possibile inviare solo messaggi di testo in modalità batch", E_USER_WARNING);
689 }
690 function setOperationRing()
691 {
692 batchOperationError();
693 }
694 function setOperationOlgo()
695 {
696 batchOperationError();
697 }
698 function setOperationGlgo()
699 {
700 batchOperationError();
701 }
702 function setOperation8Bit()
703 {
704 batchOperationError();
705 }
706 /**#@-*/
707
708
709 /**
710 * Invia un SMS tramite SMS-Batch
711 *
712 * @param string Numero telefonico con prefisso internazionale (es. +393201234567)
713 * @param string Testo del messaggio (max 160 caratteri)
714 * @param string Identificativo dell' SMS spedito (da valorizzare solo in caso di spedizione con notifica)
715 *
716 * @returns boolean True se il messaggio è stato accodato con successo
717 */
718 function sendSms($rcpt, $text, $act='')
719 {
720 return $this->sendMultiSms($rcpt, $text, $act);
721 }
722
723 /**
724 * Invia un SMS a più destinatari tramite SMS-Batch
725 *
726 * @example smsftp-multi.php Invio di un sms a più numeri tramite SMS-Batch
727 *
728 * @param array Array di numeri telefonici con prefisso internazionale (es. +393201234567)
729 * @param string Testo del messaggio (max 160 caratteri)
730 * @param array Array di identificativi dei singoli SMS spediti (da valorizzare solo in caso di spedizione con notifica)
731 *
732 * @returns integer Numero di messaggi accodati
733 */
734 function sendMultiSms($rcpts, $text, $act='')
735 {
736 global $mobyt_qty $mobyt_ops
737 $notify = true;
738
739 if (!is_array($act))
740 {
741 if($act == '')
742 $notify = false;
743 $act = array($act);
744
745 }
746
747
748 foreach ($act as $x => $v)
749 {
750 $v = trim($act[$x]);
751 $act[$x] = $v;
752 }
753
754 if (!is_array($rcpts))
755 $rcpts = array($rcpts);
756
757
758 foreach ($rcpts as $x => $v)
759 {
760 $v = trim($rcpts[$x]);
761
762 if (preg_match('/^\+\d+$/', $v))
763 $rcpts[$x] = $v;
764 else
765 {
766 unset($rcpts[$x]);
767 unset($act[$x]);
768 }
769 }
770
771 $rcpts = array_values($rcpts);
772 $act = array_values($act);
773
774 if (!count($rcpts))
775 die("Nessun numero valido fornito");
776
777
778
779 if (!is_writable($this->cache_dir) || !($fname = tempnam($this->cache_dir, 'phpSmsFtp-')))
780 die("Impossibile creare il file temporaneo");
781
782 $from = substr(preg_replace('/[\t\n\r]+/', ' ', $this->from), 0, 11);
783 $data = substr(preg_replace('/[\t\n\r]+/', ' ', $text), 0, 160);
784 $expire = 72;
785
786 if ($this->quality != MOBYT_QUALITY_DEFAULT && isset($mobyt_qty[$this->quality]))
787 $qty = $mobyt_qty[$this->quality];
788 else
789 $qty = '';
790
791 $fp = @fopen($fname, 'w');
792 foreach($rcpts as $k => $rcpt){
793 printf("%-25s%-25s%-160s%010d%-2s%-20s\n", $rcpt, $from, $data, $expire, $qty, $act[$k]);
794 @fputs($fp, sprintf("%-25s%-25s%-160s%010d%-2s%-20s\n", $rcpt, $from, $data, $expire, $qty, $act[$k]));
795 }
796 @fclose($fp);
797
798 $ok = false;
799 if ($ftp = ftp_connect('smsftp.mobyt.it'))
800 {
801 if (ftp_login($ftp, $this->login, $this->pwd))
802 {
803 ftp_pasv($ftp, $this->use_pasv);
804
805 if (ftp_chdir($ftp, 'incoming'))
806 {
807 if (ftp_put($ftp, basename($fname).'.txt', $fname, FTP_ASCII))
808 {
809 $fp = @fopen($fname, 'w');
810 if($notify)
811 @fputs($fp, "ACTION=INFO\nGET_NOTIFY=on\nWHERE=HTTP\n");
812 else
813 @fputs($fp, ".");
814 @fclose($fp);
815
816 if (ftp_put($ftp, basename($fname).'.txt.do_send', $fname, FTP_ASCII))
817 {
818 $ok = true;
819 }
820 }
821 }
822 }
823 else
824 {
825 @unlink($fname);
826 die("Login/password errati o servizio non abilitato");
827 }
828
829 ftp_close($ftp);
830 }
831
832 @unlink($fname);
833
834 if (!$ok)
835 die("Si è verificato un errore durante l'upload dei file");
836
837 return count($rcpts);
838 }
839 }
840
841
842
843 /**
844 * Classe per l'invio di SMS WAP/Push tramite il servizio SMS-Web
845 *
846 * Le impostazioni utilizzate di default sono:
847 * - Mittente: <b>"MobytSms"</b>
848 * - Autenticazione: <b>MD5</b>
849 * - Qualità: <b>Alta</b>
850 *
851 * @package Mobyt-SmsWeb
852 * @example wappush-single.php Invio di un singolo sms Wap/Push con autenticazione MD5
853 */
854 class mobytWapPush extends mobytSms
855 {
856 /**
857 * Invia un SMS WAP/Push
858 *
859 * @param string Numero telefonico con prefisso internazionale (es. +393201234567)
860 * @param string Testo del messaggio
861 * @param string Collegamento ipertestuale
862 *
863 * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ...")
864 *
865 * @example wappush-single.php Invio di un singolo sms Wap/Push con autenticazione MD5
866 */
867 function sendSms($rcpt, $title, $url)
868 {
869 $this->setQualityHigh();
870 $this->setOperation8Bit('05040B8423F0');
871
872 $data = 'DC0601AE02056A0045C6';
873
874 if (preg_match('#^http(s?)://(www\.)?#', $url, $m))
875 {
876 $ssl = isset($m[1]) && $m[1];
877 $www = isset($m[2]) && $m[2];
878
879 if ($ssl)
880 $data .= $www ? '0F' : '0E';
881 else
882 $data .= $www ? '0D' : '0C';
883
884 $url = str_replace($m[0], '', $url);
885 }
886
887 $data .= MobytWapPush::str2wbxml($url);
888 $data .= '01';
889 $data .= MobytWapPush::str2wbxml($title);
890 $data .= '0101';
891
892 return parent::sendSms($rcpt, $data);
893 }
894
895 /**
896 * Invia un SMS WAP/Push a più destinatari
897 *
898 * @param array Array di numeri telefonici con prefisso internazionale (es. +393201234567)
899 * @param string Testo del messaggio
900 * @param string Collegamento ipertestuale
901 *
902 * @returns string Elenco di risposte ricevute dal gateway ("OK ..." o "KO ..."), separate da caratteri di "a capo" (\n)
903 */
904 function sendMultiSms($rcpts, $title, $url)
905 {
906 if (!is_array($rcpts))
907 return $this->sendSms($rcpts, $text);
908
909 $ret = array();
910 foreach($rcpts as $rcpt)
911 $ret[] = $this->sendSms($rcpt, $title, $url);
912
913 return join("\n", $ret);
914 }
915
916 /**
917 * Converti stringa in formato wbxml
918 *
919 * @access private
920 */
921 function str2wbxml($str)
922 {
923 return '03'.mobytSms::stringToOTA($str).'00';
924 }
925 }
926
927
928
929
930 /**
931 * Classe per l'invio di suonerie in formato RTTTL tramite il servizio SMS-Web
932 *
933 * Le impostazioni utilizzate di default sono:
934 * - Mittente: <b>"MobytSms"</b>
935 * - Autenticazione: <b>MD5</b>
936 * - Qualità: <b>Alta</b>
937 *
938 * @package Mobyt-SmsWeb
939 * @example rtttl-single.php Invio di una suoneria RTTTL con autenticazione MD5
940 */
941 class mobytRTTTL extends mobytSms
942 {
943 /**#@+
944 * @access private
945 * @var string
946 */
947 var $data = '';
948 /**#@-*/
949
950 /**
951 * Invia una suoneria in formato RTTTL
952 *
953 * @param string Numero telefonico con prefisso internazionale (es. +393201234567)
954 * @param string Suoneria in formato RTTTL
955 *
956 * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ...")
957 *
958 * @example rtttl-single.php Invio di una suoneria RTTTL con autenticazione MD5
959 */
960 function sendSms($rcpt, $rttl)
961 {
962 $this->setQualityHigh();
963 $this->setOperationRing();
964
965 if ($rttl)
966 $this->setRtttl($rttl);
967
968 return parent::sendSms($rcpt, $this->data);
969 }
970
971 /**
972 * Invia una suoneria in formato RTTTL a più destinatari
973 *
974 * @param array Array di numeri telefonici con prefisso internazionale (es. +393201234567)
975 * @param string Suoneria in formato RTTTL
976 *
977 * @returns string Elenco di risposte ricevute dal gateway ("OK ..." o "KO ..."), separate da caratteri di "a capo" (\n)
978 */
979 function sendMultiSms($rcpts, $rtttl)
980 {
981 if (!is_array($rcpts))
982 return $this->sendSms($rcpts, $text);
983
984 $this->setRtttl($rttl);
985
986 $ret = array();
987 foreach($rcpts as $rcpt)
988 $ret[] = $this->sendSms($rcpt, '');
989
990 return join("\n", $ret);
991 }
992
993 /**
994 * Imposta dati OTA da RTTTL
995 *
996 * @access private
997 */
998 function setRtttl($rtttl)
999 {
1000 $this->data = $this->httpPost(array('rtttl' => $rtttl), '/sms-gw/getota');
1001 }
1002 }
1003
1004 /**
1005 * Classe per l'utilizzo del servizio MNC (Mobyt Number Check)
1006 *
1007 * Le impostazioni utilizzate di default sono:
1008 * - Autenticazione: <b>MD5</b>
1009 *
1010 * @package Mobyt-SmsWeb
1011 * @example mnc-query.php Richiesta di un controllo
1012 */
1013 class mobytMnc
1014 {
1015 /**#@+
1016 * @access private
1017 * @var string
1018 */
1019 var $login;
1020 var $pwd;
1021 var $auth = MOBYT_AUTH_MD5;
1022 /**#@-*/
1023
1024 /**
1025 * @param string Username di accesso (Login)
1026 * @param string Password dispositiva
1027 */
1028 function mobytMnc($login, $pwd)
1029 {
1030 $this->login = $login;
1031 $this->pwd = $pwd;
1032 }
1033
1034
1035 /**
1036 * Utilizza l'autenticazione di tipo MD5
1037 */
1038 function setAuthMd5()
1039 {
1040 $this->auth = MOBYT_AUTH_MD5;
1041 }
1042
1043 /**
1044 * Utilizza l'autenticazione con password in chiaro basata sull'IP
1045 */
1046 function setAuthPlain()
1047 {
1048 $this->auth = MOBYT_AUTH_PLAIN;
1049 }
1050
1051
1052 /**
1053 * Richiedi controllo asincrono per uno o più numeri
1054 *
1055 * @param array Numeri per il quale richiedere il controllo
1056 * @param string URL al quale ricevere la risposta
1057 * @param string Identificativo query
1058 *
1059 * @returns string Risposta ricevuta dal gateway
1060 */
1061 function queryBatch($numbers, $url, $queryid = '1')
1062 {
1063 $numbers = join(',', $numbers);
1064
1065 $fields = array(
1066 'id' => $this->login,
1067 'password' => $this->auth == MOBYT_AUTH_MD5 ? '' : $this->pwd,
1068 'numbers' => $numbers,
1069 'url' => $url,
1070 'queryid' => $queryid
1071 );
1072
1073 if ($this->auth == MOBYT_AUTH_MD5)
1074 $fields['ticket'] = md5($this->login.$numbers.$url.$queryid.$this->pwd);
1075 else
1076 $fields['ticket'] = '';
1077
1078 $ret = MobytSms::httpPost($fields, '/sms-gw/mncbatch');
1079
1080 return trim($ret);
1081 }
1082 }
1083
1084
1085 /**
1086 * Classe per l'invio di MMS
1087 *
1088 * L'invio di MMS richede l'utilizzo della classe NuSOAP, distribuita sotto licenza GNU Lesser
1089 * Public License (LGPL). Il file lib-nusoap.inc.php deve essere copiato nella stessa
1090 * directory di lib-mobytsms.inc.php per il corretto funzionamento del servizio.
1091 *
1092 * Le impostazioni utilizzate di default sono:
1093 * - Autenticazione: <b>MD5</b>
1094 *
1095 * @package Mobyt-SmsWeb
1096 * @example mms-single.php Invio di un MMS con immagine
1097 * @example mms-multi.php Invio multiplo di MMS con immagine e audio
1098 * @example mms-recv.php Ricezione MMS
1099 */
1100 class mobytMms
1101 {
1102 /**
1103 * @param string Eventuale messaggio di errore
1104 */
1105 var $errorMessage = '';
1106
1107 /**#@+
1108 * @access private
1109 * @var string
1110 */
1111 var $login;
1112 var $pwd;
1113 var $auth = MOBYT_AUTH_MD5;
1114 /**#@-*/
1115
1116
1117 /**
1118 * @param string Username di accesso (Login)
1119 * @param string Password dispositiva
1120 */
1121 function mobytMms($login, $pwd)
1122 {
1123 $this->login = $login;
1124 $this->pwd = $pwd;
1125 }
1126
1127 /**
1128 * Utilizza l'autenticazione di tipo MD5
1129 */
1130 function setAuthMd5()
1131 {
1132 $this->auth = MOBYT_AUTH_MD5;
1133 }
1134
1135 /**
1136 * Utilizza l'autenticazione con password in chiaro basata sull'IP
1137 */
1138 function setAuthPlain()
1139 {
1140 $this->auth = MOBYT_AUTH_PLAIN;
1141 }
1142
1143 /**
1144 * Ricevi singolo MMS
1145 *
1146 *
1147 * @param string mmsid (Id del messaggio precedentemente ricevuto dal gagteway
1148 * MMS durante la fase di alert (fase 1)
1149 * vedere la documentazione relativa al servizio di ricezione MMS) (opzionale)
1150 *
1151 * @returns array contiene la struttura dell'MMS.
1152 *
1153 * Esempio di struttura ritornata (MMS inviato da un nokia N70) :
1154 *
1155 *
1156 * Array
1157 * (
1158 * [phone] => +392222222
1159 * [subject] => prova mms
1160 * [timestamp] => 2007-09-12 11:13:24
1161 * [attachments] => Array
1162 * (
1163 * [0] => Array
1164 * (
1165 * [type] => image/jpeg
1166 * [filename] => 10082007.jpg
1167 * [content] =
1168 * /9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoH....
1169 *
1170 * )
1171 *
1172 * [1] => Array
1173 * (
1174 * [type] => application/smil
1175 * [filename] => <1785700435>
1176 * [content] => <smil><head><layout><root-layout width="176"
1177 * height="208"/><region id="Image" width="160" height="120" top="68"
1178 * left="8" fit="meet"/><region id="Text" width="160" height="58"
1179 * top="5" left="8" fit="scroll"/></layout></head><body><par
1180 * dur="5000ms"><img region="Image" src="10082007.jpg"/><text
1181 * region="Text" src="001prova.txt"/></par></body></smil>
1182 *
1183 * )
1184 *
1185 * [2] => Array
1186 * (
1187 * [type] => text/plain
1188 * [filename] => 001prova.txt
1189 * [content] => 001prova invio mms
1190 *
1191 * )
1192 *
1193 * )
1194 *
1195 * )
1196 *
1197 *
1198 *
1199 */
1200
1201 function recvMms($mmsid = "")
1202 {
1203 require_once('./lib-nusoap.inc.php');
1204
1205 $client = new soapclient('http://mmsgw.mobyt.it/mms-soap/?wsdl', true);
1206
1207 $err = $client->getError();
1208 if ($err)
1209 trigger_error('Errore nella creazione del client SOAP: '.$err, E_USER_ERROR);
1210
1211 $res = $client->call('recvMms', array(
1212 $this->login,
1213 $this->pwd,
1214 ));
1215
1216 if ($client->fault) {
1217 return join(' ', $res);
1218 }
1219 else if ($err) {
1220 trigger_error('Errore la call di recvMms: '.$err, E_USER_ERROR);
1221 }
1222 else {
1223 return $res;
1224 }
1225
1226 }
1227
1228
1229 /**
1230 * Invia singolo MMS
1231 *
1232 * @param string Oggetto dell'MMS
1233 * @param string Testo dell'MMS
1234 * @param mixed Numero telefonico con prefisso internazionale (es. +393201234567), o array di numeri telefonici in caso di invio multiplo
1235 * @param string Percorso del file immagine (opzionale)
1236 * @param string Percorso del file audio (opzionale)
1237 *
1238 * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ...")
1239 */
1240 function sendMms($subject, $text, $rcpt, $image = '', $sound = '')
1241 {
1242 require_once('./lib-nusoap.inc.php');
1243
1244 if (is_array($rcpt))
1245 $rcpt = join(',', $rcpt);
1246
1247 $params = array(
1248 'id' => $this->login,
1249 'password' => $this->auth == MOBYT_AUTH_MD5 ? '' : $this->pwd,
1250 'ticket' => $this->auth == MOBYT_AUTH_MD5 ? md5($this->login.$rcpt.$subject.$text.$this->pwd) : '',
1251 'subject' => $subject,
1252 'text' => $text,
1253 'rcpt' => $rcpt,
1254 'img' => '',
1255 'snd' => ''
1256 );
1257
1258 if ($image)
1259 {
1260 $imagedata = file_get_contents($image) or trigger_error('Immagine non trovata', E_USER_ERROR);
1261
1262 $params['img'] = base64_encode($imagedata);
1263 }
1264
1265 if ($sound)
1266 {
1267 $sounddata = file_get_contents($sound) or trigger_error('File audio non trovato', E_USER_ERROR);
1268
1269 $params['snd'] = base64_encode($sounddata);
1270 }
1271
1272
1273 $client = new soapclient('http://www.mobyt.it/sms-gw/?wsdl', true);
1274
1275 if ($err = $client->getError())
1276 trigger_error('Errore nella creazione del client SOAP: '.$err, E_USER_ERROR);
1277
1278 $res = $client->call('sendMms', array_values($params));
1279
1280 if ($client->fault)
1281 return join(' ', $res);
1282
1283 return 'OK '.$res;
1284 }
1285 }
1286
1287 ?>
Documentation generated on Fri, 21 Dec 2007 12:16:53 +0100 by phpDocumentor 1.2.3