PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/Configurator/Configurator.php

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