upgrade-functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. if ($updateDB == true) {
  67. //return 'Upgraded Needed - Current Version '.$oldVer.' - New Version: '.$versionCheck;
  68. // Upgrade database to latest version
  69. $updateSuccess = $this->updateDB($oldVer);
  70. }
  71. // Update config.php version if different to the installed version
  72. if ($updateSuccess && $this->version !== $this->config['configVersion']) {
  73. $this->updateConfig(array('apply_CONFIG_VERSION' => $this->version));
  74. $this->setLoggerChannel('Update');
  75. $this->logger->debug('Updated config version to ' . $this->version);
  76. }
  77. if ($updateSuccess == false) {
  78. die($this->showHTML('Database update failed', 'Please manually check logs and fix - Then reload this page'));
  79. }
  80. return true;
  81. }
  82. }
  83. public function addColumnToDatabase($table = '', $columnName = '', $definition = 'TEXT')
  84. {
  85. if ($table == '' || $columnName == '' || $definition == '') {
  86. return false;
  87. }
  88. if ($this->hasDB()) {
  89. $tableInfo = [
  90. array(
  91. 'function' => 'fetchSingle',
  92. 'query' => array(
  93. 'SELECT COUNT(*) AS has_column FROM pragma_table_info(?) WHERE name=?',
  94. $table,
  95. $columnName
  96. )
  97. )
  98. ];
  99. $query = $this->processQueries($tableInfo);
  100. if (!$query) {
  101. $columnAlter = [
  102. array(
  103. 'function' => 'query',
  104. 'query' => ['ALTER TABLE ? ADD ? ' . $definition,
  105. $table,
  106. $columnName,
  107. ]
  108. )
  109. ];
  110. $AlterQuery = $this->processQueries($columnAlter);
  111. if ($AlterQuery) {
  112. $query = $this->processQueries($tableInfo);
  113. if ($query) {
  114. return true;
  115. }
  116. }
  117. } else {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. public function updateDB($oldVerNum = false)
  124. {
  125. $tempLock = $this->config['dbLocation'] . 'DBLOCK.txt';
  126. if (!file_exists($tempLock)) {
  127. touch($tempLock);
  128. $migrationDB = 'tempMigration.db';
  129. $pathDigest = pathinfo($this->config['dbLocation'] . $this->config['dbName']);
  130. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  131. unlink($this->config['dbLocation'] . $migrationDB);
  132. }
  133. // Create Temp DB First
  134. $this->connectOtherDB();
  135. $backupDB = $pathDigest['dirname'] . '/' . $pathDigest['filename'] . '[' . date('Y-m-d_H-i-s') . ']' . ($oldVerNum ? '[' . $oldVerNum . ']' : '') . '.bak.db';
  136. copy($this->config['dbLocation'] . $this->config['dbName'], $backupDB);
  137. $success = $this->createDB($this->config['dbLocation'], true);
  138. if ($success) {
  139. $response = [
  140. array(
  141. 'function' => 'fetchAll',
  142. 'query' => array(
  143. 'SELECT name FROM sqlite_master WHERE type="table"'
  144. )
  145. ),
  146. ];
  147. $tables = $this->processQueries($response);
  148. foreach ($tables as $table) {
  149. $response = [
  150. array(
  151. 'function' => 'fetchAll',
  152. 'query' => array(
  153. 'SELECT * FROM ' . $table['name']
  154. )
  155. ),
  156. ];
  157. $data = $this->processQueries($response);
  158. $this->writeLog('success', 'Update Function - Grabbed Table data for Table: ' . $table['name'], 'Database');
  159. foreach ($data as $row) {
  160. $response = [
  161. array(
  162. 'function' => 'query',
  163. 'query' => array(
  164. 'INSERT into ' . $table['name'],
  165. $row
  166. )
  167. ),
  168. ];
  169. $this->processQueries($response, true);
  170. }
  171. $this->writeLog('success', 'Update Function - Wrote Table data for Table: ' . $table['name'], 'Database');
  172. }
  173. $this->writeLog('success', 'Update Function - All Table data converted - Starting Movement', 'Database');
  174. $this->db->disconnect();
  175. $this->otherDb->disconnect();
  176. // Remove Current Database
  177. if (file_exists($this->config['dbLocation'] . $migrationDB)) {
  178. $oldFileSize = filesize($this->config['dbLocation'] . $this->config['dbName']);
  179. $newFileSize = filesize($this->config['dbLocation'] . $migrationDB);
  180. if ($newFileSize > 0) {
  181. $this->writeLog('success', 'Update Function - Table Size of new DB ok..', 'Database');
  182. @unlink($this->config['dbLocation'] . $this->config['dbName']);
  183. copy($this->config['dbLocation'] . $migrationDB, $this->config['dbLocation'] . $this->config['dbName']);
  184. @unlink($this->config['dbLocation'] . $migrationDB);
  185. $this->writeLog('success', 'Update Function - Migrated Old Info to new Database', 'Database');
  186. @unlink($tempLock);
  187. return true;
  188. } else {
  189. $this->writeLog('error', 'Update Function - Filesize is zero', 'Database');
  190. }
  191. } else {
  192. $this->writeLog('error', 'Update Function - Migration DB does not exist', 'Database');
  193. }
  194. @unlink($tempLock);
  195. return false;
  196. } else {
  197. $this->writeLog('error', 'Update Function - Could not create migration DB', 'Database');
  198. }
  199. @unlink($tempLock);
  200. return false;
  201. }
  202. return false;
  203. }
  204. public function upgradeToVersion($version = '2.1.0')
  205. {
  206. switch ($version) {
  207. case '2.1.0':
  208. $this->upgradeSettingsTabURL();
  209. $this->upgradeHomepageTabURL();
  210. case '2.1.400':
  211. $this->removeOldPluginDirectoriesAndFiles();
  212. case '2.1.525':
  213. $this->removeOldCustomHTML();
  214. default:
  215. $this->setAPIResponse('success', 'Ran update function for version: ' . $version, 200);
  216. return true;
  217. }
  218. }
  219. public function upgradeSettingsTabURL()
  220. {
  221. $response = [
  222. array(
  223. 'function' => 'query',
  224. 'query' => array(
  225. 'UPDATE tabs SET',
  226. ['url' => 'api/v2/page/settings'],
  227. 'WHERE url = ?',
  228. 'api/?v1/settings/page'
  229. )
  230. ),
  231. ];
  232. return $this->processQueries($response);
  233. }
  234. public function upgradeHomepageTabURL()
  235. {
  236. $response = [
  237. array(
  238. 'function' => 'query',
  239. 'query' => array(
  240. 'UPDATE tabs SET',
  241. ['url' => 'api/v2/page/homepage'],
  242. 'WHERE url = ?',
  243. 'api/?v1/homepage/page'
  244. )
  245. ),
  246. ];
  247. return $this->processQueries($response);
  248. }
  249. public function removeOldPluginDirectoriesAndFiles()
  250. {
  251. $folders = [
  252. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'api',
  253. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'config',
  254. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'css',
  255. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'js',
  256. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc',
  257. ];
  258. $files = [
  259. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'bookmark.php',
  260. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'chat.php',
  261. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'healthChecks.php',
  262. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'invites.php',
  263. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'php-mailer.php',
  264. $this->root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'speedTest.php',
  265. ];
  266. foreach ($files as $file) {
  267. if (file_exists($file)) {
  268. @unlink($file);
  269. }
  270. }
  271. foreach ($folders as $folder) {
  272. if (file_exists($folder)) {
  273. @$this->rrmdir($folder);
  274. }
  275. }
  276. return true;
  277. }
  278. public function checkForConfigKeyAddToArray($keys)
  279. {
  280. $updateItems = [];
  281. foreach ($keys as $new => $old) {
  282. if (isset($this->config[$old])) {
  283. if ($this->config[$old] !== '') {
  284. $updateItemsNew = [$new => $this->config[$old]];
  285. $updateItems = array_merge($updateItems, $updateItemsNew);
  286. }
  287. }
  288. }
  289. return $updateItems;
  290. }
  291. public function removeOldCustomHTML()
  292. {
  293. $backup = $this->backupOrganizr();
  294. if ($backup) {
  295. $keys = [
  296. 'homepageCustomHTML01Enabled' => 'homepageCustomHTMLoneEnabled',
  297. 'homepageCustomHTML01Auth' => 'homepageCustomHTMLoneAuth',
  298. 'customHTML01' => 'customHTMLone',
  299. 'homepageCustomHTML02Enabled' => 'homepageCustomHTMLtwoEnabled',
  300. 'homepageCustomHTML02Auth' => 'homepageCustomHTMLtwoAuth',
  301. 'customHTML02' => 'customHTMLtwo',
  302. ];
  303. $updateItems = $this->checkForConfigKeyAddToArray($keys);
  304. $updateComplete = false;
  305. if (!empty($updateItems)) {
  306. $updateComplete = $this->updateConfig($updateItems);
  307. }
  308. if ($updateComplete) {
  309. $removeConfigItems = $this->removeConfigItem(['homepagCustomHTMLoneAuth', 'homepagCustomHTMLoneEnabled', 'homepagCustomHTMLtwoAuth', 'homepagCustomHTMLtwoEnabled', 'homepageOrdercustomhtml', 'homepageOrdercustomhtmlTwo', 'homepageCustomHTMLoneEnabled', 'homepageCustomHTMLoneAuth', 'customHTMLone', 'homepageCustomHTMLtwoEnabled', 'homepageCustomHTMLtwoAuth', 'customHTMLtwo']);
  310. if ($removeConfigItems) {
  311. return true;
  312. }
  313. }
  314. }
  315. return false;
  316. }
  317. }