Completed
Push — master ( 7a4940...1f2aec )
by Richard
16:27 queued 08:03
created

Upgrade_2014::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 6 and the first side effect is on line 209.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Class upgrade_2014
5
 */
6
class Upgrade_2014 extends XoopsUpgrade
7
{
8
    /**
9
     * __construct
10
     */
11
    public function __construct()
12
    {
13
        parent::__construct(basename(__DIR__));
14
        $this->usedFiles = array('mainfile.php');
15
    }
16
17
    /**
18
     * @return bool
19
     */
20
    public function isApplied()
21
    {
22
        return (/*$this->check_0523patch() &&*/
23
        $this->check_auth_db());
24
    }
25
26
    /**
27
     * @return bool
28
     */
29
    public function apply()
30
    {
31
        return $this->apply_auth_db();
32
        /*
33
        if ( $this->apply_0523patch() ) {
34
            return $this->apply_auth_db();
35
        }
36
37
        return false;
38
        */
39
    }
40
41
    /**
42
     * @return bool
43
     */
44
    public function check_0523patch()
45
    {
46
        $lines = file('../mainfile.php');
47
        foreach ($lines as $line) {
48
            if (strpos($line, "\$_REQUEST[\$bad_global]") !== false) {
49
                // Patch found: do not apply again
50
                return true;
51
            }
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function apply_0523patch()
61
    {
62
        $patchCode = "
63
    foreach ( array('GLOBALS', '_SESSION', 'HTTP_SESSION_VARS', '_GET', 'HTTP_GET_VARS', '_POST', 'HTTP_POST_VARS', '_COOKIE', 'HTTP_COOKIE_VARS', '_REQUEST', '_SERVER', 'HTTP_SERVER_VARS', '_ENV', 'HTTP_ENV_VARS', '_FILES', 'HTTP_POST_FILES', 'xoopsDB', 'xoopsUser', 'xoopsUserId', 'xoopsUserGroups', 'xoopsUserIsAdmin', 'xoopsConfig', 'xoopsOption', 'xoopsModule', 'xoopsModuleConfig', 'xoopsRequestUri') as \$bad_global ) {
64
        if ( isset( \$_REQUEST[\$bad_global] ) ) {
65
            header( 'Location: '.XOOPS_URL.'/' );
66
            exit();
67
        }
68
    }
69
";
70
        $manual    = '<h2>' . _MANUAL_INSTRUCTIONS . "</h2>\n<p>" . sprintf(_COPY_RED_LINES, 'mainfile.php') . "</p>
71
<pre style='border:1px solid black;width:650px;overflow:auto;'><span style='color:#ff0000;font-weight:bold;'>$patchCode</span>
72
    if (!isset(\$xoopsOption['nocommon']) && XOOPS_ROOT_PATH != '') {
73
        include XOOPS_ROOT_PATH.\"/include/common.php\";
74
    }
75
</pre>";
76
        $lines     = file('../mainfile.php');
77
78
        $insert         = -1;
79
        $matchProtector = '/modules/protector/include/precheck.inc.php';
80
        $matchDefault   = "\$xoopsOption['nocommon']";
81
82
        foreach ($lines as $k => $line) {
83
            if (strpos($line, "\$_REQUEST[\$bad_global]") !== false) {
84
                // Patch found: do not apply again
85
                $insert = -2;
86
                break;
87
            }
88
            if (strpos($line, $matchProtector) || strpos($line, $matchDefault)) {
89
                $insert = $k;
90
                break;
91
            }
92
        }
93
        if ($insert == -1) {
94
            printf(_FAILED_PATCH . '<br>', 'mainfile.php');
95
            echo $manual;
96
97
            return false;
98
        } elseif ($insert != -2) {
99
            if (!is_writable('../mainfile.php')) {
100
                echo 'mainfile.php is read-only. Please allow the server to write to this file, or apply the patch manually';
101
                echo $manual;
102
103
                return false;
104
            } else {
105
                $fp = fopen('../mainfile.php', 'wt');
106
                if (!$fp) {
107
                    echo 'Error opening mainfile.php, please apply the patch manually.';
108
                    echo $manual;
109
110
                    return false;
111
                } else {
112
                    $newline = defined(PHP_EOL) ? PHP_EOL : (strpos(php_uname(), 'Windows') ? "\r\n" : "\n");
113
                    $prepend = implode('', array_slice($lines, 0, $insert));
114
                    $append  = implode('', array_slice($lines, $insert));
115
116
                    $content = $prepend . $patchCode . $append;
117
                    $content = str_replace(array("\r\n", "\n"), $newline, $content);
118
119
                    fwrite($fp, $content);
120
                    fclose($fp);
121
                    echo 'Patch successfully applied';
122
                }
123
            }
124
        }
125
126
        return true;
127
    }
128
129
    /**
130
     * @return bool
131
     */
132
    public function check_auth_db()
133
    {
134
        $db    = $GLOBALS['xoopsDB'];
135
        $value = getDbValue($db, 'config', 'conf_id', "`conf_name` = 'ldap_provisionning' AND `conf_catid` = " . XOOPS_CONF_AUTH);
136
137
        return (bool)$value;
138
    }
139
140
    /**
141
     * @param $sql
142
     */
143
    public function query($sql)
144
    {
145
        $db = $GLOBALS['xoopsDB'];
146
        if (!($ret = $db->queryF($sql))) {
147
            echo $db->error();
148
        }
149
    }
150
151
    /**
152
     * @return bool
153
     */
154
    public function apply_auth_db()
155
    {
156
        $db = $GLOBALS['xoopsDB'];
157
158
        $cat = getDbValue($db, 'configcategory', 'confcat_id', "`confcat_name` ='_MD_AM_AUTHENTICATION'");
159
        if ($cat !== false && $cat != XOOPS_CONF_AUTH) {
160
            // 2.2 downgrade bug: LDAP cat is here but has a catid of 0
161
            $db->queryF('DELETE FROM ' . $db->prefix('configcategory') . " WHERE `confcat_name` ='_MD_AM_AUTHENTICATION' ");
162
            $db->queryF('DELETE FROM ' . $db->prefix('config') . " WHERE `conf_modid`=0 AND `conf_catid` = $cat");
163
            $cat = false;
164
        }
165
        if (empty($cat)) {
166
            // Insert config category ( always XOOPS_CONF_AUTH = 7 )
167
            $db->queryF(' INSERT INTO ' . $db->prefix('configcategory') . " (confcat_id,confcat_name) VALUES (7,'_MD_AM_AUTHENTICATION')");
168
        }
169
        // Insert config values
170
        $table = $db->prefix('config');
171
        $data  = array(
172
            'auth_method'              => "'_MD_AM_AUTHMETHOD', 'xoops', '_MD_AM_AUTHMETHODDESC', 'select', 'text', 1",
173
            'ldap_port'                => "'_MD_AM_LDAP_PORT', '389', '_MD_AM_LDAP_PORT', 'textbox', 'int', 2 ",
174
            'ldap_server'              => "'_MD_AM_LDAP_SERVER', 'your directory server', '_MD_AM_LDAP_SERVER_DESC', 'textbox', 'text', 3 ",
175
            'ldap_manager_dn'          => "'_MD_AM_LDAP_MANAGER_DN', 'manager_dn', '_MD_AM_LDAP_MANAGER_DN_DESC', 'textbox', 'text', 5",
176
            'ldap_manager_pass'        => "'_MD_AM_LDAP_MANAGER_PASS', 'manager_pass', '_MD_AM_LDAP_MANAGER_PASS_DESC', 'textbox', 'text', 6",
177
            'ldap_version'             => "'_MD_AM_LDAP_VERSION', '3', '_MD_AM_LDAP_VERSION_DESC', 'textbox', 'text', 7",
178
            'ldap_users_bypass'        => "'_MD_AM_LDAP_USERS_BYPASS', '" . serialize(array('admin')) . "', '_MD_AM_LDAP_USERS_BYPASS_DESC', 'textarea', 'array', 8",
179
            'ldap_loginname_asdn'      => "'_MD_AM_LDAP_LOGINNAME_ASDN', 'uid_asdn', '_MD_AM_LDAP_LOGINNAME_ASDN_D', 'yesno', 'int', 9",
180
            'ldap_loginldap_attr'      => "'_MD_AM_LDAP_LOGINLDAP_ATTR', 'uid', '_MD_AM_LDAP_LOGINLDAP_ATTR_D', 'textbox', 'text', 10",
181
            'ldap_filter_person'       => "'_MD_AM_LDAP_FILTER_PERSON', '', '_MD_AM_LDAP_FILTER_PERSON_DESC', 'textbox', 'text', 11",
182
            'ldap_domain_name'         => "'_MD_AM_LDAP_DOMAIN_NAME', 'mydomain', '_MD_AM_LDAP_DOMAIN_NAME_DESC', 'textbox', 'text', 12",
183
            'ldap_provisionning'       => "'_MD_AM_LDAP_PROVIS', '0', '_MD_AM_LDAP_PROVIS_DESC', 'yesno', 'int', 13",
184
            'ldap_provisionning_group' => "'_MD_AM_LDAP_PROVIS_GROUP', 'a:1:{i:0;s:1:\"2\";}', '_MD_AM_LDAP_PROVIS_GROUP_DSC', 'group_multi', 'array', 14",
185
            'ldap_mail_attr'           => "'_MD_AM_LDAP_MAIL_ATTR', 'mail', '_MD_AM_LDAP_MAIL_ATTR_DESC', 'textbox', 'text', 15",
186
            'ldap_givenname_attr'      => "'_MD_AM_LDAP_GIVENNAME_ATTR', 'givenname', '_MD_AM_LDAP_GIVENNAME_ATTR_DSC', 'textbox', 'text', 16",
187
            'ldap_surname_attr'        => "'_MD_AM_LDAP_SURNAME_ATTR', 'sn', '_MD_AM_LDAP_SURNAME_ATTR_DESC', 'textbox', 'text', 17");
188
        foreach ($data as $name => $values) {
189
            if (!getDbValue($db, 'config', 'conf_id', "`conf_modid`=0 AND `conf_catid`=7 AND `conf_name`='$name'")) {
190
                $this->query("INSERT INTO `$table` (conf_modid,conf_catid,conf_name,conf_title,conf_value,conf_desc,conf_formtype,conf_valuetype,conf_order) " . "VALUES ( 0,7,'$name',$values)");
191
            }
192
        }
193
        // Insert auth_method config options
194
        $id    = getDbValue($db, 'config', 'conf_id', "`conf_modid`=0 AND `conf_catid`=7 AND `conf_name`='auth_method'");
195
        $table = $db->prefix('configoption');
196
        $data  = array(
197
            '_MD_AM_AUTH_CONFOPTION_XOOPS' => 'xoops',
198
            '_MD_AM_AUTH_CONFOPTION_LDAP'  => 'ldap',
199
            '_MD_AM_AUTH_CONFOPTION_AD'    => 'ad');
200
        $this->query("DELETE FROM `$table` WHERE `conf_id`=$id");
201
        foreach ($data as $name => $value) {
202
            $this->query("INSERT INTO `$table` (confop_name, confop_value, conf_id) VALUES ('$name', '$value', $id)");
203
        }
204
205
        return true;
206
    }
207
}
208
209
$upg = new Upgrade_2014();
210
return $upg;
211