Added PennySMS webservice (incomplete)

git-svn-id: http://svn.cleancode.org/svn/pickles@146 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2009-08-02 23:55:24 +00:00
parent cc0151be61
commit 3272985395
4 changed files with 288 additions and 0 deletions

View file

@ -0,0 +1,87 @@
<?php
/**
* Common PennySMS Web Service Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @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;
}
}
?>

View file

@ -0,0 +1,49 @@
<?php
/**
* PennySMS (via Email) Web Service Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @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);
}
}
?>

View file

@ -0,0 +1,67 @@
<?php
/**
* PennySMS (via JSON-RPC) Web Service Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @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
}
}
?>

View file

@ -0,0 +1,85 @@
<?php
/**
* PennySMS (via XML-RPC) Web Service Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @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 = '
<?xml version="1.0"?>
<methodCall>
<methodName>send</methodName>
<params>
<param>
<value><string>' . $this->variables['api_key'] . '</string></value>
</param>
<param>
<value><string>' . $this->variables['from'] . '</string></value>
</param>
<param>
<value><string>' . $this->variables['phone'] . '</string></value>
</param>
<param>
<value><string>' . substr($this->variables['message'], 0, 160) . '</string></value>
</param>
</params>
</methodCall>
';
// 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 == '<?xml version="1.0" ?><methodResponse><params><param><value><string>OK</string></value></param></params></methodResponse>' . "\n")
{
$success = true;
}
}
return $success;
}
}
?>