upgrade-functions.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. switch ($version) {
  337. case '2.1.0':
  338. $this->upgradeSettingsTabURL();
  339. $this->upgradeHomepageTabURL();
  340. break;
  341. case '2.1.400':
  342. $this->removeOldPluginDirectoriesAndFiles();
  343. break;
  344. case '2.1.525':
  345. $this->removeOldCustomHTML();
  346. break;
  347. case '2.1.860':
  348. $this->upgradeInstalledPluginsConfigItem();
  349. break;
  350. case '2.1.1500':
  351. $this->upgradeDataToFolder();
  352. break;
  353. case '2.1.1860':
  354. $this->upgradePluginsToDataFolder();
  355. break;
  356. case '2.1.2000':
  357. $this->addGroupIdMinToDatabase();
  358. $this->addAddToAdminToDatabase();
  359. break;
  360. default:
  361. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  362. return true;
  363. }
  364. }
  365. public function removeOldCacheFolder()
  366. {
  367. $folder = $this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  368. $this->setLoggerChannel('Migration');
  369. $this->logger->info('Running Old Cache folder migration');
  370. if (file_exists($folder)) {
  371. $this->rrmdir($folder);
  372. $this->logger->info('Old Cache folder found');
  373. $this->logger->info('Removed Old Cache folder');
  374. }
  375. return true;
  376. }
  377. public function upgradeDataToFolder()
  378. {
  379. if ($this->hasDB()) {
  380. // Make main data folder
  381. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  382. // Make config folder child
  383. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);
  384. if ($rootFolderMade) {
  385. // Migrate over userTabs folder
  386. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs');
  387. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs')) {
  388. // Convert tabs over
  389. $query = [
  390. [
  391. 'function' => 'fetchAll',
  392. 'query' => [
  393. 'SELECT * FROM tabs WHERE image like "%userTabs%"'
  394. ]
  395. ],
  396. ];
  397. $tabs = $this->processQueries($query);
  398. if (count($tabs) > 0) {
  399. foreach ($tabs as $tab) {
  400. $newImage = str_replace('plugins/images/userTabs', 'data/userTabs', $tab['image']);
  401. $updateQuery = [
  402. [
  403. 'function' => 'query',
  404. 'query' => [
  405. 'UPDATE tabs SET',
  406. ['image' => $newImage],
  407. 'WHERE id = ?',
  408. $tab['id']
  409. ]
  410. ],
  411. ];
  412. $this->processQueries($updateQuery);
  413. }
  414. }
  415. $this->setLoggerChannel('Migration');
  416. $this->logger->info('The folder "userTabs" was migrated to new data folder');
  417. }
  418. // Migrate over custom cert
  419. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem')) {
  420. // Make cert folder child
  421. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR);
  422. 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')) {
  423. $this->setLoggerChannel('Migration');
  424. $this->logger->info('Moved over custom cert file');
  425. }
  426. }
  427. // Migrate over favIcon
  428. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon');
  429. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'faviconCustom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon')) {
  430. if ($this->config['favIcon'] !== '') {
  431. $this->config['favIcon'] = str_replace('plugins/images/faviconCustom', 'data/favicon', $this->config['favIcon']);
  432. $this->updateConfig(array('favIcon' => $this->config['favIcon']));
  433. }
  434. $this->setLoggerChannel('Migration');
  435. $this->logger->info('Favicon was migrated over');
  436. }
  437. // Migrate over custom pages
  438. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages');
  439. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom')) {
  440. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages')) {
  441. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom');
  442. $this->setLoggerChannel('Migration');
  443. $this->logger->info('Custom pages was migrated over');
  444. }
  445. }
  446. // Migrate over custom routes
  447. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes');
  448. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom')) {
  449. 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')) {
  450. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom');
  451. $this->setLoggerChannel('Migration');
  452. $this->logger->info('Custom routes was migrated over');
  453. }
  454. }
  455. // Migrate over cache folder
  456. $this->removeOldCacheFolder();
  457. }
  458. return true;
  459. }
  460. return false;
  461. }
  462. public function upgradePluginsToDataFolder()
  463. {
  464. if ($this->hasDB()) {
  465. // Make main data folder
  466. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  467. if ($rootFolderMade) {
  468. // Migrate over plugins folder
  469. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins');
  470. $plexLibraries = $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'plexLibraries';
  471. if (file_exists($plexLibraries)) {
  472. if (rename($plexLibraries, $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'plexLibraries')) {
  473. $this->setLoggerChannel('Migration');
  474. $this->logger->info('The plugin folder "plexLibraries" was migrated to new data folder');
  475. }
  476. }
  477. $test = $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'test';
  478. if (file_exists($test)) {
  479. if (rename($test, $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'test')) {
  480. $this->setLoggerChannel('Migration');
  481. $this->logger->info('The plugin folder "test" was migrated to new data folder');
  482. }
  483. }
  484. }
  485. return true;
  486. }
  487. return false;
  488. }
  489. public function upgradeSettingsTabURL()
  490. {
  491. $response = [
  492. array(
  493. 'function' => 'query',
  494. 'query' => array(
  495. 'UPDATE tabs SET',
  496. ['url' => 'api/v2/page/settings'],
  497. 'WHERE url = ?',
  498. 'api/?v1/settings/page'
  499. )
  500. ),
  501. ];
  502. return $this->processQueries($response);
  503. }
  504. public function upgradeHomepageTabURL()
  505. {
  506. $response = [
  507. array(
  508. 'function' => 'query',
  509. 'query' => array(
  510. 'UPDATE tabs SET',
  511. ['url' => 'api/v2/page/homepage'],
  512. 'WHERE url = ?',
  513. 'api/?v1/homepage/page'
  514. )
  515. ),
  516. ];
  517. return $this->processQueries($response);
  518. }
  519. public function upgradeInstalledPluginsConfigItem()
  520. {
  521. $oldConfigItem = $this->config['installedPlugins'];
  522. if (gettype($oldConfigItem) == 'string') {
  523. if ((strpos($oldConfigItem, '|') !== false)) {
  524. $newPlugins = [];
  525. $plugins = explode('|', $oldConfigItem);
  526. foreach ($plugins as $plugin) {
  527. $info = explode(':', $plugin);
  528. $newPlugins[$info[0]] = [
  529. 'name' => $info[0],
  530. 'version' => $info[1],
  531. 'repo' => 'organizr'
  532. ];
  533. }
  534. } else {
  535. $newPlugins = [];
  536. if ($oldConfigItem !== '') {
  537. $info = explode(':', $oldConfigItem);
  538. $newPlugins[$info[0]] = [
  539. 'name' => $info[0],
  540. 'version' => $info[1],
  541. 'repo' => 'https://github.com/Organizr/Organizr-Plugins'
  542. ];
  543. }
  544. }
  545. $this->updateConfig(['installedPlugins' => $newPlugins]);
  546. } elseif (gettype($oldConfigItem) == 'array') {
  547. $this->updateConfig(['installedPlugins' => $oldConfigItem]);
  548. }
  549. return true;
  550. }
  551. public function removeOldPluginDirectoriesAndFiles()
  552. {
  553. $folders = [
  554. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  555. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  556. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  557. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  558. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  559. ];
  560. $files = [
  561. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  562. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  563. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  564. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  565. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  566. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  567. ];
  568. foreach ($files as $file) {
  569. if (file_exists($file)) {
  570. @unlink($file);
  571. }
  572. }
  573. foreach ($folders as $folder) {
  574. if (file_exists($folder)) {
  575. @$this->rrmdir($folder);
  576. }
  577. }
  578. return true;
  579. }
  580. public function checkForConfigKeyAddToArray($keys)
  581. {
  582. $updateItems = [];
  583. foreach ($keys as $new => $old) {
  584. if (isset($this->config[$old])) {
  585. if ($this->config[$old] !== '') {
  586. $updateItemsNew = [$new => $this->config[$old]];
  587. $updateItems = array_merge($updateItems, $updateItemsNew);
  588. }
  589. }
  590. }
  591. return $updateItems;
  592. }
  593. public function removeOldCustomHTML()
  594. {
  595. $backup = $this->backupOrganizr();
  596. if ($backup) {
  597. $keys = [
  598. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  599. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  600. 'customHTML01' => 'customHTMLone',
  601. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  602. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  603. 'customHTML02' => 'customHTMLtwo',
  604. ];
  605. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  606. $updateComplete = false;
  607. if (!empty($updateItems)) {
  608. $updateComplete = $this->updateConfig($updateItems);
  609. }
  610. if ($updateComplete) {
  611. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  612. if ($removeConfigItems) {
  613. return true;
  614. }
  615. }
  616. }
  617. return false;
  618. }
  619. public function addGroupIdMinToDatabase()
  620. {
  621. $this->setLoggerChannel('Database Migration')->info('Starting database update');
  622. $addColumn = $this->addColumnToDatabase('tabs', 'group_id_min', 'INTEGER DEFAULT \'0\'');
  623. if ($addColumn) {
  624. $this->setLoggerChannel('Database Migration')->notice('Added group_id_min to database');
  625. return true;
  626. } else {
  627. $this->setLoggerChannel('Database Migration')->warning('Could not update database');
  628. return false;
  629. }
  630. }
  631. public function addAddToAdminToDatabase()
  632. {
  633. $this->setLoggerChannel('Database Migration')->info('Starting database update');
  634. $addColumn = $this->addColumnToDatabase('tabs', 'add_to_admin', 'INTEGER DEFAULT \'0\'');
  635. if ($addColumn) {
  636. $this->setLoggerChannel('Database Migration')->notice('Added add_to_admin to database');
  637. return true;
  638. } else {
  639. $this->setLoggerChannel('Database Migration')->warning('Could not update database');
  640. return false;
  641. }
  642. }
  643. }