/modules/addons/iletimerkezi/smsclass.php

https://github.com/mamalhasan/iletimerkezi-toplusms-whmcs · PHP · 336 lines · 277 code · 55 blank · 4 comment · 26 complexity · 811c17f991aaaccbe9aca5090f52e4c1 MD5 · raw file

  1. <?php
  2. /* Iletimerkezi SMS Eklentisi
  3. * whmcsSMS - http://www.whmcssms.com
  4. */
  5. class iletimerkezi {
  6. public $params;
  7. public $gsmnumber;
  8. public $message;
  9. public $userid;
  10. public $sender;
  11. public $errors = array();
  12. public $logs = array();
  13. public function setGsmnumber($number){
  14. $number = preg_replace('/\D/','',$number);
  15. $number = substr($number, -10);
  16. $this->gsmnumber = $number;
  17. }
  18. public function getGsmnumber(){
  19. return $this->gsmnumber;
  20. }
  21. public function setMessage($message){
  22. $this->message = $message;
  23. }
  24. public function getMessage(){
  25. return $this->message;
  26. }
  27. public function setUserid($userid){
  28. $this->userid = $userid;
  29. }
  30. public function getUserid(){
  31. return $this->userid;
  32. }
  33. public function getParams(){
  34. $settings = $this->getSettings();
  35. $params = json_decode($settings['apiparams']);
  36. return $params;
  37. }
  38. public function getSettings(){
  39. $result = select_query("mod_iletimerkezi_settings", "*");
  40. return mysql_fetch_array($result);
  41. }
  42. function send(){
  43. if (extension_loaded("curl")) {
  44. $params = $this->getParams();
  45. $message = $this->message;
  46. $this->addLog("Params: ".json_encode($params));
  47. $this->addLog("To: ".$this->getGsmnumber());
  48. $this->addLog("Message: ".$message);
  49. $send_xml = '<?xml version="1.0" encoding="UTF-8" ?>
  50. <request>
  51. <authentication>
  52. <username>'.$params->iletimerkezi_username.'</username>
  53. <password>'.$params->iletimerkezi_password.'</password>
  54. </authentication>
  55. <order>
  56. <sender>'.$params->senderid.'</sender>
  57. <sendDateTime></sendDateTime>
  58. <message>
  59. <text><![CDATA['.$this->message.']]></text>
  60. <receipents>
  61. <number>'.$this->gsmnumber.'</number>
  62. </receipents>
  63. </message>
  64. </order>
  65. </request>';
  66. $ch = curl_init('http://api.iletimerkezi.com/v1/send-sms');
  67. curl_setopt($ch, CURLOPT_MUTE, 1);
  68. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  69. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  70. curl_setopt($ch, CURLOPT_POST, 1);
  71. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  72. curl_setopt($ch, CURLOPT_POSTFIELDS, $send_xml);
  73. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  74. $result = curl_exec($ch);
  75. curl_close($ch);
  76. // $this->addLog("Sunucudan dönen cevap: ".$result);
  77. $order_status = false;
  78. if(preg_match('/<status>(.*?)<code>(.*?)<\/code>(.*?)<message>(.*?)<\/message>(.*?)<\/status>(.*?)<order>(.*?)<id>(.*?)<\/id>(.*?)<\/order>/si', $result, $result_matches)) {
  79. $status_code = $result_matches[2];
  80. $status_message = $result_matches[4];
  81. $order_id = $result_matches[8];
  82. if($status_code == '200') {
  83. $order_status = true;
  84. $this->addLog("Message sent.");
  85. } else {
  86. $this->addLog("Mesaj gönderilemedi. Hata: $status_message");
  87. $this->addError("Mesaj gönderilirken hata oluştu. Hata: $status_message");
  88. }
  89. } else {
  90. $this->addLog("Mesaj gönderilemedi. Hata: $result");
  91. $this->addError("Mesaj gönderilirken hata oluştu. Hata: $result");
  92. }
  93. if(!$order_status){
  94. $this->saveToDb($order_id,'error',$this->getErrors(),$this->getLogs());
  95. return false;
  96. } else {
  97. $this->saveToDb($order_id,'',null,$this->getLogs());
  98. return true;
  99. }
  100. } else {
  101. $this->addLog('Curl extension sunucunuzda yuklu degil.');
  102. $this->addError('Curl extension sunucunuzda yuklu degil.');
  103. $this->saveToDb(-1,'error',$this->getErrors(),$this->getLogs());
  104. return false;
  105. }
  106. }
  107. function getBalance(){
  108. $params = $this->getParams();
  109. $balance_xml = '<?xml version="1.0" encoding="UTF-8" ?>
  110. <request>
  111. <authentication>
  112. <username>'.$params->iletimerkezi_username.'</username>
  113. <password>'.$params->iletimerkezi_password.'</password>
  114. </authentication>
  115. </request>';
  116. $ch = curl_init('http://api.iletimerkezi.com/v1/get-balance');
  117. curl_setopt($ch, CURLOPT_MUTE, 1);
  118. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  120. curl_setopt($ch, CURLOPT_POST, 1);
  121. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  122. curl_setopt($ch, CURLOPT_POSTFIELDS, $balance_xml);
  123. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  124. $result = curl_exec($ch);
  125. curl_close($ch);
  126. if(preg_match('/<status>(.*?)<code>(.*?)<\/code>(.*?)<message>(.*?)<\/message>(.*?)<\/status>(.*?)<balance>(.*?)<sms>(.*?)<\/sms>(.*?)<\/balance>/si', $result, $result_matches)) {
  127. $status_code = $result_matches[2];
  128. $status_message = $result_matches[4];
  129. $balance = $result_matches[8];
  130. }
  131. if($status_code=='200') {
  132. return $balance;
  133. } else {
  134. return $status_message;
  135. }
  136. }
  137. function getReport($msgid){
  138. $params = $this->getParams();
  139. $balance_xml = '<?xml version="1.0" encoding="UTF-8" ?>
  140. <request>
  141. <authentication>
  142. <username>'.$params->iletimerkezi_username.'</username>
  143. <password>'.$params->iletimerkezi_password.'</password>
  144. </authentication>
  145. <order>
  146. <id>'.$msgid.'</id>
  147. <page></page>
  148. <rowCount></rowCount>
  149. </order>
  150. </request>';
  151. $ch = curl_init('http://api.iletimerkezi.com/v1/get-report');
  152. curl_setopt($ch, CURLOPT_MUTE, 1);
  153. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  154. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  155. curl_setopt($ch, CURLOPT_POST, 1);
  156. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  157. curl_setopt($ch, CURLOPT_POSTFIELDS, $balance_xml);
  158. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  159. $result = curl_exec($ch);
  160. curl_close($ch);
  161. if(preg_match('/<message>(.*?)<number>(.*?)<\/number>(.*?)<status>(.*?)<\/status>(.*?)<\/message>/si', $result, $result_matches)) {
  162. $sended_number = $result_matches[2];
  163. $status_message = $result_matches[4];
  164. if($status_message=='111') {
  165. return 'success';
  166. } elseif($status_message=='110') {
  167. return '';
  168. } else {
  169. return 'error';
  170. }
  171. }
  172. return 'error';
  173. }
  174. function getHooks() {
  175. if ($handle = opendir(dirname(__FILE__).'/hooks')) {
  176. while (false !== ($entry = readdir($handle))) {
  177. if(substr($entry,strlen($entry)-4,strlen($entry)) == ".php"){
  178. $file[] = require_once('hooks/'.$entry);
  179. }
  180. }
  181. closedir($handle);
  182. }
  183. return $file;
  184. }
  185. function saveToDb($msgid,$status,$errors = null,$logs = null){
  186. $now = date("Y-m-d H:i:s");
  187. $table = "mod_iletimerkezi_messages";
  188. $values = array(
  189. "to" => $this->getGsmnumber(),
  190. "text" => $this->getMessage(),
  191. "msgid" => $msgid,
  192. "status" => $status,
  193. "errors" => $errors,
  194. "logs" => $logs,
  195. "user" => $this->getUserid(),
  196. "datetime" => $now
  197. );
  198. insert_query($table, $values);
  199. $this->addLog("Mesaj veritabanına kaydedildi");
  200. }
  201. function util_gsmnumber($number){
  202. $replacefrom = array('-', '(',')', '.', '+', ' ');
  203. $number = str_replace($replacefrom, '', $number);
  204. if (strlen($number) < 10) {
  205. $this->addLog("Numara formatı hatalı: ".$number);
  206. $this->addError("Numara formatı hatalı: ".$number);
  207. return null;
  208. }
  209. return $number;
  210. }
  211. public function addError($error){
  212. $this->errors[] = $error;
  213. }
  214. public function addLog($log){
  215. $this->logs[] = $log;
  216. }
  217. public function getErrors()
  218. {
  219. $res = '<pre><p><ul>';
  220. foreach($this->errors as $d){
  221. $res .= "<li>$d</li>";
  222. }
  223. $res .= '</ul></p></pre>';
  224. return $res;
  225. }
  226. public function getLogs()
  227. {
  228. $res = '<pre><p><strong>Sms gönderim detayı </strong><ul>';
  229. foreach($this->logs as $d){
  230. $res .= "<li>$d</li>";
  231. }
  232. $res .= '</ul></p></pre>';
  233. return $res;
  234. }
  235. function checkHooks($hooks = null) {
  236. if($hooks == null){
  237. $hooks = $this->getHooks();
  238. }
  239. $i=0;
  240. foreach($hooks as $hook){
  241. $sql = "SELECT `id` FROM `mod_iletimerkezi_templates` WHERE `name` = '".$hook['function']."' AND `type` = '".$hook['type']."' LIMIT 1";
  242. $result = mysql_query($sql);
  243. $num_rows = mysql_num_rows($result);
  244. if($num_rows == 0){
  245. if($hook['type']){
  246. $values = array(
  247. "name" => $hook['function'],
  248. "type" => $hook['type'],
  249. "template" => $hook['defaultmessage'],
  250. "variables" => $hook['variables'],
  251. "extra" => $hook['extra'],
  252. "description" => json_encode(@$hook['description']),
  253. "active" => 1
  254. );
  255. insert_query("mod_iletimerkezi_templates", $values);
  256. $i++;
  257. }
  258. }
  259. }
  260. return $i;
  261. }
  262. function getTemplateDetails($template = null){
  263. $where = array("name" => array("sqltype" => "LIKE", "value" => $template));
  264. $result = select_query("mod_iletimerkezi_templates", "*", $where);
  265. $data = mysql_fetch_assoc($result);
  266. return $data;
  267. }
  268. function changeDateFormat($date = null){
  269. $settings = $this->getSettings();
  270. $dateformat = $settings['dateformat'];
  271. if(!$dateformat){
  272. return $date;
  273. }
  274. $date = explode("-",$date);
  275. $year = $date[0];
  276. $month = $date[1];
  277. $day = $date[2];
  278. $dateformat = str_replace(array("%d","%m","%y"),array($day,$month,$year),$dateformat);
  279. return $dateformat;
  280. }
  281. }