upgrade-functions.php 16 KB

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