晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 .
Prv8 Shell
Server : Apache
System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64
User : rainic ( 1014)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/akaindir/www/crm/modules/Settings/MailConverter/models/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //home/akaindir/www/crm/modules/Settings/MailConverter/models/Record.php
<?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.
 *************************************************************************************/

class Settings_MailConverter_Record_Model extends Settings_Vtiger_Record_Model {

	/**
	 * Function to get Id of this record instance
	 * @return <Integer> Id
	 */
	public function getId() {
		return $this->get('scannerid');
	}

	/**
	 * Function to get Name of this record instance
	 * @return <String> Name
	 */
	public function getName() {
		return $this->get('scannername');
	}

	/**
	 * Function to set module
	 * @param <Settings_MailConverter_Model> $moduleModel
	 * @return <Settings_MailConverter_Record_Model>
	 */
	public function setModule($moduleModel) {
		$this->module = $moduleModel;
		return $this;
	}

	/**
	 * Function to get module of this record
	 * @return <Settings_MailConverter_Model>
	 */
	public function getModule() {
		return $this->module;
	}

	/**
	 * Function to check whether rules exist or not for this record
	 * @return <Boolean> true/false
	 */
	public function hasRules() {
		$db = PearDatabase::getInstance();
		$result = $db->pquery('SELECT 1 FROM vtiger_mailscanner_rules WHERE scannerid = ?', array($this->getId()));
		if ($db->num_rows($result)) {
			return true;
		}
		return false;
	}

	/**
	 * Function to get Default url
	 * @return <String> Url
	 */
	public function getDefaultUrl() {
		$moduleModel = $this->getModule();
		return 'index.php?module=' . $moduleModel->getName() . '&parent=' . $moduleModel->getParentName() . '&record=' . $this->getId();
	}

	public function getListUrl() {
		$moduleModel = $this->getModule();
		return 'index.php?module=' . $moduleModel->getName() . '&parent=' . $moduleModel->getParentName() . '&view=List';
	}

	/**
	 * Function to get Scan url
	 * @return <String> Url
	 */
	public function getScanUrl() {
		$url = $this->getDefaultUrl(). '&action=ScanNow';
		return 'javascript:Settings_MailConverter_List_Js.triggerScan("'.$url.'")';
	}

	/**
	 * Function to get Rules list url
	 * @return <String> Url
	 */
	public function getRulesListUrl() {
		$url = $this->getDefaultUrl(). '&view=RulesList';
		return $url;
	}

	/**
	 * Function to get Editview url
	 * @return <String> Url
	 */
	public function getEditViewUrl() {
		return $this->getDefaultUrl() . '&create=existing&view=Edit';
	}

	public function  getCreateRuleRecordUrl() {
		$moduleModel = $this->getModule();
		$url = 'index.php?module='. $moduleModel->getName() .'&parent=Settings&scannerId='.$this->getId().'&view=EditRule';
		return 'javascript:Settings_MailConverter_Index_Js.triggerRuleEdit("'.$url.'")';
	}

	/**
	 * Function to get Delete url
	 * @return <String> Url
	 *
	 */
	public function getDeleteUrl() {
		return $this->getDefaultUrl(). '&action=DeleteMailBox';
	}

	/**
	 * Function to get record links
	 * @return <Array> List of link models <Vtiger_Link_Model>
	 */
	public function getRecordLinks() {
		$qualifiedModuleName = $this->getModule()->getName(true);
		$recordLinks = array(
			array(
				'linktype' => 'LISTVIEW',
				'linklabel' => vtranslate('LBL_EDIT', $qualifiedModuleName) . ' ' . vtranslate('MAILBOX', $qualifiedModuleName),
				'linkurl' => "javascript:window.location.href = '" . $this->getEditViewUrl() . "&mode=step1'",
				'linkicon' => 'icon-pencil'
			),
			array(
				'linktype' => 'LISTVIEW',
				'linklabel' => vtranslate('LBL_SELECT_FOLDERS', $qualifiedModuleName),
				'linkurl' => "javascript:window.location.href = '" . $this->getEditViewUrl() . "&mode=step2'",
				'linkicon' => 'icon-pencil'
			),
			array(
				'linktype' => 'LISTVIEW',
				'linklabel' => vtranslate('LBL_DELETE', $qualifiedModuleName) . ' ' . vtranslate('MAILBOX', $qualifiedModuleName),
				'linkurl' => 'javascript:Settings_MailConverter_List_Js.triggerDelete("' . $this->getDeleteUrl() . '")',
				'linkicon' => 'icon-trash'
			)
		);

		$links = array();
		if ($this->hasRules()) {
			$links[] = Vtiger_Link_Model::getInstanceFromValues(array(
					'linktype' => 'LISTVIEW',
					'linklabel' => vtranslate('LBL_SCAN_NOW', $qualifiedModuleName),
					'linkurl' => $this->getScanUrl(),
					'linkicon' => ''
			));
		}

		foreach($recordLinks as $recordLink) {
			$links[] = Vtiger_Link_Model::getInstanceFromValues($recordLink);
		}

		return $links;
	}

    /**
	 * Encrypt/Decrypt input.
	 * @access private
	 */
	function __crypt($password, $encrypt=true) {
		vimport('~~include/utils/encryption.php');
		$cryptobj = new Encryption();
		if($encrypt) return $cryptobj->encrypt(trim($password));
		else return $cryptobj->decrypt(trim($password));
	}

	/**
	 * Functon to delete this record
	 */
	public function delete() {
        vimport('~~modules/Settings/MailConverter/handlers/MailScannerInfo.php');
        $scanner = new Vtiger_MailScannerInfo(trim($this->getName()));
		$scanner->delete();
	}

	/**
	 * Function to save this record
	 * @return <Boolean> true/false (Saved/Not Saved)
	 */
	public function save() {
        vimport('~~modules/Settings/MailConverter/handlers/MailScannerInfo.php');
		$scannerLatestInfo = new Vtiger_MailScannerInfo(false, false);
		$fieldsList = $this->getModule()->getFields();
		foreach ($fieldsList as $fieldName => $fieldModel) {
			$scannerLatestInfo->$fieldName = $this->get($fieldName);
		}
		$scannerId = $this->getId();
		if (!empty($scannerId)) {
			$scannerLatestInfo->scannerid = $this->getId();
		}
		//Checking Scanner Name
		$scannerName = $this->getName();
		if($scannerName && !validateAlphanumericInput($scannerName)) {
			return false;
		}

		//Checking Server
		$server = $this->get('server');
		if($server && !validateServerName($server)) {
			return false;
		}

		
		$isConnected = true;
		$scannerOldInfo = new Vtiger_MailScannerInfo($this->get('scannerOldName'));

		if(!$scannerOldInfo->compare($scannerLatestInfo)) {
            vimport('~~modules/Settings/MailConverter/handlers/MailBox.php');
			$mailBox = new Vtiger_MailBox($scannerLatestInfo);
			$isConnected = $mailBox->connect();
		}
		if($isConnected) {
			$scannerLatestInfo->connecturl = $mailBox->_imapurl;
			//$scannerLatestInfo->isvalid = $scannerOldInfo->isvalid = $isConnected;
            if(!empty($scannerId)){
                $scannerOldInfo->scannerid = $scannerId;
            }
			$mailServerChanged = $scannerOldInfo->update($scannerLatestInfo);
            if(empty($scannerId)) {
                $this->set('scannerid', $scannerOldInfo->scannerid);
            }

			$rescanFolder = false;
			if ($this->get('searchfor') === 'all') {
				$rescanFolder = true;
			}
			$scannerOldInfo->updateAllFolderRescan($rescanFolder);
		}
		return $isConnected;
	}

	/**
	 * Function to scan this record
	 * @return <Boolean> true/false (Scaned/Not)
	 */
	public function scanNow() {
		$isValid = $this->get('isvalid');
		if ($isValid) {
			vimport('~~modules/Settings/MailConverter/handlers/MailScannerInfo.php');
			vimport('~~modules/Settings/MailConverter/handlers/MailScanner.php');
			$scannerInfo = new Vtiger_MailScannerInfo($this->getName());
			/** Start the scanning. */
			$scanner = new Vtiger_MailScanner($scannerInfo);
			$status = $scanner->performScanNow();
			return $status;
		}
		return false;
	}

	/**
	 * Function to get Folders list of this record
	 * @return <Array> Folders list
	 */
	public function getFoldersList() {
        vimport('~~modules/Settings/MailConverter/handlers/MailBox.php');
		$scannerInfo = new Vtiger_MailScannerInfo($this->getName());
		return $scannerInfo->getFolderInfo();
	}

	/**
	 * Function to get Updated folders list
	 * @return <Array> Folders List
	 */
	public function getUpdatedFoldersList() {
        vimport('~~modules/Settings/MailConverter/handlers/MailBox.php');
		$scannerInfo = new Vtiger_MailScannerInfo($this->getName());
		$mailBox = new Vtiger_MailBox($scannerInfo);

		if($mailBox->connect()) {
			$folders = $mailBox->getFolders();
			$scannerInfo->updateFolderInfo($folders);
		}

		return $scannerInfo->getFolderInfo();
	}

	/**
	 * Function to Save the folders for this record
	 */
	public function saveFolders() {
		$recordId = $this->getId();
		$db = PearDatabase::getInstance();
		$foldersData = $this->get('foldersData');

		$updateQuery = "UPDATE vtiger_mailscanner_folders SET enabled = CASE folderid ";
		foreach ($foldersData as $folderId => $enabled) {
			$updateQuery .= " WHEN $folderId THEN $enabled ";
		}
		$updateQuery .= "ELSE 0 END WHERE scannerid = ?";

		$db->pquery($updateQuery, array($this->getId()));
	}

	/**
	 * Function to update sequence of several rules
	 * @param <Array> $sequencesList
	 */
	public function updateSequence($sequencesList) {
		$db = PearDatabase::getInstance();

		$updateQuery = "UPDATE vtiger_mailscanner_rules SET sequence = CASE";
		foreach ($sequencesList as $sequence => $ruleId) {
			$updateQuery .= " WHEN ruleid = $ruleId THEN $sequence ";
		}
		$updateQuery .= " END";

		$db->pquery($updateQuery, array());
	}

	//Static functions started

	/**
	 * Function to get Clean instance of this record
	 * @return <Settings_MailConverter_Record_Model>
	 */
	public static function getCleanInstance() {
		$recordModel = new self();
		return $recordModel->setModule(Settings_Vtiger_Module_Model::getInstance('Settings:MailConverter'));
	}

	/**
	 * Function to get instance of this record using by recordId
	 * @param <Integer> $recordId
	 * @return <Settings_MailConverter_Record_Model>
	 */
	public static function getInstanceById($recordId) {
		$db = PearDatabase::getInstance();
		$result = $db->pquery('SELECT * FROM vtiger_mailscanner WHERE scannerid = ?', array($recordId));
		if ($db->num_rows($result)) {
			$recordModel = self::getCleanInstance();
			$recordModel->setData($db->query_result_rowdata($result));
            return $recordModel->set('password', $recordModel->__crypt($recordModel->get('password'),false));
		}
		return false;
	}

	/**
	 * Function to get List of mail scanner records
	 * @return <Array> List of record models <Settings_MailConverter_Record_Model>
	 */
	public static function getAll() {
		$db = PearDatabase::getInstance();
		$moduleModel = Settings_Vtiger_Module_Model::getInstance('Settings:MailConverter');

		$result = $db->pquery('SELECT * FROM vtiger_mailscanner', array());
		$numOfRows = $db->num_rows($result);

		$recordModelsList = array();
		for ($i=0; $i<$numOfRows; $i++) {
			$rowData = $db->query_result_rowdata($result, $i);
			$recordModel = new self();
			$recordModelsList[$rowData['scannerid']] = $recordModel->setData($rowData)->setModule($moduleModel);
		}
		return $recordModelsList;
	}

	public static function getCount() {
		$db = PearDatabase::getInstance();
		$moduleModel = Settings_Vtiger_Module_Model::getInstance('Settings:MailConverter');

		$result = $db->pquery('SELECT 1 FROM vtiger_mailscanner', array());
		$numOfRows = $db->num_rows($result);
		return $numOfRows;
	}

	public function getDetailViewFields() {
        $detailViewIgnoredFields = array('scannername');
        $module = $this->getModule();
        $fields = $module->getFields();
        foreach($detailViewIgnoredFields as $ignoreFieldName) {
            unset($fields[$ignoreFieldName]);
        }
        return $fields;
        return array_diff($fields,$detailViewIgnoredFields);

    }

	public function isFieldEditable($fieldModel) {
		return $fieldModel->isEditable();
	}

	public function getDisplayValue($fieldName) {
        $value = $this->get($fieldName);
        if($fieldName == 'isvalid') {
            if($value == 1){
                return 'Enabled';
            }
            return 'Disabled';
        }
        else if($fieldName == 'time_zone') {
            return Settings_MailConverter_Field_Model::$timeZonePickListValues[$value];
        }
        return $value;
    }
}

haha - 2025