1 |
1889
|
Luisehahne
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
5 |
|
|
*
|
6 |
|
|
* This program is free software: you can redistribute it and/or modify
|
7 |
|
|
* it under the terms of the GNU General Public License as published by
|
8 |
|
|
* the Free Software Foundation, either version 3 of the License, or
|
9 |
|
|
* (at your option) any later version.
|
10 |
|
|
*
|
11 |
|
|
* This program is distributed in the hope that it will be useful,
|
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
* GNU General Public License for more details.
|
15 |
|
|
*
|
16 |
|
|
* You should have received a copy of the GNU General Public License
|
17 |
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
18 |
|
|
*/
|
19 |
|
|
|
20 |
|
|
/**
|
21 |
|
|
* UpgradeHelper.php
|
22 |
|
|
*
|
23 |
|
|
* @category Core
|
24 |
|
|
* @package Core_Upgrade
|
25 |
|
|
* @copyright Werner v.d.Decken <wkl@isteam.de>
|
26 |
|
|
* @author Werner v.d.Decken <wkl@isteam.de>
|
27 |
|
|
* @license http://www.gnu.org/licenses/gpl.html GPL License
|
28 |
|
|
* @version 0.0.1
|
29 |
2070
|
darkviper
|
* @revision $Revision$
|
30 |
|
|
* @link $HeadURL$
|
31 |
|
|
* @lastmodified $Date$
|
32 |
1889
|
Luisehahne
|
* @since File available since 17.03.2013
|
33 |
|
|
* @description some helper function for upgrade-script.php
|
34 |
|
|
*/
|
35 |
|
|
class UpgradeHelper {
|
36 |
|
|
|
37 |
1923
|
darkviper
|
/**
|
38 |
|
|
* Compare available tables against a list of tables
|
39 |
1965
|
darkviper
|
* @param array list of needed table names without TablePrefix
|
40 |
1923
|
darkviper
|
* @return array list of missing tables
|
41 |
1965
|
darkviper
|
* @description this method is the replaement for self::existsAllTables()
|
42 |
1923
|
darkviper
|
*/
|
43 |
1965
|
darkviper
|
public static function getMissingTables(array $aTablesList)
|
44 |
1889
|
Luisehahne
|
{
|
45 |
1923
|
darkviper
|
$aTablesList = array_flip($aTablesList);
|
46 |
1889
|
Luisehahne
|
$oDb = WbDatabase::getInstance();
|
47 |
|
|
$sPattern = addcslashes ( $oDb->TablePrefix, '%_' );
|
48 |
2012
|
darkviper
|
if (($oTables = $oDb->query( 'SHOW TABLES LIKE "'.$sPattern.'%"'))) {
|
49 |
|
|
while ($aTable = $oTables->fetchRow(MYSQL_NUM)) {
|
50 |
1889
|
Luisehahne
|
$sTable = preg_replace('/^'.preg_quote($oDb->TablePrefix, '/').'/s', '', $aTable[0]);
|
51 |
2012
|
darkviper
|
if (isset($aTablesList[$sTable])) {
|
52 |
1923
|
darkviper
|
unset($aTablesList[$sTable]);
|
53 |
1889
|
Luisehahne
|
}
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
1923
|
darkviper
|
return array_flip($aTablesList);
|
57 |
1889
|
Luisehahne
|
}
|
58 |
1965
|
darkviper
|
/**
|
59 |
|
|
* Alias for self::getMissingTables()
|
60 |
|
|
* @param array list of needed table names without TablePrefix
|
61 |
|
|
* @return array list of missing tables
|
62 |
|
|
*/
|
63 |
|
|
public static function existsAllTables(array $aTablesList)
|
64 |
|
|
{
|
65 |
|
|
return self::getMissingTables($aTablesList);
|
66 |
|
|
}
|
67 |
2012
|
darkviper
|
/**
|
68 |
|
|
* Sanitize and repair Pagetree links
|
69 |
|
|
* @return boolean|int number of updated records or false on error
|
70 |
|
|
*/
|
71 |
|
|
public static function sanitizePagesTreeLinkStructure()
|
72 |
|
|
{
|
73 |
|
|
$oDb = WbDatabase::getInstance();
|
74 |
|
|
$iCounter = 0;
|
75 |
|
|
$aPages = array();
|
76 |
|
|
try {
|
77 |
|
|
$sql = 'SELECT `page_id`, `link`, `page_trail` '
|
78 |
|
|
. 'FROM `'.$oDb->TablePrefix.'pages` '
|
79 |
|
|
. 'ORDER BY `page_id`';
|
80 |
|
|
$oPages = $oDb->doQuery($sql);
|
81 |
|
|
// read 'page_id', 'link' and 'page_trail' from all pages
|
82 |
|
|
while ($aPage = $oPages->fetchRow(MYSQL_ASSOC)) {
|
83 |
|
|
// extact filename only from complete link
|
84 |
|
|
$aPages[$aPage['page_id']]['filename'] = preg_replace('/.*?\/([^\/]*$)/', '\1', $aPage['link']);
|
85 |
|
|
$aPages[$aPage['page_id']]['page_trail'] = $aPage['page_trail'];
|
86 |
|
|
$aPages[$aPage['page_id']]['link'] = $aPage['link'];
|
87 |
|
|
}
|
88 |
|
|
foreach ($aPages as $iKey=>$aRecord) {
|
89 |
|
|
// iterate all pages
|
90 |
|
|
$aTmp = array();
|
91 |
|
|
$aIds = explode(',', $aRecord['page_trail']);
|
92 |
|
|
// rebuild link from filenames using page_trail
|
93 |
|
|
foreach($aIds as $iId) {
|
94 |
|
|
$aTmp[] = $aPages[$iId]['filename'];
|
95 |
|
|
}
|
96 |
|
|
$sLink = '/'.implode('/', $aTmp);
|
97 |
|
|
if ($sLink != $aPages[$iKey]['link']) {
|
98 |
|
|
// update page if old link is different to new generated link
|
99 |
|
|
$sql = 'UPDATE `'.$oDb->TablePrefix.'pages` '
|
100 |
|
|
. 'SET `link`=\''.$sLink.'\' '
|
101 |
|
|
. 'WHERE `page_id`='.$iKey;
|
102 |
|
|
$oDb->doQuery($sql);
|
103 |
|
|
$iCounter++;
|
104 |
|
|
}
|
105 |
|
|
}
|
106 |
|
|
} catch(WbDatabaseException $e) {
|
107 |
|
|
return false;
|
108 |
|
|
}
|
109 |
|
|
return $iCounter;
|
110 |
|
|
}
|
111 |
|
|
/**
|
112 |
|
|
*
|
113 |
|
|
* @param string $sMsg Message to show
|
114 |
|
|
*/
|
115 |
|
|
public static function dieWithMessage($sMsg)
|
116 |
|
|
{
|
117 |
|
|
$sMsg = 'Fatal error occured during initial startup.<br /><br />'.PHP_EOL.$sMsg
|
118 |
|
|
. '<br />'.PHP_EOL.'Please correct these errors and then '
|
119 |
|
|
. '<a href="http://'.$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"].'" '
|
120 |
|
|
. 'title="restart">klick here to restart the upgrade-script</a>.<br />'.PHP_EOL;
|
121 |
|
|
die($sMsg);
|
122 |
|
|
}
|
123 |
|
|
/**
|
124 |
|
|
*
|
125 |
|
|
* @param string $sAppPath path to the current installation
|
126 |
|
|
* @return boolean
|
127 |
|
|
*/
|
128 |
|
|
public static function checkSetupFiles($sAppPath)
|
129 |
|
|
{
|
130 |
|
|
if (defined('DB_PASSWORD')) {
|
131 |
|
|
// old config.php is active
|
132 |
2077
|
darkviper
|
if ( !is_writable($sAppPath.'config.php')
|
133 |
|
|
|| (file_exists($sAppPath.'config.php.new') && !is_writable($sAppPath.'config.php.new'))
|
134 |
|
|
|| (file_exists($sAppPath.'setup.ini.php') && !is_writable($sAppPath.'setup.ini.php'))
|
135 |
|
|
|| (!file_exists($sAppPath.'setup.ini.php') && !is_writable($sAppPath.'setup.ini.php.new'))
|
136 |
|
|
)
|
137 |
2012
|
darkviper
|
{
|
138 |
|
|
// stop script if there's an error occured
|
139 |
2077
|
darkviper
|
$sMsg = 'Following files must exist and be writable to run upgrade:<br />'.PHP_EOL
|
140 |
|
|
. '<ul>'.PHP_EOL
|
141 |
|
|
. '<li>config.php</li>'.PHP_EOL
|
142 |
|
|
. (file_exists($sAppPath.'config.php.new') ? '<li>config.php.new</li>'.PHP_EOL : '')
|
143 |
|
|
. (file_exists($sAppPath.'setup.ini.php') ? '<li>setup.ini.php</li>'.PHP_EOL : '')
|
144 |
|
|
. (!file_exists($sAppPath.'setup.ini.php') ? '<li>setup.ini.php.new</li>'.PHP_EOL : '')
|
145 |
|
|
. '</ul>'.PHP_EOL;
|
146 |
2012
|
darkviper
|
self::dieWithMessage($sMsg);
|
147 |
|
|
} else { // ok, let's change the files now!
|
148 |
2015
|
darkviper
|
if (file_exists($sAppPath.'setup.ini.php')) {
|
149 |
2012
|
darkviper
|
// if 'setup.ini.php' exists
|
150 |
2015
|
darkviper
|
if (!is_writeable($sAppPath.'setup.ini.php')) {
|
151 |
2012
|
darkviper
|
// but it's not writable
|
152 |
|
|
$sMsg = 'The file \'setup.ini.php\' already exists but is not writeable!';
|
153 |
|
|
self::dieWithMessage($sMsg);
|
154 |
|
|
}
|
155 |
|
|
} else {
|
156 |
|
|
// try to rename 'setup.ini.php.new' into 'setup.ini.php'
|
157 |
2015
|
darkviper
|
if (!rename($sAppPath.'setup.ini.php.new', $sAppPath.'setup.ini.php')) {
|
158 |
|
|
$sMsg = 'Can not rename \''.$sAppPath.'setup.ini.php.new\' into \''.$sAppPath.'setup.ini.php\' !!<br />'
|
159 |
|
|
. 'Create an empty file \''.$sAppPath.'setup.ini.php\' and make it writeable for the server!';
|
160 |
2012
|
darkviper
|
self::dieWithMessage($sMsg);
|
161 |
|
|
}
|
162 |
|
|
}
|
163 |
|
|
// now first read constants from old config.php
|
164 |
|
|
$sContent = file_get_contents($sAppPath.'config.php');
|
165 |
|
|
$sPattern = '/^\s*define\s*\(\s*([\'\"])(.*?)\1\s*,.*;\s*$/m';
|
166 |
|
|
if (preg_match_all($sPattern, $sContent, $aMatches)) {
|
167 |
|
|
// get all already defined consts
|
168 |
|
|
$aAllConsts = get_defined_constants(true);
|
169 |
|
|
$aSetupConsts = array();
|
170 |
|
|
// collect the needed values from defined consts
|
171 |
|
|
foreach($aMatches[2] as $sConstName) {
|
172 |
|
|
$aSetupConsts[$sConstName] = (isset($aAllConsts['user'][$sConstName]) ? $aAllConsts['user'][$sConstName] : '');
|
173 |
|
|
}
|
174 |
|
|
// try to sanitize available values
|
175 |
|
|
$aSetupConsts['DB_TYPE'] = ((isset($aSetupConsts['DB_TYPE']) && $aSetupConsts['DB_TYPE'] != '') ? $aSetupConsts['DB_TYPE'] : 'mysql');
|
176 |
|
|
$aSetupConsts['DB_PORT'] = ((isset($aSetupConsts['DB_PORT']) && $aSetupConsts['DB_PORT'] != '') ? $aSetupConsts['DB_PORT'] : '3306');
|
177 |
|
|
$aSetupConsts['DB_CHARSET'] = (isset($aSetupConsts['DB_CHARSET']) ? $aSetupConsts['DB_CHARSET'] : '');
|
178 |
|
|
$aSetupConsts['DB_CHARSET'] = preg_replace('/[^0-9a-z]*/i', '', $aSetupConsts['DB_CHARSET']);
|
179 |
|
|
$aSetupConsts['TABLE_PREFIX'] = (isset($aSetupConsts['TABLE_PREFIX']) ? $aSetupConsts['TABLE_PREFIX'] : '');
|
180 |
|
|
$aSetupConsts['ADMIN_DIRECTORY'] = trim(str_replace('\\', '/', preg_replace('/^'.preg_quote(WB_PATH, '/').'/', '', ADMIN_PATH)), '/').'/';
|
181 |
|
|
$aSetupConsts['WB_URL'] = rtrim(str_replace('\\', '/', WB_URL), '/').'/';
|
182 |
|
|
// Try and write settings to config file
|
183 |
2015
|
darkviper
|
if (self::writeSetupIni($sAppPath, $aSetupConsts)) {
|
184 |
|
|
if (self::writeConfig($sAppPath)) {
|
185 |
2012
|
darkviper
|
return true;
|
186 |
|
|
} else {
|
187 |
|
|
$sMsg ='Error writing \''.$sAppPath.'config.php\'!';
|
188 |
|
|
}
|
189 |
|
|
} else {
|
190 |
|
|
$sMsg ='Error writing \''.$sAppPath.'setup.ini.php\'!';
|
191 |
|
|
}
|
192 |
|
|
} else {
|
193 |
|
|
$sMsg = 'No valid content found in \''.$sAppPath.'config.php\'!';
|
194 |
|
|
}
|
195 |
|
|
self::dieWithMessage($sMsg);
|
196 |
|
|
}
|
197 |
|
|
} else {
|
198 |
|
|
$sFileContent = file_get_contents($sAppPath.'config.php');
|
199 |
|
|
if (preg_match('/\s*define\s*\(.*\)\s*;/i', $sFileContent)) {
|
200 |
|
|
// if config.php does not contain any defines
|
201 |
|
|
if (is_writable($sAppPath.'config.php')) {
|
202 |
|
|
// overwrite config.php with default content for compatibility
|
203 |
|
|
if (self::writeConfig($sAppPath.'config.php')) {
|
204 |
|
|
return true;
|
205 |
|
|
} else {
|
206 |
|
|
$sMsg ='Error writing \''.$sAppPath.'config.php\'!';
|
207 |
|
|
}
|
208 |
|
|
} else {
|
209 |
|
|
$sMsg ='File is not writable \''.$sAppPath.'config.php\'!';
|
210 |
|
|
}
|
211 |
|
|
self::dieWithMessage($sMsg);
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
|
|
/**
|
216 |
|
|
*
|
217 |
|
|
* @param string $sAppPath the path where setup.ini.php is located
|
218 |
|
|
* @param array $aSetupValues
|
219 |
|
|
* @return boolean
|
220 |
|
|
*/
|
221 |
|
|
public static function writeSetupIni($sAppPath, array $aSetupValues)
|
222 |
|
|
{
|
223 |
|
|
$sSetupContent =
|
224 |
|
|
";<?php die('sorry, illegal file access'); ?>#####\n"
|
225 |
|
|
.";################################################\n"
|
226 |
|
|
."; WebsiteBaker configuration file\n"
|
227 |
|
|
."; auto generated ".date('Y-m-d h:i:s A e ')."\n"
|
228 |
|
|
.";################################################\n"
|
229 |
|
|
."[Constants]\n"
|
230 |
|
|
."DEBUG = false\n"
|
231 |
|
|
."AppUrl = \"".$aSetupValues['WB_URL']."\"\n"
|
232 |
|
|
."AcpDir = \"".$aSetupValues['ADMIN_DIRECTORY']."\"\n"
|
233 |
|
|
.";##########\n"
|
234 |
|
|
."[DataBase]\n"
|
235 |
|
|
."type = \"".$aSetupValues['DB_TYPE']."\"\n"
|
236 |
|
|
."user = \"".$aSetupValues['DB_USERNAME']."\"\n"
|
237 |
|
|
."pass = \"".$aSetupValues['DB_PASSWORD']."\"\n"
|
238 |
|
|
."host = \"".$aSetupValues['DB_HOST']."\"\n"
|
239 |
2015
|
darkviper
|
."port = \"".$aSetupValues['DB_PORT']."\"\n"
|
240 |
2012
|
darkviper
|
."name = \"".$aSetupValues['DB_NAME']."\"\n"
|
241 |
|
|
."charset = \"".$aSetupValues['DB_CHARSET']."\"\n"
|
242 |
|
|
."table_prefix = \"".$aSetupValues['TABLE_PREFIX']."\"\n"
|
243 |
|
|
.";\n"
|
244 |
|
|
.";################################################\n";
|
245 |
|
|
$sSetupFile = $sAppPath.'setup.ini.php';
|
246 |
|
|
if (file_put_contents($sSetupFile, $sSetupContent)) {
|
247 |
|
|
return true;
|
248 |
|
|
}
|
249 |
|
|
return false;
|
250 |
|
|
}
|
251 |
|
|
/**
|
252 |
|
|
*
|
253 |
|
|
* @param string $sAppPath the path where config.php is located
|
254 |
|
|
* @return boolean
|
255 |
|
|
*/
|
256 |
|
|
public static function writeConfig($sAppPath)
|
257 |
|
|
{
|
258 |
|
|
$sConfigContent = "<?php\n"
|
259 |
|
|
."/* this file is for backward compatibility only */\n"
|
260 |
|
|
."/* never put any code in this file! */\n"
|
261 |
|
|
."include_once(dirname(__FILE__).'/framework/initialize.php');\n";
|
262 |
|
|
$sConfigFile = $sAppPath.'config.php';
|
263 |
|
|
if (file_put_contents($sConfigFile, $sConfigContent)) {
|
264 |
|
|
return true;
|
265 |
|
|
}
|
266 |
|
|
return false;
|
267 |
|
|
}
|
268 |
1889
|
Luisehahne
|
|
269 |
|
|
} // end of class UpgradeHelper
|