]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Administration/updater_utils.php
Release 6.3.1
[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                 $query="select count(*) count from users where status='Active' and deleted=0 and is_admin='1'";
77                 $result=$db->query($query, 'fetching admin count', false);
78                 $row = $db->fetchByAssoc($result);
79                 if(!empty($row)) {
80                         $info['admin_users'] = $row['count'];
81                 }
82                 if(empty($authLevel)){
83                         $authLevel = 0;
84                 }
85                 $query="select count(*) count from users";
86                 $result=$db->query($query, 'fetching all users count', false);
87                 $row = $db->fetchByAssoc($result);
88
89                 if(!empty($row)) {
90                         $info['registered_users'] = $row['count'];
91                 }
92                 $lastMonth = db_convert("'". $timedate->getNow()->modify("-30 days")->asDb(false) . "'", 'datetime');
93                 if( !$send_usage_info){
94                         $info['users_active_30_days'] = -1;
95                 }
96                 else{
97                         $query = "SELECT count( DISTINCT users.id ) user_count FROM tracker, users WHERE users.id = tracker.user_id AND  tracker.date_modified >= $lastMonth";
98                         $result=$db->query($query, 'fetching last 30 users count', false);
99                         $row = $db->fetchByAssoc($result);
100                         $info['users_active_30_days'] = $row['user_count'];
101
102                 }
103
104
105
106
107                 if(!$send_usage_info){
108                         $info['latest_tracker_id'] = -1;
109                 }else{
110                         $query="select id from tracker order by date_modified desc";
111                         $id=$db->getOne($query,'fetching most recent tracker entry',false);
112                         if ( $id !== false )
113                             $info['latest_tracker_id'] = $id;
114                 }
115
116                 $dbManager = &DBManagerFactory::getInstance();
117                 $info['db_type']=$sugar_config['dbconfig']['db_type'];
118                 $info['db_version']=$dbManager->version();
119         }
120         if(file_exists('distro.php')){
121                 include('distro.php');
122                 if(!empty($distro_name))$info['distro_name'] = $distro_name;
123         }
124         $info['auth_level'] = $authLevel;
125         $info['os'] = php_uname('s');
126         $info['os_version'] = php_uname('r');
127         $info['timezone_u'] = $GLOBALS['current_user']->getPreference('timezone');
128         $info['timezone'] = date('e');
129         if($info['timezone'] == 'e'){
130                 $info['timezone'] = date('T');
131         }
132         return $info;
133
134 }
135
136 function getBaseSystemInfo($send_usage_info=true){
137     global $authLevel;
138     include('sugar_version.php');
139     $info=array();
140
141     if($send_usage_info){
142         $info['sugar_db_version']=$sugar_db_version;
143     }
144     $info['sugar_version']=$sugar_version;
145     $info['sugar_flavor']=$sugar_flavor;
146     $info['auth_level'] = $authLevel;
147
148
149
150     return $info;
151
152
153 }
154
155 function check_now($send_usage_info=true, $get_request_data=false, $response_data = false, $from_install=false ) {
156         global $sugar_config, $timedate;
157         global $db, $license;
158
159
160         $return_array=array();
161     if(!$from_install && empty($license))loadLicense(true);
162
163         if(!$response_data){
164
165         if($from_install){
166                 $info = getBaseSystemInfo(false);
167
168         }else{
169             $info = getSystemInfo($send_usage_info);
170         }
171
172                 require_once('include/nusoap/nusoap.php');
173
174                 $GLOBALS['log']->debug('USING HTTPS TO CONNECT TO HEARTBEAT');
175                 $sclient = new nusoapclient('https://updates.sugarcrm.com/heartbeat/soap.php', false, false, false, false, false, 15, 15);
176                 $ping = $sclient->call('sugarPing', array());
177                 if(empty($ping) || $sclient->getError()){
178                         $sclient = '';
179                 }
180
181                 if(empty($sclient)){
182                         $GLOBALS['log']->debug('USING HTTP TO CONNECT TO HEARTBEAT');
183                         $sclient = new nusoapclient('http://updates.sugarcrm.com/heartbeat/soap.php', false, false, false, false, false, 15, 15);
184                 }
185
186
187
188
189
190
191                         $key = '4829482749329';
192
193
194
195                 $encoded = sugarEncode($key, serialize($info));
196
197                 if($get_request_data){
198                         $request_data = array('key'=>$key, 'data'=>$encoded);
199                         return serialize($request_data);
200                 }
201                 $encodedResult = $sclient->call('sugarHome', array('key'=>$key, 'data'=>$encoded));
202
203         }else{
204                 $encodedResult =        $response_data['data'];
205                 $key = $response_data['key'];
206
207         }
208
209         if($response_data || !$sclient->getError()){
210                 $serializedResultData = sugarDecode($key,$encodedResult);
211                 $resultData = unserialize($serializedResultData);
212                 if($response_data && empty($resultData))
213                 {
214                         $resultData = array();
215                         $resultData['validation'] = 'invalid validation key';
216                 }
217         }else
218         {
219                 $resultData = array();
220                 $resultData['versions'] = array();
221
222         }
223
224         if($response_data || !$sclient->getError() )
225         {
226                 if(!empty($resultData['msg'])){
227                         if(!empty($resultData['msg']['admin'])){
228                                 $license->saveSetting('license', 'msg_admin', base64_encode($resultData['msg']['admin']));
229                         }else{
230                                 $license->saveSetting('license', 'msg_admin','');
231                         }
232                         if(!empty($resultData['msg']['all'])){
233                                 $license->saveSetting('license', 'msg_all', base64_encode($resultData['msg']['all']));
234                         }else{
235                                 $license->saveSetting('license', 'msg_all','');
236                         }
237                 }else{
238                         $license->saveSetting('license', 'msg_admin','');
239                         $license->saveSetting('license', 'msg_all','');
240                 }
241                 $license->saveSetting('license', 'last_validation', 'success');
242                 unset($_SESSION['COULD_NOT_CONNECT']);
243         }
244         else
245         {
246                 $resultData = array();
247                 $resultData['versions'] = array();
248
249                 $license->saveSetting('license', 'last_connection_fail', TimeDate::getInstance()->nowDb());
250                 $license->saveSetting('license', 'last_validation', 'no_connection');
251
252                 if( empty($license->settings['license_last_validation_success']) && empty($license->settings['license_last_validation_fail']) && empty($license->settings['license_vk_end_date'])){
253                         $license->saveSetting('license', 'vk_end_date', TimeDate::getInstance()->nowDb());
254
255                         $license->saveSetting('license', 'validation_key', base64_encode(serialize(array('verified'=>false))));
256                 }
257                 $_SESSION['COULD_NOT_CONNECT'] =TimeDate::getInstance()->nowDb();
258
259         }
260         if(!empty($resultData['versions'])){
261
262                 $license->saveSetting('license', 'latest_versions',base64_encode(serialize($resultData['versions'])));
263         }else{
264                 $resultData['versions'] = array();
265                 $license->saveSetting('license', 'latest_versions','')  ;
266         }
267
268
269
270
271         include('sugar_version.php');
272
273         if(sizeof($resultData) == 1 && !empty($resultData['versions'][0]['version'])
274         && compareVersions($sugar_version, $resultData['versions'][0]['version']))
275         {
276                 $resultData['versions'][0]['version'] = $sugar_version;
277                 $resultData['versions'][0]['description'] = "You have the latest version.";
278         }
279
280
281         return $resultData['versions'];
282 }
283 /*
284  * returns true if $ver1 > $ver2
285  */
286 function compareVersions($ver1, $ver2)
287 {
288     $ver_arr_1 = preg_split("/[^0-9]/", $ver1); 
289     $ver_arr_2 = preg_split("/[^0-9]/", $ver2); 
290     $count = (count($ver_arr_1) >= count($ver_arr_2)) ? count($ver_arr_1) : count($ver_arr_2);
291     for ($i = 0; $i < $count; $i++)
292     {
293         if (!isset($ver_arr_1[$i]))
294             $ver_arr_1[$i] = 0;
295         
296         if (!isset($ver_arr_2[$i]))
297             $ver_arr_2[$i] = 0;
298         
299         if ($ver_arr_1[$i] > $ver_arr_2[$i])
300             return true;
301     }
302     return false;
303 }
304 function set_CheckUpdates_config_setting($value) {
305
306
307         $admin=new Administration();
308         $admin->saveSetting('Update','CheckUpdates',$value);
309 }
310 /* return's value for the 'CheckUpdates' config setting
311 * if the setting does not exist one gets created with a default value of automatic.
312 */
313 function get_CheckUpdates_config_setting() {
314
315         $checkupdates='automatic';
316
317
318         $admin=new Administration();
319         $admin=$admin->retrieveSettings('Update',true);
320         if (empty($admin->settings) or empty($admin->settings['Update_CheckUpdates'])) {
321                 $admin->saveSetting('Update','CheckUpdates','automatic');
322         } else {
323                 $checkupdates=$admin->settings['Update_CheckUpdates'];
324         }
325         return $checkupdates;
326 }
327
328 function set_last_check_version_config_setting($value) {
329
330
331         $admin=new Administration();
332         $admin->saveSetting('Update','last_check_version',$value);
333 }
334 function get_last_check_version_config_setting() {
335
336
337
338         $admin=new Administration();
339         $admin=$admin->retrieveSettings('Update');
340         if (empty($admin->settings) or empty($admin->settings['Update_last_check_version'])) {
341                 return null;
342         } else {
343                 return $admin->settings['Update_last_check_version'];
344         }
345 }
346
347
348 function set_last_check_date_config_setting($value) {
349
350
351         $admin=new Administration();
352         $admin->saveSetting('Update','last_check_date',$value);
353 }
354 function get_last_check_date_config_setting() {
355
356
357
358         $admin=new Administration();
359         $admin=$admin->retrieveSettings('Update');
360         if (empty($admin->settings) or empty($admin->settings['Update_last_check_date'])) {
361                 return 0;
362         } else {
363                 return $admin->settings['Update_last_check_date'];
364         }
365 }
366
367 function set_sugarbeat($value) {
368         global $sugar_config;
369         $_SUGARBEAT="sugarbeet";
370         $sugar_config[$_SUGARBEAT] = $value;
371         write_array_to_file( "sugar_config", $sugar_config, "config.php" );
372 }
373 function get_sugarbeat() {
374
375
376         global $sugar_config;
377         $_SUGARBEAT="sugarbeet";
378
379         if (isset($sugar_config[$_SUGARBEAT]) && $sugar_config[$_SUGARBEAT] == false) {
380         return false;
381         }
382         return true;
383
384 }
385
386
387
388 function shouldCheckSugar(){
389         global $license, $timedate;
390         if(
391
392         get_CheckUpdates_config_setting() == 'automatic' ){
393                 return true;
394         }
395
396         return false;
397 }
398
399
400
401 function loadLicense($firstLogin=false){
402
403         $GLOBALS['license']=new Administration();
404         $GLOBALS['license']=$GLOBALS['license']->retrieveSettings('license', $firstLogin);
405
406 }
407
408 function loginLicense(){
409         global $current_user, $license, $authLevel;
410         loadLicense(true);
411
412         $authLevel = 0;
413
414         if (shouldCheckSugar()) {
415
416
417                 $last_check_date=get_last_check_date_config_setting();
418                 $current_date_time=time();
419                 $time_period=3*23*3600 ;
420                 if (($current_date_time - $last_check_date) > $time_period
421                 ) {
422                         $version = check_now(get_sugarbeat());
423
424                         unset($_SESSION['license_seats_needed']);
425                         loadLicense();
426                         set_last_check_date_config_setting("$current_date_time");
427                         include('sugar_version.php');
428
429                         if(!empty($version)&& count($version) == 1 && $version[0]['version'] > $sugar_version  && is_admin($current_user))
430                         {
431                                 //set session variables.
432                                 $_SESSION['available_version']=$version[0]['version'];
433                                 $_SESSION['available_version_description']=$version[0]['description'];
434                                 set_last_check_version_config_setting($version[0]['version']);
435                         }
436                 }
437         }
438
439
440 }
441
442
443
444
445
446
447
448
449 ?>