Conditions | 41 |
Paths | > 20000 |
Total Lines | 137 |
Code Lines | 90 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
85 | public static function validate() |
||
86 | { |
||
87 | global $xoopsUser; |
||
88 | |||
89 | $args = func_get_args(); |
||
90 | $args_num = func_num_args(); |
||
91 | |||
92 | $user = null; |
||
93 | $uname = null; |
||
94 | $email = null; |
||
95 | $pass = null; |
||
96 | $vpass = null; |
||
97 | |||
98 | switch ($args_num) { |
||
99 | case 1: |
||
100 | $user = $args[0]; |
||
101 | break; |
||
102 | case 2: |
||
103 | list($uname, $email) = $args; |
||
104 | break; |
||
105 | case 3: |
||
106 | list($user, $pass, $vpass) = $args; |
||
107 | break; |
||
108 | case 4: |
||
109 | list($uname, $email, $pass, $vpass) = $args; |
||
110 | break; |
||
111 | default: |
||
112 | return false; |
||
113 | } |
||
114 | if (is_object($user)) { |
||
115 | $uname = $user->getVar('uname', 'n'); |
||
116 | $email = $user->getVar('email', 'n'); |
||
117 | } |
||
118 | /** @var XoopsConfigHandler $config_handler */ |
||
119 | $config_handler = xoops_getHandler('config'); |
||
120 | $xoopsConfigUser = $config_handler->getConfigsByCat(XOOPS_CONF_USER); |
||
121 | |||
122 | xoops_loadLanguage('user'); |
||
123 | $myts = \MyTextSanitizer::getInstance(); |
||
|
|||
124 | |||
125 | $xoopsUser_isAdmin = is_object($xoopsUser) && $xoopsUser->isAdmin(); |
||
126 | $stop = ''; |
||
127 | // Invalid email address |
||
128 | if (!checkEmail($email)) { |
||
129 | $stop .= _US_INVALIDMAIL . '<br>'; |
||
130 | } |
||
131 | if (strrpos($email, ' ') > 0) { |
||
132 | $stop .= _US_EMAILNOSPACES . '<br>'; |
||
133 | } |
||
134 | // Check forbidden email address if current operator is not an administrator |
||
135 | if (!$xoopsUser_isAdmin) { |
||
136 | foreach ($xoopsConfigUser['bad_emails'] as $be) { |
||
137 | if (!empty($be) && preg_match('/' . $be . '/i', $email)) { |
||
138 | $stop .= _US_INVALIDMAIL . '<br>'; |
||
139 | break; |
||
140 | } |
||
141 | } |
||
142 | } |
||
143 | $uname = xoops_trim($uname); |
||
144 | switch ($xoopsConfigUser['uname_test_level']) { |
||
145 | case 0: |
||
146 | // strict |
||
147 | $restriction = '/[^a-zA-Z0-9\_\-]/'; |
||
148 | break; |
||
149 | case 1: |
||
150 | // medium |
||
151 | $restriction = '/[^a-zA-Z0-9\_\-\<\>\,\.\$\%\#\@\!\\\'\']/'; |
||
152 | break; |
||
153 | case 2: |
||
154 | // loose |
||
155 | $restriction = '/[\000-\040]/'; |
||
156 | break; |
||
157 | } |
||
158 | if (empty($uname) || preg_match($restriction, $uname)) { |
||
159 | $stop .= _US_INVALIDNICKNAME . '<br>'; |
||
160 | } |
||
161 | // Check uname settings if current operator is not an administrator |
||
162 | if (!$xoopsUser_isAdmin) { |
||
163 | if (strlen($uname) > $xoopsConfigUser['maxuname']) { |
||
164 | $stop .= sprintf(_US_NICKNAMETOOLONG, $xoopsConfigUser['maxuname']) . '<br>'; |
||
165 | } |
||
166 | if (strlen($uname) < $xoopsConfigUser['minuname']) { |
||
167 | $stop .= sprintf(_US_NICKNAMETOOSHORT, $xoopsConfigUser['minuname']) . '<br>'; |
||
168 | } |
||
169 | foreach ($xoopsConfigUser['bad_unames'] as $bu) { |
||
170 | if (!empty($bu) && preg_match('/' . $bu . '/i', $uname)) { |
||
171 | $stop .= _US_NAMERESERVED . '<br>'; |
||
172 | break; |
||
173 | } |
||
174 | } |
||
175 | /** |
||
176 | * if (strrpos($uname, ' ') > 0) { |
||
177 | * $stop .= _US_NICKNAMENOSPACES . '<br>'; |
||
178 | * } |
||
179 | */ |
||
180 | } |
||
181 | /** @var XoopsMySQLDatabase $xoopsDB */ |
||
182 | $xoopsDB = XoopsDatabaseFactory::getDatabaseConnection(); |
||
183 | // Check if uname/email already exists if the user is a new one |
||
184 | $uid = is_object($user) ? $user->getVar('uid') : 0; |
||
185 | $sql = 'SELECT COUNT(*) FROM `' . $xoopsDB->prefix('users') . '` WHERE `uname` = ' . $xoopsDB->quote(addslashes($uname)) . (($uid > 0) ? " AND `uid` <> {$uid}" : ''); |
||
186 | $result = $xoopsDB->query($sql); |
||
187 | if (!$xoopsDB->isResultSet($result)) { |
||
188 | throw new \RuntimeException( |
||
189 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
190 | ); |
||
191 | } |
||
192 | list($count) = $xoopsDB->fetchRow($result); |
||
193 | if ((int)$count > 0) { |
||
194 | $stop .= _US_NICKNAMETAKEN . '<br>'; |
||
195 | } |
||
196 | $sql = 'SELECT COUNT(*) FROM `' . $xoopsDB->prefix('users') . '` WHERE `email` = ' . $xoopsDB->quote(addslashes($email)) . (($uid > 0) ? " AND `uid` <> {$uid}" : ''); |
||
197 | $result = $xoopsDB->query($sql); |
||
198 | if (!$xoopsDB->isResultSet($result)) { |
||
199 | throw new \RuntimeException( |
||
200 | \sprintf(_DB_QUERY_ERROR, $sql) . $xoopsDB->error(), E_USER_ERROR |
||
201 | ); |
||
202 | } |
||
203 | list($count) = $xoopsDB->fetchRow($result); |
||
204 | if ((int)$count > 0) { |
||
205 | $stop .= _US_EMAILTAKEN . '<br>'; |
||
206 | } |
||
207 | // If password is not set, skip password validation |
||
208 | if ($pass === null && $vpass === null) { |
||
209 | return $stop; |
||
210 | } |
||
211 | |||
212 | if (!isset($pass) || $pass == '' || !isset($vpass) || $vpass == '') { |
||
213 | $stop .= _US_ENTERPWD . '<br>'; |
||
214 | } |
||
215 | if (isset($pass) && ($pass != $vpass)) { |
||
216 | $stop .= _US_PASSNOTSAME . '<br>'; |
||
217 | } elseif (($pass != '') && (strlen($pass) < $xoopsConfigUser['minpass'])) { |
||
218 | $stop .= sprintf(_US_PWDTOOSHORT, $xoopsConfigUser['minpass']) . '<br>'; |
||
219 | } |
||
220 | |||
221 | return $stop; |
||
222 | } |
||
347 |