4
0

upgrade-functions.php 22 KB

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