| 1 | <?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: upgrade.php 1593 2012-02-01 22:29:36Z Luisehahne $
 | 
  
    | 13 |  * @filesource		$HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/news/upgrade.php $
 | 
  
    | 14 |  * @lastmodified    $Date: 2012-02-01 23:29:36 +0100 (Wed, 01 Feb 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, $msg, $admin, $MESSAGE;
 | 
  
    | 29 | 		$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 | 					$msg[] = ($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
 | 
  
    | 42 | 				}else {
 | 
  
    | 43 | 					$msg[] = $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
 | 
  
    | 44 | 					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 | 
 | 
  
    | 82 | 	$array = rebuildFolderProtectFile($sPostsPath);
 | 
  
    | 83 | 	// 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 | 			$msg[] = 'save date of creation from '.$count.' old accessfiles and delete these files.<br />';
 | 
  
    | 111 | 		}
 | 
  
    | 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 | 						$msg[] = $MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE'].'<br />';
 | 
  
    | 155 | 						return;
 | 
  
    | 156 | 					}else {
 | 
  
    | 157 | 						$msg[] = ($MESSAGE['PAGES_CANNOT_CREATE_ACCESS_FILE']);
 | 
  
    | 158 | 					}
 | 
  
    | 159 | 				}
 | 
  
    | 160 | 				$count++;
 | 
  
    | 161 | 			}
 | 
  
    | 162 | 		}
 | 
  
    | 163 | 		if($globalStarted) { $msg[] = 'created '.$count.' new accessfiles.'; }
 | 
  
    | 164 | 	}
 | 
  
    | 165 | }
 | 
  
    | 166 | 
 | 
  
    | 167 | 	$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 | 			}
 | 
  
    | 176 | 		} else {
 | 
  
    | 177 | 			$msg[] = $database->get_error();
 | 
  
    | 178 | 		}
 | 
  
    | 179 | 	}
 | 
  
    | 180 | // ------------------------------------
 | 
  
    | 181 | 	mod_news_Upgrade();
 | 
  
    | 182 | /* **** END UPGRADE ********************************************************* */
 |