upgrade-functions.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. <?php
  2. trait UpgradeFunctions
  3. {
  4. public function upgradeCheck()
  5. {
  6. if (!$this->checkForUpdates) {
  7. return true;
  8. }
  9. if ($this->hasDB()) {
  10. $tempLock = $this->config['dbLocation'] . 'DBLOCK.txt';
  11. $updateComplete = $this->config['dbLocation'] . 'completed.txt';
  12. $cleanup = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'upgrade' . DIRECTORY_SEPARATOR;
  13. if (file_exists($updateComplete)) {
  14. @unlink($updateComplete);
  15. @$this->rrmdir($cleanup);
  16. }
  17. if (file_exists($tempLock)) {
  18. die($this->showHTML('Upgrading', 'Please wait...'));
  19. }
  20. $updateDB = false;
  21. $updateSuccess = true;
  22. $compare = new Composer\Semver\Comparator;
  23. $oldVer = $this->config['configVersion'];
  24. // Upgrade check start for version below
  25. $versionCheck = '2.0.0-beta-200';
  26. if ($compare->lessThan($oldVer, $versionCheck)) {
  27. $updateDB = true;
  28. $oldVer = $versionCheck;
  29. }
  30. // End Upgrade check start for version above
  31. // Upgrade check start for version below
  32. $versionCheck = '2.0.0-beta-500';
  33. if ($compare->lessThan($oldVer, $versionCheck)) {
  34. $updateDB = true;
  35. $oldVer = $versionCheck;
  36. }
  37. // End Upgrade check start for version above
  38. // Upgrade check start for version below
  39. $versionCheck = '2.0.0-beta-800';
  40. if ($compare->lessThan($oldVer, $versionCheck)) {
  41. $updateDB = true;
  42. $oldVer = $versionCheck;
  43. }
  44. // End Upgrade check start for version above
  45. // Upgrade check start for version below
  46. $versionCheck = '2.1.0';
  47. if ($compare->lessThan($oldVer, $versionCheck)) {
  48. $updateDB = false;
  49. $oldVer = $versionCheck;
  50. $this->upgradeToVersion($versionCheck);
  51. }
  52. // End Upgrade check start for version above
  53. // Upgrade check start for version below
  54. $versionCheck = '2.1.400';
  55. if ($compare->lessThan($oldVer, $versionCheck)) {
  56. $updateDB = false;
  57. $oldVer = $versionCheck;
  58. $this->upgradeToVersion($versionCheck);
  59. }
  60. // End Upgrade check start for version above
  61. // Upgrade check start for version below
  62. $versionCheck = '2.1.525';
  63. if ($compare->lessThan($oldVer, $versionCheck)) {
  64. $updateDB = false;
  65. $oldVer = $versionCheck;
  66. $this->upgradeToVersion($versionCheck);
  67. }
  68. // End Upgrade check start for version above
  69. // Upgrade check start for version below
  70. $versionCheck = '2.1.860';
  71. if ($compare->lessThan($oldVer, $versionCheck)) {
  72. $updateDB = false;
  73. $oldVer = $versionCheck;
  74. $this->upgradeToVersion($versionCheck);
  75. }
  76. // End Upgrade check start for version above
  77. // Upgrade check start for version below
  78. $versionCheck = '2.1.1500';
  79. if ($compare->lessThan($oldVer, $versionCheck)) {
  80. $updateDB = false;
  81. $oldVer = $versionCheck;
  82. $this->upgradeToVersion($versionCheck);
  83. }
  84. // End Upgrade check start for version above
  85. // Upgrade check start for version below
  86. $versionCheck = '2.1.1860';
  87. if ($compare->lessThan($oldVer, $versionCheck)) {
  88. $updateDB = false;
  89. $oldVer = $versionCheck;
  90. $this->upgradeToVersion($versionCheck);
  91. }
  92. // End Upgrade check start for version above
  93. // Upgrade check start for version below
  94. $versionCheck = '2.1.2200';
  95. if ($compare->lessThan($oldVer, $versionCheck)) {
  96. $updateDB = false;
  97. $oldVer = $versionCheck;
  98. $this->upgradeToVersion($versionCheck);
  99. }
  100. // End Upgrade check start for version above
  101. // Upgrade check start for version below
  102. $versionCheck = '2.1.5000';
  103. if ($compare->lessThan($oldVer, $versionCheck)) {
  104. $updateDB = false;
  105. $oldVer = $versionCheck;
  106. $this->upgradeToVersion($versionCheck);
  107. }
  108. // End Upgrade check start for version above
  109. if ($updateDB == true) {
  110. //return 'Upgraded Needed - Current Version '.$oldVer.' - New Version: '.$versionCheck;
  111. // Upgrade database to latest version
  112. $updateSuccess = $this->updateDB($oldVer);
  113. }
  114. // Update config.php version if different to the installed version
  115. if ($updateSuccess && $this->version !== $this->config['configVersion']) {
  116. $this->updateConfig(array('apply_CONFIG_VERSION' => $this->version));
  117. $this->setLoggerChannel('Update')->notice('Updated config version to ' . $this->version);
  118. }
  119. if ($updateSuccess == false) {
  120. die($this->showHTML('Database update failed', 'Please manually check logs and fix - Then reload this page'));
  121. }
  122. return true;
  123. }
  124. }
  125. public function dropColumnFromDatabase($table = '', $columnName = '')
  126. {
  127. if ($table == '' || $columnName == '') {
  128. return false;
  129. }
  130. if ($this->hasDB()) {
  131. $columnExists = $this->checkIfColumnExists($table, $columnName);
  132. if ($columnExists) {
  133. $columnAlter = [
  134. array(
  135. 'function' => 'query',
  136. 'query' => ['ALTER TABLE %n DROP %n',
  137. (string)$table,
  138. (string)$columnName,
  139. ]
  140. )
  141. ];
  142. $AlterQuery = $this->processQueries($columnAlter);
  143. return (boolean)($AlterQuery);
  144. }
  145. }
  146. return false;
  147. }
  148. public function checkIfColumnExists($table = '', $columnName = '')
  149. {
  150. if ($table == '' || $columnName == '') {
  151. return false;
  152. }
  153. if ($this->hasDB()) {
  154. if ($this->config['driver'] === 'sqlite3') {
  155. $term = 'SELECT COUNT(*) AS has_column FROM pragma_table_info(?) WHERE name=?';
  156. } else {
  157. $term = 'SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = "' . $this->config['dbName'] . '" AND TABLE_NAME=? AND COLUMN_NAME=?';
  158. }
  159. $tableInfo = [
  160. array(
  161. 'function' => 'fetchSingle',
  162. 'query' => array(
  163. $term,
  164. (string)$table,
  165. (string)$columnName
  166. )
  167. )
  168. ];
  169. $query = $this->processQueries($tableInfo);
  170. return (boolean)($query);
  171. }
  172. }
  173. public function addColumnToDatabase($table = '', $columnName = '', $definition = 'TEXT')
  174. {
  175. if ($table == '' || $columnName == '' || $definition == '') {
  176. return false;
  177. }
  178. if ($this->hasDB()) {
  179. if ($this->config['driver'] === 'sqlite3') {
  180. $term = 'SELECT COUNT(*) AS has_column FROM pragma_table_info(?) WHERE name=?';
  181. } else {
  182. $term = 'SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = "' . $this->config['dbName'] . '" AND TABLE_NAME=? AND COLUMN_NAME=?';
  183. }
  184. $tableInfo = [
  185. array(
  186. 'function' => 'fetchSingle',
  187. 'query' => array(
  188. $term,
  189. (string)$table,
  190. (string)$columnName
  191. )
  192. )
  193. ];
  194. $query = $this->processQueries($tableInfo);
  195. if (!$query) {
  196. $columnAlter = [
  197. array(
  198. 'function' => 'query',
  199. 'query' => ['ALTER TABLE %n ADD %n ' . (string)$definition,
  200. (string)$table,
  201. (string)$columnName,
  202. ]
  203. )
  204. ];
  205. $AlterQuery = $this->processQueries($columnAlter);
  206. if ($AlterQuery) {
  207. $query = $this->processQueries($tableInfo);
  208. if ($query) {
  209. return true;
  210. }
  211. }
  212. } else {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. public function createMysqliDatabase($database, $migration = false)
  219. {
  220. $query = [
  221. array(
  222. 'function' => 'fetchAll',
  223. 'query' => array(
  224. 'DROP DATABASE IF EXISTS tempMigration'
  225. )
  226. ),
  227. array(
  228. 'function' => 'fetchAll',
  229. 'query' => array(
  230. 'CREATE DATABASE IF NOT EXISTS %n',
  231. $database
  232. )
  233. ),
  234. ];
  235. //$query = ['CREATE DB %n', $database];
  236. return $this->processQueries($query, $migration);
  237. }
  238. public function updateDB($oldVerNum = false)
  239. {
  240. $tempLock = $this->config['dbLocation'] . 'DBLOCK.txt';
  241. if (!file_exists($tempLock)) {
  242. touch($tempLock);
  243. $migrationDB = 'tempMigration.db';
  244. $pathDigest = pathinfo($this->config['dbLocation'] . $this->config['dbName']);
  245. // Delete old backup sqlite db if exists
  246. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  247. unlink($this->config['dbLocation'] . $migrationDB);
  248. }
  249. // Create Temp DB First
  250. $this->createNewDB('tempMigration', true);
  251. $this->connectOtherDB();
  252. if ($this->config['driver'] == 'sqlite3') {
  253. // Backup sqlite database
  254. $backupDB = $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db';
  255. copy($this->config['dbLocation'] . $this->config['dbName'], $backupDB);
  256. }
  257. $success = $this->createDB($this->config['dbLocation'], true);
  258. if ($success) {
  259. switch ($this->config['driver']) {
  260. case 'sqlite3':
  261. $query = 'SELECT name FROM sqlite_master WHERE type="table"';
  262. break;
  263. case 'mysqli':
  264. $query = 'SELECT Table_name as name from information_schema.tables where table_schema = "tempMigration"';
  265. break;
  266. }
  267. $response = [
  268. array(
  269. 'function' => 'fetchAll',
  270. 'query' => array(
  271. $query
  272. )
  273. ),
  274. ];
  275. $tables = $this->processQueries($response);
  276. $defaultTables = $this->getDefaultTablesFormatted();
  277. foreach ($tables as $table) {
  278. if (in_array($table['name'], $defaultTables)) {
  279. $response = [
  280. array(
  281. 'function' => 'fetchAll',
  282. 'query' => array(
  283. 'SELECT * FROM %n', $table['name']
  284. )
  285. ),
  286. ];
  287. $data = $this->processQueries($response);
  288. $this->setLoggerChannel('Migration')->info('Obtained Table data', ['table' => $table['name']]);
  289. foreach ($data as $row) {
  290. $response = [
  291. array(
  292. 'function' => 'query',
  293. 'query' => array(
  294. 'INSERT into %n', $table['name'],
  295. $row
  296. )
  297. ),
  298. ];
  299. $this->processQueries($response, true);
  300. }
  301. $this->setLoggerChannel('Migration')->info('Wrote Table data', ['table' => $table['name']]);
  302. }
  303. }
  304. if ($this->config['driver'] == 'mysqli') {
  305. $response = [
  306. array(
  307. 'function' => 'query',
  308. 'query' => array(
  309. 'DROP DATABASE IF EXISTS %n', $this->config['dbName']
  310. )
  311. ),
  312. ];
  313. $data = $this->processQueries($response);
  314. if ($data) {
  315. $create = $this->createNewDB($this->config['dbName']);
  316. if ($create) {
  317. $structure = $this->createDB($this->config['dbLocation']);
  318. if ($structure) {
  319. foreach ($tables as $table) {
  320. if (in_array($table['name'], $defaultTables)) {
  321. $response = [
  322. array(
  323. 'function' => 'fetchAll',
  324. 'query' => array(
  325. 'SELECT * FROM %n', $table['name']
  326. )
  327. ),
  328. ];
  329. $data = $this->processQueries($response, true);
  330. $this->setLoggerChannel('Migration')->info('Obtained Table data', ['table' => $table['name']]);
  331. foreach ($data as $row) {
  332. $response = [
  333. array(
  334. 'function' => 'query',
  335. 'query' => array(
  336. 'INSERT into %n', $table['name'],
  337. $row
  338. )
  339. ),
  340. ];
  341. $this->processQueries($response);
  342. }
  343. $this->setLoggerChannel('Migration')->info('Wrote Table data', ['table' => $table['name']]);
  344. }
  345. }
  346. } else {
  347. $this->setLoggerChannel('Migration')->warning('Could not recreate Database structure');
  348. }
  349. } else {
  350. $this->setLoggerChannel('Migration')->warning('Could not recreate Database');
  351. }
  352. } else {
  353. $this->setLoggerChannel('Migration')->warning('Could not drop old tempMigration Database');
  354. }
  355. $this->setLoggerChannel('Migration')->info('All Table data converted');
  356. @unlink($tempLock);
  357. return true;
  358. }
  359. //$this->db->disconnect();
  360. //$this->otherDb->disconnect();
  361. // Remove Current Database
  362. if ($this->config['driver'] == 'sqlite3') {
  363. $this->setLoggerChannel('Migration')->info('All Table data converted');
  364. $this->setLoggerChannel('Migration')->info('Starting Database movement for sqlite3');
  365. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  366. $oldFileSize = filesize($this->config['dbLocation'] . $this->config['dbName']);
  367. $newFileSize = filesize($this->config['dbLocation'] . $migrationDB);
  368. if ($newFileSize > 0) {
  369. $this->setLoggerChannel('Migration')->info('New Table size has been verified');
  370. @unlink($this->config['dbLocation'] . $this->config['dbName']);
  371. copy($this->config['dbLocation'] . $migrationDB, $this->config['dbLocation'] . $this->config['dbName']);
  372. @unlink($this->config['dbLocation'] . $migrationDB);
  373. $this->setLoggerChannel('Migration')->info('Migrated Old Info to new Database');
  374. @unlink($tempLock);
  375. return true;
  376. } else {
  377. $this->setLoggerChannel('Migration')->warning('Database filesize is zero');
  378. }
  379. } else {
  380. $this->setLoggerChannel('Migration')->warning('Migration Database does not exist');
  381. }
  382. }
  383. @unlink($tempLock);
  384. return false;
  385. } else {
  386. $this->setLoggerChannel('Migration')->warning('Could not create migration Database');
  387. }
  388. @unlink($tempLock);
  389. return false;
  390. }
  391. return false;
  392. }
  393. public function resetUpdateFeature($feature = null)
  394. {
  395. if (!$feature) {
  396. $this->setResponse(409, 'Feature not supplied');
  397. return false;
  398. }
  399. $feature = strtolower($feature);
  400. switch ($feature) {
  401. case 'groupmax':
  402. case 'groupidmax':
  403. case 'group-max':
  404. case 'group-id-max':
  405. case 'group_id':
  406. case 'group_id_max':
  407. $columnExists = $this->checkIfColumnExists('tabs', 'group_id_max');
  408. if ($columnExists) {
  409. $query = [
  410. [
  411. 'function' => 'query',
  412. 'query' => [
  413. 'UPDATE tabs SET group_id_max=0'
  414. ]
  415. ],
  416. ];
  417. $tabs = $this->processQueries($query);
  418. if (!$tabs) {
  419. $this->setResponse(500, 'An error occurred');
  420. return false;
  421. }
  422. } else {
  423. $this->setResponse(500, 'group_id_max column does not exist');
  424. return false;
  425. }
  426. break;
  427. default:
  428. $this->setResponse(404, 'Feature not found in reset update');
  429. return false;
  430. }
  431. $this->setResponse(200, 'Ran reset update feature for ' . $feature);
  432. return true;
  433. }
  434. public function upgradeToVersion($version = '2.1.0')
  435. {
  436. $this->setLoggerChannel('Upgrade')->notice('Starting upgrade to version ' . $version);
  437. switch ($version) {
  438. case '2.1.0':
  439. $this->upgradeSettingsTabURL();
  440. $this->upgradeHomepageTabURL();
  441. break;
  442. case '2.1.400':
  443. $this->removeOldPluginDirectoriesAndFiles();
  444. break;
  445. case '2.1.525':
  446. $this->removeOldCustomHTML();
  447. break;
  448. case '2.1.860':
  449. $this->upgradeInstalledPluginsConfigItem();
  450. break;
  451. case '2.1.1500':
  452. $this->upgradeDataToFolder();
  453. break;
  454. case '2.1.1860':
  455. $this->upgradePluginsToDataFolder();
  456. break;
  457. case '2.1.2200':
  458. $this->backupOrganizr();
  459. $this->addGroupIdMaxToDatabase();
  460. $this->addAddToAdminToDatabase();
  461. break;
  462. case '2.1.5000':
  463. $this->fixGroupOIDC();
  464. break;
  465. }
  466. $this->setLoggerChannel('Upgrade')->notice('Finished upgrade to version ' . $version);
  467. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  468. return true;
  469. }
  470. public function fixGroupOIDC()
  471. {
  472. $this->updateConfig(array('oidcDefaultGroupId' => (string) $this->config['oidcDefaultGroupId']));
  473. $this->logger->info(
  474. 'Updated OIDC default group id to string type',
  475. [
  476. 'oldValue' => $this->config['oidcDefaultGroupId'],
  477. 'newValue' => (string) $this->config['oidcDefaultGroupId']
  478. ]);
  479. }
  480. public function removeOldCacheFolder()
  481. {
  482. $folder = $this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  483. $this->setLoggerChannel('Migration');
  484. $this->logger->info('Running Old Cache folder migration');
  485. if (file_exists($folder)) {
  486. $this->rrmdir($folder);
  487. $this->logger->info('Old Cache folder found');
  488. $this->logger->info('Removed Old Cache folder');
  489. }
  490. return true;
  491. }
  492. public function upgradeDataToFolder()
  493. {
  494. if ($this->hasDB()) {
  495. // Make main data folder
  496. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  497. // Make config folder child
  498. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);
  499. if ($rootFolderMade) {
  500. // Migrate over userTabs folder
  501. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs');
  502. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs')) {
  503. // Convert tabs over
  504. $query = [
  505. [
  506. 'function' => 'fetchAll',
  507. 'query' => [
  508. 'SELECT * FROM tabs WHERE image like "%userTabs%"'
  509. ]
  510. ],
  511. ];
  512. $tabs = $this->processQueries($query);
  513. if (count($tabs) > 0) {
  514. foreach ($tabs as $tab) {
  515. $newImage = str_replace('plugins/images/userTabs', 'data/userTabs', $tab['image']);
  516. $updateQuery = [
  517. [
  518. 'function' => 'query',
  519. 'query' => [
  520. 'UPDATE tabs SET',
  521. ['image' => $newImage],
  522. 'WHERE id = ?',
  523. $tab['id']
  524. ]
  525. ],
  526. ];
  527. $this->processQueries($updateQuery);
  528. }
  529. }
  530. $this->setLoggerChannel('Migration');
  531. $this->logger->info('The folder "userTabs" was migrated to new data folder');
  532. }
  533. // Migrate over custom cert
  534. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem')) {
  535. // Make cert folder child
  536. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR);
  537. 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')) {
  538. $this->setLoggerChannel('Migration');
  539. $this->logger->info('Moved over custom cert file');
  540. }
  541. }
  542. // Migrate over favIcon
  543. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon');
  544. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'faviconCustom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon')) {
  545. if ($this->config['favIcon'] !== '') {
  546. $this->config['favIcon'] = str_replace('plugins/images/faviconCustom', 'data/favicon', $this->config['favIcon']);
  547. $this->updateConfig(array('favIcon' => $this->config['favIcon']));
  548. }
  549. $this->setLoggerChannel('Migration');
  550. $this->logger->info('Favicon was migrated over');
  551. }
  552. // Migrate over custom pages
  553. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages');
  554. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom')) {
  555. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages')) {
  556. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom');
  557. $this->setLoggerChannel('Migration');
  558. $this->logger->info('Custom pages was migrated over');
  559. }
  560. }
  561. // Migrate over custom routes
  562. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes');
  563. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom')) {
  564. 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')) {
  565. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom');
  566. $this->setLoggerChannel('Migration');
  567. $this->logger->info('Custom routes was migrated over');
  568. }
  569. }
  570. // Migrate over cache folder
  571. $this->removeOldCacheFolder();
  572. }
  573. return true;
  574. }
  575. return false;
  576. }
  577. public function upgradePluginsToDataFolder()
  578. {
  579. if ($this->hasDB()) {
  580. // Make main data folder
  581. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  582. if ($rootFolderMade) {
  583. // Migrate over plugins folder
  584. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins');
  585. $plexLibraries = $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'plexLibraries';
  586. if (file_exists($plexLibraries)) {
  587. if (rename($plexLibraries, $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'plexLibraries')) {
  588. $this->setLoggerChannel('Migration');
  589. $this->logger->info('The plugin folder "plexLibraries" was migrated to new data folder');
  590. }
  591. }
  592. $test = $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'test';
  593. if (file_exists($test)) {
  594. if (rename($test, $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'test')) {
  595. $this->setLoggerChannel('Migration');
  596. $this->logger->info('The plugin folder "test" was migrated to new data folder');
  597. }
  598. }
  599. }
  600. return true;
  601. }
  602. return false;
  603. }
  604. public function upgradeSettingsTabURL()
  605. {
  606. $response = [
  607. array(
  608. 'function' => 'query',
  609. 'query' => array(
  610. 'UPDATE tabs SET',
  611. ['url' => 'api/v2/page/settings'],
  612. 'WHERE url = ?',
  613. 'api/?v1/settings/page'
  614. )
  615. ),
  616. ];
  617. return $this->processQueries($response);
  618. }
  619. public function upgradeHomepageTabURL()
  620. {
  621. $response = [
  622. array(
  623. 'function' => 'query',
  624. 'query' => array(
  625. 'UPDATE tabs SET',
  626. ['url' => 'api/v2/page/homepage'],
  627. 'WHERE url = ?',
  628. 'api/?v1/homepage/page'
  629. )
  630. ),
  631. ];
  632. return $this->processQueries($response);
  633. }
  634. public function upgradeInstalledPluginsConfigItem()
  635. {
  636. $oldConfigItem = $this->config['installedPlugins'];
  637. if (gettype($oldConfigItem) == 'string') {
  638. if ((strpos($oldConfigItem, '|') !== false)) {
  639. $newPlugins = [];
  640. $plugins = explode('|', $oldConfigItem);
  641. foreach ($plugins as $plugin) {
  642. $info = explode(':', $plugin);
  643. $newPlugins[$info[0]] = [
  644. 'name' => $info[0],
  645. 'version' => $info[1],
  646. 'repo' => 'organizr'
  647. ];
  648. }
  649. } else {
  650. $newPlugins = [];
  651. if ($oldConfigItem !== '') {
  652. $info = explode(':', $oldConfigItem);
  653. $newPlugins[$info[0]] = [
  654. 'name' => $info[0],
  655. 'version' => $info[1],
  656. 'repo' => 'https://github.com/Organizr/Organizr-Plugins'
  657. ];
  658. }
  659. }
  660. $this->updateConfig(['installedPlugins' => $newPlugins]);
  661. } elseif (gettype($oldConfigItem) == 'array') {
  662. $this->updateConfig(['installedPlugins' => $oldConfigItem]);
  663. }
  664. return true;
  665. }
  666. public function removeOldPluginDirectoriesAndFiles()
  667. {
  668. $folders = [
  669. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  670. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  671. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  672. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  673. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  674. ];
  675. $files = [
  676. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  677. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  678. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  679. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  680. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  681. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  682. ];
  683. foreach ($files as $file) {
  684. if (file_exists($file)) {
  685. @unlink($file);
  686. }
  687. }
  688. foreach ($folders as $folder) {
  689. if (file_exists($folder)) {
  690. @$this->rrmdir($folder);
  691. }
  692. }
  693. return true;
  694. }
  695. public function checkForConfigKeyAddToArray($keys)
  696. {
  697. $updateItems = [];
  698. foreach ($keys as $new => $old) {
  699. if (isset($this->config[$old])) {
  700. if ($this->config[$old] !== '') {
  701. $updateItemsNew = [$new => $this->config[$old]];
  702. $updateItems = array_merge($updateItems, $updateItemsNew);
  703. }
  704. }
  705. }
  706. return $updateItems;
  707. }
  708. public function removeOldCustomHTML()
  709. {
  710. $backup = $this->backupOrganizr();
  711. if ($backup) {
  712. $keys = [
  713. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  714. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  715. 'customHTML01' => 'customHTMLone',
  716. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  717. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  718. 'customHTML02' => 'customHTMLtwo',
  719. ];
  720. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  721. $updateComplete = false;
  722. if (!empty($updateItems)) {
  723. $updateComplete = $this->updateConfig($updateItems);
  724. }
  725. if ($updateComplete) {
  726. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  727. if ($removeConfigItems) {
  728. return true;
  729. }
  730. }
  731. }
  732. return false;
  733. }
  734. public function addGroupIdMaxToDatabase()
  735. {
  736. $this->setLoggerChannel('Database Migration')->info('Starting database update');
  737. $hasOldColumn = $this->checkIfColumnExists('tabs', 'group_id_min');
  738. if ($hasOldColumn) {
  739. $this->setLoggerChannel('Database Migration')->info('Cleaning up database by removing old group_id_min');
  740. $removeColumn = $this->dropColumnFromDatabase('tabs', 'group_id_min');
  741. if ($removeColumn) {
  742. $this->setLoggerChannel('Database Migration')->info('Removed group_id_min from database');
  743. } else {
  744. $this->setLoggerChannel('Database Migration')->warning('Error removing group_id_min from database');
  745. }
  746. }
  747. $addColumn = $this->addColumnToDatabase('tabs', 'group_id_max', 'INTEGER DEFAULT \'0\'');
  748. if ($addColumn) {
  749. $this->setLoggerChannel('Database Migration')->notice('Added group_id_max to database');
  750. return true;
  751. } else {
  752. $this->setLoggerChannel('Database Migration')->warning('Could not update database');
  753. return false;
  754. }
  755. }
  756. public function addAddToAdminToDatabase()
  757. {
  758. $this->setLoggerChannel('Database Migration')->info('Starting database update');
  759. $addColumn = $this->addColumnToDatabase('tabs', 'add_to_admin', 'INTEGER DEFAULT \'0\'');
  760. if ($addColumn) {
  761. $this->setLoggerChannel('Database Migration')->notice('Added add_to_admin to database');
  762. return true;
  763. } else {
  764. $this->setLoggerChannel('Database Migration')->warning('Could not update database');
  765. return false;
  766. }
  767. }
  768. }