From 77fd1f51a3c5f5a52f72ef8a299fe368228e2285 Mon Sep 17 00:00:00 2001 From: vdigital Date: Fri, 23 May 2008 21:05:41 +0000 Subject: git-svn-id: http://piwigo.org/svn/trunk@2357 68402e56-0260-453c-a942-63ccdbb3a9ee --- BSF/include/ws_protocols/index.php | 30 +++ BSF/include/ws_protocols/json_encoder.php | 87 +++++++++ BSF/include/ws_protocols/php_encoder.php | 54 ++++++ BSF/include/ws_protocols/rest_encoder.php | 281 ++++++++++++++++++++++++++++ BSF/include/ws_protocols/rest_handler.php | 57 ++++++ BSF/include/ws_protocols/xmlrpc_encoder.php | 115 ++++++++++++ 6 files changed, 624 insertions(+) create mode 100644 BSF/include/ws_protocols/index.php create mode 100644 BSF/include/ws_protocols/json_encoder.php create mode 100644 BSF/include/ws_protocols/php_encoder.php create mode 100644 BSF/include/ws_protocols/rest_encoder.php create mode 100644 BSF/include/ws_protocols/rest_handler.php create mode 100644 BSF/include/ws_protocols/xmlrpc_encoder.php (limited to 'BSF/include/ws_protocols') 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 @@ + 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 @@ + $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 @@ + '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 @@ +_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('"); + } + } + + 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( + '', ']]>', $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 = ' + + +'; + return $ret; + } + + $this->_writer = new PwgXmlWriter(); + $this->encode($response); + $ret = $this->_writer->getOutput(); + $ret = ' + +'.$ret.' +'; + + 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 @@ +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 @@ +'.($data ? '1' : '0').''; + case 'integer': + return ''.$data.''; + case 'double': + return ''.$data.''; + case 'string': + return ''.htmlspecialchars($data).''; + case 'object': + case 'array': + $is_array = range(0, count($data) - 1) === array_keys($data); + if ($is_array) + { + $return = ''."\n"; + foreach ($data as $item) + { + $return .= ' '.xmlrpc_encode($item)."\n"; + } + $return .= ''; + } + else + { + $return = ''."\n"; + foreach ($data as $name => $value) + { + $name = htmlspecialchars($name); + $return .= " $name"; + $return .= xmlrpc_encode($value)."\n"; + } + $return .= ''; + } + 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 = << + + + + + faultCode + {$code} + + + faultString + {$msg} + + + + + +EOD; + return $ret; + } + + parent::flattenResponse($response); + $ret = xmlrpc_encode($response); + $ret = << + + + + $ret + + + + +EOD; + return $ret; + } + + function getContentType() + { + return 'text/xml'; + } +} + +?> -- cgit v1.2.3