<?php
/**
*
* @desc This is class for SMS gateway http://litesms.net/from/partner/liaren.html
* @see http://code.google.com/p/lagger
* @author Barbushin Sergey http://www.linkedin.com/in/barbushin
*
*/
define('SMS_SENDER_LOGIN', 'somelogin');
define('SMS_SENDER_PASSWORD', 'somepassword');
class SmsSender {
protected $login;
protected $password;
public function __construct($login = SMS_SENDER_LOGIN, $password = SMS_SENDER_PASSWORD) {
$this->login = $login;
$this->password = $password;
}
protected function translit($string) {
$table = array('Ð'=>'A','Ð'=>'B','Ð'=>'V','Ð'=>'G','Ð'=>'D','Ð'=>'E','Ð'=>'YO','Ð'=>'ZH','Ð'=>'Z','Ð'=>'I','Ð'=>'J','Ð'=>'K','Ð'=>'L','Ð'=>'M','Ð'=>'N','Ð'=>'O','Ð'=>'P','Ð '=>'R','С'=>'S','Т'=>'T','У'=>'U','Ф'=>'F','Ð¥'=>'H','Ц'=>'C','Ч'=>'CH','Ш'=>'SH','Щ'=>'CSH','Ь'=>'','Ы'=>'Y','Ъ'=>'','Ð'=>'E','Ю'=>'YU','Я'=>'YA','а'=>'a','б'=>'b','в'=>'v','г'=>'g','д'=>'d','е'=>'e','Ñ'=>'yo','ж'=>'zh','з'=>'z','и'=>'i','й'=>'j','к'=>'k','л'=>'l','м'=>'m','н'=>'n','о'=>'o','п'=>'p','Ñ'=>'r','Ñ'=>'s','Ñ'=>'t','Ñ'=>'u','Ñ'=>'f','Ñ
'=>'h','Ñ'=>'c','Ñ'=>'ch','Ñ'=>'sh','Ñ'=>'csh','Ñ'=>'','Ñ'=>'y','Ñ'=>'','Ñ'=>'e','Ñ'=>'yu','Ñ'=>'ya');
return str_replace(array_keys($table), array_values($table), $string);
}
public function send($from, $to, $message, $translit = false) {
$request = array(
'action' => 'send_sms',
'phone' => $to,
'translit' => (int)$translit,
'message' => $translit ? $this->translit($message) : $message,
'from' => $from);
$response = $this->makeRequest($request);
if (preg_match('/Message_ID=(.+)$/u', $response, $m)) {
return $m[1];
}
else {
throw new SmsSender_Exception($response);
}
}
protected function makeRequest(array $request, &$requestBody = null, &$responseBody = null) {
$request['login'] = $this->login;
$request['password'] = $this->password;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://litesms.net/sms.php');
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request));
$responseBody = curl_exec($ch);
curl_close($ch);
return $responseBody;
}
}
class SmsSender_Exception extends Exception {
}