Project

General

Profile

« Previous | Next » 

Revision 1457

Added by Dietmar almost 13 years ago

Preparing 2.8.2 stable, last tests

View differences:

upgrade.php
1
<?php
2
/**
3
 *
4
 * @category        modules
5
 * @package         news
6
 * @author          WebsiteBaker Project
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
if(defined('WB_URL')) {
20

  
21
    function create_new_post($filename, $filetime=NULL, $content )
22
    {
23
    global $page_id, $section_id, $post_id;
24
	// The depth of the page directory in the directory hierarchy
25
	// '/pages' is at depth 1
26
	$pages_dir_depth = count(explode('/',PAGES_DIRECTORY))-1;
27
	// Work-out how many ../'s we need to get to the index page
28
	$index_location = '../';
29
	for($i = 0; $i < $pages_dir_depth; $i++)
30
    {
31
		$index_location .= '../';
32
	}
33

  
34
	// Write to the filename
35
	$content .='
36
define("POST_SECTION", $section_id);
37
define("POST_ID", $post_id);
38
require("'.$index_location.'config.php");
39
require(WB_PATH."/index.php");
40
?>';
41
    	if($handle = fopen($filename, 'w+'))
42
        {
43
        	fwrite($handle, $content);
44
        	fclose($handle);
45
            if($filetime)
46
            {
47
                touch($filename, $filetime);
48
            }
49
        	change_mode($filename);
50
        }
51
    }
52

  
53
    // read files from /pages/posts/
54
    if( !function_exists('scandir') )
55
    {
56
        function scandir($directory, $sorting_order = 0)
57
        {
58
            $dh  = opendir($directory);
59
            while( false !== ($filename = readdir($dh)) )
60
            {
61
                $files[] = $filename;
62
            }
63
            if( $sorting_order == 0 )
64
            {
65
                sort($files);
66
            } else
67
            {
68
                rsort($files);
69
            }
70
            return($files);
71
        }
72
    }
73

  
74
    $target_dir = WB_PATH . PAGES_DIRECTORY.'/posts/';
75
	$files = scandir($target_dir);
76
	natcasesort($files);
77

  
78
		// All files in /pages/posts/
79
		foreach( $files as $file )
80
        {
81
            if( file_exists($target_dir.$file)
82
                AND ($file != '.')
83
                    AND ($file != '..')
84
                        AND ($file != 'index.php') )
85
            {
86
                clearstatcache();
87
                $timestamp = filemtime ( $target_dir.$file );
88
                $lines = file($target_dir.$file);
89
                $content = '';
90
                // read lines until first define
91
                foreach ($lines as $line_num => $line) {
92
                    if(strstr($line,'define'))
93
                    {
94
                      break;
95
                    }
96
                    $content .= $line;
97
                }
98

  
99
                create_new_post($target_dir.$file, $timestamp, $content);
100
            }
101

  
102
        }
103
// Print admin footer
104
$admin->print_footer();
105
}
106
?>
1
<?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
	function mod_news_Upgrade()
24
	{
25
		global $database, $admin, $MESSAGE;
26
		$callingScript = $_SERVER["SCRIPT_NAME"];
27
		$tmp = 'upgrade-script.php';
28
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
29

  
30
		$sPagesPath = WB_PATH.PAGES_DIRECTORY;
31
		$sPostsPath = $sPagesPath.'/posts';
32
	// create /posts/ - directory if not exists
33
		if(!file_exists($sPostsPath)) {
34
			if(is_writable($sPagesPath)) {
35
				make_dir(WB_PATH.PAGES_DIRECTORY.'/posts/');
36
			}else {
37
				if(!$globalStarted){
38
					$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
39
				}else {
40
					echo $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
41
					return;
42
				}
43
			}
44
			if($globalStarted) {echo 'directory "'.PAGES_DIRECTORY.'/posts/" created.<br />'; }
45
		}
46
	// check if new fields must be added
47
		$doImportDate = true;
48
		if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_when')) {
49
			if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_when',
50
			                        'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
51
				if($globalStarted){
52
					echo $MESSAGE['RECORD_MODIFIED_FAILED'].'<br />';
53
					return;
54
				}else {
55
					$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
56
				}
57
			}
58
			if($globalStarted) { echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_when` added.<br />'; }
59
		}else { $doImportDate = false; }
60
		if(!$database->field_exists(TABLE_PREFIX.'mod_news_posts', 'created_by')) {
61
			if(!$database->field_add(TABLE_PREFIX.'mod_news_posts', 'created_by',
62
			                        'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
63
				if($globalStarted){
64
					echo $MESSAGE['RECORD_MODIFIED_FAILED'].'<br />';
65
					return;
66
				}else {
67
					$admin->print_error($MESSAGE['RECORD_MODIFIED_FAILED']);
68
				}
69
			}
70
			if($globalStarted) {echo 'datafield `'.TABLE_PREFIX.'mod_news_posts`.`created_by` added.<br />'; }
71
		}
72
	// preset new fields `created_by` and `created_when` from existing values
73
		if($doImportDate) {
74
			$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
75
			$sql .= 'SET `created_by`=`posted_by`, `created_when`=`posted_when`';
76
			$database->query($sql);
77
		}
78
	// now iterate through all existing accessfiles,
79
	// write its creation date into database
80
		$oDir = new DirectoryIterator($sPostsPath);
81
		$count = 0;
82
		foreach ($oDir as $fileinfo)
83
		{
84
			$fileName = $fileinfo->getFilename();
85
			if((!$fileinfo->isDot()) &&
86
			   ($fileName != 'index.php') &&
87
			   (substr_compare($fileName,PAGE_EXTENSION,(0-strlen(PAGE_EXTENSION)),strlen(PAGE_EXTENSION)) === 0)
88
			  )
89
			{
90
			// save creation date from old accessfile
91
				if($doImportDate) {
92
					$link = '/posts/'.preg_replace('/'.preg_quote(PAGE_EXTENSION).'$/i', '', $fileinfo->getFilename());
93
					$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
94
					$sql .= 'SET `created_when`='.$fileinfo->getMTime().' ';
95
					$sql .= 'WHERE `link`=\''.$link.'\'';
96
					$database->query($sql);
97
				}
98
			// delete old access file
99
				unlink($fileinfo->getPathname());
100
				$count++;
101
			}
102
		}
103
		unset($oDir);
104
		if($globalStarted && $count > 0) {
105
			echo 'save date of creation from '.$count.' old accessfiles and delete these files.<br />';
106
		}
107
// ************************************************
108
	// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
109
		$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
110
		$sql .= 'SET `created_when`=`published_when` ';
111
		$sql .= 'WHERE `published_when`<`created_when`';
112
		$database->query($sql);
113
		$sql  = 'UPDATE `'.TABLE_PREFIX.'mod_news_posts` ';
114
		$sql .= 'SET `created_when`=`posted_when` ';
115
		$sql .= 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
116
		$database->query($sql);
117
// ************************************************
118

  
119
	// rebuild all access-files
120
		$count = 0;
121
		// $backSteps = preg_replace('/^'.preg_quote(WB_PATH).'/', '', $sPostsPath);
122
		$backSteps = preg_replace('@^'.preg_quote(WB_PATH).'@', '', $sPostsPath);
123
		$backSteps = str_repeat( '../', substr_count($backSteps, '/'));
124
		$sql  = 'SELECT `page_id`,`post_id`,`section_id`,`link` ';
125
		$sql .= 'FROM `'.TABLE_PREFIX.'mod_news_posts`';
126
		$sql .= 'WHERE `link` != \'\'';
127
		if( ($resPosts = $database->query($sql)) )
128
		{
129
			while( $recPost = $resPosts->fetchRow() )
130
			{
131
				$file = $sPagesPath.$recPost['link'].PAGE_EXTENSION;
132
				$content =
133
					'<?php'."\n".
134
					'// *** This file is generated by WebsiteBaker Ver.'.VERSION."\n".
135
					'// *** Creation date: '.date('c')."\n".
136
					'// *** Do not modify this file manually'."\n".
137
					'// *** WB will rebuild this file from time to time!!'."\n".
138
					'// *************************************************'."\n".
139
					"\t".'$page_id    = '.$recPost['page_id'].';'."\n".
140
					"\t".'$section_id = '.$recPost['section_id'].';'."\n".
141
					"\t".'$post_id    = '.$recPost['post_id'].';'."\n".
142
					"\t".'$post_section = '.$recPost['section_id'].';'."\n".
143
//					"\t".'define(\'POST_SECTION\', '.$recPost['section_id'].');'."\n".
144
//					"\t".'define(\'POST_ID\',      '.$recPost['post_id'].');'."\n".
145
					"\t".'require(\''.$backSteps.'index.php\');'."\n".
146
					'// *************************************************'."\n";
147
				if( file_put_contents($file, $content) !== false ) {
148
				// Chmod the file
149
					change_mode($file);
150
				}else {
151
					if($globalStarted){
152
						echo $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
153
						return;
154
					}else {
155
						$admin->print_error($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
156
					}
157
				}
158
				$count++;
159
			}
160
		}
161
		if($globalStarted) { echo 'created '.$count.' new accessfiles.'; }
162
		if(!$globalStarted) { $admin->print_footer(); }
163
	}
164
	mod_news_Upgrade();
165
/* **** END UPGRADE ********************************************************* */
107 166

  

Also available in: Unified diff