Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        modules
5
 * @package         news
6
 * @subpackage      upgrade
7
 * @copyright       2009-2012, WebsiteBaker 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: upgrade.php 1770 2012-09-24 15:40:38Z Luisehahne $
13
 * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/news/upgrade.php $
14
 * @lastmodified    $Date: 2012-09-24 17:40:38 +0200 (Mon, 24 Sep 2012) $
15
 *
16
 */
17

    
18
/* -------------------------------------------------------- */
19
// 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
/* **** START UPGRADE ******************************************************* */
24
if(!function_exists('mod_news_Upgrade'))
25
{
26
	function mod_news_Upgrade()
27
	{
28
		global $database, $admin, $MESSAGE,$bDebugModus;
29
    	$msg = array();
30
		$callingScript = $_SERVER["SCRIPT_NAME"];
31
// check if upgrade startet by upgrade-script to echo a message
32
		$tmp = 'upgrade-script.php';
33
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
34
        /**
35
         * check database engine
36
         */
37
    	$aTable = array('mod_news_posts','mod_news_groups','mod_news_comments','mod_news_settings');
38
         for($x=0; $x<sizeof($aTable);$x++) {
39
        	if(($sOldType = $database->getTableEngine(TABLE_PREFIX.$aTable[$x]))) {
40
        		if(('myisam' != strtolower($sOldType))) {
41
        			if(!$database->query('ALTER TABLE `'.TABLE_PREFIX.$aTable[$x].'` Engine = \'MyISAM\' ')) {
42
        				$msg[] = $database->get_error();
43
        			} else{
44
                        $msg[] = 'TABLE `'.TABLE_PREFIX.$aTable[$x].'` changed to Engine = \'MyISAM\'';
45
        			}
46
        		} else {
47
                 $msg[] = 'TABLE `'.TABLE_PREFIX.$aTable[$x].'` has Engine = \'MyISAM\'';
48
        		}
49
        	} else {
50
        		$msg[] = $database->get_error();
51
        	}
52
        }
53

    
54

    
55
		$sPagesPath = WB_PATH.PAGES_DIRECTORY;
56
		$sPostsPath = $sPagesPath.'/posts';
57
	// create /posts/ - directory if not exists
58
		if(!file_exists($sPostsPath)) {
59
			if(is_writable($sPagesPath)) {
60
				make_dir(WB_PATH.PAGES_DIRECTORY.'/posts/');
61
			} else {
62
				if(!$globalStarted){
63
					$msg[] = ($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
64
//					$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
65
					return $msg;
66
				} else {
67
					echo  ''.$MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'';
68
					return;
69
				}
70
			}
71
            $msg[] = '<strong>Directory "'.PAGES_DIRECTORY.'/posts/" created</strong>';
72
		} else {
73
            $msg[] = '<strong>Directory "'.PAGES_DIRECTORY.'/posts/" already exists.</strong>';
74
		}
75
	// check if new fields must be added
76
		$doImportDate = true;
77
		if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_when')) {
78
			if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_when',
79
			                        'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
80
				if($globalStarted){
81
					echo ''.$MESSAGE['RECORD_MODIFIED_FAILED'].'';
82
					return;
83
				} else {
84
//					$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
85
					$msg[] = $MESSAGE['RECORD_MODIFIED_FAILED'];
86
					return $msg;
87
				}
88
			}
89

    
90
        if($globalStarted) {
91
            echo 'Datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_when` added.<br />';
92
        }
93
		} else { $doImportDate = false; }
94

    
95
		if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_by')) {
96
			if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_by',
97
			                        'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
98
				if($globalStarted){
99
					echo ''.$MESSAGE['RECORD_MODIFIED_FAILED'].'';
100
					return;
101
				}else {
102
//					$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
103
					$msg[] = $MESSAGE['RECORD_MODIFIED_FAILED'];
104
					return $msg;
105
				}
106
			}
107
            if($globalStarted) {
108
                echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_by` added.<br />';
109
            }
110
		}
111
	// preset new fields `created_by` and `created_when` from existing values
112
		if($doImportDate) {
113
			$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
114
			$sql .= 'SET `created_by`=`posted_by`, `created_when`=`posted_when`';
115
			$database->query($sql);
116
		}
117

    
118
    /**
119
     * rebuild news post folder
120
     */
121
	$array = rebuildFolderProtectFile($sPostsPath);
122
	// now iterate through all existing accessfiles,
123
	// write its creation date into database
124
		$oDir = new DirectoryIterator($sPostsPath);
125
		$count = 0;
126
		foreach ($oDir as $fileinfo)
127
		{
128
			$fileName = $fileinfo->getFilename();
129
			if((!$fileinfo->isDot()) &&
130
			   ($fileName != 'index.php') &&
131
			   (substr_compare($fileName,PAGE_EXTENSION,(0-strlen(PAGE_EXTENSION)),strlen(PAGE_EXTENSION)) === 0)
132
			  )
133
			{
134
			// save creation date from old accessfile
135
				if($doImportDate) {
136
					$link = '/posts/'.preg_replace('/'.preg_quote(PAGE_EXTENSION).'$/i', '', $fileinfo->getFilename());
137
					$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
138
					$sql .= 'SET `created_when`='.$fileinfo->getMTime().' ';
139
					$sql .= 'WHERE `link`=\''.$link.'\'';
140
					$database->query($sql);
141
				}
142
			// delete old access file
143
				unlink($fileinfo->getPathname());
144
				$count++;
145
			}
146
		}
147
		unset($oDir);
148

    
149
		if($count > 0) {
150
			$msg[] = 'save date of creation from '.$count.' old accessfiles and delete these files.';
151
		}
152
// ************************************************
153
	// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
154
		$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
155
		$sql .= 'SET `created_when`=`published_when` ';
156
		$sql .= 'WHERE `published_when`<`created_when`';
157
		$database->query($sql);
158
		$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
159
		$sql .= 'SET `created_when`=`posted_when` ';
160
		$sql .= 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
161
		$database->query($sql);
162
// ************************************************
163

    
164
	// rebuild all access-files
165
		$count = 0;
166
		$backSteps = preg_replace('@^'.preg_quote(WB_PATH).'@', '', $sPostsPath);
167
		$backSteps = str_repeat( '../', substr_count($backSteps, '/'));
168
		$sql  = 'SELECT `page_id`,`post_id`,`section_id`,`link` ';
169
		$sql .= 'FROM `'.TABLE_PREFIX.'mod_news_posts`';
170
		$sql .= 'WHERE `link` != \'\'';
171
		if( ($resPosts = $database->query($sql)) )
172
		{
173
			while( $recPost = $resPosts->fetchRow() )
174
			{
175
				$file = $sPagesPath.$recPost['link'].PAGE_EXTENSION;
176
				$content =
177
					'<?php'."\n".
178
					'// *** This file is generated by WebsiteBaker Ver.'.VERSION."\n".
179
					'// *** Creation date: '.date('c')."\n".
180
					'// *** Do not modify this file manually'."\n".
181
					'// *** WB will rebuild this file from time to time!!'."\n".
182
					'// *************************************************'."\n".
183
					"\t".'$page_id    = '.$recPost['page_id'].';'."\n".
184
					"\t".'$section_id = '.$recPost['section_id'].';'."\n".
185
					"\t".'$post_id    = '.$recPost['post_id'].';'."\n".
186
					"\t".'$post_section = '.$recPost['section_id'].';'."\n".
187
					"\t".'require(\''.$backSteps.'index.php\');'."\n".
188
					'// *************************************************'."\n";
189
				if( file_put_contents($file, $content) !== false ) {
190
				// Chmod the file
191
					change_mode($file);
192
				}else {
193
					$msg[] = ($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
194
				}
195
				$count++;
196
			}
197
            $msg[] = '<strong>Number of new created access files: '.$count.'</strong>';
198
		}
199
        // only for upgrade-script
200
        if($globalStarted) {
201
            if($bDebugModus) {
202
                foreach($msg as $title) {
203
                    echo '<strong>'.$title.'</strong><br />';
204
                }
205
            }
206
        }
207
        return $msg;
208
	}
209
}
210
// end mod_news_Upgrade
211

    
212
// ------------------------------------
213

    
214
$msg = mod_news_Upgrade();
215

    
216

    
217
/* **** END UPGRADE ********************************************************* */
(31-31/32)