ocsclient.php 6.9 KB

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