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