Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so dont bother with any of their useless mail servers here and just use oauth login instead. Thank the nice Russians for causing that. :)

Paste

Pasted as PHP by registered user electronico_nc ( 13 years ago )
<?php

class Smg_Socket_Mmg {
 
 private $_server;
 private $_port;
 private $_url;
 private $_ssl;
 private $_timeout=30;
 private $_headers;
 private $_UserName;
 private $_Password;
 private $_SenderId;
 
 public $errorCode;
 public $errorMessage;
 
 public $MsgID;
 public $NbSMS;
 
 public function __construct($server, $port, $url, $UserName, $Password, $SenderId, $ssl=false) {
  
  $this->_server  = $server;
  $this->_port  = $port;
  $this->_url  = $url;
  $this->_ssl  = $ssl;
  $this->_UserName = $UserName;
  $this->_Password = $Password;
  $this->_SenderId = $SenderId;
 }
 
 public function setTimeout($timeout) {
  
  $this->_timeout = (int)$timeout;
  return $this;
 }
 
 public function addHeader($header) {
  
  $this->_headers .= $header . "\r\n";
  return $this;
 }
 
 public function send ( $data ) {
  
  $headers = '';
  $response = '';
  
  $data['UserName'] = $this->_UserName;
  $data['Password'] = $this->_Password;
  $data['SenderId'] = $this->_SenderId;
  
  $data = http_build_query($data);
  $data = urldecode($data);
  
  $this->addHeader("POST " . $this->_url . "  HTTP/1.1");
         $this->addHeader("Host: ".$this->_server);
         $this->addHeader("Content-length: ".strlen( $data ));
         //$this->addHeader("Content-Type: application/x-www-form-urlencoded");
  // 2013-03-21 : lox : add encoding
  $this->addHeader("Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1");
  $this->addHeader("Connection: close");
  //$sock = fsockopen&#40; 'www.google.com', 80&#41;;
  //var_export($sock); exit;
  //phpinfo(); exit;
  
  
  
  $sock = @fsockopen&#40; ($this->_ssl ? 'ssl://' : ''&#41; .  $this->_server, $this->_port, $errorCode, $errorMessage, $this->_timeout);
  // connection to server OK
  if ( $sock ) {
   stream_set_blocking($sock, false );
       stream_set_timeout($sock, $this->_timeout);
   
   fwrite($sock, $this->_headers ."\r\n");
              fwrite($sock, $data."\r\n");

   //log
   $fp = fopen&#40;'/var/log/smg/mt'.$this->MsgID, 'a+'&#41;;
            fwrite($fp, date('Y-m-d H:i:s') ."\r\n");
            fwrite($fp, $this->_headers ."\r\n");
            fwrite($fp, $data."\r\n");
            fwrite($fp, "--------------------------------------------------------------\r\n");
            fwrite($fp, $headers ."\r\n");
            fwrite($fp, $response ."\r\n");
            fclose($fp);
   // fin log
            
            
   $http_response = false;
              while ( !$http_response ) {
               $http_response = fgets($sock, 64);
              }
              
   // if response OK
   if ( strpos($http_response, "200 OK" ) ) {
    while (!feof($sock) && !strpos($headers, "\r\n\r\n") ) {
                   $headers .= fgets($sock, 128);
               }
             
               while (!feof($sock) ) {
                   $response .= fgets($sock, 128);
               }
             
               fclose($sock);
            
            $Response = $this->_parseResponse($response);

               return $Response;             
   }
   // else retrieve error code and error message
   else {
    // log
    $fp = fopen&#40;'/var/log/smg/mt'.$this->MsgID, 'a+'&#41;;
            fwrite($fp, date('Y-m-d H:i:s') ."\r\n");
             fwrite($fp, $this->_headers ."\r\n");
             fwrite($fp, $data."\r\n");
             fwrite($fp, "--------------------------------------------------------------\r\n");
             fwrite($fp, $headers ."\r\n");
             fwrite($fp, $response ."\r\n");
              fclose($fp);
    // fin log

             $this->errorCode  = '-'.substr($http_response, 9 , 3);
              $this->errorMessage = substr($http_response, 13);   
               return false;
             }
            
            // no connection to server
  } else {
   // log
   $fp = fopen&#40;'/var/log/smg/mt'.$this->MsgID, 'a+'&#41;;
           fwrite($fp, date('Y-m-d H:i:s') ."\r\n");
            fwrite($fp, $this->_headers ."\r\n");
            fwrite($fp, $data."\r\n");
            fwrite($fp, "--------------------------------------------------------------\r\n");
            fwrite($fp, $headers ."\r\n");
            fwrite($fp, $response ."\r\n");
             fclose($fp);
   // fin log

   $this->errorCode = '-98' ;
   $this->errorMessage = 'Cannot to connect to MMG';
   return false;   
  }
 }
 
 private function _parseResponse($response) {
  
  $body = preg_match("/.*<body[^>]*>(.*)<\/body>.*/si", $response, $matches);
  
  $response = trim($matches[1]);

  $lines = explode("<br>", $response);
  $lines = array_map('trim', $lines);
  
  $status = explode("=", $lines[0]);
  
  /* no status... */
  if($status[0] !== 'Status') {
   $this->errorCode = '-99';
   $this->errorMessage = 'Reponse de la MMG incorrecte';
   return false;
  }
  
  /* bad status */
  if( (int)$status[1] != 0) {
   $this->errorCode = $status[1];
   $this->errorMessage = isset($lines[1]) ? 'MMG - ' . $lines[1]: 'MMG - Unknow error';
   return false;
  }
  
  /* try to get msgID */
  $MsgID = explode("=", $lines[1]);
  if( trim($MsgID['0']) === 'MsgId' && (int)trim($MsgID['1']) ) {
   $this->MsgID = $MsgID['1'];
  }
  
  /* try to get NbSMS */
  $NbSMS = explode("=", $lines[2]);
  if( trim($NbSMS['0']) === 'NbSMS' && (int)trim($NbSMS['1']) ) {
   $this->NbSMS = $NbSMS['1'];
  }
  
  return true;
 }

}

?>

 

Revise this Paste

Your Name: Code Language: