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.

ocsclient.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * This class provides an easy way for apps to store config values in the
  25. * database.
  26. */
  27. class OC_OCSClient{
  28. /**
  29. * @brief Get the url of the OCS AppStore server.
  30. * @returns string of the AppStore server
  31. *
  32. * This function returns the url of the OCS AppStore server. It´s possible
  33. * to set it in the config file or it will fallback to the default
  34. */
  35. private static function getAppStoreURL() {
  36. $url = OC_Config::getValue('appstoreurl', 'http://api.apps.owncloud.com/v1');
  37. return($url);
  38. }
  39. /**
  40. * @brief Get the content of an OCS url call.
  41. * @returns string of the response
  42. * This function calls an OCS server and returns the response. It also sets a sane timeout
  43. */
  44. private static function getOCSresponse($url) {
  45. $data = \OC_Util::getUrlContent($url);
  46. return($data);
  47. }
  48. /**
  49. * @brief Get all the categories from the OCS server
  50. * @returns array with category ids
  51. * @note returns NULL if config value appstoreenabled is set to false
  52. * This function returns a list of all the application categories on the OCS server
  53. */
  54. public static function getCategories() {
  55. if(OC_Config::getValue('appstoreenabled', true)==false) {
  56. return null;
  57. }
  58. $url=OC_OCSClient::getAppStoreURL().'/content/categories';
  59. $xml=OC_OCSClient::getOCSresponse($url);
  60. if($xml==false) {
  61. return null;
  62. }
  63. $data=simplexml_load_string($xml);
  64. $tmp=$data->data;
  65. $cats=array();
  66. foreach($tmp->category as $value) {
  67. $id= (int) $value->id;
  68. $name= (string) $value->name;
  69. $cats[$id]=$name;
  70. }
  71. return $cats;
  72. }
  73. /**
  74. * @brief Get all the applications from the OCS server
  75. * @returns array with application data
  76. *
  77. * This function returns a list of all the applications on the OCS server
  78. */
  79. public static function getApplications($categories, $page, $filter) {
  80. if(OC_Config::getValue('appstoreenabled', true)==false) {
  81. return(array());
  82. }
  83. if(is_array($categories)) {
  84. $categoriesstring=implode('x', $categories);
  85. }else{
  86. $categoriesstring=$categories;
  87. }
  88. $version='&version='.implode('x', \OC_Util::getVersion());
  89. $filterurl='&filter='.urlencode($filter);
  90. $url=OC_OCSClient::getAppStoreURL().'/content/data?categories='.urlencode($categoriesstring)
  91. .'&sortmode=new&page='.urlencode($page).'&pagesize=100'.$filterurl.$version;
  92. $apps=array();
  93. $xml=OC_OCSClient::getOCSresponse($url);
  94. if($xml==false) {
  95. return null;
  96. }
  97. $data=simplexml_load_string($xml);
  98. $tmp=$data->data->content;
  99. for($i = 0; $i < count($tmp); $i++) {
  100. $app=array();
  101. $app['id']=(string)$tmp[$i]->id;
  102. $app['name']=(string)$tmp[$i]->name;
  103. $app['label']=(string)$tmp[$i]->label;
  104. $app['version']=(string)$tmp[$i]->version;
  105. $app['type']=(string)$tmp[$i]->typeid;
  106. $app['typename']=(string)$tmp[$i]->typename;
  107. $app['personid']=(string)$tmp[$i]->personid;
  108. $app['license']=(string)$tmp[$i]->license;
  109. $app['detailpage']=(string)$tmp[$i]->detailpage;
  110. $app['preview']=(string)$tmp[$i]->smallpreviewpic1;
  111. $app['changed']=strtotime($tmp[$i]->changed);
  112. $app['description']=(string)$tmp[$i]->description;
  113. $app['score']=(string)$tmp[$i]->score;
  114. $apps[]=$app;
  115. }
  116. return $apps;
  117. }
  118. /**
  119. * @brief Get an the applications from the OCS server
  120. * @returns array with application data
  121. *
  122. * This function returns an applications from the OCS server
  123. */
  124. public static function getApplication($id) {
  125. if(OC_Config::getValue('appstoreenabled', true)==false) {
  126. return null;
  127. }
  128. $url=OC_OCSClient::getAppStoreURL().'/content/data/'.urlencode($id);
  129. $xml=OC_OCSClient::getOCSresponse($url);
  130. if($xml==false) {
  131. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  132. return null;
  133. }
  134. $data=simplexml_load_string($xml);
  135. $tmp=$data->data->content;
  136. $app=array();
  137. $app['id']=$tmp->id;
  138. $app['name']=$tmp->name;
  139. $app['version']=$tmp->version;
  140. $app['type']=$tmp->typeid;
  141. $app['label']=$tmp->label;
  142. $app['typename']=$tmp->typename;
  143. $app['personid']=$tmp->personid;
  144. $app['detailpage']=$tmp->detailpage;
  145. $app['preview1']=$tmp->smallpreviewpic1;
  146. $app['preview2']=$tmp->smallpreviewpic2;
  147. $app['preview3']=$tmp->smallpreviewpic3;
  148. $app['changed']=strtotime($tmp->changed);
  149. $app['description']=$tmp->description;
  150. $app['detailpage']=$tmp->detailpage;
  151. $app['score']=$tmp->score;
  152. return $app;
  153. }
  154. /**
  155. * @brief Get the download url for an application from the OCS server
  156. * @returns array with application data
  157. *
  158. * This function returns an download url for an applications from the OCS server
  159. */
  160. public static function getApplicationDownload($id, $item) {
  161. if(OC_Config::getValue('appstoreenabled', true)==false) {
  162. return null;
  163. }
  164. $url=OC_OCSClient::getAppStoreURL().'/content/download/'.urlencode($id).'/'.urlencode($item);
  165. $xml=OC_OCSClient::getOCSresponse($url);
  166. if($xml==false) {
  167. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  168. return null;
  169. }
  170. $data=simplexml_load_string($xml);
  171. $tmp=$data->data->content;
  172. $app=array();
  173. if(isset($tmp->downloadlink)) {
  174. $app['downloadlink']=$tmp->downloadlink;
  175. }else{
  176. $app['downloadlink']='';
  177. }
  178. return $app;
  179. }
  180. }