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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 url of the OCS KB server.
  41. * @returns string of the KB server
  42. * This function returns the url of the OCS knowledge base server. It´s
  43. * possible to set it in the config file or it will fallback to the default
  44. */
  45. private static function getKBURL() {
  46. $url = OC_Config::getValue('knowledgebaseurl', 'http://api.apps.owncloud.com/v1');
  47. return($url);
  48. }
  49. /**
  50. * @brief Get the content of an OCS url call.
  51. * @returns string of the response
  52. * This function calls an OCS server and returns the response. It also sets a sane timeout
  53. */
  54. private static function getOCSresponse($url) {
  55. $data = \OC_Util::getUrlContent($url);
  56. return($data);
  57. }
  58. /**
  59. * @brief Get all the categories from the OCS server
  60. * @returns array with category ids
  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(OC_Config::getValue('appstoreenabled', true)==false) {
  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. $data=simplexml_load_string($xml);
  74. $tmp=$data->data;
  75. $cats=array();
  76. foreach($tmp->category as $value) {
  77. $id= (int) $value->id;
  78. $name= (string) $value->name;
  79. $cats[$id]=$name;
  80. }
  81. return $cats;
  82. }
  83. /**
  84. * @brief Get all the applications from the OCS server
  85. * @returns array with application data
  86. *
  87. * This function returns a list of all the applications on the OCS server
  88. */
  89. public static function getApplications($categories, $page, $filter) {
  90. if(OC_Config::getValue('appstoreenabled', true)==false) {
  91. return(array());
  92. }
  93. if(is_array($categories)) {
  94. $categoriesstring=implode('x', $categories);
  95. }else{
  96. $categoriesstring=$categories;
  97. }
  98. $version='&version='.implode('x', \OC_Util::getVersion());
  99. $filterurl='&filter='.urlencode($filter);
  100. $url=OC_OCSClient::getAppStoreURL().'/content/data?categories='.urlencode($categoriesstring)
  101. .'&sortmode=new&page='.urlencode($page).'&pagesize=100'.$filterurl.$version;
  102. $apps=array();
  103. $xml=OC_OCSClient::getOCSresponse($url);
  104. if($xml==false) {
  105. return null;
  106. }
  107. $data=simplexml_load_string($xml);
  108. $tmp=$data->data->content;
  109. for($i = 0; $i < count($tmp); $i++) {
  110. $app=array();
  111. $app['id']=(string)$tmp[$i]->id;
  112. $app['name']=(string)$tmp[$i]->name;
  113. $app['label']=(string)$tmp[$i]->label;
  114. $app['version']=(string)$tmp[$i]->version;
  115. $app['type']=(string)$tmp[$i]->typeid;
  116. $app['typename']=(string)$tmp[$i]->typename;
  117. $app['personid']=(string)$tmp[$i]->personid;
  118. $app['license']=(string)$tmp[$i]->license;
  119. $app['detailpage']=(string)$tmp[$i]->detailpage;
  120. $app['preview']=(string)$tmp[$i]->smallpreviewpic1;
  121. $app['changed']=strtotime($tmp[$i]->changed);
  122. $app['description']=(string)$tmp[$i]->description;
  123. $app['score']=(string)$tmp[$i]->score;
  124. $apps[]=$app;
  125. }
  126. return $apps;
  127. }
  128. /**
  129. * @brief Get an the applications from the OCS server
  130. * @returns array with application data
  131. *
  132. * This function returns an applications from the OCS server
  133. */
  134. public static function getApplication($id) {
  135. if(OC_Config::getValue('appstoreenabled', true)==false) {
  136. return null;
  137. }
  138. $url=OC_OCSClient::getAppStoreURL().'/content/data/'.urlencode($id);
  139. $xml=OC_OCSClient::getOCSresponse($url);
  140. if($xml==false) {
  141. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  142. return null;
  143. }
  144. $data=simplexml_load_string($xml);
  145. $tmp=$data->data->content;
  146. $app=array();
  147. $app['id']=$tmp->id;
  148. $app['name']=$tmp->name;
  149. $app['version']=$tmp->version;
  150. $app['type']=$tmp->typeid;
  151. $app['label']=$tmp->label;
  152. $app['typename']=$tmp->typename;
  153. $app['personid']=$tmp->personid;
  154. $app['detailpage']=$tmp->detailpage;
  155. $app['preview1']=$tmp->smallpreviewpic1;
  156. $app['preview2']=$tmp->smallpreviewpic2;
  157. $app['preview3']=$tmp->smallpreviewpic3;
  158. $app['changed']=strtotime($tmp->changed);
  159. $app['description']=$tmp->description;
  160. $app['detailpage']=$tmp->detailpage;
  161. $app['score']=$tmp->score;
  162. return $app;
  163. }
  164. /**
  165. * @brief Get the download url for an application from the OCS server
  166. * @returns array with application data
  167. *
  168. * This function returns an download url for an applications from the OCS server
  169. */
  170. public static function getApplicationDownload($id, $item) {
  171. if(OC_Config::getValue('appstoreenabled', true)==false) {
  172. return null;
  173. }
  174. $url=OC_OCSClient::getAppStoreURL().'/content/download/'.urlencode($id).'/'.urlencode($item);
  175. $xml=OC_OCSClient::getOCSresponse($url);
  176. if($xml==false) {
  177. OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
  178. return null;
  179. }
  180. $data=simplexml_load_string($xml);
  181. $tmp=$data->data->content;
  182. $app=array();
  183. if(isset($tmp->downloadlink)) {
  184. $app['downloadlink']=$tmp->downloadlink;
  185. }else{
  186. $app['downloadlink']='';
  187. }
  188. return $app;
  189. }
  190. /**
  191. * @brief Get all the knowledgebase entries from the OCS server
  192. * @returns array with q and a data
  193. *
  194. * This function returns a list of all the knowledgebase entries from the OCS server
  195. */
  196. public static function getKnownledgebaseEntries($page, $pagesize, $search='') {
  197. $kbe = array('totalitems' => 0);
  198. if(OC_Config::getValue('knowledgebaseenabled', true)) {
  199. $p = (int) $page;
  200. $s = (int) $pagesize;
  201. $searchcmd = '';
  202. if ($search) {
  203. $searchcmd = '&search='.urlencode($search);
  204. }
  205. $url = OC_OCSClient::getKBURL().'/knowledgebase/data?type=150&page='. $p .'&pagesize='. $s . $searchcmd;
  206. $xml = OC_OCSClient::getOCSresponse($url);
  207. $data = @simplexml_load_string($xml);
  208. if($data===false) {
  209. OC_Log::write('core', 'Unable to parse knowledgebase content', OC_Log::FATAL);
  210. return null;
  211. }
  212. $tmp = $data->data->content;
  213. for($i = 0; $i < count($tmp); $i++) {
  214. $kbe[] = array(
  215. 'id' => $tmp[$i]->id,
  216. 'name' => $tmp[$i]->name,
  217. 'description' => $tmp[$i]->description,
  218. 'answer' => $tmp[$i]->answer,
  219. 'preview1' => $tmp[$i]->smallpreviewpic1,
  220. 'detailpage' => $tmp[$i]->detailpage
  221. );
  222. }
  223. $kbe['totalitems'] = $data->meta->totalitems;
  224. }
  225. return $kbe;
  226. }
  227. }