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 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. if(OC_Util::getEditionString()===''){
  37. $default='http://api.apps.owncloud.com/v1';
  38. }else{
  39. $default='';
  40. }
  41. $url = OC_Config::getValue('appstoreurl', $default);
  42. return($url);
  43. }
  44. /**
  45. * @brief Get the content of an OCS url call.
  46. * @returns string of the response
  47. * This function calls an OCS server and returns the response. It also sets a sane timeout
  48. * @param string $url
  49. */
  50. private static function getOCSresponse($url) {
  51. $data = \OC_Util::getUrlContent($url);
  52. return($data);
  53. }
  54. /**
  55. * @brief Get all the categories from the OCS server
  56. * @returns array with category ids
  57. * @note returns NULL if config value appstoreenabled is set to false
  58. * This function returns a list of all the application categories on the OCS server
  59. */
  60. public static function getCategories() {
  61. if(OC_Config::getValue('appstoreenabled', true)==false) {
  62. return null;
  63. }
  64. $url=OC_OCSClient::getAppStoreURL().'/content/categories';
  65. $xml=OC_OCSClient::getOCSresponse($url);
  66. if($xml==false) {
  67. return null;
  68. }
  69. $loadEntities = libxml_disable_entity_loader(true);
  70. $data = simplexml_load_string($xml);
  71. libxml_disable_entity_loader($loadEntities);
  72. $tmp=$data->data;
  73. $cats=array();
  74. foreach($tmp->category as $value) {
  75. $id= (int) $value->id;
  76. $name= (string) $value->name;
  77. $cats[$id]=$name;
  78. }
  79. return $cats;
  80. }
  81. /**
  82. * @brief Get all the applications from the OCS server
  83. * @returns array with application data
  84. *
  85. * This function returns a list of all the applications on the OCS server
  86. * @param $categories
  87. * @param int $page
  88. * @param string $filter
  89. */
  90. public static function getApplications($categories, $page, $filter) {
  91. if(OC_Config::getValue('appstoreenabled', true)==false) {
  92. return(array());
  93. }
  94. if(is_array($categories)) {
  95. $categoriesstring=implode('x', $categories);
  96. }else{
  97. $categoriesstring=$categories;
  98. }
  99. $version='&version='.implode('x', \OC_Util::getVersion());
  100. $filterurl='&filter='.urlencode($filter);
  101. $url=OC_OCSClient::getAppStoreURL().'/content/data?categories='.urlencode($categoriesstring)
  102. .'&sortmode=new&page='.urlencode($page).'&pagesize=100'.$filterurl.$version;
  103. $apps=array();
  104. $xml=OC_OCSClient::getOCSresponse($url);
  105. if($xml==false) {
  106. return null;
  107. }
  108. $loadEntities = libxml_disable_entity_loader(true);
  109. $data = simplexml_load_string($xml);
  110. libxml_disable_entity_loader($loadEntities);
  111. $tmp=$data->data->content;
  112. for($i = 0; $i < count($tmp); $i++) {
  113. $app=array();
  114. $app['id']=(string)$tmp[$i]->id;
  115. $app['name']=(string)$tmp[$i]->name;
  116. $app['label']=(string)$tmp[$i]->label;
  117. $app['version']=(string)$tmp[$i]->version;
  118. $app['type']=(string)$tmp[$i]->typeid;
  119. $app['typename']=(string)$tmp[$i]->typename;
  120. $app['personid']=(string)$tmp[$i]->personid;
  121. $app['license']=(string)$tmp[$i]->license;
  122. $app['detailpage']=(string)$tmp[$i]->detailpage;
  123. $app['preview']=(string)$tmp[$i]->smallpreviewpic1;
  124. $app['changed']=strtotime($tmp[$i]->changed);
  125. $app['description']=(string)$tmp[$i]->description;
  126. $app['score']=(string)$tmp[$i]->score;
  127. $apps[]=$app;
  128. }
  129. return $apps;
  130. }
  131. /**
  132. * @brief Get an the applications from the OCS server
  133. * @param string $id
  134. * @returns array with application data
  135. *
  136. * This function returns an applications from the OCS server
  137. */
  138. public static function getApplication($id) {
  139. if(OC_Config::getValue('appstoreenabled', true)==false) {
  140. return null;
  141. }
  142. $url=OC_OCSClient::getAppStoreURL().'/content/data/'.urlencode($id);
  143. $xml=OC_OCSClient::getOCSresponse($url);
  144. if($xml==false) {
  145. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  146. return null;
  147. }
  148. $loadEntities = libxml_disable_entity_loader(true);
  149. $data = simplexml_load_string($xml);
  150. libxml_disable_entity_loader($loadEntities);
  151. $tmp=$data->data->content;
  152. $app=array();
  153. $app['id']=$tmp->id;
  154. $app['name']=$tmp->name;
  155. $app['version']=$tmp->version;
  156. $app['type']=$tmp->typeid;
  157. $app['label']=$tmp->label;
  158. $app['typename']=$tmp->typename;
  159. $app['personid']=$tmp->personid;
  160. $app['detailpage']=$tmp->detailpage;
  161. $app['preview1']=$tmp->smallpreviewpic1;
  162. $app['preview2']=$tmp->smallpreviewpic2;
  163. $app['preview3']=$tmp->smallpreviewpic3;
  164. $app['changed']=strtotime($tmp->changed);
  165. $app['description']=$tmp->description;
  166. $app['detailpage']=$tmp->detailpage;
  167. $app['score']=$tmp->score;
  168. return $app;
  169. }
  170. /**
  171. * @brief Get the download url for an application from the OCS server
  172. * @returns array with application data
  173. *
  174. * This function returns an download url for an applications from the OCS server
  175. * @param string $id
  176. * @param integer $item
  177. */
  178. public static function getApplicationDownload($id, $item) {
  179. if(OC_Config::getValue('appstoreenabled', true)==false) {
  180. return null;
  181. }
  182. $url=OC_OCSClient::getAppStoreURL().'/content/download/'.urlencode($id).'/'.urlencode($item);
  183. $xml=OC_OCSClient::getOCSresponse($url);
  184. if($xml==false) {
  185. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  186. return null;
  187. }
  188. $loadEntities = libxml_disable_entity_loader(true);
  189. $data = simplexml_load_string($xml);
  190. libxml_disable_entity_loader($loadEntities);
  191. $tmp=$data->data->content;
  192. $app=array();
  193. if(isset($tmp->downloadlink)) {
  194. $app['downloadlink']=$tmp->downloadlink;
  195. }else{
  196. $app['downloadlink']='';
  197. }
  198. return $app;
  199. }
  200. }