ocsclient.php 6.9 KB

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