]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Configurator/Configurator.php
Release 6.5.15
[Github/sugarcrm.git] / modules / Configurator / Configurator.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
42 class Configurator {
43         var $config = '';
44         var $override = '';
45         var $allow_undefined = array ('stack_trace_errors', 'export_delimiter', 'use_real_names', 'developerMode', 'default_module_favicon', 'authenticationClass', 'SAML_loginurl', 'SAML_X509Cert', 'dashlet_auto_refresh_min', 'show_download_tab', 'enable_action_menu');
46         var $errors = array ('main' => '');
47         var $logger = NULL;
48         var $previous_sugar_override_config_array = array();
49         var $useAuthenticationClass = false;
50     protected $error = null;
51
52         function Configurator() {
53                 $this->loadConfig();
54         }
55
56         function loadConfig() {
57                 $this->logger = LoggerManager::getLogger();
58                 global $sugar_config;
59                 $this->config = $sugar_config;
60         }
61
62         function populateFromPost() {
63                 $sugarConfig = SugarConfig::getInstance();
64                 foreach ($_POST as $key => $value) {
65                         if ($key == "logger_file_ext") {
66                             $trim_value = preg_replace('/.*\.([^\.]+)$/', '\1', $value);
67                             if(in_array($trim_value, $this->config['upload_badext'])) {
68                                 $GLOBALS['log']->security("Invalid log file extension: trying to use invalid file extension '$value'.");
69                                 continue;
70                             }
71                         }
72                         if (isset ($this->config[$key]) || in_array($key, $this->allow_undefined)) {
73                                 if (strcmp("$value", 'true') == 0) {
74                                         $value = true;
75                                 }
76                                 if (strcmp("$value", 'false') == 0) {
77                                         $value = false;
78                                 }
79                 $this->config[$key] = $value;
80                         } else {
81                 $v = $sugarConfig->get(str_replace('_', '.', $key));
82             if ($v  !== null){
83                            setDeepArrayValue($this->config, $key, $value);
84                         }}
85
86                 }
87
88         }
89
90         function handleOverride($fromParseLoggerSettings=false) {
91                 global $sugar_config, $sugar_version;
92                 $sc = SugarConfig::getInstance();
93                 $overrideArray = $this->readOverride();
94                 $this->previous_sugar_override_config_array = $overrideArray;
95                 $diffArray = deepArrayDiff($this->config, $sugar_config);
96                 $overrideArray = sugarArrayMergeRecursive($overrideArray, $diffArray);
97
98                 // To remember checkbox state
99       if (!$this->useAuthenticationClass && !$fromParseLoggerSettings) {
100          if (isset($overrideArray['authenticationClass']) &&
101             $overrideArray['authenticationClass'] == 'SAMLAuthenticate') {
102           unset($overrideArray['authenticationClass']);
103         }
104       }
105
106                 $overideString = "<?php\n/***CONFIGURATOR***/\n";
107
108                 sugar_cache_put('sugar_config', $this->config);
109                 $GLOBALS['sugar_config'] = $this->config;
110
111                 //print_r($overrideArray);
112         //Bug#53013: Clean the tpl cache if action menu style has been changed.
113         if( isset($overrideArray['enable_action_menu']) &&
114                 ( !isset($this->previous_sugar_override_config_array['enable_action_menu']) ||
115                     $overrideArray['enable_action_menu'] != $this->previous_sugar_override_config_array['enable_action_menu'] )
116         ) {
117             require_once('modules/Administration/QuickRepairAndRebuild.php');
118             $repair = new RepairAndClear;
119             $repair->module_list = array();
120             $repair->clearTpls();
121         }
122
123                 foreach($overrideArray as $key => $val) {
124                         if (in_array($key, $this->allow_undefined) || isset ($sugar_config[$key])) {
125                                 if (is_string($val) && strcmp($val, 'true') == 0) {
126                                         $val = true;
127                                         $this->config[$key] = $val;
128                                 }
129                                 if (is_string($val) && strcmp($val, 'false') == 0) {
130                                         $val = false;
131                                         $this->config[$key] = false;
132                                 }
133                         }
134                         $overideString .= override_value_to_string_recursive2('sugar_config', $key, $val);
135                 }
136                 $overideString .= '/***CONFIGURATOR***/';
137
138                 $this->saveOverride($overideString);
139                 if(isset($this->config['logger']['level']) && $this->logger) $this->logger->setLevel($this->config['logger']['level']);
140         }
141
142         //bug #27947 , if previous $sugar_config['stack_trace_errors'] is true and now we disable it , we should clear all the cache.
143         function clearCache(){
144                 global $sugar_config, $sugar_version;
145                 $currentConfigArray = $this->readOverride();
146                 foreach($currentConfigArray as $key => $val) {
147                         if (in_array($key, $this->allow_undefined) || isset ($sugar_config[$key])) {
148                                 if (empty($val) ) {
149                                         if(!empty($this->previous_sugar_override_config_array['stack_trace_errors']) && $key == 'stack_trace_errors'){
150                                                 require_once('include/TemplateHandler/TemplateHandler.php');
151                                                 TemplateHandler::clearAll();
152                                                 return;
153                                         }
154                                 }
155                         }
156                 }
157         }
158
159         function saveConfig() {
160         if($this->saveImages() === false)
161         {
162             return false;
163         }
164
165                 $this->populateFromPost();
166                 $this->handleOverride();
167                 $this->clearCache();
168         }
169
170         function readOverride() {
171                 $sugar_config = array();
172                 if (file_exists('config_override.php')) {
173                     if ( !is_readable('config_override.php') ) {
174                         $GLOBALS['log']->fatal("Unable to read the config_override.php file. Check the file permissions");
175                     }
176                 else {
177                     include('config_override.php');
178                 }
179                 }
180                 return $sugar_config;
181         }
182         function saveOverride($override) {
183         require_once('install/install_utils.php');
184             if ( !file_exists('config_override.php') ) {
185                 touch('config_override.php');
186             }
187             if ( !(make_writable('config_override.php')) ||  !(is_writable('config_override.php')) ) {
188                 $GLOBALS['log']->fatal("Unable to write to the config_override.php file. Check the file permissions");
189                 return;
190             }
191                 $fp = sugar_fopen('config_override.php', 'w');
192                 fwrite($fp, $override);
193                 fclose($fp);
194         }
195
196         function overrideClearDuplicates($array_name, $key) {
197                 if (!empty ($this->override)) {
198                         $pattern = '/.*CONFIGURATOR[^\$]*\$'.$array_name.'\[\''.$key.'\'\][\ ]*=[\ ]*[^;]*;\n/';
199                         $this->override = preg_replace($pattern, '', $this->override);
200                 } else {
201                         $this->override = "<?php\n\n?>";
202                 }
203
204         }
205
206         function replaceOverride($array_name, $key, $value) {
207                 $GLOBALS[$array_name][$key] = $value;
208                 $this->overrideClearDuplicates($array_name, $key);
209                 $new_entry = '/***CONFIGURATOR***/'.override_value_to_string($array_name, $key, $value);
210                 $this->override = str_replace('?>', "$new_entry\n?>", $this->override);
211         }
212
213         function restoreConfig() {
214                 $this->readOverride();
215                 $this->overrideClearDuplicates('sugar_config', '[a-zA-Z0-9\_]+');
216                 $this->saveOverride();
217                 ob_clean();
218                 header('Location: index.php?action=EditView&module=Configurator');
219         }
220
221         function saveImages() {
222                 if (!empty ($_POST['company_logo'])) {
223             if($this->saveCompanyLogo("upload://".$_POST['company_logo']) === false)
224             {
225                 return false;
226             }
227                 }
228         }
229
230         function checkTempImage($path)
231         {
232         if(!verify_uploaded_image($path)) {
233             $error = translate('LBL_ALERT_TYPE_IMAGE');
234                 $GLOBALS['log']->fatal("A user ({$GLOBALS['current_user']->id}) attempted to use an invalid file for the logo - {$path}");
235             $this->error = $error;
236             return false;
237                 }
238                 return $path;
239         }
240
241     public function getError()
242     {
243         $e = $this->error;
244         $this->error = null;
245         return $e;
246     }
247     /**
248      * Saves the company logo to the custom directory for the default theme, so all themes can use it
249      *
250      * @param string $path path to the image to set as the company logo image
251      */
252         function saveCompanyLogo($path)
253     {
254         $path = $this->checkTempImage($path);
255         if($path === false)
256         {
257             return false;
258         }
259
260         mkdir_recursive('custom/'.SugarThemeRegistry::current()->getDefaultImagePath(), true);
261         copy($path,'custom/'. SugarThemeRegistry::current()->getDefaultImagePath(). '/company_logo.png');
262         sugar_cache_clear('company_logo_attributes');
263         SugarThemeRegistry::clearAllCaches();
264         }
265         /**
266          * @params : none
267          * @return : An array of logger configuration properties including log size, file extensions etc. See SugarLogger for more details.
268          * Parses the old logger settings from the log4php.properties files.
269          *
270          */
271
272         function parseLoggerSettings(){
273                 if(!function_exists('setDeepArrayValue')){
274                         require('include/utils/array_utils.php');
275                 }
276                 if (file_exists('log4php.properties')) {
277                         $fileContent = file_get_contents('log4php.properties');
278                         $old_props = explode('\n', $fileContent);
279                         $new_props = array();
280                         $key_names=array();
281                         foreach($old_props as $value) {
282                                 if(!empty($value) && !preg_match("/^\/\//", $value)) {
283                                         $temp = explode("=",$value);
284                                         $property = isset( $temp[1])? $temp[1] : array();
285                                         if(preg_match("/log4php.appender.A2.MaxFileSize=/",$value)){
286                                                 setDeepArrayValue($this->config, 'logger_file_maxSize', rtrim( $property));
287                                         }
288                                         elseif(preg_match("/log4php.appender.A2.File=/", $value)){
289                                                 $ext = preg_split("/\./",$property);
290                                                 if(preg_match( "/^\./", $property)){ //begins with .
291                                                         setDeepArrayValue($this->config, 'logger_file_ext', isset($ext[2]) ? '.' . rtrim( $ext[2]):'.log');
292                                                         setDeepArrayValue($this->config, 'logger_file_name', rtrim( ".".$ext[1]));
293                                                 }else{
294                                                         setDeepArrayValue($this->config, 'logger_file_ext', isset($ext[1]) ? '.' . rtrim( $ext[1]):'.log');
295                                                         setDeepArrayValue($this->config, 'logger_file_name', rtrim( $ext[0] ));
296                                                 }
297                                         }elseif(preg_match("/log4php.appender.A2.layout.DateFormat=/",$value)){
298                                                 setDeepArrayValue($this->config, 'logger_file_dateFormat', trim(rtrim( $property), '""'));
299
300                                         }elseif(preg_match("/log4php.rootLogger=/",$value)){
301                                                 $property = explode(",",$property);
302                                                 setDeepArrayValue($this->config, 'logger_level', rtrim( $property[0]));
303                                         }
304                                 }
305                         }
306                         setDeepArrayValue($this->config, 'logger_file_maxLogs', 10);
307                         setDeepArrayValue($this->config, 'logger_file_suffix', "%m_%Y");
308                         $this->handleOverride();
309                         unlink('log4php.properties');
310                         $GLOBALS['sugar_config'] = $this->config; //load the rest of the sugar_config settings.
311                         require_once('include/SugarLogger/SugarLogger.php');
312                         //$logger = new SugarLogger(); //this will create the log file.
313
314                 }
315
316                 if (!isset($this->config['logger']) || empty($this->config['logger'])) {
317                         $this->config['logger'] = array (
318                         'file' => array(
319                                 'ext' => '.log',
320                                 'name' => 'sugarcrm',
321                                 'dateFormat' => '%c',
322                                 'maxSize' => '10MB',
323                                 'maxLogs' => 10,
324                                 'suffix' => ''), // bug51583, change default suffix to blank for backwards comptability
325                         'level' => 'fatal');
326                 }
327                 $this->handleOverride(true);
328
329
330         }
331
332
333
334
335 }
336 ?>