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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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:'
  85. .' http://www.freedesktop.org/wiki/Specifications/open-collaboration-services. DEBUG OUTPUT:'."\n";
  86. $txt.=OC_OCS::getDebugOutput();
  87. echo(OC_OCS::generateXml($format, 'failed', 999, $txt));
  88. }
  89. /**
  90. * generated some debug information to make it easier to find faild API calls
  91. * @return debug data string
  92. */
  93. private static function getDebugOutput() {
  94. $txt='';
  95. $txt.="debug output:\n";
  96. if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."\n";
  97. if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."\n";
  98. if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."\n";
  99. if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
  100. return($txt);
  101. }
  102. /**
  103. * generates the xml or json response for the API call from an multidimenional data array.
  104. * @param string $format
  105. * @param string $status
  106. * @param string $statuscode
  107. * @param string $message
  108. * @param array $data
  109. * @param string $tag
  110. * @param string $tagattribute
  111. * @param int $dimension
  112. * @param int $itemscount
  113. * @param int $itemsperpage
  114. * @return string xml/json
  115. */
  116. private static function generateXml($format, $status, $statuscode,
  117. $message, $data=array(), $tag='', $tagattribute='', $dimension=-1, $itemscount='', $itemsperpage='') {
  118. if($format=='json') {
  119. $json=array();
  120. $json['status']=$status;
  121. $json['statuscode']=$statuscode;
  122. $json['message']=$message;
  123. $json['totalitems']=$itemscount;
  124. $json['itemsperpage']=$itemsperpage;
  125. $json['data']=$data;
  126. return(json_encode($json));
  127. }else{
  128. $txt='';
  129. $writer = xmlwriter_open_memory();
  130. xmlwriter_set_indent( $writer, 2 );
  131. xmlwriter_start_document($writer );
  132. xmlwriter_start_element($writer, 'ocs');
  133. xmlwriter_start_element($writer, 'meta');
  134. xmlwriter_write_element($writer, 'status', $status);
  135. xmlwriter_write_element($writer, 'statuscode', $statuscode);
  136. xmlwriter_write_element($writer, 'message', $message);
  137. if($itemscount<>'') xmlwriter_write_element($writer, 'totalitems', $itemscount);
  138. if(!empty($itemsperpage)) xmlwriter_write_element($writer, 'itemsperpage', $itemsperpage);
  139. xmlwriter_end_element($writer);
  140. if($dimension=='0') {
  141. // 0 dimensions
  142. xmlwriter_write_element($writer, 'data', $data);
  143. }elseif($dimension=='1') {
  144. xmlwriter_start_element($writer, 'data');
  145. foreach($data as $key=>$entry) {
  146. xmlwriter_write_element($writer, $key, $entry);
  147. }
  148. xmlwriter_end_element($writer);
  149. }elseif($dimension=='2') {
  150. xmlwriter_start_element($writer, 'data');
  151. foreach($data as $entry) {
  152. xmlwriter_start_element($writer, $tag);
  153. if(!empty($tagattribute)) {
  154. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  155. }
  156. foreach($entry as $key=>$value) {
  157. if(is_array($value)) {
  158. foreach($value as $k=>$v) {
  159. xmlwriter_write_element($writer, $k, $v);
  160. }
  161. } else {
  162. xmlwriter_write_element($writer, $key, $value);
  163. }
  164. }
  165. xmlwriter_end_element($writer);
  166. }
  167. xmlwriter_end_element($writer);
  168. }elseif($dimension=='3') {
  169. xmlwriter_start_element($writer, 'data');
  170. foreach($data as $entrykey=>$entry) {
  171. xmlwriter_start_element($writer, $tag);
  172. if(!empty($tagattribute)) {
  173. xmlwriter_write_attribute($writer, 'details', $tagattribute);
  174. }
  175. foreach($entry as $key=>$value) {
  176. if(is_array($value)) {
  177. xmlwriter_start_element($writer, $entrykey);
  178. foreach($value as $k=>$v) {
  179. xmlwriter_write_element($writer, $k, $v);
  180. }
  181. xmlwriter_end_element($writer);
  182. } else {
  183. xmlwriter_write_element($writer, $key, $value);
  184. }
  185. }
  186. xmlwriter_end_element($writer);
  187. }
  188. xmlwriter_end_element($writer);
  189. }elseif($dimension=='dynamic') {
  190. xmlwriter_start_element($writer, 'data');
  191. OC_OCS::toxml($writer, $data, 'comment');
  192. xmlwriter_end_element($writer);
  193. }
  194. xmlwriter_end_element($writer);
  195. xmlwriter_end_document( $writer );
  196. $txt.=xmlwriter_output_memory( $writer );
  197. unset($writer);
  198. return($txt);
  199. }
  200. }
  201. public static function toXml($writer, $data, $node) {
  202. foreach($data as $key => $value) {
  203. if (is_numeric($key)) {
  204. $key = $node;
  205. }
  206. if (is_array($value)) {
  207. xmlwriter_start_element($writer, $key);
  208. OC_OCS::toxml($writer, $value, $node);
  209. xmlwriter_end_element($writer);
  210. }else{
  211. xmlwriter_write_element($writer, $key, $value);
  212. }
  213. }
  214. }
  215. /**
  216. * get private data
  217. * @param string $user
  218. * @param string $app
  219. * @param string $key
  220. * @param bool $like use LIKE instead of = when comparing keys
  221. * @return array
  222. */
  223. public static function getData($user, $app="", $key="") {
  224. if($app) {
  225. $apps=array($app);
  226. }else{
  227. $apps=OC_Preferences::getApps($user);
  228. }
  229. if($key) {
  230. $keys=array($key);
  231. }else{
  232. foreach($apps as $app) {
  233. $keys=OC_Preferences::getKeys($user, $app);
  234. }
  235. }
  236. $result=array();
  237. foreach($apps as $app) {
  238. foreach($keys as $key) {
  239. $value=OC_Preferences::getValue($user, $app, $key);
  240. $result[]=array('app'=>$app, 'key'=>$key, 'value'=>$value);
  241. }
  242. }
  243. return $result;
  244. }
  245. }