aboutsummaryrefslogtreecommitdiffstats
path: root/include/ws_protocols
diff options
context:
space:
mode:
authorrvelices <rv-github@modusoptimus.com>2007-01-06 11:13:08 +0000
committerrvelices <rv-github@modusoptimus.com>2007-01-06 11:13:08 +0000
commitf992150313a63d30aeda6e5f3dcd64fc62a06adb (patch)
tree6295bead230ea82610b53a09a9d52b15a6991768 /include/ws_protocols
parent6314122c5d610f2fbc9fc5c60dec32f7f901a2bb (diff)
Web service first version.
git-svn-id: http://piwigo.org/svn/trunk@1698 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to 'include/ws_protocols')
-rw-r--r--include/ws_protocols/json_encoder.php90
-rw-r--r--include/ws_protocols/php_encoder.php57
-rw-r--r--include/ws_protocols/rest_encoder.php285
-rw-r--r--include/ws_protocols/rest_handler.php60
-rw-r--r--include/ws_protocols/xmlrpc_encoder.php118
5 files changed, 610 insertions, 0 deletions
diff --git a/include/ws_protocols/json_encoder.php b/include/ws_protocols/json_encoder.php
new file mode 100644
index 000000000..ffeb59954
--- /dev/null
+++ b/include/ws_protocols/json_encoder.php
@@ -0,0 +1,90 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | PhpWebGallery - a PHP based picture gallery |
+// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
+// +-----------------------------------------------------------------------+
+// | branch : BSF (Best So Far)
+// | file : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $
+// | last update : $Date: 2006-12-21 18:49:12 -0500 (Thu, 21 Dec 2006) $
+// | last modifier : $Author: rvelices $
+// | revision : $Rev: 1678 $
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation |
+// | |
+// | This program 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 |
+// | General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU General Public License |
+// | along with this program; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA. |
+// +-----------------------------------------------------------------------+
+
+
+#_____________________ PHP 5.2
+if (! function_exists('json_encode')) {
+ function json_encode($data) {
+ switch (gettype($data)) {
+ case 'boolean':
+ return ($data ? 'true' : 'false');
+ case 'null':
+ case 'NULL':
+ return 'null';
+ case 'integer':
+ case 'double':
+ return $data;
+ case 'string':
+ return '"'. str_replace(array("\\",'"',"/","\n","\r","\t"), array("\\\\",'\"',"\\/","\\n","\\r","\\t"), $data) .'"';
+ case 'object':
+ case 'array':
+ if ($data === array()) return '[]'; # empty array
+ if (range(0, count($data) - 1) !== array_keys($data) ) { # string keys, unordered, non-incremental keys, .. - whatever, make object
+ $out = "\n".'{';
+ foreach($data as $key => $value) {
+ $out .= json_encode((string) $key) . ':' . json_encode($value) . ',';
+ }
+ $out = substr($out, 0, -1) . "\n". '}';
+ }else{
+ # regular array
+ $out = "\n".'[' . join("\n".',', array_map('json_encode', $data)) ."\n".']';
+ }
+ return $out;
+ }
+ }
+}
+
+class PwgJsonEncoder extends PwgResponseEncoder
+{
+ function encodeResponse($response)
+ {
+ $respClass = strtolower( get_class($response) );
+ if ($respClass=='pwgerror')
+ {
+ return json_encode(
+ array(
+ 'stat' => 'fail',
+ 'err' => $response->code(),
+ 'message' => $response->message(),
+ )
+ );
+ }
+ parent::flattenResponse($response);
+ return json_encode(
+ array(
+ 'stat' => 'ok',
+ 'result' => $response,
+ )
+ );
+ }
+
+ function getContentType()
+ {
+ return 'text/plain';
+ }
+}
+
+?>
diff --git a/include/ws_protocols/php_encoder.php b/include/ws_protocols/php_encoder.php
new file mode 100644
index 000000000..f12df2594
--- /dev/null
+++ b/include/ws_protocols/php_encoder.php
@@ -0,0 +1,57 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | PhpWebGallery - a PHP based picture gallery |
+// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
+// +-----------------------------------------------------------------------+
+// | branch : BSF (Best So Far)
+// | file : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $
+// | last update : $Date: 2006-12-21 18:49:12 -0500 (Thu, 21 Dec 2006) $
+// | last modifier : $Author: rvelices $
+// | revision : $Rev: 1678 $
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation |
+// | |
+// | This program 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 |
+// | General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU General Public License |
+// | along with this program; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA. |
+// +-----------------------------------------------------------------------+
+
+class PwgSerialPhpEncoder extends PwgResponseEncoder
+{
+ function encodeResponse($response)
+ {
+ $respClass = strtolower( get_class($response) );
+ if ($respClass=='pwgerror')
+ {
+ return serialize(
+ array(
+ 'stat' => 'fail',
+ 'err' => $response->code(),
+ 'message' => $response->message(),
+ )
+ );
+ }
+ parent::flattenResponse($response);
+ return serialize(
+ array(
+ 'stat' => 'ok',
+ 'result' => $response
+ )
+ );
+ }
+
+ function getContentType()
+ {
+ return 'text/plain';
+ }
+}
+
+?>
diff --git a/include/ws_protocols/rest_encoder.php b/include/ws_protocols/rest_encoder.php
new file mode 100644
index 000000000..c66cf2536
--- /dev/null
+++ b/include/ws_protocols/rest_encoder.php
@@ -0,0 +1,285 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | PhpWebGallery - a PHP based picture gallery |
+// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
+// +-----------------------------------------------------------------------+
+// | branch : BSF (Best So Far)
+// | file : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $
+// | last update : $Date: 2006-12-21 18:49:12 -0500 (Thu, 21 Dec 2006) $
+// | last modifier : $Author: rvelices $
+// | revision : $Rev: 1678 $
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation |
+// | |
+// | This program 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 |
+// | General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU General Public License |
+// | along with this program; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA. |
+// +-----------------------------------------------------------------------+
+
+
+class PwgXmlWriter
+{
+ var $_indent;
+ var $_indentStr;
+
+ var $_elementStack;
+ var $_lastTagOpen;
+ var $_indentLevel;
+
+ var $_encodedXml;
+
+ function PwgXmlWriter()
+ {
+ $this->_elementStack = array();
+ $this->_lastTagOpen = false;
+ $this->_indentLevel = 0;
+
+ $this->_encodedXml = '';
+ $this->_indent = true;
+ $this->_indentStr = "\t";
+ }
+
+ function &getOutput()
+ {
+ return $this->_encodedXml;
+ }
+
+
+ function start_element($name)
+ {
+ $this->_end_prev(false);
+ if (!empty($this->_elementStack))
+ {
+ $this->_eol_indent();
+ }
+ $this->_indentLevel++;
+ $this->_indent();
+ $this->_output( '<'.$name );
+ $this->_lastTagOpen = true;
+ array_push( $this->_elementStack, $name);
+ }
+
+ function end_element($x)
+ {
+ $close_tag = $this->_end_prev(true);
+ $name = array_pop( $this->_elementStack );
+ if ($close_tag)
+ {
+ $this->_indentLevel--;
+ $this->_indent();
+// $this->_eol_indent();
+ $this->_output('</'.$name.">");
+ }
+ }
+
+ function write_content($value)
+ {
+ $this->_end_prev(false);
+ $value = (string)$value;
+ $need_cdata = (strpos($value, "\r")!==false)?true:false;
+ if ($need_cdata)
+ {
+ $this->_output( '<![CDATA[' . $value . ']]>' );
+ }
+ else
+ {
+ $this->_output( htmlspecialchars( $value ) );
+ }
+ }
+
+ function write_attribute($name, $value)
+ {
+ $this->_output(' '.$name.'="'.$this->encode_attribute($value).'"');
+ }
+
+ function encode_attribute($value)
+ {
+ return htmlspecialchars( (string)$value);
+ }
+
+ function _end_prev($done)
+ {
+ $ret = true;
+ if ($this->_lastTagOpen)
+ {
+ if ($done)
+ {
+ $this->_indentLevel--;
+ $this->_output( ' />' );
+ //$this->_eol_indent();
+ $ret = false;
+ }
+ else
+ {
+ $this->_output( '>' );
+ }
+ $this->_lastTagOpen = false;
+ }
+ return $ret;
+ }
+
+ function _eol_indent()
+ {
+ if ($this->_indent)
+ $this->_output("\n");
+ }
+
+ function _indent()
+ {
+ if ($this->_indent and
+ $this->_indentLevel > count($this->_elementStack) )
+ {
+ $this->_output(
+ str_repeat( $this->_indentStr, count($this->_elementStack) )
+ );
+ }
+ }
+
+ function _output($raw_content)
+ {
+ $this->_encodedXml .= $raw_content;
+ }
+}
+
+class PwgRestEncoder extends PwgResponseEncoder
+{
+ function encodeResponse($response)
+ {
+ global $lang_info;
+ $respClass = strtolower( get_class($response) );
+ if ($respClass=='pwgerror')
+ {
+ $ret = '<?xml version="1.0"?>
+<rsp stat="fail">
+ <err code="'.$response->code().'" msg="'.htmlspecialchars($response->message()).'" />
+</rsp>';
+ return $ret;
+ }
+
+//parent::flattenResponse($response);
+
+ $this->_writer = new PwgXmlWriter();
+ $this->encode($response);
+ $ret = $this->_writer->getOutput();
+ $ret = '<?xml version="1.0" encoding="'.$lang_info['charset'].'" ?>
+<rsp stat="ok">
+'.$ret.'
+</rsp>';
+
+ return $ret;
+ }
+
+ function getContentType()
+ {
+ return 'text/xml';
+ }
+
+ function encode_array($data, $itemName, $xml_attributes=array())
+ {
+ foreach ($data as $item)
+ {
+ $this->_writer->start_element( $itemName );
+ $this->encode($item, $xml_attributes);
+ $this->_writer->end_element( $itemName );
+ }
+ }
+
+ function encode_struct($data, $skip_underscore, $xml_attributes=array())
+ {
+ foreach ($data as $name => $value)
+ {
+ if (is_numeric($name))
+ continue;
+ if ($skip_underscore and $name[0]=='_')
+ continue;
+ if ( is_null($value) )
+ continue; // null means we dont put it
+ if ( $name==WS_XML_ATTRIBUTES)
+ {
+ foreach ($value as $attr_name => $attr_value)
+ {
+ $this->_writer->write_attribute($attr_name, $attr_value);
+ }
+ unset($data[$name]);
+ }
+ else if ( isset($xml_attributes[$name]) )
+ {
+ $this->_writer->write_attribute($name, $value);
+ unset($data[$name]);
+ }
+ }
+
+ foreach ($data as $name => $value)
+ {
+ if (is_numeric($name))
+ continue;
+ if ($skip_underscore and $name[0]=='_')
+ continue;
+ if ( is_null($value) )
+ continue; // null means we dont put it
+ if ($name!=WS_XML_CONTENT)
+ $this->_writer->start_element($name);
+ $this->encode($value);
+ if ($name!=WS_XML_CONTENT)
+ $this->_writer->end_element($name);
+ }
+ }
+
+ function encode($data, $xml_attributes=array() )
+ {
+ switch (gettype($data))
+ {
+ case 'null':
+ case 'NULL':
+ $this->_writer->write_content('');
+ break;
+ case 'boolean':
+ $this->_writer->write_content($data ? '1' : '0');
+ break;
+ case 'integer':
+ case 'double':
+ $this->_writer->write_content($data);
+ break;
+ case 'string':
+ $this->_writer->write_content($data);
+ break;
+ case 'array':
+ $is_array = range(0, count($data) - 1) === array_keys($data);
+ if ($is_array)
+ {
+ $this->encode_array($data, 'item' );
+ }
+ else
+ {
+ $this->encode_struct($data, false, $xml_attributes);
+ }
+ break;
+ case 'object':
+ switch ( strtolower(get_class($data)) )
+ {
+ case 'pwgnamedarray':
+ $this->encode_array($data->_content, $data->_itemName, $data->_xmlAttributes);
+ break;
+ case 'pwgnamedstruct':
+ $this->encode_array( array($data->_content), $data->_name, $data->_xmlAttributes);
+ break;
+ default:
+ $this->encode_struct(get_object_vars($data), true);
+ break;
+ }
+ break;
+ default:
+ trigger_error("Invalid type ". gettype($data)." ".get_class($data), E_USER_WARNING );
+ }
+ }
+}
+
+?>
diff --git a/include/ws_protocols/rest_handler.php b/include/ws_protocols/rest_handler.php
new file mode 100644
index 000000000..184fc205a
--- /dev/null
+++ b/include/ws_protocols/rest_handler.php
@@ -0,0 +1,60 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | PhpWebGallery - a PHP based picture gallery |
+// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
+// +-----------------------------------------------------------------------+
+// | branch : BSF (Best So Far)
+// | file : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $
+// | last update : $Date: 2006-12-21 18:49:12 -0500 (Thu, 21 Dec 2006) $
+// | last modifier : $Author: rvelices $
+// | revision : $Rev: 1678 $
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation |
+// | |
+// | This program 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 |
+// | General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU General Public License |
+// | along with this program; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA. |
+// +-----------------------------------------------------------------------+
+
+class PwgRestRequestHandler
+{
+ function handleRequest(&$service)
+ {
+ $params = array();
+
+ $param_array = $service->isPost() ? $_POST : $_GET;
+ foreach ($param_array as $name => $value)
+ {
+ if ($name=='format')
+ continue;
+ if ($name=='method')
+ {
+ $method = $value;
+ }
+ else
+ {
+ $params[$name]=$value;
+ }
+ }
+
+ if ( empty($method) )
+ {
+ $service->sendResponse(
+ new PwgError(400, 'Missing "method" name')
+ );
+ return;
+ }
+ $resp = $service->invoke($method, $params);
+ $service->sendResponse($resp);
+ }
+}
+
+?>
diff --git a/include/ws_protocols/xmlrpc_encoder.php b/include/ws_protocols/xmlrpc_encoder.php
new file mode 100644
index 000000000..69919165e
--- /dev/null
+++ b/include/ws_protocols/xmlrpc_encoder.php
@@ -0,0 +1,118 @@
+<?php
+// +-----------------------------------------------------------------------+
+// | PhpWebGallery - a PHP based picture gallery |
+// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
+// +-----------------------------------------------------------------------+
+// | branch : BSF (Best So Far)
+// | file : $URL: svn+ssh://rvelices@svn.gna.org/svn/phpwebgallery/trunk/action.php $
+// | last update : $Date: 2006-12-21 18:49:12 -0500 (Thu, 21 Dec 2006) $
+// | last modifier : $Author: rvelices $
+// | revision : $Rev: 1678 $
+// +-----------------------------------------------------------------------+
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation |
+// | |
+// | This program 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 |
+// | General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU General Public License |
+// | along with this program; if not, write to the Free Software |
+// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
+// | USA. |
+// +-----------------------------------------------------------------------+
+
+function xmlrpc_encode($data)
+{
+ switch (gettype($data))
+ {
+ case 'boolean':
+ return '<boolean>'.($data ? '1' : '0').'</boolean>';
+ case 'integer':
+ return '<int>'.$data.'</int>';
+ case 'double':
+ return '<double>'.$data.'</double>';
+ case 'string':
+ return '<string>'.htmlspecialchars($data).'</string>';
+ case 'object':
+ case 'array':
+ $is_array = range(0, count($data) - 1) === array_keys($data);
+ if ($is_array)
+ {
+ $return = '<array><data>'."\n";
+ foreach ($data as $item)
+ {
+ $return .= ' <value>'.xmlrpc_encode($item)."</value>\n";
+ }
+ $return .= '</data></array>';
+ }
+ else
+ {
+ $return = '<struct>'."\n";
+ foreach ($data as $name => $value)
+ {
+ $name = htmlspecialchars($name);
+ $return .= " <member><name>$name</name><value>";
+ $return .= xmlrpc_encode($value)."</value></member>\n";
+ }
+ $return .= '</struct>';
+ }
+ return $return;
+ }
+}
+
+class PwgXmlRpcEncoder extends PwgResponseEncoder
+{
+ function encodeResponse($response)
+ {
+ $respClass = strtolower( get_class($response) );
+ if ($respClass=='pwgerror')
+ {
+ $code = $response->code();
+ $msg = htmlspecialchars($response->message());
+ $ret = <<<EOD
+<methodResponse>
+ <fault>
+ <value>
+ <struct>
+ <member>
+ <name>faultCode</name>
+ <value><int>{$code}</int></value>
+ </member>
+ <member>
+ <name>faultString</name>
+ <value><string>{$msg}</string></value>
+ </member>
+ </struct>
+ </value>
+ </fault>
+</methodResponse>
+EOD;
+ return $ret;
+ }
+
+ parent::flattenResponse($response);
+ $ret = xmlrpc_encode($response);
+ $ret = <<<EOD
+<methodResponse>
+ <params>
+ <param>
+ <value>
+ $ret
+ </value>
+ </param>
+ </params>
+</methodResponse>
+EOD;
+ return $ret;
+ }
+
+ function getContentType()
+ {
+ return 'text/xml';
+ }
+}
+
+?>