Example #1
0
 public function __construct()
 {
     self::setLogger(new Logs());
     $logTypes = new logTypes();
     $err = $logTypes->getRow(array('where' => '`name`="error"'));
     $notice = $logTypes->getRow(array('where' => '`name`="notice"'));
     $warn = $logTypes->getRow(array('where' => '`name`="warning"'));
     $types = (array) array('error' => $err, 'notice' => $notice, 'warning' => $warn);
     self::setTypes($types);
     $logUrgency = new LogUrgency();
     $low = $logUrgency->getRow(array('where' => '`name`="low"'));
     $med = $logUrgency->getRow(array('where' => '`name`="medium"'));
     $high = $logUrgency->getRow(array('where' => '`name`="high"'));
     $urg = (array) array('low' => $low, 'medium' => $med, 'high' => $high);
     self::setUrgency($urg);
 }
 /** Email::send()
  * @return true on success false otherwise
  */
 public function send()
 {
     // error checking
     if (!$this->_body) {
         $this->_errors[] = "Cannot send out email as no email body has been defined.";
     }
     if (!$this->_from) {
         $this->_from = (array) array("Manline group" => "*****@*****.**");
     }
     if (!$this->_to) {
         $this->_errors[] = "Cannot send out email as no recipients have been defined.";
     }
     // assemble the data array
     $data = $this->_assemble();
     if ($this->_test) {
         echo '<pre style="font-family:verdana, arial;font-size:15px;">';
         echo 'class_vars: <br />';
         echo '<br />from: ';
         print_r($this->_from);
         echo 'to: ';
         print_r($this->_to);
         echo '<br />subject: ';
         print_r($this->_subject);
         echo '<br />attachments: ';
         print_r($this->_attachments);
         echo '<br />headers: ';
         print_r($this->_headers);
         echo '<br />data<br />';
         print_r($data);
         echo '</pre>';
         return;
     }
     if ($this->_errors) {
         return $this->_errors;
     }
     if (mail($data['to'], $this->_subject, $data['message'], $data['headers']) === false) {
         // do some logging
         $logTypes = new logTypes();
         $logUrgency = new LogUrgency();
         $err = $logTypes->getRow(array('where' => '`name`="error"'));
         $med = $logUrgency->getRow(array('where' => '`name`="medium"'));
         $data = (array) array();
         $data["log_typeid"] = $err['id'];
         $data["log_urgencyid"] = $med['id'];
         $data["title"] = "Could not successfully send out email";
         $data["userid"] = $_SESSION['userid'];
         $data["message"] = implode("\n", $data);
         $data["file"] = __FILE__;
         $data["update_at"] = $_SERVER['REQUEST_TIME'];
         $data["create_at"] = $_SERVER['REQUEST_TIME'];
         $data["url"] = $_SERVER['REQUEST_URI'];
         if ($this->_log->create($data) === false) {
             throw new man_exception("Could not successfully query the database");
         }
         return false;
     }
     return true;
 }
 public function send()
 {
     $logTypes = new logTypes();
     $logUrgency = new LogUrgency();
     $err = $logTypes->getRow(array('where' => '`name`="error"'));
     $warn = $logTypes->getRow(array('where' => '`name`="warning"'));
     $med = $logUrgency->getRow(array('where' => '`name`="medium"'));
     $url = (string) 'http://bulksms.2way.co.za/eapi/submission/';
     $url .= strlen($this->_body) > 160 ? 'send_batch/1/1.0' : 'send_sms/2/2.0';
     $port = 80;
     $fields = (string) '';
     $post_fields = (array) array('username' => $this->_user, 'password' => $this->_pass);
     if (strlen($this->_body) > 160) {
         $messagearray = str_split($this->_body, 155);
         $post_fields['batch_data'] = (string) 'msisdn,message' . '~';
         if (is_array($this->_to)) {
             foreach ($this->_to as $value) {
                 foreach ($messagearray as $line) {
                     if (parent::formatCellNumber($value)) {
                         $post_fields['batch_data'] .= '"' . parent::formatCellNumber($value) . '","' . $line . '"~';
                     }
                 }
             }
         } else {
             foreach ($messagearray as $line) {
                 if (parent::formatCellNumber($this->_to)) {
                     $post_fields['batch_data'] .= '"' . parent::formatCellNumber($this->_to) . '","' . $line . '"~';
                 }
             }
         }
         $post_fields['batch_data'] = rtrim($post_fields['batch_data'], '~');
     } else {
         $post_fields['message'] = $this->_body;
         if (is_array($this->_to)) {
             $post_fields['msisdn'] = (string) '';
             foreach ($this->_to as $value) {
                 if (parent::formatCellNumber($value)) {
                     $post_fields['msisdn'] .= parent::formatCellNumber($value) . ',';
                 }
             }
             $post_fields['msisdn'] = rtrim($post_fields['msisdn'], ',');
         } else {
             if (parent::formatCellNumber($this->_to)) {
                 $post_fields['msisdn'] = parent::formatCellNumber($this->_to);
             }
         }
     }
     foreach ($post_fields as $key => $value) {
         if ($key == 'batch_data') {
             $split = preg_split('/~/', $value);
             $fields .= urlencode($key) . '=';
             foreach ($split as $skey => $sval) {
                 $fields .= urlencode($sval) . '%0A';
             }
             $fields = rtrim($fields, '%0A');
         } else {
             $fields .= urlencode($key) . '=' . urlencode($value) . '&';
         }
     }
     $fields = rtrim($fields, '&');
     if ($this->_test) {
         echo '<pre>number:<br />';
         print_r($this->_to);
         echo '<br />message:<br />';
         print_r($this->_body);
         echo '<br />fields:<br />';
         print_r($fields);
         echo '<br />message array:<br />';
         print_r(isset($messagearray) ? $messagearray : '');
         echo '</pre>';
         return false;
     }
     $ch = curl_init();
     //: open the curl connection/
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
     # Added by Feighen 2010-11-03 11h16
     curl_setopt($ch, CURLOPT_PORT, $port);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
     $response_string = curl_exec($ch);
     $curl_info = curl_getinfo($ch);
     if ($response_string === false) {
         // do some logging
         $data = (array) array();
         $data["log_typeid"] = $err['id'];
         $data["log_urgencyid"] = $med['id'];
         $data["title"] = "Could not successfully execute cURL statement";
         $data["userid"] = $_SESSION['userid'];
         $data["message"] = preg_replace("/'/", "\\'", "cURL returned the following error: " . curl_error($ch));
         $data["file"] = __FILE__;
         $data["update_at"] = $_SERVER['REQUEST_TIME'];
         $data["create_at"] = $_SERVER['REQUEST_TIME'];
         $data["url"] = $_SERVER['REQUEST_URI'];
         if ($this->_log->create($data) === false) {
             throw new man_exception("Could not successfully query the database");
         }
         return false;
     } elseif ($curl_info['http_code'] != 200) {
         $data = (array) array();
         $data["log_typeid"] = $err['id'];
         $data["log_urgencyid"] = $med['id'];
         $data["title"] = "cURL statement produced an invalid return statement";
         $data["userid"] = $_SESSION['userid'];
         $data["message"] = "cURL returned the following return code: " . $curl_info['http_code'] . ". It needs to be a 200 for a successful transfer";
         $data["file"] = __FILE__;
         $data["update_at"] = $_SERVER['REQUEST_TIME'];
         $data["create_at"] = $_SERVER['REQUEST_TIME'];
         $data["url"] = $_SERVER['REQUEST_URI'];
         if ($this->_log->create($data) === false) {
             throw new man_exception("Could not successfully query the database");
         }
         return false;
     } else {
         $result = split('\\|', $response_string);
         if (count($result) != 3) {
             $data = (array) array();
             $data["log_typeid"] = $warn['id'];
             $data["log_urgencyid"] = $med['id'];
             $data["title"] = "Incorrect parameter count";
             $data["userid"] = $_SESSION['userid'];
             $data["message"] = "A split of the cURL response string returned " . count($result) . ". A total of three is required";
             $data["file"] = __FILE__;
             $data["update_at"] = $_SERVER['REQUEST_TIME'];
             $data["create_at"] = $_SERVER['REQUEST_TIME'];
             $data["url"] = $_SERVER['REQUEST_URI'];
             if ($this->_log->create($data) === false) {
                 throw new man_exception("Could not successfully query the database");
             }
             return false;
         } else {
             if ($result[0] == '0') {
                 return true;
             } else {
                 $data = (array) array();
                 $data["log_typeid"] = $warning['id'];
                 $data["log_urgencyid"] = $med['id'];
                 $data["title"] = "Parameter one of the return from cURL is incorrect";
                 $data["userid"] = $_SESSION['userid'];
                 $data["message"] = "Parameter one of the cURL return should be zero. It returned " . $result[0] . " and " . $result[1] . ".";
                 $data["file"] = __FILE__;
                 $data["update_at"] = $_SERVER['REQUEST_TIME'];
                 $data["create_at"] = $_SERVER['REQUEST_TIME'];
                 $data["url"] = $_SERVER['REQUEST_URI'];
                 if ($this->_log->create($data) === false) {
                     throw new man_exception("Could not successfully query the database");
                 }
                 return false;
             }
         }
     }
     curl_close($ch);
     //: close the curl connection
 }