functions_attach.php

来自「这是php编的论坛的原代码」· PHP 代码 · 共 852 行 · 第 1/2 页

PHP
852
字号
<?php
/***************************************************************************
 *                            functions_attach.php
 *                            -------------------
 *   begin                : Friday, Mar 29, 2002
 *   copyright            : (C) 2002 Meik Sievertsen
 *   email                : acyd.burn@gmx.de
 *
 *   $Id: functions_attach.php,v 1.1.1.1 2003/02/11 22:27:31 wei.gao Exp $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *
 ***************************************************************************/

//
// All Attachment Functions needed everywhere
//

//
// A simple dectobase64 function
//
function base64_pack($number) 
{ 
	$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
	$base = strlen($chars);

	if ($number > 4096)
	{
		return;
	}
	else if ($number < $base)
	{
		return $chars[$number];
	}
	
	$hexval = '';
	
	while ($number > 0) 
	{ 
		$remainder = $number%$base;
	
		if ($remainder < $base)
		{
			$hexval = $chars[$remainder] . $hexval;
		}

		$number = floor($number/$base); 
	} 

	return $hexval; 
}

//
// base64todec function
//
function base64_unpack($string)
{
	$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
	$base = strlen($chars);

	$length = strlen($string); 
	$number = 0; 

	for($i = 1; $i <= $length; $i++)
	{ 
		$pos = $length - $i; 
		$operand = strpos($chars, substr($string,$pos,1));
		$exponent = pow($base, $i-1); 
		$decValue = $operand * $exponent; 
		$number += $decValue; 
	} 

	return $number; 
}

//
// Per Forum based Extension Group Permissions (Encode Number) -> Theoretically up to 158 Forums saveable. :)
// We are using a base of 64, but splitting it to one-char and two-char numbers. :)
//
function auth_pack($auth_array)
{
	$one_char_encoding = '#';
	$two_char_encoding = '.';
	$one_char = FALSE;
	$two_char = FALSE;
	$auth_cache = '';
	
	for ($i = 0; $i < count($auth_array); $i++)
	{
		$val = base64_pack(intval($auth_array[$i]));
		if ((strlen($val) == 1) && ($one_char == FALSE))
		{
			$auth_cache .= $one_char_encoding;
			$one_char = TRUE;
		}
		else if ((strlen($val) == 2) && ($two_char == FALSE))
		{		
			$auth_cache .= $two_char_encoding;
			$two_char = TRUE;
		}
		
		$auth_cache .= $val;
	}

	return $auth_cache;
}

//
// Reverse the auth_pack process
//
function auth_unpack($auth_cache)
{
	$one_char_encoding = '#';
	$two_char_encoding = '.';

	$auth = array();
	$auth_len = 1;
	
	for ($pos = 0; $pos < strlen($auth_cache); $pos+=$auth_len)
	{
		$forum_auth = substr($auth_cache, $pos, 1);
		if ($forum_auth == $one_char_encoding)
		{
			$auth_len = 1;
			continue;
		}
		else if ($forum_auth == $two_char_encoding)
		{
			$auth_len = 2;
			$pos--;
			continue;
		}
		
		$forum_auth = substr($auth_cache, $pos, $auth_len);
		$forum_id = base64_unpack($forum_auth);
		$auth[] = intval($forum_id);
	}
	return $auth;
}

//
// Used for determining if Forum ID is authed, please use this Function on all Posting Screens
//
function is_forum_authed($auth_cache, $check_forum_id)
{
	$one_char_encoding = '#';
	$two_char_encoding = '.';

	if (trim($auth_cache) == '')
	{
		return (TRUE);
	}

	$auth = array();
	$auth_len = 1;
	
	for ($pos = 0; $pos < strlen($auth_cache); $pos+=$auth_len)
	{
		$forum_auth = substr($auth_cache, $pos, 1);
		if ($forum_auth == $one_char_encoding)
		{
			$auth_len = 1;
			continue;
		}
		else if ($forum_auth == $two_char_encoding)
		{
			$auth_len = 2;
			$pos--;
			continue;
		}
		
		$forum_auth = substr($auth_cache, $pos, $auth_len);
		$forum_id = base64_unpack($forum_auth);
		if (intval($forum_id) == intval($check_forum_id))
		{
			return (TRUE);
		}
	}
	return (FALSE);
}

//
// Init FTP Session
//
function attach_init_ftp($mode = FALSE)
{
	global $lang, $attach_config;

	$server = ( $attach_config['ftp_server'] == '' ) ? 'localhost' : $attach_config['ftp_server'];
	
	$ftp_path = ( $mode == MODE_THUMBNAIL ) ? $attach_config['ftp_path'] . '/' . THUMB_DIR : $attach_config['ftp_path'];

	$conn_id = @ftp_connect($server);

	if (!$conn_id)
	{
		message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_connect'], $server));
	}

	$login_result = @ftp_login($conn_id, $attach_config['ftp_user'], $attach_config['ftp_pass']);

	if ( (!$login_result) )
	{
		message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_login'], $attach_config['ftp_user']));
	}
		
	if (!@ftp_pasv($conn_id, intval($attach_config['ftp_pasv_mode'])))
	{
		message_die(GENERAL_ERROR, $lang['Ftp_error_pasv_mode']);
	}
	
	$result = @ftp_chdir($conn_id, $ftp_path);

	if (!$result)
	{
		message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_path'], $ftp_path));
	}

	return ($conn_id);
}

//
// Deletes an Attachment
//
function unlink_attach($filename, $mode = FALSE)
{
	global $upload_dir, $attach_config, $lang;

	if (!intval($attach_config['allow_ftp_upload']))
	{
		if ($mode == MODE_THUMBNAIL)
		{
			$filename = $upload_dir . '/' . THUMB_DIR . '/t_' . $filename;
		}
		else
		{
			$filename = $upload_dir . '/' . $filename;
		}

		$deleted = @unlink($filename);

		if (@file_exists(@amod_realpath($filename)) ) 
		{
			$filesys = eregi_replace('/','\\', $filename);
			$deleted = @system("del $filesys");

			if (@file_exists(@amod_realpath($filename))) 
			{
				$deleted = @chmod ($filename, 0775);
				$deleted = @unlink($filename);
				$deleted = @system("del $filesys");
			}
		}
	}
	else
	{
		$conn_id = attach_init_ftp($mode);

		if ($mode == MODE_THUMBNAIL)
		{
			$filename = 't_' . $filename;
		}
		
		$res = @ftp_delete($conn_id, $filename);
		if (!$res)
		{
			if (ATTACH_DEBUG)
			{
				$add = ( $mode == MODE_THUMBNAIL ) ? ('/' . THUMB_DIR) : ''; 
				message_die(GENERAL_ERROR, sprintf($lang['Ftp_error_delete'], $attach_config['ftp_path'] . $add));
			}

			return ($deleted);
		}

		@ftp_quit($conn_id);

		$deleted = TRUE;
	}

	return ($deleted);
}

//
// FTP File to Location
//
function ftp_file($source_file, $dest_file, $mimetype, $disable_error_mode = FALSE)
{
	global $attach_config, $lang, $error, $error_msg;

	$conn_id = attach_init_ftp();

	//
	// Binary or Ascii ?
	//
	$mode = FTP_BINARY;
	if ( (preg_match("/text/i", $mimetype)) || (preg_match("/html/i", $mimetype)) )
	{
		$mode = FTP_ASCII;
	}

	$res = @ftp_put($conn_id, $dest_file, $source_file, $mode);
				
	if ( (!$res) && (!$disable_error_mode) )
	{
		$error = TRUE;
		if(!empty($error_msg))
		{
			$error_msg .= '<br />';
		}
		$error_msg = sprintf($lang['Ftp_error_upload'], $attach_config['ftp_path']) . '<br />';
		@ftp_quit($conn_id);
		return (FALSE);
	}

	if (!$res)
	{
		return (FALSE);
	}
				
	@ftp_site($conn_id, 'CHMOD 0644 ' . $dest_file);
	@ftp_quit($conn_id);
	return (TRUE);
}

//
// Check if Attachment exist
//
function attachment_exists($filename)
{
	global $upload_dir, $attach_config;

	if (!intval($attach_config['allow_ftp_upload']))
	{
		if( !@file_exists(@amod_realpath($upload_dir . '/' . $filename)) )
		{
			return (FALSE);
		}
		else
		{
			return (TRUE);
		}
	}
	else
	{
		$found = FALSE;

		$conn_id = attach_init_ftp();

		$file_listing = array();

		$file_listing = @ftp_rawlist($conn_id, $filename);

		for ($i = 0; $i < count($file_listing); $i++)
		{
			if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file_listing[$i], $regs) )
			{
				if ($regs[1] == 'd') 
				{	
					$dirinfo[0] = 1;	// Directory == 1
				}
				$dirinfo[1] = $regs[2]; // Size
				$dirinfo[2] = $regs[3]; // Date
				$dirinfo[3] = $regs[4]; // Filename
				$dirinfo[4] = $regs[5]; // Time
			}
			
			if ( ($dirinfo[0] != 1) && ($dirinfo[4] == $filename) )
			{
				$found = TRUE;
			}
		}

		@ftp_quit($conn_id);	
		
		return ($found);
	}
}

//
// Check if Thumbnail exist
//
function thumbnail_exists($filename)
{
	global $upload_dir, $attach_config;

	if (!intval($attach_config['allow_ftp_upload']))
	{
		if( !@file_exists(@amod_realpath($upload_dir . '/' . THUMB_DIR . '/t_' . $filename)) )
		{
			return (FALSE);
		}
		else
		{
			return (TRUE);
		}
	}
	else
	{
		$found = FALSE;

		$conn_id = attach_init_ftp(MODE_THUMBNAIL);

		$file_listing = array();

		$filename = 't_' . $filename;
		$file_listing = @ftp_rawlist($conn_id, $filename);

		for ($i = 0; $i < count($file_listing); $i++)
		{
			if (ereg("([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)", $file_listing[$i], $regs) )
			{
				if ($regs[1] == 'd') 
				{	
					$dirinfo[0] = 1;	// Directory == 1
				}
				$dirinfo[1] = $regs[2]; // Size

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?