| Conditions | 20 |
| Paths | 398 |
| Total Lines | 106 |
| Code Lines | 68 |
| Lines | 34 |
| Ratio | 32.08 % |
| 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 |
||
| 105 | public function ChangePassword(\RainLoop\Account $oAccount, $sPrevPassword, $sNewPassword) |
||
| 106 | { |
||
| 107 | $bResult = false; |
||
| 108 | |||
| 109 | try |
||
| 110 | { |
||
| 111 | $sDomain = \MailSo\Base\Utils::GetDomainFromEmail($oAccount->Email()); |
||
| 112 | $sUserDn = \strtr($this->sUserDnFormat, array( |
||
| 113 | '{domain}' => $sDomain, |
||
| 114 | '{domain:dc}' => 'dc='.\strtr($sDomain, array('.' => ',dc=')), |
||
| 115 | '{email}' => $oAccount->Email(), |
||
| 116 | '{email:user}' => \MailSo\Base\Utils::GetAccountNameFromEmail($oAccount->Email()), |
||
| 117 | '{email:domain}' => $sDomain, |
||
| 118 | '{login}' => $oAccount->Login(), |
||
| 119 | '{imap:login}' => $oAccount->Login(), |
||
| 120 | '{imap:host}' => $oAccount->DomainIncHost(), |
||
| 121 | '{imap:port}' => $oAccount->DomainIncPort() |
||
| 122 | )); |
||
| 123 | |||
| 124 | $oCon = @\ldap_connect($this->sHostName, $this->iHostPort); |
||
| 125 | if ($oCon) |
||
| 126 | { |
||
| 127 | @\ldap_set_option($oCon, LDAP_OPT_PROTOCOL_VERSION, 3); |
||
| 128 | |||
| 129 | View Code Duplication | if (!@\ldap_bind($oCon, $sUserDn, $sPrevPassword)) |
|
| 130 | { |
||
| 131 | if ($this->oLogger) |
||
| 132 | { |
||
| 133 | $sError = $oCon ? @\ldap_error($oCon) : ''; |
||
| 134 | $iErrno = $oCon ? @\ldap_errno($oCon) : 0; |
||
| 135 | |||
| 136 | $this->oLogger->Write('ldap_bind error: '.$sError.' ('.$iErrno.')', |
||
| 137 | \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); |
||
| 138 | } |
||
| 139 | |||
| 140 | return false; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | else |
||
| 144 | { |
||
| 145 | return false; |
||
| 146 | } |
||
| 147 | |||
| 148 | $sSshaSalt = ''; |
||
| 149 | $sShaPrefix = '{SHA}'; |
||
| 150 | $sEncodedNewPassword = $sNewPassword; |
||
| 151 | switch (\strtolower($this->sPasswordEncType)) |
||
| 152 | { |
||
| 153 | case 'ssha': |
||
| 154 | $sSshaSalt = $this->getSalt(4); |
||
| 155 | $sShaPrefix = '{SSHA}'; |
||
| 156 | case 'sha': |
||
| 157 | switch (true) |
||
| 158 | { |
||
| 159 | default: |
||
| 160 | View Code Duplication | case \function_exists('sha1'): |
|
| 161 | $sEncodedNewPassword = $sShaPrefix.\base64_encode(\sha1($sNewPassword.$sSshaSalt, true).$sSshaSalt); |
||
| 162 | break; |
||
| 163 | View Code Duplication | case \function_exists('hash'): |
|
| 164 | $sEncodedNewPassword = $sShaPrefix.\base64_encode(\hash('sha1', $sNewPassword, true).$sSshaSalt); |
||
| 165 | break; |
||
| 166 | case \function_exists('mhash') && defined('MHASH_SHA1'): |
||
| 167 | $sEncodedNewPassword = $sShaPrefix.\base64_encode(\mhash(MHASH_SHA1, $sNewPassword).$sSshaSalt); |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | break; |
||
| 171 | case 'md5': |
||
| 172 | $sEncodedNewPassword = '{MD5}'.\base64_encode(\pack('H*', \md5($sNewPassword))); |
||
| 173 | break; |
||
| 174 | case 'crypt': |
||
| 175 | $sEncodedNewPassword = '{CRYPT}'.\crypt($sNewPassword, $this->getSalt(2)); |
||
| 176 | break; |
||
| 177 | } |
||
| 178 | |||
| 179 | $aEntry = array(); |
||
| 180 | $aEntry[$this->sPasswordField] = (string) $sEncodedNewPassword; |
||
| 181 | |||
| 182 | View Code Duplication | if (!!@\ldap_modify($oCon, $sUserDn, $aEntry)) |
|
| 183 | { |
||
| 184 | $bResult = true; |
||
| 185 | } |
||
| 186 | else |
||
| 187 | { |
||
| 188 | if ($this->oLogger) |
||
| 189 | { |
||
| 190 | $sError = $oCon ? @\ldap_error($oCon) : ''; |
||
| 191 | $iErrno = $oCon ? @\ldap_errno($oCon) : 0; |
||
| 192 | |||
| 193 | $this->oLogger->Write('ldap_modify error: '.$sError.' ('.$iErrno.')', |
||
| 194 | \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | catch (\Exception $oException) |
||
| 199 | { |
||
| 200 | if ($this->oLogger) |
||
| 201 | { |
||
| 202 | $this->oLogger->WriteException($oException, |
||
| 203 | \MailSo\Log\Enumerations\Type::WARNING, 'LDAP'); |
||
| 204 | } |
||
| 205 | |||
| 206 | $bResult = false; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $bResult; |
||
| 210 | } |
||
| 211 | |||
| 231 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.