Project

General

Profile

1
<?php
2
/**
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
 *
5
 * 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
 *
10
 * 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
 */
18

    
19
/**
20
 * upgrade.php
21
 * 
22
 * @category     Module
23
 * @package      news
24
 * @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: 1990 $
31
 * @link         $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/news/upgrade.php $
32
 * @lastmodified $Date: 2013-10-19 20:21:55 +0200 (Sat, 19 Oct 2013) $
33
 * @since        File available since 17.01.2013
34
 * @description  xyz
35
 * 
36
 */
37

    
38
/* -------------------------------------------------------- */
39
// Must include code to stop this file being accessed directly
40
if(!defined('WB_URL')) {
41
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
42
	throw new IllegalFileException();
43
}
44

    
45

    
46
/* -------------------------------------------------------- */
47
/* **** START UPGRADE ******************************************************* */
48
//if(!function_exists('mod_news_Upgrade'))
49
//{
50
	function mod_news_Upgrade($bDebug=false)
51
	{
52
		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
		$callingScript = $_SERVER["SCRIPT_NAME"];
59
// check if upgrade startet by upgrade-script to echo a message
60
		$tmp = 'upgrade-script.php';
61
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
62
// check for missing tables, if true stop the upgrade
63
		$aTable = array('mod_news_posts','mod_news_groups','mod_news_comments','mod_news_settings');
64
		$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
					}
88
				} else {
89
					$msg[] = $database->get_error()." $FAIL";
90
				}
91
			}
92

    
93
			$sPagesPath = WB_PATH.PAGES_DIRECTORY;
94
			$sPostsPath = $sPagesPath.'/posts';
95
// create /posts/ - directory if not exists
96
			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
			}else {
109
					$msg[] = ($mLang->MESSAGE_PAGES_CANNOT_CREATE_ACCESS_FILE)." $FAIL";
110
			}
111
	// check if new fields must be added
112
			$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
			}
124

    
125
			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
			}
136
// preset new fields `created_by` and `created_by` from existing values
137
			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
	if($doImportDate) {
143
	/**
144
	 * rebuild news post folder
145
	 */
146
//	$array = rebuildFolderProtectFile($sPostsPath);
147
		// 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
					}
173
				}
174
				unset($oDir);
175
			}
176
			if($count > 0) {
177
				$msg[] = 'Save date of creation from '.$count.' old accessfiles and delete these files.'." $OK";
178
			}
179
	}
180
// ************************************************
181
// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
182
			$sql = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` '
183
			     . 'SET `created_when`=`published_when` '
184
			     . 'WHERE `published_when`<`created_when`';
185
			$database->query($sql);
186
			$sql = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` '
187
			     . 'SET `created_when`=`posted_when` '
188
			     . 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
189
			$database->query($sql);
190
// ************************************************
191
// rebuild all access files
192
			$aReport = array('FilesDeleted'=>0,'FilesCreated'=>0,);
193
        	if( !$globalStarted && class_exists('m_news_Reorg') ) {
194
        		$oReorg = new m_news_Reorg(ModuleReorgAbstract::LOG_EXTENDED);
195
				$oReorg->execute(); // show details
196
                $aReport = $oReorg->getReport();
197
                unset($oReorg);
198
        	}
199
// ************************************************
200
			// only for upgrade-script
201
			if($globalStarted) {
202
				if($bDebug) {
203
					echo '<strong>'.implode('<br />',$msg).'</strong><br />';
204
				}
205
			} 
206
		}
207

    
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
		if($globalStarted) {
212
			echo "<strong>News upgrade successfull finished $OK</strong><br />";
213
		}
214
		$mLang->disableAddon();
215
		return ( ($globalStarted==true ) ? $globalStarted : $msg);
216
	}
217
//}
218
// end mod_news_Upgrade
219

    
220
// ------------------------------------
221
// Don't show the messages twice
222
$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
223
if( is_array($msg = mod_news_Upgrade($bDebugModus)) ) {
224
// only show if manuell upgrade
225
	echo ''.implode('<br />',$msg).'<br />';
226
}
227
/* **** END UPGRADE ********************************************************* */
(33-33/34)