Cheetah
ChWsbUpgradeUtil.php
Go to the documentation of this file.
1 <?php
2 
9 {
10  var $oDb;
11  var $sFolder;
12 
13  function __construct($oDb)
14  {
15  $this->oDb = $oDb;
16  }
17 
18  function executeCheck ($sModule = '')
19  {
20  if (!$this->sFolder)
21  return 'Upgrade path folder is not defined';
22 
23  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'check.php';
24  if (!file_exists($sFile))
25  return $sModule ? true : 'Check script was not found: ' . $sFile;
26 
27  return include ($sFile);
28  }
29 
30  function executeConclusion ($sModule = '')
31  {
32  if (!$this->sFolder)
33  return '';
34 
35  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'conclusion.html';
36  if (!file_exists($sFile))
37  return '';
38 
39  return file_get_contents ($sFile);
40  }
41 
43  {
44  if (!$this->sFolder)
45  return 'Upgrade path folder is not defined';
46  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'script.php';
47  return file_exists($sFile) ? true : false;
48  }
49 
50  function executeScript ($sModule = '')
51  {
52  if (!$this->sFolder)
53  return 'Upgrade path folder is not defined';
54 
55  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'script.php';
56  if (!file_exists($sFile))
57  return true; // if custom script was not found just skip it
58 
59  return include ($sFile);
60  }
61 
62  function isExecuteSQLAvail ($sModule = '')
63  {
64  if (!$this->sFolder)
65  return 'Upgrade path folder is not defined';
66  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'sql.sql';
67  return file_exists($sFile) ? true : false;
68  }
69 
70  function executeSQL ($sModule = '')
71  {
72  if (!$this->sFolder)
73  return 'Upgrade path folder is not defined';
74 
75  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'sql.sql';
76  if (!file_exists($sFile))
77  return true; // if sql script was not found just skip it
78 
79  $aReplace = array ();
80  if ($sModule) {
81  $aModule = $this->oDb->getRow ("SELECT * FROM `sys_modules` WHERE `uri` = '$sModule' LIMIT 1");
82  if (!$aModule)
83  return true; // it looks like module is not installed - skip it
84  $aReplace = array (
85  'from' => array ('[db_prefix]'),
86  'to' => array ($aModule['db_prefix']),
87  );
88  }
89 
90  $mixedResult = $this->oDb->executeSQL ($sFile, $aReplace);
91 
92  if (true === $mixedResult)
93  return true;
94 
95  if (!is_array($mixedResult)) // it looks like string error, return it
96  return $mixedResult;
97 
98  $s = '';
99  foreach ($mixedResult as $a)
100  $s .= "<b>{$a['query']}</b>: {$a['error']} <br />";
101  return $s;
102  }
103 
104  function executeLangsAdd ($sModule = '')
105  {
106  if (!$this->sFolder)
107  return 'Upgrade path folder is not defined';
108 
109  $aLangs = $this->readLangs ($sModule);
110  foreach ($aLangs as $sLang) {
111  $this->_executeLangAdd($sModule, $sLang);
112  }
113 
114  return true;
115  }
116 
117  function _executeLangAdd ($sModule, $sLang = 'en')
118  {
119  $sFile = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'lang_' . $sLang . '.php';
120  if (!file_exists($sFile))
121  return true; // just skip if language file is not found
122 
123  include ($sFile);
124  if (!$aLangContent && !is_array($aLangContent))
125  return true;
126 
127  $iLanguageId = (int)$this->oDb->getOne("SELECT `ID` FROM `sys_localization_languages` WHERE `Name`='$sLang' LIMIT 1");
128  if (!$iLanguageId)
129  return true; // just skip the language if it is not available
130 
131  if ($sModule) {
132  $aModule = $this->oDb->getRow ("SELECT * FROM `sys_modules` WHERE `uri` = '$sModule' LIMIT 1");
133  if (!$aModule)
134  return true; // it looks like module is not installed - skip it
135 
136  $sModuleConfigFile = CH_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/config.php';
137  require ($sModuleConfigFile);
138  $iCategoryId = $this->oDb->getOne ("SELECT `ID` FROM `sys_localization_categories` WHERE `Name`='" . $aConfig['language_category'] . "' LIMIT 1");
139  if (!$iCategoryId && $aConfig['language_category']) {
140  if ($this->oDb->query ("INSERT INTO `sys_localization_categories` SET `Name`='" . $aConfig['language_category'] . "'"))
141  $iCategoryId = $this->oDb->lastId();
142  else
143  return "Can not determine or create language category ID";
144  }
145  } else {
146  $iCategoryId = 1;
147  }
148 
149  foreach ($aLangContent as $sKey => $sValue) {
150  $aLangKey = $this->oDb->getRow("SELECT `ID`, `IDCategory` FROM `sys_localization_keys` WHERE `Key`='" . $this->oDb->escape($sKey) . "' LIMIT 1");
151  $iLangKeyId = isset($aLangKey['ID']) && (int)$aLangKey['ID'] ? (int)$aLangKey['ID'] : false;
152  if (!$iLangKeyId) {
153  if (!$this->oDb->query("INSERT INTO `sys_localization_keys`(`IDCategory`, `Key`) VALUES('$iCategoryId', '" . $this->oDb->escape($sKey) . "')"))
154  continue;
155  $iLangKeyId = $this->oDb->lastId();
156  } else {
157  $iLangKeyCat = isset($aLangKey['IDCategory']) && (int)$aLangKey['IDCategory'] ? (int)$aLangKey['IDCategory'] : 0;
158  if ($iLangKeyCat != $iCategoryId)
159  $this->oDb->query("UPDATE `sys_localization_keys` SET `IDCategory` = '$iCategoryId' WHERE `Key` = '" . $this->oDb->escape($sKey) . "' LIMIT 1");
160  }
161  $this->oDb->res("DELETE FROM `sys_localization_strings` WHERE `IDKey` = '$iLangKeyId' AND `IDLanguage` = '$iLanguageId'");
162  $this->oDb->res("INSERT INTO `sys_localization_strings`(`IDKey`, `IDLanguage`, `String`) VALUES('$iLangKeyId', '$iLanguageId', '" . $this->oDb->escape($sValue) . "')");
163  }
164 
165  return true;
166  }
167 
168  function readLangs ($sModule = '')
169  {
170  $sDir = CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '');
171 
172  if (!($h = opendir($sDir)))
173  return false;
174 
175  $aRet = array();
176  while (false !== ($sFile = readdir($h))) {
177  if ('.' == $sFile || '..' == $sFile || '.' == $sFile[0] || !is_file($sDir . '/' . $sFile) || !preg_match('/^lang_([a-z]+)\.php$/', $sFile, $m))
178  continue;
179  $sLang = $m[1];
180  if ($sLang != $this->oDb->getOne("SELECT `Name` FROM `sys_localization_languages` WHERE `Name` = '$sLang' LIMIT 1"))
181  continue;
182  $aRet[] = $sLang;
183  }
184 
185  closedir($h);
186 
187  return $aRet;
188 
189  }
190 
191  function readModules ()
192  {
193  if (!$this->sFolder)
194  return false;
195 
196  if (!($h = opendir(CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/modules/')))
197  return false;
198 
199  $aRet = array();
200  while (false !== ($sModule = readdir($h))) {
201  if ('.' == $sModule || '..' == $sModule || '.' == $sModule[0] || !is_dir(CH_UPGRADE_DIR_UPGRADES . $this->sFolder . '/modules/' . $sModule))
202  continue;
203  if ($sModule != $this->oDb->getOne("SELECT `uri` FROM `sys_modules` WHERE `uri` = '$sModule' LIMIT 1"))
204  continue;
205  $aRet[] = $sModule;
206  }
207 
208  closedir($h);
209 
210  return $aRet;
211  }
212 
213  function checkFolder ($sFolder = '')
214  {
215  if (!$sFolder)
217  $sFullPath = CH_UPGRADE_DIR_UPGRADES . $sFolder . '/';
218 
219  if (!preg_match('/^[A-Za-z0-9\.\-]+$/', $sFolder) || !file_exists($sFullPath))
220  return 'Upgrade path was not found';
221 
222  return true;
223  }
224 
225  function setFolder ($sFolder)
226  {
227  $this->sFolder = $sFolder;
228  }
229 
230  function readUpgrades ()
231  {
232  if (!($h = opendir(CH_UPGRADE_DIR_UPGRADES))) {
233  return false;
234  }
235 
236  $aRet = array();
237  while (false !== ($sFolder = readdir($h))) {
238  if ('.' == $sFolder || '..' == $sFolder || !is_dir(CH_UPGRADE_DIR_UPGRADES . $sFolder) || !$this->checkFolder($sFolder))
239  continue;
240  $aRet[] = $sFolder;
241  }
242 
243  closedir($h);
244 
245  return $aRet;
246  }
247 }
ChWsbUpgradeUtil\readUpgrades
readUpgrades()
Definition: ChWsbUpgradeUtil.php:230
ChWsbUpgradeUtil\executeSQL
executeSQL($sModule='')
Definition: ChWsbUpgradeUtil.php:70
true
if(!defined("TRUE_VAL")) define("TRUE_VAL" true
Definition: constants.inc.php:8
ChWsbUpgradeUtil\executeConclusion
executeConclusion($sModule='')
Definition: ChWsbUpgradeUtil.php:30
$aModule
$aModule
Definition: classifieds.php:21
$aLangContent
$aLangContent
Definition: en.php:8
php
ChWsbUpgradeUtil\isExecuteScriptAvail
isExecuteScriptAvail($sModule='')
Definition: ChWsbUpgradeUtil.php:42
$sModule
if(!file_exists($sRayHeaderPath)) $sModule
Definition: index.php:14
ChWsbUpgradeUtil\$sFolder
$sFolder
Definition: ChWsbUpgradeUtil.php:11
ChWsbUpgradeUtil\readModules
readModules()
Definition: ChWsbUpgradeUtil.php:191
ChWsbUpgradeUtil\readLangs
readLangs($sModule='')
Definition: ChWsbUpgradeUtil.php:168
$sFile
$sFile
Definition: index.php:20
ChWsbUpgradeUtil\checkFolder
checkFolder($sFolder='')
Definition: ChWsbUpgradeUtil.php:213
ChWsbUpgradeUtil\$oDb
$oDb
Definition: ChWsbUpgradeUtil.php:10
$aConfig
$aConfig
Definition: config.php:8
$s
$s
Definition: embed.php:13
ChWsbUpgradeUtil\isExecuteSQLAvail
isExecuteSQLAvail($sModule='')
Definition: ChWsbUpgradeUtil.php:62
ChWsbUpgradeUtil\executeLangsAdd
executeLangsAdd($sModule='')
Definition: ChWsbUpgradeUtil.php:104
ChWsbUpgradeUtil\__construct
__construct($oDb)
Definition: ChWsbUpgradeUtil.php:13
ChWsbUpgradeUtil\executeScript
executeScript($sModule='')
Definition: ChWsbUpgradeUtil.php:50
ChWsbUpgradeUtil\_executeLangAdd
_executeLangAdd($sModule, $sLang='en')
Definition: ChWsbUpgradeUtil.php:117
ChWsbUpgradeUtil
Definition: ChWsbUpgradeUtil.php:9
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
CH_UPGRADE_DIR_UPGRADES
const CH_UPGRADE_DIR_UPGRADES
Definition: index.php:12
ChWsbUpgradeUtil\executeCheck
executeCheck($sModule='')
Definition: ChWsbUpgradeUtil.php:18
ChWsbUpgradeUtil\setFolder
setFolder($sFolder)
Definition: ChWsbUpgradeUtil.php:225