upgrade-functions.php 26 KB

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