ocsclient.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. * Get the url of the OCS AppStore server.
  30. * @return 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='https://api.owncloud.com/v1';
  38. }else{
  39. $default='';
  40. }
  41. $url = OC_Config::getValue('appstoreurl', $default);
  42. return($url);
  43. }
  44. /**
  45. * Get the content of an OCS url call.
  46. * @return 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. * Get all the categories from the OCS server
  56. * @return array|null an array of category ids or null
  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. * Get all the applications from the OCS server
  83. * @return array|null an array of application data or null
  84. *
  85. * This function returns a list of all the applications on the OCS server
  86. * @param array|string $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. * Get an the applications from the OCS server
  133. * @param string $id
  134. * @return array|null an array of application data or null
  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 for app ' . $id, 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. if (is_null($tmp)) {
  153. OC_Log::write('core', 'Invalid OCS content returned for app ' . $id, OC_Log::FATAL);
  154. return null;
  155. }
  156. $app=array();
  157. $app['id']=$tmp->id;
  158. $app['name']=$tmp->name;
  159. $app['version']=$tmp->version;
  160. $app['type']=$tmp->typeid;
  161. $app['label']=$tmp->label;
  162. $app['typename']=$tmp->typename;
  163. $app['personid']=$tmp->personid;
  164. $app['detailpage']=$tmp->detailpage;
  165. $app['preview1']=$tmp->smallpreviewpic1;
  166. $app['preview2']=$tmp->smallpreviewpic2;
  167. $app['preview3']=$tmp->smallpreviewpic3;
  168. $app['changed']=strtotime($tmp->changed);
  169. $app['description']=$tmp->description;
  170. $app['detailpage']=$tmp->detailpage;
  171. $app['score']=$tmp->score;
  172. return $app;
  173. }
  174. /**
  175. * Get the download url for an application from the OCS server
  176. * @return array|null an array of application data or null
  177. *
  178. * This function returns an download url for an applications from the OCS server
  179. * @param string $id
  180. * @param integer $item
  181. */
  182. public static function getApplicationDownload($id, $item) {
  183. if(OC_Config::getValue('appstoreenabled', true)==false) {
  184. return null;
  185. }
  186. $url=OC_OCSClient::getAppStoreURL().'/content/download/'.urlencode($id).'/'.urlencode($item);
  187. $xml=OC_OCSClient::getOCSresponse($url);
  188. if($xml==false) {
  189. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  190. return null;
  191. }
  192. $loadEntities = libxml_disable_entity_loader(true);
  193. $data = simplexml_load_string($xml);
  194. libxml_disable_entity_loader($loadEntities);
  195. $tmp=$data->data->content;
  196. $app=array();
  197. if(isset($tmp->downloadlink)) {
  198. $app['downloadlink']=$tmp->downloadlink;
  199. }else{
  200. $app['downloadlink']='';
  201. }
  202. return $app;
  203. }
  204. }