ocs.php 7.7 KB

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