'll', SMSMARKET_QUALITY_MQS => 'l', SMSMARKET_QUALITY_HQS => 'h', SMSMARKET_QUALITY_AUTO => 'a', SMSMARKET_QUALITY_AUTO_NY => 'n' ); /**#@-*/ /** * Classe per l'invio di SMS e il controllo del credito residuo tramite richiesta POST/GET HTTP * * Le impostazioni utilizzate di default sono: * - Mittente: "SMSMarket" * - Autenticazione: basata su IP con password inviata in chiaro * - Qualità: Non impostata - Il default è l'utilizzo della modalità automatica * * @package SMSMarket-ModuleHTTP * @example SendSingleSms.php Invio di un singolo sms */ class smsmarketSms { /**#@+ * @access private * @var string */ var $auth = SMSMARKET_AUTH_PLAIN; var $quality = SMSMARKET_QUALITY_DEFAULT; var $from; var $domain = 'http://sms.smsmarket.it'; var $login; var $pwd; var $udh; /**#@-*/ /** * @param string Username di accesso (Login) * @param string Password di accesso * @param string Intestazione mittente * * @see setFrom */ function smsmarketSms($login, $pwd, $from = 'SMSMarket') { $this->login = $login; $this->pwd = $pwd; $this->setFrom($from); } /** * Imposta intestazione mittente * * Il mittente può essere composto da un massimo di 11 caratteri alfanumerici o un numero telefonico con prefisso internazionale. * * @param string Intestazione mittente */ function setFrom($from) { $this->from = substr($from, 0, 14); } /** * Imposta l'indirizzo URL del dominio dell'amministratore/rivenditore sul quale dovranno loggarsi gli eventuali clienti * L'URL deve essere nel formato 'http://www.miodominio.it' * * @param string URL */ function setDomain($domain) { $this->domain = $domain; } /** * Utilizza l'autenticazione con password in chiaro basata sull'IP */ function setAuthPlain() { $this->auth = SMSMARKET_AUTH_PLAIN; } /** * Imposta la qualità messaggi al default dell'account */ function setQualityDefault() { $this->quality = SMSMARKET_QUALITY_DEFAULT; } /** * Imposta la qualità messaggi come bassa */ function setQualityLow() { $this->quality = SMSMARKET_QUALITY_LQS; } /** * Imposta la qualità messaggi come media */ function setQualityMedium() { $this->quality = SMSMARKET_QUALITY_MQS; } /** * Imposta la qualità messaggi come alta */ function setQualityHigh() { $this->quality = SMSMARKET_QUALITY_HQS; } /** * Imposta la qualità messaggi automatica */ function setQualityAuto() { $this->quality = SMSMARKET_QUALITY_AUTO; } /** * Imposta la qualità messaggi automatica con notifica */ function setQualityAutoNotify() { $this->quality = SMSMARKET_QUALITY_AUTO_NY; } /** * Controlla il credito disponibile intermini di credito in euro o sms disponibili. * * @returns string In caso di successo 'OK ', dove è il numero di sms o il credito in euro a seconda del * parametro type specificato.
In caso di errore 'KO * * @example ControllaSMS.php Controllo il credito residuo e i messaggi disponibili */ function getCredit($type='credit') { $fields = array( 'user' => $this->login, 'pass' => $this->pwd, ); $fields['type'] = $type ; $fields['domain'] = $this->domain; $fields['path'] = '/sms/credit.php'; return trim($this->httpPost($fields)); } /** * Invia un SMS * * 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. * * @param string Numero telefonico con prefisso internazionale (es. +393201234567) * @param string Testo del messaggio (max 160 caratteri) * @param string Identificatore univoco del messaggio da utilizzare nel caso sia richiesta la notifica * @param string Tipologia di messaggio (TEXT, WAPPUSH) * @param string Indirizzo URL cui dovrà collegarsi il destinatario in caso di SMS WAPPUSH * @param integer Se uguale a 1 verrà restituito l'identificativo univo della spedizione da utilizzare per il controllo dello * stato della spedizione tramite POST HTTP * * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ..."). In caso di successo verrà visualizzato anche il costo del messaggio inviato * * @example SendSingleSms.php Invio di un singolo sms */ function sendSms($rcpt, $text, $act='', $operation='TEXT', $url='',$return_id='') { global $smsmarket_qty, $smsmarket_ops; $fields = array( 'sender' => $this->from, 'rcpt' => $rcpt, 'data' => $text, 'user' => $this->login, 'operation' => $operation, 'url' => $url, 'return_id'=> $return_id ); if ($this->quality == SMSMARKET_QUALITY_AUTO_NY) { if ($act == '') { // Generate random act while (strlen($act) < 16) $act .= preg_replace('/[^0-9]/', '', md5(uniqid('', true))); if (strlen($act) > 20) $act = substr($act, 0, 20); } $fields['act'] = $act; } if ($this->quality != SMSMARKET_QUALITY_DEFAULT && isset($smsmarket_qty[$this->quality])) $fields['qty'] = $smsmarket_qty[$this->quality]; $fields['pass'] = $this->pwd; $fields['domain'] = $this->domain; $fields['path'] = '/sms/send.php'; return trim($this->httpPost($fields)); } /** * Invia un SMS a più destinatari * * 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. * * @example SendMultiSms.php Invio di un sms a più numeri con autenticazione tramite password in chiaro * * @param array Array di numeri telefonici con prefisso internazionale (es. +393201234567) * @param string Testo del messaggio (max 160 caratteri) * @param string Tipologia di messaggio (TEXT, WAPPUSH) * @param string Indirizzo URL cui dovrà collegarsi il destinatario in caso di SMS WAPPUSH * @param integer Se uguale a 1 verrà restituito l'identificativo univo della spedizione da utilizzare per il controllo dello * stato della spedizione tramite POST HTTP * * @returns string Elenco di risposte ricevute dal gateway ("OK ..." o "KO ..."), separate da caratteri di "a capo" (\n) */ function sendMultiSms($rcpts, $data, $operation='TEXT', $url='',$return_id='') { global $smsmarket_qty, $smsmarket_ops; if (!is_array($rcpts)) return $this->sendSms($rcpts, $data); $fields = array( 'user' => $this->login, 'pass' => $this->auth == SMSMARKET_AUTH_MD5 ? '' : $this->pwd, 'sender' => $this->from, 'data' => $data, 'operation' => $operation, 'url' => $url, 'return_id'=> $return_id ); if ($this->quality != SMSMARKET_QUALITY_DEFAULT && isset($smsmarket_qty[$this->quality])) $fields['qty'] = $smsmarket_qty[$this->quality]; $fields['domain'] = $this->domain; $fields['path']='/sms/batch.php'; $ret = array(); foreach ($rcpts as $act => $rcpt) { $fields['rcpt'] = $rcpt; if ($this->quality == SMSMARKET_QUALITY_AUTO_NY) $fields['act'] = $act; $ret[] = trim($this->httpPost($fields)); } return join("\n", $ret); } /** * Invia richiesta MNC * * @param array Array di numeri telefonici con prefisso internazionale (es. +393201234567) * @param string Testo del messaggio (max 160 caratteri) * @param integer Se uguale a 1 verrà restituito l'identificativo univo della spedizione da utilizzare per il controllo dello * stato della spedizione tramite POST HTTP * * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ..."). * * @example SendMNC.php Invio di una richiesta MNC */ function sendMNC($numbers,$return_id='') { global $smsmarket_qty, $smsmarket_ops; $fields = array( 'user' => $this->login, 'pass' => $this->auth == SMSMARKET_AUTH_MD5 ? '' : $this->pwd, 'numbers' => $numbers, 'return_id'=> $return_id ); $fields['domain'] = $this->domain; $fields['path'] = '/sms/mnc.php'; return trim($this->httpPost($fields)); } /** * Controllo satto delle spedizioni * * @param string Identificativo univoco della spedizione * @param string Tipo di report (queue, notify, mnc) * @param string Schema del report * * @returns string Risposta ricevuta dal gateway ("OK ..." o "KO ..."). * * @example ControlloStato.php Verifica stato della spedizione */ function sendStatus($id, $type, $schema='1') { global $smsmarket_qty, $smsmarket_ops; $fields = array( 'user' => $this->login, 'pass' => $this->auth == SMSMARKET_AUTH_MD5 ? '' : $this->pwd, 'id' => $id, 'type' => $type, 'schema' => $schema ); $fields['domain'] = $this->domain; $fields['path'] = '/sms/batch-status.php'; return trim($this->httpPost($fields)); } /** * Send an HTTP POST request, choosing either cURL or fsockopen * * @access private */ function httpPost($fields) { $qs = array(); foreach ($fields as $k => $v) $qs[] = $k.'='.urlencode($v); $qs = join('&', $qs); if (function_exists('curl_init')) return smsmarketSms::httpPostCurl($qs, $fields['domain'].$fields['path']); $errno = $errstr = ''; if ($fp = @fsockopen(substr($fields['domain'],7), 80, $errno, $errstr, 30)) { fputs($fp, "POST ".$fields['path']." HTTP/1.0\r\n"); fputs($fp, "Host: ".substr($fields['domain'],7)."\r\n"); fputs($fp, "User-Agent: phpSMSMarketSms/".SMSMARKET_PHPSMS_VERSION."\r\n"); fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-Length: ".strlen($qs)."\r\n"); fputs($fp, "Connection: close\r\n"); fputs($fp, "\r\n".$qs); $content = ''; while (!feof($fp)) $content .= fgets($fp, 1024); fclose($fp); return preg_replace("/^.*?\r\n\r\n/s", '', $content); } return false; } /** * Send an HTTP POST request, through cURL * * @access private */ function httpPostCurl($qs, $domain) { if ($ch = @curl_init($domain)) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'phpSMSMarketSms/'.SMSMARKET_PHPSMS_VERSION.' (curl)'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $qs); return curl_exec($ch); } return false; } } ?>