2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
37 require_once('include/nusoap/nusoap.php');
38 require_once('ModuleInstall/PackageManager/PackageManagerDownloader.php');
40 define("HTTPS_URL", "https://depot.sugarcrm.com/depot/SugarDepotSoap.php");
41 define("ACTIVE_STATUS", "ACTIVE");
43 class PackageManagerComm{
45 * Initialize the soap client and store in the $GLOBALS object for use
47 * @param login designates whether we want to try to login after we initialize or not
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;
56 //if we do not have a session, then try to login
57 if($login && empty($_SESSION['SugarDepotSessionID'])){
58 PackageManagerComm::login();
63 * Check for errors in the response or error_str
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);
73 * Set the credentials for use during login
75 * @param username Mambo username
76 * @param password Mambo password
77 * @param download_key User's download key
79 function setCredentials($username, $password, $download_key){
80 $_SESSION['SugarDepotUsername'] = $username;
81 $_SESSION['SugarDepotPassword'] = $password;
82 $_SESSION['SugarDepotDownloadKey'] = $download_key;
86 * Clears out the session so we can reauthenticate.
88 function clearSession(){
89 $_SESSION['SugarDepotSessionID'] = null;
90 unset($_SESSION['SugarDepotSessionID']);
92 /////////////////////////////////////////////////////////
93 ////////// BEGIN: Base Functions for Communicating with the depot
97 * @return true if successful, false otherwise
99 function login($terms_checked = true){
100 if(empty($_SESSION['SugarDepotSessionID'])){
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;
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");
125 return $_SESSION['SugarDepotSessionID'];
129 * Logout from the depot
132 PackageManagerComm::initialize();
133 $result = $GLOBALS['SugarDepot']->call('depotLogout', array('session_id' => $_SESSION['SugarDepotSessionID']));
137 * Get all promotions from the depot
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;
147 * A generic function which given a category_id some filter will
148 * return an object which contains categories and packages
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
155 function getCategoryPackages($category_id, $filter = array()){
156 PackageManagerComm::initialize();
158 return $GLOBALS['SugarDepot']->call('depotGetCategoriesPackages', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'filter' => $filter));
162 * Return a list of child categories to the parent specified in category_id
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
169 function getCategories($category_id, $filter = array()){
170 PackageManagerComm::initialize();
172 return $GLOBALS['SugarDepot']->call('depotGetCategories', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'filter' => $filter));
176 * Return a list of packages which belong to the parent category_id
178 * @param category_id the category_id to fetch
179 * @param filter a filter which will limit theh number of results returned
183 function getPackages($category_id, $filter = array()){
184 PackageManagerComm::initialize();
186 return $GLOBALS['SugarDepot']->call('depotGetPackages', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'filter' => $filter));
190 * Return a list of releases belong to a package
192 * @param category_id the category_id to fetch
193 * @param package_id the package id which the release belongs to
197 function getReleases($category_id, $package_id, $filter = array()){
198 PackageManagerComm::initialize();
200 return $GLOBALS['SugarDepot']->call('depotGetReleases', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'package_id' => $package_id, 'filter' => $filter));
204 * Download a given release
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
212 function download($category_id, $package_id, $release_id){
213 PackageManagerComm::initialize();
215 return $GLOBALS['SugarDepot']->call('depotDownloadRelease', array('session_id' => $_SESSION['SugarDepotSessionID'], 'category_id' => $category_id, 'package_id' => $package_id, 'release_id' => $release_id));
219 * Add a requested download to the queue
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
226 function addDownload($category_id, $package_id, $release_id){
227 PackageManagerComm::initialize();
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'));
233 * Call the PackageManagerDownloader function which uses curl in order to download the specified file
235 * @param filename the file to download
236 * @return path to downloaded file
238 static public function performDownload($filename){
239 PackageManagerComm::initialize();
241 $GLOBALS['log']->debug("Performing download from depot: Session ID: ".$_SESSION['SugarDepotSessionID']." Filename: ".$filename);
242 return PackageManagerDownloader::download($_SESSION['SugarDepotSessionID'], $filename);
246 * Retrieve documentation for the given release or package
248 * @param package_id the specified package to retrieve documentation
249 * @param release_id the specified release to retrieve documentation
253 function getDocumentation($package_id, $release_id){
254 PackageManagerComm::initialize();
256 return $GLOBALS['SugarDepot']->call('depotGetDocumentation', array('session_id' => $_SESSION['SugarDepotSessionID'], 'package_id' => $package_id, 'release_id' => $release_id));
259 function getTermsAndConditions(){
260 PackageManagerComm::initialize(false);
261 return $GLOBALS['SugarDepot']->call('depotTermsAndConditions',array());
265 * Log that the user has clicked on a document
267 * @param document_id the document the user has clicked on
269 function downloadedDocumentation($document_id){
270 PackageManagerComm::initialize();
272 $GLOBALS['log']->debug("Logging Document: ".$document_id);
273 $GLOBALS['SugarDepot']->call('depotDownloadedDocumentation', array('session_id' => $_SESSION['SugarDepotSessionID'], 'document_id' => $document_id));
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
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
283 * @return array of name_value_lists of corresponding updates
285 function checkForUpdates($objects_to_check){
286 PackageManagerComm::initialize();
288 return $GLOBALS['SugarDepot']->call('depotCheckForUpdates', array('session_id' => $_SESSION['SugarDepotSessionID'], 'objects' => $objects_to_check));
291 * Ping the server to determine if we have established proper communication
293 * @return true if we can communicate with the server and false otherwise
296 PackageManagerComm::initialize(false);
298 $status = $GLOBALS['SugarDepot']->call('sugarPing', array());
299 if(empty($status) || $GLOBALS['SugarDepot']->getError() || $status != ACTIVE_STATUS){
305 ////////// END: Base Functions for Communicating with the depot
306 ////////////////////////////////////////////////////////