You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.3 KiB

14 years ago
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Michael Gapczynski
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  12. * License as published by the Free Software Foundation; either
  13. * version 3 of the License, or any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public
  21. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. /**
  25. * Class to handle open collaboration services API requests
  26. *
  27. */
  28. class OC_OCS {
  29. /**
  30. * reads input date from get/post/cookies and converts the date to a special data-type
  31. *
  32. * @param string $method HTTP method to read the key from
  33. * @param string $key Parameter to read
  34. * @param string $type Variable type to format data
  35. * @param string $default Default value to return if the key is not found
  36. * @return string Data or if the key is not found and no default is set it will exit with a 400 Bad request
  37. */
  38. public static function readData($method, $key, $type = 'raw', $default = null) {
  39. $data = false;
  40. if ($method == 'get') {
  41. if (isset($_GET[$key])) {
  42. $data = $_GET[$key];
  43. } else if (isset($default)) {
  44. return $default;
  45. } else {
  46. $data = false;
  47. }
  48. } else if ($method == 'post') {
  49. if (isset($_POST[$key])) {
  50. $data = $_POST[$key];
  51. } else if (isset($default)) {
  52. return $default;
  53. } else {
  54. $data = false;
  55. }
  56. }
  57. if ($data === false) {
  58. echo self::generateXml('', 'fail', 400, 'Bad request. Please provide a valid '.$key);
  59. exit();
  60. } else {
  61. // NOTE: Is the raw type necessary? It might be a little risky without sanitization
  62. if ($type == 'raw') return $data;
  63. elseif ($type == 'text') return OC_Util::sanitizeHTML($data);
  64. elseif ($type == 'int') return (int) $data;
  65. elseif ($type == 'float') return (float) $data;
  66. elseif ($type == 'array') return OC_Util::sanitizeHTML($data);
  67. else return OC_Util::sanitizeHTML($data);
  68. }
  69. }
  70. public static function notFound() {
  71. if($_SERVER['REQUEST_METHOD'] == 'GET') {
  72. $method='get';
  73. }elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
  74. $method='put';
  75. parse_str(file_get_contents("php://input"), $put_vars);
  76. }elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  77. $method='post';
  78. }else{
  79. echo('internal server error: method not supported');
  80. exit();
  81. }
  82. $format = self::readData($method, 'format', 'text', '');
  83. $txt='Invalid query, please check the syntax. API specifications are here:'
  84. .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n";
  85. $txt.=OC_OCS::getDebugOutput();
  86. echo(OC_OCS::generateXml($format, 'failed', 999, $txt));
  87. }
  88. /**
  89. * generated some debug information to make it easier to find faild API calls
  90. * @return string data string
  91. */
  92. private static function getDebugOutput() {
  93. $txt='';
  94. $txt.="debug output:\n";
  95. if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."\n";
  96. if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."\n";
  97. if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."\n";
  98. if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
  99. return($txt);
  100. }
  101. /**
  102. * generates the xml or json response for the API call from an multidimenional data array.
  103. * @param string $format
  104. * @param string $status
  105. * @param string $statuscode
  106. * @param string $message
  107. * @param array $data
  108. * @param string $tag
  109. * @param string $tagattribute
  110. * @param int $dimension
  111. * @param int|string $itemscount
  112. * @param int|string $itemsperpage
  113. * @return string xml/json
  114. */
  115. private static function generateXml($format, $status, $statuscode,
  116. $message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
  117. if($format=='json') {
  118. $json=array();
  119. $json['status']=$status;
  120. $json['statuscode']=$statuscode;
  121. $json['message']=$message;
  122. $json['totalitems']=$itemscount;
  123. $json['itemsperpage']=$itemsperpage;
  124. $json['data']=$data;
  125. return(json_encode($json));
  126. }else{
  127. $txt='';
  128. $writer = xmlwriter_open_memory();
  129. xmlwriter_set_indent( $writer, 2 );
  130. xmlwriter_start_document($writer );
  131. xmlwriter_start_element($writer, 'ocs');
  132. xmlwriter_start_element($writer, 'meta');
  133. xmlwriter_write_element($writer, 'status', $status);
  134. xmlwriter_write_element($writer, 'statuscode', $statuscode);
  135. xmlwriter_write_element($writer, 'message', $message);
  136. if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
  137. if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
  138. xmlwriter_end_element($writer);
  139. if($dimension=='0') {
  140. // 0 dimensions
  141. xmlwriter_write_element($writer, 'data', $data);
  142. }elseif($dimension=='1') {
  143. xmlwriter_start_element($writer, 'data');
  144. foreach($data as $key=>$entry) {
  145. xmlwriter_write_element($writer, $key, $entry);
  146. }
  147. xmlwriter_end_element($writer);
  148. }elseif($dimension=='2') {
  149. xmlwriter_start_element($writer, 'data');
  150. foreach($data as $entry) {
  151. xmlwriter_start_element($writer, $tag);
  152. if(!empty($tagattribute)) {
  153. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  154. }
  155. foreach($entry as $key=>$value) {
  156. if(is_array($value)) {
  157. foreach($value as $k=>$v) {
  158. xmlwriter_write_element($writer, $k, $v);
  159. }
  160. } else {
  161. xmlwriter_write_element($writer, $key, $value);
  162. }
  163. }
  164. xmlwriter_end_element($writer);
  165. }
  166. xmlwriter_end_element($writer);
  167. }elseif($dimension=='3') {
  168. xmlwriter_start_element($writer, 'data');
  169. foreach($data as $entrykey=>$entry) {
  170. xmlwriter_start_element($writer, $tag);
  171. if(!empty($tagattribute)) {
  172. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  173. }
  174. foreach($entry as $key=>$value) {
  175. if(is_array($value)) {
  176. xmlwriter_start_element($writer, $entrykey);
  177. foreach($value as $k=>$v) {
  178. xmlwriter_write_element($writer, $k, $v);
  179. }
  180. xmlwriter_end_element($writer);
  181. } else {
  182. xmlwriter_write_element($writer, $key, $value);
  183. }
  184. }
  185. xmlwriter_end_element($writer);
  186. }
  187. xmlwriter_end_element($writer);
  188. }elseif($dimension=='dynamic') {
  189. xmlwriter_start_element($writer, 'data');
  190. OC_OCS::toxml($writer, $data, 'comment');
  191. xmlwriter_end_element($writer);
  192. }
  193. xmlwriter_end_element($writer);
  194. xmlwriter_end_document( $writer );
  195. $txt.=xmlwriter_output_memory( $writer );
  196. unset($writer);
  197. return($txt);
  198. }
  199. }
  200. /**
  201. * @param $writer
  202. * @param $data
  203. * @param string $node
  204. */
  205. public static function toXml($writer, $data, $node) {
  206. foreach($data as $key => $value) {
  207. if (is_numeric($key)) {
  208. $key = $node;
  209. }
  210. if (is_array($value)) {
  211. xmlwriter_start_element($writer, $key);
  212. OC_OCS::toxml($writer, $value, $node);
  213. xmlwriter_end_element($writer);
  214. }else{
  215. xmlwriter_write_element($writer, $key, $value);
  216. }
  217. }
  218. }
  219. }