PageRenderTime 59ms CodeModel.GetById 5ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 1ms

/include/utils.php

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

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