upgrade-functions.php 23 KB

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