Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * SMS Gateway handler class
- *
- * @author weDevs
- */
- class WeDevs_SMS_Gateways {
- private static $_instance;
- /**
- * Gateway slug
- *
- * @param string $provider name of the gateway
- */
- function __construct() {
- add_filter( 'wedevs_sms_via_smsglobal', array($this, 'smsGlobalAPI') );
- add_filter( 'wedevs_sms_via_clickatell', array($this, 'clickatellAPI') );
- add_filter( 'wedevs_sms_via_twillo', array($this, 'twilio_api') );
- add_filter( 'wedevs_sms_via_nexmo', array($this, 'nexmo_api') );
- }
- public static function instance() {
- if ( !self::$_instance ) {
- self::$_instance = new WeDevs_SMS_Gateways();
- }
- return self::$_instance;
- }
- /**
- * Get all sms gateways
- *
- * @return array
- */
- function get_gateways() {
- $gateways = array(
- 'clickatell' => array('label' => 'Clickatell'),
- 'smsglobal' => array('label' => 'SMSGlobal'),
- 'nexmo' => array('label' => 'Nexmo'),
- 'twillo' => array('label' => 'Twilio'),
- );
- return apply_filters( 'wedevs_sms_gateways', $gateways );
- }
- /**
- * Check for sms send throttleing
- * Users should not request for sms frquently
- *
- * @return bool false means not send sms now
- */
- function check_throttle() {
- $offset = (int) wedevs_sms_get_option( 'sms_throttle_offset' ); //minutes
- $sms_throttle = wedevs_sms_get_option( 'sms_throttle' );
- //not enabled? bail out
- if ( $sms_throttle != 'on' ) {
- return true;
- }
- //check users
- if ( is_user_logged_in() ) {
- $last_sent = get_user_meta( get_current_user_id(), 'sms_last_sent', true );
- } else {
- $last_sent = isset( $_COOKIE['sms_last_sent'] ) ? $_COOKIE['sms_last_sent'] : 1;
- }
- if ( $last_sent ) {
- $last_sent = strtotime( $last_sent ) + $offset * 60;
- if ( (time() - $last_sent) > 0 ) {
- return true;
- }
- }
- return false;
- }
- /**
- * Set last sms sent time
- */
- function set_last_sent() {
- $last_sent = current_time( 'mysql' );
- if ( is_user_logged_in() ) {
- update_user_meta( get_current_user_id(), 'sms_last_sent', $last_sent );
- } else {
- setcookie( 'sms_last_sent', $last_sent, time() + 86400, '/' );
- }
- }
- function send( $to ) {
- $active_gateway = wedevs_sms_get_option( 'active_gateway' );
- if ( empty( $active_gateway ) ) {
- $response = array(
- 'success' => false,
- 'message' => 'No active gateway found'
- );
- return $response;
- }
- $code = rand( 1000, 9999 );
- $sms_text = wedevs_sms_get_option( 'sms_text' );
- $sms_text = str_replace( '%CODE%', $code, $sms_text );
- $sms_data = array('text' => $sms_text, 'to' => $to, 'code' => $code);
- //check for simultaneous user request
- if ( !$this->check_throttle() ) {
- /* validation failed */
- $offset = (int) wedevs_sms_get_option( 'sms_throttle_offset' ); //minutes
- $status = array(
- 'success' => false,
- 'message' => sprintf( __( 'Please try after %d minutes again', 'wedevs' ), $offset )
- );
- if ( $offset >= 60 ) {
- $status['message'] = sprintf( __( 'Please try after %d hour(s) again', 'wedevs' ), $offset / 60 );
- }
- } else {
- //firing the hook to listerns
- $status = apply_filters( 'wedevs_sms_via_' . $active_gateway, $sms_data );
- }
- //set last sms sent time
- if ( $status['success'] == true ) {
- $this->set_last_sent( $status );
- }
- return $status;
- }
- /**
- * Sends SMS via SMSGlobal api
- *
- * @uses `wedevs_sms_via_smsglobal` filter to fire
- *
- * @param array $sms_data
- * @return boolean
- */
- function smsGlobalAPI( $sms_data ) {
- $response = array(
- 'success' => false,
- 'message' => wedevs_sms_get_option( 'sms_sent_error' )
- );
- $username = wedevs_sms_get_option( 'smsglobal_username' );
- $password = wedevs_sms_get_option( 'smsglobal_pass' );
- $from = wedevs_sms_get_option( 'sender_name' );
- //bail out if no username or password given
- if ( empty( $username ) || empty( $password ) ) {
- return $response;
- }
- $content = 'action=sendsms' .
- '&user=' . rawurlencode( $username ) .
- '&password=' . rawurlencode( $password ) .
- '&to=' . rawurlencode( $sms_data['to'] ) .
- '&from=' . rawurlencode( $from ) .
- '&text=' . rawurlencode( $sms_data['text'] );
- $smsglobal_response = file_get_contents( 'http://www.smsglobal.com.au/http-api.php?' . $content );
- //Sample Response
- //OK: 0; Sent queued message ID: 04b4a8d4a5a02176 SMSGlobalMsgID:6613115713715266
- //ERROR: 8 - Invalid Mobile Number
- //ERROR 13 - Invalid Mobile Number
- $explode_response = explode( 'SMSGlobalMsgID:', $smsglobal_response );
- if ( count( $explode_response ) == 2 ) {
- $response = array(
- 'success' => true,
- 'code' => $sms_data['code'],
- 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
- );
- }
- return $response;
- }
- /**
- * Sends SMS via Clickatell api
- *
- * @uses `wedevs_sms_via_clickatell` filter to fire
- *
- * @param type $sms_data
- * @return boolean
- */
- function clickatellAPI( $sms_data ) {
- $response = array(
- 'success' => false,
- 'message' => wedevs_sms_get_option( 'sms_sent_error' )
- );
- $username = wedevs_sms_get_option( 'clickatell_username' );
- $password = wedevs_sms_get_option( 'clickatell_pass' );
- $api_key = wedevs_sms_get_option( 'clickatell_api' );
- //bail out if nothing provided
- if ( empty( $username ) || empty( $password ) || empty( $api_key ) ) {
- return $response;
- }
- // https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=%s&content=%s', $sms_data['to'], $sms_data['code']
- // https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=40786038877&content=Cod validare cont evaluari.net:+text
- // https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=0786038877&content=Your verification code is: 6692
- // auth call
- $baseurl = "http://api.clickatell.com";
- $url = sprintf( '%s/http/auth?user=%s&password=%s&api_id=%s', $baseurl, $username, $password, $api_key );
- // do auth call
- $ret = file( $url );
- // explode our response. return string is on first line of the data returned
- $sess = explode( ":", $ret[0] );
- if ( $sess[0] == "OK" ) {
- $sess_id = trim( $sess[1] ); // remove any whitespace
- // $url = sprintf( '%s/http/sendmsg?session_id=%s&to=%s&text=%s', $baseurl, $sess_id, $sms_data['to'], $sms_data['text'] );
- $url = sprintf( 'https://platform.clickatell.com/messages/http/send?apiKey=RVoGQ21ERq-NAsiDchjILA==&to=%s&content=%s', urlencode($sms_data['to']), urlencode($sms_data['text'] ));
- // do sendmsg call
- $ret = file( $url );
- $send = explode( ":", $ret[0] );
- if ( $send[0] == "ID" ) {
- $response = array(
- 'success' => true,
- 'code' => $sms_data['code'],
- 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
- );
- }
- }
- return $response;
- }
- /**
- * Sends SMS via Nexmo api
- *
- * @uses `wedevs_sms_via_nexmo` filter to fire
- *
- * @param type $sms_data
- * @return boolean
- */
- function nexmo_api( $sms_data ) {
- $response = array(
- 'success' => false,
- 'message' => wedevs_sms_get_option( 'sms_sent_error' )
- );
- $username = wedevs_sms_get_option( 'nexmo_username' );
- $password = wedevs_sms_get_option( 'nexmo_pass' );
- $from = wedevs_sms_get_option( 'sender_name' );
- $url = 'http://rest.nexmo.com/sms/json';
- $args = array(
- 'body' => array(
- 'username' => $username,
- 'password' => $password,
- 'from' => $from,
- 'to' => $sms_data['to'],
- 'type' => 'text',
- 'text' => $sms_data['text']
- )
- );
- $response_obj = wp_remote_post( $url, $args );
- if ( !is_wp_error( $response_obj ) ) {
- $api_response = json_decode( wp_remote_retrieve_body( $response_obj ) );
- //success
- if ( $api_response->messages[0]->status == '0' ) {
- $response = array(
- 'success' => true,
- 'code' => $sms_data['code'],
- 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
- );
- }
- }
- return $response;
- }
- /**
- * Sends SMS via Twillo api
- *
- * @uses `wedevs_sms_via_twillo` filter to fire
- *
- * @param type $sms_data
- * @return boolean
- */
- function twilio_api( $sms_data ) {
- $response = array(
- 'success' => false,
- 'message' => wedevs_sms_get_option( 'sms_sent_error' )
- );
- $sid = wedevs_sms_get_option( 'twilio_username' );
- $token = wedevs_sms_get_option( 'twilio_pass' );
- $from = wedevs_sms_get_option( 'twilio_number' );
- require_once dirname( __FILE__ ) . '/lib/twilio/Twilio.php';
- $client = new Services_Twilio( $sid, $token );
- try {
- $message = $client->account->sms_messages->create(
- $from, '+' . $sms_data['to'], $sms_data['text']
- );
- if ( $message->status != 'failed' ) {
- $response = array(
- 'success' => true,
- 'code' => $sms_data['code'],
- 'message' => wedevs_sms_get_option( 'sms_sent_msg' )
- );
- }
- } catch (Exception $exc) {
- $response['message'] = $exc->getMessage();
- }
- return $response;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement