ocsclient.php 5.9 KB

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