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.

ocs.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  25. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  26. /**
  27. * Class to handle open collaboration services API requests
  28. *
  29. */
  30. class OC_OCS {
  31. /**
  32. * reads input date from get/post/cookies and converts the date to a special data-type
  33. *
  34. * @param string HTTP method to read the key from
  35. * @param string Parameter to read
  36. * @param string Variable type to format data
  37. * @param mixed Default value to return if the key is not found
  38. * @return mixed Data or if the key is not found and no default is set it will exit with a 400 Bad request
  39. */
  40. public static function readData($method, $key, $type = 'raw', $default = null) {
  41. if ($method == 'get') {
  42. if (isset($_GET[$key])) {
  43. $data = $_GET[$key];
  44. } else if (isset($default)) {
  45. return $default;
  46. } else {
  47. $data = false;
  48. }
  49. } else if ($method == 'post') {
  50. if (isset($_POST[$key])) {
  51. $data = $_POST[$key];
  52. } else if (isset($default)) {
  53. return $default;
  54. } else {
  55. $data = false;
  56. }
  57. }
  58. if ($data === false) {
  59. echo self::generateXml('', 'fail', 400, 'Bad request. Please provide a valid '.$key);
  60. exit();
  61. } else {
  62. // NOTE: Is the raw type necessary? It might be a little risky without sanitization
  63. if ($type == 'raw') return $data;
  64. elseif ($type == 'text') return OC_Util::sanitizeHTML($data);
  65. elseif ($type == 'int') return (int) $data;
  66. elseif ($type == 'float') return (float) $data;
  67. elseif ($type == 'array') return OC_Util::sanitizeHTML($data);
  68. else return OC_Util::sanitizeHTML($data);
  69. }
  70. }
  71. public static function notFound() {
  72. if($_SERVER['REQUEST_METHOD'] == 'GET') {
  73. $method='get';
  74. }elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
  75. $method='put';
  76. parse_str(file_get_contents("php://input"), $put_vars);
  77. }elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  78. $method='post';
  79. }else{
  80. echo('internal server error: method not supported');
  81. exit();
  82. }
  83. $format = self::readData($method, 'format', 'text', '');
  84. $txt='Invalid query, please check the syntax. API specifications are here: 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 debug 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 $itemscount
  112. * @param int $itemsperpage
  113. * @return string xml/json
  114. */
  115. private static function generateXml($format, $status, $statuscode, $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. public static function toXml($writer, $data, $node) {
  200. foreach($data as $key => $value) {
  201. if (is_numeric($key)) {
  202. $key = $node;
  203. }
  204. if (is_array($value)) {
  205. xmlwriter_start_element($writer, $key);
  206. OC_OCS::toxml($writer, $value, $node);
  207. xmlwriter_end_element($writer);
  208. }else{
  209. xmlwriter_write_element($writer, $key, $value);
  210. }
  211. }
  212. }
  213. /**
  214. * get private data
  215. * @param string $user
  216. * @param string $app
  217. * @param string $key
  218. * @param bool $like use LIKE instead of = when comparing keys
  219. * @return array
  220. */
  221. public static function getData($user, $app="", $key="") {
  222. if($app) {
  223. $apps=array($app);
  224. }else{
  225. $apps=OC_Preferences::getApps($user);
  226. }
  227. if($key) {
  228. $keys=array($key);
  229. }else{
  230. foreach($apps as $app) {
  231. $keys=OC_Preferences::getKeys($user, $app);
  232. }
  233. }
  234. $result=array();
  235. foreach($apps as $app) {
  236. foreach($keys as $key) {
  237. $value=OC_Preferences::getValue($user, $app, $key);
  238. $result[]=array('app'=>$app, 'key'=>$key, 'value'=>$value);
  239. }
  240. }
  241. return $result;
  242. }
  243. }