| Conditions | 20 |
| Paths | 100 |
| Total Lines | 69 |
| Code Lines | 48 |
| 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 |
||
| 37 | function authenticate($username, $passwd, $passwd_type='text') |
||
| 38 | { |
||
| 39 | unset($passwd_type); // not used but required by function signature |
||
| 40 | |||
| 41 | switch ($GLOBALS['egw_info']['server']['mail_login_type']) |
||
| 42 | { |
||
| 43 | case 'vmailmgr': |
||
| 44 | $username = $username . '@' . $GLOBALS['egw_info']['server']['mail_suffix']; |
||
| 45 | break; |
||
| 46 | case 'email': |
||
| 47 | $username = $GLOBALS['egw']->accounts->id2name($username, 'account_email'); |
||
| 48 | break; |
||
| 49 | case 'uidNumber': |
||
| 50 | $username = 'u'.$GLOBALS['egw']->accounts->name2id($username); |
||
| 51 | break; |
||
| 52 | } |
||
| 53 | |||
| 54 | list($host, $port) = explode(':', $GLOBALS['egw_info']['server']['mail_server']); |
||
| 55 | |||
| 56 | // use Horde_Imap_Client by default, to not require PHP imap extension anymore |
||
| 57 | if (class_exists('Horde_Imap_Client_Socket') && !in_array($GLOBALS['egw_info']['server']['mail_server_type'], array('pop', 'pops'))) |
||
| 58 | { |
||
| 59 | $imap = new Horde_Imap_Client_Socket(array( |
||
| 60 | 'username' => $username, |
||
| 61 | 'password' => $passwd, |
||
| 62 | 'hostspec' => $host, |
||
| 63 | 'port' => $port ? $port : ($GLOBALS['egw_info']['server']['mail_server_type'] == 'imaps' ? 993 : 143), |
||
| 64 | 'secure' => $GLOBALS['egw_info']['server']['mail_server_type'] == 'imaps' ? 'ssl' : 'tls', |
||
| 65 | )); |
||
| 66 | try { |
||
| 67 | $imap->login(); |
||
| 68 | $mailauth = true; |
||
| 69 | $imap->logout(); |
||
| 70 | } |
||
| 71 | catch(Horde_Imap_Client_Exception $e) { |
||
| 72 | // throw everything but authentication failed as exception |
||
| 73 | if ($e->getCode() != Horde_Imap_Client_Exception::LOGIN_AUTHENTICATIONFAILED) throw $e; |
||
| 74 | |||
| 75 | $mailauth = false; |
||
| 76 | } |
||
| 77 | //error_log(__METHOD__."('$username', \$passwd) checked via Horde code returning ".array2string($mailauth)); |
||
| 78 | } |
||
| 79 | else |
||
| 80 | { |
||
| 81 | check_load_extension('imap', true); |
||
| 82 | |||
| 83 | switch ($GLOBALS['egw_info']['server']['mail_server_type']) |
||
| 84 | { |
||
| 85 | case 'imap': |
||
| 86 | default: |
||
| 87 | if (!isset($port)) $port = 143; |
||
| 88 | $mailauth = imap_open('{'.$host.':'.$port.'/imap/novalidate-cert}INBOX', $username , $passwd); |
||
| 89 | break; |
||
| 90 | case 'imaps': |
||
| 91 | if (!isset($port)) $port = 993; |
||
| 92 | $mailauth = imap_open('{'.$host.'/imap/ssl/novalidate-cert:'.$port.'}INBOX', $username , $passwd); |
||
| 93 | break; |
||
| 94 | case 'pop3': |
||
| 95 | if (!isset($port)) $port = 110; |
||
| 96 | $mailauth = imap_open('{'.$host.'/pop3/novalidate-cert:'.$port.'}INBOX', $username , $passwd); |
||
| 97 | break; |
||
| 98 | case 'pop3s': |
||
| 99 | if (!isset($port)) $port = 995; |
||
| 100 | $mailauth = imap_open('{'.$host.'/pop3/ssl/novalidate-cert:'.$port.'}INBOX', $username , $passwd); |
||
| 101 | break; |
||
| 102 | } |
||
| 103 | if ($mailauth) imap_close($mailauth); |
||
|
|
|||
| 104 | } |
||
| 105 | return !!$mailauth; |
||
| 106 | } |
||
| 123 |