From 327298539589a1c4c6da8350d3e2865ac8cd76b9 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 2 Aug 2009 23:55:24 +0000 Subject: [PATCH] Added PennySMS webservice (incomplete) git-svn-id: http://svn.cleancode.org/svn/pickles@146 4d10bc64-7434-11dc-a737-d2d0f8310089 --- classes/WebService/PennySMS/Common.php | 87 ++++++++++++++++++++++++++ classes/WebService/PennySMS/Email.php | 49 +++++++++++++++ classes/WebService/PennySMS/JSON.php | 67 ++++++++++++++++++++ classes/WebService/PennySMS/XML.php | 85 +++++++++++++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 classes/WebService/PennySMS/Common.php create mode 100644 classes/WebService/PennySMS/Email.php create mode 100644 classes/WebService/PennySMS/JSON.php create mode 100644 classes/WebService/PennySMS/XML.php diff --git a/classes/WebService/PennySMS/Common.php b/classes/WebService/PennySMS/Common.php new file mode 100644 index 0000000..15bdb06 --- /dev/null +++ b/classes/WebService/PennySMS/Common.php @@ -0,0 +1,87 @@ +. + * + * @author Joshua John Sherman + * @copyright Copyright 2009 Joshua John Sherman + * @link http://phpwithpickles.org + * @license http://www.gnu.org/copyleft/lesser.html + * @package PICKLES + */ + +/** + * Common PennySMS Web Service Class + * + * This is the class that each PennySMS gateway class should be extending from. + */ +abstract class WebService_PennySMS_Common extends WebService_Common +{ + protected $variables = array(); + + /** + * Constructor + * + * Runs the parent's constructor and adds the module to the object. + */ + public function __construct(Config $config, Error $error) + { + parent::__construct($config, $error); + + $this->config = $config; + $this->error = $error; + } + + /** + * Variable Setter + * + * Loads an array full of our variables to use + */ + public function set($variable, $value) + { + $this->variables[$variable] = $value; + } + + /** + * Abstract processing function that is overloaded within the loaded gateway + */ + //public abstract function process(); + + /** + * Check Variables + * + * Checks that the variables are present and non-blank + */ + protected function checkVariables() + { + $valid = false; + + // Checks that the variables are set + if (isset($this->variables['api_key'], $this->variables['from'], $this->variables['phone'], $this->variables['message'])) + { + // Checks that the variables aren't empty + if (trim($this->variables['api_key']) != '' && trim($this->variables['from']) != '' && trim($this->variables['phone']) != '' && trim($this->variables['message']) != '') + { + $valid = true; + } + } + + return $valid; + } +} + +?> diff --git a/classes/WebService/PennySMS/Email.php b/classes/WebService/PennySMS/Email.php new file mode 100644 index 0000000..738037e --- /dev/null +++ b/classes/WebService/PennySMS/Email.php @@ -0,0 +1,49 @@ +. + * + * @author Joshua John Sherman + * @copyright Copyright 2009 Joshua John Sherman + * @link http://phpwithpickles.org + * @license http://www.gnu.org/copyleft/lesser.html + * @package PICKLES + */ + +/** + * PennySMS (via Email) Web Service + */ +class WebService_PennySMS_Email extends WebService_PennySMS_Common +{ + public function process() + { + // @todo check that API key is not null; + // @todo check that the phone is there + // @todo check that the message is <= 160 characters + + $to = 'api@pennysms.com'; + $subject = 'Text Message via PennySMS (via Email)'; + $message = 'key: ' . $this->variables['api_key'] . "\n" + . 'cell: ' . $this->variables['phone'] . "\n" + . "\n" + . substr($this->variables['message'], 0, 160); + + mail($to, $subject, $message); + } +} + +?> diff --git a/classes/WebService/PennySMS/JSON.php b/classes/WebService/PennySMS/JSON.php new file mode 100644 index 0000000..ecc9e0f --- /dev/null +++ b/classes/WebService/PennySMS/JSON.php @@ -0,0 +1,67 @@ +. + * + * @author Joshua John Sherman + * @copyright Copyright 2009 Joshua John Sherman + * @link http://phpwithpickles.org + * @license http://www.gnu.org/copyleft/lesser.html + * @package PICKLES + */ + +/** + * PennySMS (via JSON-RPC) Web Service + */ +class WebService_PennySMS_JSON extends WebService_PennySMS_Common +{ + public function process() + { + // @todo check that API key is not null; + // @todo check that the phone is there + // @todo check that the message is <= 160 characters + + $array = array( + 'method' => 'send', + 'params' => array( + (string)$this->variables['api_key'], + $this->variables['from'], + $this->variables['phone'], + addslashes(substr($this->variables['message'], 0, 160)) + ) + ); + + $json = json_encode($array); + var_dump($json); + + + $params = array( + 'http' => array( + 'method' => 'POST', + 'header' => 'Content-Type: text/json' . "\r\n", + 'content' => $json + ) + ); + + $context = stream_context_create($params); + $response = file_get_contents('http://api.pennysms.com/jsonrpc', false, $context); + + // @todo error trapping / re-runs + } +} + +?> diff --git a/classes/WebService/PennySMS/XML.php b/classes/WebService/PennySMS/XML.php new file mode 100644 index 0000000..ea31c4e --- /dev/null +++ b/classes/WebService/PennySMS/XML.php @@ -0,0 +1,85 @@ +. + * + * @author Joshua John Sherman + * @copyright Copyright 2009 Joshua John Sherman + * @link http://phpwithpickles.org + * @license http://www.gnu.org/copyleft/lesser.html + * @package PICKLES + */ + +/** + * PennySMS (via XML-RPC) Web Service + */ +class WebService_PennySMS_XML extends WebService_PennySMS_Common +{ + public function process() + { + $success = false; + + if ($this->checkVariables() === true) + { + $xml = ' + + + send + + + ' . $this->variables['api_key'] . ' + + + ' . $this->variables['from'] . ' + + + ' . $this->variables['phone'] . ' + + + ' . substr($this->variables['message'], 0, 160) . ' + + + + '; + + // Cleans up the XML before sending it + $xml = str_replace(array("\t", "\r", "\n"), '', $xml); + + $params = array( + 'http' => array( + 'method' => 'POST', + 'header' => 'Content-Type: text/xml' . "\r\n", + 'content' => $xml + ) + ); + + $context = stream_context_create($params); + $response = file_get_contents('http://api.pennysms.com/xmlrpc', false, $context); + + Logger::write('pennysms', 'SENT: ' . $xml . ' RCVD: ' . trim($response)); + + if ($response == 'OK' . "\n") + { + $success = true; + } + } + + return $success; + } +} + +?>