WML000.inc 12.4 KB
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
<?php
//*****************************************************************************
//* 
//* プログラム名:Webメール
//* プログラムID:WML000.inc
//* 機能			:メール解析系関数群
//* 作成者		:
//* 
//*****************************************************************************

/*
 * pop3-class.php By TOMO (2002/07/06) 
 *
 * [Version] : 1.1.0 (2002/07/06)
 * [URL]     : http://www.spencernetwork.org/
 * [E-MAIL]  : groove@spencernetwork.org
 *
 * pop3-class.php is free but without any warranty.
 * use this script at your own risk.
 *
 */

/* Interface 

[ Constractor ]

    pop3(string server, string user, string pass [, int port = 110])

[ Property ]

    bool apop
    bool debug
    int time_out
    string server 
    int port
    string user
    string pass

[ Method ]

    bool open(void)
    bool close(void)
    array get_stat(void)
    array get_list(int num = 0)
    array get_uidl(int num = 0)
    bool dele(int num)
    string top(int num, int line)
    string head(int num)
    string retr(int num)
    bool noop(void)
    bool rset(void)

*/

class pop3
{
	/* Public variables */
	var $apop;      // use APOP
	var $debug;     // print debug message
	var $time_out;  // fsockopen() time-out
	var $server;    // pop3 server
	var $port;      // pop3 port number
	var $user;      // login user
	var $pass;      // login password

	/* Private variables */
	var $_sock;

	function pop3($server, $user, $pass, $port = 110)
	{
		$this->apop     = FALSE;
		$this->debug    = FALSE;
		$this->time_out = 30;
		$this->server   = $server;
		$this->port     = $port;
		$this->user     = $user;
		$this->pass     = $pass;

		$this->_sock    = FALSE;
	}

	/* Public functions */
	function open()
	{
		if ($this->_sock) $this->close();

		$this->_debug("Trying to ".$this->server.":".$this->port." ...\n");
		$this->_sock = @fsockopen($this->server, $this->port, $errno, $errstr, $this->time_out);
		if (!$this->_sock) {
			$this->_debug("Error: Could not connect to ".$this->server."\n");
			$this->_debug("Error: ".$errstr." (".$errno.")\n");
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			$this->_debug("Error: Could not connect to ".$this->server."\n");
			return FALSE;
		}

		if (ereg("<.+>", $resp, $match)) {
			$time_stamp = $match[0];
		} else {
			$time_stamp = '';
		}

		$this->_debug("Connected to ".$this->server."\n");

		if (!$this->_login($time_stamp)) {
			$this->_debug("Error: Login failed\n");
			return FALSE;
		}
		$this->_debug("Login succeeded\n");
		return TRUE;
	}

	function get_stat()
	{
		if (!$this->_putcmd('STAT')) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}
		list($num, $octet) = explode(' ', $resp);
		return array($num, $octet);
	}

	function get_list($num = 0)
	{
		return $this->_get_list('LIST', $num);
	}

	function dele($num)
	{
		if (!$this->_putcmd('DELE', $num)) {
			return FALSE;
		}
		return $this->_ok($resp);
	}

	function get_uidl($num = 0)
	{
		return $this->_get_list('UIDL', $num);
	}

	function top($num, $line)
	{
		if (!$this->_putcmd('TOP', $num.' '.$line)) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}
		return $this->_getresp();
	}

	function head($num)
	{
		return $this->top($num, 0);
	}

	function retr($num)
	{
		if (!$this->_putcmd('RETR', $num)) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}
		return $this->_getresp();
	}

	function noop()
	{
		if (!$this->_putcmd('NOOP')) {
			return FALSE;
		}
		return $this->_ok($resp);
	}

	function rset()
	{
		if (!$this->_putcmd('RSET')) {
			return FALSE;
		}
		return $this->_ok($resp);
	}

	function close()
	{
		if (!$this->_sock) {
			$this->_debug("Error: Socket is not opened\n");
			return FALSE;
		}

		if (!$this->_putcmd('QUIT')) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}

		$ret = fclose($this->_sock);
		if ($ret) {
			$this->_debug("Disconnected from remote host\n");
			return TRUE;
		} else {
			$this->_debug("Error: Could not disconnect from remote host\n");
			return FALSE;
		}
	}

	/* Private Functions */

	function _login($time_stamp)
	{
		if ($this->apop) {
			return $this->_login_apop($time_stamp);
		} else {
			return $this->_login_pop();
		}
	}

	function _login_pop()
	{
		if (!$this->_putcmd('USER', $this->user)) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}

		if (!$this->_putcmd('PASS', $this->pass)) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}

		return TRUE;
	}

	function _login_apop($time_stamp)
	{
		$digest = md5($time_stamp.$this->pass);

		if (!$this->_putcmd('APOP', $this->user." ".$digest)) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}

		return TRUE;
	}

	function _putcmd($cmd, $arg = '')
	{
		if (!$this->_sock) {
			$this->_debug("Error: Socket is not opened\n");
			return FALSE;
		}

		if ($arg != '') {
			$cmd = $cmd.' '.$arg;
		}

		if (!fputs($this->_sock, $cmd."\r\n")) {
			$this->_debug("Error: Could not put command $cmd\n");
			return FALSE;
		}

		$this->_debug('> '.$cmd."\n");

		return TRUE;
	}

	function _ok(&$resp)
	{
		if (!$this->_sock) {
			$this->_debug("Error: Socket is not opened\n");
			return FALSE;
		}

		$line = trim(fgets($this->_sock, 512));
		$this->_debug('< ' . $line . "\n");

		$tmp = split(' ', $line, 2);
		if (isset($tmp[1])) {
			$resp = $tmp[1];
		} else {
			$resp = '';
		}

		if (substr($line, 0, 1) != '+') {
			return FALSE;
		}

		return TRUE;
	}

	function _getresp()
	{
		if (!$this->_sock) {
			$this->_debug("Error: Socket is not opened\n");
			return FALSE;
		}

		$response = '';
		$line = '';
		while ($line != ".\r\n") {
			$line = ereg_replace("^\\.\\.", '.', $line);
			$response .= $line;
			$line = fgets($this->_sock, 512);
		}
		$this->_debug(str_replace("\r\n", "\n", $response));
		return $response;
	}

	function _debug($message)
	{
		if ($this->debug) {
			echo $message;
		}
	}

	function _get_list($cmd, $num = 0)
	{
		if ($num < 1) {
			$arg = '';
		} else {
			$arg = $num;
		}

		if (!$this->_putcmd($cmd, $arg)) {
			return FALSE;
		}
		if (!$this->_ok($resp)) {
			return FALSE;
		}

		if ($num < 1) {
			$resp = trim($this->_getresp());
		}

		if ($resp == '') {
			return array('');
		}

		$data = explode("\r\n", $resp );
		reset($data);
		while (list($key, $value) = each($data)) {
			list($no, $uid) = explode(' ', $value);
			$list[$no] = $uid;
		}
		return $list;
	}
}
require_once 'jcode.phps';
define("", "");

function auth($userid, $passwd, $realem = '', $crypt = 1){

	if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
		@header("WWW-Authenticate: Basic realm=\"".$realem."\"");
		@header("HTTP/1.0 401 Unauthorized");
		return FALSE;
	}
	$auth_id = $_SERVER['PHP_AUTH_USER'];
	switch ($crypt) {
		case 1:
			$auth_pw = md5($_SERVER['PHP_AUTH_PW']);
			break;
		case 2:
			$auth_pw = crypt($_SERVER['PHP_AUTH_PW'], substr($passwd, 0, 2));
			break;
		default:
			$auth_pw = $_SERVER['PHP_AUTH_PW'];
			break;
	}

	if (strcmp($auth_id, $userid) == 0 && strcmp($auth_pw, $passwd) == 0) {
		return TRUE;
	}

	@header("WWW-Authenticate: Basic realm=\"".$realem."\"");
	@header("HTTP/1.0 401 Unauthorized");
	return FALSE;
}

function link_url($str){
	$regex = "((https?://|ftp://)([0-9A-Za-z:_?./~%;()=+#-]|&amp;)+)";
	$str = eregi_replace($regex, "<a href=\"\\1\" target=\"_blank\">\\1</a>", $str);
	$regex_mail = "((mailto:)?([0-9A-Za-z._-]+@[0-9A-Za-z.-]+))";
	$str = eregi_replace($regex_mail, "<a href=\"mailto:\\3\">\\1</a>", $str);
	return $str;
}

function jstrhead($str, $len){
	$b = unpack('C*', $str);
	$n = count($b);
	$h = '';

	for ($i = 1, $l = 0; $i <= $n; ++$i) {
		if ($b[$i] >= 0x80) {  //Japanese
				if ($b[$i] != 0x8E) {				//EUC-JP
					++$l;
				}
				$c = chr($b[$i]).chr($b[++$i]);
		} else {  //ASCII
			$c = chr($b[$i]);
		}
		if (++$l > $len) break;
		$h .= $c;
	}

	return $h;
}

function decode_body($body, $enc){
	switch (strtolower($enc)) {
		case 'quoted-printable':
			$body = quoted_printable_decode($body);
			break;
		case 'base64':
			$body = base64_decode($body);
			break;
		default;
			break;
	}
	return $body;
}

function decode_mimeheader($str, $charset_to = 0){
	// combine multiple-line
	$ret_str = ereg_replace("\r\n[\t ]+", "", $str);

	// regex for encoded-word
	$expr = "^.*(=\\?([^?]+)\\?([BbQq])\\?([^?]*)\\?=).*$";

	while (ereg($expr, $ret_str, $match)) {

		// encoding
		switch (strtolower($match[3])) {
			case 'b':
				$dec_data = base64_decode($match[4]);
				break;
			case 'q':
				$dec_data = quoted_printable_decode($match[4]);
				break;
			default:
				$dec_data = $match[4];
				break;
		}

		// charset
		$charset_from = check_jcode_encoding($match[2]);

		// convert charet
		$dec_data = JcodeConvert($dec_data, $charset_from, $charset_to);

		// replace encoded-word
		$ret_str = str_replace($match[1], $dec_data, $ret_str);
	}

	return $ret_str;
}

function color_mail($body, $header = "")
{
	$body   = htmlspecialchars($body, ENT_QUOTES);
	$header = htmlspecialchars($header, ENT_QUOTES);

	$line_body   = split("\r?\n", $body);
	$line_header = split("\r?\n", $header);

	$line_body_max   = count($line_body);
	$line_header_max = count($line_header);

	for ($cnt = 0; $cnt < $line_header_max; ++$cnt) {
		$line_header[$cnt] = _color_header($line_header[$cnt]);
	}

	for ($cnt = 0; $cnt < $line_body_max; ++$cnt) {
		$line_body[$cnt] = _color_body($line_body[$cnt]);
	}

	$str = implode("\n", $line_header) . "\n\n" . implode("\n", $line_body);
	$str = _color_signature($str);

	return $str;
}

function is_valid_uid($uid)
{

	$len = strlen($uid);
	if ($len < 1 || $len > 70) {
		return FALSE;
	}

	if (ereg("[^\x21-\x7E]", $uid)) {
		return FALSE;
	}

	return TRUE;
}

function format_date($date)
{
	return date('Y/m/d&\\nb\\sp;H:i', strtotime($date));
}
function format_date2($date)
{
	return date('YmdHi', strtotime($date));
}

function format_size($size)
{
	return sprintf('%0.1f', $size / 1024).'KB';
}

function get_name($str)
{
	if (ereg('(.+)<.+@.+>.*', $str, $match)) {
		if (ereg('.*"([^"]+)".*', $match[1], $match2)) {
			return $match2[1];
		}
		return $match[1];
	}

	if (ereg('.+\\((.+)\\)', $str, $match)) {
		return $match[1];
	}

	if (ereg('<?([0-9A-Za-z._-]+)@[0-9A-Za-z.-]+>?', $str, $match)) {
		return $match[1];
	}

	return $str;
}

function parse_ctype($ctype)
{
	$type    = '';
	$subtype = '';
	$value = parse_header_value($ctype);
	list($type, $subtype) = split("/", $value['value'], 2);
	$ret['type']    = $type;
	$ret['subtype'] = $subtype;
	$ret['param']   = $value['param'];
	return $ret;
}

function parse_header_value($header)
{
	$v = split(";", $header, 2);
	$h['value'] = trim($v[0]);
	if (!isset($v[1])) {
		return $h;
	}

	$param = split(";", $v[1]);
	$n = count($param);
	for ($i = 0; $i < $n; ++$i) {
		$param_name  = '';
		$param_value = '';
		list($param_name, $param_value) = split("=", $param[$i], 2);
		$h['param'][strtolower(trim($param_name))]
			= ereg_replace('^"([^"]+)"$', "\\1", trim($param_value));
	}

	return $h;
}

function parse_header($header)
{
	$header = trim(ereg_replace("[\r\n]+[\t ]+", "", $header));
	$array_header = split("[\r\n]+", $header);
	$header_num = count($array_header);
	for ($i = 0; $i < $header_num; ++$i) {
		$header_name  = '';
		$header_value = '';
		list($header_name, $header_value) = split(":", $array_header[$i], 2);
		$header_list[strtolower($header_name)] = trim($header_value);
	}
	return $header_list;
}

//*********************
//  Private Functions
//*********************

function _color_body($str)
{
	$quote = "(&gt; ?)";
	for ($i = 5; $i > 0; --$i) {
		$regex = "(^".$quote."{".$i."}.*$)";
		$str   = ereg_replace($regex, "<font class=\"quot".$i."\">\\1</font>", $str);
	}

	$str = _color_option($str);

	return $str;
}

function _color_header($str)
{
	$regex = "(^[^: \t]*[: \t])(.+$)";
	return ereg_replace($regex, "<font class=\"header_all_name\">\\1</font><font class=\"header_all\">\\2</font>", $str);
}

function _color_option($str)
{
	$regex = "(^#.*)"; // Starting with "#"
	return ereg_replace($regex, "<font class=\"option\">\\1</font>", $str);
}

function _color_signature($str)
{
	$regex = "(\r?\n-- \r?\n.+$)"; // See draft-ietf-usefor-article
	return ereg_replace($regex, "<font class=\"signature\">\\1</font>", $str);
}

function check_jcode_encoding($str_encoding)
{
	switch (strtolower($str_encoding)) {
		case 'euc-jp':
			$jc_encoding = 1;
			break;
		case 'shift_jis':
			$jc_encoding = 2;
			break;
		case 'iso-2022-jp':
			$jc_encoding = 3;
			break;
		case 'utf-8':
			global $table_utf8_jis;
			include_once('code_table.ucs2jis');
			$jc_encoding = 4;
			break;
		default:
			$jc_encoding = 0;
			break;
	}
	return $jc_encoding;
}

?>