Cheetah
ChWsbUpdater.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbInstaller');
9 
11 {
13  {
14  parent::__construct($aConfig);
15  $this->_sModulePath = $this->_sBasePath . $aConfig['module_dir'];
16 
17  $this->_aActions = array_merge($this->_aActions, array(
18  'check_module_exists' => array(
19  'title' => _t('_adm_txt_modules_check_module_exists'),
20  ),
21  'check_module_version' => array(
22  'title' => _t('_adm_txt_modules_check_module_version'),
23  ),
24  'check_module_hash' => array(
25  'title' => _t('_adm_txt_modules_check_module_hash'),
26  ),
27  'update_files' => array(
28  'title' => _t('_adm_txt_modules_update_files'),
29  ),
30  ));
31  }
32  function update($aParams)
33  {
34  global $MySQL;
35 
36  $aResult = array(
37  'operation_title' => _t('_adm_txt_modules_operation_update', $this->_aConfig['title'], $this->_aConfig['version_from'], $this->_aConfig['version_to'])
38  );
39 
40  //--- Check for module to update ---//
41  $aModuleInfo = $MySQL->getRow("SELECT `id`, `version` FROM `sys_modules` WHERE `path`= ? AND `uri`= ? LIMIT 1", [$this->_aConfig['module_dir'], $this->_aConfig['module_uri']]);
42  if(!$aModuleInfo)
43  return array_merge($aResult, array(
44  'message' => $this->_displayResult('check_module_exists', false, '_adm_txt_modules_module_not_found'),
45  'result' => false
46  ));
47 
48  //--- Check version ---//
49  if($aModuleInfo['version'] != $this->_aConfig['version_from'])
50  return array_merge($aResult, array(
51  'message' => $this->_displayResult('check_module_version', false, '_adm_txt_modules_wrong_version'),
52  'result' => false
53  ));
54 
55  //--- Check hash ---//
56  $aFilesOrig = $MySQL->getAllWithKey("SELECT `file`, `hash` FROM `sys_modules_file_tracks` WHERE `module_id`= ?", "file", [$aModuleInfo['id']]);
57 
58  $aFiles = array();
59  $this->_hash($this->_sModulePath, $aFiles);
60  foreach($aFiles as $aFile)
61  if(!isset($aFilesOrig[$aFile['file']]) || $aFilesOrig[$aFile['file']]['hash'] != $aFile['hash'])
62  return array_merge($aResult, array(
63  'message' => $this->_displayResult('check_module_hash', false, '_adm_txt_modules_module_was_modified'),
64  'result' => false
65  ));
66 
67  //--- Perform action and check results ---//
68  $aResult = array_merge($aResult, $this->_perform('install', 'Update'));
69  if($aResult['result']) {
70  $MySQL->query("UPDATE `sys_modules` SET `version`='" . $this->_aConfig['version_to'] . "' WHERE `id`='" . $aModuleInfo['id'] . "'");
71  $MySQL->query("DELETE FROM `sys_modules_file_tracks` WHERE `module_id`='" . $aModuleInfo['id'] . "'");
72 
73  $aFiles = array();
74  $this->_hash(CH_DIRECTORY_PATH_ROOT . 'modules/' . $this->_aConfig['module_dir'], $aFiles);
75  foreach($aFiles as $aFile)
76  $MySQL->query("INSERT IGNORE INTO `sys_modules_file_tracks`(`module_id`, `file`, `hash`) VALUES('" . $aModuleInfo['id'] . "', '" . $aFile['file'] . "', '" . $aFile['hash'] . "')");
77  }
78 
79  return $aResult;
80  }
81 
82  //--- Action Methods ---//
83  function actionUpdateFiles($bInstall = true)
84  {
85  $sPath = $this->_sHomePath . 'source/';
86  if(!file_exists($sPath))
88 
89  $sFtpHost = getParam('sys_ftp_host');
90  if(empty($sFtpHost))
91  $sFtpHost = $_SERVER['HTTP_HOST'];
92 
93  $oFtp = new ChWsbFtp($sFtpHost, getParam('sys_ftp_login'), getParam('sys_ftp_password'), getParam('sys_ftp_dir'));
94  if($oFtp->connect() == false)
96 
97  return $oFtp->copy($sPath . '*', 'modules/' . $this->_aConfig['module_dir']) ? CH_WSB_INSTALLER_SUCCESS : CH_WSB_INSTALLER_FAILED;
98  }
99  function actionUpdateLanguages($bInstall = true)
100  {
101  global $MySQL;
102 
103  $aLanguages = $MySQL->getAll("SELECT `ID` AS `id`, `Name` AS `name`, `Title` AS `title` FROM `sys_localization_languages`");
104 
105  //--- Process languages' key=>value pears ---//
106  $sModuleConfig = $this->_sHomePath .'install/config.php';
107  if(!file_exists($sModuleConfig))
108  return array('code' => CH_WSB_INSTALLER_FAILED, 'content' => '_adm_txt_modules_module_config_not_found');
109 
110  include($sModuleConfig);
111  $iCategoryId = (int)$MySQL->getOne("SELECT `ID` FROM `sys_localization_categories` WHERE `Name`='" . $aConfig['language_category'] . "' LIMIT 1");
112 
113  foreach($aLanguages as $aLanguage)
114  $this->_updateLanguage($bInstall, $aLanguage, $iCategoryId);
115 
116  //--- Recompile all language files ---//
117  $aResult = array();
118  foreach($aLanguages as $aLanguage) {
119  $bResult = compileLanguage($aLanguage['id']);
120 
121  if(!$bResult)
122  $aResult[] = $aLanguage['title'];
123  }
124  return empty($aResult) ? CH_WSB_INSTALLER_SUCCESS : array('code' => CH_WSB_INSTALLER_FAILED, 'content' => $aResult);
125  }
126 
127  //--- Protected methods ---//
128  function _updateLanguage($bInstall, $aLanguage, $iCategoryId = 0)
129  {
130  global $MySQL;
131 
132  $sPath = $this->_sHomePath . 'install/langs/' . $aLanguage['name'] . '.php';
133  if(!file_exists($sPath)) return false;
134 
135  include($sPath);
136 
137  //--- Process delete ---//
138  if(isset($aLangContentDelete) && is_array($aLangContentDelete))
139  foreach($aLangContentDelete as $sKey)
140  $MySQL->query("DELETE FROM `tk`, `ts` USING `sys_localization_keys` AS `tk` LEFT JOIN `sys_localization_strings` AS `ts` ON `tk`.`ID`=`ts`.`IDKey` WHERE `tk`.`Key`='" . $sKey . "' AND `ts`.`IDLanguage`='" . $aLanguage['id'] . "'");
141 
142  //--- Process add ---//
143  if(isset($aLangContentAdd) && is_array($aLangContentAdd))
144  foreach($aLangContentAdd as $sKey => $sValue) {
145  $mixedResult = $MySQL->query("INSERT IGNORE INTO `sys_localization_keys`(`IDCategory`, `Key`) VALUES('" . $iCategoryId . "', '" . $sKey . "')");
146  if($mixedResult === false || $mixedResult <= 0)
147  continue;
148 
149  $iLangKeyId = (int)$MySQL->lastId();
150  $MySQL->query("INSERT INTO `sys_localization_strings`(`IDKey`, `IDLanguage`, `String`) VALUES('" . $iLangKeyId . "', '" . $aLanguage['id'] . "', '" . addslashes($sValue) . "')");
151  }
152 
153  //--- Process Update ---//
154  if(isset($aLangContentUpdate) && is_array($aLangContentUpdate))
155  foreach($aLangContentUpdate as $sKey => $sValue) {
156  $iLangKeyId = (int)$MySQL->getOne("SELECT `ID` FROM `sys_localization_keys` WHERE `Key`='" . $sKey . "'");
157  if($iLangKeyId == 0)
158  continue;
159 
160  $MySQL->query("UPDATE `sys_localization_strings` SET `String`='" . addslashes(clear_xss($sValue)) . "' WHERE `IDKey`='" . $iLangKeyId . "' AND `IDLanguage`='" . $aLanguage['id'] . "'");
161  }
162 
163  return true;
164  }
165 }
compileLanguage
compileLanguage($langID=0)
Definition: languages.inc.php:301
ChWsbUpdater\actionUpdateFiles
actionUpdateFiles($bInstall=true)
Definition: ChWsbUpdater.php:83
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
$aResult
$aResult
Definition: index.php:19
php
ChWsbInstaller
Definition: ChWsbInstaller.php:38
ChWsbUpdater\update
update($aParams)
Definition: ChWsbUpdater.php:32
getParam
getParam($sParamName, $bUseCache=true)
Definition: db.inc.php:130
ChWsbInstaller\_perform
_perform($sOperationName, $sOperationTitle)
Definition: ChWsbInstaller.php:284
ChWsbUpdater\actionUpdateLanguages
actionUpdateLanguages($bInstall=true)
Definition: ChWsbUpdater.php:99
global
if(!defined("GLOBAL_MODULE")) define("GLOBAL_MODULE" global
Definition: header.inc.php:25
CH_WSB_INSTALLER_FAILED
const CH_WSB_INSTALLER_FAILED
Definition: ChWsbInstaller.php:15
$bResult
$bResult
Definition: get_file.php:11
ChWsbInstaller\_hash
_hash($sPath, &$aFiles)
Definition: ChWsbInstaller.php:261
_t
_t($key, $arg0="", $arg1="", $arg2="")
Definition: languages.inc.php:509
$aConfig
$aConfig
Definition: config.php:8
ChWsbUpdater\__construct
__construct($aConfig)
Definition: ChWsbUpdater.php:12
ChWsbUpdater\_updateLanguage
_updateLanguage($bInstall, $aLanguage, $iCategoryId=0)
Definition: ChWsbUpdater.php:128
ChWsbInstaller\_displayResult
_displayResult($sAction, $bResult, $sResult='')
Definition: ChWsbInstaller.php:312
ChWsbUpdater
Definition: ChWsbUpdater.php:11
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
CH_WSB_INSTALLER_SUCCESS
const CH_WSB_INSTALLER_SUCCESS
Definition: ChWsbInstaller.php:14
ChWsbFtp
Definition: ChWsbFtp.php:9
clear_xss
clear_xss($val)
Definition: utils.inc.php:700