ocs.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 data from get/post 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. }elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  76. $method='post';
  77. }else{
  78. echo('internal server error: method not supported');
  79. exit();
  80. }
  81. $format = self::readData($method, 'format', 'text', '');
  82. $txt='Invalid query, please check the syntax. API specifications are here:'
  83. .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n";
  84. $txt.=OC_OCS::getDebugOutput();
  85. echo(OC_OCS::generateXml($format, 'failed', 999, $txt));
  86. }
  87. /**
  88. * generated some debug information to make it easier to find failed API calls
  89. * @return string data
  90. */
  91. private static function getDebugOutput() {
  92. $txt='';
  93. $txt.="debug output:\n";
  94. if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."\n";
  95. if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."\n";
  96. if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."\n";
  97. if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
  98. return($txt);
  99. }
  100. /**
  101. * generates the xml or json response for the API call from an multidimenional data array.
  102. * @param string $format
  103. * @param string $status
  104. * @param string $statuscode
  105. * @param string $message
  106. * @param array $data
  107. * @param string $tag
  108. * @param string $tagattribute
  109. * @param int $dimension
  110. * @param int|string $itemscount
  111. * @param int|string $itemsperpage
  112. * @return string xml/json
  113. */
  114. public static function generateXml($format, $status, $statuscode,
  115. $message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
  116. if($format=='json') {
  117. $json=array();
  118. $json['status']=$status;
  119. $json['statuscode']=$statuscode;
  120. $json['message']=$message;
  121. $json['totalitems']=$itemscount;
  122. $json['itemsperpage']=$itemsperpage;
  123. $json['data']=$data;
  124. return(json_encode($json));
  125. }else{
  126. $txt='';
  127. $writer = xmlwriter_open_memory();
  128. xmlwriter_set_indent( $writer, 2 );
  129. xmlwriter_start_document($writer );
  130. xmlwriter_start_element($writer, 'ocs');
  131. xmlwriter_start_element($writer, 'meta');
  132. xmlwriter_write_element($writer, 'status', $status);
  133. xmlwriter_write_element($writer, 'statuscode', $statuscode);
  134. xmlwriter_write_element($writer, 'message', $message);
  135. if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
  136. if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
  137. xmlwriter_end_element($writer);
  138. if($dimension=='0') {
  139. // 0 dimensions
  140. xmlwriter_write_element($writer, 'data', $data);
  141. }elseif($dimension=='1') {
  142. xmlwriter_start_element($writer, 'data');
  143. foreach($data as $key=>$entry) {
  144. xmlwriter_write_element($writer, $key, $entry);
  145. }
  146. xmlwriter_end_element($writer);
  147. }elseif($dimension=='2') {
  148. xmlwriter_start_element($writer, 'data');
  149. foreach($data as $entry) {
  150. xmlwriter_start_element($writer, $tag);
  151. if(!empty($tagattribute)) {
  152. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  153. }
  154. foreach($entry as $key=>$value) {
  155. if(is_array($value)) {
  156. foreach($value as $k=>$v) {
  157. xmlwriter_write_element($writer, $k, $v);
  158. }
  159. } else {
  160. xmlwriter_write_element($writer, $key, $value);
  161. }
  162. }
  163. xmlwriter_end_element($writer);
  164. }
  165. xmlwriter_end_element($writer);
  166. }elseif($dimension=='3') {
  167. xmlwriter_start_element($writer, 'data');
  168. foreach($data as $entrykey=>$entry) {
  169. xmlwriter_start_element($writer, $tag);
  170. if(!empty($tagattribute)) {
  171. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  172. }
  173. foreach($entry as $key=>$value) {
  174. if(is_array($value)) {
  175. xmlwriter_start_element($writer, $entrykey);
  176. foreach($value as $k=>$v) {
  177. xmlwriter_write_element($writer, $k, $v);
  178. }
  179. xmlwriter_end_element($writer);
  180. } else {
  181. xmlwriter_write_element($writer, $key, $value);
  182. }
  183. }
  184. xmlwriter_end_element($writer);
  185. }
  186. xmlwriter_end_element($writer);
  187. }elseif($dimension=='dynamic') {
  188. xmlwriter_start_element($writer, 'data');
  189. OC_OCS::toxml($writer, $data, 'comment');
  190. xmlwriter_end_element($writer);
  191. }
  192. xmlwriter_end_element($writer);
  193. xmlwriter_end_document( $writer );
  194. $txt.=xmlwriter_output_memory( $writer );
  195. unset($writer);
  196. return($txt);
  197. }
  198. }
  199. /**
  200. * @param resource $writer
  201. * @param array $data
  202. * @param string $node
  203. */
  204. public static function toXml($writer, $data, $node) {
  205. foreach($data as $key => $value) {
  206. if (is_numeric($key)) {
  207. $key = $node;
  208. }
  209. if (is_array($value)) {
  210. xmlwriter_start_element($writer, $key);
  211. OC_OCS::toxml($writer, $value, $node);
  212. xmlwriter_end_element($writer);
  213. }else{
  214. xmlwriter_write_element($writer, $key, $value);
  215. }
  216. }
  217. }
  218. }