BxDolUpgradeUtil.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaUpgrade UNA Upgrade Script
  7. * @{
  8. */
  9. class BxDolUpgradeUtil
  10. {
  11. protected $oDb;
  12. protected $sFolder;
  13. public function __construct($oDb)
  14. {
  15. $this->oDb = $oDb;
  16. }
  17. function executeCheck ($sModule = '')
  18. {
  19. if (!$this->sFolder)
  20. return 'Upgrade path folder is not defined';
  21. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'check.php';
  22. if (!file_exists($sFile))
  23. return $sModule ? true : 'Check script was not found: ' . $sFile;
  24. return include ($sFile);
  25. }
  26. function executeConclusion ($sModule = '')
  27. {
  28. if (!$this->sFolder)
  29. return '';
  30. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'conclusion.html';
  31. if (!file_exists($sFile))
  32. return '';
  33. return file_get_contents ($sFile);
  34. }
  35. function isExecuteScriptAvail ($sModule = '')
  36. {
  37. if (!$this->sFolder)
  38. return 'Upgrade path folder is not defined';
  39. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'script.php';
  40. return file_exists($sFile) ? true : false;
  41. }
  42. function executeScript ($sModule = '')
  43. {
  44. if (!$this->sFolder)
  45. return 'Upgrade path folder is not defined';
  46. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'script.php';
  47. if (!file_exists($sFile))
  48. return true; // if custom script was not found just skip it
  49. return include ($sFile);
  50. }
  51. function isExecuteSQLAvail ($sModule = '')
  52. {
  53. if (!$this->sFolder)
  54. return 'Upgrade path folder is not defined';
  55. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'sql.sql';
  56. return file_exists($sFile) ? true : false;
  57. }
  58. function executeSQL ($sModule = '')
  59. {
  60. if (!$this->sFolder)
  61. return 'Upgrade path folder is not defined';
  62. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'sql.sql';
  63. if (!file_exists($sFile))
  64. return true; // if sql script was not found just skip it
  65. $aReplace = array ();
  66. if ($sModule) {
  67. $aModule = $this->oDb->getRow ("SELECT * FROM `sys_modules` WHERE `uri` = '$sModule' LIMIT 1");
  68. if (!$aModule)
  69. return true; // it looks like module is not installed - skip it
  70. $aReplace = array (
  71. 'from' => array ('{db_prefix}', '{db_name}'),
  72. 'to' => array ($aModule['db_prefix'], defined('BX_DATABASE_NAME') ? BX_DATABASE_NAME : ''),
  73. );
  74. }
  75. $mixedResult = $this->oDb->executeSQL ($sFile, $aReplace);
  76. if (true === $mixedResult)
  77. return true;
  78. if (!is_array($mixedResult)) // it looks like string error, return it
  79. return $mixedResult;
  80. $s = '';
  81. foreach ($mixedResult as $a)
  82. $s .= "<b>{$a['query']}</b>: {$a['error']} <br />";
  83. return $s;
  84. }
  85. function executeLangsAdd ($sModule = '')
  86. {
  87. if (!$this->sFolder)
  88. return 'Upgrade path folder is not defined';
  89. $aLangs = $this->readLangs ($sModule);
  90. foreach ($aLangs as $sLang) {
  91. $this->_executeLangAdd($sModule, $sLang);
  92. }
  93. return true;
  94. }
  95. function _executeLangAdd ($sModule, $sLang = 'en')
  96. {
  97. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '') . 'lang_' . $sLang . '.php';
  98. if (!file_exists($sFile))
  99. return true; // just skip if language file is not found
  100. include ($sFile);
  101. if (!$aLangContent && !is_array($aLangContent))
  102. return true;
  103. $iLanguageId = (int)$this->oDb->getOne("SELECT `ID` FROM `sys_localization_languages` WHERE `Name`='$sLang' LIMIT 1");
  104. if (!$iLanguageId)
  105. return true; // just skip the language if it is not available
  106. if ($sModule) {
  107. $aModule = $this->oDb->getRow ("SELECT * FROM `sys_modules` WHERE `uri` = '$sModule' LIMIT 1");
  108. if (!$aModule)
  109. return true; // it looks like module is not installed - skip it
  110. $sModuleConfigFile = BX_DIRECTORY_PATH_MODULES . $aModule['path'] . 'install/config.php';
  111. require ($sModuleConfigFile);
  112. $iCategoryId = $this->oDb->getOne ("SELECT `ID` FROM `sys_localization_categories` WHERE `Name`='" . $aConfig['language_category'] . "' LIMIT 1");
  113. if (!$iCategoryId && $aConfig['language_category']) {
  114. if ($this->oDb->query ("INSERT INTO `sys_localization_categories` SET `Name`='" . $aConfig['language_category'] . "'"))
  115. $iCategoryId = $this->oDb->lastId();
  116. else
  117. return "Can not determine or create language category ID";
  118. }
  119. } else {
  120. $iCategoryId = 1;
  121. }
  122. foreach ($aLangContent as $sKey => $sValue) {
  123. $aLangKey = $this->oDb->getRow("SELECT `ID`, `IDCategory` FROM `sys_localization_keys` WHERE `Key`='" . $this->oDb->escape($sKey) . "' LIMIT 1");
  124. $iLangKeyId = isset($aLangKey['ID']) && (int)$aLangKey['ID'] ? (int)$aLangKey['ID'] : false;
  125. if (!$iLangKeyId) {
  126. if (!$this->oDb->query("INSERT INTO `sys_localization_keys`(`IDCategory`, `Key`) VALUES('$iCategoryId', '" . $this->oDb->escape($sKey) . "')"))
  127. continue;
  128. $iLangKeyId = $this->oDb->lastId();
  129. } else {
  130. $iLangKeyCat = isset($aLangKey['IDCategory']) && (int)$aLangKey['IDCategory'] ? (int)$aLangKey['IDCategory'] : 0;
  131. if ($iLangKeyCat != $iCategoryId)
  132. $this->oDb->query("UPDATE `sys_localization_keys` SET `IDCategory` = '$iCategoryId' WHERE `Key` = '" . $this->oDb->escape($sKey) . "' LIMIT 1");
  133. }
  134. $this->oDb->res("DELETE FROM `sys_localization_strings` WHERE `IDKey` = '$iLangKeyId' AND `IDLanguage` = '$iLanguageId'");
  135. $this->oDb->res("INSERT INTO `sys_localization_strings`(`IDKey`, `IDLanguage`, `String`) VALUES('$iLangKeyId', '$iLanguageId', '" . $this->oDb->escape($sValue) . "')");
  136. }
  137. return true;
  138. }
  139. function readLangs ($sModule = '')
  140. {
  141. $sDir = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/' . ($sModule ? 'modules/' . $sModule . '/' : '');
  142. if (!($h = opendir($sDir)))
  143. return false;
  144. $aRet = array();
  145. while (false !== ($sFile = readdir($h))) {
  146. if ('.' == $sFile || '..' == $sFile || '.' == $sFile[0] || !is_file($sDir . '/' . $sFile) || !preg_match('/^lang_([a-z]+)\.php$/', $sFile, $m))
  147. continue;
  148. $sLang = $m[1];
  149. if ($sLang != $this->oDb->getOne("SELECT `Name` FROM `sys_localization_languages` WHERE `Name` = '$sLang' LIMIT 1"))
  150. continue;
  151. $aRet[] = $sLang;
  152. }
  153. closedir($h);
  154. return $aRet;
  155. }
  156. function readModules ()
  157. {
  158. if (!$this->sFolder)
  159. return false;
  160. if (!file_exists(BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/modules/'))
  161. return false;
  162. if (!($h = opendir(BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/modules/')))
  163. return false;
  164. $aRet = array();
  165. while (false !== ($sModule = readdir($h))) {
  166. if ('.' == $sModule || '..' == $sModule || '.' == $sModule[0] || !is_dir(BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/modules/' . $sModule))
  167. continue;
  168. if ($sModule != $this->oDb->getOne("SELECT `uri` FROM `sys_modules` WHERE `uri` = '$sModule' LIMIT 1"))
  169. continue;
  170. $aRet[] = $sModule;
  171. }
  172. closedir($h);
  173. return $aRet;
  174. }
  175. function checkFolder ($sFolder = '')
  176. {
  177. if (!$sFolder)
  178. $sFolder = $this->sFolder;
  179. $sFullPath = BX_UPGRADE_DIR_UPGRADES . $sFolder . '/';
  180. if (!preg_match('/^[A-Za-z0-9\.\-]+$/', $sFolder) || !file_exists($sFullPath))
  181. return 'Upgrade path was not found';
  182. return true;
  183. }
  184. function setFolder ($sFolder)
  185. {
  186. $this->sFolder = $sFolder;
  187. }
  188. function readUpgrades ()
  189. {
  190. if (!($h = opendir(BX_UPGRADE_DIR_UPGRADES))) {
  191. return false;
  192. }
  193. $aRet = array();
  194. while (false !== ($sFolder = readdir($h))) {
  195. if ('.' == $sFolder || '..' == $sFolder || !is_dir(BX_UPGRADE_DIR_UPGRADES . $sFolder) || !$this->checkFolder($sFolder))
  196. continue;
  197. $aRet[] = $sFolder;
  198. }
  199. closedir($h);
  200. return $aRet;
  201. }
  202. function checkPermissions ()
  203. {
  204. $aFilesFailedRead = array();
  205. $aFilesFailedWrite = array();
  206. $sPathBaseUpgrade = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/files/';
  207. $aFiles = array();
  208. if (!$this->_checkPermissionGetFilesList($sPathBaseUpgrade, $aFiles) || empty($aFiles))
  209. return 'Can\'t retrieve list of files for the upgrade';
  210. foreach ($aFiles as $sFileSource) {
  211. if (!is_readable($sFileSource)) // check if source file is readable
  212. $aFilesFailedRead[] = $sFileSource;
  213. $sFileDest = BX_DIRECTORY_PATH_ROOT . bx_ltrim_str($sFileSource, $sPathBaseUpgrade);
  214. if (file_exists($sFileDest)) {
  215. if (!is_writable($sFileDest)) // check if destination file exists and is writable
  216. $aFilesFailedWrite[] = $sFileDest;
  217. } else {
  218. $sDir = pathinfo($sFileDest, PATHINFO_DIRNAME);
  219. if (file_exists($sDir) && !is_writable($sDir)) // check if directory is writable, where new desination file will be places
  220. $aFilesFailedWrite[] = $sFileDest;
  221. }
  222. }
  223. if (empty($aFilesFailedRead) && empty($aFilesFailedWrite))
  224. return true;
  225. $s = !empty($aFilesFailedRead) ? sprintf('The following files can\'t be read: %s. ', implode(', ', $aFilesFailedRead)) : '';
  226. $s .= !empty($aFilesFailedWrite) ? sprintf('The following files can\'t be written: %s.', implode(', ', $aFilesFailedWrite)) : '';
  227. return $s;
  228. }
  229. function _checkPermissionGetFilesList ($sPath, &$aFiles)
  230. {
  231. if (!file_exists($sPath) || !is_dir($sPath) || !($rSource = opendir($sPath)))
  232. return false;
  233. while (($sFile = readdir($rSource)) !== false) {
  234. if ($sFile == '.' || $sFile =='..')
  235. continue;
  236. if (is_dir($sPath . $sFile))
  237. $this->_checkPermissionGetFilesList($sPath . $sFile . '/', $aFiles);
  238. else
  239. $aFiles[] = $sPath . $sFile;
  240. }
  241. closedir($rSource);
  242. return true;
  243. }
  244. function copyFiles ()
  245. {
  246. // TODO: use standalone copying
  247. require_once(BX_DIRECTORY_PATH_CLASSES . 'BxDolFile.php');
  248. $o = BxDolFile::getInstance();
  249. $sDirSource = bx_ltrim_str(BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/files/', BX_DIRECTORY_PATH_ROOT);
  250. if (!$o->copy($sDirSource, ''))
  251. return "Files copying failed";
  252. return true;
  253. }
  254. function filesDelete ()
  255. {
  256. $sFile = BX_UPGRADE_DIR_UPGRADES . $this->sFolder . '/files_delete.php';
  257. if (!file_exists($sFile))
  258. return true; // just skip it if file isn't found
  259. include ($sFile);
  260. if (!$aFilesDelete && !is_array($aFilesDelete))
  261. return true;
  262. foreach ($aFilesDelete as $sFile) {
  263. if (is_dir(BX_DIRECTORY_PATH_ROOT . $sFile))
  264. $this->rrmdir(BX_DIRECTORY_PATH_ROOT . $sFile);
  265. else
  266. @unlink(BX_DIRECTORY_PATH_ROOT . $sFile);
  267. }
  268. return true;
  269. }
  270. function updateFilesHash ()
  271. {
  272. require_once(BX_DIRECTORY_PATH_CLASSES . 'BxDolIO.php');
  273. require_once(BX_DIRECTORY_PATH_CLASSES . 'BxDolInstallerUtils.php');
  274. require_once(BX_DIRECTORY_PATH_CLASSES . 'BxDolInstallerHasher.php');
  275. $o = new BxDolInstallerHasher();
  276. $aFiles = $o->getSystemFilesHash ();
  277. if (!$aFiles)
  278. return 'Can\'t get files list to update hash';
  279. $sQuery = $this->oDb->prepare("DELETE FROM `sys_modules_file_tracks` WHERE `module_id` = ?", BX_SYSTEM_MODULE_ID);
  280. $this->oDb->query($sQuery);
  281. foreach($aFiles as $aFile) {
  282. $sQuery = $this->oDb->prepare("INSERT IGNORE INTO `sys_modules_file_tracks`(`module_id`, `file`, `hash`) VALUES(?, ?, ?)", BX_SYSTEM_MODULE_ID, $aFile['file'], $aFile['hash']);
  283. $this->oDb->query($sQuery);
  284. }
  285. return true;
  286. }
  287. function rrmdir($directory)
  288. {
  289. if (substr($directory,-1) == "/")
  290. $directory = substr($directory,0,-1);
  291. if (!file_exists($directory) || !is_dir($directory))
  292. return false;
  293. elseif (!is_readable($directory))
  294. return false;
  295. if (!($directoryHandle = opendir($directory)))
  296. return false;
  297. while ($contents = readdir($directoryHandle)) {
  298. if ($contents != '.' && $contents != '..') {
  299. $path = $directory . "/" . $contents;
  300. if (is_dir($path))
  301. bx_rrmdir($path);
  302. else
  303. @unlink($path);
  304. }
  305. }
  306. closedir($directoryHandle);
  307. if (!@rmdir($directory))
  308. return false;
  309. return true;
  310. }
  311. }
  312. /** @} */