PageRenderTime 30ms CodeModel.GetById 101ms app.highlight 97ms RepoModel.GetById 13ms app.codeStats 8ms

/include/utils.php

https://github.com/vincentamari/SuperSweetAdmin
PHP | 4192 lines | 3577 code | 251 blank | 364 comment | 295 complexity | 1e9d2e6845f607b9386c0f95d3fad748 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/*********************************************************************************
   3 * SugarCRM is a customer relationship management program developed by
   4 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
   5 * 
   6 * This program is free software; you can redistribute it and/or modify it under
   7 * the terms of the GNU Affero General Public License version 3 as published by the
   8 * Free Software Foundation with the addition of the following permission added
   9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12 * 
  13 * This program is distributed in the hope that it will be useful, but WITHOUT
  14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
  16 * details.
  17 * 
  18 * You should have received a copy of the GNU Affero General Public License along with
  19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21 * 02110-1301 USA.
  22 * 
  23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  25 * 
  26 * The interactive user interfaces in modified source and object code versions
  27 * of this program must display Appropriate Legal Notices, as required under
  28 * Section 5 of the GNU Affero General Public License version 3.
  29 * 
  30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  31 * these Appropriate Legal Notices must retain the display of the "Powered by
  32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  33 * technical reasons, the Appropriate Legal Notices must display the words
  34 * "Powered by SugarCRM".
  35 ********************************************************************************/
  36
  37/*********************************************************************************
  38
  39 * Description:  Includes generic helper functions used throughout the application.
  40 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  41 * All Rights Reserved.
  42 * Contributor(s): ______________________________________..
  43 ********************************************************************************/
  44require_once('include/SugarObjects/SugarConfig.php');
  45require_once('include/utils/security_utils.php');
  46
  47
  48
  49function make_sugar_config(&$sugar_config)
  50{
  51	/* used to convert non-array config.php file to array format */
  52	global $admin_export_only;
  53	global $cache_dir;
  54	global $calculate_response_time;
  55	global $create_default_user;
  56	global $dateFormats;
  57	global $dbconfig;
  58	global $dbconfigoption;
  59	global $default_action;
  60	global $default_charset;
  61	global $default_currency_name;
  62	global $default_currency_symbol;
  63	global $default_currency_iso4217;
  64	global $defaultDateFormat;
  65	global $default_language;
  66	global $default_module;
  67	global $default_password;
  68	global $default_permission_mode;
  69	global $default_theme;
  70	global $defaultTimeFormat;
  71	global $default_user_is_admin;
  72	global $default_user_name;
  73	global $disable_export;
  74	global $disable_persistent_connections;
  75	global $display_email_template_variable_chooser;
  76	global $display_inbound_email_buttons;
  77	global $history_max_viewed;
  78	global $host_name;
  79	global $import_dir;
  80	global $languages;
  81	global $list_max_entries_per_page;
  82	global $lock_default_user_name;
  83	global $log_memory_usage;
  84	global $requireAccounts;
  85	global $RSS_CACHE_TIME;
  86	global $session_dir;
  87	global $site_URL;
  88	global $site_url;
  89	global $sugar_version;
  90	global $timeFormats;
  91	global $tmp_dir;
  92	global $translation_string_prefix;
  93	global $unique_key;
  94	global $upload_badext;
  95	global $upload_dir;
  96	global $upload_maxsize;
  97	global $import_max_execution_time;
  98	global $list_max_entries_per_subpanel;
  99	global $passwordsetting;
 100
 101	// assumes the following variables must be set:
 102	// $dbconfig, $dbconfigoption, $cache_dir, $import_dir, $session_dir, $site_URL, $tmp_dir, $upload_dir
 103
 104	$sugar_config = array (
 105	'admin_export_only' => empty($admin_export_only) ? false : $admin_export_only,
 106	'export_delimiter' => empty($export_delimiter) ? ',' : $export_delimiter,
 107	'cache_dir' => empty($cache_dir) ? 'cache/' : $cache_dir,
 108	'calculate_response_time' => empty($calculate_response_time) ? true : $calculate_response_time,
 109	'create_default_user' => empty($create_default_user) ? false : $create_default_user,
 110	'chartEngine' => 'Jit',
 111	'date_formats' => empty($dateFormats) ? array(
 112	'Y-m-d'=>'2010-12-23',
 113	'd-m-Y' => '23-12-2010',
 114	'm-d-Y'=>'12-23-2010',
 115	'Y/m/d'=>'2010/12/23',
 116	'd/m/Y' => '23/12/2010',
 117	'm/d/Y'=>'12/23/2010',
 118	'Y.m.d' => '2010.12.23',
 119	'd.m.Y' => '23.12.2010',
 120	'm.d.Y' => '12.23.2010'
 121		) : $dateFormats,
 122	'dbconfig' => $dbconfig,  // this must be set!!
 123	'dbconfigoption' => $dbconfigoption,  // this must be set!!
 124	'default_action' => empty($default_action) ? 'index' : $default_action,
 125	'default_charset' => empty($default_charset) ? 'UTF-8' : $default_charset,
 126	'default_currency_name' => empty($default_currency_name) ? 'US Dollar' : $default_currency_name,
 127	'default_currency_symbol' => empty($default_currency_symbol) ? '$' : $default_currency_symbol,
 128	'default_currency_iso4217' => empty($default_currency_iso4217) ? '$' : $default_currency_iso4217,
 129	'default_date_format' => empty($defaultDateFormat) ? 'm/d/Y' : $defaultDateFormat,
 130	'default_export_charset' => 'UTF-8',
 131	'default_language' => empty($default_language) ? 'en_us' : $default_language,
 132	'default_module' => empty($default_module) ? 'Home' : $default_module,
 133	'default_password' => empty($default_password) ? '' : $default_password,
 134	'default_permissions' => array (
 135		'dir_mode' => 02770,
 136		'file_mode' => 0660,
 137		'chown' => '',
 138		'chgrp' => '',
 139	),
 140    'default_theme' => empty($default_theme) ? 'Sugar5' : $default_theme,
 141    'default_time_format' => empty($defaultTimeFormat) ? 'h:ia' : $defaultTimeFormat,
 142	'default_user_is_admin' => empty($default_user_is_admin) ? false : $default_user_is_admin,
 143	'default_user_name' => empty($default_user_name) ? '' : $default_user_name,
 144	'disable_export' => empty($disable_export) ? false : $disable_export,
 145    'disable_persistent_connections' => empty($disable_persistent_connections) ? false : $disable_persistent_connections,
 146    'display_email_template_variable_chooser' => empty($display_email_template_variable_chooser) ? false : $display_email_template_variable_chooser,
 147	'display_inbound_email_buttons' => empty($display_inbound_email_buttons) ? false : $display_inbound_email_buttons,
 148	'history_max_viewed' => empty($history_max_viewed) ? 50 : $history_max_viewed,
 149	'host_name' => empty($host_name) ? 'localhost' : $host_name,
 150	'import_dir' => $import_dir,  // this must be set!!
 151	'import_max_records_per_file' => 100,
 152	'languages' => empty($languages) ? array('en_us' => 'English (US)') : $languages,
 153	'list_max_entries_per_page' => empty($list_max_entries_per_page) ? 20 : $list_max_entries_per_page,
 154	'list_max_entries_per_subpanel' => empty($list_max_entries_per_subpanel) ? 10 : $list_max_entries_per_subpanel,
 155	'lock_default_user_name' => empty($lock_default_user_name) ? false : $lock_default_user_name,
 156	'log_memory_usage' => empty($log_memory_usage) ? false : $log_memory_usage,
 157    'portal_view' => 'single_user',
 158	'resource_management' => array (
 159	    'special_query_limit' => 50000,
 160	    'special_query_modules' => array('Reports', 'Export', 'Import', 'Administration', 'Sync'),
 161	    'default_limit' => 1000,
 162    ),
 163	'require_accounts' => empty($requireAccounts) ? true : $requireAccounts,
 164	'rss_cache_time' => empty($RSS_CACHE_TIME) ? '10800' : $RSS_CACHE_TIME,
 165	'session_dir' => $session_dir,  // this must be set!!
 166	'site_url' => empty($site_URL) ? $site_url : $site_URL,  // this must be set!!
 167	'showDetailData' => true, // if true, read-only ACL fields will still appear on EditViews as non-editable
 168	'showThemePicker' => true,
 169	'sugar_version' => empty($sugar_version) ? 'unknown' : $sugar_version,
 170	'time_formats' => empty($timeFormats) ? array (
 171	'H:i'=>'23:00', 'h:ia'=>'11:00 pm', 'h:iA'=>'11:00PM',
 172	'H.i'=>'23.00', 'h.ia'=>'11.00 pm', 'h.iA'=>'11.00PM' ) : $timeFormats,
 173	'tmp_dir' => $tmp_dir,  // this must be set!!
 174	'translation_string_prefix' => empty($translation_string_prefix) ? false : $translation_string_prefix,
 175	'unique_key' => empty($unique_key) ? md5(create_guid()) : $unique_key,
 176	'upload_badext' => empty($upload_badext) ? array (
 177	'php', 'php3', 'php4', 'php5', 'pl', 'cgi', 'py',
 178	'asp', 'cfm', 'js', 'vbs', 'html', 'htm' ) : $upload_badext,
 179	'upload_dir' => $upload_dir,  // this must be set!!
 180	'upload_maxsize' => empty($upload_maxsize) ? 3000000 : $upload_maxsize,
 181	'import_max_execution_time' => empty($import_max_execution_time) ? 3600 : $import_max_execution_time,
 182	'lock_homepage' => false,
 183	'lock_subpanels' => false,
 184	'max_dashlets_homepage' => 15,
 185	'dashlet_display_row_options' => array('1','3','5','10'),
 186	'default_max_tabs' => empty($max_tabs) ? '7' : $max_tabs,
 187	'default_subpanel_tabs' =>  empty($subpanel_tabs) ? true : $subpanel_tabs,
 188	'default_subpanel_links' => empty($subpanel_links) ? false : $subpanel_links,
 189	'default_swap_last_viewed' => empty($swap_last_viewed) ? false : $swap_last_viewed,
 190	'default_swap_shortcuts' => empty($swap_shortcuts) ? false : $swap_shortcuts,
 191	'default_navigation_paradigm' => empty($navigation_paradigm) ? 'gm' : $navigation_paradigm,
 192    'default_call_status' => 'Planned',
 193	'js_lang_version' => 1,
 194	'passwordsetting' => empty($passwordsetting) ? array (
 195	    'SystemGeneratedPasswordON' => '',
 196	    'generatepasswordtmpl' => '',
 197	    'lostpasswordtmpl' => '',
 198	    'forgotpasswordON' => true,
 199    	'linkexpiration' => '1',
 200    	'linkexpirationtime' => '30',
 201    	'linkexpirationtype' => '1',
 202	    'systexpiration' => '0',
 203	    'systexpirationtime' => '',
 204	    'systexpirationtype' => '0',
 205	    'systexpirationlogin' => '',
 206		) : $passwordsetting,
 207	);
 208}
 209
 210function get_sugar_config_defaults() {
 211	global $locale;
 212	/**
 213	 * used for getting base values for array style config.php.  used by the
 214	 * installer and to fill in new entries on upgrades.  see also:
 215	 * sugar_config_union
 216	 */
 217
 218	$sugar_config_defaults = array (
 219	'admin_export_only' => false,
 220	'export_delimiter' => ',',
 221	'cache_dir' => 'cache/',
 222	'calculate_response_time' => true,
 223	'create_default_user' => false,
 224 	'chartEngine' => 'Jit',
 225	'date_formats' => array (
 226	'Y-m-d' => '2010-12-23', 'm-d-Y' => '12-23-2010', 'd-m-Y' => '23-12-2010',
 227	'Y/m/d' => '2010/12/23', 'm/d/Y' => '12/23/2010', 'd/m/Y' => '23/12/2010',
 228	'Y.m.d' => '2010.12.23', 'd.m.Y' => '23.12.2010', 'm.d.Y' => '12.23.2010',),
 229	'dbconfigoption' => array (
 230	'persistent' => true,
 231	'autofree' => false,
 232	'debug' => 0,
 233	'seqname_format' => '%s_seq',
 234	'portability' => 0,
 235	'ssl' => false ),
 236	'default_action' => 'index',
 237	'default_charset' => return_session_value_or_default('default_charset',
 238	'UTF-8'),
 239	'default_currency_name' => return_session_value_or_default('default_currency_name', 'US Dollar'),
 240	'default_currency_symbol' => return_session_value_or_default('default_currency_symbol', '$'),
 241	'default_currency_iso4217' => return_session_value_or_default('default_currency_iso4217', 'USD'),
 242	'default_currency_significant_digits' => return_session_value_or_default('default_currency_significant_digits', 2),
 243	'default_number_grouping_seperator' => return_session_value_or_default('default_number_grouping_seperator', ','),
 244	'default_decimal_seperator' => return_session_value_or_default('default_decimal_seperator', '.'),
 245	'default_date_format' => 'm/d/Y',
 246	'default_export_charset' => 'UTF-8',
 247	'default_language' => return_session_value_or_default('default_language',
 248	'en_us'),
 249	'default_module' => 'Home',
 250	'default_password' => '',
 251	'default_permissions' => array (
 252		'dir_mode' => 02770,
 253		'file_mode' => 0660,
 254		'user' => '',
 255		'group' => '',
 256	),
 257	'default_theme' => return_session_value_or_default('site_default_theme', 'Sugar5'),
 258	'default_time_format' => 'h:ia',
 259	'default_user_is_admin' => false,
 260	'default_user_name' => '',
 261	'disable_export' => false,
 262	'disable_persistent_connections' =>
 263	return_session_value_or_default('disable_persistent_connections',
 264	'false'),
 265    'display_email_template_variable_chooser' => false,
 266	'display_inbound_email_buttons' => false,
 267	'dump_slow_queries' => false,
 268	'email_default_editor' => 'html',
 269	'email_default_client' => 'sugar',
 270	'email_default_delete_attachments' => true,
 271	'history_max_viewed' => 50,
 272	'installer_locked' => true,
 273	'import_max_records_per_file' => 100,
 274	'languages' => array('en_us' => 'English (US)'),
 275	'large_scale_test' => false,
 276	'list_max_entries_per_page' => 20,
 277	'list_max_entries_per_subpanel' => 10,
 278	'lock_default_user_name' => false,
 279	'log_memory_usage' => false,
 280	'portal_view' => 'single_user',
 281    'resource_management' => array (
 282	    'special_query_limit' => 50000,
 283	    'special_query_modules' => array('Reports', 'Export', 'Import', 'Administration', 'Sync'),
 284	    'default_limit' => 1000,
 285    ),
 286	'require_accounts' => true,
 287	'rss_cache_time' => return_session_value_or_default('rss_cache_time',
 288	'10800'),
 289	'save_query' => 'all',
 290	'showDetailData' => true, // if true, read-only ACL fields will still appear on EditViews as non-editable
 291	'showThemePicker' => true,
 292	'slow_query_time_msec' => '100',
 293    'sugarbeet' => true,
 294    'time_formats' => array (
 295	'H:i'=>'23:00', 'h:ia'=>'11:00pm', 'h:iA'=>'11:00PM',
 296	'H.i'=>'23.00', 'h.ia'=>'11.00pm', 'h.iA'=>'11.00PM' ),
 297    'tracker_max_display_length' => 15,
 298	'translation_string_prefix' =>
 299	return_session_value_or_default('translation_string_prefix', false),
 300	'upload_badext' => array (
 301	'php', 'php3', 'php4', 'php5', 'pl', 'cgi', 'py',
 302	'asp', 'cfm', 'js', 'vbs', 'html', 'htm' ),
 303	'upload_maxsize' => 3000000,
 304	'import_max_execution_time' => 3600,
 305//	'use_php_code_json' => returnPhpJsonStatus(),
 306	'verify_client_ip' => true,
 307	'js_custom_version' => '',
 308	'js_lang_version' => 1,
 309	'default_number_grouping_seperator' => ',',
 310	'default_decimal_seperator' => '.',
 311	'lock_homepage' => false,
 312	'lock_subpanels' => false,
 313	'max_dashlets_homepage' => '15',
 314	'default_max_tabs' => '7',
 315	'dashlet_display_row_options' => array('1','3','5','10'),
 316	'default_subpanel_tabs' =>  true,
 317	'default_subpanel_links' => false,
 318	'default_swap_last_viewed' => false,
 319	'default_swap_shortcuts' => false,
 320	'default_navigation_paradigm' => 'gm',
 321	'admin_access_control' => false,
 322  	'use_common_ml_dir'	=> false,
 323  	'common_ml_dir' => '',
 324	'vcal_time' => '2',
 325	'passwordsetting' => empty($passwordsetting) ? array (
 326	    'SystemGeneratedPasswordON' => '',
 327	    'generatepasswordtmpl' => '',
 328	    'lostpasswordtmpl' => '',
 329	    'forgotpasswordON' => false,
 330    	'linkexpiration' => '1',
 331    	'linkexpirationtime' => '30',
 332    	'linkexpirationtype' => '1',
 333	    'systexpiration' => '0',
 334	    'systexpirationtime' => '',
 335	    'systexpirationtype' => '0',
 336	    'systexpirationlogin' => '',
 337		) : $passwordsetting,
 338	'use_real_names' => true,
 339	);
 340
 341	if(!is_object($locale)) {
 342		$locale = new Localization();
 343	}
 344
 345	$sugar_config_defaults['default_currencies'] = $locale->getDefaultCurrencies();
 346
 347	$sugar_config_defaults = sugarArrayMerge($locale->getLocaleConfigDefaults(), $sugar_config_defaults);
 348	return( $sugar_config_defaults );
 349}
 350
 351/**
 352 * @deprecated use SugarView::getMenu() instead
 353 */
 354function load_menu($path){
 355	global $module_menu;
 356
 357	if(file_exists($path . 'Menu.php'))
 358	{
 359		require($path . 'Menu.php');
 360	}
 361	if(file_exists('custom/' . $path . 'Ext/Menus/menu.ext.php'))
 362	{
 363		require('custom/' . $path . 'Ext/Menus/menu.ext.php');
 364	}
 365	if(file_exists('custom/application/Ext/Menus/menu.ext.php'))
 366	{
 367		require('custom/application/Ext/Menus/menu.ext.php');
 368	}
 369	return $module_menu;
 370}
 371
 372/**
 373 * get_notify_template_file
 374 * This function will return the location of the email notifications template to use
 375 *
 376 * @return string relative file path to email notifications template file
 377 */
 378function get_notify_template_file($language){
 379	/*
 380	 * Order of operation:
 381	 * 1) custom version of specified language
 382	 * 2) stock version of specified language
 383	 * 3) custom version of en_us template
 384	 * 4) stock en_us template
 385	 */
 386
 387	// set $file to the base code template so it's set if none of the conditions pass
 388	$file = "include/language/en_us.notify_template.html";
 389
 390	if(file_exists("custom/include/language/{$language}.notify_template.html")){
 391		$file = "custom/include/language/{$language}.notify_template.html";
 392	}
 393	else if(file_exists("include/language/{$language}.notify_template.html")){
 394		$file = "include/language/{$language}.notify_template.html";
 395	}
 396	else if(file_exists("custom/include/language/en_us.notify_template.html")){
 397		$file = "custom/include/language/en_us.notify_template.html";
 398	}
 399
 400	return $file;
 401}
 402
 403function sugar_config_union( $default, $override ){
 404	// a little different then array_merge and array_merge_recursive.  we want
 405	// the second array to override the first array if the same value exists,
 406	// otherwise merge the unique keys.  it handles arrays of arrays recursively
 407	// might be suitable for a generic array_union
 408	if( !is_array( $override ) ){
 409		$override = array();
 410	}
 411	foreach( $default as $key => $value ){
 412		if( !array_key_exists($key, $override) ){
 413			$override[$key] = $value;
 414		}
 415		else if( is_array( $key ) ){
 416			$override[$key] = sugar_config_union( $value, $override[$key] );
 417		}
 418	}
 419	return( $override );
 420}
 421
 422function make_not_writable( $file ){
 423	// Returns true if the given file/dir has been made not writable
 424	$ret_val = false;
 425	if( is_file($file) || is_dir($file) ){
 426		if( !is_writable($file) ){
 427			$ret_val = true;
 428		}
 429		else {
 430			$original_fileperms = fileperms($file);
 431
 432			// take away writable permissions
 433			$new_fileperms = $original_fileperms & ~0x0092;
 434			@sugar_chmod($file, $new_fileperms);
 435
 436			if( !is_writable($file) ){
 437				$ret_val = true;
 438			}
 439		}
 440	}
 441	return $ret_val;
 442}
 443
 444
 445/** This function returns the name of the person.
 446 * It currently returns "first last".  It should not put the space if either name is not available.
 447 * It should not return errors if either name is not available.
 448 * If no names are present, it will return ""
 449 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 450 * All Rights Reserved.
 451 * Contributor(s): ______________________________________..
 452 */
 453function return_name($row, $first_column, $last_column)
 454{
 455	$first_name = "";
 456	$last_name = "";
 457	$full_name = "";
 458
 459	if(isset($row[$first_column]))
 460	{
 461		$first_name = stripslashes($row[$first_column]);
 462	}
 463
 464	if(isset($row[$last_column]))
 465	{
 466		$last_name = stripslashes($row[$last_column]);
 467	}
 468
 469	$full_name = $first_name;
 470
 471	// If we have a first name and we have a last name
 472	if($full_name != "" && $last_name != "")
 473	{
 474		// append a space, then the last name
 475		$full_name .= " ".$last_name;
 476	}
 477	// If we have no first name, but we have a last name
 478	else if($last_name != "")
 479	{
 480		// append the last name without the space.
 481		$full_name .= $last_name;
 482	}
 483
 484	return $full_name;
 485}
 486
 487
 488function get_languages()
 489{
 490	global $sugar_config;
 491	$lang = $sugar_config['languages'];
 492    if(!empty($sugar_config['disabled_languages'])){
 493        foreach(explode(',', $sugar_config['disabled_languages']) as $disable) {
 494            unset($lang[$disable]);
 495        }
 496    }
 497	return $lang;
 498}
 499
 500function get_all_languages()
 501{
 502	global $sugar_config;
 503	return $sugar_config['languages'];
 504}
 505
 506
 507function get_language_display($key)
 508{
 509	global $sugar_config;
 510	return $sugar_config['languages'][$key];
 511}
 512
 513function get_assigned_user_name($assigned_user_id, $is_group = '') {
 514	static $saved_user_list = null;
 515
 516	if(empty($saved_user_list)) {
 517		$saved_user_list = get_user_array(false, '', '', false, null, $is_group);
 518	}
 519
 520	if(isset($saved_user_list[$assigned_user_id])) {
 521		return $saved_user_list[$assigned_user_id];
 522	}
 523
 524	return '';
 525}
 526
 527/**
 528 * retrieves the user_name column value (login)
 529 * @param string id GUID of user
 530 * @return string
 531 */
 532function get_user_name($id) {
 533	global $db;
 534
 535	if(empty($db))
 536	$db = DBManagerFactory::getInstance();
 537
 538	$q = "SELECT user_name FROM users WHERE id='{$id}'";
 539	$r = $db->query($q);
 540	$a = $db->fetchByAssoc($r);
 541
 542	return (empty($a)) ? '' : $a['user_name'];
 543}
 544
 545
 546//TODO Update to use global cache
 547function get_user_array($add_blank=true, $status="Active", $assigned_user="", $use_real_name=false, $user_name_begins = null, $is_group=' AND portal_only=0 ', $from_cache = true) {
 548	global $locale;
 549	global $sugar_config;
 550
 551	if(empty($locale)) {
 552
 553		$locale = new Localization();
 554	}
 555	if($from_cache)
 556		$user_array = get_register_value('user_array', $add_blank. $status . $assigned_user);
 557
 558	if(!isset($user_array)) {
 559		$db = DBManagerFactory::getInstance();
 560		$temp_result = Array();
 561		// Including deleted users for now.
 562		if (empty($status)) {
 563			$query = "SELECT id, first_name, last_name, user_name from users WHERE 1=1".$is_group;
 564		}
 565		else {
 566			$query = "SELECT id, first_name, last_name, user_name from users WHERE status='$status'".$is_group;
 567		}
 568
 569		if (!empty($user_name_begins)) {
 570			$query .= " AND user_name LIKE '$user_name_begins%' ";
 571		}
 572		if (!empty($assigned_user)) {
 573			$query .= " OR id='$assigned_user'";
 574		}
 575		$query = $query.' ORDER BY user_name ASC';
 576		$GLOBALS['log']->debug("get_user_array query: $query");
 577		$result = $db->query($query, true, "Error filling in user array: ");
 578
 579		if ($add_blank==true) {
 580			// Add in a blank row
 581			$temp_result[''] = '';
 582		}
 583
 584		// Get the id and the name.
 585		while($row = $db->fetchByAssoc($result)) {
 586			if($use_real_name == true || showFullName()) {
 587				if(isset($row['last_name'])) { // cn: we will ALWAYS have both first_name and last_name (empty value if blank in db)
 588					$temp_result[$row['id']] = $locale->getLocaleFormattedName($row['first_name'],$row['last_name']);
 589				} else {
 590					$temp_result[$row['id']] = $row['user_name'];
 591				}
 592			} else {
 593				$temp_result[$row['id']] = $row['user_name'];
 594			}
 595		}
 596
 597		$user_array = $temp_result;
 598		if($from_cache)
 599			set_register_value('user_array', $add_blank. $status . $assigned_user, $temp_result);
 600	}
 601
 602
 603	return $user_array;
 604}
 605
 606
 607/**
 608 * uses a different query to return a list of users than get_user_array()
 609 * @param args string where clause entry
 610 * @return array Array of Users' details that match passed criteria
 611 */
 612function getUserArrayFromFullName($args, $hide_portal_users = false) {
 613	global $locale;
 614	$db = DBManagerFactory::getInstance();
 615
 616	$argArray = array();
 617	if(strpos($args, " ")) {
 618		$argArray = explode(" ", $args);
 619	} else {
 620		$argArray[] = $args;
 621	}
 622
 623	$inClause = '';
 624	foreach($argArray as $arg) {
 625		if(!empty($inClause)) {
 626			$inClause .= ' OR ';
 627		}
 628		if(empty($arg))
 629		continue;
 630
 631		$inClause .= "(first_name LIKE '{$arg}%' OR last_name LIKE '{$arg}%')";
 632	}
 633
 634	$query  = "SELECT id, first_name, last_name, user_name FROM users WHERE status='Active' AND deleted=0 AND ";
 635	if ( $hide_portal_users ) {
 636	    $query .= " portal_only=0 AND ";
 637	}
 638	$query .= $inClause;
 639	$query .= " ORDER BY last_name ASC";
 640
 641	$r = $db->query($query);
 642	$ret = array();
 643	while($a = $db->fetchByAssoc($r)) {
 644		$ret[$a['id']] = $locale->getLocaleFormattedName($a['first_name'], $a['last_name']);
 645	}
 646
 647	return $ret;
 648}
 649
 650/**
 651 *
 652 * based on user pref then system pref
 653 */
 654function showFullName() {
 655	global $sugar_config;
 656	global $current_user;
 657	static $showFullName = null;
 658
 659	if (is_null($showFullName)) {
 660		$sysPref = (isset($sugar_config['use_real_names']) && $sugar_config['use_real_names'] == true) ? true : false;
 661		$userPref = (is_object($current_user)) ? $current_user->getPreference('use_real_names') : null;
 662
 663		if($userPref != null) {
 664			$bool = ($userPref == 'on') ? true : false;
 665			$showFullName = $bool;
 666		} else {
 667			$showFullName = $sysPref;
 668		}
 669	}
 670
 671	return $showFullName;
 672}
 673
 674function clean($string, $maxLength)
 675{
 676	$string = substr($string, 0, $maxLength);
 677	return escapeshellcmd($string);
 678}
 679
 680/**
 681 * Copy the specified request variable to the member variable of the specified object.
 682 * Do no copy if the member variable is already set.
 683 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 684 * All Rights Reserved.
 685 * Contributor(s): ______________________________________..
 686 */
 687function safe_map($request_var, & $focus, $always_copy = false)
 688{
 689	safe_map_named($request_var, $focus, $request_var, $always_copy);
 690}
 691
 692/**
 693 * Copy the specified request variable to the member variable of the specified object.
 694 * Do no copy if the member variable is already set.
 695 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 696 * All Rights Reserved.
 697 * Contributor(s): ______________________________________..
 698 */
 699function safe_map_named($request_var, & $focus, $member_var, $always_copy)
 700{
 701	if (isset($_REQUEST[$request_var]) && ($always_copy || is_null($focus->$member_var))) {
 702		$GLOBALS['log']->debug("safe map named called assigning '{$_REQUEST[$request_var]}' to $member_var");
 703		$focus->$member_var = $_REQUEST[$request_var];
 704	}
 705}
 706
 707/**
 708 * This function retrieves an application language file and returns the array of strings included in the $app_list_strings var.
 709 *
 710 * @param string $language specific language to load
 711 * @return array lang strings
 712 */
 713function return_app_list_strings_language($language)
 714{
 715	global $app_list_strings;
 716	global $sugar_config;
 717
 718	$cache_key = 'app_list_strings.'.$language;
 719
 720	// Check for cached value
 721	$cache_entry = sugar_cache_retrieve($cache_key);
 722	if(!empty($cache_entry))
 723	{
 724		return $cache_entry;
 725	}
 726
 727	$default_language = $sugar_config['default_language'];
 728	$temp_app_list_strings = $app_list_strings;
 729
 730	$langs = array();
 731	if ($language != 'en_us') {
 732	    $langs[] = 'en_us';
 733	}
 734	if ($default_language != 'en_us' && $language != $default_language) {
 735	    $langs[] = $default_language;
 736	}
 737	$langs[] = $language;
 738
 739	$app_list_strings_array = array();
 740
 741	foreach ( $langs as $lang ) {
 742	    $app_list_strings = array();
 743	    if(file_exists("include/language/$lang.lang.php")) {
 744            include("include/language/$lang.lang.php");
 745            $GLOBALS['log']->info("Found language file: $lang.lang.php");
 746        }
 747        if(file_exists("include/language/$lang.lang.override.php")) {
 748            include("include/language/$lang.lang.override.php");
 749            $GLOBALS['log']->info("Found override language file: $lang.lang.override.php");
 750        }
 751        if(file_exists("include/language/$lang.lang.php.override")) {
 752            include("include/language/$lang.lang.php.override");
 753            $GLOBALS['log']->info("Found override language file: $lang.lang.php.override");
 754        }
 755        
 756        $app_list_strings_array[] = $app_list_strings;
 757    }
 758    
 759    $app_list_strings = array();
 760    foreach ( $app_list_strings_array as $app_list_strings_item ) {
 761        $app_list_strings = sugarArrayMerge($app_list_strings, $app_list_strings_item);
 762    }
 763
 764    foreach ( $langs as $lang ) {
 765        if(file_exists("custom/application/Ext/Language/$lang.lang.ext.php")) {
 766            $app_list_strings = _mergeCustomAppListStrings("custom/application/Ext/Language/$lang.lang.ext.php" , $app_list_strings);
 767            $GLOBALS['log']->info("Found extended language file: $lang.lang.ext.php");
 768        }
 769        if(file_exists("custom/include/language/$lang.lang.php")) {
 770            include("custom/include/language/$lang.lang.php");
 771            $GLOBALS['log']->info("Found custom language file: $lang.lang.php");
 772        }
 773    }
 774
 775	if(!isset($app_list_strings)) {
 776		$GLOBALS['log']->fatal("Unable to load the application language file for the selected language ($language) or the default language ($default_language) or the en_us language");
 777		return null;
 778	}
 779
 780	$return_value = $app_list_strings;
 781	$app_list_strings = $temp_app_list_strings;
 782
 783	sugar_cache_put($cache_key, $return_value);
 784
 785	return $return_value;
 786}
 787
 788/**
 789* The dropdown items in custom language files is $app_list_strings['$key']['$second_key'] = $value not
 790* $GLOBALS['app_list_strings']['$key'] = $value, so we have to delete the original ones in app_list_strings and relace it with the custom ones.
 791 * @param file string the language that you want include,
 792 * @param app_list_strings array the golbal strings
 793 * @return array
 794 */
 795 //jchi 25347
 796function _mergeCustomAppListStrings($file , $app_list_strings){
 797	$app_list_strings_original = $app_list_strings;
 798	unset($app_list_strings);
 799	include($file);
 800	if(!isset($app_list_strings) || !is_array($app_list_strings)){
 801		return $app_list_strings_original;
 802	}
 803	//Bug 25347: We should not merge custom dropdown fields unless they relate to parent fields or the module list.
 804	foreach($app_list_strings as $key=>$value)
 805	{
 806		$exemptDropdowns = array("moduleList", "parent_type_display", "record_type_display", "record_type_display_notes");
 807		if (!in_array($key, $exemptDropdowns) && array_key_exists($key, $app_list_strings_original))
 808		{
 809	   		unset($app_list_strings_original["$key"]);
 810		}
 811   }
 812   $app_list_strings = sugarArrayMergeRecursive($app_list_strings_original , $app_list_strings);
 813   return $app_list_strings;
 814}
 815
 816/**
 817 * This function retrieves an application language file and returns the array of strings included.
 818 *
 819 * @param string $language specific language to load
 820 * @return array lang strings
 821 */
 822function return_application_language($language)
 823{
 824	global $app_strings, $sugar_config;
 825
 826	$cache_key = 'app_strings.'.$language;
 827
 828	// Check for cached value
 829	$cache_entry = sugar_cache_retrieve($cache_key);
 830	if(!empty($cache_entry))
 831	{
 832		return $cache_entry;
 833	}
 834
 835	$temp_app_strings = $app_strings;
 836	$default_language = $sugar_config['default_language'];
 837
 838	$langs = array();
 839	if ($language != 'en_us') {
 840	    $langs[] = 'en_us';
 841	}
 842	if ($default_language != 'en_us' && $language != $default_language) {
 843	    $langs[] = $default_language;
 844	}
 845
 846	$langs[] = $language;
 847
 848	$app_strings_array = array();
 849
 850	foreach ( $langs as $lang ) {
 851	    $app_strings = array();
 852	    if(file_exists("include/language/$lang.lang.php")) {
 853            include("include/language/$lang.lang.php");
 854            $GLOBALS['log']->info("Found language file: $lang.lang.php");
 855        }
 856        if(file_exists("include/language/$lang.lang.override.php")) {
 857            include("include/language/$lang.lang.override.php");
 858            $GLOBALS['log']->info("Found override language file: $lang.lang.override.php");
 859        }
 860        if(file_exists("include/language/$lang.lang.php.override")) {
 861            include("include/language/$lang.lang.php.override");
 862            $GLOBALS['log']->info("Found override language file: $lang.lang.php.override");
 863        }
 864        if(file_exists("custom/application/Ext/Language/$lang.lang.ext.php")) {
 865            include("custom/application/Ext/Language/$lang.lang.ext.php");
 866            $GLOBALS['log']->info("Found extended language file: $lang.lang.ext.php");
 867        }
 868        if(file_exists("custom/include/language/$lang.lang.php")) {
 869            include("custom/include/language/$lang.lang.php");
 870            $GLOBALS['log']->info("Found custom language file: $lang.lang.php");
 871        }
 872        $app_strings_array[] = $app_strings;
 873	}
 874
 875	$app_strings = array();
 876    foreach ( $app_strings_array as $app_strings_item ) {
 877        $app_strings = sugarArrayMerge($app_strings, $app_strings_item);
 878    }
 879
 880	if(!isset($app_strings)) {
 881		$GLOBALS['log']->fatal("Unable to load the application language strings");
 882		return null;
 883	}
 884
 885	// If we are in debug mode for translating, turn on the prefix now!
 886	if($sugar_config['translation_string_prefix']) {
 887		foreach($app_strings as $entry_key=>$entry_value) {
 888			$app_strings[$entry_key] = $language.' '.$entry_value;
 889		}
 890	}
 891	if(isset($_SESSION['show_deleted'])) {
 892		$app_strings['LBL_DELETE_BUTTON'] = $app_strings['LBL_UNDELETE_BUTTON'];
 893		$app_strings['LBL_DELETE_BUTTON_LABEL'] = $app_strings['LBL_UNDELETE_BUTTON_LABEL'];
 894		$app_strings['LBL_DELETE_BUTTON_TITLE'] = $app_strings['LBL_UNDELETE_BUTTON_TITLE'];
 895		$app_strings['LBL_DELETE'] = $app_strings['LBL_UNDELETE'];
 896	}
 897
 898	$app_strings['LBL_ALT_HOT_KEY'] = get_alt_hot_key();
 899
 900	$return_value = $app_strings;
 901	$app_strings = $temp_app_strings;
 902
 903	sugar_cache_put($cache_key, $return_value);
 904
 905	return $return_value;
 906}
 907
 908/**
 909 * This function retrieves a module's language file and returns the array of strings included.
 910 *
 911 * @param string $language specific language to load
 912 * @param string $module module name to load strings for
 913 * @param bool $refresh optional, true if you want to rebuild the language strings
 914 * @return array lang strings
 915 */
 916function return_module_language($language, $module, $refresh=false)
 917{
 918	global $mod_strings;
 919	global $sugar_config;
 920	global $currentModule;
 921
 922	// Jenny - Bug 8119: Need to check if $module is not empty
 923	if (empty($module)) {
 924		$stack  = debug_backtrace();
 925		$GLOBALS['log']->warn("Variable module is not in return_module_language ". var_export($stack, true));
 926		return array();
 927	}
 928
 929	$cache_key = "mod_strings_lang.".$language.$module;
 930	// Check for cached value
 931	$cache_entry = sugar_cache_retrieve($cache_key);
 932	if(!empty($cache_entry))
 933	{
 934		return $cache_entry;
 935	}
 936
 937	// Store the current mod strings for later
 938	$temp_mod_strings = $mod_strings;
 939	$loaded_mod_strings = array();
 940	$language_used = $language;
 941	$default_language = $sugar_config['default_language'];
 942
 943	if(empty($language)) {
 944		$language = $default_language;
 945	}
 946
 947	// Bug 21559 - So we can get all the strings defined in the template, refresh
 948	// the vardefs file if the cached language file doesn't exist.
 949    if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$language.'.lang.php')
 950			&& !empty($GLOBALS['beanList'][$module])){
 951		$object = $GLOBALS['beanList'][$module];
 952		if ($object == 'aCase') {
 953            $object = 'Case';
 954		}
 955		VardefManager::refreshVardefs($module,$object);
 956	}
 957
 958	$loaded_mod_strings = LanguageManager::loadModuleLanguage($module, $language,$refresh);
 959
 960	// cn: bug 6048 - merge en_us with requested language
 961	if($language != $sugar_config['default_language'])
 962        $loaded_mod_strings = sugarArrayMerge(
 963            LanguageManager::loadModuleLanguage($module, $sugar_config['default_language'],$refresh),
 964                $loaded_mod_strings
 965            );
 966
 967    // Load in en_us strings by default
 968    if($language != 'en_us' && $sugar_config['default_language'] != 'en_us')
 969        $loaded_mod_strings = sugarArrayMerge(
 970            LanguageManager::loadModuleLanguage($module, 'en_us', $refresh),
 971                $loaded_mod_strings
 972            );
 973
 974	// If we are in debug mode for translating, turn on the prefix now!
 975	if($sugar_config['translation_string_prefix']) {
 976		foreach($loaded_mod_strings as $entry_key=>$entry_value) {
 977			$loaded_mod_strings[$entry_key] = $language_used.' '.$entry_value;
 978		}
 979	}
 980
 981	$return_value = $loaded_mod_strings;
 982	if(!isset($mod_strings)){
 983		$mod_strings = $return_value;
 984	}
 985	else
 986		$mod_strings = $temp_mod_strings;
 987
 988    sugar_cache_put($cache_key, $return_value);
 989	return $return_value;
 990}
 991
 992
 993/** This function retrieves an application language file and returns the array of strings included in the $mod_list_strings var.
 994 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 995 * All Rights Reserved.
 996 * Contributor(s): ______________________________________..
 997 * If you are using the current language, do not call this function unless you are loading it for the first time */
 998function return_mod_list_strings_language($language,$module) {
 999	global $mod_list_strings;
1000	global $sugar_config;
1001	global $currentModule;
1002
1003	$cache_key = "mod_list_str_lang.".$language.$module;
1004
1005	// Check for cached value
1006	$cache_entry = sugar_cache_retrieve($cache_key);
1007	if(!empty($cache_entry))
1008	{
1009		return $cache_entry;
1010	}
1011
1012	$language_used = $language;
1013	$temp_mod_list_strings = $mod_list_strings;
1014	$default_language = $sugar_config['default_language'];
1015
1016	if($currentModule == $module && isset($mod_list_strings) && $mod_list_strings != null) {
1017		return $mod_list_strings;
1018	}
1019
1020	// cn: bug 6351 - include en_us if file langpack not available
1021	// cn: bug 6048 - merge en_us with requested language
1022	include("modules/$module/language/en_us.lang.php");
1023	$en_mod_list_strings = array();
1024	if($language_used != $default_language)
1025	$en_mod_list_strings = $mod_list_strings;
1026
1027	if(file_exists("modules/$module/language/$language.lang.php")) {
1028		include("modules/$module/language/$language.lang.php");
1029	}
1030
1031	if(file_exists("modules/$module/language/$language.lang.override.php")){
1032		include("modules/$module/language/$language.lang.override.php");
1033	}
1034
1035	if(file_exists("modules/$module/language/$language.lang.php.override")){
1036		echo 'Please Change:<br>' . "modules/$module/language/$language.lang.php.override" . '<br>to<br>' . 'Please Change:<br>' . "modules/$module/language/$language.lang.override.php";
1037		include("modules/$module/language/$language.lang.php.override");
1038	}
1039
1040	// cn: bug 6048 - merge en_us with requested language
1041	$mod_list_strings = sugarArrayMerge($en_mod_list_strings, $mod_list_strings);
1042
1043	// if we still don't have a language pack, then log an error
1044	if(!isset($mod_list_strings)) {
1045		$GLOBALS['log']->fatal("Unable to load the application list language file for the selected language($language) or the default language($default_language) for module({$module})");
1046		return null;
1047	}
1048
1049	$return_value = $mod_list_strings;
1050	$mod_list_strings = $temp_mod_list_strings;
1051
1052	sugar_cache_put($cache_key, $return_value);
1053	return $return_value;
1054}
1055
1056
1057/** This function retrieves a theme's language file and returns the array of strings included.
1058 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1059 * All Rights Reserved.
1060 * Contributor(s): ______________________________________..
1061 */
1062function return_theme_language($language, $theme)
1063{
1064	global $mod_strings, $sugar_config, $currentModule;
1065
1066	$language_used = $language;
1067	$default_language = $sugar_config['default_language'];
1068
1069	include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php");
1070	if(file_exists(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php")){
1071		include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php");
1072	}
1073	if(file_exists(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override")){
1074		echo 'Please Change:<br>' . SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override" . '<br>to<br>' . 'Please Change:<br>' . SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php";
1075		include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override");
1076	}
1077	if(!isset($theme_strings))
1078	{
1079		$GLOBALS['log']->warn("Unable to find the theme file for language: ".$language." and theme: ".$theme);
1080		require(SugarThemeRegistry::get($theme)->getFilePath()."/language/$default_language.lang.php");
1081		$language_used = $default_language;
1082	}
1083
1084	if(!isset($theme_strings))
1085	{
1086		$GLOBALS['log']->fatal("Unable to load the theme($theme) language file for the selected language($language) or the default language($default_language)");
1087		return null;
1088	}
1089
1090	// If we are in debug mode for translating, turn on the prefix now!
1091	if($sugar_config['translation_string_prefix'])
1092	{
1093		foreach($theme_strings as $entry_key=>$entry_value)
1094		{
1095			$theme_strings[$entry_key] = $language_used.' '.$entry_value;
1096		}
1097	}
1098
1099	return $theme_strings;
1100}
1101
1102
1103
1104/** If the session variable is defined and is not equal to "" then return it.  Otherwise, return the default value.
1105 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1106 * All Rights Reserved.
1107 * Contributor(s): ______________________________________..
1108 */
1109function return_session_value_or_default($varname, $default)
1110{
1111	if(isset($_SESSION[$varname]) && $_SESSION[$varname] != "")
1112	{
1113		return $_SESSION[$varname];
1114	}
1115
1116	return $default;
1117}
1118
1119/**
1120 * Creates an array of where restrictions.  These are used to construct a where SQL statement on the query
1121 * It looks for the variable in the $_REQUEST array.  If it is set and is not "" it will create a where clause out of it.
1122 * @param &$where_clauses - The array to append the clause to
1123 * @param $variable_name - The name of the variable to look for an add to the where clause if found
1124 * @param $SQL_name - [Optional] If specified, this is the SQL column name that is used.  If not specified, the $variable_name is used as the SQL_name.
1125 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1126 * All Rights Reserved.
1127 * Contributor(s): ______________________________________..
1128 */
1129function append_where_clause(&$where_clauses, $variable_name, $SQL_name = null)
1130{
1131	if($SQL_name == null)
1132	{
1133		$SQL_name = $variable_name;
1134	}
1135
1136	if(isset($_REQUEST[$variable_name]) && $_REQUEST[$variable_name] != "")
1137	{
1138		array_push($where_clauses, "$SQL_name like '".$GLOBALS['db']->quote($_REQUEST[$variable_name])."%'");
1139	}
1140}
1141
1142/**
1143 * Generate the appropriate SQL based on the where clauses.
1144 * @param $where_clauses - An Array of individual where clauses stored as strings
1145 * @returns string where_clause - The final SQL where clause to be executed.
1146 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1147 * All Rights Reserved.
1148 * Contributor(s): ______________________________________..
1149 */
1150function generate_where_statement($where_clauses)
1151{
1152	$where = "";
1153	foreach($where_clauses as $clause)
1154	{
1155		if($where != "")
1156		$where .= " and ";
1157		$where .= $clause;
1158	}
1159
1160	$GLOBALS['log']->info("Here is the where clause for the list view: $where");
1161	return $where;
1162}
1163
1164/**
1165 * determines if a passed string matches the criteria for a Sugar GUID
1166 * @param string $guid
1167 * @return bool False on failure
1168 */
1169function is_guid($guid) {
1170	if(strlen($guid) != 36) {
1171		return false;
1172	}
1173
1174	if(preg_match("/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/i", $guid)) {
1175		return true;
1176	}
1177
1178	return true;;
1179}
1180
1181
1182/**
1183 * A temporary method of generating GUIDs of the correct format for our DB.
1184 * @return String contianing a GUID in the format: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
1185 *
1186 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1187 * All Rights Reserved.
1188 * Contributor(s): ______________________________________..
1189 */
1190function create_guid()
1191{
1192	$microTime = microtime();
1193	list($a_dec, $a_sec) = explode(" ", $microTime);
1194
1195	$dec_hex = dechex($a_dec* 1000000);
1196	$sec_hex = dechex($a_sec);
1197
1198	ensure_length($dec_hex, 5);
1199	ensure_length($sec_hex, 6);
1200
1201	$guid = "";
1202	$guid .= $dec_hex;
1203	$guid .= create_guid_section(3);
1204	$guid .= '-';
1205	$guid .= create_guid_section(4);
1206	$guid .= '-';
1207	$guid .= create_guid_section(4);
1208	$guid .= '-';
1209	$guid .= create_guid_section(4);
1210	$guid .= '-';
1211	$guid .= $sec_hex;
1212	$guid .= create_guid_section(6);
1213
1214	return $guid;
1215
1216}
1217
1218function create_guid_section($characters)
1219{
1220	$return = "";
1221	for($i=0; $i<$characters; $i++)
1222	{
1223		$return .= dechex(mt_rand(0,15));
1224	}
1225	return $return;
1226}
1227
1228function ensure_length(&$string, $length)
1229{
1230	$strlen = strlen($string);
1231	if($strlen < $length)
1232	{
1233		$string = str_pad($string,$length,"0");
1234	}
1235	else if($strlen > $length)
1236	{
1237		$string = substr($string, 0, $length);
1238	}
1239}
1240
1241function microtime_diff($a, $b) {
1242	list($a_dec, $a_sec) = explode(" ", $a);
1243	list($b_dec, $b_sec) = explode(" ", $b);
1244	return $b_sec - $a_sec + $b_dec - $a_dec;
1245}
1246
1247// check if Studio is displayed.
1248function displayStudioForCurrentUser()
1249{
1250    if ( is_admin($GLOBALS['current_user']) ) {
1251        return true;
1252    }
1253
1254
1255
1256	return true;
1257
1258}
1259
1260function displayWorkflowForCurrentUser()
1261{
1262    $_SESSION['display_workflow_for_user'] = false;
1263    return false;
1264}
1265
1266// return an array with all modules where the user is an admin.
1267function get_admin_modules_for_user($user) {
1268    global $beanList;
1269    $admin_modules = array();
1270
1271    return ($admin_modules);
1272}
1273
1274 function get_workflow_admin_modules_for_user($user){
1275    if (isset($_SESSION['get_workflow_admin_modules_for_user'])) {
1276        return $_SESSION['get_workflow_admin_modules_for_user'];
1277    }
1278
1279    global $moduleList;
1280    $workflow_mod_list = array();
1281    foreach($moduleList as $module){
1282		$workflow_mod_list[$module] = $module;
1283	}
1284
1285	// This list is taken from teh previous version of workflow_utils.php
1286    $workflow_mod_list['Tasks'] = "Tasks";
1287    $workflow_mod_list['Calls'] = "Calls";
1288    $workflow_mod_list['Meetings'] = "Meetings";
1289    $workflow_mod_list['Notes'] = "Notes";
1290    $workflow_mod_list['ProjectTask'] = "Project Tasks";
1291	$workflow_mod_list['Leads'] = "Leads";
1292	$workflow_mod_list['Opportunities'] = "Opportunities";
1293	// End of list
1294
1295    $workflow_admin_modules = array();
1296    if(empty($user)) {
1297    	return $workflow_admin_modules;
1298    }
1299    $actions = ACLAction::getUserActions($user->id);
1300    //check for ForecastSchedule because it doesn't exist in $workflow_mod_list
1301    if (isset($actions['ForecastSchedule']['module']['admin']['aclaccess']) && ($actions['ForecastSchedule']['module']['admin']['aclaccess']==ACL_ALLOW_DEV ||
1302        $actions['ForecastSchedule']['module']['admin']['aclaccess']==ACL_ALLOW_ADMIN_DEV)) {
1303        $workflow_admin_modules['Forecasts'] = 'Forecasts';
1304    }
1305    foreach ($workflow_mod_list as $key=>$val) {
1306        if(!in_array($val, $workflow_admin_modules) && ($val!='iFrames' && $val!='Feeds' && $val!='Home' && $val!='Dashboard'
1307            && $val!='Calendar' && $val!='Activities' && $val!='Reports') &&
1308            (is_admin_for_module($user,$key))) {
1309                $workflow_admin_modules[$key] = $val;
1310        }
1311    }
1312    $_SESSION['get_workflow_admin_modules_for_user'] = $workflow_admin_modules;
1313    return ($workflow_admin_modules);
1314}
1315
1316// Check if user is admin for at least one module.
1317function is_admin_for_any_module($user) {
1318    return false;
1319}
1320
1321
1322// Check if user is admin for a specific module.
1323function is_admin_for_module($user,$module) {
1324    return false;
1325}
1326
1327
1328/**
1329 * Check if user id belongs to a system admin.
1330 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1331 * All Rights Reserved.
1332 * Contributor(s): ______________________________________..
1333 */
1334function is_admin($user) {
1335	if(!empty($user) && ($user->is_admin == '1' || $user->is_admin === 'on')){
1336		return true;
1337	}
1338
1339	return false;
1340}
1341
1342/**
1343 * Return the display name for a theme if it exists.
1344 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1345 * All Rights Reserved.
1346 * Contributor(s): ______________________________________..
1347 *
1348 * @deprecated use SugarThemeRegistry::get($theme)->name instead
1349 */
1350function get_theme_display($theme)
1351{
1352	return SugarThemeRegistry::get($theme)->name;
1353}
1354
1355/**
1356 * Return an array of directory names.
1357 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1358 * All Rights Reserved.
1359 * Contributor(s): ______________________________________..
1360 *
1361 * @deprecated use SugarThemeRegistry::availableThemes() instead.
1362 */
1363function get_themes()
1364{
1365    return SugarThemeRegistry::availableThemes();
1366}
1367
1368/**
1369 * THIS FUNCTION IS DEPRECATED AND SHOULD NOT BE USED; USE get_select_options_with_id()
1370 * Create HTML to display select options in a dropdown list.  To be used inside
1371 * of a select statement in a form.
1372 * param $option_list - the array of strings to that contains the option list
1373 * param $selected - the string which contains the default value
1374 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1375 * All Rights Reserved.
1376 * Contributor(s): ______________________________________..
1377 */
1378function get_select_options ($option_list, $selected) {
1379	return get_select_options_with_id($option_list, $selected);
1380}
1381
1382/**
1383 * Create HTML to display select options in a dropdown list.  To be used inside
1384 * of a select statement in a form.   This method expects the option list to have keys and values.  The keys are the ids.  The values are the display strings.
1385 * param $option_list - the array of strings to that contains the option list
1386 * param $selected - the string which contains the default value
1387 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1388 * All Rights Reserved.
1389 * Contributor(s): ______________________________________..
1390 */
1391function get_select_options_with_id ($option_list, $selected_key) {
1392	return get_select_options_with_id_separate_key($option_list, $option_list, $selected_key);
1393}
1394
1395
1396/**
1397 * Create HTML to display select options in a dropdown list.  To be used inside
1398 * of a select statement in a form.   This method expects the option list to have keys and values.  The keys are the ids.  The values are the display strings.
1399 * param $label_list - the array of strings to that contains the option list
1400 * param $key_list - the array of strings to that contains the values list
1401 * param $selected - the string which contains the default value
1402 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1403 * All Rights Reserved.
1404 * Contributor(s): ______________________________________..
1405 */
1406function get_select_options_with_id_separate_key ($label_list, $key_list, $selected_key, $massupdate=false) {
1407	global $app_strings;
1408	$select_options = "";
1409
1410	//for setting null selection values to human readable --None--
1411	$pattern = "/'0?'></";
1412	$replacement = "''>".$app_strings['LBL_NONE']."<";
1413
1414	if (empty($key_list)) $key_list = array();
1415	//create the type dropdown domain and set the selected value if $opp value already exists
1416	foreach ($key_list as $option_key=>$option_value) {
1417
1418		$selected_string = '';
1419		// the system is evaluating $selected_key == 0 || '' to true.  Be very careful when changing this.  Test all cases.
1420		// The bug was only happening with one of the users in the drop down.  It was being replaced by none.
1421		if (($option_key != '' && $selected_key == $option_key) || ($selected_key == '' && $option_key == '' && !$massupdate) || (is_array($selected_key) &&  in_array($option_key, $selected_key)))
1422		{
1423			$selected_string = 'selected ';
1424		}
1425
1426		$html_value = $option_key;
1427
1428		$select_options .= "\n<OPTION ".$selected_string."value='$html_value'>$label_list[$option_key]</OPTION>";
1429	}
1430	$select_options = preg_replace($pattern, $replacement, $select_options);
1431	return $select_options;
1432}
1433
1434
1435/**
1436 * Call this method instead of die().
1437 * Then we call the die method with the error message that is passed in.
1438 */
1439function sugar_die($error_message)
1440{
1441	global

Large files files are truncated, but you can click here to view the full file