Project

General

Profile

1 1538 Luisehahne
<?php
2
/**
3
 *
4
 * @category        modules
5
 * @package         wysiwyg
6
 * @author          WebsiteBaker Project
7 1576 darkviper
 * @copyright       2009-2012, Website Baker Org. e.V.
8 1890 Luisehahne
 * @link            http://www.websitebaker2.org/
9 1538 Luisehahne
 * @license         http://www.gnu.org/licenses/gpl.html
10 1652 Luisehahne
 * @platform        WebsiteBaker 2.8.3
11 1538 Luisehahne
 * @requirements    PHP 5.2.2 and higher
12 1890 Luisehahne
 * @version         $Id$
13
 * @filesource      $HeadURL$
14 1538 Luisehahne
 * @lastmodified    $Date$
15
 *
16
 */
17
/* -------------------------------------------------------- */
18 1576 darkviper
// Must include code to stop this file being accessed directly
19
if(!defined('WB_PATH')) {
20 1652 Luisehahne
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
21 1576 darkviper
	throw new IllegalFileException();
22 1538 Luisehahne
}
23
/* -------------------------------------------------------- */
24 1754 Luisehahne
25 1890 Luisehahne
	function mod_wysiwyg_upgrade ($bDebug=false) {
26
		global $OK ,$FAIL;
27 2058 darkviper
		$oDb = WbDatabase::getInstance();
28
        $oReg = WbAdaptor::getInstance();
29 1890 Luisehahne
		$msg = array();
30 1875 Luisehahne
		$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 1890 Luisehahne
// check for missing tables, if true stop the upgrade
35 1875 Luisehahne
		$aTable = array('mod_wysiwyg');
36 1890 Luisehahne
		$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 2058 darkviper
				if(($sOldType = $oDb->getTableEngine($oDb->TablePrefix.$aTable[$x]))) {
47 1890 Luisehahne
					if(('myisam' != strtolower($sOldType))) {
48 2058 darkviper
						if(!$oDb->query('ALTER TABLE `'.$oDb->TablePrefix.$aTable[$x].'` Engine = \'MyISAM\' ')) {
49
							$msg[] = $oDb->get_error();
50 1890 Luisehahne
						} else{
51 2058 darkviper
							$msg[] = 'TABLE `'.$oDb->TablePrefix.$aTable[$x].'` changed to Engine = \'MyISAM\''." $OK";
52 1890 Luisehahne
						}
53
					} else {
54 2058 darkviper
						 $msg[] = 'TABLE `'.$oDb->TablePrefix.$aTable[$x].'` has Engine = \'MyISAM\''." $OK";
55 1875 Luisehahne
					}
56
				} else {
57 2058 darkviper
					$msg[] = $oDb->get_error();
58 1875 Luisehahne
				}
59 1890 Luisehahne
			}
60
// add change or missing index
61 2058 darkviper
			$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 1890 Luisehahne
				}
67
			}
68 2058 darkviper
			if(!$oDb->index_add($sTable, '', 'section_id', 'PRIMARY')) {
69
				$msg[] = ''.$oDb->get_error();
70 1875 Luisehahne
			} else {
71 1890 Luisehahne
				$msg[] = 'Create PRIMARY KEY ( `section_id` )'." $OK";
72 1875 Luisehahne
			}
73 1890 Luisehahne
// change table structure
74 2058 darkviper
			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
75 1890 Luisehahne
			$sDescription = 'LONGTEXT NOT NULL';
76
			$sFieldName = 'text';
77 2058 darkviper
			if(!$oDb->field_modify($sTable,$sFieldName,$sDescription)) {
78
				$msg[] = ''.$oDb->get_error();
79 1890 Luisehahne
			} else {
80
				$msg[] = 'Field ( `text` ) description has been changed successfully'." $OK";
81 1875 Luisehahne
			}
82 1890 Luisehahne
			$sFieldName = 'content';
83 2058 darkviper
			if(!$oDb->field_modify($sTable,$sFieldName,$sDescription)) {
84
				$msg[] = ''.$oDb->get_error();
85 1890 Luisehahne
			} else {
86
				$msg[] = 'Field ( `content` ) description has been changed successfully'." $OK";
87
			}
88 2058 darkviper
// 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 2083 darkviper
                $aSearch = array( '/\{SYSVAR\:MEDIA_REL\}\/*/s',
95
                                  '/\{SYSVAR\:WB_URL\}\/*/s',
96
                                  '/'.preg_quote('"'.$oReg->AppUrl.$oReg->MediaDir, '/').'*/s',
97
                                  '/'.preg_quote('"'.$oReg->AppUrl, '/').'*/s',
98
                                  '/(\{SYSVAR\:AppUrl\.MediaDir\})\/+/s',
99
                                  '/(\{SYSVAR\:AppUrl\})\/+/s'
100 2058 darkviper
                                );
101 2083 darkviper
                $aReplace = array( '{SYSVAR:AppUrl.MediaDir}', '{SYSVAR:AppUrl}', '{SYSVAR:AppUrl.MediaDir}', '{SYSVAR:AppUrl}', '\1', '\1');
102 2058 darkviper
                while (($aEntry = $oEntrySet->fetchRow(MYSQL_ASSOC))) {
103
                    $iCount = 0;
104 2084 darkviper
                    $aEntry['content'] = preg_replace($aSearch, $aReplace, $aEntry['content'], -1, $iCount);
105 2058 darkviper
                    if ($iCount > 0) {
106
                        $iReplaced += $iCount;
107 2084 darkviper
                        $sql = 'UPDATE `'.$oDb->TablePrefix.'mod_wysiwyg` '
108
                             . 'SET `content`=\''.$oDb->escapeString($aEntry['content']).'\' '
109
                             . 'WHERE `section_id`='.$aEntry['section_id'];
110 2058 darkviper
                        $oDb->doQuery($sql);
111
                        $iRecords++;
112
                    }
113
                }
114 2083 darkviper
                $msg[] = '['.$iRecords.'] records with ['.$iReplaced.'] SYSVAR placeholder(s) repaired/inserted'." $OK";
115
// only for $callingScript upgrade-script.php
116
                if($globalStarted && $bDebug) {
117
                    echo '<strong>'.implode('<br />',$msg).'</strong><br />';
118
                }
119 2058 darkviper
            }
120 2083 darkviper
        }
121 1890 Luisehahne
		$msg[] = 'WYSIWYG upgrade successfull finished'." $OK";
122
		if($globalStarted) {
123
			echo "<strong>WYSIWYG upgrade successfull finished $OK</strong><br />";
124
		}
125
		return ( ($globalStarted==true ) ? $globalStarted : $msg);
126 1875 Luisehahne
	}
127 1890 Luisehahne
// ------------------------------------
128
$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
129
if( is_array($msg = mod_wysiwyg_upgrade($bDebugModus)) ) {
130
	echo '<strong>'.implode('<br />',$msg).'</strong><br />';
131 1576 darkviper
}