diff options
author | vdigital <vdigital@piwigo.org> | 2008-05-23 21:05:41 +0000 |
---|---|---|
committer | vdigital <vdigital@piwigo.org> | 2008-05-23 21:05:41 +0000 |
commit | 77fd1f51a3c5f5a52f72ef8a299fe368228e2285 (patch) | |
tree | a67ede42904657ccf3349ecdaef1cec8b8e36ff8 /BSF/include/ws_protocols | |
parent | 553727dffacc48e8337c1d141f2a25af359e74b1 (diff) |
git-svn-id: http://piwigo.org/svn/trunk@2357 68402e56-0260-453c-a942-63ccdbb3a9ee
Diffstat (limited to '')
-rw-r--r-- | BSF/include/ws_protocols/index.php | 30 | ||||
-rw-r--r-- | BSF/include/ws_protocols/json_encoder.php | 87 | ||||
-rw-r--r-- | BSF/include/ws_protocols/php_encoder.php | 54 | ||||
-rw-r--r-- | BSF/include/ws_protocols/rest_encoder.php | 281 | ||||
-rw-r--r-- | BSF/include/ws_protocols/rest_handler.php | 57 | ||||
-rw-r--r-- | BSF/include/ws_protocols/xmlrpc_encoder.php | 115 |
6 files changed, 624 insertions, 0 deletions
diff --git a/BSF/include/ws_protocols/index.php b/BSF/include/ws_protocols/index.php new file mode 100644 index 000000000..c15b15795 --- /dev/null +++ b/BSF/include/ws_protocols/index.php @@ -0,0 +1,30 @@ +<?php +// +-----------------------------------------------------------------------+ +// | Piwigo - a PHP based picture gallery | +// +-----------------------------------------------------------------------+ +// | Copyright(C) 2008 Piwigo Team http://piwigo.org | +// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | +// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | +// +-----------------------------------------------------------------------+ +// | 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. | +// +-----------------------------------------------------------------------+ + +// Recursive call +$url = '../'; +header( 'Request-URI: '.$url ); +header( 'Content-Location: '.$url ); +header( 'Location: '.$url ); +exit(); +?> diff --git a/BSF/include/ws_protocols/json_encoder.php b/BSF/include/ws_protocols/json_encoder.php new file mode 100644 index 000000000..8c2d7b0a0 --- /dev/null +++ b/BSF/include/ws_protocols/json_encoder.php @@ -0,0 +1,87 @@ +<?php +// +-----------------------------------------------------------------------+ +// | Piwigo - a PHP based picture gallery | +// +-----------------------------------------------------------------------+ +// | Copyright(C) 2008 Piwigo Team http://piwigo.org | +// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | +// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | +// +-----------------------------------------------------------------------+ +// | 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/BSF/include/ws_protocols/php_encoder.php b/BSF/include/ws_protocols/php_encoder.php new file mode 100644 index 000000000..ce9afd3e6 --- /dev/null +++ b/BSF/include/ws_protocols/php_encoder.php @@ -0,0 +1,54 @@ +<?php +// +-----------------------------------------------------------------------+ +// | Piwigo - a PHP based picture gallery | +// +-----------------------------------------------------------------------+ +// | Copyright(C) 2008 Piwigo Team http://piwigo.org | +// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | +// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | +// +-----------------------------------------------------------------------+ +// | 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/BSF/include/ws_protocols/rest_encoder.php b/BSF/include/ws_protocols/rest_encoder.php new file mode 100644 index 000000000..e057c6427 --- /dev/null +++ b/BSF/include/ws_protocols/rest_encoder.php @@ -0,0 +1,281 @@ +<?php +// +-----------------------------------------------------------------------+ +// | Piwigo - a PHP based picture gallery | +// +-----------------------------------------------------------------------+ +// | Copyright(C) 2008 Piwigo Team http://piwigo.org | +// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | +// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | +// +-----------------------------------------------------------------------+ +// | 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; + $this->_output( htmlspecialchars( $value ) ); + } + + function write_cdata($value) + { + $this->_end_prev(false); + $value = (string)$value; + $this->_output( + '<![CDATA[' + . str_replace(']]>', ']]>', $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) + { + $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; + } + + $this->_writer = new PwgXmlWriter(); + $this->encode($response); + $ret = $this->_writer->getOutput(); + $ret = '<?xml version="1.0" encoding="'.get_pwg_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/BSF/include/ws_protocols/rest_handler.php b/BSF/include/ws_protocols/rest_handler.php new file mode 100644 index 000000000..c9c8ad9b0 --- /dev/null +++ b/BSF/include/ws_protocols/rest_handler.php @@ -0,0 +1,57 @@ +<?php +// +-----------------------------------------------------------------------+ +// | Piwigo - a PHP based picture gallery | +// +-----------------------------------------------------------------------+ +// | Copyright(C) 2008 Piwigo Team http://piwigo.org | +// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | +// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | +// +-----------------------------------------------------------------------+ +// | 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' or $name=='partner') + continue; // ignore - special keys + 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/BSF/include/ws_protocols/xmlrpc_encoder.php b/BSF/include/ws_protocols/xmlrpc_encoder.php new file mode 100644 index 000000000..26f0f510e --- /dev/null +++ b/BSF/include/ws_protocols/xmlrpc_encoder.php @@ -0,0 +1,115 @@ +<?php +// +-----------------------------------------------------------------------+ +// | Piwigo - a PHP based picture gallery | +// +-----------------------------------------------------------------------+ +// | Copyright(C) 2008 Piwigo Team http://piwigo.org | +// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | +// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | +// +-----------------------------------------------------------------------+ +// | 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'; + } +} + +?> |