ocsclient.php 7.5 KB

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