Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.73 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * SMS Gateway handler class
  5. *
  6. * @author weDevs
  7. */
  8. class WeDevs_SMS_Gateways {
  9.  
  10. private static $_instance;
  11.  
  12. /**
  13. * Gateway slug
  14. *
  15. * @param string $provider name of the gateway
  16. */
  17. function __construct() {
  18. add_filter( 'wedevs_sms_via_smsglobal', array($this, 'smsGlobalAPI') );
  19. add_filter( 'wedevs_sms_via_clickatell', array($this, 'clickatellAPI') );
  20. add_filter( 'wedevs_sms_via_twillo', array($this, 'twilio_api') );
  21. add_filter( 'wedevs_sms_via_nexmo', array($this, 'nexmo_api') );
  22. }
  23.  
  24. public static function instance() {
  25. if ( !self::$_instance ) {
  26. self::$_instance = new WeDevs_SMS_Gateways();
  27. }
  28.  
  29. return self::$_instance;
  30. }
  31.  
  32. /**
  33. * Get all sms gateways
  34. *
  35. * @return array
  36. */
  37. function get_gateways() {
  38. $gateways = array(
  39. 'clickatell' => array('label' => 'Clickatell'),
  40. 'smsglobal' => array('label' => 'SMSGlobal'),
  41. 'nexmo' => array('label' => 'Nexmo'),
  42. 'twillo' => array('label' => 'Twilio'),
  43. );
  44.  
  45. return apply_filters( 'wedevs_sms_gateways', $gateways );
  46. }
  47.  
  48. /**
  49. * Check for sms send throttleing
  50. * Users should not request for sms frquently
  51. *
  52. * @return bool false means not send sms now
  53. */
  54. function check_throttle() {
  55. $offset = (int) wedevs_sms_get_option( 'sms_throttle_offset' ); //minutes
  56. $sms_throttle = wedevs_sms_get_option( 'sms_throttle' );
  57.  
  58. //not enabled? bail out
  59. if ( $sms_throttle != 'on' ) {
  60. return true;
  61. }
  62.  
  63. //check users
  64. if ( is_user_logged_in() ) {
  65. $last_sent = get_user_meta( get_current_user_id(), 'sms_last_sent', true );
  66. } else {
  67. $last_sent = isset( $_COOKIE['sms_last_sent'] ) ? $_COOKIE['sms_last_sent'] : 1;
  68. }
  69.  
  70. if ( $last_sent ) {
  71. $last_sent = strtotime( $last_sent ) + $offset * 60;
  72. if ( (time() - $last_sent) > 0 ) {
  73. return true;
  74. }
  75. }
  76.  
  77. return false;
  78. }
  79.  
  80. /**
  81. * Set last sms sent time
  82. */
  83. function set_last_sent() {
  84. $last_sent = current_time( 'mysql' );
  85.  
  86. if ( is_user_logged_in() ) {
  87. update_user_meta( get_current_user_id(), 'sms_last_sent', $last_sent );
  88. } else {
  89. setcookie( 'sms_last_sent', $last_sent, time() + 86400, '/' );
  90. }
  91. }
  92.  
  93. function send( $to ) {
  94.  
  95. $active_gateway = wedevs_sms_get_option( 'active_gateway' );
  96.  
  97. if ( empty( $active_gateway ) ) {
  98. $response = array(
  99. 'success' => false,
  100. 'message' => 'No active gateway found'
  101. );
  102.  
  103. return $response;
  104. }
  105.  
  106. $code = rand( 1000, 9999 );
  107. $sms_text = wedevs_sms_get_option( 'sms_text' );
  108. $sms_text = str_replace( '%CODE%', $code, $sms_text );
  109. $sms_data = array('text' => $sms_text, 'to' => $to, 'code' => $code);
  110.  
  111. //check for simultaneous user request
  112. if ( !$this->check_throttle() ) {
  113. /* validation failed */
  114. $offset = (int) wedevs_sms_get_option( 'sms_throttle_offset' ); //minutes
  115.  
  116. $status = array(
  117. 'success' => false,
  118. 'message' => sprintf( __( 'Please try after %d minutes again', 'wedevs' ), $offset )
  119. );
  120.  
  121. if ( $offset >= 60 ) {
  122. $status['message'] = sprintf( __( 'Please try after %d hour(s) again', 'wedevs' ), $offset / 60 );
  123. }
  124. } else {
  125. //firing the hook to listerns
  126. $status = apply_filters( 'wedevs_sms_via_' . $active_gateway, $sms_data );
  127. }
  128.  
  129. //set last sms sent time
  130. if ( $status['success'] == true ) {
  131. $this->set_last_sent( $status );
  132. }
  133.  
  134. return $status;
  135. }
  136.  
  137. /**
  138. * Sends SMS via SMSGlobal api
  139. *
  140. * @uses `wedevs_sms_via_smsglobal` filter to fire
  141. *
  142. * @param array $sms_data
  143. * @return boolean
  144. */
  145. function smsGlobalAPI( $sms_data ) {
  146. $response = array(
  147. 'success' => false,
  148. 'message' => wedevs_sms_get_option( 'sms_sent_error' )
  149. );
  150.  
  151. $username = wedevs_sms_get_option( 'smsglobal_username' );
  152. $password = wedevs_sms_get_option( 'smsglobal_pass' );
  153. $from = wedevs_sms_get_option( 'sender_name' );
  154.  
  155. //bail out if no username or password given
  156. if ( empty( $username ) || empty( $password ) ) {
  157. return $response;
  158. }
  159.  
  160. $content = 'action=sendsms' .
  161. '&user=' . rawurlencode( $username ) .
  162. '&password=' . rawurlencode( $password ) .
  163. '&to=' . rawurlencode( $sms_data['to'] ) .
  164. '&from=' . rawurlencode( $from ) .
  165. '&text=' . rawurlencode( $sms_data['text'] );
  166.  
  167. $smsglobal_response = file_get_contents( 'http://www.smsglobal.com.au/http-api.php?' . $content );
  168.  
  169. //Sample Response
  170. //OK: 0; Sent queued message ID: 04b4a8d4a5a02176 SMSGlobalMsgID:6613115713715266
  171. //ERROR: 8 - Invalid Mobile Number
  172. //ERROR 13 - Invalid Mobile Number
  173.  
  174. $explode_response = explode( 'SMSGlobalMsgID:', $smsglobal_response );
  175.  
  176. if ( count( $explode_response ) == 2 ) {
  177. $response = array(
  178. 'success' => true,
  179. 'code' => $sms_data['code'],
  180. 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
  181. );
  182. }
  183.  
  184. return $response;
  185. }
  186.  
  187. /**
  188. * Sends SMS via Clickatell api
  189. *
  190. * @uses `wedevs_sms_via_clickatell` filter to fire
  191. *
  192. * @param type $sms_data
  193. * @return boolean
  194. */
  195. function clickatellAPI( $sms_data ) {
  196. $response = array(
  197. 'success' => false,
  198. 'message' => wedevs_sms_get_option( 'sms_sent_error' )
  199. );
  200.  
  201. $username = wedevs_sms_get_option( 'clickatell_username' );
  202. $password = wedevs_sms_get_option( 'clickatell_pass' );
  203. $api_key = wedevs_sms_get_option( 'clickatell_api' );
  204.  
  205. //bail out if nothing provided
  206. if ( empty( $username ) || empty( $password ) || empty( $api_key ) ) {
  207. return $response;
  208. }
  209.  
  210. // https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=%s&content=%s', $sms_data['to'], $sms_data['code']
  211. // https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=40786038877&content=Cod validare cont evaluari.net:+text
  212. // https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&amp;to=0786038877&amp;content=Your verification code is: 6692
  213.  
  214.  
  215.  
  216. // auth call
  217. $baseurl = "http://api.clickatell.com";
  218. $url = sprintf( '%s/http/auth?user=%s&password=%s&api_id=%s', $baseurl, $username, $password, $api_key );
  219.  
  220. // do auth call
  221. $ret = file( $url );
  222.  
  223. // explode our response. return string is on first line of the data returned
  224. $sess = explode( ":", $ret[0] );
  225. if ( $sess[0] == "OK" ) {
  226.  
  227. $sess_id = trim( $sess[1] ); // remove any whitespace
  228.  
  229. // $url = sprintf( '%s/http/sendmsg?session_id=%s&to=%s&text=%s', $baseurl, $sess_id, $sms_data['to'], $sms_data['text'] );
  230.  
  231. $url = sprintf( 'https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=%s&content=%s', urlencode($sms_data['to']), urlencode($sms_data['text'] ));
  232.  
  233.  
  234. // do sendmsg call
  235. $ret = file( $url );
  236. $send = explode( ":", $ret[0] );
  237.  
  238. if ( $send[0] == "ID" ) {
  239. $response = array(
  240. 'success' => true,
  241. 'code' => $sms_data['code'],
  242. 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
  243. );
  244. }
  245. }
  246.  
  247. return $response;
  248. }
  249.  
  250.  
  251. /**
  252. * Sends SMS via Nexmo api
  253. *
  254. * @uses `wedevs_sms_via_nexmo` filter to fire
  255. *
  256. * @param type $sms_data
  257. * @return boolean
  258. */
  259. function nexmo_api( $sms_data ) {
  260. $response = array(
  261. 'success' => false,
  262. 'message' => wedevs_sms_get_option( 'sms_sent_error' )
  263. );
  264.  
  265. $username = wedevs_sms_get_option( 'nexmo_username' );
  266. $password = wedevs_sms_get_option( 'nexmo_pass' );
  267. $from = wedevs_sms_get_option( 'sender_name' );
  268.  
  269. $url = 'http://rest.nexmo.com/sms/json';
  270. $args = array(
  271. 'body' => array(
  272. 'username' => $username,
  273. 'password' => $password,
  274. 'from' => $from,
  275. 'to' => $sms_data['to'],
  276. 'type' => 'text',
  277. 'text' => $sms_data['text']
  278. )
  279. );
  280.  
  281. $response_obj = wp_remote_post( $url, $args );
  282.  
  283. if ( !is_wp_error( $response_obj ) ) {
  284. $api_response = json_decode( wp_remote_retrieve_body( $response_obj ) );
  285.  
  286. //success
  287. if ( $api_response->messages[0]->status == '0' ) {
  288. $response = array(
  289. 'success' => true,
  290. 'code' => $sms_data['code'],
  291. 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
  292. );
  293. }
  294. }
  295.  
  296. return $response;
  297. }
  298.  
  299. /**
  300. * Sends SMS via Twillo api
  301. *
  302. * @uses `wedevs_sms_via_twillo` filter to fire
  303. *
  304. * @param type $sms_data
  305. * @return boolean
  306. */
  307. function twilio_api( $sms_data ) {
  308. $response = array(
  309. 'success' => false,
  310. 'message' => wedevs_sms_get_option( 'sms_sent_error' )
  311. );
  312.  
  313. $sid = wedevs_sms_get_option( 'twilio_username' );
  314. $token = wedevs_sms_get_option( 'twilio_pass' );
  315. $from = wedevs_sms_get_option( 'twilio_number' );
  316.  
  317. require_once dirname( __FILE__ ) . '/lib/twilio/Twilio.php';
  318.  
  319. $client = new Services_Twilio( $sid, $token );
  320. try {
  321. $message = $client->account->sms_messages->create(
  322. $from, '+' . $sms_data['to'], $sms_data['text']
  323. );
  324.  
  325. if ( $message->status != 'failed' ) {
  326. $response = array(
  327. 'success' => true,
  328. 'code' => $sms_data['code'],
  329. 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
  330. );
  331. }
  332. } catch (Exception $exc) {
  333. $response['message'] = $exc->getMessage();
  334. }
  335.  
  336. return $response;
  337. }
  338.  
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement