upgrade-functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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.1400';
  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. if ($updateDB == true) {
  83. //return 'Upgraded Needed - Current Version '.$oldVer.' - New Version: '.$versionCheck;
  84. // Upgrade database to latest version
  85. $updateSuccess = $this->updateDB($oldVer);
  86. }
  87. // Update config.php version if different to the installed version
  88. if ($updateSuccess && $this->version !== $this->config['configVersion']) {
  89. $this->updateConfig(array('apply_CONFIG_VERSION' => $this->version));
  90. $this->setLoggerChannel('Update');
  91. $this->logger->debug('Updated config version to ' . $this->version);
  92. }
  93. if ($updateSuccess == false) {
  94. die($this->showHTML('Database update failed', 'Please manually check logs and fix - Then reload this page'));
  95. }
  96. return true;
  97. }
  98. }
  99. public function addColumnToDatabase($table = '', $columnName = '', $definition = 'TEXT')
  100. {
  101. if ($table == '' || $columnName == '' || $definition == '') {
  102. return false;
  103. }
  104. if ($this->hasDB()) {
  105. $tableInfo = [
  106. array(
  107. 'function' => 'fetchSingle',
  108. 'query' => array(
  109. 'SELECT COUNT(*) AS has_column FROM pragma_table_info(?) WHERE name=?',
  110. $table,
  111. $columnName
  112. )
  113. )
  114. ];
  115. $query = $this->processQueries($tableInfo);
  116. if (!$query) {
  117. $columnAlter = [
  118. array(
  119. 'function' => 'query',
  120. 'query' => ['ALTER TABLE ? ADD ? ' . $definition,
  121. $table,
  122. $columnName,
  123. ]
  124. )
  125. ];
  126. $AlterQuery = $this->processQueries($columnAlter);
  127. if ($AlterQuery) {
  128. $query = $this->processQueries($tableInfo);
  129. if ($query) {
  130. return true;
  131. }
  132. }
  133. } else {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. public function updateDB($oldVerNum = false)
  140. {
  141. $tempLock = $this->config['dbLocation'] . 'DBLOCK.txt';
  142. if (!file_exists($tempLock)) {
  143. touch($tempLock);
  144. $migrationDB = 'tempMigration.db';
  145. $pathDigest = pathinfo($this->config['dbLocation'] . $this->config['dbName']);
  146. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  147. unlink($this->config['dbLocation'] . $migrationDB);
  148. }
  149. // Create Temp DB First
  150. $this->connectOtherDB();
  151. $backupDB = $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db';
  152. copy($this->config['dbLocation'] . $this->config['dbName'], $backupDB);
  153. $success = $this->createDB($this->config['dbLocation'], true);
  154. if ($success) {
  155. $response = [
  156. array(
  157. 'function' => 'fetchAll',
  158. 'query' => array(
  159. 'SELECT name FROM sqlite_master WHERE type="table"'
  160. )
  161. ),
  162. ];
  163. $tables = $this->processQueries($response);
  164. foreach ($tables as $table) {
  165. $response = [
  166. array(
  167. 'function' => 'fetchAll',
  168. 'query' => array(
  169. 'SELECT * FROM ' . $table['name']
  170. )
  171. ),
  172. ];
  173. $data = $this->processQueries($response);
  174. $this->writeLog('success', 'Update Function - Grabbed Table data for Table: ' . $table['name'], 'Database');
  175. foreach ($data as $row) {
  176. $response = [
  177. array(
  178. 'function' => 'query',
  179. 'query' => array(
  180. 'INSERT into ' . $table['name'],
  181. $row
  182. )
  183. ),
  184. ];
  185. $this->processQueries($response, true);
  186. }
  187. $this->writeLog('success', 'Update Function - Wrote Table data for Table: ' . $table['name'], 'Database');
  188. }
  189. $this->writeLog('success', 'Update Function - All Table data converted - Starting Movement', 'Database');
  190. $this->db->disconnect();
  191. $this->otherDb->disconnect();
  192. // Remove Current Database
  193. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  194. $oldFileSize = filesize($this->config['dbLocation'] . $this->config['dbName']);
  195. $newFileSize = filesize($this->config['dbLocation'] . $migrationDB);
  196. if ($newFileSize > 0) {
  197. $this->writeLog('success', 'Update Function - Table Size of new DB ok..', 'Database');
  198. @unlink($this->config['dbLocation'] . $this->config['dbName']);
  199. copy($this->config['dbLocation'] . $migrationDB, $this->config['dbLocation'] . $this->config['dbName']);
  200. @unlink($this->config['dbLocation'] . $migrationDB);
  201. $this->writeLog('success', 'Update Function - Migrated Old Info to new Database', 'Database');
  202. @unlink($tempLock);
  203. return true;
  204. } else {
  205. $this->writeLog('error', 'Update Function - Filesize is zero', 'Database');
  206. }
  207. } else {
  208. $this->writeLog('error', 'Update Function - Migration DB does not exist', 'Database');
  209. }
  210. @unlink($tempLock);
  211. return false;
  212. } else {
  213. $this->writeLog('error', 'Update Function - Could not create migration DB', 'Database');
  214. }
  215. @unlink($tempLock);
  216. return false;
  217. }
  218. return false;
  219. }
  220. public function upgradeToVersion($version = '2.1.0')
  221. {
  222. switch ($version) {
  223. case '2.1.0':
  224. $this->upgradeSettingsTabURL();
  225. $this->upgradeHomepageTabURL();
  226. case '2.1.400':
  227. $this->removeOldPluginDirectoriesAndFiles();
  228. case '2.1.525':
  229. $this->removeOldCustomHTML();
  230. case '2.1.860':
  231. $this->upgradeInstalledPluginsConfigItem();
  232. case '2.1.1400':
  233. $this->upgradeDataToFolder();
  234. default:
  235. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  236. return true;
  237. }
  238. }
  239. public function upgradeDataToFolder()
  240. {
  241. if ($this->hasDB()) {
  242. // Make main data folder
  243. $rootFolderMade = $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data');
  244. // Make config folder child
  245. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR);
  246. if ($rootFolderMade) {
  247. // Migrate over userTabs folder
  248. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs');
  249. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'userTabs')) {
  250. // Convert tabs over
  251. $query = [
  252. [
  253. 'function' => 'fetchAll',
  254. 'query' => [
  255. 'SELECT * FROM tabs WHERE image like "%userTabs%"'
  256. ]
  257. ],
  258. ];
  259. $tabs = $this->processQueries($query);
  260. if (count($tabs) > 0) {
  261. foreach ($tabs as $tab) {
  262. $newImage = str_replace('plugins/images/userTabs', 'data/userTabs', $tab['image']);
  263. $updateQuery = [
  264. [
  265. 'function' => 'query',
  266. 'query' => [
  267. 'UPDATE tabs SET',
  268. ['image' => $newImage],
  269. 'WHERE id = ?',
  270. $tab['id']
  271. ]
  272. ],
  273. ];
  274. $this->processQueries($updateQuery);
  275. }
  276. }
  277. $this->setLoggerChannel('Migration');
  278. $this->logger->info('The folder "userTabs" was migrated to new data folder');
  279. }
  280. // Migrate over custom cert
  281. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'functions' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'custom.pem')) {
  282. // Make cert folder child
  283. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR);
  284. 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')) {
  285. $this->setLoggerChannel('Migration');
  286. $this->logger->info('Moved over custom cert file');
  287. }
  288. }
  289. // Migrate over favIcon
  290. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon');
  291. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'faviconCustom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'favicon')) {
  292. if ($this->config['favIcon'] !== '') {
  293. $this->config['favIcon'] = str_replace('plugins/images/faviconCustom', 'data/favicon', $this->config['favIcon']);
  294. $this->updateConfig(array('favIcon' => $this->config['favIcon']));
  295. }
  296. $this->setLoggerChannel('Migration');
  297. $this->logger->info('Favicon was migrated over');
  298. }
  299. // Migrate over custom pages
  300. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages');
  301. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom')) {
  302. if ($this->rcopy($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom', $this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'pages')) {
  303. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'pages' . DIRECTORY_SEPARATOR . 'custom');
  304. $this->setLoggerChannel('Migration');
  305. $this->logger->info('Custom pages was migrated over');
  306. }
  307. }
  308. // Migrate over custom routes
  309. $this->makeDir($this->root . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'routes');
  310. if (file_exists($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom')) {
  311. 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')) {
  312. $this->rrmdir($this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'v2' . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom');
  313. $this->setLoggerChannel('Migration');
  314. $this->logger->info('Custom routes was migrated over');
  315. }
  316. }
  317. }
  318. return true;
  319. }
  320. return false;
  321. }
  322. public function upgradeSettingsTabURL()
  323. {
  324. $response = [
  325. array(
  326. 'function' => 'query',
  327. 'query' => array(
  328. 'UPDATE tabs SET',
  329. ['url' => 'api/v2/page/settings'],
  330. 'WHERE url = ?',
  331. 'api/?v1/settings/page'
  332. )
  333. ),
  334. ];
  335. return $this->processQueries($response);
  336. }
  337. public function upgradeHomepageTabURL()
  338. {
  339. $response = [
  340. array(
  341. 'function' => 'query',
  342. 'query' => array(
  343. 'UPDATE tabs SET',
  344. ['url' => 'api/v2/page/homepage'],
  345. 'WHERE url = ?',
  346. 'api/?v1/homepage/page'
  347. )
  348. ),
  349. ];
  350. return $this->processQueries($response);
  351. }
  352. public function upgradeInstalledPluginsConfigItem()
  353. {
  354. $oldConfigItem = $this->config['installedPlugins'];
  355. if (gettype($oldConfigItem) == 'string') {
  356. if ((strpos($oldConfigItem, '|') !== false)) {
  357. $newPlugins = [];
  358. $plugins = explode('|', $oldConfigItem);
  359. foreach ($plugins as $plugin) {
  360. $info = explode(':', $plugin);
  361. $newPlugins[$info[0]] = [
  362. 'name' => $info[0],
  363. 'version' => $info[1],
  364. 'repo' => 'organizr'
  365. ];
  366. }
  367. } else {
  368. $newPlugins = [];
  369. if ($oldConfigItem !== '') {
  370. $info = explode(':', $oldConfigItem);
  371. $newPlugins[$info[0]] = [
  372. 'name' => $info[0],
  373. 'version' => $info[1],
  374. 'repo' => 'https://github.com/Organizr/Organizr-Plugins'
  375. ];
  376. }
  377. }
  378. $this->updateConfig(['installedPlugins' => $newPlugins]);
  379. } elseif (gettype($oldConfigItem) == 'array') {
  380. $this->updateConfig(['installedPlugins' => $oldConfigItem]);
  381. }
  382. return true;
  383. }
  384. public function removeOldPluginDirectoriesAndFiles()
  385. {
  386. $folders = [
  387. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  388. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  389. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  390. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  391. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  392. ];
  393. $files = [
  394. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  395. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  396. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  397. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  398. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  399. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  400. ];
  401. foreach ($files as $file) {
  402. if (file_exists($file)) {
  403. @unlink($file);
  404. }
  405. }
  406. foreach ($folders as $folder) {
  407. if (file_exists($folder)) {
  408. @$this->rrmdir($folder);
  409. }
  410. }
  411. return true;
  412. }
  413. public function checkForConfigKeyAddToArray($keys)
  414. {
  415. $updateItems = [];
  416. foreach ($keys as $new => $old) {
  417. if (isset($this->config[$old])) {
  418. if ($this->config[$old] !== '') {
  419. $updateItemsNew = [$new => $this->config[$old]];
  420. $updateItems = array_merge($updateItems, $updateItemsNew);
  421. }
  422. }
  423. }
  424. return $updateItems;
  425. }
  426. public function removeOldCustomHTML()
  427. {
  428. $backup = $this->backupOrganizr();
  429. if ($backup) {
  430. $keys = [
  431. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  432. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  433. 'customHTML01' => 'customHTMLone',
  434. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  435. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  436. 'customHTML02' => 'customHTMLtwo',
  437. ];
  438. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  439. $updateComplete = false;
  440. if (!empty($updateItems)) {
  441. $updateComplete = $this->updateConfig($updateItems);
  442. }
  443. if ($updateComplete) {
  444. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  445. if ($removeConfigItems) {
  446. return true;
  447. }
  448. }
  449. }
  450. return false;
  451. }
  452. }