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