]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/updater_utils.php
Release 6.4.0
[Github/sugarcrm.git] / modules / Administration / updater_utils.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40 ********************************************************************************/
41 require_once('include/utils/encryption_utils.php');
42
43 function getSystemInfo($send_usage_info=true){
44         global $sugar_config;
45         global $db, $authLevel, $administration, $timedate;
46         $info=array();
47         $info = getBaseSystemInfo($send_usage_info);
48     if($send_usage_info){
49                 if($authLevel > 0){
50                         if(isset($_SERVER['SERVER_ADDR']))
51                                 $info['ip_address'] = $_SERVER['SERVER_ADDR'];
52                         else
53                                 $info['ip_address'] = '127.0.0.1';
54                 }
55                 $info['application_key']=$sugar_config['unique_key'];
56                 $info['php_version']=phpversion();
57                 if(isset($_SERVER['SERVER_SOFTWARE'])) {
58                         $info['server_software'] = $_SERVER['SERVER_SOFTWARE'];
59                 } // if
60
61                 //get user count.
62
63                 $user_list = get_user_array(false, "Active", "", false, null, " AND is_group=0 AND portal_only=0 ", false);
64
65
66
67                 $info['users']=count($user_list);
68                 if(empty($administration)){
69
70                         $administration = new Administration();
71                 }
72                 $administration->retrieveSettings('system');
73                 $info['system_name'] = (!empty($administration->settings['system_name']))?substr($administration->settings['system_name'], 0 ,255):'';
74
75
76                 $result=$db->getOne("select count(*) count from users where status='Active' and deleted=0 and is_admin='1'", false, 'fetching admin count');
77                 if($result !== false) {
78                         $info['admin_users'] = $result;
79                 }
80
81                 if(empty($authLevel)){
82                         $authLevel = 0;
83                 }
84
85                 $result=$db->getOne("select count(*) count from users", false, 'fetching all users count');
86                 if($result !== false) {
87                         $info['registered_users'] = $result;
88                 }
89
90                 $lastMonth = $db->convert("'". $timedate->getNow()->modify("-30 days")->asDb(false) . "'", 'datetime');
91                 if( !$send_usage_info) {
92                         $info['users_active_30_days'] = -1;
93                 } else {
94                         $info['users_active_30_days'] = $db->getOne("SELECT count( DISTINCT users.id ) user_count FROM tracker, users WHERE users.id = tracker.user_id AND  tracker.date_modified >= $lastMonth", false, 'fetching last 30 users count');
95                 }
96
97
98
99
100                 if(!$send_usage_info){
101                         $info['latest_tracker_id'] = -1;
102                 }else{
103                         $id=$db->getOne("select id from tracker order by date_modified desc", false, 'fetching most recent tracker entry');
104                         if ( $id !== false )
105                             $info['latest_tracker_id'] = $id;
106                 }
107
108                 $info['db_type']=$sugar_config['dbconfig']['db_type'];
109                 $info['db_version']=$db->version();
110         }
111         if(file_exists('distro.php')){
112                 include('distro.php');
113                 if(!empty($distro_name))$info['distro_name'] = $distro_name;
114         }
115         $info['auth_level'] = $authLevel;
116         $info['os'] = php_uname('s');
117         $info['os_version'] = php_uname('r');
118         $info['timezone_u'] = $GLOBALS['current_user']->getPreference('timezone');
119         $info['timezone'] = date('e');
120         if($info['timezone'] == 'e'){
121                 $info['timezone'] = date('T');
122         }
123         return $info;
124
125 }
126
127 function getBaseSystemInfo($send_usage_info=true){
128     global $authLevel;
129     include('sugar_version.php');
130     $info=array();
131
132     if($send_usage_info){
133         $info['sugar_db_version']=$sugar_db_version;
134     }
135     $info['sugar_version']=$sugar_version;
136     $info['sugar_flavor']=$sugar_flavor;
137     $info['auth_level'] = $authLevel;
138
139
140
141     return $info;
142
143
144 }
145
146 function check_now($send_usage_info=true, $get_request_data=false, $response_data = false, $from_install=false ) {
147         global $sugar_config, $timedate;
148         global $db, $license;
149
150
151         $return_array=array();
152     if(!$from_install && empty($license))loadLicense(true);
153
154         if(!$response_data){
155
156         if($from_install){
157                 $info = getBaseSystemInfo(false);
158
159         }else{
160             $info = getSystemInfo($send_usage_info);
161         }
162
163                 require_once('include/nusoap/nusoap.php');
164
165                 $GLOBALS['log']->debug('USING HTTPS TO CONNECT TO HEARTBEAT');
166                 $sclient = new nusoapclient('https://updates.sugarcrm.com/heartbeat/soap.php', false, false, false, false, false, 15, 15);
167                 $ping = $sclient->call('sugarPing', array());
168                 if(empty($ping) || $sclient->getError()){
169                         $sclient = '';
170                 }
171
172                 if(empty($sclient)){
173                         $GLOBALS['log']->debug('USING HTTP TO CONNECT TO HEARTBEAT');
174                         $sclient = new nusoapclient('http://updates.sugarcrm.com/heartbeat/soap.php', false, false, false, false, false, 15, 15);
175                 }
176
177
178
179
180
181
182                         $key = '4829482749329';
183
184
185
186                 $encoded = sugarEncode($key, serialize($info));
187
188                 if($get_request_data){
189                         $request_data = array('key'=>$key, 'data'=>$encoded);
190                         return serialize($request_data);
191                 }
192                 $encodedResult = $sclient->call('sugarHome', array('key'=>$key, 'data'=>$encoded));
193
194         }else{
195                 $encodedResult =        $response_data['data'];
196                 $key = $response_data['key'];
197
198         }
199
200         if($response_data || !$sclient->getError()){
201                 $serializedResultData = sugarDecode($key,$encodedResult);
202                 $resultData = unserialize($serializedResultData);
203                 if($response_data && empty($resultData))
204                 {
205                         $resultData = array();
206                         $resultData['validation'] = 'invalid validation key';
207                 }
208         }else
209         {
210                 $resultData = array();
211                 $resultData['versions'] = array();
212
213         }
214
215         if($response_data || !$sclient->getError() )
216         {
217                 if(!empty($resultData['msg'])){
218                         if(!empty($resultData['msg']['admin'])){
219                                 $license->saveSetting('license', 'msg_admin', base64_encode($resultData['msg']['admin']));
220                         }else{
221                                 $license->saveSetting('license', 'msg_admin','');
222                         }
223                         if(!empty($resultData['msg']['all'])){
224                                 $license->saveSetting('license', 'msg_all', base64_encode($resultData['msg']['all']));
225                         }else{
226                                 $license->saveSetting('license', 'msg_all','');
227                         }
228                 }else{
229                         $license->saveSetting('license', 'msg_admin','');
230                         $license->saveSetting('license', 'msg_all','');
231                 }
232                 $license->saveSetting('license', 'last_validation', 'success');
233                 unset($_SESSION['COULD_NOT_CONNECT']);
234         }
235         else
236         {
237                 $resultData = array();
238                 $resultData['versions'] = array();
239
240                 $license->saveSetting('license', 'last_connection_fail', TimeDate::getInstance()->nowDb());
241                 $license->saveSetting('license', 'last_validation', 'no_connection');
242
243                 if( empty($license->settings['license_last_validation_success']) && empty($license->settings['license_last_validation_fail']) && empty($license->settings['license_vk_end_date'])){
244                         $license->saveSetting('license', 'vk_end_date', TimeDate::getInstance()->nowDb());
245
246                         $license->saveSetting('license', 'validation_key', base64_encode(serialize(array('verified'=>false))));
247                 }
248                 $_SESSION['COULD_NOT_CONNECT'] =TimeDate::getInstance()->nowDb();
249
250         }
251         if(!empty($resultData['versions'])){
252
253                 $license->saveSetting('license', 'latest_versions',base64_encode(serialize($resultData['versions'])));
254         }else{
255                 $resultData['versions'] = array();
256                 $license->saveSetting('license', 'latest_versions','')  ;
257         }
258
259
260
261
262         include('sugar_version.php');
263
264         if(sizeof($resultData) == 1 && !empty($resultData['versions'][0]['version'])
265         && compareVersions($sugar_version, $resultData['versions'][0]['version']))
266         {
267                 $resultData['versions'][0]['version'] = $sugar_version;
268                 $resultData['versions'][0]['description'] = "You have the latest version.";
269         }
270
271
272         return $resultData['versions'];
273 }
274 /*
275  * returns true if $ver1 > $ver2
276  */
277 function compareVersions($ver1, $ver2)
278 {
279     if(!preg_match_all("/[0-9]/", $ver1, $matches1))
280     {
281         return false;
282     }
283
284     if(!preg_match_all("/[0-9]/", $ver2, $matches2))
285     {
286         return true;
287     }
288
289     //Now recreate string with only numbers
290     $ver1 = implode('', $matches1[0]);
291     $ver2 = implode('', $matches2[0]);
292
293     $len1 = strlen($ver1);
294     $len2 = strlen($ver2);
295
296     //Now apply padding
297     if($len1 > $len2) {
298         $ver2 = str_pad($ver2, $len1, '0');
299     } else if($len2 > $len1) {
300         $ver1 = str_pad($ver1, $len2, '0');
301     }
302
303     //Return result
304     return (int)$ver1 > (int)$ver2;
305 }
306 function set_CheckUpdates_config_setting($value) {
307
308
309         $admin=new Administration();
310         $admin->saveSetting('Update','CheckUpdates',$value);
311 }
312 /* return's value for the 'CheckUpdates' config setting
313 * if the setting does not exist one gets created with a default value of automatic.
314 */
315 function get_CheckUpdates_config_setting() {
316
317         $checkupdates='automatic';
318
319
320         $admin=new Administration();
321         $admin=$admin->retrieveSettings('Update',true);
322         if (empty($admin->settings) or empty($admin->settings['Update_CheckUpdates'])) {
323                 $admin->saveSetting('Update','CheckUpdates','automatic');
324         } else {
325                 $checkupdates=$admin->settings['Update_CheckUpdates'];
326         }
327         return $checkupdates;
328 }
329
330 function set_last_check_version_config_setting($value) {
331
332
333         $admin=new Administration();
334         $admin->saveSetting('Update','last_check_version',$value);
335 }
336 function get_last_check_version_config_setting() {
337
338
339
340         $admin=new Administration();
341         $admin=$admin->retrieveSettings('Update');
342         if (empty($admin->settings) or empty($admin->settings['Update_last_check_version'])) {
343                 return null;
344         } else {
345                 return $admin->settings['Update_last_check_version'];
346         }
347 }
348
349
350 function set_last_check_date_config_setting($value) {
351
352
353         $admin=new Administration();
354         $admin->saveSetting('Update','last_check_date',$value);
355 }
356 function get_last_check_date_config_setting() {
357
358
359
360         $admin=new Administration();
361         $admin=$admin->retrieveSettings('Update');
362         if (empty($admin->settings) or empty($admin->settings['Update_last_check_date'])) {
363                 return 0;
364         } else {
365                 return $admin->settings['Update_last_check_date'];
366         }
367 }
368
369 function set_sugarbeat($value) {
370         global $sugar_config;
371         $_SUGARBEAT="sugarbeet";
372         $sugar_config[$_SUGARBEAT] = $value;
373         write_array_to_file( "sugar_config", $sugar_config, "config.php" );
374 }
375 function get_sugarbeat() {
376
377
378         global $sugar_config;
379         $_SUGARBEAT="sugarbeet";
380
381         if (isset($sugar_config[$_SUGARBEAT]) && $sugar_config[$_SUGARBEAT] == false) {
382         return false;
383         }
384         return true;
385
386 }
387
388
389
390 function shouldCheckSugar(){
391         global $license, $timedate;
392         if(
393
394         get_CheckUpdates_config_setting() == 'automatic' ){
395                 return true;
396         }
397
398         return false;
399 }
400
401
402
403 function loadLicense($firstLogin=false){
404
405         $GLOBALS['license']=new Administration();
406         $GLOBALS['license']=$GLOBALS['license']->retrieveSettings('license', $firstLogin);
407
408 }
409
410 function loginLicense(){
411         global $current_user, $license, $authLevel;
412         loadLicense(true);
413
414         $authLevel = 0;
415
416         if (shouldCheckSugar()) {
417
418
419                 $last_check_date=get_last_check_date_config_setting();
420                 $current_date_time=time();
421                 $time_period=3*23*3600 ;
422                 if (($current_date_time - $last_check_date) > $time_period
423                 ) {
424                         $version = check_now(get_sugarbeat());
425
426                         unset($_SESSION['license_seats_needed']);
427                         loadLicense();
428                         set_last_check_date_config_setting("$current_date_time");
429                         include('sugar_version.php');
430
431                         if(!empty($version)&& count($version) == 1 && $version[0]['version'] > $sugar_version  && is_admin($current_user))
432                         {
433                                 //set session variables.
434                                 $_SESSION['available_version']=$version[0]['version'];
435                                 $_SESSION['available_version_description']=$version[0]['description'];
436                                 set_last_check_version_config_setting($version[0]['version']);
437                         }
438                 }
439         }
440
441
442 }
443
444
445
446
447
448
449
450
451 ?>