1: <?php
2:
3: /**
4: * Dynamic login modules
5: * @package modules
6: * @subpackage dynamic_login
7: */
8:
9: if (!defined('DEBUG_MODE')) { die(); }
10:
11: require_once APP_PATH.'modules/nux/modules.php';
12:
13: /**
14: * @subpackage dynamic_login/lib
15: */
16: class Hm_Discover_Services {
17:
18: private $timeout = 1;
19: private $host;
20: private $domain = false;
21: private $server = false;
22: private $base_domain = false;
23: private $port = false;
24: private $tls = false;
25: private $type = false;
26: private $smtp_port = false;
27: private $smtp_tls = false;
28: private $smtp_ports = array(587, 465, 25);
29: private $imap_ports = array(993, 143);
30: private $transports = array('tls://' => true, 'tcp://' => false);
31: private $service = false;
32: private $mail_pre = '';
33: private $smtp_pre = '';
34: private $host_pre = '';
35: private $dyn_host = false;
36: private $dyn_user = false;
37:
38: public function __construct($email, $config, $service=false, $host=false) {
39: $this->service = $service;
40: $this->host = $host;
41: $this->domain = $this->get_domain($email, $host);
42: $this->mail_pre = $config['mail_pre'];
43: $this->smtp_pre = $config['smtp_pre'];
44: $this->host_pre = $config['host_pre'];
45: $this->dyn_host = $config['host'];
46: $this->dyn_user = $config['user'];
47: }
48:
49: public function get_host_details() {
50: $details = $this->service_check();
51: if (count($details) > 0) {
52: return $details;
53: }
54: return $this->manual_host_check();
55: }
56:
57: private function get_domain($email, $host) {
58: $domain = 'localhost';
59: if ($this->dyn_host) {
60: if (mb_substr($host, 0, mb_strlen($this->host_pre)) == $this->host_pre) {
61: $domain = mb_substr($host, mb_strlen($this->host_pre));
62: }
63: }
64: elseif ($this->dyn_user || $domain == 'localhost') {
65: if (mb_strpos($email, '@')) {
66: $parts = explode('@', $email);
67: $domain = $parts[1];
68: }
69: }
70: $this->base_domain = $domain;
71: if ($this->mail_pre) {
72: $domain = sprintf('%s.%s', $this->mail_pre, $domain);
73: }
74: return $domain;
75: }
76:
77: private function manual_host_check() {
78: if (!$this->domain) {
79: return array();
80: }
81: $this->get_mx_host();
82: if (!$this->server) {
83: return array();
84: }
85: $this->check_imap_ports();
86: if (!$this->port) {
87: return array();
88: }
89: $this->check_ports($this->smtp_ports, true);
90: return $this->host_details();
91: }
92:
93: private function service_check() {
94: if ($this->service) {
95: return Nux_Quick_Services::details($this->service);
96: }
97: return array();
98: }
99:
100: private function host_details() {
101: $smtp_server = $this->server;
102: if ($this->smtp_pre) {
103: $smtp_server = sprintf('%s.%s', $this->smtp_pre, $this->server);
104: }
105: return array(
106: 'type' => $this->type,
107: 'port' => $this->port,
108: 'server' => $this->server,
109: 'name' => $this->domain,
110: 'tls' => $this->tls,
111: 'smtp' => array(
112: 'port' => $this->smtp_port,
113: 'tls' => $this->smtp_tls,
114: 'server' => $smtp_server
115: )
116: );
117: }
118:
119: private function get_mx_host() {
120: if ($this->dyn_host) {
121: $this->server = $this->domain;
122: }
123: else {
124: getmxrr($this->domain, $hosts);
125: if (is_array($hosts) && count($hosts) > 0) {
126: $this->server = array_shift($hosts);
127: }
128: }
129: }
130:
131: private function check_imap_ports() {
132: $this->check_ports($this->imap_ports);
133: if ($this->port) {
134: $this->type = 'imap';
135: }
136: }
137:
138: private function check_ports($ports, $smtp=false) {
139: foreach ($ports as $port) {
140: foreach ($this->transports as $trans => $tls) {
141: $fp = $this->connect($port, $trans);
142: if ($fp) {
143: fclose($fp);
144: if ($smtp) {
145: $this->smtp_port = $port;
146: $this->smtp_tls = $tls;
147: }
148: else {
149: $this->port = $port;
150: $this->tls = $tls;
151: }
152: break 2;
153: }
154: }
155: }
156: }
157:
158: private function connect($port, $trans) {
159: $ctx = stream_context_create();
160: stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
161: stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
162: return @stream_socket_client($trans.$this->server.':'.$port, $errn,
163: $errs, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);
164: }
165: }
166: