ocsclient.php 6.9 KB

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