~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to usr/share/owncloud/lib/private/ocsclient.php

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
/**
25
 
 * This class provides an easy way for apps to store config values in the
26
 
 * database.
27
 
 */
28
 
 
29
 
class OC_OCSClient{
30
 
 
31
 
        /**
32
 
         * @brief Get the url of the OCS AppStore server.
33
 
         * @returns string of the AppStore server
34
 
         *
35
 
         * This function returns the url of the OCS AppStore server. It´s possible
36
 
         * to set it in the config file or it will fallback to the default
37
 
         */
38
 
        private static function getAppStoreURL() {
39
 
                if(OC_Util::getEditionString()===''){
40
 
                        $default='http://api.apps.owncloud.com/v1';
41
 
                }else{
42
 
                        $default='';
43
 
                }
44
 
                $url = OC_Config::getValue('appstoreurl', $default);
45
 
                return($url);
46
 
        }
47
 
 
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
 
        /**
60
 
         * @brief Get all the categories from the OCS server
61
 
         * @returns array with category ids
62
 
         * @note returns NULL if config value appstoreenabled is set to false
63
 
         * This function returns a list of all the application categories on the OCS server
64
 
         */
65
 
        public static function getCategories() {
66
 
                if(OC_Config::getValue('appstoreenabled', false)==false) {
67
 
                        return null;
68
 
                }
69
 
                $url=OC_OCSClient::getAppStoreURL().'/content/categories';
70
 
                $xml=OC_OCSClient::getOCSresponse($url);
71
 
                if($xml==false) {
72
 
                        return null;
73
 
                }
74
 
                $data=simplexml_load_string($xml);
75
 
 
76
 
                $tmp=$data->data;
77
 
                $cats=array();
78
 
 
79
 
                foreach($tmp->category as $value) {
80
 
 
81
 
                        $id= (int) $value->id;
82
 
                        $name= (string) $value->name;
83
 
                        $cats[$id]=$name;
84
 
 
85
 
                }
86
 
 
87
 
                return $cats;
88
 
        }
89
 
 
90
 
        /**
91
 
         * @brief Get all the applications from the OCS server
92
 
         * @returns array with application data
93
 
         *
94
 
         * This function returns a list of all the applications on the OCS server
95
 
         */
96
 
        public static function getApplications($categories, $page, $filter) {
97
 
                if(OC_Config::getValue('appstoreenabled', false)==false) {
98
 
                        return(array());
99
 
                }
100
 
 
101
 
                if(is_array($categories)) {
102
 
                        $categoriesstring=implode('x', $categories);
103
 
                }else{
104
 
                        $categoriesstring=$categories;
105
 
                }
106
 
 
107
 
                $version='&version='.implode('x', \OC_Util::getVersion());
108
 
                $filterurl='&filter='.urlencode($filter);
109
 
                $url=OC_OCSClient::getAppStoreURL().'/content/data?categories='.urlencode($categoriesstring)
110
 
                        .'&sortmode=new&page='.urlencode($page).'&pagesize=100'.$filterurl.$version;
111
 
                $apps=array();
112
 
                $xml=OC_OCSClient::getOCSresponse($url);
113
 
 
114
 
                if($xml==false) {
115
 
                        return null;
116
 
                }
117
 
                $data=simplexml_load_string($xml);
118
 
 
119
 
                $tmp=$data->data->content;
120
 
                for($i = 0; $i < count($tmp); $i++) {
121
 
                        $app=array();
122
 
                        $app['id']=(string)$tmp[$i]->id;
123
 
                        $app['name']=(string)$tmp[$i]->name;
124
 
                        $app['label']=(string)$tmp[$i]->label;
125
 
                        $app['version']=(string)$tmp[$i]->version;
126
 
                        $app['type']=(string)$tmp[$i]->typeid;
127
 
                        $app['typename']=(string)$tmp[$i]->typename;
128
 
                        $app['personid']=(string)$tmp[$i]->personid;
129
 
                        $app['license']=(string)$tmp[$i]->license;
130
 
                        $app['detailpage']=(string)$tmp[$i]->detailpage;
131
 
                        $app['preview']=(string)$tmp[$i]->smallpreviewpic1;
132
 
                        $app['changed']=strtotime($tmp[$i]->changed);
133
 
                        $app['description']=(string)$tmp[$i]->description;
134
 
                        $app['score']=(string)$tmp[$i]->score;
135
 
 
136
 
                        $apps[]=$app;
137
 
                }
138
 
                return $apps;
139
 
        }
140
 
 
141
 
 
142
 
        /**
143
 
         * @brief Get an the applications from the OCS server
144
 
         * @returns array with application data
145
 
         *
146
 
         * This function returns an  applications from the OCS server
147
 
         */
148
 
        public static function getApplication($id) {
149
 
                if(OC_Config::getValue('appstoreenabled', false)==false) {
150
 
                        return null;
151
 
                }
152
 
                $url=OC_OCSClient::getAppStoreURL().'/content/data/'.urlencode($id);
153
 
                $xml=OC_OCSClient::getOCSresponse($url);
154
 
 
155
 
                if($xml==false) {
156
 
                        OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
157
 
                        return null;
158
 
                }
159
 
                $data=simplexml_load_string($xml);
160
 
 
161
 
                $tmp=$data->data->content;
162
 
                $app=array();
163
 
                $app['id']=$tmp->id;
164
 
                $app['name']=$tmp->name;
165
 
                $app['version']=$tmp->version;
166
 
                $app['type']=$tmp->typeid;
167
 
                $app['label']=$tmp->label;
168
 
                $app['typename']=$tmp->typename;
169
 
                $app['personid']=$tmp->personid;
170
 
                $app['detailpage']=$tmp->detailpage;
171
 
                $app['preview1']=$tmp->smallpreviewpic1;
172
 
                $app['preview2']=$tmp->smallpreviewpic2;
173
 
                $app['preview3']=$tmp->smallpreviewpic3;
174
 
                $app['changed']=strtotime($tmp->changed);
175
 
                $app['description']=$tmp->description;
176
 
                $app['detailpage']=$tmp->detailpage;
177
 
                $app['score']=$tmp->score;
178
 
 
179
 
                return $app;
180
 
        }
181
 
 
182
 
        /**
183
 
                * @brief Get the download url for an application from the OCS server
184
 
                * @returns array with application data
185
 
                *
186
 
                * This function returns an download url for an applications from the OCS server
187
 
                */
188
 
        public static function getApplicationDownload($id, $item) {
189
 
                if(OC_Config::getValue('appstoreenabled', false)==false) {
190
 
                        return null;
191
 
                }
192
 
                $url=OC_OCSClient::getAppStoreURL().'/content/download/'.urlencode($id).'/'.urlencode($item);
193
 
                $xml=OC_OCSClient::getOCSresponse($url);
194
 
 
195
 
                if($xml==false) {
196
 
                        OC_Log::write('core', 'Unable to parse OCS content', OC_Log::FATAL);
197
 
                        return null;
198
 
                }
199
 
                $data=simplexml_load_string($xml);
200
 
 
201
 
                $tmp=$data->data->content;
202
 
                $app=array();
203
 
                if(isset($tmp->downloadlink)) {
204
 
                        $app['downloadlink']=$tmp->downloadlink;
205
 
                }else{
206
 
                        $app['downloadlink']='';
207
 
                }
208
 
                return $app;
209
 
        }
210
 
 
211
 
 
212
 
 
213
 
}