Project

General

Profile

1 1457 Luisehahne
<?php
2
/**
3 1866 Luisehahne
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 1457 Luisehahne
 *
5 1866 Luisehahne
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9 1457 Luisehahne
 *
10 1866 Luisehahne
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 1457 Luisehahne
 */
18
19 1866 Luisehahne
/**
20
 * upgrade.php
21
 *
22
 * @category     Module
23 1918 Luisehahne
 * @package      news
24 1866 Luisehahne
 * @subpackage   upgrade
25
 * @author       Dietmar Wöllbrink <dietmar.woellbrink@websitebaker.org>
26
 * @author       Werner v.d.Decken <wkl@isteam.de>
27
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
28
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
29
 * @version      0.0.1
30
 * @revision     $Revision$
31
 * @link         $HeadURL$
32
 * @lastmodified $Date$
33
 * @since        File available since 17.01.2013
34
 * @description  xyz
35
 *
36
 */
37
38 1538 Luisehahne
/* -------------------------------------------------------- */
39 1587 darkviper
// Must include code to stop this file being accessed directly
40 1895 Luisehahne
if(!defined('WB_URL')) {
41
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
42
	throw new IllegalFileException();
43
}
44
45
46 1587 darkviper
/* -------------------------------------------------------- */
47 1457 Luisehahne
/* **** START UPGRADE ******************************************************* */
48 1895 Luisehahne
//if(!function_exists('mod_news_Upgrade'))
49
//{
50
	function mod_news_Upgrade($bDebug=false)
51 1457 Luisehahne
	{
52 1895 Luisehahne
		global $OK ,$FAIL;
53
		$database=WbDatabase::getInstance();
54
		$mLang = Translate::getinstance();
55
		$sModName = basename(dirname(__FILE__));
56
		$mLang->enableAddon('modules\\'.$sModName);
57
		$msg    = array();
58 1457 Luisehahne
		$callingScript = $_SERVER["SCRIPT_NAME"];
59 1765 Luisehahne
// check if upgrade startet by upgrade-script to echo a message
60 1457 Luisehahne
		$tmp = 'upgrade-script.php';
61
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
62 1895 Luisehahne
// check for missing tables, if true stop the upgrade
63 1866 Luisehahne
		$aTable = array('mod_news_posts','mod_news_groups','mod_news_comments','mod_news_settings');
64 1895 Luisehahne
		$aPackage = UpgradeHelper::existsAllTables($aTable);
65
		if( sizeof($aPackage) > 0){
66
			$msg[] =  'TABLE '.implode(' missing! '.$FAIL.'<br />TABLE ',$aPackage).' missing! '.$FAIL;
67
			$msg[] = "WYSIWYG upgrade failed $FAIL";
68
			if($globalStarted) {
69
				echo '<strong>'.implode('<br />',$msg).'</strong><br />';
70
			}
71
			$mLang->disableAddon();
72
			return ( ($globalStarted==true ) ? $globalStarted : $msg);
73
		} else {
74
			/**
75
			 * check database engine
76
			 */
77
			 for($x=0; $x<sizeof($aTable);$x++) {
78
				if(($sOldType = $database->getTableEngine($database->TablePrefix.$aTable[$x]))) {
79
					if(('myisam' != strtolower($sOldType))) {
80
						if(!$database->query('ALTER TABLE `'.$database->TablePrefix.$aTable[$x].'` Engine = \'MyISAM\' ')) {
81
							$msg[] = $database->get_error()." $FAIL";
82
						} else{
83
							$msg[] = 'TABLE `'.$database->TablePrefix.$aTable[$x].'` changed to Engine = \'MyISAM\''." $OK";
84
						}
85
					} else {
86
						 $msg[] = 'TABLE `'.$database->TablePrefix.$aTable[$x].'` has Engine = \'MyISAM\''." $OK";
87 1866 Luisehahne
					}
88
				} else {
89 1895 Luisehahne
					$msg[] = $database->get_error()." $FAIL";
90 1866 Luisehahne
				}
91
			}
92 1457 Luisehahne
93 1895 Luisehahne
			$sPagesPath = WB_PATH.PAGES_DIRECTORY;
94
			$sPostsPath = $sPagesPath.'/posts';
95 1918 Luisehahne
// create /posts/ - directory if not exists
96 1895 Luisehahne
			if(is_writable($sPagesPath)) {
97
				if(!($bRetval = is_dir($sPostsPath))) {
98
					$iOldUmask = umask(0) ;
99
					// sanitize directory mode to 'o+rwx/g+x/u+x' and create path
100
					$bRetval = mkdir($sPostsPath, (OCTAL_DIR_MODE |0711), true);
101
					umask($iOldUmask);
102
				}
103
				if($bRetval) {
104
					$msg[] = 'Directory "'.PAGES_DIRECTORY.'/posts/" already exists or created.'." $OK";
105
				}else {
106
					$msg[] = ($mLang->MESSAGE_PAGES_CANNOT_CREATE_ACCESS_FILE)." $FAIL";
107
				}
108 1866 Luisehahne
			}else {
109 1895 Luisehahne
					$msg[] = ($mLang->MESSAGE_PAGES_CANNOT_CREATE_ACCESS_FILE)." $FAIL";
110 1866 Luisehahne
			}
111 1457 Luisehahne
	// check if new fields must be added
112 1895 Luisehahne
			$doImportDate = true;
113
			if(!$database->field_exists($database->TablePrefix.'mod_news_posts', 'created_when')) {
114
				if(!$database->field_add($database->TablePrefix.'mod_news_posts', 'created_when',
115
				                        'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
116
					$msg[] = $mLang->MESSAGE_RECORD_MODIFIED_FAILED." $FAIL";
117
				} else {
118
					$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_when` added.'." $OK";
119
				}
120
			} else {
121
				$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_when` already exists.'." $OK";
122
				$doImportDate = false;
123 1457 Luisehahne
			}
124 1765 Luisehahne
125 1895 Luisehahne
			if(!$database->field_exists($database->TablePrefix.'mod_news_posts', 'created_by')) {
126
				if(!$database->field_add($database->TablePrefix.'mod_news_posts', 'created_by',
127
				                        'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
128
					$msg[] = $mLang->MESSAGE_RECORD_MODIFIED_FAILED." $FAIL";
129
				} else {
130
					$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_by` added.'." $OK";
131
				}
132
			} else {
133
				$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_by` already exists.'." $OK";
134
				$doImportDate = false;
135 1457 Luisehahne
			}
136 1918 Luisehahne
// preset new fields `created_by` and `created_by` from existing values
137 1895 Luisehahne
			if($doImportDate) {
138
				$sql  = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` ';
139
				$sql .= 'SET `created_by`=`posted_by`, `created_when`=`posted_when`';
140
				$database->query($sql);
141
			}
142 1918 Luisehahne
	if($doImportDate) {
143 1866 Luisehahne
	/**
144
	 * rebuild news post folder
145
	 */
146
//	$array = rebuildFolderProtectFile($sPostsPath);
147 1895 Luisehahne
		// now iterate through all existing accessfiles,
148
		// write its creation date into database
149
			if(is_writable($sPostsPath)) {
150
				$oDir = new DirectoryIterator($sPostsPath);
151
				$count = 0;
152
				foreach ($oDir as $fileinfo)
153
				{
154
					$fileName = $fileinfo->getFilename();
155
					if((!$fileinfo->isDot()) &&
156
					   ($fileName != 'index.php') &&
157
					   (substr_compare($fileName,PAGE_EXTENSION,(0-strlen(PAGE_EXTENSION)),strlen(PAGE_EXTENSION)) === 0)
158
					  )
159
					{
160
					// save creation date from old accessfile
161
						if($doImportDate) {
162
							$link = '/posts/'.preg_replace('/'.preg_quote(PAGE_EXTENSION).'$/i', '', $fileinfo->getFilename());
163
							$sql  = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` ';
164
							$sql .= 'SET `created_when`='.$fileinfo->getMTime().' ';
165
							$sql .= 'WHERE `link`=\''.$link.'\'';
166
							if($database->query($sql)) {
167
								// delete old access file
168
								unlink($fileinfo->getPathname());
169
								$count++;
170
							}
171
						}
172 1866 Luisehahne
					}
173 1457 Luisehahne
				}
174 1895 Luisehahne
				unset($oDir);
175 1457 Luisehahne
			}
176 1895 Luisehahne
			if($count > 0) {
177
				$msg[] = 'Save date of creation from '.$count.' old accessfiles and delete these files.'." $OK";
178
			}
179 1918 Luisehahne
	}
180 1457 Luisehahne
// ************************************************
181 1918 Luisehahne
// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
182 1895 Luisehahne
			$sql  = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` ';
183
			$sql .= 'SET `created_when`=`published_when` ';
184
			$sql .= 'WHERE `published_when`<`created_when`';
185
			$database->query($sql);
186
			$sql  = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` ';
187
			$sql .= 'SET `created_when`=`posted_when` ';
188
			$sql .= 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
189
			$database->query($sql);
190 1457 Luisehahne
// ************************************************
191 1985 Luisehahne
			$aReport = array('FilesDeleted'=>0,'FilesCreated'=>0,);
192
        	$sModulReorg = 'm_news_Reorg';
193
        	if( !$globalStarted && class_exists($sModulReorg) ) {
194
        		$oReorg = new $sModulReorg($sModulReorg::LOG_EXTENDED);
195
				$aReturnMsg = $oReorg->execute(); // show details
196
                $aReport = $oReorg->getReport();
197
                unset($oReorg);
198
        	}
199 1457 Luisehahne
200 1895 Luisehahne
			// only for upgrade-script
201
			if($globalStarted) {
202
				if($bDebug) {
203
					echo '<strong>'.implode('<br />',$msg).'</strong><br />';
204
				}
205
			}
206
		}
207 1985 Luisehahne
208
//		$msg[] = '<strong>'.$aReport['FilesDeleted'].' Files successful deleted</strong>';
209
		$msg[] = '<strong>Number of new formated access files: '.$aReport['FilesCreated'].'</strong>';
210
		$msg[] = "<strong>News upgrade successfull finished</strong>";
211 1895 Luisehahne
		if($globalStarted) {
212
			echo "<strong>News upgrade successfull finished $OK</strong><br />";
213
		}
214
		$mLang->disableAddon();
215
		return ( ($globalStarted==true ) ? $globalStarted : $msg);
216 1457 Luisehahne
	}
217 1895 Luisehahne
//}
218 1765 Luisehahne
// end mod_news_Upgrade
219 1477 Luisehahne
220 1538 Luisehahne
// ------------------------------------
221 1985 Luisehahne
// Don't show the messages twice
222 1895 Luisehahne
$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
223
if( is_array($msg = mod_news_Upgrade($bDebugModus)) ) {
224 1985 Luisehahne
// only show if manuell upgrade
225
	echo ''.implode('<br />',$msg).'<br />';
226 1866 Luisehahne
}
227
/* **** END UPGRADE ********************************************************* */