From f992150313a63d30aeda6e5f3dcd64fc62a06adb Mon Sep 17 00:00:00 2001 From: rvelices Date: Sat, 6 Jan 2007 11:13:08 +0000 Subject: Web service first version. git-svn-id: http://piwigo.org/svn/trunk@1698 68402e56-0260-453c-a942-63ccdbb3a9ee --- include/ws_protocols/json_encoder.php | 90 ++++++++++ include/ws_protocols/php_encoder.php | 57 +++++++ include/ws_protocols/rest_encoder.php | 285 ++++++++++++++++++++++++++++++++ include/ws_protocols/rest_handler.php | 60 +++++++ include/ws_protocols/xmlrpc_encoder.php | 118 +++++++++++++ 5 files changed, 610 insertions(+) create mode 100644 include/ws_protocols/json_encoder.php create mode 100644 include/ws_protocols/php_encoder.php create mode 100644 include/ws_protocols/rest_encoder.php create mode 100644 include/ws_protocols/rest_handler.php create mode 100644 include/ws_protocols/xmlrpc_encoder.php (limited to 'include/ws_protocols') 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 @@ + $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 @@ + '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 @@ +_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; + $need_cdata = (strpos($value, "\r")!==false)?true:false; + if ($need_cdata) + { + $this->_output( '' ); + } + 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 = ' + + +'; + return $ret; + } + +//parent::flattenResponse($response); + + $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/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 @@ +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 @@ +'.($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