<?php
/**
 * SMS sender class based on serwersms.pl
 */
class SMS{
	var $login = null;
	var $password = null;
	var $mobile = Array( /* Add any other 4-digit prefix of mobile numbers if unactual */
	'4850', '4851', '4860', '4866', '4869', '4872', '4878', '4879', '4888' );
	function __construct( $login, $password ){
		$this->login = $login;
		$this->password = $password;
	}
	function sendSMS( $number, $content, $time = null, $nadawca = null, $test = false ){
		$resultXML = true;
		$number = preg_replace( '/[^0-9]/', '', $number );
		$number = preg_replace( '/^0+/', '', $number );
		if( strlen( $number ) == 9 && is_numeric( $number ) ){
			$number = '+48' . $number;
		} elseif( strlen( $number ) == 11 && is_numeric( $number ) ){
			$number = '+' . $number;
		} else{
			$resultXML = false;
		}
		if( !in_array( substr( $number, 1, 4 ), $this->mobile ) ) $resultXML = false;
		if( empty( $content ) ) $resultXML = false;
		$content = change2latin( $content );
		if( $resultXML !== false ){
			$resultXML = http::httpPost( 'www.api1.serwersms.pl', 80, '/zdalnie/index.php', 'akcja=wyslij_sms&login=' . $this->login . '&haslo=' . $this->password . '&numer=' . $number . '&wiadomosc=' . $content . ( $test ? '&test=1' : null ) . ( isset( $time ) ? ( '&data_wysylki=' . $time ) : null ) . ( isset( $nadawca ) ? ( '&nadawca=' . $nadawca ) : null ) . '&system=sellsmart' );
		}
		if( $GLOBALS['config']['sms_debug'] ) file_put_contents( 'logs/sms.log', var_export( Array( 'number' => $number, 'content' => $content, 'time' => $time, 'nadawca' => $nadawca, 'response' => $resultXML ), true ), FILE_APPEND );
		return $resultXML;
	}
	function sendSMS_delay( $number, $content ){
		global $config;
		if( ( date( 'H' ) >= $config['sms_send_min_hour'] ) && ( date( 'H' ) < $config['sms_send_max_hour'] ) ){
			return $this->sendSMS( $number, $content , null, $config['sms_sender_name'] );
		} else if( ( date( 'H' ) < $config['sms_send_min_hour'] ) ){
			return $this->sendSMS( $number, $content, date( 'Y-m-d ' ) . str_repeat( '0', 2 - strlen( $config['sms_send_min_hour'] ) ) . $config['sms_send_min_hour'] . ':00', $config['sms_sender_name'] );
		} else{
			return $this->sendSMS( $number, $content, date( 'Y-m-d ', time( ) + 86400 ) . str_repeat( '0', 2 - strlen( $config['sms_send_min_hour'] ) ) . $config['sms_send_min_hour'] . ':00', $config['sms_sender_name'] );
		}
	}
}
