| 1 | <?php
 | 
  
    | 2 | /**
 | 
  
    | 3 |  *
 | 
  
    | 4 |  * @category        modules
 | 
  
    | 5 |  * @package         wysiwyg
 | 
  
    | 6 |  * @author          WebsiteBaker Project
 | 
  
    | 7 |  * @copyright       2009-2012, 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.3
 | 
  
    | 11 |  * @requirements    PHP 5.2.2 and higher
 | 
  
    | 12 |  * @version         $Id: upgrade.php 2058 2014-01-01 02:21:24Z darkviper $
 | 
  
    | 13 |  * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/modules/wysiwyg/upgrade.php $
 | 
  
    | 14 |  * @lastmodified    $Date: 2014-01-01 03:21:24 +0100 (Wed, 01 Jan 2014) $
 | 
  
    | 15 |  *
 | 
  
    | 16 |  */
 | 
  
    | 17 | /* -------------------------------------------------------- */
 | 
  
    | 18 | // Must include code to stop this file being accessed directly
 | 
  
    | 19 | if(!defined('WB_PATH')) {
 | 
  
    | 20 | 	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
 | 
  
    | 21 | 	throw new IllegalFileException();
 | 
  
    | 22 | }
 | 
  
    | 23 | /* -------------------------------------------------------- */
 | 
  
    | 24 | 
 | 
  
    | 25 | 	function mod_wysiwyg_upgrade ($bDebug=false) {
 | 
  
    | 26 | 		global $OK ,$FAIL;
 | 
  
    | 27 | 		$oDb = WbDatabase::getInstance();
 | 
  
    | 28 |         $oReg = WbAdaptor::getInstance();
 | 
  
    | 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 | // check for missing tables, if true stop the upgrade
 | 
  
    | 35 | 		$aTable = array('mod_wysiwyg');
 | 
  
    | 36 | 		$aPackage = UpgradeHelper::existsAllTables($aTable);
 | 
  
    | 37 | 		if( sizeof($aPackage) > 0){
 | 
  
    | 38 | 			$msg[] =  'TABLE '.implode(' missing! '.$FAIL.'<br />TABLE ',$aPackage).' missing! '.$FAIL;
 | 
  
    | 39 | 			$msg[] = 'WYSIWYG upgrade failed '." $FAIL";
 | 
  
    | 40 | 			if($globalStarted) {
 | 
  
    | 41 | 				echo '<strong>'.implode('<br />',$msg).'</strong><br />';
 | 
  
    | 42 | 			}
 | 
  
    | 43 | 			return ( ($globalStarted==true ) ? $globalStarted : $msg);
 | 
  
    | 44 | 		} else {
 | 
  
    | 45 | 			for($x=0; $x<sizeof($aTable);$x++) {
 | 
  
    | 46 | 				if(($sOldType = $oDb->getTableEngine($oDb->TablePrefix.$aTable[$x]))) {
 | 
  
    | 47 | 					if(('myisam' != strtolower($sOldType))) {
 | 
  
    | 48 | 						if(!$oDb->query('ALTER TABLE `'.$oDb->TablePrefix.$aTable[$x].'` Engine = \'MyISAM\' ')) {
 | 
  
    | 49 | 							$msg[] = $oDb->get_error();
 | 
  
    | 50 | 						} else{
 | 
  
    | 51 | 							$msg[] = 'TABLE `'.$oDb->TablePrefix.$aTable[$x].'` changed to Engine = \'MyISAM\''." $OK";
 | 
  
    | 52 | 						}
 | 
  
    | 53 | 					} else {
 | 
  
    | 54 | 						 $msg[] = 'TABLE `'.$oDb->TablePrefix.$aTable[$x].'` has Engine = \'MyISAM\''." $OK";
 | 
  
    | 55 | 					}
 | 
  
    | 56 | 				} else {
 | 
  
    | 57 | 					$msg[] = $oDb->get_error();
 | 
  
    | 58 | 				}
 | 
  
    | 59 | 			}
 | 
  
    | 60 | // add change or missing index
 | 
  
    | 61 | 			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
 | 
  
    | 62 | 			if($oDb->index_exists($sTable, 'PRIMARY')) {
 | 
  
    | 63 | 				$sql = 'ALTER TABLE `'.$oDb->DbName.'`.`'.$sTable.'` DROP PRIMARY KEY';
 | 
  
    | 64 | 				if(!$oDb->query($sql)) {
 | 
  
    | 65 | 					$msg[] = ''.$oDb->get_error();
 | 
  
    | 66 | 				}
 | 
  
    | 67 | 			}
 | 
  
    | 68 | 			if(!$oDb->index_add($sTable, '', 'section_id', 'PRIMARY')) {
 | 
  
    | 69 | 				$msg[] = ''.$oDb->get_error();
 | 
  
    | 70 | 			} else {
 | 
  
    | 71 | 				$msg[] = 'Create PRIMARY KEY ( `section_id` )'." $OK";
 | 
  
    | 72 | 			}
 | 
  
    | 73 | // change table structure
 | 
  
    | 74 | 			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
 | 
  
    | 75 | 			$sDescription = 'LONGTEXT NOT NULL';
 | 
  
    | 76 | 			$sFieldName = 'text';
 | 
  
    | 77 | 			if(!$oDb->field_modify($sTable,$sFieldName,$sDescription)) {
 | 
  
    | 78 | 				$msg[] = ''.$oDb->get_error();
 | 
  
    | 79 | 			} else {
 | 
  
    | 80 | 				$msg[] = 'Field ( `text` ) description has been changed successfully'." $OK";
 | 
  
    | 81 | 			}
 | 
  
    | 82 | 			$sFieldName = 'content';
 | 
  
    | 83 | 			if(!$oDb->field_modify($sTable,$sFieldName,$sDescription)) {
 | 
  
    | 84 | 				$msg[] = ''.$oDb->get_error();
 | 
  
    | 85 | 			} else {
 | 
  
    | 86 | 				$msg[] = 'Field ( `content` ) description has been changed successfully'." $OK";
 | 
  
    | 87 | 			}
 | 
  
    | 88 | // change internal absolute links into Sysvar placeholders and repair already existing entries
 | 
  
    | 89 | 			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
 | 
  
    | 90 |             $sql = 'SELECT `section_id`, `content` FROM `'.$oDb->TablePrefix.'mod_wysiwyg` ';
 | 
  
    | 91 |             if (($oEntrySet = $oDb->doQuery($sql))) {
 | 
  
    | 92 |                 $iRecords = 0;
 | 
  
    | 93 |                 $iReplaced = 0;
 | 
  
    | 94 |                 $aSearch = array( '/\{SYSVAR\:MEDIA_REL\}[\/\\\\]?/sU',
 | 
  
    | 95 |                                   '/\{SYSVAR\:WB_URL\}[\/\\\\]?/sU',
 | 
  
    | 96 |                                   '/(\{SYSVAR\:AppUrl\.MediaDir\})[\/\\\\]?/sU',
 | 
  
    | 97 |                                   '/(\{SYSVAR\:AppUrl\})[\/\\\\]?/sU'
 | 
  
    | 98 |                                 );
 | 
  
    | 99 |                 $aReplace = array( '{SYSVAR:AppUrl.MediaDir}', '{SYSVAR:AppUrl}', '\1', '\1' );
 | 
  
    | 100 |                 while (($aEntry = $oEntrySet->fetchRow(MYSQL_ASSOC))) {
 | 
  
    | 101 |                     $iCount = 0;
 | 
  
    | 102 |                     $aEntry['content'] = preg_replace($aSearch, $aReplace, $aEntry['content'], -1, $iCount);
 | 
  
    | 103 |                     if ($iCount > 0) {
 | 
  
    | 104 |                         $iReplaced += $iCount;
 | 
  
    | 105 |                         $sql = 'UPDATE `'.$oDb->TablePrefix.'mod_wysiwyg` '
 | 
  
    | 106 |                              . 'SET `content`=\''.$oDb->escapeString($aEntry['content']).'\' '
 | 
  
    | 107 |                              . 'WHERE `section_id`='.$aEntry['section_id'];
 | 
  
    | 108 |                         $oDb->doQuery($sql);
 | 
  
    | 109 |                         $iRecords++;
 | 
  
    | 110 |                     }
 | 
  
    | 111 |                 }
 | 
  
    | 112 |                 $msg[] = '['.$iRecords.'] records with ['.$iReplaced.'] SYSVAR placeholder(s) repaired'." $OK";
 | 
  
    | 113 |             }
 | 
  
    | 114 |             try {
 | 
  
    | 115 |                 $sql  = 'UPDATE `'.$sTable.'` ';
 | 
  
    | 116 |                 $sql .= 'SET `content` = REPLACE(`content`, \'"'.$oReg->AppPath.$oReg->MediaDir.'\', \'"{SYSVAR:AppPath.MediaDir}\')';
 | 
  
    | 117 |                 $oDb->doQuery($sql);
 | 
  
    | 118 | 				$msg[] = 'Change internal absolute Media links into SYSVAR placeholders'." $OK";
 | 
  
    | 119 |             } catch(WbDatabaseException $e) {
 | 
  
    | 120 | 				$msg[] = ''.$oDb->get_error();
 | 
  
    | 121 |             }
 | 
  
    | 122 |             try {
 | 
  
    | 123 |                 $sql  = 'UPDATE `'.$sTable.'` ';
 | 
  
    | 124 |                 $sql .= 'SET `content` = REPLACE(`content`, \'"'.$oReg->AppPath.'\', \'"{SYSVAR:AppPath}\')';
 | 
  
    | 125 |                 $oDb->doQuery($sql);
 | 
  
    | 126 | 				$msg[] = 'Change internal absolute links into SYSVAR placeholders'." $OK";
 | 
  
    | 127 |             } catch(WbDatabaseException $e) {
 | 
  
    | 128 | 				$msg[] = ''.$oDb->get_error();
 | 
  
    | 129 |             }
 | 
  
    | 130 | // only for $callingScript upgrade-script.php
 | 
  
    | 131 | 			if($globalStarted) {
 | 
  
    | 132 | 				if($bDebug) {
 | 
  
    | 133 | 					echo '<strong>'.implode('<br />',$msg).'</strong><br />';
 | 
  
    | 134 | 				}
 | 
  
    | 135 | 			}
 | 
  
    | 136 | 		}
 | 
  
    | 137 | 		$msg[] = 'WYSIWYG upgrade successfull finished'." $OK";
 | 
  
    | 138 | 		if($globalStarted) {
 | 
  
    | 139 | 			echo "<strong>WYSIWYG upgrade successfull finished $OK</strong><br />";
 | 
  
    | 140 | 		}
 | 
  
    | 141 | 		return ( ($globalStarted==true ) ? $globalStarted : $msg);
 | 
  
    | 142 | 	}
 | 
  
    | 143 | // ------------------------------------
 | 
  
    | 144 | $bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
 | 
  
    | 145 | if( is_array($msg = mod_wysiwyg_upgrade($bDebugModus)) ) {
 | 
  
    | 146 | 	echo '<strong>'.implode('<br />',$msg).'</strong><br />';
 | 
  
    | 147 | }
 |