upgrade-functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. trait UpgradeFunctions
  3. {
  4. public function upgradeCheck()
  5. {
  6. if ($this->hasDB()) {
  7. $tempLock = $this->config['dbLocation'] . 'DBLOCK.txt';
  8. $updateComplete = $this->config['dbLocation'] . 'completed.txt';
  9. $cleanup = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR;
  10. if (file_exists($updateComplete)) {
  11. @unlink($updateComplete);
  12. @$this->rrmdir($cleanup);
  13. }
  14. if (file_exists($tempLock)) {
  15. die($this->showHTML('Upgrading', 'Please wait...'));
  16. }
  17. $updateDB = false;
  18. $updateSuccess = true;
  19. $compare = new Composer\Semver\Comparator;
  20. $oldVer = $this->config['configVersion'];
  21. // Upgrade check start for version below
  22. $versionCheck = '2.0.0-beta-200';
  23. if ($compare->lessThan($oldVer, $versionCheck)) {
  24. $updateDB = true;
  25. $oldVer = $versionCheck;
  26. }
  27. // End Upgrade check start for version above
  28. // Upgrade check start for version below
  29. $versionCheck = '2.0.0-beta-500';
  30. if ($compare->lessThan($oldVer, $versionCheck)) {
  31. $updateDB = true;
  32. $oldVer = $versionCheck;
  33. }
  34. // End Upgrade check start for version above
  35. // Upgrade check start for version below
  36. $versionCheck = '2.0.0-beta-800';
  37. if ($compare->lessThan($oldVer, $versionCheck)) {
  38. $updateDB = true;
  39. $oldVer = $versionCheck;
  40. }
  41. // End Upgrade check start for version above
  42. // Upgrade check start for version below
  43. $versionCheck = '2.1.0';
  44. if ($compare->lessThan($oldVer, $versionCheck)) {
  45. $updateDB = false;
  46. $oldVer = $versionCheck;
  47. $this->upgradeToVersion($versionCheck);
  48. }
  49. // End Upgrade check start for version above
  50. // Upgrade check start for version below
  51. $versionCheck = '2.1.400';
  52. if ($compare->lessThan($oldVer, $versionCheck)) {
  53. $updateDB = false;
  54. $oldVer = $versionCheck;
  55. $this->upgradeToVersion($versionCheck);
  56. }
  57. // End Upgrade check start for version above
  58. // Upgrade check start for version below
  59. $versionCheck = '2.1.525';
  60. if ($compare->lessThan($oldVer, $versionCheck)) {
  61. $updateDB = false;
  62. $oldVer = $versionCheck;
  63. $this->upgradeToVersion($versionCheck);
  64. }
  65. // End Upgrade check start for version above
  66. // Upgrade check start for version below
  67. $versionCheck = '2.1.860';
  68. if ($compare->lessThan($oldVer, $versionCheck)) {
  69. $updateDB = false;
  70. $oldVer = $versionCheck;
  71. $this->upgradeToVersion($versionCheck);
  72. }
  73. // End Upgrade check start for version above
  74. // Upgrade check start for version below
  75. $versionCheck = '2.1.1500';
  76. if ($compare->lessThan($oldVer, $versionCheck)) {
  77. $updateDB = false;
  78. $oldVer = $versionCheck;
  79. $this->upgradeToVersion($versionCheck);
  80. }
  81. // End Upgrade check start for version above
  82. if ($updateDB == true) {
  83. //return 'Upgraded Needed - Current Version '.$oldVer.' - New Version: '.$versionCheck;
  84. // Upgrade database to latest version
  85. $updateSuccess = $this->updateDB($oldVer);
  86. }
  87. // Update config.php version if different to the installed version
  88. if ($updateSuccess && $this->version !== $this->config['configVersion']) {
  89. $this->updateConfig(array('apply_CONFIG_VERSION' => $this->version));
  90. $this->setLoggerChannel('Update');
  91. $this->logger->debug('Updated config version to ' . $this->version);
  92. }
  93. if ($updateSuccess == false) {
  94. die($this->showHTML('Database update failed', 'Please manually check logs and fix - Then reload this page'));
  95. }
  96. return true;
  97. }
  98. }
  99. public function addColumnToDatabase($table = '', $columnName = '', $definition = 'TEXT')
  100. {
  101. if ($table == '' || $columnName == '' || $definition == '') {
  102. return false;
  103. }
  104. if ($this->hasDB()) {
  105. $tableInfo = [
  106. array(
  107. 'function' => 'fetchSingle',
  108. 'query' => array(
  109. 'SELECT COUNT(*) AS has_column FROM pragma_table_info(?) WHERE name=?',
  110. $table,
  111. $columnName
  112. )
  113. )
  114. ];
  115. $query = $this->processQueries($tableInfo);
  116. if (!$query) {
  117. $columnAlter = [
  118. array(
  119. 'function' => 'query',
  120. 'query' => ['ALTER TABLE ? ADD ? ' . $definition,
  121. $table,
  122. $columnName,
  123. ]
  124. )
  125. ];
  126. $AlterQuery = $this->processQueries($columnAlter);
  127. if ($AlterQuery) {
  128. $query = $this->processQueries($tableInfo);
  129. if ($query) {
  130. return true;
  131. }
  132. }
  133. } else {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. public function updateDB($oldVerNum = false)
  140. {
  141. $tempLock = $this->config['dbLocation'] . 'DBLOCK.txt';
  142. if (!file_exists($tempLock)) {
  143. touch($tempLock);
  144. $migrationDB = 'tempMigration.db';
  145. $pathDigest = pathinfo($this->config['dbLocation'] . $this->config['dbName']);
  146. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  147. unlink($this->config['dbLocation'] . $migrationDB);
  148. }
  149. // Create Temp DB First
  150. $this->connectOtherDB();
  151. $backupDB = $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db';
  152. copy($this->config['dbLocation'] . $this->config['dbName'], $backupDB);
  153. $success = $this->createDB($this->config['dbLocation'], true);
  154. if ($success) {
  155. $response = [
  156. array(
  157. 'function' => 'fetchAll',
  158. 'query' => array(
  159. 'SELECT name FROM sqlite_master WHERE type="table"'
  160. )
  161. ),
  162. ];
  163. $tables = $this->processQueries($response);
  164. foreach ($tables as $table) {
  165. $response = [
  166. array(
  167. 'function' => 'fetchAll',
  168. 'query' => array(
  169. 'SELECT * FROM ' . $table['name']
  170. )
  171. ),
  172. ];
  173. $data = $this->processQueries($response);
  174. $this->writeLog('success', 'Update Function - Grabbed Table data for Table: ' . $table['name'], 'Database');
  175. foreach ($data as $row) {
  176. $response = [
  177. array(
  178. 'function' => 'query',
  179. 'query' => array(
  180. 'INSERT into ' . $table['name'],
  181. $row
  182. )
  183. ),
  184. ];
  185. $this->processQueries($response, true);
  186. }
  187. $this->writeLog('success', 'Update Function - Wrote Table data for Table: ' . $table['name'], 'Database');
  188. }
  189. $this->writeLog('success', 'Update Function - All Table data converted - Starting Movement', 'Database');
  190. $this->db->disconnect();
  191. $this->otherDb->disconnect();
  192. // Remove Current Database
  193. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  194. $oldFileSize = filesize($this->config['dbLocation'] . $this->config['dbName']);
  195. $newFileSize = filesize($this->config['dbLocation'] . $migrationDB);
  196. if ($newFileSize > 0) {
  197. $this->writeLog('success', 'Update Function - Table Size of new DB ok..', 'Database');
  198. @unlink($this->config['dbLocation'] . $this->config['dbName']);
  199. copy($this->config['dbLocation'] . $migrationDB, $this->config['dbLocation'] . $this->config['dbName']);
  200. @unlink($this->config['dbLocation'] . $migrationDB);
  201. $this->writeLog('success', 'Update Function - Migrated Old Info to new Database', 'Database');
  202. @unlink($tempLock);
  203. return true;
  204. } else {
  205. $this->writeLog('error', 'Update Function - Filesize is zero', 'Database');
  206. }
  207. } else {
  208. $this->writeLog('error', 'Update Function - Migration DB does not exist', 'Database');
  209. }
  210. @unlink($tempLock);
  211. return false;
  212. } else {
  213. $this->writeLog('error', 'Update Function - Could not create migration DB', 'Database');
  214. }
  215. @unlink($tempLock);
  216. return false;
  217. }
  218. return false;
  219. }
  220. public function upgradeToVersion($version = '2.1.0')
  221. {
  222. switch ($version) {
  223. case '2.1.0':
  224. $this->upgradeSettingsTabURL();
  225. $this->upgradeHomepageTabURL();
  226. case '2.1.400':
  227. $this->removeOldPluginDirectoriesAndFiles();
  228. case '2.1.525':
  229. $this->removeOldCustomHTML();
  230. case '2.1.860':
  231. $this->upgradeInstalledPluginsConfigItem();
  232. case '2.1.1500':
  233. $this->upgradeDataToFolder();
  234. default:
  235. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  236. return true;
  237. }
  238. }
  239. public function removeOldCacheFolder()
  240. {
  241. $folder = $this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  242. $this->setLoggerChannel('Migration');
  243. $this->logger->info('Running Old Cache folder migration');
  244. if (file_exists($folder)) {
  245. $this->rrmdir($folder);
  246. $this->logger->info('Old Cache folder found');
  247. $this->logger->info('Removed Old Cache folder');
  248. }
  249. return true;
  250. }
  251. public function upgradeDataToFolder()
  252. {
  253. if ($this->hasDB()) {
  254. // Make main data folder
  255. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  256. // Make config folder child
  257. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);
  258. if ($rootFolderMade) {
  259. // Migrate over userTabs folder
  260. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs');
  261. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs')) {
  262. // Convert tabs over
  263. $query = [
  264. [
  265. 'function' => 'fetchAll',
  266. 'query' => [
  267. 'SELECT * FROM tabs WHERE image like "%userTabs%"'
  268. ]
  269. ],
  270. ];
  271. $tabs = $this->processQueries($query);
  272. if (count($tabs) > 0) {
  273. foreach ($tabs as $tab) {
  274. $newImage = str_replace('plugins/images/userTabs', 'data/userTabs', $tab['image']);
  275. $updateQuery = [
  276. [
  277. 'function' => 'query',
  278. 'query' => [
  279. 'UPDATE tabs SET',
  280. ['image' => $newImage],
  281. 'WHERE id = ?',
  282. $tab['id']
  283. ]
  284. ],
  285. ];
  286. $this->processQueries($updateQuery);
  287. }
  288. }
  289. $this->setLoggerChannel('Migration');
  290. $this->logger->info('The folder "userTabs" was migrated to new data folder');
  291. }
  292. // Migrate over custom cert
  293. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem')) {
  294. // Make cert folder child
  295. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR);
  296. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem')) {
  297. $this->setLoggerChannel('Migration');
  298. $this->logger->info('Moved over custom cert file');
  299. }
  300. }
  301. // Migrate over favIcon
  302. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon');
  303. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'faviconCustom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon')) {
  304. if ($this->config['favIcon'] !== '') {
  305. $this->config['favIcon'] = str_replace('plugins/images/faviconCustom', 'data/favicon', $this->config['favIcon']);
  306. $this->updateConfig(array('favIcon' => $this->config['favIcon']));
  307. }
  308. $this->setLoggerChannel('Migration');
  309. $this->logger->info('Favicon was migrated over');
  310. }
  311. // Migrate over custom pages
  312. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages');
  313. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom')) {
  314. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages')) {
  315. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom');
  316. $this->setLoggerChannel('Migration');
  317. $this->logger->info('Custom pages was migrated over');
  318. }
  319. }
  320. // Migrate over custom routes
  321. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes');
  322. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom')) {
  323. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes')) {
  324. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom');
  325. $this->setLoggerChannel('Migration');
  326. $this->logger->info('Custom routes was migrated over');
  327. }
  328. }
  329. // Migrate over cache folder
  330. $this->removeOldCacheFolder();
  331. }
  332. return true;
  333. }
  334. return false;
  335. }
  336. public function upgradeSettingsTabURL()
  337. {
  338. $response = [
  339. array(
  340. 'function' => 'query',
  341. 'query' => array(
  342. 'UPDATE tabs SET',
  343. ['url' => 'api/v2/page/settings'],
  344. 'WHERE url = ?',
  345. 'api/?v1/settings/page'
  346. )
  347. ),
  348. ];
  349. return $this->processQueries($response);
  350. }
  351. public function upgradeHomepageTabURL()
  352. {
  353. $response = [
  354. array(
  355. 'function' => 'query',
  356. 'query' => array(
  357. 'UPDATE tabs SET',
  358. ['url' => 'api/v2/page/homepage'],
  359. 'WHERE url = ?',
  360. 'api/?v1/homepage/page'
  361. )
  362. ),
  363. ];
  364. return $this->processQueries($response);
  365. }
  366. public function upgradeInstalledPluginsConfigItem()
  367. {
  368. $oldConfigItem = $this->config['installedPlugins'];
  369. if (gettype($oldConfigItem) == 'string') {
  370. if ((strpos($oldConfigItem, '|') !== false)) {
  371. $newPlugins = [];
  372. $plugins = explode('|', $oldConfigItem);
  373. foreach ($plugins as $plugin) {
  374. $info = explode(':', $plugin);
  375. $newPlugins[$info[0]] = [
  376. 'name' => $info[0],
  377. 'version' => $info[1],
  378. 'repo' => 'organizr'
  379. ];
  380. }
  381. } else {
  382. $newPlugins = [];
  383. if ($oldConfigItem !== '') {
  384. $info = explode(':', $oldConfigItem);
  385. $newPlugins[$info[0]] = [
  386. 'name' => $info[0],
  387. 'version' => $info[1],
  388. 'repo' => 'https://github.com/Organizr/Organizr-Plugins'
  389. ];
  390. }
  391. }
  392. $this->updateConfig(['installedPlugins' => $newPlugins]);
  393. } elseif (gettype($oldConfigItem) == 'array') {
  394. $this->updateConfig(['installedPlugins' => $oldConfigItem]);
  395. }
  396. return true;
  397. }
  398. public function removeOldPluginDirectoriesAndFiles()
  399. {
  400. $folders = [
  401. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  402. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  403. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  404. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  405. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  406. ];
  407. $files = [
  408. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  409. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  410. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  411. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  412. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  413. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  414. ];
  415. foreach ($files as $file) {
  416. if (file_exists($file)) {
  417. @unlink($file);
  418. }
  419. }
  420. foreach ($folders as $folder) {
  421. if (file_exists($folder)) {
  422. @$this->rrmdir($folder);
  423. }
  424. }
  425. return true;
  426. }
  427. public function checkForConfigKeyAddToArray($keys)
  428. {
  429. $updateItems = [];
  430. foreach ($keys as $new => $old) {
  431. if (isset($this->config[$old])) {
  432. if ($this->config[$old] !== '') {
  433. $updateItemsNew = [$new => $this->config[$old]];
  434. $updateItems = array_merge($updateItems, $updateItemsNew);
  435. }
  436. }
  437. }
  438. return $updateItems;
  439. }
  440. public function removeOldCustomHTML()
  441. {
  442. $backup = $this->backupOrganizr();
  443. if ($backup) {
  444. $keys = [
  445. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  446. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  447. 'customHTML01' => 'customHTMLone',
  448. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  449. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  450. 'customHTML02' => 'customHTMLtwo',
  451. ];
  452. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  453. $updateComplete = false;
  454. if (!empty($updateItems)) {
  455. $updateComplete = $this->updateConfig($updateItems);
  456. }
  457. if ($updateComplete) {
  458. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  459. if ($removeConfigItems) {
  460. return true;
  461. }
  462. }
  463. }
  464. return false;
  465. }
  466. }