Project

General

Profile

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