]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - ModuleInstall/PackageManager/PackageManagerComm.php
Release 6.4.0
[Github/sugarcrm.git] / ModuleInstall / PackageManager / PackageManagerComm.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('include/nusoap/nusoap.php');
38 require_once('ModuleInstall/PackageManager/PackageManagerDownloader.php');
39
40 define("HTTPS_URL", "https://depot.sugarcrm.com/depot/SugarDepotSoap.php");
41 define("ACTIVE_STATUS", "ACTIVE");
42
43 class PackageManagerComm{
44      /**
45       * Initialize the soap client and store in the $GLOBALS object for use
46       *
47       * @param login    designates whether we want to try to login after we initialize or not
48       */
49      function initialize($login = true){
50         if(empty($GLOBALS['SugarDepot'])){
51             $GLOBALS['log']->debug('USING HTTPS TO CONNECT TO HEARTBEAT');
52             $soap_client = new nusoapclient(HTTPS_URL, false);
53             $ping = $soap_client->call('sugarPing', array());
54             $GLOBALS['SugarDepot'] = $soap_client;
55         }
56         //if we do not have a session, then try to login
57         if($login && empty($_SESSION['SugarDepotSessionID'])){
58             PackageManagerComm::login();
59         }
60      }
61
62      /**
63       * Check for errors in the response or error_str
64       */
65      function errorCheck(){
66         if(!empty($GLOBALS['SugarDepot']->error_str)){
67                 $GLOBALS['log']->fatal($GLOBALS['SugarDepot']->error_str);
68                 $GLOBALS['log']->fatal($GLOBALS['SugarDepot']->response);
69         }
70      }
71
72      /**
73       * Set the credentials for use during login
74       *
75       * @param username    Mambo username
76       * @param password     Mambo password
77       * @param download_key User's download key
78       */
79      function setCredentials($username, $password, $download_key){
80         $_SESSION['SugarDepotUsername'] = $username;
81         $_SESSION['SugarDepotPassword'] = $password;
82         $_SESSION['SugarDepotDownloadKey'] = $download_key;
83      }
84
85      /**
86       * Clears out the session so we can reauthenticate.
87       */
88      function clearSession(){
89         $_SESSION['SugarDepotSessionID'] = null;
90         unset($_SESSION['SugarDepotSessionID']);
91      }
92      /////////////////////////////////////////////////////////
93      ////////// BEGIN: Base Functions for Communicating with the depot
94      /**
95       * Login to the depot
96       *
97       * @return true if successful, false otherwise
98       */
99      function login($terms_checked = true){
100       if(empty($_SESSION['SugarDepotSessionID'])){
101               global $license;
102                 $GLOBALS['log']->debug("Begin SugarDepot Login");
103                 PackageManagerComm::initialize(false);
104                 require('sugar_version.php');
105                 require('config.php');
106                 $credentials = PackageManager::getCredentials();
107                 if(empty($license))loadLicense();
108                 $info = sugarEncode('2813', serialize(getSystemInfo(true)));
109                 $pm = new PackageManager();
110                 $installed = $pm->buildInstalledReleases();
111                 $installed = base64_encode(serialize($installed));
112                 $params = array('installed_modules' => $installed, 'terms_checked' => $terms_checked, 'system_name' => $credentials['system_name']);
113                 $terms_version = (!empty($_SESSION['SugarDepot_TermsVersion']) ? $_SESSION['SugarDepot_TermsVersion'] : '');
114                 if(!empty($terms_version))
115                         $params['terms_version'] = $terms_version;
116
117                 $result = $GLOBALS['SugarDepot']->call('depotLogin', array(array('user_name' => $credentials['username'], 'password' => $credentials['password']),'info'=>$info, 'params' => $params));
118                 PackageManagerComm::errorCheck();
119                 if(!is_array($result))
120                         $_SESSION['SugarDepotSessionID'] = $result;
121                 $GLOBALS['log']->debug("End SugarDepot Login");
122                 return $result;
123       }
124       else
125         return $_SESSION['SugarDepotSessionID'];
126      }
127
128      /**
129       * Logout from the depot
130       */
131      function logout(){
132         PackageManagerComm::initialize();
133         $result = $GLOBALS['SugarDepot']->call('depotLogout', array('session_id' => $_SESSION['SugarDepotSessionID']));
134      }
135
136      /**
137       * Get all promotions from the depot
138       */
139      function getPromotion(){
140         PackageManagerComm::initialize();
141         //check for fault first and then return
142         $name_value_list = $GLOBALS['SugarDepot']->call('depotGetPromotion', array('session_id' => $_SESSION['SugarDepotSessionID']));
143         return $name_value_list;
144      }
145
146     /**
147      * A generic function which given a category_id some filter will
148      * return an object which contains categories and packages
149      *
150      * @param category_id  the category_id to fetch
151      * @param filter       a filter which will limit theh number of results returned
152      * @return categories_and_packages
153      * @see categories_and_packages
154     */
155     function getCategoryPackages($category_id, $filter = array()){
156         PackageManagerComm::initialize();
157         //check for fault
158          return $GLOBALS['SugarDepot']->call('depotGetCategoriesPackages', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'filter' => $filter));
159     }
160
161     /**
162      * Return a list of child categories to the parent specified in category_id
163      *
164      * @param category_id  the parent category_id
165      * @param filter       a filter which will limit theh number of results returned
166      * @return categories_and_packages
167      * @see categories_and_packages
168      */
169     function getCategories($category_id, $filter = array()){
170         PackageManagerComm::initialize();
171         //check for fault
172         return $GLOBALS['SugarDepot']->call('depotGetCategories', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'filter' => $filter));
173     }
174
175     /**
176      * Return a list of packages which belong to the parent category_id
177      *
178      * @param category_id  the category_id to fetch
179      * @param filter       a filter which will limit theh number of results returned
180      * @return packages
181      * @see packages
182     */
183     function getPackages($category_id, $filter = array()){
184         PackageManagerComm::initialize();
185         //check for fault
186          return $GLOBALS['SugarDepot']->call('depotGetPackages', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'filter' => $filter));
187     }
188
189     /**
190      * Return a list of releases belong to a package
191      *
192      * @param category_id  the category_id to fetch
193      * @param package_id  the package id which the release belongs to
194      * @return packages
195      * @see packages
196     */
197     function getReleases($category_id, $package_id, $filter = array()){
198         PackageManagerComm::initialize();
199          //check for fault
200          return $GLOBALS['SugarDepot']->call('depotGetReleases', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'package_id' => $package_id, 'filter' => $filter));
201     }
202
203     /**
204      * Download a given release
205      *
206      * @param category_id  the category_id to fetch
207      * @param package_id  the package id which the release belongs to
208      * @param release_id  the release we want to download
209      * @return download
210      * @see download
211     */
212     function download($category_id, $package_id, $release_id){
213         PackageManagerComm::initialize();
214          //check for fault
215          return $GLOBALS['SugarDepot']->call('depotDownloadRelease', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'package_id' => $package_id, 'release_id' => $release_id));
216     }
217
218     /**
219      * Add a requested download to the queue
220      *
221      * @param category_id  the category_id to fetch
222      * @param package_id  the package id which the release belongs to
223      * @param release_id  the release we want to download
224      * @return the filename to download
225      */
226     function addDownload($category_id, $package_id, $release_id){
227         PackageManagerComm::initialize();
228          //check for fault
229          return $GLOBALS['SugarDepot']->call('depotAddDownload', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'package_id' => $package_id, 'release_id' => $release_id, 'download_key' => '123'));
230     }
231
232     /**
233      * Call the PackageManagerDownloader function which uses curl in order to download the specified file
234      *
235      * @param filename  the file to download
236      * @return path to downloaded file
237      */
238     static public function performDownload($filename){
239         PackageManagerComm::initialize();
240          //check for fault
241          $GLOBALS['log']->debug("Performing download from depot: Session ID: ".$_SESSION['SugarDepotSessionID']." Filename: ".$filename);
242          return PackageManagerDownloader::download($_SESSION['SugarDepotSessionID'], $filename);
243     }
244
245     /**
246      * Retrieve documentation for the given release or package
247      *
248      * @param package_id        the specified package to retrieve documentation
249      * @param release_id        the specified release to retrieve documentation
250      *
251      * @return documents
252      */
253     function getDocumentation($package_id, $release_id){
254          PackageManagerComm::initialize();
255          //check for fault
256          return $GLOBALS['SugarDepot']->call('depotGetDocumentation', array('session_id' => $_SESSION['SugarDepotSessionID'], 'package_id' => $package_id, 'release_id' => $release_id));
257     }
258
259     function getTermsAndConditions(){
260          PackageManagerComm::initialize(false);
261           return $GLOBALS['SugarDepot']->call('depotTermsAndConditions',array());
262     }
263
264     /**
265      * Log that the user has clicked on a document
266      *
267      * @param document_id       the document the user has clicked on
268      */
269     function downloadedDocumentation($document_id){
270          PackageManagerComm::initialize();
271          //check for fault
272          $GLOBALS['log']->debug("Logging Document: ".$document_id);
273          $GLOBALS['SugarDepot']->call('depotDownloadedDocumentation', array('session_id' => $_SESSION['SugarDepotSessionID'], 'document_id' => $document_id));
274     }
275
276         /**
277          * Send the list of installed objects, could be patches, or modules, .. to the depot and allow the depot to send back
278          * a list of corresponding updates
279          *
280          * @param objects_to_check      an array of name_value_lists which contain the appropriate values
281          *                                                      which will allow the depot to check for updates
282          *
283          * @return array of name_value_lists of corresponding updates
284          */
285         function checkForUpdates($objects_to_check){
286                 PackageManagerComm::initialize();
287          //check for fault
288          return $GLOBALS['SugarDepot']->call('depotCheckForUpdates', array('session_id' => $_SESSION['SugarDepotSessionID'], 'objects' => $objects_to_check));
289         }
290      /**
291      * Ping the server to determine if we have established proper communication
292      *
293      * @return true if we can communicate with the server and false otherwise
294     */
295      function isAlive(){
296         PackageManagerComm::initialize(false);
297
298         $status = $GLOBALS['SugarDepot']->call('sugarPing', array());
299         if(empty($status) || $GLOBALS['SugarDepot']->getError() || $status != ACTIVE_STATUS){
300             return false;
301         }else{
302             return true;
303         }
304      }
305      ////////// END: Base Functions for Communicating with the depot
306      ////////////////////////////////////////////////////////
307 }
308
309 ?>