Skip to content
Snippets Groups Projects
Functions.php 60.1 KiB
Newer Older
Ravichandra Adiga's avatar
Ravichandra Adiga committed
<?php
/*+**********************************************************************************
 * The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
 ************************************************************************************/

/**
 * TODO need to organize into classes based on functional grouping.
 */

class Vtiger_Functions {

	const LINK_TO_ANCHOR_TEXT_SYMBOL = '#';
        static $supportedImageFormats = array('jpeg', 'png', 'jpg', 'pjpeg', 'x-png', 'gif', 'bmp', 'vnd.adobe.photoshop', 'tiff', 'svg+xml', 'x-eps', 'x-dwg', 'vnd.dwg', 'webp', 'x-ms-bmp', 'ico', 'vnd.microsoft.icon', 'x-icon');
Ravichandra Adiga's avatar
Ravichandra Adiga committed

	static function userIsAdministrator($user) {
		return (isset($user->is_admin) && $user->is_admin == 'on');
	}

    /**
     * this function returns JS date format of current user
     *
     * return string
    */
    public static function currentUserJSDateFormat()
    {
        $datePopupFormat = '';
        $currentUser = Users_Record_Model::getCurrentUserModel();

        switch ($currentUser->get('date_format')) {
            case 'dd.mm.yyyy':
                $datePopupFormat = '%d.%m.%Y';
				break;
			case 'mm.dd.yyyy':
                $datePopupFormat = '%m.%d.%Y';
				break;
			case 'yyyy.mm.dd':
                $datePopupFormat = '%Y.%m.%d';
				break;
			case 'dd/mm/yyyy':
                $datePopupFormat = '%d/%m/%Y';
				break;
			case 'mm/dd/yyyy':
                $datePopupFormat = '%m/%d/%Y';
				break;
			case 'yyyy/mm/dd':
                $datePopupFormat = '%Y/%m/%d';
                break;
            case 'dd-mm-yyyy':
                $datePopupFormat = '%d-%m-%Y';
                break;
            case 'mm-dd-yyyy':
                $datePopupFormat = '%m-%d-%Y';
                break;
            case 'yyyy-mm-dd':
                $datePopupFormat = '%Y-%m-%d';
                break;
        }

        return $datePopupFormat;
    }
Ravichandra Adiga's avatar
Ravichandra Adiga committed

	/**
	 * This function returns the date in user specified format.
	 * limitation is that mm-dd-yyyy and dd-mm-yyyy will be considered same by this API.
	 * As in the date value is on mm-dd-yyyy and user date format is dd-mm-yyyy then the mm-dd-yyyy
	 * value will be return as the API will be considered as considered as in same format.
	 * this due to the fact that this API tries to consider the where given date is in user date
	 * format. we need a better gauge for this case.
	 * @global Users $current_user
	 * @param Date $cur_date_val the date which should a changed to user date format.
	 * @return Date
	 */
	static function currentUserDisplayDate($value) {
		global $current_user;
		$dat_fmt = $current_user->date_format;
		if ($dat_fmt == '') {
			$dat_fmt = 'dd-mm-yyyy';
		}
		$date = new DateTimeField($value);
		return $date->getDisplayDate();
	}

	static function currentUserDisplayDateNew() {
		global $log, $current_user;
		$date = new DateTimeField(null);
		return $date->getDisplayDate($current_user);
	}

	// i18n
	static function getTranslatedString($str, $module='', $language='') {
		return Vtiger_Language_Handler::getTranslatedString($str, $module, $language);
	}

	// CURRENCY
	protected static $userIdCurrencyIdCache = array();

	static function userCurrencyId($userid) {
		global $adb;
		if (!isset(self::$userIdCurrencyIdCache[$userid])) {
			$result = $adb->pquery('SELECT id,currency_id FROM vtiger_users', array());
			while ($row = $adb->fetch_array($result)) {
				self::$userIdCurrencyIdCache[$row['id']] =
						$row['currency_id'];
			}
		}
		return self::$userIdCurrencyIdCache[$userid];
	}

	protected static $currencyInfoCache = array();

	protected static function getCurrencyInfo($currencyid) {
		global $adb;
		if (!isset(self::$currencyInfoCache[$currencyid])) {
			$result = $adb->pquery('SELECT * FROM vtiger_currency_info', array());
			while ($row = $adb->fetch_array($result)) {
				self::$currencyInfoCache[$row['id']] = $row;
			}
		}
		return isset(self::$currencyInfoCache[$currencyid])? self::$currencyInfoCache[$currencyid] : null;
Ravichandra Adiga's avatar
Ravichandra Adiga committed
	}

	static function getCurrencyName($currencyid, $show_symbol = true) {
		$currencyInfo = self::getCurrencyInfo($currencyid);
		if ($show_symbol) {
			return sprintf("%s : %s", Vtiger_Deprecated::getTranslatedCurrencyString($currencyInfo['currency_name']), $currencyInfo['currency_symbol']);
		}
		return $currencyInfo['currency_name'];
	}

	static function getCurrencySymbolandRate($currencyid) {
		$currencyInfo = self::getCurrencyInfo($currencyid);
		$currencyRateSymbol = array(
			'rate' => $currencyInfo ? $currencyInfo['conversion_rate'] : 0,
			'symbol'=>$currencyInfo ? $currencyInfo['currency_symbol'] : ""
Ravichandra Adiga's avatar
Ravichandra Adiga committed
		);
		return $currencyRateSymbol;
	}

	// MODULE
	protected static $moduleIdNameCache = array();
	protected static $moduleNameIdCache = array();
	protected static $moduleIdDataCache = array();

	protected static function getBasicModuleInfo($mixed) {
		$id = $name = NULL;
		if (is_numeric($mixed)) $id = $mixed;
		else $name = $mixed;
		$reload = false;
		if ($name) {
			if (!isset(self::$moduleNameIdCache[$name])) {$reload = true;}
		} else if ($id) {
			if (!isset(self::$moduleIdNameCache[$id])) {$reload = true;}
		}
		if ($reload) {
			global $adb;
			$result = $adb->pquery('SELECT tabid, name, ownedby FROM vtiger_tab', array());
			while ($row = $adb->fetch_array($result)) {
				self::$moduleIdNameCache[$row['tabid']] = $row;
				self::$moduleNameIdCache[$row['name']]  = $row;
			}
		}
		if ($id && isset(self::$moduleIdNameCache[$id])) {
			return self::$moduleIdNameCache[$id];
		}
		if ($name && isset(self::$moduleNameIdCache[$name])) {
			return self::$moduleNameIdCache[$name];
		}
		return null;
Ravichandra Adiga's avatar
Ravichandra Adiga committed
	}

	static function getModuleData($mixed) {
		$id = $name = NULL;
		if (is_numeric($mixed)) $id = $mixed;
		else $name = (string)$mixed;
		$reload = false;

		if ($name && !isset(self::$moduleNameIdCache[$name])) {$reload = true;}
		else if ($id && !isset(self::$moduleIdNameCache[$id])) {$reload = true;}
		else if ($name) {
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			if (!$id) $id = self::$moduleNameIdCache[$name]['tabid'];
			if (!isset(self::$moduleIdDataCache[$id])) { $reload = true; }
		}

		if ($reload) {
			global $adb;
			$result = $adb->pquery('SELECT * FROM vtiger_tab', array());
			while ($row = $adb->fetch_array($result)) {
				self::$moduleIdNameCache[$row['tabid']] = $row;
				self::$moduleNameIdCache[$row['name']]  = $row;
				self::$moduleIdDataCache[$row['tabid']] = $row;
			}
			if ($name && isset(self::$moduleNameIdCache[$name])) {
				$id = self::$moduleNameIdCache[$name]['tabid'];
			}
		}
		return $id ? self::$moduleIdDataCache[$id] : NULL;
	}

	static function getModuleId($name) {
		$moduleInfo = self::getBasicModuleInfo($name);
		return $moduleInfo ? $moduleInfo['tabid'] : NULL;
	}

	static function getModuleName($id) {
		$moduleInfo = self::getBasicModuleInfo($id);
		return $moduleInfo ? $moduleInfo['name'] : NULL;
	}

	static function getModuleOwner($name) {
		$moduleInfo = self::getBasicModuleInfo($name);
		return $moduleInfo ? $moduleInfo['ownedby'] : NULL;
	}

	protected static $moduleEntityCache = array();

	static function getEntityModuleInfo($mixed) {
		$name = NULL;
		if (is_numeric($mixed)) $name = self::getModuleName ($mixed);
		else $name = $mixed;

		if ($name && !isset(self::$moduleEntityCache[$name])) {
			global $adb;
			$result = $adb->pquery('SELECT fieldname,modulename,tablename,entityidfield,entityidcolumn from vtiger_entityname', array());
			while ($row = $adb->fetch_array($result)) {
				self::$moduleEntityCache[$row['modulename']] = $row;
			}
		}

		return isset(self::$moduleEntityCache[$name])?
			self::$moduleEntityCache[$name] : NULL;
	}

	static function getEntityModuleSQLColumnString($mixed) {
		$data = array();
		$info = self::getEntityModuleInfo($mixed);
		if ($info) {
			$data['tablename'] = $info['tablename'];
			$fieldnames = $info['fieldname'];
			if (strpos(',', $fieldnames) !== false) {
				$fieldnames = sprintf("concat(%s)", implode(",' ',", explode(',', $fieldnames)));
			}
			$data['fieldname'] = $fieldnames;
		}
		return $data;
	}

	static function getEntityModuleInfoFieldsFormatted($mixed) {
		$info = self::getEntityModuleInfo($mixed);
		$fieldnames = $info ? $info['fieldname'] : NULL;
		if ($fieldnames && stripos($fieldnames, ',') !== false) {
			$fieldnames = explode(',', $fieldnames);
		}
		$info['fieldname'] = $fieldnames;
		return $info;
	}

	// MODULE RECORD
	protected static $crmRecordIdMetadataCache = array();

	protected static function getCRMRecordMetadata($mixedid) {
		global $adb;

		$multimode = is_array($mixedid);

		$ids = $multimode ? $mixedid : array($mixedid);
		$missing = array();
		foreach ($ids as $id) {
			if ($id && !isset(self::$crmRecordIdMetadataCache[$id])) {
				$missing[] = $id;
			}
		}

		if ($missing) {
			$sql = sprintf("SELECT crmid, setype, label FROM vtiger_crmentity WHERE %s", implode(' OR ', array_fill(0, php7_count($missing), 'crmid=?')));
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$result = $adb->pquery($sql, $missing);
			while ($row = $adb->fetch_array($result)) {
				self::$crmRecordIdMetadataCache[$row['crmid']] = $row;
			}
		}

		$result = array();
		foreach ($ids as $id) {
			if (isset(self::$crmRecordIdMetadataCache[$id])) {
				$result[$id] = self::$crmRecordIdMetadataCache[$id];
			} else {
				$result[$id] = NULL;
			}
		}

		return $multimode?  $result : array_shift($result);
	}

	static function getCRMRecordType($id) {
		$metadata = self::getCRMRecordMetadata($id);
		return $metadata ? $metadata['setype'] : NULL;
	}

	static function getCRMRecordLabel($id, $default='') {
		$metadata = self::getCRMRecordMetadata($id);
		return $metadata ? $metadata['label'] : $default;
	}

	static function getUserRecordLabel($id, $default='') {
		$labels = self::getCRMRecordLabels('Users', $id);
		return isset($labels[$id])? $labels[$id] : $default;
	}

	static function getGroupRecordLabel($id, $default='') {
		$labels = self::getCRMRecordLabels('Groups', $id);
		return isset($labels[$id])? $labels[$id] : $default;
	}

	static function getCRMRecordLabels($module, $ids) {
		if (!is_array($ids)) $ids = array($ids);

		if ($module == 'Users' || $module == 'Groups') {
			// TODO Cache separately?
			return self::computeCRMRecordLabels($module, $ids);
		} else {
			$metadatas = self::getCRMRecordMetadata($ids);
			$result = array();
			foreach ($metadatas as $data) {
				$result[$data['crmid']] = $data['label'];
			}
			return $result;
		}
	}

	static function updateCRMRecordLabel($module, $id) {
		global $adb;
		$labelInfo = self::computeCRMRecordLabels($module, $id);
		if ($labelInfo) {
			$label = decode_html($labelInfo[$id]);
			$adb->pquery('UPDATE vtiger_crmentity SET label=? WHERE crmid=?', array($label, $id));
			self::$crmRecordIdMetadataCache[$id] = array(
				'setype' => $module,
				'crmid'  => $id,
				'label'  => $labelInfo[$id]
			);
		}
	}

	static function getOwnerRecordLabel($id) {
		$result = self::getOwnerRecordLabels($id);
		return $result ? array_shift($result) : NULL;
	}

	static function getOwnerRecordLabels($ids) {
		if (!is_array($ids)) $ids = array($ids);

		$nameList = array();
		if ($ids) {
			$nameList = self::getCRMRecordLabels('Users', $ids);
			$groups = array();
			$diffIds = array_diff($ids, array_keys($nameList));
			if ($diffIds) {
				$groups = self::getCRMRecordLabels('Groups', array_values($diffIds));
			}
			if ($groups) {
				foreach ($groups as $id => $label) {
					$nameList[$id] = $label;
				}
			}
		}

		return $nameList;
	}

	static function computeCRMRecordLabels($module, $ids) {
		global $adb;

		if (!is_array($ids)) $ids = array($ids);

		if ($module == 'Events') {
			$module = 'Calendar';
		}

		if ($module) {
			$entityDisplay = array();

			if ($ids) {

				if ($module == 'Groups') {
					$metainfo = array('tablename' => 'vtiger_groups','entityidfield' => 'groupid','fieldname' => 'groupname');
				} else if ($module == 'DocumentFolders') {
					$metainfo = array('tablename' => 'vtiger_attachmentsfolder','entityidfield' => 'folderid','fieldname' => 'foldername');
Ravichandra Adiga's avatar
Ravichandra Adiga committed
				} else {
					$metainfo = self::getEntityModuleInfo($module);
				}

				$table = $metainfo['tablename'];
				$idcolumn = $metainfo['entityidfield'];
				$columns  = explode(',', $metainfo['fieldname']);

				// NOTE: Ignore field-permission check for non-admin (to compute record label).
				$columnString = php7_count($columns) < 2? $columns[0] :
Ravichandra Adiga's avatar
Ravichandra Adiga committed
					sprintf("concat(%s)", implode(",' ',", $columns));

				$sql = sprintf('SELECT '. implode(',',$columns).', %s AS id FROM %s WHERE %s IN (%s)',
						 $idcolumn, $table, $idcolumn, generateQuestionMarks($ids));

				$result = $adb->pquery($sql, $ids);

				if($result) {
					while ($row = $adb->fetch_array($result)) {
						$labelValues = array();
						foreach($columns as $columnName) {
							$labelValues[] = $row[$columnName];
						}
						$entityDisplay[$row['id']] = implode(' ',$labelValues);
					}
				}
			}

			return $entityDisplay;
		}
	}

	protected static $groupIdNameCache = array();

	static function getGroupName($id) {
		global $adb;
yogeshwar's avatar
yogeshwar committed
		if (!isset(self::$groupIdNameCache[$id]) || !self::$groupIdNameCache[$id]) {
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$result = $adb->pquery('SELECT groupid, groupname FROM vtiger_groups');
			while ($row = $adb->fetch_array($result)) {
				self::$groupIdNameCache[$row['groupid']] = $row['groupname'];
			}
		}
		$result = array();
		if (isset(self::$groupIdNameCache[$id])) {
			$result[] = decode_html(self::$groupIdNameCache[$id]);
			$result[] = $id;
		}
		return $result;
	}

	protected static $userIdNameCache = array();

	static function getUserName($id) {
		global $adb;
		if (!self::$userIdNameCache[$id]) {
			$result = $adb->pquery('SELECT id, user_name FROM vtiger_users');
			while ($row = $adb->fetch_array($result)) {
				self::$userIdNameCache[$row['id']] = $row['user_name'];
			}
		}
		return (isset(self::$userIdNameCache[$id])) ? self::$userIdNameCache[$id] : NULL;
	}

	static function getModuleFieldInfos($mixed) {
		global $adb;

		$moduleFieldInfo = array();
		$moduleInfo = self::getBasicModuleInfo($mixed);
		$module = $moduleInfo['name'];

		if(Vtiger_Cache::get('ModuleFieldInfo',$module)){
			return Vtiger_Cache::get('ModuleFieldInfo',$module);
		}

		if ($module) {
			$result = $adb->pquery('SELECT * FROM vtiger_field WHERE tabid=?', array(self::getModuleId($module)));

			while ($row = $adb->fetch_array($result)) {
				$moduleFieldInfo[$module][$row['fieldname']] = $row;
			}
			if (isset($moduleFieldInfo[$module])) {
				Vtiger_Cache::set('ModuleFieldInfo',$module,$moduleFieldInfo[$module]);
			}
Ravichandra Adiga's avatar
Ravichandra Adiga committed
		}
		return isset($moduleFieldInfo[$module]) ? $moduleFieldInfo[$module] : NULL;
Ravichandra Adiga's avatar
Ravichandra Adiga committed
	}

	static function getModuleFieldInfoWithId($fieldid) {
		global $adb;
		$result = $adb->pquery('SELECT * FROM vtiger_field WHERE fieldid=?', array($fieldid));
		return ($adb->num_rows($result))? $adb->fetch_array($result) : NULL;
	}

	static function getModuleFieldInfo($moduleid, $mixed) {
		$field = NULL;
		if (empty($moduleid) && is_numeric($mixed)) {
			$field = self::getModuleFieldInfoWithId($mixed);
		} else {
			$fieldsInfo = self::getModuleFieldInfos($moduleid);
			if ($fieldsInfo) {
				if (is_numeric($mixed)) {
					foreach ($fieldsInfo as $name => $row) {
						if ($row['fieldid'] == $mixed) {
							$field = $row;
							break;
						}
					}
				} else {
					$field = isset($fieldsInfo[$mixed]) ? $fieldsInfo[$mixed] : NULL;
				}
			}
		}
		return $field;
	}

	static function getModuleFieldId($moduleid, $mixed, $onlyactive=true) {
		$field = self::getModuleFieldInfo($moduleid, $mixed, $onlyactive);

		if ($field) {
			if ($onlyactive && ($field['presence'] != '0' && $field['presence'] != '2')) {
				$field = NULL;
			}
		}
		return $field ? $field['fieldid'] : false;
	}


	// Utility
	static function formatDecimal($value){
		$fld_value = $value;
yogeshwar's avatar
yogeshwar committed
		if(!$value)return $value;
yogeshwar's avatar
yogeshwar committed
			if(strpos($value, '.')) {
				$fld_value = rtrim($value, '0');
			}
			$value = rtrim($fld_value, '.');
yogeshwar's avatar
yogeshwar committed
		
Ravichandra Adiga's avatar
Ravichandra Adiga committed
		return $value;
	}

	static function fromHTML($string, $encode=true) {
		if (is_string($string)) {
			if (preg_match('/(script).*(\/script)/i', $string)) {
				$string = preg_replace(array('/</', '/>/', '/"/'), array('&lt;', '&gt;', '&quot;'), $string);
			}
		}
		return $string;
	}

	static function fromHTML_FCK($string) {
		if (is_string($string)) {
			if (preg_match('/(script).*(\/script)/i', $string)) {
				$string = str_replace('script', '', $string);
			}
		}
		return $string;
	}

	static function fromHTML_Popup($string, $encode = true) {
		$popup_toHtml = array(
			'"' => '&quot;',
			"'" => '&#039;',
		);
		//if($encode && is_string($string))$string = html_entity_decode($string, ENT_QUOTES);
		if ($encode && is_string($string)) {
			$string = addslashes(str_replace(array_values($popup_toHtml), array_keys($popup_toHtml), $string));
		}
		return $string;
	}

	static function br2nl($str) {
		$str = preg_replace("/(\r\n)/", "\\r\\n", $str);
		$str = preg_replace("/'/", " ", $str);
		$str = preg_replace("/\"/", " ", $str);
		return $str;
	}

	static function suppressHTMLTags($string) {
		return preg_replace(array('/</', '/>/', '/"/'), array('&lt;', '&gt;', '&quot;'), $string);
	}

	static function getInventoryTermsAndCondition($moduleName) {
		global $adb;
		$sql = 'SELECT tandc FROM vtiger_inventory_tandc WHERE type = ?';
		$result = $adb->pquery($sql, array($moduleName));
		$tandc = $adb->query_result($result, 0, 'tandc');
		return $tandc;
	}

	/**
	 * Function to get group permissions given to config.inc.php file
	 * @return type
	 */
	static function getGroupPermissionsFromConfigFile(){
		$rootDirectory = vglobal('root_directory');
		return exec("ls -l $rootDirectory/config.inc.php | awk 'BEGIN {OFS=\":\"}{print $3,$4}'");
	}

	static function initStorageFileDirectory() {
		$filepath = 'storage/';

		$year  = date('Y');
		$month = date('F');
		$day   = date('j');
		$week  = '';
		$permissions = self::getGroupPermissionsFromConfigFile();
		if (!is_dir($filepath . $year)) {
			//create new folder
			mkdir($filepath . $year);
			$yearPath = $filepath.$year;
			exec("chown -R $permissions  $yearPath");
		}

		if (!is_dir($filepath . $year . "/" . $month)) {
			//create new folder
			$monthFilePath = "$year/$month";
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$monthPath = $filepath.$monthFilePath;
			mkdir($filepath . $monthFilePath);
			exec("chown -R $permissions  $monthPath");
		}

		if ($day > 0 && $day <= 7)
			$week = 'week1';
		elseif ($day > 7 && $day <= 14)
			$week = 'week2';
		elseif ($day > 14 && $day <= 21)
			$week = 'week3';
		elseif ($day > 21 && $day <= 28)
			$week = 'week4';
		else
			$week = 'week5';

		if (!is_dir($filepath . $year . "/" . $month . "/" . $week)) {
			//create new folder
			$weekFilePath = "$year/$month/$week";
			$weekPath = $filepath . $weekFilePath;
			mkdir($filepath . $weekFilePath );
			exec("chown -R $permissions  $weekPath");
		}

		$filepath = $filepath . $year . "/" . $month . "/" . $week . "/";

		return $filepath;
	}

    static function validateImageMetadata($data, $short = true) {
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                $ok = self::validateImageMetadata($value, $short);
                if (!$ok)
                    return false;
            }
        } else {
            if (stripos($data, $short ? "<?" : "<?php") !== false) { // suspicious dynamic content 
                return false;
            }
        }
        return true;
    }
    static function validateImage($file_details) {
        global $app_strings;
        $allowedImageFormats = Vtiger_Functions::$supportedImageFormats;
        // Determine mime-types based on file-content for generic type (Outlook add-on).
        if ($file_details['type'] == 'application/octet-stream' && function_exists('mime_content_type')) {
            $file_details['type'] = mime_content_type($file_details['tmp_name']);
        }
        $mimeTypesList = array_merge($allowedImageFormats, array('x-ms-bmp')); //bmp another format
        $file_type_details = explode("/", $file_details['type']);
        $filetype = $file_type_details['1'];
        if ($filetype) {
            $filetype = strtolower($filetype);
        }

        if (!in_array($filetype, $allowedImageFormats)) {
		if ($saveimage) {
			$fileExtensionPath = pathinfo($file_details['name'], PATHINFO_EXTENSION);
			if (!in_array(strtolower($fileExtensionPath), $allowedImageFormats)) {
		//checking the filename has dot character
		if ($saveimage) {
			$firstCharacter = $file_details['name'][0];
			if ($firstCharacter == '.') {
				$saveimage = false;
			}
		}
		
        //mime type check
            $mimeType = mime_content_type($file_details['tmp_name']);
            $mimeTypeContents = explode('/', $mimeType);
            if (!$file_details['size'] || strtolower($mimeTypeContents[0]) !== 'image' || !in_array($mimeTypeContents[1], $mimeTypesList)) {
        //metadata check
        $shortTagSupported = ini_get('short_open_tag') ? true : false;
            $tmpFileName = $file_details['tmp_name'];

            if ($file_details['type'] == 'image/jpeg' || $file_details['type'] == 'image/tiff') {
                $exifdata = @exif_read_data($file_details['tmp_name']);
                if ($exifdata && !self::validateImageMetadata($exifdata, $shortTagSupported)) {
                //131225968::remove sensitive information(like,GPS or camera information) from the image
                if ($saveimage && ($file_details['type'] == 'image/jpeg' ) && extension_loaded('gd') && function_exists('gd_info')) {
                    $img = imagecreatefromjpeg($tmpFileName);
                    imagejpeg($img, $tmpFileName);
                }
            }
        }

            $imageContents = file_get_contents($tmpFileName);
            if (stripos($imageContents, $shortTagSupported ? "<?" : "<?php") !== false) { // suspicious dynamic content.
        if (($filetype == 'svg+xml' || $mimeTypeContents[1] == 'svg+xml') && $saveimage) {
            //remove malicious html attributes with its value from the contents.
            $imageContents = purifyHtmlEventAttributes($imageContents, true);
            $filePointer = fopen("$tmpFileName", "w");
            fwrite($filePointer, $imageContents);
            fclose($filePointer);
            /*
             * File functions like  filegroup(), fileowner(), filesize(), filetype(), fileperms() and few others,caches file information, we need to clear the cache so it will not return the cache value if we perform/call same function after updating the file
            clearstatcache();
        }

        return $saveimage;
    }
    static function getMergedDescription($description, $id, $parent_type, $removeTags = false) {
Ravichandra Adiga's avatar
Ravichandra Adiga committed
		global $current_user;
		$token_data_pair = explode('$', $description);
		$emailTemplate = new EmailTemplate($parent_type, $description, $id, $current_user);
		$emailTemplate->removeTags = $removeTags;
		$description = $emailTemplate->getProcessedDescription();
		$tokenDataPair = explode('$', $description);
		$fields = Array();
		for ($i = 1; $i < php7_count($token_data_pair); $i++) {
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$module = explode('-', $tokenDataPair[$i]);
Prasad's avatar
Prasad committed
			if (count($module) < 2) {
				// if not $module-fieldname$
				continue;
			}
			if (!isset($fields[$module[0]])) {
				$fields[$module[0]] = array();
			}
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$fields[$module[0]][] = $module[1];
		}
Prasad's avatar
Prasad committed
		if (isset($fields['custom']) && is_array($fields['custom']) && php7_count($fields['custom']) > 0) {
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$description = self::getMergedDescriptionCustomVars($fields, $description,$id,$parent_type);
		}
Prasad's avatar
Prasad committed
		if(isset($fields['companydetails']) && is_array($fields['companydetails']) && php7_count($fields['companydetails']) > 0){
Ravichandra Adiga's avatar
Ravichandra Adiga committed
			$description = self::getMergedDescriptionCompanyDetails($fields,$description);
		}

		//for merging record id merge tags(eg: $helpdesk-id$) with record values
		if(is_array($fields) && !empty($fields)) {
			foreach ($fields as $moduleName => $fields) {
				if(in_array('id',$fields)) {
					if(strtolower($parent_type) === $moduleName) {
						$needle = "$$moduleName-id$";
						$description = str_replace($needle,$id,$description);
					}
				}
			}
		}
		return $description;
	}

	/**
	 * Function replaces all company merge tags will respective value.
	 * @param type $fields
	 * @param type $description
	 * @return type
	 */
	static function getMergedDescriptionCompanyDetails($fields, $description){
		$companyModuleModel = Settings_Vtiger_CompanyDetails_Model::getInstance();
		foreach($fields['companydetails'] as $columnname){
			$token_data = '$companydetails-' . $columnname . '$';
			$token_value = $companyModuleModel->get($columnname);
			if(empty($token_value)){
				$token_value = '';
			}
			$description = str_replace($token_data, $token_value, $description);
		}
		return $description;
	}

	static function getMergedDescriptionCustomVars($fields, $description, $recordId = '', $module = '') {
		global $site_URL, $PORTAL_URL;
		foreach ($fields['custom'] as $columnname) {
			$token_data = '$custom-' . $columnname . '$';
			$token_value = '';
			switch ($columnname) {
				case 'currentdate'		:	$token_value = date("F j, Y");
											break;
				case 'currenttime'		:	$token_value = date("G:i:s T");
											break;
				case 'siteurl'			:	$token_value = $site_URL;
											break;
				case 'portalurl'		:	$token_value = $PORTAL_URL;
											break;
				case 'crmdetailviewurl'	:	if($module !== 'Users') {
											$token_value = $site_URL."/index.php?module=$module&view=Detail&record=$recordId";
											} else {
											  $token_value = $token_data;
										  }
										  break;
			}
			if ($columnname !== 'viewinbrowser') {
				$description = str_replace($token_data, $token_value, $description);
			}
		}
		return $description;
	}

	static function getSingleFieldValue($tablename, $fieldname, $idname, $id) {
		global $adb;
        $fieldname = Vtiger_Util_Helper::validateStringForSql($fieldname);
        $idname = Vtiger_Util_Helper::validateStringForSql($idname);
Ravichandra Adiga's avatar
Ravichandra Adiga committed
		$fieldval = $adb->query_result($adb->pquery("select $fieldname from $tablename where $idname = ?", array($id)), 0, $fieldname);
		return $fieldval;
	}

	static function getRecurringObjValue() {
		$recurring_data = array();
		if (isset($_REQUEST['recurringtype']) && $_REQUEST['recurringtype'] != null && $_REQUEST['recurringtype'] != '--None--') {
			if (!empty($_REQUEST['date_start'])) {
				$startDate = $_REQUEST['date_start'];
			}
			if (!empty($_REQUEST['calendar_repeat_limit_date'])) {
				$endDate = $_REQUEST['calendar_repeat_limit_date'];
				$recurring_data['recurringenddate'] = $endDate;
			} elseif (isset($_REQUEST['due_date']) && $_REQUEST['due_date'] != null) {
				$endDate = $_REQUEST['due_date'];
			}
			if (!empty($_REQUEST['time_start'])) {
				$startTime = $_REQUEST['time_start'];
			}
			if (!empty($_REQUEST['time_end'])) {
				$endTime = $_REQUEST['time_end'];
			}

			$recurring_data['startdate'] = $startDate;
			$recurring_data['starttime'] = $startTime;
			$recurring_data['enddate'] = $endDate;
			$recurring_data['endtime'] = $endTime;

			$recurring_data['type'] = $_REQUEST['recurringtype'];
			if ($_REQUEST['recurringtype'] == 'Weekly') {
				if (isset($_REQUEST['sun_flag']) && $_REQUEST['sun_flag'] != null)
					$recurring_data['sun_flag'] = true;
				if (isset($_REQUEST['mon_flag']) && $_REQUEST['mon_flag'] != null)
					$recurring_data['mon_flag'] = true;
				if (isset($_REQUEST['tue_flag']) && $_REQUEST['tue_flag'] != null)
					$recurring_data['tue_flag'] = true;
				if (isset($_REQUEST['wed_flag']) && $_REQUEST['wed_flag'] != null)
					$recurring_data['wed_flag'] = true;
				if (isset($_REQUEST['thu_flag']) && $_REQUEST['thu_flag'] != null)
					$recurring_data['thu_flag'] = true;
				if (isset($_REQUEST['fri_flag']) && $_REQUEST['fri_flag'] != null)
					$recurring_data['fri_flag'] = true;
				if (isset($_REQUEST['sat_flag']) && $_REQUEST['sat_flag'] != null)
					$recurring_data['sat_flag'] = true;
			}
			elseif ($_REQUEST['recurringtype'] == 'Monthly') {
				if (isset($_REQUEST['repeatMonth']) && $_REQUEST['repeatMonth'] != null)
					$recurring_data['repeatmonth_type'] = $_REQUEST['repeatMonth'];
				if ($recurring_data['repeatmonth_type'] == 'date') {
					if (isset($_REQUEST['repeatMonth_date']) && $_REQUEST['repeatMonth_date'] != null)
						$recurring_data['repeatmonth_date'] = $_REQUEST['repeatMonth_date'];
					else
						$recurring_data['repeatmonth_date'] = 1;
				}
				elseif ($recurring_data['repeatmonth_type'] == 'day') {
					$recurring_data['repeatmonth_daytype'] = $_REQUEST['repeatMonth_daytype'];
					switch ($_REQUEST['repeatMonth_day']) {
						case 0 :
							$recurring_data['sun_flag'] = true;
							break;
						case 1 :
							$recurring_data['mon_flag'] = true;
							break;
						case 2 :
							$recurring_data['tue_flag'] = true;
							break;
						case 3 :
							$recurring_data['wed_flag'] = true;
							break;
						case 4 :
							$recurring_data['thu_flag'] = true;
							break;
						case 5 :
							$recurring_data['fri_flag'] = true;
							break;
						case 6 :
							$recurring_data['sat_flag'] = true;
							break;
					}
				}
			}
			if (isset($_REQUEST['repeat_frequency']) && $_REQUEST['repeat_frequency'] != null)
				$recurring_data['repeat_frequency'] = $_REQUEST['repeat_frequency'];

			$recurObj = RecurringType::fromUserRequest($recurring_data);
			return $recurObj;
		}
	}

	static function getTicketComments($ticketid) {
		global $adb;
		$moduleName = getSalesEntityType($ticketid);
		$commentlist = '';
		$sql = "SELECT commentcontent FROM vtiger_modcomments WHERE related_to = ?";
		$result = $adb->pquery($sql, array($ticketid));
		for ($i = 0; $i < $adb->num_rows($result); $i++) {
			$comment = $adb->query_result($result, $i, 'commentcontent');
			if ($comment != '') {
				$commentlist .= '<br><br>' . $comment;
			}
		}
		if ($commentlist != '')
			$commentlist = '<br><br>' . getTranslatedString("The comments are", $moduleName) . ' : ' . $commentlist;
		return $commentlist;
	}

	static function generateRandomPassword() {
		$salt = "abcdefghijklmnopqrstuvwxyz0123456789";
		srand((double) microtime() * 1000000);
		$i = 0;
		while ($i <= 7) {
			$num = rand() % 33;
			$tmp = substr($salt, $num, 1);
			$pass = $pass . $tmp;
			$i++;
		}
		return $pass;
	}

	static function getTagCloudView($id = "") {
		global $adb;
		if ($id == '') {
			$tag_cloud_status = 1;
		} else {
			$query = "select visible from vtiger_homestuff where userid=? and stufftype='Tag Cloud'";
			$res = $adb->pquery($query, array($id));
			$tag_cloud_status = $adb->query_result($res, 0, 'visible');
		}

		if ($tag_cloud_status == 0) {
			$tag_cloud_view = 'true';
		} else {
			$tag_cloud_view = 'false';
		}
		return $tag_cloud_view;
	}

	static function transformFieldTypeOfData($table_name, $column_name, $type_of_data) {
		$field = $table_name . ":" . $column_name;
		//Add the field details in this array if you want to change the advance filter field details

		static $new_field_details = Array(
			//Contacts Related Fields
			"vtiger_contactdetails:accountid" => "V",
			"vtiger_contactsubdetails:birthday" => "D",
			"vtiger_contactdetails:email" => "V",
			"vtiger_contactdetails:secondaryemail" => "V",
			//Potential Related Fields
			"vtiger_potential:campaignid" => "V",
			//Account Related Fields
			"vtiger_account:parentid" => "V",
			"vtiger_account:email1" => "V",
			"vtiger_account:email2" => "V",
			//Lead Related Fields
			"vtiger_leaddetails:email" => "V",
			"vtiger_leaddetails:secondaryemail" => "V",
			//Documents Related Fields
			"vtiger_senotesrel:crmid" => "V",
			//Calendar Related Fields
			"vtiger_seactivityrel:crmid" => "V",
			"vtiger_seactivityrel:contactid" => "V",
			"vtiger_recurringevents:recurringtype" => "V",
			//HelpDesk Related Fields
			"vtiger_troubletickets:parent_id" => "V",
			"vtiger_troubletickets:product_id" => "V",
			//Product Related Fields
			"vtiger_products:discontinued" => "C",
			"vtiger_products:vendor_id" => "V",
			"vtiger_products:parentid" => "V",
			//Faq Related Fields