PageRenderTime 177ms CodeModel.GetById 80ms app.highlight 64ms RepoModel.GetById 2ms app.codeStats 2ms

/include/utils.php

https://github.com/mikmagic/sugarcrm_dev
PHP | 4325 lines | 3681 code | 258 blank | 386 comment | 304 complexity | 88daed9f46940b94fdfe45feae42cb08 MD5 | raw file

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

   1<?php
   2/*********************************************************************************
   3 * SugarCRM Community Edition 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 = !empty($sugar_config['use_real_names']);
 661		$userPref = (is_object($current_user)) ? $current_user->getPreference('use_real_names') : null;
 662
 663		if($userPref != null) {
 664			$showFullName = ($userPref == 'on');
 665		} else {
 666			$showFullName = $sysPref;
 667		}
 668	}
 669
 670	return $showFullName;
 671}
 672
 673function clean($string, $maxLength)
 674{
 675	$string = substr($string, 0, $maxLength);
 676	return escapeshellcmd($string);
 677}
 678
 679/**
 680 * Copy the specified request variable to the member variable of the specified object.
 681 * Do no copy if the member variable is already set.
 682 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 683 * All Rights Reserved.
 684 * Contributor(s): ______________________________________..
 685 */
 686function safe_map($request_var, & $focus, $always_copy = false)
 687{
 688	safe_map_named($request_var, $focus, $request_var, $always_copy);
 689}
 690
 691/**
 692 * Copy the specified request variable to the member variable of the specified object.
 693 * Do no copy if the member variable is already set.
 694 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 695 * All Rights Reserved.
 696 * Contributor(s): ______________________________________..
 697 */
 698function safe_map_named($request_var, & $focus, $member_var, $always_copy)
 699{
 700	if (isset($_REQUEST[$request_var]) && ($always_copy || is_null($focus->$member_var))) {
 701		$GLOBALS['log']->debug("safe map named called assigning '{$_REQUEST[$request_var]}' to $member_var");
 702		$focus->$member_var = $_REQUEST[$request_var];
 703	}
 704}
 705
 706/**
 707 * This function retrieves an application language file and returns the array of strings included in the $app_list_strings var.
 708 *
 709 * @param string $language specific language to load
 710 * @return array lang strings
 711 */
 712function return_app_list_strings_language($language)
 713{
 714	global $app_list_strings;
 715	global $sugar_config;
 716
 717	$cache_key = 'app_list_strings.'.$language;
 718
 719	// Check for cached value
 720	$cache_entry = sugar_cache_retrieve($cache_key);
 721	if(!empty($cache_entry))
 722	{
 723		return $cache_entry;
 724	}
 725
 726	$default_language = $sugar_config['default_language'];
 727	$temp_app_list_strings = $app_list_strings;
 728
 729	$langs = array();
 730	if ($language != 'en_us') {
 731	    $langs[] = 'en_us';
 732	}
 733	if ($default_language != 'en_us' && $language != $default_language) {
 734	    $langs[] = $default_language;
 735	}
 736	$langs[] = $language;
 737
 738	$app_list_strings_array = array();
 739
 740	foreach ( $langs as $lang ) {
 741	    $app_list_strings = array();
 742	    if(file_exists("include/language/$lang.lang.php")) {
 743            include("include/language/$lang.lang.php");
 744            $GLOBALS['log']->info("Found language file: $lang.lang.php");
 745        }
 746        if(file_exists("include/language/$lang.lang.override.php")) {
 747            include("include/language/$lang.lang.override.php");
 748            $GLOBALS['log']->info("Found override language file: $lang.lang.override.php");
 749        }
 750        if(file_exists("include/language/$lang.lang.php.override")) {
 751            include("include/language/$lang.lang.php.override");
 752            $GLOBALS['log']->info("Found override language file: $lang.lang.php.override");
 753        }
 754
 755        $app_list_strings_array[] = $app_list_strings;
 756    }
 757
 758    $app_list_strings = array();
 759    foreach ( $app_list_strings_array as $app_list_strings_item ) {
 760        $app_list_strings = sugarArrayMerge($app_list_strings, $app_list_strings_item);
 761    }
 762
 763    foreach ( $langs as $lang ) {
 764        if(file_exists("custom/application/Ext/Language/$lang.lang.ext.php")) {
 765            $app_list_strings = _mergeCustomAppListStrings("custom/application/Ext/Language/$lang.lang.ext.php" , $app_list_strings);
 766            $GLOBALS['log']->info("Found extended language file: $lang.lang.ext.php");
 767        }
 768        if(file_exists("custom/include/language/$lang.lang.php")) {
 769            include("custom/include/language/$lang.lang.php");
 770            $GLOBALS['log']->info("Found custom language file: $lang.lang.php");
 771        }
 772    }
 773
 774	if(!isset($app_list_strings)) {
 775		$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");
 776		return null;
 777	}
 778
 779	$return_value = $app_list_strings;
 780	$app_list_strings = $temp_app_list_strings;
 781
 782	sugar_cache_put($cache_key, $return_value);
 783
 784	return $return_value;
 785}
 786
 787/**
 788* The dropdown items in custom language files is $app_list_strings['$key']['$second_key'] = $value not
 789* $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.
 790 * @param file string the language that you want include,
 791 * @param app_list_strings array the golbal strings
 792 * @return array
 793 */
 794 //jchi 25347
 795function _mergeCustomAppListStrings($file , $app_list_strings){
 796	$app_list_strings_original = $app_list_strings;
 797	unset($app_list_strings);
 798	include($file);
 799	if(!isset($app_list_strings) || !is_array($app_list_strings)){
 800		return $app_list_strings_original;
 801	}
 802	//Bug 25347: We should not merge custom dropdown fields unless they relate to parent fields or the module list.
 803	foreach($app_list_strings as $key=>$value)
 804	{
 805		$exemptDropdowns = array("moduleList", "parent_type_display", "record_type_display", "record_type_display_notes");
 806		if (!in_array($key, $exemptDropdowns) && array_key_exists($key, $app_list_strings_original))
 807		{
 808	   		unset($app_list_strings_original["$key"]);
 809		}
 810   }
 811   $app_list_strings = sugarArrayMergeRecursive($app_list_strings_original , $app_list_strings);
 812   return $app_list_strings;
 813}
 814
 815/**
 816 * This function retrieves an application language file and returns the array of strings included.
 817 *
 818 * @param string $language specific language to load
 819 * @return array lang strings
 820 */
 821function return_application_language($language)
 822{
 823	global $app_strings, $sugar_config;
 824
 825	$cache_key = 'app_strings.'.$language;
 826
 827	// Check for cached value
 828	$cache_entry = sugar_cache_retrieve($cache_key);
 829	if(!empty($cache_entry))
 830	{
 831		return $cache_entry;
 832	}
 833
 834	$temp_app_strings = $app_strings;
 835	$default_language = $sugar_config['default_language'];
 836
 837	$langs = array();
 838	if ($language != 'en_us') {
 839	    $langs[] = 'en_us';
 840	}
 841	if ($default_language != 'en_us' && $language != $default_language) {
 842	    $langs[] = $default_language;
 843	}
 844
 845	$langs[] = $language;
 846
 847	$app_strings_array = array();
 848
 849	foreach ( $langs as $lang ) {
 850	    $app_strings = array();
 851	    if(file_exists("include/language/$lang.lang.php")) {
 852            include("include/language/$lang.lang.php");
 853            $GLOBALS['log']->info("Found language file: $lang.lang.php");
 854        }
 855        if(file_exists("include/language/$lang.lang.override.php")) {
 856            include("include/language/$lang.lang.override.php");
 857            $GLOBALS['log']->info("Found override language file: $lang.lang.override.php");
 858        }
 859        if(file_exists("include/language/$lang.lang.php.override")) {
 860            include("include/language/$lang.lang.php.override");
 861            $GLOBALS['log']->info("Found override language file: $lang.lang.php.override");
 862        }
 863        if(file_exists("custom/application/Ext/Language/$lang.lang.ext.php")) {
 864            include("custom/application/Ext/Language/$lang.lang.ext.php");
 865            $GLOBALS['log']->info("Found extended language file: $lang.lang.ext.php");
 866        }
 867        if(file_exists("custom/include/language/$lang.lang.php")) {
 868            include("custom/include/language/$lang.lang.php");
 869            $GLOBALS['log']->info("Found custom language file: $lang.lang.php");
 870        }
 871        $app_strings_array[] = $app_strings;
 872	}
 873
 874	$app_strings = array();
 875    foreach ( $app_strings_array as $app_strings_item ) {
 876        $app_strings = sugarArrayMerge($app_strings, $app_strings_item);
 877    }
 878
 879	if(!isset($app_strings)) {
 880		$GLOBALS['log']->fatal("Unable to load the application language strings");
 881		return null;
 882	}
 883
 884	// If we are in debug mode for translating, turn on the prefix now!
 885	if($sugar_config['translation_string_prefix']) {
 886		foreach($app_strings as $entry_key=>$entry_value) {
 887			$app_strings[$entry_key] = $language.' '.$entry_value;
 888		}
 889	}
 890	if(isset($_SESSION['show_deleted'])) {
 891		$app_strings['LBL_DELETE_BUTTON'] = $app_strings['LBL_UNDELETE_BUTTON'];
 892		$app_strings['LBL_DELETE_BUTTON_LABEL'] = $app_strings['LBL_UNDELETE_BUTTON_LABEL'];
 893		$app_strings['LBL_DELETE_BUTTON_TITLE'] = $app_strings['LBL_UNDELETE_BUTTON_TITLE'];
 894		$app_strings['LBL_DELETE'] = $app_strings['LBL_UNDELETE'];
 895	}
 896
 897	$app_strings['LBL_ALT_HOT_KEY'] = get_alt_hot_key();
 898
 899	$return_value = $app_strings;
 900	$app_strings = $temp_app_strings;
 901
 902	sugar_cache_put($cache_key, $return_value);
 903
 904	return $return_value;
 905}
 906
 907/**
 908 * This function retrieves a module's language file and returns the array of strings included.
 909 *
 910 * @param string $language specific language to load
 911 * @param string $module module name to load strings for
 912 * @param bool $refresh optional, true if you want to rebuild the language strings
 913 * @return array lang strings
 914 */
 915function return_module_language($language, $module, $refresh=false)
 916{
 917	global $mod_strings;
 918	global $sugar_config;
 919	global $currentModule;
 920
 921	// Jenny - Bug 8119: Need to check if $module is not empty
 922	if (empty($module)) {
 923		$stack  = debug_backtrace();
 924		$GLOBALS['log']->warn("Variable module is not in return_module_language ". var_export($stack, true));
 925		return array();
 926	}
 927
 928	$cache_key = LanguageManager::getLanguageCacheKey($module, $language);
 929	// Check for cached value
 930	$cache_entry = sugar_cache_retrieve($cache_key);
 931	if(!empty($cache_entry))
 932	{
 933		return $cache_entry;
 934	}
 935
 936	// Store the current mod strings for later
 937	$temp_mod_strings = $mod_strings;
 938	$loaded_mod_strings = array();
 939	$language_used = $language;
 940	$default_language = $sugar_config['default_language'];
 941
 942	if(empty($language)) {
 943		$language = $default_language;
 944	}
 945
 946	// Bug 21559 - So we can get all the strings defined in the template, refresh
 947	// the vardefs file if the cached language file doesn't exist.
 948    if(!file_exists($GLOBALS['sugar_config']['cache_dir'].'modules/'. $module . '/language/'.$language.'.lang.php')
 949			&& !empty($GLOBALS['beanList'][$module])){
 950		$object = $GLOBALS['beanList'][$module];
 951		if ($object == 'aCase') {
 952            $object = 'Case';
 953		}
 954		VardefManager::refreshVardefs($module,$object);
 955	}
 956
 957	$loaded_mod_strings = LanguageManager::loadModuleLanguage($module, $language,$refresh);
 958
 959	// cn: bug 6048 - merge en_us with requested language
 960	if($language != $sugar_config['default_language'])
 961        $loaded_mod_strings = sugarArrayMerge(
 962            LanguageManager::loadModuleLanguage($module, $sugar_config['default_language'],$refresh),
 963                $loaded_mod_strings
 964            );
 965
 966    // Load in en_us strings by default
 967    if($language != 'en_us' && $sugar_config['default_language'] != 'en_us')
 968        $loaded_mod_strings = sugarArrayMerge(
 969            LanguageManager::loadModuleLanguage($module, 'en_us', $refresh),
 970                $loaded_mod_strings
 971            );
 972
 973	// If we are in debug mode for translating, turn on the prefix now!
 974	if($sugar_config['translation_string_prefix']) {
 975		foreach($loaded_mod_strings as $entry_key=>$entry_value) {
 976			$loaded_mod_strings[$entry_key] = $language_used.' '.$entry_value;
 977		}
 978	}
 979
 980	$return_value = $loaded_mod_strings;
 981	if(!isset($mod_strings)){
 982		$mod_strings = $return_value;
 983	}
 984	else
 985		$mod_strings = $temp_mod_strings;
 986
 987    sugar_cache_put($cache_key, $return_value);
 988	return $return_value;
 989}
 990
 991
 992/** This function retrieves an application language file and returns the array of strings included in the $mod_list_strings var.
 993 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 994 * All Rights Reserved.
 995 * Contributor(s): ______________________________________..
 996 * If you are using the current language, do not call this function unless you are loading it for the first time */
 997function return_mod_list_strings_language($language,$module) {
 998	global $mod_list_strings;
 999	global $sugar_config;
1000	global $currentModule;
1001
1002	$cache_key = "mod_list_str_lang.".$language.$module;
1003
1004	// Check for cached value
1005	$cache_entry = sugar_cache_retrieve($cache_key);
1006	if(!empty($cache_entry))
1007	{
1008		return $cache_entry;
1009	}
1010
1011	$language_used = $language;
1012	$temp_mod_list_strings = $mod_list_strings;
1013	$default_language = $sugar_config['default_language'];
1014
1015	if($currentModule == $module && isset($mod_list_strings) && $mod_list_strings != null) {
1016		return $mod_list_strings;
1017	}
1018
1019	// cn: bug 6351 - include en_us if file langpack not available
1020	// cn: bug 6048 - merge en_us with requested language
1021	include("modules/$module/language/en_us.lang.php");
1022	$en_mod_list_strings = array();
1023	if($language_used != $default_language)
1024	$en_mod_list_strings = $mod_list_strings;
1025
1026	if(file_exists("modules/$module/language/$language.lang.php")) {
1027		include("modules/$module/language/$language.lang.php");
1028	}
1029
1030	if(file_exists("modules/$module/language/$language.lang.override.php")){
1031		include("modules/$module/language/$language.lang.override.php");
1032	}
1033
1034	if(file_exists("modules/$module/language/$language.lang.php.override")){
1035		echo 'Please Change:<br>' . "modules/$module/language/$language.lang.php.override" . '<br>to<br>' . 'Please Change:<br>' . "modules/$module/language/$language.lang.override.php";
1036		include("modules/$module/language/$language.lang.php.override");
1037	}
1038
1039	// cn: bug 6048 - merge en_us with requested language
1040	$mod_list_strings = sugarArrayMerge($en_mod_list_strings, $mod_list_strings);
1041
1042	// if we still don't have a language pack, then log an error
1043	if(!isset($mod_list_strings)) {
1044		$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})");
1045		return null;
1046	}
1047
1048	$return_value = $mod_list_strings;
1049	$mod_list_strings = $temp_mod_list_strings;
1050
1051	sugar_cache_put($cache_key, $return_value);
1052	return $return_value;
1053}
1054
1055
1056/** This function retrieves a theme's language file and returns the array of strings included.
1057 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1058 * All Rights Reserved.
1059 * Contributor(s): ______________________________________..
1060 */
1061function return_theme_language($language, $theme)
1062{
1063	global $mod_strings, $sugar_config, $currentModule;
1064
1065	$language_used = $language;
1066	$default_language = $sugar_config['default_language'];
1067
1068	include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php");
1069	if(file_exists(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php")){
1070		include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.override.php");
1071	}
1072	if(file_exists(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override")){
1073		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";
1074		include(SugarThemeRegistry::get($theme)->getFilePath()."/language/$current_language.lang.php.override");
1075	}
1076	if(!isset($theme_strings))
1077	{
1078		$GLOBALS['log']->warn("Unable to find the theme file for language: ".$language." and theme: ".$theme);
1079		require(SugarThemeRegistry::get($theme)->getFilePath()."/language/$default_language.lang.php");
1080		$language_used = $default_language;
1081	}
1082
1083	if(!isset($theme_strings))
1084	{
1085		$GLOBALS['log']->fatal("Unable to load the theme($theme) language file for the selected language($language) or the default language($default_language)");
1086		return null;
1087	}
1088
1089	// If we are in debug mode for translating, turn on the prefix now!
1090	if($sugar_config['translation_string_prefix'])
1091	{
1092		foreach($theme_strings as $entry_key=>$entry_value)
1093		{
1094			$theme_strings[$entry_key] = $language_used.' '.$entry_value;
1095		}
1096	}
1097
1098	return $theme_strings;
1099}
1100
1101
1102
1103/** If the session variable is defined and is not equal to "" then return it.  Otherwise, return the default value.
1104 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1105 * All Rights Reserved.
1106 * Contributor(s): ______________________________________..
1107 */
1108function return_session_value_or_default($varname, $default)
1109{
1110	if(isset($_SESSION[$varname]) && $_SESSION[$varname] != "")
1111	{
1112		return $_SESSION[$varname];
1113	}
1114
1115	return $default;
1116}
1117
1118/**
1119 * Creates an array of where restrictions.  These are used to construct a where SQL statement on the query
1120 * 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.
1121 * @param &$where_clauses - The array to append the clause to
1122 * @param $variable_name - The name of the variable to look for an add to the where clause if found
1123 * @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.
1124 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1125 * All Rights Reserved.
1126 * Contributor(s): ______________________________________..
1127 */
1128function append_where_clause(&$where_clauses, $variable_name, $SQL_name = null)
1129{
1130	if($SQL_name == null)
1131	{
1132		$SQL_name = $variable_name;
1133	}
1134
1135	if(isset($_REQUEST[$variable_name]) && $_REQUEST[$variable_name] != "")
1136	{
1137		array_push($where_clauses, "$SQL_name like '".$GLOBALS['db']->quote($_REQUEST[$variable_name])."%'");
1138	}
1139}
1140
1141/**
1142 * Generate the appropriate SQL based on the where clauses.
1143 * @param $where_clauses - An Array of individual where clauses stored as strings
1144 * @returns string where_clause - The final SQL where clause to be executed.
1145 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1146 * All Rights Reserved.
1147 * Contributor(s): ______________________________________..
1148 */
1149function generate_where_statement($where_clauses)
1150{
1151	$where = "";
1152	foreach($where_clauses as $clause)
1153	{
1154		if($where != "")
1155		$where .= " and ";
1156		$where .= $clause;
1157	}
1158
1159	$GLOBALS['log']->info("Here is the where clause for the list view: $where");
1160	return $where;
1161}
1162
1163/**
1164 * determines if a passed string matches the criteria for a Sugar GUID
1165 * @param string $guid
1166 * @return bool False on failure
1167 */
1168function is_guid($guid) {
1169	if(strlen($guid) != 36) {
1170		return false;
1171	}
1172
1173	if(preg_match("/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/i", $guid)) {
1174		return true;
1175	}
1176
1177	return true;;
1178}
1179
1180
1181/**
1182 * A temporary method of generating GUIDs of the correct format for our DB.
1183 * @return String contianing a GUID in the format: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
1184 *
1185 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1186 * All Rights Reserved.
1187 * Contributor(s): ______________________________________..
1188 */
1189function create_guid()
1190{
1191	$microTime = microtime();
1192	list($a_dec, $a_sec) = explode(" ", $microTime);
1193
1194	$dec_hex = dechex($a_dec* 1000000);
1195	$sec_hex = dechex($a_sec);
1196
1197	ensure_length($dec_hex, 5);
1198	ensure_length($sec_hex, 6);
1199
1200	$guid = "";
1201	$guid .= $dec_hex;
1202	$guid .= create_guid_section(3);
1203	$guid .= '-';
1204	$guid .= create_guid_section(4);
1205	$guid .= '-';
1206	$guid .= create_guid_section(4);
1207	$guid .= '-';
1208	$guid .= create_guid_section(4);
1209	$guid .= '-';
1210	$guid .= $sec_hex;
1211	$guid .= create_guid_section(6);
1212
1213	return $guid;
1214
1215}
1216
1217function create_guid_section($characters)
1218{
1219	$return = "";
1220	for($i=0; $i<$characters; $i++)
1221	{
1222		$return .= dechex(mt_rand(0,15));
1223	}
1224	return $return;
1225}
1226
1227function ensure_length(&$string, $length)
1228{
1229	$strlen = strlen($string);
1230	if($strlen < $length)
1231	{
1232		$string = str_pad($string,$length,"0");
1233	}
1234	else if($strlen > $length)
1235	{
1236		$string = substr($string, 0, $length);
1237	}
1238}
1239
1240function microtime_diff($a, $b) {
1241	list($a_dec, $a_sec) = explode(" ", $a);
1242	list($b_dec, $b_sec) = explode(" ", $b);
1243	return $b_sec - $a_sec + $b_dec - $a_dec;
1244}
1245
1246// check if Studio is displayed.
1247function displayStudioForCurrentUser()
1248{
1249    global $current_user;
1250    if ( $current_user->isAdmin() ) {
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    $GLOBALS['log']->deprecated("get_admin_modules_for_user() is deprecated as of 6.2.2 and may disappear in the future, use Users->getDeveloperModules() instead");
1269
1270    if(!isset($user)){
1271        $modules = array();
1272        return $modules;
1273    }
1274
1275    return($user->getDeveloperModules());
1276    
1277}
1278
1279 function get_workflow_admin_modules_for_user($user){
1280    if (isset($_SESSION['get_workflow_admin_modules_for_user'])) {
1281        return $_SESSION['get_workflow_admin_modules_for_user'];
1282    }
1283
1284    global $moduleList;
1285    $workflow_mod_list = array();
1286    foreach($moduleList as $module){
1287		$workflow_mod_list[$module] = $module;
1288	}
1289
1290	// This list is taken from teh previous version of workflow_utils.php
1291    $workflow_mod_list['Tasks'] = "Tasks";
1292    $workflow_mod_list['Calls'] = "Calls";
1293    $workflow_mod_list['Meetings'] = "Meetings";
1294    $workflow_mod_list['Notes'] = "Notes";
1295    $workflow_mod_list['ProjectTask'] = "Project Tasks";
1296	$workflow_mod_list['Leads'] = "Leads";
1297	$workflow_mod_list['Opportunities'] = "Opportunities";
1298	// End of list
1299
1300    $workflow_admin_modules = array();
1301    if(empty($user)) {
1302    	return $workflow_admin_modules;
1303    }
1304    $actions = ACLAction::getUserActions($user->id);
1305    //check for ForecastSchedule because it doesn't exist in $workflow_mod_list
1306    if (isset($actions['ForecastSchedule']['module']['admin']['aclaccess']) && ($actions['ForecastSchedule']['module']['admin']['aclaccess']==ACL_ALLOW_DEV ||
1307        $actions['ForecastSchedule']['module']['admin']['aclaccess']==ACL_ALLOW_ADMIN_DEV)) {
1308        $workflow_admin_modules['Forecasts'] = 'Forecasts';
1309    }
1310    foreach ($workflow_mod_list as $key=>$val) {
1311        if(!in_array($val, $workflow_admin_modules) && ($val!='iFrames' && $val!='Feeds' && $val!='Home' && $val!='Dashboard'
1312            && $val!='Calendar' && $val!='Activities' && $val!='Reports') &&
1313           ($user->isDeveloperForModule($key))) {
1314                $workflow_admin_modules[$key] = $val;
1315        }
1316    }
1317    $_SESSION['get_workflow_admin_modules_for_user'] = $workflow_admin_modules;
1318    return ($workflow_admin_modules);
1319}
1320
1321// Check if user is admin for at least one module.
1322function is_admin_for_any_module($user) {
1323    if (!isset($user)){
1324        return false;
1325    }
1326    if($user->isAdmin()) {
1327        return true;
1328    }
1329    return false;
1330}
1331
1332
1333// Check if user is admin for a specific module.
1334function is_admin_for_module($user,$module) {
1335    if (!isset($user)) {
1336        return false;
1337    }
1338    if ($user->isAdmin()) {
1339        return true;
1340    }
1341    return false;
1342}
1343
1344
1345/**
1346 * Check if user id belongs to a system admin.
1347 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1348 * All Rights Reserved.
1349 * Contributor(s): ______________________________________..
1350 */
1351function is_admin($user) {
1352    if(empty($user)) {
1353        return false;
1354    }
1355    
1356	return $user->isAdmin();
1357}
1358
1359/**
1360 * Return the display name for a theme if it exists.
1361 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1362 * All Rights Reserved.
1363 * Contributor(s): ______________________________________..
1364 *
1365 * @deprecated use SugarThemeRegistry::get($theme)->name instead
1366 */
1367function get_theme_display($theme)
1368{
1369	return SugarThemeRegistry::get($theme)->name;
1370}
1371
1372/**
1373 * Return an array of directory names.
1374 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1375 * All Rights Reserved.
1376 * Contributor(s): ______________________________________..
1377 *
1378 * @deprecated use SugarThemeRegistry::availableThemes() instead.
1379 */
1380function get_themes()
1381{
1382    return SugarThemeRegistry::availableThemes();
1383}
1384
1385/**
1386 * THIS FUNCTION IS DEPRECATED AND SHOULD NOT BE USED; USE get_select_options_with_id()
1387 * Create HTML to display select options in a dropdown list.  To be used inside
1388 * of a select statement in a form.
1389 * param $option_list - the array of strings to that contains the option list
1390 * param $selected - the string which contains the default value
1391 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1392 * All Rights Reserved.
1393 * Contributor(s): ______________________________________..
1394 */
1395function get_select_options ($option_list, $selected) {
1396	return get_select_options_with_id($option_list, $selected);
1397}
1398
1399/**
1400 * Create HTML to display select options in a dropdown list.  To be used inside
1401 * 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.
1402 * param $option_list - the array of strings to that contains the option list
1403 * param $selected - the string which contains the default value
1404 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1405 * All Rights Reserved.
1406 * Contributor(s): ______________________________________..
1407 */
1408function get_select_options_with_id ($option_list, $selected_key) {
1409	return get_select_options_with_id_separate_key($option_list, $option_list, $selected_key);
1410}
1411
1412
1413/**
1414 * Create HTML to display select options in a dropdown list.  To be used inside
1415 * 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.
1416 * param $label_list - the array of strings to that contains the option list
1417 * param $key_list - the array of strings to that contains the values list
1418 * param $selected - the string which contains the default value
1419 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1420 * All Rights Reserved.
1421 * Contributor(s): ______________________________________..
1422 */
1423function get_select_options_with_id_separate_key ($label_list, $key_list, $selected_key, $massupdate=false) {
1424	global $app_strings;
1425	$select_options = "";
1426
1427	//for setting null selection values to human readable --None--
1428	$pattern = "/'0?'></";
1429	$replacement = "''>".$app_strings['LBL_NONE']."<";
1430
1431	if (empty($key_list)) $key_list = array();
1432	//create the type dropdown domain and set the selected value if $opp value already exists
1433	foreach ($key_list as $option_key=>$option_value) {
1434
1435		$selected_string = '';
1436		// the system is evaluating $selected_key == 0 || '' to true.  Be very careful when changing this.  Test all cases.
1437		// The bug was only happening with one of the users in the drop down.  It was being replaced by none.
1438		if (($option_key != '' && $selected_key == $option_key) || ($selected_key == '' && $option_key == '' && !$massupdate) || (is_array($selected_key) &&  in_array($option_key, $selected_key)))
1439		{
1440			$selected_string = 'selected ';
1441		}
1442
1443		$html_value = $option_key;
1444
1445		$select_options .= "\n<OP…

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