api.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Tom Needham
  6. * @author Michael Gapczynski
  7. * @author Bart Visscher
  8. * @copyright 2012 Tom Needham tom@owncloud.com
  9. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  10. * @copyright 2012 Bart Visscher bartv@thisnet.nl
  11. *
  12. * This library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  14. * License as published by the Free Software Foundation; either
  15. * version 3 of the License, or any later version.
  16. *
  17. * This library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public
  23. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. class OC_API {
  27. /**
  28. * API authentication levels
  29. */
  30. const GUEST_AUTH = 0;
  31. const USER_AUTH = 1;
  32. const SUBADMIN_AUTH = 2;
  33. const ADMIN_AUTH = 3;
  34. private static $server;
  35. /**
  36. * initialises the OAuth store and server
  37. */
  38. private static function init() {
  39. self::$server = new OC_OAuth_Server(new OC_OAuth_Store());
  40. }
  41. /**
  42. * api actions
  43. */
  44. protected static $actions = array();
  45. /**
  46. * registers an api call
  47. * @param string $method the http method
  48. * @param string $url the url to match
  49. * @param callable $action the function to run
  50. * @param string $app the id of the app registering the call
  51. * @param int $authLevel the level of authentication required for the call
  52. * @param array $defaults
  53. * @param array $requirements
  54. */
  55. public static function register($method, $url, $action, $app,
  56. $authLevel = OC_API::USER_AUTH,
  57. $defaults = array(),
  58. $requirements = array()) {
  59. $name = strtolower($method).$url;
  60. $name = str_replace(array('/', '{', '}'), '_', $name);
  61. if(!isset(self::$actions[$name])) {
  62. OC::getRouter()->useCollection('ocs');
  63. OC::getRouter()->create($name, $url)
  64. ->method($method)
  65. ->action('OC_API', 'call');
  66. self::$actions[$name] = array();
  67. }
  68. self::$actions[$name] = array('app' => $app, 'action' => $action, 'authlevel' => $authLevel);
  69. }
  70. /**
  71. * handles an api call
  72. * @param array $parameters
  73. */
  74. public static function call($parameters) {
  75. // Prepare the request variables
  76. if($_SERVER['REQUEST_METHOD'] == 'PUT') {
  77. parse_str(file_get_contents("php://input"), $parameters['_put']);
  78. } else if($_SERVER['REQUEST_METHOD'] == 'DELETE'){
  79. parse_str(file_get_contents("php://input"), $parameters['_delete']);
  80. }
  81. $name = $parameters['_route'];
  82. // Check authentication and availability
  83. if(self::isAuthorised(self::$actions[$name])) {
  84. if(is_callable(self::$actions[$name]['action'])) {
  85. $response = call_user_func(self::$actions[$name]['action'], $parameters);
  86. } else {
  87. $response = new OC_OCS_Result(null, 998, 'Api method not found');
  88. }
  89. } else {
  90. $response = new OC_OCS_Result(null, 997, 'Unauthorised');
  91. }
  92. // Send the response
  93. $formats = array('json', 'xml');
  94. $format = !empty($_GET['format']) && in_array($_GET['format'], $formats) ? $_GET['format'] : 'xml';
  95. self::respond($response, $format);
  96. // logout the user to be stateless
  97. OC_User::logout();
  98. }
  99. /**
  100. * authenticate the api call
  101. * @param array $action the action details as supplied to OC_API::register()
  102. * @return bool
  103. */
  104. private static function isAuthorised($action) {
  105. $level = $action['authlevel'];
  106. switch($level) {
  107. case OC_API::GUEST_AUTH:
  108. // Anyone can access
  109. return true;
  110. break;
  111. case OC_API::USER_AUTH:
  112. // User required
  113. return self::loginUser();
  114. break;
  115. case OC_API::SUBADMIN_AUTH:
  116. // Check for subadmin
  117. $user = self::loginUser();
  118. if(!$user) {
  119. return false;
  120. } else {
  121. $subAdmin = OC_SubAdmin::isSubAdmin($user);
  122. $admin = OC_Group::inGroup($user, 'admin');
  123. if($subAdmin || $admin) {
  124. return true;
  125. } else {
  126. return false;
  127. }
  128. }
  129. break;
  130. case OC_API::ADMIN_AUTH:
  131. // Check for admin
  132. $user = self::loginUser();
  133. if(!$user) {
  134. return false;
  135. } else {
  136. return OC_Group::inGroup($user, 'admin');
  137. }
  138. break;
  139. default:
  140. // oops looks like invalid level supplied
  141. return false;
  142. break;
  143. }
  144. }
  145. /**
  146. * http basic auth
  147. * @return string|false (username, or false on failure)
  148. */
  149. private static function loginUser(){
  150. $authUser = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';
  151. $authPw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
  152. return OC_User::login($authUser, $authPw) ? $authUser : false;
  153. }
  154. /**
  155. * respond to a call
  156. * @param int|array $result the result from the api method
  157. * @param string $format the format xml|json
  158. */
  159. private static function respond($result, $format='xml') {
  160. $response = array('ocs' => $result->getResult());
  161. if ($format == 'json') {
  162. OC_JSON::encodedPrint($response);
  163. } else if ($format == 'xml') {
  164. header('Content-type: text/xml; charset=UTF-8');
  165. $writer = new XMLWriter();
  166. $writer->openMemory();
  167. $writer->setIndent( true );
  168. $writer->startDocument();
  169. self::toXML($response, $writer);
  170. $writer->endDocument();
  171. echo $writer->outputMemory(true);
  172. }
  173. }
  174. private static function toXML($array, $writer) {
  175. foreach($array as $k => $v) {
  176. if (is_numeric($k)) {
  177. $k = 'element';
  178. }
  179. if (is_array($v)) {
  180. $writer->startElement($k);
  181. self::toXML($v, $writer);
  182. $writer->endElement();
  183. } else {
  184. $writer->writeElement($k, $v);
  185. }
  186. }
  187. }
  188. }