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