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