Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        modules
5
 * @package         output_filter
6
 * @copyright       WebsiteBaker Org. e.V.
7
 * @author          Dietmar Wöllbrink
8
 * @author          Manuela v.d.Decken <manuela@isteam.de>
9
 * @link            http://websitebaker.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.3
12
 * @requirements    PHP 5.3.6 and higher
13
 * @version         $Id: cmdUpgrade.inc 2 2017-07-02 15:14:29Z Manuela $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb/2.10.x/trunk/modules/output_filter/cmd/cmdUpgrade.inc $
15
 * @lastmodified    $Date: 2017-07-02 17:14:29 +0200 (Sun, 02 Jul 2017) $
16
 *
17
 */
18
// Must include code to stop this file being access directly
19
/* -------------------------------------------------------- */
20
// Must include code to stop this file being accessed directly
21
if(!defined('WB_PATH')) { throw new RuntimeException('Illegal access'); }
22
/* -------------------------------------------------------- */
23

    
24
    $sTable = TABLE_PREFIX.'mod_output_filter';
25
    $i = (!isset($i) ? 1 : $i);
26
    $OK   = "<span class=\"ok\">OK</span>";
27
    $FAIL = "<span class=\"error\">FAILED</span>";
28
    $iErr = false;
29
    $aOldSettings = array();
30

    
31
    if (is_writable($sAddonPath.'/filters/filterShortUrl.php')){
32
        unlink($sAddonPath.'/filters/filterShortUrl.php');
33
    }
34
    $globalStarted = preg_match('/upgrade\-script\.php$/', $callingScript);
35
    $msg = array(
36
        '<div style="margin:1em auto;font-size:1.1em;">',
37
        '<h4>Step '.$i++.': Updating Output Filter Settings</h4>'
38
    );
39
//    print $callingScript.'<br /><br />';
40
    $sInstallStruct = $sAddonPath.'/install-struct.sql';
41
    if ( !is_readable($sInstallStruct)) {
42
        $msg[] = '<strong>\'missing or not readable file [install-struct.sql]\'</strong> '.$FAIL.'<br />';
43
        $iErr = true;
44
    } else {
45
        $aFiles = glob($sAddonPath.'/filters/*');
46
        $getDefaultSettings = (function() use ( $database, $sAddonName, $aFiles ){
47
            $aDefaults = array(
48
                'at_replacement'  => '@',
49
                'dot_replacement' => '.',
50
                'email_filter'    => '1',
51
                'mailto_filter'   => '1'
52
            );
53
            foreach ( $aFiles  as $sFilterFile) {
54
                $key = preg_replace('=^.*?filter([^\.\/\\\\]+)(\.[^\.]+)?$=is', '\1', $sFilterFile);
55
                $aDefaults[$key] = '0';
56
            }
57
          return $aDefaults;
58
        });
59
        $aDefaults =  $getDefaultSettings();
60
//        $aAllowedFilters = array_keys ( $aDefaults );
61
        // try to create table if not exists
62
        $database->SqlImport($sInstallStruct, TABLE_PREFIX, true );
63
        // read settings first
64
        $sql = 'SELECT * FROM `'.$sTable.'`';
65
        // check if table already upgraded
66
        if ( $bOldStructure = $database->field_exists($sTable, 'sys_rel') )
67
        {
68
            if (($oSettings = $database->query($sql)))
69
            {
70
              //
71
                if (!($aOldSettings = $oSettings->fetchRow(MYSQLI_ASSOC))) {
72
                    $msg[] = '<strong>\'Output Filter backup old settings\'</strong> '.$FAIL.'<br />';
73
                    $iErr = true;
74
                } else {
75
                    // add new defaults to old settings without invalide values
76
                    $aNewSettings = array_intersect_key( $aOldSettings, $aDefaults );
77
                    $aOldSettings = array_replace_recursive( $aDefaults, $aNewSettings );
78
                }
79
            }
80
        } elseif ( $database->field_exists($sTable, 'name') ) {
81
              $aOldSettings = $aDefaults;
82
              // overwrite standardsettings ($aOldSettings)
83
              $sql = 'SELECT * FROM `'.$sTable.'`';
84
              if (($oSettings = $database->query($sql))) {
85
                while( $aSettings = $oSettings->fetchRow( MYSQLI_ASSOC ) )
86
                {
87
                    $key = $aSettings['name'];
88
                    $aOldSettings[$key] = $aSettings['value'];
89
                }
90
              }
91
          }
92
        // delete not existing filter in table
93
        $SettingsDenied = array( 'at_replacement', 'dot_replacement', 'email_filter', 'mailto_filter', 'OutputFilterMode' );
94
        $aAllowedFilters  = array_keys ( $aOldSettings );
95
        $aFilterExists    = array_diff ( $aAllowedFilters, $SettingsDenied );
96
        foreach ( $aFilterExists  as $sFilterName) {
97
            $sFilterFile = WB_PATH.'/modules/'.$sAddonName.'/filters/filter'.$sFilterName.'.php';
98
            if( !file_exists( $sFilterFile ) ) {
99
                $sql  = 'DELETE FROM `'.TABLE_PREFIX.'mod_output_filter`'
100
                      . 'WHERE `name` = \''.$sFilterName.'\'';
101
                if( $database->query( $sql ) ){
102
                    unset($aOldSettings[$sFilterName]);
103
                }
104
            }
105
        }
106
        // drop old table and create new one
107
        if ($database->SqlImport($sInstallStruct, TABLE_PREFIX, false))
108
        {
109
            if ($aOldSettings) {
110
            // restore old settings if there any
111
                $sNameValPairs = '';
112
                foreach ($aOldSettings as $index => $val) {
113
                    $sNameValPairs .= ', (\''.$index.'\', \''.$database->escapeString($val).'\')';
114
                }
115
                $sValues = ltrim($sNameValPairs, ', ');
116
                $sql = 'REPLACE INTO `'.$sTable.'` (`name`, `value`) '
117
                     . 'VALUES '.$sValues;
118
                if (!$database->query($sql)) {
119
                    $msg[] = '<strong>\'Output Filter restore old settings\'</strong> '.$FAIL.'<br />';
120
                    $iErr = true;
121
                }
122
            }
123
            $aFilterToDelete= array ('Canonical');
124
            foreach ( $aFilterToDelete  as $sFilterName) {
125
                $sFilterFile = WB_PATH.'/modules/'.$sAddonName.'/filters/filter'.$sFilterName.'.php';
126
                if (is_writable($sFilterFile)){unlink($sFilterFile);}
127
            }
128
        } else {
129
            $msg[] = '<strong>\'Output Filter recreate table\'</strong> '.$FAIL.'<br />';
130
            $iErr = true;
131
        }
132
        if (!$iErr) {
133
            $msg[] = '<strong>\'Output Filter successful updated\'</strong> '.$OK.'<br />';
134
        }
135
        unset($getDefaultSettings);
136
    }
137
    $msg[] = '</div>';
138
    if (!$globalStarted) {print implode("\n", $msg)."\n";}
139

    
140

    
(5-5/5)