]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - ModuleInstall/PackageManager/PackageController.php
Release 6.4.0
[Github/sugarcrm.git] / ModuleInstall / PackageManager / PackageController.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37  require_once('ModuleInstall/PackageManager/PackageManagerDisplay.php');
38  require_once('ModuleInstall/PackageManager/PackageManager.php');
39  class PackageController{
40         var $_pm;
41
42         /**
43          * Constructor: this class is called from the the ajax call and handles invoking the correct
44          * functionality on the server.
45          */
46         function PackageController(){
47            $this->_pm = new PackageManager();
48         }
49
50         function performBasicSearch(){
51             $json = getJSONobj();
52             $search_term = '';
53             $node_id = '';
54              if(isset($_REQUEST['search_term'])) {
55                 $search_term = nl2br($_REQUEST['search_term']);
56             }
57              if(isset($_REQUEST['node_id'])) {
58                 $node_id = nl2br($_REQUEST['node_id']);
59             }
60             $xml = PackageManager::getPackages($node_id);
61             echo 'result = ' . $json->encode(array('packages' => $xml));
62         }
63
64         /**
65          * Retrieve a list of packages which belong to the corresponding category
66          *
67          * @param category_id   this is passed via POST and is the category id of packages
68          *                      we wish to retrieve
69          * @return packages     xml string consisting of the packages and releases which belong to
70          *                      the category
71          */
72         function getPackages(){
73             $json = getJSONobj();
74             $category_id = '';
75
76              if(isset($_REQUEST['category_id'])) {
77                 $category_id = nl2br($_REQUEST['category_id']);
78             }
79             $xml = PackageManager::getPackages($category_id);
80             echo 'result = ' . $json->encode(array('package_output' => $xml));
81         }
82
83         /**
84          * Obtain a list of releases from the server.  This function is currently used for generating the patches/langpacks for upgrade wizard
85          * as well as during installation
86          */
87         function getReleases(){
88             $json = getJSONobj();
89             $category_id = '';
90                 $package_id = '';
91                 $types = '';
92             if(isset($_REQUEST['category_id'])) {
93                 $category_id = nl2br($_REQUEST['category_id']);
94             }
95             if(isset($_REQUEST['package_id'])) {
96                 $package_id = nl2br($_REQUEST['package_id']);
97             }
98             if(isset($_REQUEST['types'])) {
99                 $types = nl2br($_REQUEST['types']);
100             }
101             $types = explode(',', $types);
102
103             $filter = array();
104                 $count = count($types);
105                 $index = 1;
106                 $type_str = '';
107                 foreach($types as $type){
108                         $type_str .= "'".$type."'";
109                         if($index < $count)
110                                 $type_str .= ",";
111                         $index++;
112                 }
113
114                 $filter = array('type' => $type_str);
115                 $filter = PackageManager::toNameValueList($filter);
116             $releases = PackageManager::getReleases($category_id, $package_id, $filter);
117             $nodes = array();
118             $release_map = array();
119             foreach($releases['packages'] as $release){
120                 $release = PackageManager::fromNameValueList($release);
121                                 $nodes[] = array('description' => $release['description'], 'version' => $release['version'], 'build_number' => $release['build_number'], 'id' => $release['id']);
122                         $release_map[$release['id']] = array('package_id' => $release['package_id'], 'category_id' => $release['category_id']);
123                 }
124                 $_SESSION['ML_PATCHES'] = $release_map;
125             echo 'result = ' . $json->encode(array('releases' => $nodes));
126         }
127
128         /**
129          * Obtain a promotion from the depot
130          */
131         function getPromotion(){
132             $json = getJSONobj();
133
134             $header = PackageManager::getPromotion();
135
136             echo 'result = ' . $json->encode(array('promotion' => $header));
137         }
138
139         /**
140          * Download the given release
141          *
142          * @param category_id   this is passed via POST and is the category id of the release we wish to download
143          * @param package_id   this is passed via POST and is the package id of the release we wish to download
144          * @param release_id   this is passed via POST and is the release id of the release we wish to download
145          * @return bool         true is successful in downloading, false otherwise
146          */
147         function download(){
148             global $sugar_config;
149             $json = getJSONobj();
150             $package_id = '';
151             $category_id  = '';
152             $release_id = '';
153             if(isset($_REQUEST['package_id'])) {
154                 $package_id = nl2br($_REQUEST['package_id']);
155             }
156             if(isset($_REQUEST['category_id'])) {
157                 $category_id = nl2br($_REQUEST['category_id']);
158             }
159             if(isset($_REQUEST['release_id'])) {
160                 $release_id = nl2br($_REQUEST['release_id']);
161             }
162             $GLOBALS['log']->debug("PACKAGE ID: ".$package_id);
163             $GLOBALS['log']->debug("CATEGORY ID: ".$category_id);
164             $GLOBALS['log']->debug("RELEASE ID: ".$release_id);
165             $result = $this->_pm->download($category_id, $package_id, $release_id);
166             $GLOBALS['log']->debug("RESULT: ".print_r($result,true));
167             $success = 'false';
168             if($result != null){
169                 $GLOBALS['log']->debug("Performing Setup");
170                 $this->_pm->performSetup($result, 'module', false);
171                 $GLOBALS['log']->debug("Complete Setup");
172                 $success = 'true';
173             }
174             echo 'result = ' . $json->encode(array('success' => $success));
175         }
176
177          /**
178          * Retrieve a list of categories that are subcategories to the selected category
179          *
180          * @param id - the id of the parent_category, -1 if this is the root
181          * @return array - a list of categories/nodes which are underneath this node
182          */
183         function getCategories(){
184             $json = getJSONobj();
185             $node_id = '';
186              if(isset($_REQUEST['category_id'])) {
187                 $node_id = nl2br($_REQUEST['category_id']);
188             }
189             $GLOBALS['log']->debug("NODE ID: ".$node_id);
190             $nodes = PackageManager::getCategories($node_id);
191             echo 'result = ' . $json->encode(array('nodes' => $nodes));
192         }
193
194          function getNodes(){
195             $json = getJSONobj();
196             $category_id = '';
197              if(isset($_REQUEST['category_id'])) {
198                 $category_id = nl2br($_REQUEST['category_id']);
199             }
200             $GLOBALS['log']->debug("CATEGORY ID: ".$category_id);
201             $nodes = PackageManager::getModuleLoaderCategoryPackages($category_id);
202             $GLOBALS['log']->debug(var_export($nodes, true));
203             echo 'result = ' . $json->encode(array('nodes' => $nodes));
204         }
205
206         /**
207          * Check the SugarDepot for updates for the given type as passed in via POST
208          * @param type      the type to check for
209          * @return array    return an array of releases for each given installed object if an update is found
210          */
211         function checkForUpdates(){
212             $json = getJSONobj();
213             $type = '';
214              if(isset($_REQUEST['type'])) {
215                 $type = nl2br($_REQUEST['type']);
216             }
217             $pm = new PackageManager();
218             $updates = $pm->checkForUpdates();
219             $nodes = array();
220                         $release_map = array();
221             if(!empty($updates)){
222                     foreach($updates as $update){
223                         $update = PackageManager::fromNameValueList($update);
224                         $nodes[] = array('label' => $update['name'], 'description' => $update['description'], 'version' => $update['version'], 'build_number' => $update['build_number'], 'id' => $update['id'], 'type' => $update['type']);
225                                         $release_map[$update['id']] = array('package_id' => $update['package_id'], 'category_id' => $update['category_id'], 'type' => $update['type']);
226                     }
227             }
228            //patches
229            $filter = array(array('name' => 'type', 'value' => "'patch'"));
230             $releases = $pm->getReleases('', '', $filter);
231             if(!empty($releases['packages'])){
232                 foreach($releases['packages'] as $update){
233                         $update = PackageManager::fromNameValueList($update);
234                                         $nodes[] = array('label' => $update['name'], 'description' => $update['description'], 'version' => $update['version'], 'build_number' => $update['build_number'], 'id' => $update['id'], 'type' => $update['type']);
235                                         $release_map[$update['id']] = array('package_id' => $update['package_id'], 'category_id' => $update['category_id'], 'type' => $update['type']);
236                     }
237             }
238                         $_SESSION['ML_PATCHES'] = $release_map;
239             echo 'result = ' . $json->encode(array('updates' => $nodes));
240         }
241
242         function getLicenseText(){
243             $json = getJSONobj();
244             $file = '';
245             if(isset($_REQUEST['file'])) {
246                 $file = hashToFile($_REQUEST['file']);
247             }
248             $GLOBALS['log']->debug("FILE : ".$file);
249             echo 'result = ' . $json->encode(array('license_display' => PackageManagerDisplay::buildLicenseOutput($file)));
250         }
251
252         /**
253          *  build the list of modules that are currently in the staging area waiting to be installed
254          */
255         function getPackagesInStaging(){
256             $packages = $this->_pm->getPackagesInStaging('module');
257             $json = getJSONobj();
258
259             echo 'result = ' . $json->encode(array('packages' => $packages));
260         }
261
262         /**
263          *  build the list of modules that are currently in the staging area waiting to be installed
264          */
265         function performInstall(){
266             $file = '';
267              if(isset($_REQUEST['file'])) {
268                 $file = hashToFile($_REQUEST['file']);
269             }
270                 if(!empty($file)){
271                     $this->_pm->performInstall($file);
272                         }
273             $json = getJSONobj();
274
275             echo 'result = ' . $json->encode(array('result' => 'success'));
276         }
277
278         function authenticate(){
279             $json = getJSONobj();
280             $username = '';
281             $password = '';
282             $servername = '';
283             $terms_checked = '';
284             if(isset($_REQUEST['username'])) {
285                 $username = nl2br($_REQUEST['username']);
286             }
287             if(isset($_REQUEST['password'])) {
288                 $password = nl2br($_REQUEST['password']);
289             }
290                  if(isset($_REQUEST['servername'])) {
291                 $servername = $_REQUEST['servername'];
292             }
293             if(isset($_REQUEST['terms_checked'])) {
294                 $terms_checked = $_REQUEST['terms_checked'];
295                 if($terms_checked == 'on')
296                         $terms_checked = true;
297             }
298
299             if(!empty($username) && !empty($password)){
300                 $password = md5($password);
301                 $result = PackageManager::authenticate($username, $password, $servername, $terms_checked);
302                 if(!is_array($result) && $result == true)
303                     $status  = 'success';
304                 else
305                     $status  = $result['faultstring'];
306             }else{
307                 $status  = 'failed';
308             }
309
310             echo 'result = ' . $json->encode(array('status' => $status));
311         }
312
313         function getDocumentation(){
314                 $json = getJSONobj();
315             $package_id = '';
316             $release_id = '';
317
318             if(isset($_REQUEST['package_id'])) {
319                 $package_id = nl2br($_REQUEST['package_id']);
320             }
321             if(isset($_REQUEST['release_id'])) {
322                 $release_id = nl2br($_REQUEST['release_id']);
323             }
324
325             $documents = PackageManager::getDocumentation($package_id, $release_id);
326             $GLOBALS['log']->debug("DOCUMENTS: ".var_export($documents, true));
327             echo 'result = ' . $json->encode(array('documents' => $documents));
328         }
329
330         function downloadedDocumentation(){
331                 $json = getJSONobj();
332             $document_id = '';
333
334             if(isset($_REQUEST['document_id'])) {
335                 $document_id = nl2br($_REQUEST['document_id']);
336             }
337              $GLOBALS['log']->debug("Downloading Document: ".$document_id);
338             PackageManagerComm::downloadedDocumentation($document_id);
339             echo 'result = ' . $json->encode(array('result' => 'true'));
340         }
341
342         /**
343          * Remove metadata files such as foo-manifest
344          * Enter description here ...
345          * @param unknown_type $file
346          * @param unknown_type $meta
347          */
348         protected function rmMetaFile($file, $meta)
349         {
350             $metafile = pathinfo($file, PATHINFO_DIRNAME)."/". pathinfo($file, PATHINFO_FILENAME)."-$meta.php";
351             if(file_exists($metafile)) {
352                 unlink($metafile);
353             }
354         }
355
356                 function remove(){
357                 $json = getJSONobj();
358             $file = '';
359
360             if(isset($_REQUEST['file'])) {
361                  $file = urldecode(hashToFile($_REQUEST['file']));
362             }
363             $GLOBALS['log']->debug("FILE TO REMOVE: ".$file);
364             if(!empty($file)){
365                 unlink($file);
366                 foreach(array("manifest", "icon") as $meta) {
367                     $this->rmMetaFile($file, $meta);
368                 }
369             }
370             echo 'result = ' . $json->encode(array('result' => 'true'));
371         }
372  }
373 ?>