4
0

upgrade-functions.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. }
  455. $this->setLoggerChannel('Upgrade')->notice('Finished upgrade to version ' . $version);
  456. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  457. return true;
  458. }
  459. public function removeOldCacheFolder()
  460. {
  461. $folder = $this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  462. $this->setLoggerChannel('Migration');
  463. $this->logger->info('Running Old Cache folder migration');
  464. if (file_exists($folder)) {
  465. $this->rrmdir($folder);
  466. $this->logger->info('Old Cache folder found');
  467. $this->logger->info('Removed Old Cache folder');
  468. }
  469. return true;
  470. }
  471. public function upgradeDataToFolder()
  472. {
  473. if ($this->hasDB()) {
  474. // Make main data folder
  475. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  476. // Make config folder child
  477. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);
  478. if ($rootFolderMade) {
  479. // Migrate over userTabs folder
  480. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs');
  481. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs')) {
  482. // Convert tabs over
  483. $query = [
  484. [
  485. 'function' => 'fetchAll',
  486. 'query' => [
  487. 'SELECT * FROM tabs WHERE image like "%userTabs%"'
  488. ]
  489. ],
  490. ];
  491. $tabs = $this->processQueries($query);
  492. if (count($tabs) > 0) {
  493. foreach ($tabs as $tab) {
  494. $newImage = str_replace('plugins/images/userTabs', 'data/userTabs', $tab['image']);
  495. $updateQuery = [
  496. [
  497. 'function' => 'query',
  498. 'query' => [
  499. 'UPDATE tabs SET',
  500. ['image' => $newImage],
  501. 'WHERE id = ?',
  502. $tab['id']
  503. ]
  504. ],
  505. ];
  506. $this->processQueries($updateQuery);
  507. }
  508. }
  509. $this->setLoggerChannel('Migration');
  510. $this->logger->info('The folder "userTabs" was migrated to new data folder');
  511. }
  512. // Migrate over custom cert
  513. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem')) {
  514. // Make cert folder child
  515. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR);
  516. 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')) {
  517. $this->setLoggerChannel('Migration');
  518. $this->logger->info('Moved over custom cert file');
  519. }
  520. }
  521. // Migrate over favIcon
  522. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon');
  523. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'faviconCustom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon')) {
  524. if ($this->config['favIcon'] !== '') {
  525. $this->config['favIcon'] = str_replace('plugins/images/faviconCustom', 'data/favicon', $this->config['favIcon']);
  526. $this->updateConfig(array('favIcon' => $this->config['favIcon']));
  527. }
  528. $this->setLoggerChannel('Migration');
  529. $this->logger->info('Favicon was migrated over');
  530. }
  531. // Migrate over custom pages
  532. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages');
  533. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom')) {
  534. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages')) {
  535. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom');
  536. $this->setLoggerChannel('Migration');
  537. $this->logger->info('Custom pages was migrated over');
  538. }
  539. }
  540. // Migrate over custom routes
  541. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes');
  542. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom')) {
  543. 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')) {
  544. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom');
  545. $this->setLoggerChannel('Migration');
  546. $this->logger->info('Custom routes was migrated over');
  547. }
  548. }
  549. // Migrate over cache folder
  550. $this->removeOldCacheFolder();
  551. }
  552. return true;
  553. }
  554. return false;
  555. }
  556. public function upgradePluginsToDataFolder()
  557. {
  558. if ($this->hasDB()) {
  559. // Make main data folder
  560. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  561. if ($rootFolderMade) {
  562. // Migrate over plugins folder
  563. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins');
  564. $plexLibraries = $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'plexLibraries';
  565. if (file_exists($plexLibraries)) {
  566. if (rename($plexLibraries, $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'plexLibraries')) {
  567. $this->setLoggerChannel('Migration');
  568. $this->logger->info('The plugin folder "plexLibraries" was migrated to new data folder');
  569. }
  570. }
  571. $test = $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'test';
  572. if (file_exists($test)) {
  573. if (rename($test, $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'test')) {
  574. $this->setLoggerChannel('Migration');
  575. $this->logger->info('The plugin folder "test" was migrated to new data folder');
  576. }
  577. }
  578. }
  579. return true;
  580. }
  581. return false;
  582. }
  583. public function upgradeSettingsTabURL()
  584. {
  585. $response = [
  586. array(
  587. 'function' => 'query',
  588. 'query' => array(
  589. 'UPDATE tabs SET',
  590. ['url' => 'api/v2/page/settings'],
  591. 'WHERE url = ?',
  592. 'api/?v1/settings/page'
  593. )
  594. ),
  595. ];
  596. return $this->processQueries($response);
  597. }
  598. public function upgradeHomepageTabURL()
  599. {
  600. $response = [
  601. array(
  602. 'function' => 'query',
  603. 'query' => array(
  604. 'UPDATE tabs SET',
  605. ['url' => 'api/v2/page/homepage'],
  606. 'WHERE url = ?',
  607. 'api/?v1/homepage/page'
  608. )
  609. ),
  610. ];
  611. return $this->processQueries($response);
  612. }
  613. public function upgradeInstalledPluginsConfigItem()
  614. {
  615. $oldConfigItem = $this->config['installedPlugins'];
  616. if (gettype($oldConfigItem) == 'string') {
  617. if ((strpos($oldConfigItem, '|') !== false)) {
  618. $newPlugins = [];
  619. $plugins = explode('|', $oldConfigItem);
  620. foreach ($plugins as $plugin) {
  621. $info = explode(':', $plugin);
  622. $newPlugins[$info[0]] = [
  623. 'name' => $info[0],
  624. 'version' => $info[1],
  625. 'repo' => 'organizr'
  626. ];
  627. }
  628. } else {
  629. $newPlugins = [];
  630. if ($oldConfigItem !== '') {
  631. $info = explode(':', $oldConfigItem);
  632. $newPlugins[$info[0]] = [
  633. 'name' => $info[0],
  634. 'version' => $info[1],
  635. 'repo' => 'https://github.com/Organizr/Organizr-Plugins'
  636. ];
  637. }
  638. }
  639. $this->updateConfig(['installedPlugins' => $newPlugins]);
  640. } elseif (gettype($oldConfigItem) == 'array') {
  641. $this->updateConfig(['installedPlugins' => $oldConfigItem]);
  642. }
  643. return true;
  644. }
  645. public function removeOldPluginDirectoriesAndFiles()
  646. {
  647. $folders = [
  648. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  649. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  650. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  651. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  652. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  653. ];
  654. $files = [
  655. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  656. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  657. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  658. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  659. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  660. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  661. ];
  662. foreach ($files as $file) {
  663. if (file_exists($file)) {
  664. @unlink($file);
  665. }
  666. }
  667. foreach ($folders as $folder) {
  668. if (file_exists($folder)) {
  669. @$this->rrmdir($folder);
  670. }
  671. }
  672. return true;
  673. }
  674. public function checkForConfigKeyAddToArray($keys)
  675. {
  676. $updateItems = [];
  677. foreach ($keys as $new => $old) {
  678. if (isset($this->config[$old])) {
  679. if ($this->config[$old] !== '') {
  680. $updateItemsNew = [$new => $this->config[$old]];
  681. $updateItems = array_merge($updateItems, $updateItemsNew);
  682. }
  683. }
  684. }
  685. return $updateItems;
  686. }
  687. public function removeOldCustomHTML()
  688. {
  689. $backup = $this->backupOrganizr();
  690. if ($backup) {
  691. $keys = [
  692. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  693. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  694. 'customHTML01' => 'customHTMLone',
  695. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  696. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  697. 'customHTML02' => 'customHTMLtwo',
  698. ];
  699. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  700. $updateComplete = false;
  701. if (!empty($updateItems)) {
  702. $updateComplete = $this->updateConfig($updateItems);
  703. }
  704. if ($updateComplete) {
  705. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  706. if ($removeConfigItems) {
  707. return true;
  708. }
  709. }
  710. }
  711. return false;
  712. }
  713. public function addGroupIdMaxToDatabase()
  714. {
  715. $this->setLoggerChannel('Database Migration')->info('Starting database update');
  716. $hasOldColumn = $this->checkIfColumnExists('tabs', 'group_id_min');
  717. if ($hasOldColumn) {
  718. $this->setLoggerChannel('Database Migration')->info('Cleaning up database by removing old group_id_min');
  719. $removeColumn = $this->dropColumnFromDatabase('tabs', 'group_id_min');
  720. if ($removeColumn) {
  721. $this->setLoggerChannel('Database Migration')->info('Removed group_id_min from database');
  722. } else {
  723. $this->setLoggerChannel('Database Migration')->warning('Error removing group_id_min from database');
  724. }
  725. }
  726. $addColumn = $this->addColumnToDatabase('tabs', 'group_id_max', 'INTEGER DEFAULT \'0\'');
  727. if ($addColumn) {
  728. $this->setLoggerChannel('Database Migration')->notice('Added group_id_max to database');
  729. return true;
  730. } else {
  731. $this->setLoggerChannel('Database Migration')->warning('Could not update database');
  732. return false;
  733. }
  734. }
  735. public function addAddToAdminToDatabase()
  736. {
  737. $this->setLoggerChannel('Database Migration')->info('Starting database update');
  738. $addColumn = $this->addColumnToDatabase('tabs', 'add_to_admin', 'INTEGER DEFAULT \'0\'');
  739. if ($addColumn) {
  740. $this->setLoggerChannel('Database Migration')->notice('Added add_to_admin to database');
  741. return true;
  742. } else {
  743. $this->setLoggerChannel('Database Migration')->warning('Could not update database');
  744. return false;
  745. }
  746. }
  747. }