upgrade-functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. default:
  225. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  226. return true;
  227. }
  228. }
  229. public function upgradeSettingsTabURL()
  230. {
  231. $response = [
  232. array(
  233. 'function' => 'query',
  234. 'query' => array(
  235. 'UPDATE tabs SET',
  236. ['url' => 'api/v2/page/settings'],
  237. 'WHERE url = ?',
  238. 'api/?v1/settings/page'
  239. )
  240. ),
  241. ];
  242. return $this->processQueries($response);
  243. }
  244. public function upgradeHomepageTabURL()
  245. {
  246. $response = [
  247. array(
  248. 'function' => 'query',
  249. 'query' => array(
  250. 'UPDATE tabs SET',
  251. ['url' => 'api/v2/page/homepage'],
  252. 'WHERE url = ?',
  253. 'api/?v1/homepage/page'
  254. )
  255. ),
  256. ];
  257. return $this->processQueries($response);
  258. }
  259. public function upgradeInstalledPluginsConfigItem()
  260. {
  261. $oldConfigItem = $this->config['installedPlugins'];
  262. if (gettype($oldConfigItem) == 'string') {
  263. if ((strpos($oldConfigItem, '|') !== false)) {
  264. $newPlugins = [];
  265. $plugins = explode('|', $oldConfigItem);
  266. foreach ($plugins as $plugin) {
  267. $info = explode(':', $plugin);
  268. $newPlugins[$info[0]] = [
  269. 'name' => $info[0],
  270. 'version' => $info[1],
  271. 'repo' => 'organizr'
  272. ];
  273. }
  274. } else {
  275. $newPlugins = [];
  276. if ($oldConfigItem !== '') {
  277. $info = explode(':', $oldConfigItem);
  278. $newPlugins[$info[0]] = [
  279. 'name' => $info[0],
  280. 'version' => $info[1],
  281. 'repo' => 'https://github.com/Organizr/Organizr-Plugins'
  282. ];
  283. }
  284. }
  285. $this->updateConfig(['installedPlugins' => $newPlugins]);
  286. } elseif (gettype($oldConfigItem) == 'array') {
  287. $this->updateConfig(['installedPlugins' => $oldConfigItem]);
  288. }
  289. return true;
  290. }
  291. public function removeOldPluginDirectoriesAndFiles()
  292. {
  293. $folders = [
  294. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  295. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  296. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  297. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  298. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  299. ];
  300. $files = [
  301. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  302. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  303. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  304. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  305. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  306. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  307. ];
  308. foreach ($files as $file) {
  309. if (file_exists($file)) {
  310. @unlink($file);
  311. }
  312. }
  313. foreach ($folders as $folder) {
  314. if (file_exists($folder)) {
  315. @$this->rrmdir($folder);
  316. }
  317. }
  318. return true;
  319. }
  320. public function checkForConfigKeyAddToArray($keys)
  321. {
  322. $updateItems = [];
  323. foreach ($keys as $new => $old) {
  324. if (isset($this->config[$old])) {
  325. if ($this->config[$old] !== '') {
  326. $updateItemsNew = [$new => $this->config[$old]];
  327. $updateItems = array_merge($updateItems, $updateItemsNew);
  328. }
  329. }
  330. }
  331. return $updateItems;
  332. }
  333. public function removeOldCustomHTML()
  334. {
  335. $backup = $this->backupOrganizr();
  336. if ($backup) {
  337. $keys = [
  338. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  339. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  340. 'customHTML01' => 'customHTMLone',
  341. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  342. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  343. 'customHTML02' => 'customHTMLtwo',
  344. ];
  345. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  346. $updateComplete = false;
  347. if (!empty($updateItems)) {
  348. $updateComplete = $this->updateConfig($updateItems);
  349. }
  350. if ($updateComplete) {
  351. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  352. if ($removeConfigItems) {
  353. return true;
  354. }
  355. }
  356. }
  357. return false;
  358. }
  359. }