Project

General

Profile

1 1457 Luisehahne
<?php
2
/**
3
 *
4
 * @category        WebsiteBaker
5
 * @package         modules
6
 * @subpackage      news
7
 * @copyright       2009-2011, Website Baker Org. e.V.
8
 * @link			http://www.websitebaker2.org/
9
 * @license         http://www.gnu.org/licenses/gpl.html
10
 * @platform        WebsiteBaker 2.8.x
11
 * @requirements    PHP 5.2.2 and higher
12
 * @version         $Id$
13
 * @filesource		$HeadURL$
14
 * @lastmodified    $Date$
15
 *
16
 */
17
18 1538 Luisehahne
/* -------------------------------------------------------- */
19 1587 darkviper
// Must include code to stop this file being accessed directly
20
require_once( dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
21
if(!defined('WB_PATH')) { throw new IllegalFileException(); }
22
/* -------------------------------------------------------- */
23 1457 Luisehahne
/* **** START UPGRADE ******************************************************* */
24 1477 Luisehahne
if(!function_exists('mod_news_Upgrade'))
25
{
26 1457 Luisehahne
	function mod_news_Upgrade()
27
	{
28 1538 Luisehahne
		global $database, $msg, $admin, $MESSAGE;
29 1457 Luisehahne
		$callingScript = $_SERVER["SCRIPT_NAME"];
30
		$tmp = 'upgrade-script.php';
31
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
32
33
		$sPagesPath = WB_PATH.PAGES_DIRECTORY;
34
		$sPostsPath = $sPagesPath.'/posts';
35
	// create /posts/ - directory if not exists
36
		if(!file_exists($sPostsPath)) {
37
			if(is_writable($sPagesPath)) {
38
				make_dir(WB_PATH.PAGES_DIRECTORY.'/posts/');
39
			}else {
40
				if(!$globalStarted){
41 1538 Luisehahne
					$msg[] = ($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
42 1457 Luisehahne
				}else {
43 1538 Luisehahne
					$msg[] = $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
44 1457 Luisehahne
					return;
45
				}
46
			}
47
			if($globalStarted) {echo 'directory "'.PAGES_DIRECTORY.'/posts/" created.<br />'; }
48
		}
49
	// check if new fields must be added
50
		$doImportDate = true;
51
		if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_when')) {
52
			if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_when',
53
			                        'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
54
				if($globalStarted){
55
					echo $MESSAGE['RECORD_MODIFIED_FAILED'].'<br />';
56
					return;
57
				}else {
58
					$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
59
				}
60
			}
61
			if($globalStarted) { echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_when` added.<br />'; }
62
		}else { $doImportDate = false; }
63
		if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_by')) {
64
			if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_by',
65
			                        'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
66
				if($globalStarted){
67
					echo $MESSAGE['RECORD_MODIFIED_FAILED'].'<br />';
68
					return;
69
				}else {
70
					$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
71
				}
72
			}
73
			if($globalStarted) {echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_by` added.<br />'; }
74
		}
75
	// preset new fields `created_by` and `created_when` from existing values
76
		if($doImportDate) {
77
			$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
78
			$sql .= 'SET `created_by`=`posted_by`, `created_when`=`posted_when`';
79
			$database->query($sql);
80
		}
81 1593 Luisehahne
82
	$array = rebuildFolderProtectFile($sPostsPath);
83 1457 Luisehahne
	// now iterate through all existing accessfiles,
84
	// write its creation date into database
85
		$oDir = new DirectoryIterator($sPostsPath);
86
		$count = 0;
87
		foreach ($oDir as $fileinfo)
88
		{
89
			$fileName = $fileinfo->getFilename();
90
			if((!$fileinfo->isDot()) &&
91
			   ($fileName != 'index.php') &&
92
			   (substr_compare($fileName,PAGE_EXTENSION,(0-strlen(PAGE_EXTENSION)),strlen(PAGE_EXTENSION)) === 0)
93
			  )
94
			{
95
			// save creation date from old accessfile
96
				if($doImportDate) {
97
					$link = '/posts/'.preg_replace('/'.preg_quote(PAGE_EXTENSION).'$/i', '', $fileinfo->getFilename());
98
					$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
99
					$sql .= 'SET `created_when`='.$fileinfo->getMTime().' ';
100
					$sql .= 'WHERE `link`=\''.$link.'\'';
101
					$database->query($sql);
102
				}
103
			// delete old access file
104
				unlink($fileinfo->getPathname());
105
				$count++;
106
			}
107
		}
108
		unset($oDir);
109
		if($globalStarted && $count > 0) {
110 1538 Luisehahne
			$msg[] = 'save date of creation from '.$count.' old accessfiles and delete these files.<br />';
111 1457 Luisehahne
		}
112
// ************************************************
113
	// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
114
		$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
115
		$sql .= 'SET `created_when`=`published_when` ';
116
		$sql .= 'WHERE `published_when`<`created_when`';
117
		$database->query($sql);
118
		$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
119
		$sql .= 'SET `created_when`=`posted_when` ';
120
		$sql .= 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
121
		$database->query($sql);
122
// ************************************************
123
124
	// rebuild all access-files
125
		$count = 0;
126
		$backSteps = preg_replace('@^'.preg_quote(WB_PATH).'@', '', $sPostsPath);
127
		$backSteps = str_repeat( '../', substr_count($backSteps, '/'));
128
		$sql  = 'SELECT `page_id`,`post_id`,`section_id`,`link` ';
129
		$sql .= 'FROM `'.TABLE_PREFIX.'mod_news_posts`';
130
		$sql .= 'WHERE `link` != \'\'';
131
		if( ($resPosts = $database->query($sql)) )
132
		{
133
			while( $recPost = $resPosts->fetchRow() )
134
			{
135
				$file = $sPagesPath.$recPost['link'].PAGE_EXTENSION;
136
				$content =
137
					'<?php'."\n".
138
					'// *** This file is generated by WebsiteBaker Ver.'.VERSION."\n".
139
					'// *** Creation date: '.date('c')."\n".
140
					'// *** Do not modify this file manually'."\n".
141
					'// *** WB will rebuild this file from time to time!!'."\n".
142
					'// *************************************************'."\n".
143
					"\t".'$page_id    = '.$recPost['page_id'].';'."\n".
144
					"\t".'$section_id = '.$recPost['section_id'].';'."\n".
145
					"\t".'$post_id    = '.$recPost['post_id'].';'."\n".
146
					"\t".'$post_section = '.$recPost['section_id'].';'."\n".
147
					"\t".'require(\''.$backSteps.'index.php\');'."\n".
148
					'// *************************************************'."\n";
149
				if( file_put_contents($file, $content) !== false ) {
150
				// Chmod the file
151
					change_mode($file);
152
				}else {
153
					if($globalStarted){
154 1538 Luisehahne
						$msg[] = $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
155 1457 Luisehahne
						return;
156
					}else {
157 1538 Luisehahne
						$msg[] = ($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
158 1457 Luisehahne
					}
159
				}
160
				$count++;
161
			}
162
		}
163 1538 Luisehahne
		if($globalStarted) { $msg[] = 'created '.$count.' new accessfiles.'; }
164 1457 Luisehahne
	}
165 1477 Luisehahne
}
166
167 1587 darkviper
	$msg = array();
168
	$aTable = array('mod_news_posts','mod_news_groups','mod_news_comments','mod_news_settings');
169
	for($x=0; $x<sizeof($aTable);$x++) {
170
		if(($sOldType = $database->getTableEngine(TABLE_PREFIX.$aTable[$x]))) {
171
			if(('myisam' != strtolower($sOldType))) {
172
				if(!$database->query('ALTER TABLE `'.TABLE_PREFIX.$aTable[$x].'` Engine = \'MyISAM\' ')) {
173
					$msg[] = $database->get_error();
174
				}
175 1538 Luisehahne
			}
176 1587 darkviper
		} else {
177
			$msg[] = $database->get_error();
178 1538 Luisehahne
		}
179
	}
180
// ------------------------------------
181 1457 Luisehahne
	mod_news_Upgrade();
182 1587 darkviper
/* **** END UPGRADE ********************************************************* */