| ');
$phpinfo = preg_replace('/ | ]*>([^<]+)<\/th>/',"\\1",$phpinfo);
$phpinfo = preg_replace('/ | ]*>([^<]+)<\/td>/',"\\1",$phpinfo);
$parsedInfo = preg_split('/([^<]+<\/h.>)/', $phpinfo, -1, PREG_SPLIT_DELIM_CAPTURE);
$match = '';
$version = '';
$returnInfo = array();
if(preg_match('/PHP Version ([^<]+)<\/h1>/', $phpinfo, $version)) {
$returnInfo['PHP Version'] = $version[1];
}
for ($i=1; $i([^<]+)<\/h.>/', $parsedInfo[$i], $match)) {
$vName = trim($match[1]);
$parsedInfo2 = explode("\n",$parsedInfo[$i+1]);
foreach ($parsedInfo2 AS $vOne) {
$vPat = '([^<]+)<\/info>';
$vPat3 = "/$vPat\s*$vPat\s*$vPat/";
$vPat2 = "/$vPat\s*$vPat/";
if (preg_match($vPat3,$vOne,$match)) { // 3cols
$returnInfo[$vName][trim($match[1])] = array(trim($match[2]),trim($match[3]));
} elseif (preg_match($vPat2,$vOne,$match)) { // 2cols
$returnInfo[$vName][trim($match[1])] = trim($match[2]);
}
}
} elseif(true) {
}
}
return $returnInfo;
}
/**
* This function will take a string that has tokens like {0}, {1} and will replace
* those tokens with the args provided
* @param $format string to format
* @param $args args to replace
* @return $result a formatted string
*/
function string_format($format, $args){
$result = $format;
/** Bug47277 fix.
* If args array has only one argument, and it's empty, so empty single quotes are used '' . That's because
* IN () fails and IN ('') works.
*/
if (count($args) == 1)
{
reset($args);
$singleArgument = current($args);
if (empty($singleArgument))
{
return str_replace("{0}", "''", $result);
}
}
/* End of fix */
for($i = 0; $i < count($args); $i++){
$result = str_replace('{'.$i.'}', $args[$i], $result);
}
return $result;
}
/**
* Generate a string for displaying a unique identifier that is composed
* of a system_id and number. This is use to allow us to generate quote
* numbers using a DB auto-increment key from offline clients and still
* have the number be unique (since it is modified by the system_id.
*
* @param $num of bean
* @param $system_id from system
* @return $result a formatted string
*/
function format_number_display($num, $system_id){
global $sugar_config;
if(isset($num) && !empty($num)){
$num=unformat_number($num);
if(isset($system_id) && $system_id == 1){
return sprintf("%d", $num);
}
else{
return sprintf("%d-%d", $num, $system_id);
}
}
}
function checkLoginUserStatus(){
}
/**
* This function will take a number and system_id and format
* @param $url URL containing host to append port
* @param $port the port number - if '' is passed, no change to url
* @return $resulturl the new URL with the port appended to the host
*/
function appendPortToHost($url, $port)
{
$resulturl = $url;
// if no port, don't change the url
if($port != '')
{
$split = explode("/", $url);
//check if it starts with http, in case they didn't include that in url
if(str_begin($url, 'http'))
{
//third index ($split[2]) will be the host
$split[2] .= ":".$port;
}
else // otherwise assumed to start with host name
{
//first index ($split[0]) will be the host
$split[0] .= ":".$port;
}
$resulturl = implode("/", $split);
}
return $resulturl;
}
/**
* Singleton to return JSON object
* @return JSON object
*/
function getJSONobj() {
static $json = null;
if(!isset($json)) {
require_once('include/JSON.php');
$json = new JSON(JSON_LOOSE_TYPE);
}
return $json;
}
require_once('include/utils/db_utils.php');
/**
* Set default php.ini settings for entry points
*/
function setPhpIniSettings() {
// zlib module
// Bug 37579 - Comment out force enabling zlib.output_compression, since it can cause problems on certain hosts
/*
if(function_exists('gzclose') && headers_sent() == false) {
ini_set('zlib.output_compression', 1);
}
*/
// mbstring module
//nsingh: breaks zip/unzip functionality. Commenting out 4/23/08
/*if(function_exists('mb_strlen')) {
ini_set('mbstring.func_overload', 7);
ini_set('mbstring.internal_encoding', 'UTF-8');
}*/
// http://us3.php.net/manual/en/ref.pcre.php#ini.pcre.backtrack-limit
// starting with 5.2.0, backtrack_limit breaks JSON decoding
$backtrack_limit = ini_get('pcre.backtrack_limit');
if(!empty($backtrack_limit)) {
ini_set('pcre.backtrack_limit', '-1');
}
}
/**
* Identical to sugarArrayMerge but with some speed improvements and used specifically to merge
* language files. Language file merges do not need to account for null values so we can get some
* performance increases by using this specialized function. Note this merge function does not properly
* handle null values.
*
* @param $gimp
* @param $dom
* @return array
*/
function sugarLangArrayMerge($gimp, $dom)
{
if(is_array($gimp) && is_array($dom))
{
foreach($dom as $domKey => $domVal)
{
if(isset($gimp[$domKey]))
{
if(is_array($domVal))
{
$tempArr = array();
foreach ( $domVal as $domArrKey => $domArrVal )
$tempArr[$domArrKey] = $domArrVal;
foreach ( $gimp[$domKey] as $gimpArrKey => $gimpArrVal )
if ( !isset($tempArr[$gimpArrKey]) )
$tempArr[$gimpArrKey] = $gimpArrVal;
$gimp[$domKey] = $tempArr;
}
else
{
$gimp[$domKey] = $domVal;
}
}
else
{
$gimp[$domKey] = $domVal;
}
}
}
// if the passed value for gimp isn't an array, then return the $dom
elseif(is_array($dom))
{
return $dom;
}
return $gimp;
}
/**
* like array_merge() but will handle array elements that are themselves arrays;
* PHP's version just overwrites the element with the new one.
*
* @internal Note that this function deviates from the internal array_merge()
* functions in that it does does not treat numeric keys differently
* than string keys. Additionally, it deviates from
* array_merge_recursive() by not creating an array when like values
* found.
*
* @param array gimp the array whose values will be overloaded
* @param array dom the array whose values will pwn the gimp's
* @return array beaten gimp
*/
function sugarArrayMerge($gimp, $dom) {
if(is_array($gimp) && is_array($dom)) {
foreach($dom as $domKey => $domVal) {
if(array_key_exists($domKey, $gimp)) {
if(is_array($domVal)) {
$tempArr = array();
foreach ( $domVal as $domArrKey => $domArrVal )
$tempArr[$domArrKey] = $domArrVal;
foreach ( $gimp[$domKey] as $gimpArrKey => $gimpArrVal )
if ( !array_key_exists($gimpArrKey, $tempArr) )
$tempArr[$gimpArrKey] = $gimpArrVal;
$gimp[$domKey] = $tempArr;
} else {
$gimp[$domKey] = $domVal;
}
} else {
$gimp[$domKey] = $domVal;
}
}
}
// if the passed value for gimp isn't an array, then return the $dom
elseif(is_array($dom))
return $dom;
return $gimp;
}
/**
* Similiar to sugarArrayMerge except arrays of N depth are merged.
*
* @param array gimp the array whose values will be overloaded
* @param array dom the array whose values will pwn the gimp's
* @return array beaten gimp
*/
function sugarArrayMergeRecursive($gimp, $dom) {
if(is_array($gimp) && is_array($dom)) {
foreach($dom as $domKey => $domVal) {
if(array_key_exists($domKey, $gimp)) {
if(is_array($domVal) && is_array($gimp[$domKey])) {
$gimp[$domKey] = sugarArrayMergeRecursive($gimp[$domKey], $domVal);
} else {
$gimp[$domKey] = $domVal;
}
} else {
$gimp[$domKey] = $domVal;
}
}
}
// if the passed value for gimp isn't an array, then return the $dom
elseif(is_array($dom))
return $dom;
return $gimp;
}
/**
* finds the correctly working versions of PHP-JSON
* @return bool True if NOT found or WRONG version
*/
function returnPhpJsonStatus() {
if(function_exists('json_encode')) {
$phpInfo = getPhpInfo(8);
return version_compare($phpInfo['json']['json version'], '1.1.1', '<');
}
return true; // not found
}
/**
* getTrackerSubstring
*
* Returns a [number]-char or less string for the Tracker to display in the header
* based on the tracker_max_display_length setting in config.php. If not set,
* or invalid length, then defaults to 15 for COM editions, 30 for others.
*
* @param string name field for a given Object
* @return string [number]-char formatted string if length of string exceeds the max allowed
*/
function getTrackerSubstring($name) {
static $max_tracker_item_length;
//Trim the name
$name = html_entity_decode($name, ENT_QUOTES, 'UTF-8');
$strlen = function_exists('mb_strlen') ? mb_strlen($name) : strlen($name);
global $sugar_config;
if(!isset($max_tracker_item_length)) {
if(isset($sugar_config['tracker_max_display_length'])) {
$max_tracker_item_length = (is_int($sugar_config['tracker_max_display_length']) && $sugar_config['tracker_max_display_length'] > 0 && $sugar_config['tracker_max_display_length'] < 50) ? $sugar_config['tracker_max_display_length'] : 15;
} else {
$max_tracker_item_length = 15;
}
}
if($strlen > $max_tracker_item_length) {
$chopped = function_exists('mb_substr') ? mb_substr($name, 0, $max_tracker_item_length, "UTF-8") : substr($name, 0, $max_tracker_item_length, "UTF-8");
} else {
$chopped = $name;
}
return $chopped;
}
function generate_search_where ($field_list=array(),$values=array(),&$bean,$add_custom_fields=false,$module='') {
$where_clauses= array();
$like_char='%';
$table_name=$bean->object_name;
foreach ($field_list[$module] as $field=>$parms) {
if(isset($values[$field]) && $values[$field] != "") {
$operator='like';
if (!empty($parms['operator'])) {
$operator=$parms['operator'];
}
if (is_array($values[$field])) {
$operator='in';
$field_value='';
foreach ($values[$field] as $key => $val) {
if ($val != ' ' and $val != '') {
if (!empty($field_value)) {
$field_value.=',';
}
$field_value .= "'".$GLOBALS['db']->quote($val)."'";
}
}
} else {
$field_value=$GLOBALS['db']->quote($values[$field]);
}
//set db_fields array.
if (!isset($parms['db_field']) ) {
$parms['db_field'] = array($field);
}
if (isset($parms['my_items']) and $parms['my_items'] == true) {
global $current_user;
$field_value = $GLOBALS['db']->quote($current_user->id);
$operator='=';
}
$where='';
$itr=0;
if ($field_value != '') {
foreach ($parms['db_field'] as $db_field) {
if (strstr($db_field,'.')===false) {
$db_field=$bean->table_name.".".$db_field;
}
if ($GLOBALS['db']->supports('case_sensitive') && isset($parms['query_type']) && $parms['query_type']=='case_insensitive') {
$db_field='upper('.$db_field.")";
$field_value=strtoupper($field_value);
}
$itr++;
if (!empty($where)) {
$where .= " OR ";
}
switch (strtolower($operator)) {
case 'like' :
$where .= $db_field . " like '".$field_value.$like_char."'";
break;
case 'in':
$where .= $db_field . " in (".$field_value.')';
break;
case '=':
$where .= $db_field . " = '".$field_value ."'";
break;
}
}
}
if (!empty($where)) {
if ($itr>1) {
array_push($where_clauses, '( '.$where.' )');
} else {
array_push($where_clauses, $where);
}
}
}
}
if ($add_custom_fields) {
require_once('modules/DynamicFields/DynamicField.php');
$bean->setupCustomFields($module);
$bean->custom_fields->setWhereClauses($where_clauses);
}
return $where_clauses;
}
function add_quotes($str) {
return "'{$str}'";
}
/**
* This function will rebuild the config file
* @param $sugar_config
* @param $sugar_version
* @return bool true if successful
*/
function rebuildConfigFile($sugar_config, $sugar_version) {
// add defaults to missing values of in-memory sugar_config
$sugar_config = sugarArrayMerge(get_sugar_config_defaults(), $sugar_config );
// need to override version with default no matter what
$sugar_config['sugar_version'] = $sugar_version;
ksort( $sugar_config );
if( write_array_to_file( "sugar_config", $sugar_config, "config.php" ) ){
return true;
}
else {
return false;
}
}
/**
* getJavascriptSiteURL
* This function returns a URL for the client javascript calls to access
* the site. It uses $_SERVER['HTTP_REFERER'] in the event that Proxy servers
* are used to access the site. Thus, the hostname in the URL returned may
* not always match that of $sugar_config['site_url']. Basically, the
* assumption is that however the user accessed the website is how they
* will continue to with subsequent javascript requests. If the variable
* $_SERVER['HTTP_REFERER'] is not found then we default to old algorithm.
* @return $site_url The url used to refer to the website
*/
function getJavascriptSiteURL() {
global $sugar_config;
if(!empty($_SERVER['HTTP_REFERER'])) {
$url = parse_url($_SERVER['HTTP_REFERER']);
$replacement_url = $url['scheme']."://".$url['host'];
if(!empty($url['port']))
$replacement_url .= ':'.$url['port'];
$site_url = preg_replace('/^http[s]?\:\/\/[^\/]+/',$replacement_url,$sugar_config['site_url']);
} else {
$site_url = preg_replace('/^http(s)?\:\/\/[^\/]+/',"http$1://".$_SERVER['HTTP_HOST'],$sugar_config['site_url']);
if(!empty($_SERVER['SERVER_PORT']) &&$_SERVER['SERVER_PORT'] == '443') {
$site_url = preg_replace('/^http\:/','https:',$site_url);
}
}
$GLOBALS['log']->debug("getJavascriptSiteURL(), site_url=". $site_url);
return $site_url;
}
// works nicely with array_map() -- can be used to wrap single quotes around each element in an array
function add_squotes($str) {
return "'" . $str . "'";
}
// recursive function to count the number of levels within an array
function array_depth($array, $depth_count=-1, $depth_array=array()){
$depth_count++;
if (is_array($array)){
foreach ($array as $key => $value){
$depth_array[] = array_depth($value, $depth_count);
}
}
else{
return $depth_count;
}
foreach ($depth_array as $value){
$depth_count = $value > $depth_count ? $value : $depth_count;
}
return $depth_count;
}
/**
* Creates a new Group User
* @param string $name Name of Group User
* @return string GUID of new Group User
*/
function createGroupUser($name) {
$group = new User();
$group->user_name = $name;
$group->last_name = $name;
$group->is_group = 1;
$group->deleted = 0;
$group->status = 'Active'; // cn: bug 6711
$group->setPreference('timezone', TimeDate::userTimezone());
$group->save();
return $group->id;
}
/*
* Helper function to locate an icon file given only a name
* Searches through the various paths for the file
* @param string iconFileName The filename of the icon
* @return string Relative pathname of the located icon, or '' if not found
*/
function _getIcon($iconFileName)
{
$iconName = "icon_{$iconFileName}.gif";
$iconFound = SugarThemeRegistry::current()->getImageURL($iconName,false);
//First try un-ucfirst-ing the icon name
if ( empty($iconFound) )
$iconName = "icon_" . strtolower(substr($iconFileName,0,1)).substr($iconFileName,1) . ".gif";
$iconFound = SugarThemeRegistry::current()->getImageURL($iconName,false);
//Next try removing the icon prefix
if ( empty($iconFound) )
$iconName = "{$iconFileName}.gif";
$iconFound = SugarThemeRegistry::current()->getImageURL($iconName,false);
if ( empty($iconFound) )
$iconName = '';
return $iconName;
}
/**
* Function to grab the correct icon image for Studio
* @param string $iconFileName Name of the icon file
* @param string $altfilename Name of a fallback icon file (displayed if the imagefilename doesn't exist)
* @param string $width Width of image
* @param string $height Height of image
* @param string $align Alignment of image
* @param string $alt Alt tag of image
* @return string $string tag with corresponding image
*/
function getStudioIcon($iconFileName='', $altFileName='', $width='48', $height='48', $align='baseline', $alt='' )
{
global $app_strings, $theme;
$iconName = _getIcon($iconFileName);
if(empty($iconName)){
$iconName = _getIcon($altFileName);
if (empty($iconName))
{
return $app_strings['LBL_NO_IMAGE'];
}
}
return SugarThemeRegistry::current()->getImage($iconName, "align=\"$align\" border=\"0\"", $width, $height);
}
/**
* Function to grab the correct icon image for Dashlets Dialog
* @param string $filename Location of the icon file
* @param string $module Name of the module to fall back onto if file does not exist
* @param string $width Width of image
* @param string $height Height of image
* @param string $align Alignment of image
* @param string $alt Alt tag of image
* @return string $string tag with corresponding image
*/
function get_dashlets_dialog_icon($module='', $width='32', $height='32', $align='absmiddle',$alt=''){
global $app_strings, $theme;
$iconName = _getIcon($module . "_32");
if (empty($iconName))
{
$iconName = _getIcon($module);
}
if(empty($iconName)){
return $app_strings['LBL_NO_IMAGE'];
}
return SugarThemeRegistry::current()->getImage($iconName, "align=\"$align\" border=\"0\"", $width, $height);
}
// works nicely to change UTF8 strings that are html entities - good for PDF conversions
function html_entity_decode_utf8($string)
{
static $trans_tbl;
// replace numeric entities
//php will have issues with numbers with leading zeros, so do not include them in what we send to code2utf.
$string = preg_replace('~*([0-9a-f]+);~ei', 'code2utf(hexdec("\\1"))', $string);
$string = preg_replace('~*([0-9]+);~e', 'code2utf(\\1)', $string);
// replace literal entities
if (!isset($trans_tbl))
{
$trans_tbl = array();
foreach (get_html_translation_table(HTML_ENTITIES) as $val=>$key)
$trans_tbl[$key] = utf8_encode($val);
}
return strtr($string, $trans_tbl);
}
// Returns the utf string corresponding to the unicode value
function code2utf($num)
{
if ($num < 128) return chr($num);
if ($num < 2048) return chr(($num >> 6) + 192) . chr(($num & 63) + 128);
if ($num < 65536) return chr(($num >> 12) + 224) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
if ($num < 2097152) return chr(($num >> 18) + 240) . chr((($num >> 12) & 63) + 128) . chr((($num >> 6) & 63) + 128) . chr(($num & 63) + 128);
return '';
}
function str_split_php4($string, $length = 1) {
$string_length = strlen($string);
$return = array();
$cursor = 0;
if ($length > $string_length) {
// use the string_length as the string is shorter than the length
$length = $string_length;
}
for ($cursor = 0; $cursor < $string_length; $cursor = $cursor + $length) {
$return[] = substr($string, $cursor, $length);
}
return $return;
}
if (version_compare(phpversion(), '5.0.0', '<')) {
function str_split($string, $length = 1) {
return str_split_php4($string, $length);
}
}
/*
* @deprecated use DBManagerFactory::isFreeTDS
*/
function is_freetds()
{
return DBManagerFactory::isFreeTDS();
}
/**
* Chart dashlet helper function that returns the correct CSS file, dependent on the current theme.
*
* @todo this won't work completely right until we impliment css compression and combination
* for now, we'll just include the last css file found.
*
* @return chart.css file to use
*/
function chartStyle()
{
return SugarThemeRegistry::current()->getCSSURL('chart.css');
}
/**
* Chart dashlet helper functions that returns the correct XML color file for charts,
* dependent on the current theme.
*
* @return sugarColors.xml to use
*/
function chartColors()
{
if (SugarThemeRegistry::current()->getCSSURL('sugarColors.xml')=='')
return SugarThemeRegistry::current()->getImageURL('sugarColors.xml');
return SugarThemeRegistry::current()->getCSSURL('sugarColors.xml');
}
/* End Chart Dashlet helper functions */
/**
* This function is designed to set up the php enviroment
* for AJAX requests.
*/
function ajaxInit() {
ini_set('display_errors', 'false');
}
/**
* Returns an absolute path from the given path, determining if it is relative or absolute
*
* @param string $path
* @return string
*/
function getAbsolutePath(
$path,
$currentServer = false
)
{
$path = trim($path);
// try to match absolute paths like \\server\share, /directory or c:\
if ( ( substr($path,0,2) == '\\\\' )
|| ( $path[0] == '/' )
|| preg_match('/^[A-z]:/i',$path)
|| $currentServer )
return $path;
return getcwd().'/'.$path;
}
/**
* Returns the bean object of the given module
*
* @deprecated use SugarModule::loadBean() instead
* @param string $module
* @return object
*/
function loadBean(
$module
)
{
return SugarModule::get($module)->loadBean();
}
/**
* Returns true if the application is being accessed on a touch screen interface ( like an iPad )
*/
function isTouchScreen()
{
$ua = empty($_SERVER['HTTP_USER_AGENT']) ? "undefined" : strtolower($_SERVER['HTTP_USER_AGENT']);
// first check if we have forced use of the touch enhanced interface
if ( isset($_COOKIE['touchscreen']) && $_COOKIE['touchscreen'] == '1' ) {
return true;
}
// next check if we should use the touch interface with our device
if ( strpos($ua, 'ipad') !== false ) {
return true;
}
return false;
}
/**
* Returns the shortcut keys to access the shortcut links. Shortcut
* keys vary depending on browser versions and operating systems.
* @return String value of the shortcut keys
*/
function get_alt_hot_key() {
$ua = '';
if ( isset($_SERVER['HTTP_USER_AGENT']) )
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
$isMac = strpos($ua, 'mac') !== false;
$isLinux = strpos($ua, 'linux') !== false;
if(!$isMac && !$isLinux && strpos($ua, 'mozilla') !== false) {
if(preg_match('/firefox\/(\d)?\./', $ua, $matches)) {
return $matches[1] < 2 ? 'Alt+' : 'Alt+Shift+';
}
}
return $isMac ? 'Ctrl+' : 'Alt+';
}
function can_start_session(){
if(!empty($_GET['PHPSESSID'])) {
return true;
}
$session_id = session_id();
return empty($session_id) ? true : false;
}
function load_link_class($properties){
$class = 'Link2';
if(!empty($properties['link_class']) && !empty($properties['link_file'])){
require_once($properties['link_file']);
$class = $properties['link_class'];
}
return $class;
}
function inDeveloperMode()
{
return isset($GLOBALS['sugar_config']['developerMode']) && $GLOBALS['sugar_config']['developerMode'];
}
/**
* Filter the protocol list for inbound email accounts.
*
* @param array $protocol
*/
function filterInboundEmailPopSelection($protocol)
{
if ( !isset($GLOBALS['sugar_config']['allow_pop_inbound']) || ! $GLOBALS['sugar_config']['allow_pop_inbound'] )
{
if( isset($protocol['pop3']) )
unset($protocol['pop3']);
}
else
$protocol['pop3'] = 'POP3';
return $protocol;
}
/**
* The function is used because currently we are not supporting mbstring.func_overload
* For some user using mssql without FreeTDS, they may store multibyte charaters in varchar using latin_general collation. It cannot store so many mutilbyte characters, so we need to use strlen.
* The varchar in MySQL, Orcale, and nvarchar in FreeTDS, we can store $length mutilbyte charaters in it. we need mb_substr to keep more info.
* @returns the substred strings.
*/
function sugar_substr($string, $length, $charset='UTF-8')
{
if(mb_strlen($string,$charset) > $length) {
$string = trim(mb_substr(trim($string),0,$length,$charset));
}
return $string;
}
/**
* The function is used because on FastCGI enviroment, the ucfirst(Chinese Characters) will produce bad charcters.
* This will work even without setting the mbstring.*encoding
*/
function sugar_ucfirst($string, $charset='UTF-8') {
return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset) . mb_substr($string, 1, mb_strlen($string), $charset);
}
/**
*
*/
function unencodeMultienum($string) {
if (is_array($string))
{
return $string;
}
if (substr($string, 0 ,1) == "^" && substr($string, -1) == "^") {
$string = substr(substr($string, 1), 0, strlen($string) -2);
}
return explode('^,^', $string);
}
function encodeMultienumValue($arr) {
if (!is_array($arr))
return $arr;
if (empty($arr))
return "";
$string = "^" . implode('^,^', $arr) . "^";
return $string;
}
/**
* create_export_query is used for export and massupdate
* We haven't handle the these fields: $field['type'] == 'relate' && isset($field['link']
* This function will correct the where clause and output necessary join condition for them
* @param $module: the module name
* @param $searchFields: searchFields which is got after $searchForm->populateFromArray()
* @param $where: where clauses
* @return $ret_array['where']: corrected where clause
* @return $ret_array['join']: extra join condition
*/
function create_export_query_relate_link_patch($module, $searchFields, $where){
if(file_exists('modules/'.$module.'/SearchForm.html')){
$ret_array['where'] = $where;
return $ret_array;
}
$seed = loadBean($module);
foreach($seed->field_defs as $name=>$field)
{
if( $field['type'] == 'relate' && isset($field['link']) && !empty($searchFields[$name]['value']) ){
$seed->load_relationship($field['link']);
$params = array();
if(empty($join_type))
{
$params['join_type'] = ' LEFT JOIN ';
}
else
{
$params['join_type'] = $join_type;
}
if(isset($data['join_name']))
{
$params['join_table_alias'] = $field['join_name'];
}
else
{
$params['join_table_alias'] = 'join_'.$field['name'];
}
if(isset($data['join_link_name']))
{
$params['join_table_link_alias'] = $field['join_link_name'];
}
else
{
$params['join_table_link_alias'] = 'join_link_'.$field['name'];
}
$join = $seed->$field['link']->getJoin($params, true);
$join_table_alias = 'join_'.$field['name'];
if(isset($field['db_concat_fields'])){
$db_field = db_concat($join_table_alias, $field['db_concat_fields']);
$where = preg_replace('/'.$field['name'].'/', $db_field, $where);
}else{
$where = preg_replace('/(^|[\s(])' . $field['name'] . '/', '${1}' . $join_table_alias . '.'.$field['rname'], $where);
}
}
}
$ret_array = array('where'=>$where, 'join'=>$join['join']);
return $ret_array;
}
/**
* We need to clear all the js cache files, including the js language files in serval places in MB. So I extract them into a util function here.
* @Depends on QuickRepairAndRebuild.php
* @Relate bug 30642 ,23177
*/
function clearAllJsAndJsLangFilesWithoutOutput(){
global $current_language , $mod_strings;
$MBmodStrings = $mod_strings;
$mod_strings = return_module_language ( $current_language, 'Administration' ) ;
include_once ('modules/Administration/QuickRepairAndRebuild.php') ;
$repair = new RepairAndClear();
$repair->module_list = array();
$repair->show_output = false;
$repair->clearJsLangFiles();
$repair->clearJsFiles();
$mod_strings = $MBmodStrings;
}
/**
* This function will allow you to get a variable value from query string
*/
function getVariableFromQueryString($variable, $string){
$matches = array();
$number = preg_match("/{$variable}=([a-zA-Z0-9_-]+)[&]?/", $string, $matches);
if($number){
return $matches[1];
}
else{
return false;
}
}
/**
* should_hide_iframes
* This is a helper method to determine whether or not to show iframes (My Sites) related
* information in the application.
*
* @return boolean flag indicating whether or not iframes module should be hidden
*/
function should_hide_iframes() {
//Remove the MySites module
if(file_exists('modules/iFrames/iFrame.php')) {
if(!class_exists("iFrame")) {
require_once('modules/iFrames/iFrame.php');
}
return false;
}
return true;
}
/**
* Given a version such as 5.5.0RC1 return RC. If we have a version such as: 5.5 then return GA
*
* @param string $version
* @return string RC, BETA, GA
*/
function getVersionStatus($version){
if(preg_match('/^[\d\.]+?([a-zA-Z]+?)[\d]*?$/si', $version, $matches)) {
return strtoupper($matches[1]);
}else{
return 'GA';
}
}
/**
* Return the numeric portion of a version. For example if passed 5.5.0RC1 then return 5.5. If given
* 5.5.1RC1 then return 5.5.1
*
* @param string $version
* @return version
*/
function getMajorMinorVersion($version){
if(preg_match('/^([\d\.]+).*$/si', $version, $matches2)){
$version = $matches2[1];
$arr = explode('.', $version);
if(count($arr) > 2){
if($arr[2] == '0'){
$version = substr($version, 0, 3);
}
}
}
return $version;
}
/**
* Return string composed of seconds & microseconds of current time, without dots
* @return string
*/
function sugar_microtime()
{
$now = explode(' ', microtime());
$unique_id = $now[1].str_replace('.', '', $now[0]);
return $unique_id;
}
/**
* Extract urls from a piece of text
* @param $string
* @return array of urls found in $string
*/
function getUrls($string)
{
$lines = explode(" ", trim($string));
$urls = array();
foreach($lines as $line){
$regex = '/http?\:\/\/[^\" ]+/i';
preg_match_all($regex, $line, $matches);
foreach($matches[0] as $match){
$urls[] = $match;
}
}
return $urls;
}
/**
* Sanitize image file from hostile content
* @param string $path Image file
* @param bool $jpeg Accept only JPEGs?
*/
function verify_image_file($path, $jpeg = false)
{
if(function_exists('imagepng') && function_exists('imagejpeg') && function_exists('imagecreatefromstring')) {
$img = imagecreatefromstring(file_get_contents($path));
if(!$img) {
return false;
}
$img_size = getimagesize($path);
$filetype = $img_size['mime'];
//if filetype is jpeg or if we are only allowing jpegs, create jpg image
if($filetype == "image/jpeg" || $jpeg) {
ob_start();
imagejpeg($img);
$image = ob_get_clean();
// not writing directly because imagejpeg does not work with streams
if(file_put_contents($path, $image)) {
return true;
}
} elseif ($filetype == "image/png") { // else if the filetype is png, create png
imagealphablending($img, true);
imagesavealpha($img, true);
ob_start();
imagepng($img);
$image = ob_get_clean();
if(file_put_contents($path, $image)) {
return true;
}
} else {
return false;
}
} else {
// check image manually
$fp = fopen($path, "r");
if(!$fp) return false;
$data = fread($fp, 4096);
fclose($fp);
if(preg_match("/<(html|!doctype|script|body|head|plaintext|table|img |pre(>| )|frameset|iframe|object|link|base|style|font|applet|meta|center|form|isindex)/i",
$data, $m)) {
$GLOBALS['log']->info("Found {$m[0]} in $path, not allowing upload");
return false;
}
return true;
}
return false;
}
/**
* Verify uploaded image
* Verifies that image has proper extension, MIME type and doesn't contain hostile contant
* @param string $path Image path
* @param bool $jpeg_only Accept only JPEGs?
*/
function verify_uploaded_image($path, $jpeg_only = false)
{
$supportedExtensions = array('jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg');
if(!$jpeg_only) {
$supportedExtensions['png'] = 'image/png';
}
if(!file_exists($path) || !is_file($path)) {
return false;
}
$img_size = getimagesize($path);
$filetype = $img_size['mime'];
$ext = end(explode(".", $path));
if(substr_count('..', $path) > 0 || ($ext !== $path && !in_array(strtolower($ext), array_keys($supportedExtensions))) ||
!in_array($filetype, array_values($supportedExtensions))) {
return false;
}
return verify_image_file($path, $jpeg_only);
}
function cmp_beans($a, $b)
{
global $sugar_web_service_order_by;
//If the order_by field is not valid, return 0;
if (empty($sugar_web_service_order_by) || !isset($a->$sugar_web_service_order_by) || !isset($b->$sugar_web_service_order_by)){
return 0;
}
if (is_object($a->$sugar_web_service_order_by) || is_object($b->$sugar_web_service_order_by)
|| is_array($a->$sugar_web_service_order_by) || is_array($b->$sugar_web_service_order_by))
{
return 0;
}
if ($a->$sugar_web_service_order_by < $b->$sugar_web_service_order_by)
{
return -1;
} else {
return 1;
}
}
function order_beans($beans, $field_name)
{
//Since php 5.2 doesn't include closures, we must use a global to pass the order field to cmp_beans.
global $sugar_web_service_order_by;
$sugar_web_service_order_by = $field_name;
usort($beans, "cmp_beans");
return $beans;
}
/**
* Return search like string
* This function takes a user input string and returns a string that contains wild card(s) that can be used in db query.
* @param string $str string to be searched
* @param string $like_char Database like character, usually '%'
* @return string Returns a string to be searched in db query
*/
function sql_like_string($str, $like_char, $wildcard = '%', $appendWildcard = true) {
// override default wildcard character
if (isset($GLOBALS['sugar_config']['search_wildcard_char']) &&
strlen($GLOBALS['sugar_config']['search_wildcard_char']) == 1) {
$wildcard = $GLOBALS['sugar_config']['search_wildcard_char'];
}
// add wildcard at the beginning of the search string
if (isset($GLOBALS['sugar_config']['search_wildcard_infront']) &&
$GLOBALS['sugar_config']['search_wildcard_infront'] == true) {
if (substr($str,0,1) <> $wildcard)
$str = $wildcard.$str;
}
// add wildcard at the end of search string (default)
if ($appendWildcard) {
if(substr($str,-1) <> $wildcard) {
$str .= $wildcard;
}
}
return str_replace($wildcard, $like_char, $str);
}
//check to see if custom utils exists
if(file_exists('custom/include/custom_utils.php')){
include_once('custom/include/custom_utils.php');
}
//check to see if custom utils exists in Extension framework
if(file_exists('custom/application/Ext/Utils/custom_utils.ext.php')) {
include_once('custom/application/Ext/Utils/custom_utils.ext.php');
}
/**
* @param $input - the input string to sanitize
* @param int $quotes - use quotes
* @param string $charset - the default charset
* @param bool $remove - strip tags or not
* @return string - the sanitized string
*/
function sanitize($input, $quotes = ENT_QUOTES, $charset = 'UTF-8', $remove = false)
{
return htmlentities($input, $quotes, $charset);
}
/**
* @return string - the full text search engine name
*/
function getFTSEngineType()
{
if (isset($GLOBALS['sugar_config']['full_text_engine']) && is_array($GLOBALS['sugar_config']['full_text_engine'])) {
foreach ($GLOBALS['sugar_config']['full_text_engine'] as $name => $defs) {
return $name;
}
}
return '';
}
/**
* @param string $optionName - name of the option to be retrieved from app_list_strings
* @return array - the array to be used in option element
*/
function getFTSBoostOptions($optionName)
{
if (isset($GLOBALS['app_list_strings'][$optionName])) {
return $GLOBALS['app_list_strings'][$optionName];
}
else {
return array();
}
}
/**
* utf8_recursive_encode
*
* This function walks through an Array and recursively calls utf8_encode on the
* values of each of the elements.
*
* @param $data Array of data to encode
* @return utf8 encoded Array data
*/
function utf8_recursive_encode($data)
{
$result = array();
foreach($data as $key=>$val) {
if(is_array($val)) {
$result[$key] = utf8_recursive_encode($val);
} else {
$result[$key] = utf8_encode($val);
}
}
return $result;
}
/**
* get_language_header
*
* This is a utility function for 508 Compliance. It returns the lang=[Current Language] text string used
* inside the tag. If no current language is specified, it defaults to lang='en'.
*
* @return String The lang=[Current Language] markup to insert into the tag
*/
function get_language_header()
{
return isset($GLOBALS['current_language']) ? "lang='{$GLOBALS['current_language']}'" : "lang='en'";
}
/**
* get_custom_file_if_exists
*
* This function handles the repetitive code we have where we first check if a file exists in the
* custom directory to determine whether we should load it, require it, include it, etc. This function returns the
* path of the custom file if it exists. It basically checks if custom/{$file} exists and returns this path if so;
* otherwise it return $file
*
* @param $file String of filename to check
* @return $file String of filename including custom directory if found
*/
function get_custom_file_if_exists($file)
{
return file_exists("custom/{$file}") ? "custom/{$file}" : $file;
}
|