summaryrefslogtreecommitdiff
path: root/final/app/include/ludo/auth.inc.php
blob: 516bd45765e0d32664463ed63b18dd500419617a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
 * Collection de fonctions pour une authentification externe POP3
 */

// Constantes d'erreur
define("ERR_POP3_AUTH_BADSOCK", -1);
define("ERR_POP3_AUTH_SERVERNACK", -2);
define("ERR_POP3_AUTH_BADUSER", -3);
define("ERR_POP3_AUTH_BADPASS", -4);
define("ERR_BAD_PARAMS", -100);

// Fonction d'authentification pop3
function pop3_auth_simple($mail, $upw)
{
	//~ echo "DEBUG : function pop3_auth_simple($mail, $upw)\n";
	//~ if ( ($ret = ereg ( "(^.*)@([[:alnum:]]+\.[[:alnum:]]+)", $mail, $re_tokens ) ) )
	if ( ($ret = ereg ( "(^.*)@(.+)", $mail, $re_tokens ) ) )
	{
		/* $re_tokens	[]
		 *			[0] : email complet
		 *			[1] : nom_mail
		 *			[2] : serveur_mail
		 */
		$server_mail=strtolower($re_tokens[2]);
		include('config.inc.php');
		
		if ( isset($CONFIG['AUTH']['POP']['SERVERS'][$server_mail]) )
		{
			$srv_conf = $CONFIG['AUTH']['POP']['SERVERS'][$server_mail];
			if ( is_array($srv_conf) )
			{
				$server=$srv_conf['subdomain'];
				$port=$srv_conf['port'];
				if ( $srv_conf['username_is_full_mail'] )
					{ $user = $mail; } else { $user = $re_tokens[1]; }
				return pop3_auth ($server, $port, $user, $upw);
			}
		}
		//~ else { print_r($CONFIG); echo "\nDEBUG : \$server_mail==$server_mail\n";}
	}//else echo "Pb EREG\n";

	/* DEBUG
	echo $mail . "\n" . $ret . "\n";;
	print_r($re_tokens);
	*/
	return ERR_BAD_PARAMS;
}

function pop3_auth ($server, $port, $user, $upw)
{
	//~ echo "function pop3_auth ($server, $port, $user, $upw)\n";

	//echo "DEBUG : fsockopen($server, $port, $errno, $errstr, 10);";
	$sock = @fsockopen($server, $port, $errno, $errstr, 10);
	if ( $sock === false)
	{
		return ERR_POP3_AUTH_BADSOCK;
	}

	//stream_set_timeout ( $sock, 1, 0 );
	
	fputs($sock, "user $user\r\n");

	if ( pop3_GetAndTestReply($sock) === false )
	{
		return ERR_POP3_AUTH_BADUSER;
	}

	fputs($sock, "pass $upw\r\n");

	if ( pop3_GetAndTestReply($sock) === false )
	{
		return ERR_POP3_AUTH_BADPASS;
	}

	fputs($sock, "quit\r\n");
	
	return 0;
}

// Fonction interne testant les r&eacute;ponses du serveur POP3
function pop3_GetAndTestReply($sock)
{
	$reply = fgets($sock, 128);
	echo $reply . "\n";
	return ( substr($reply, 0, 4) == "+OK " );
}
?>