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.

lib_ocs.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Class to handle open collaboration services API requests
  24. *
  25. */
  26. class OC_OCS {
  27. /**
  28. * reads input date from get/post/cookies and converts the date to a special data-type
  29. *
  30. * @param variable $key
  31. * @param variable-type $type
  32. * @param priority $getpriority
  33. * @param default $default
  34. * @return data
  35. */
  36. public static function readdata($key,$type='raw',$getpriority=false,$default='') {
  37. if($getpriority) {
  38. if(isset($_GET[$key])) {
  39. $data=$_GET[$key];
  40. } elseif(isset($_POST[$key])) {
  41. $data=$_POST[$key];
  42. } else {
  43. if($default=='') {
  44. if(($type=='int') or ($type=='float')) $data=0; else $data='';
  45. } else {
  46. $data=$default;
  47. }
  48. }
  49. } else {
  50. if(isset($_POST[$key])) {
  51. $data=$_POST[$key];
  52. } elseif(isset($_GET[$key])) {
  53. $data=$_GET[$key];
  54. } elseif(isset($_COOKIE[$key])) {
  55. $data=$_COOKIE[$key];
  56. } else {
  57. if($default=='') {
  58. if(($type=='int') or ($type=='float')) $data=0; else $data='';
  59. } else {
  60. $data=$default;
  61. }
  62. }
  63. }
  64. if($type=='raw') return($data);
  65. elseif($type=='text') return(addslashes(strip_tags($data)));
  66. elseif($type=='int') { $data = (int) $data; return($data); }
  67. elseif($type=='float') { $data = (float) $data; return($data); }
  68. elseif($type=='array') { $data = $data; return($data); }
  69. }
  70. /**
  71. main function to handle the REST request
  72. **/
  73. public static function handle() {
  74. // overwrite the 404 error page returncode
  75. header("HTTP/1.0 200 OK");
  76. if($_SERVER['REQUEST_METHOD'] == 'GET') {
  77. $method='get';
  78. }elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
  79. $method='put';
  80. parse_str(file_get_contents("php://input"),$put_vars);
  81. }elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
  82. $method='post';
  83. }else{
  84. echo('internal server error: method not supported');
  85. exit();
  86. }
  87. // preprocess url
  88. $url=$_SERVER['REQUEST_URI'];
  89. if(substr($url,(strlen($url)-1))<>'/') $url.='/';
  90. $ex=explode('/',$url);
  91. $paracount=count($ex);
  92. // eventhandler
  93. // CONFIG
  94. // apiconfig - GET - CONFIG
  95. if(($method=='get') and (strtolower($ex[$paracount-3])=='v1.php') and (strtolower($ex[$paracount-2])=='config')){
  96. $format=OC_OCS::readdata('format','text');
  97. OC_OCS::apiconfig($format);
  98. // PERSON
  99. // personcheck - POST - PERSON/CHECK
  100. }elseif(($method=='post') and (strtolower($ex[$paracount-4])=='v1.php') and (strtolower($ex[$paracount-3])=='person') and (strtolower($ex[$paracount-2])=='check')){
  101. $format=OC_OCS::readdata('format','text');
  102. $login=OC_OCS::readdata('login','text');
  103. $passwd=OC_OCS::readdata('password','text');
  104. OC_OCS::personcheck($format,$login,$passwd);
  105. // ACTIVITY
  106. // activityget - GET ACTIVITY page,pagesize als urlparameter
  107. }elseif(($method=='get') and (strtolower($ex[$paracount-3])=='v1.php')and (strtolower($ex[$paracount-2])=='activity')){
  108. $format=OC_OCS::readdata('format','text');
  109. $page=OC_OCS::readdata('page','int');
  110. $pagesize=OC_OCS::readdata('pagesize','int');
  111. if($pagesize<1 or $pagesize>100) $pagesize=10;
  112. OC_OCS::activityget($format,$page,$pagesize);
  113. // activityput - POST ACTIVITY
  114. }elseif(($method=='post') and (strtolower($ex[$paracount-3])=='v1.php')and (strtolower($ex[$paracount-2])=='activity')){
  115. $format=OC_OCS::readdata('format','text');
  116. $message=OC_OCS::readdata('message','text');
  117. OC_OCS::activityput($format,$message);
  118. }else{
  119. $format=OC_OCS::readdata('format','text');
  120. $txt='please check the syntax. api specifications are here: http://www.freedesktop.org/wiki/Specifications/open-collaboration-services'."\n";
  121. $txt.=OC_OCS::getdebugoutput();
  122. echo(OC_OCS::generatexml($format,'failed',999,$txt));
  123. }
  124. exit();
  125. }
  126. /**
  127. * generated some debug information to make it easier to find faild API calls
  128. * @return debug data string
  129. */
  130. private static function getdebugoutput() {
  131. $txt='';
  132. $txt.="debug output:\n";
  133. if(isset($_SERVER['REQUEST_METHOD'])) $txt.='http request method: '.$_SERVER['REQUEST_METHOD']."\n";
  134. if(isset($_SERVER['REQUEST_URI'])) $txt.='http request uri: '.$_SERVER['REQUEST_URI']."\n";
  135. if(isset($_GET)) foreach($_GET as $key=>$value) $txt.='get parameter: '.$key.'->'.$value."\n";
  136. if(isset($_POST)) foreach($_POST as $key=>$value) $txt.='post parameter: '.$key.'->'.$value."\n";
  137. return($txt);
  138. }
  139. /**
  140. * checks if the user is authenticated
  141. * checks the IP whitlist, apikeys and login/password combination
  142. * if $forceuser is true and the authentication failed it returns an 401 http response.
  143. * if $forceuser is false and authentification fails it returns an empty username string
  144. * @param bool $forceuser
  145. * @return username string
  146. */
  147. private static function checkpassword($forceuser=true) {
  148. //valid user account ?
  149. if(isset($_SERVER['PHP_AUTH_USER'])) $authuser=$_SERVER['PHP_AUTH_USER']; else $authuser='';
  150. if(isset($_SERVER['PHP_AUTH_PW'])) $authpw=$_SERVER['PHP_AUTH_PW']; else $authpw='';
  151. if(empty($authuser)) {
  152. if($forceuser){
  153. header('WWW-Authenticate: Basic realm="your valid user account or api key"');
  154. header('HTTP/1.0 401 Unauthorized');
  155. exit;
  156. }else{
  157. $identifieduser='';
  158. }
  159. }else{
  160. if(!OC_USER::login($authuser,$authpw)){
  161. if($forceuser){
  162. header('WWW-Authenticate: Basic realm="your valid user account or api key"');
  163. header('HTTP/1.0 401 Unauthorized');
  164. exit;
  165. }else{
  166. $identifieduser='';
  167. }
  168. }else{
  169. $identifieduser=$authuser;
  170. }
  171. }
  172. return($identifieduser);
  173. }
  174. /**
  175. * generates the xml or json response for the API call from an multidimenional data array.
  176. * @param string $format
  177. * @param string $status
  178. * @param string $statuscode
  179. * @param string $message
  180. * @param array $data
  181. * @param string $tag
  182. * @param string $tagattribute
  183. * @param int $dimension
  184. * @param int $itemscount
  185. * @param int $itemsperpage
  186. * @return string xml/json
  187. */
  188. private static function generatexml($format,$status,$statuscode,$message,$data=array(),$tag='',$tagattribute='',$dimension=-1,$itemscount='',$itemsperpage='') {
  189. if($format=='json') {
  190. $json=array();
  191. $json['status']=$status;
  192. $json['statuscode']=$statuscode;
  193. $json['message']=$message;
  194. $json['totalitems']=$itemscount;
  195. $json['itemsperpage']=$itemsperpage;
  196. $json['data']=$data;
  197. return(json_encode($json));
  198. }else{
  199. $txt='';
  200. $writer = xmlwriter_open_memory();
  201. xmlwriter_set_indent( $writer, 2 );
  202. xmlwriter_start_document($writer );
  203. xmlwriter_start_element($writer,'ocs');
  204. xmlwriter_start_element($writer,'meta');
  205. xmlwriter_write_element($writer,'status',$status);
  206. xmlwriter_write_element($writer,'statuscode',$statuscode);
  207. xmlwriter_write_element($writer,'message',$message);
  208. if($itemscount<>'') xmlwriter_write_element($writer,'totalitems',$itemscount);
  209. if(!empty($itemsperpage)) xmlwriter_write_element($writer,'itemsperpage',$itemsperpage);
  210. xmlwriter_end_element($writer);
  211. if($dimension=='0') {
  212. // 0 dimensions
  213. xmlwriter_write_element($writer,'data',$data);
  214. }elseif($dimension=='1') {
  215. xmlwriter_start_element($writer,'data');
  216. foreach($data as $key=>$entry) {
  217. xmlwriter_write_element($writer,$key,$entry);
  218. }
  219. xmlwriter_end_element($writer);
  220. }elseif($dimension=='2') {
  221. xmlwriter_start_element($writer,'data');
  222. foreach($data as $entry) {
  223. xmlwriter_start_element($writer,$tag);
  224. if(!empty($tagattribute)) {
  225. xmlwriter_write_attribute($writer,'details',$tagattribute);
  226. }
  227. foreach($entry as $key=>$value) {
  228. if(is_array($value)){
  229. foreach($value as $k=>$v) {
  230. xmlwriter_write_element($writer,$k,$v);
  231. }
  232. } else {
  233. xmlwriter_write_element($writer,$key,$value);
  234. }
  235. }
  236. xmlwriter_end_element($writer);
  237. }
  238. xmlwriter_end_element($writer);
  239. }elseif($dimension=='3') {
  240. xmlwriter_start_element($writer,'data');
  241. foreach($data as $entrykey=>$entry) {
  242. xmlwriter_start_element($writer,$tag);
  243. if(!empty($tagattribute)) {
  244. xmlwriter_write_attribute($writer,'details',$tagattribute);
  245. }
  246. foreach($entry as $key=>$value) {
  247. if(is_array($value)){
  248. xmlwriter_start_element($writer,$entrykey);
  249. foreach($value as $k=>$v) {
  250. xmlwriter_write_element($writer,$k,$v);
  251. }
  252. xmlwriter_end_element($writer);
  253. } else {
  254. xmlwriter_write_element($writer,$key,$value);
  255. }
  256. }
  257. xmlwriter_end_element($writer);
  258. }
  259. xmlwriter_end_element($writer);
  260. }elseif($dimension=='dynamic') {
  261. xmlwriter_start_element($writer,'data');
  262. OC_OCS::toxml($writer,$data,'comment');
  263. xmlwriter_end_element($writer);
  264. }
  265. xmlwriter_end_element($writer);
  266. xmlwriter_end_document( $writer );
  267. $txt.=xmlwriter_output_memory( $writer );
  268. unset($writer);
  269. return($txt);
  270. }
  271. }
  272. public static function toxml($writer,$data,$node) {
  273. foreach($data as $key => $value) {
  274. if (is_numeric($key)) {
  275. $key = $node;
  276. }
  277. if (is_array($value)){
  278. xmlwriter_start_element($writer,$key);
  279. OC_OCS::toxml($writer,$value,$node);
  280. xmlwriter_end_element($writer);
  281. }else{
  282. xmlwriter_write_element($writer,$key,$value);
  283. }
  284. }
  285. }
  286. /**
  287. * return the config data of this server
  288. * @param string $format
  289. * @return string xml/json
  290. */
  291. private static function apiconfig($format) {
  292. $user=OC_OCS::checkpassword(false);
  293. $url=substr($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'],0,-11).'';
  294. $xml['version']='1.5';
  295. $xml['website']='ownCloud';
  296. $xml['host']=$_SERVER['HTTP_HOST'];
  297. $xml['contact']='';
  298. $xml['ssl']='false';
  299. echo(OC_OCS::generatexml($format,'ok',100,'',$xml,'config','',1));
  300. }
  301. /**
  302. * check if the provided login/apikey/password is valid
  303. * @param string $format
  304. * @param string $login
  305. * @param string $passwd
  306. * @return string xml/json
  307. */
  308. private static function personcheck($format,$login,$passwd) {
  309. if($login<>''){
  310. if(OC_USER::login($login,$passwd)){
  311. $xml['person']['personid']=$login;
  312. echo(OC_OCS::generatexml($format,'ok',100,'',$xml,'person','check',2));
  313. }else{
  314. echo(OC_OCS::generatexml($format,'failed',102,'login not valid'));
  315. }
  316. }else{
  317. echo(OC_OCS::generatexml($format,'failed',101,'please specify all mandatory fields'));
  318. }
  319. }
  320. // ACTIVITY API #############################################
  321. /**
  322. * get my activities
  323. * @param string $format
  324. * @param string $page
  325. * @param string $pagesize
  326. * @return string xml/json
  327. */
  328. private static function activityget($format,$page,$pagesize) {
  329. $user=OC_OCS::checkpassword();
  330. $result = OC_DB::query('select count(*) as co from log');
  331. $entry=$result->fetchRow();
  332. $totalcount=$entry['co'];
  333. OC_DB::free_result($result);
  334. $result = OC_DB::select('select id,timestamp,user,type,message from log order by timestamp desc limit '.($page*$pagesize).','.$pagesize);
  335. $itemscount=count($result);
  336. $url='http://'.substr($_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'],0,-11).'';
  337. $xml=array();
  338. foreach($result as $i=>$log) {
  339. $xml[$i]['id']=$log['id'];
  340. $xml[$i]['personid']=$log['user'];
  341. $xml[$i]['firstname']=$log['user'];
  342. $xml[$i]['lastname']='';
  343. $xml[$i]['profilepage']=$url;
  344. $pic=$url.'/img/owncloud-icon.png';
  345. $xml[$i]['avatarpic']=$pic;
  346. $xml[$i]['timestamp']=date('c',$log['timestamp']);
  347. $xml[$i]['type']=1;
  348. $xml[$i]['message']=OC_LOG::$TYPE[$log['type']].' '.strip_tags($log['message']);
  349. $xml[$i]['link']=$url;
  350. }
  351. $txt=OC_OCS::generatexml($format,'ok',100,'',$xml,'activity','full',2,$totalcount,$pagesize);
  352. echo($txt);
  353. }
  354. /**
  355. * submit a activity
  356. * @param string $format
  357. * @param string $message
  358. * @return string xml/json
  359. */
  360. private static function activityput($format,$message) {
  361. // not implemented in ownCloud
  362. $user=OC_OCS::checkpassword();
  363. echo(OC_OCS::generatexml($format,'ok',100,''));
  364. }
  365. }
  366. ?>