functions_delete.php

来自「这是php编的论坛的原代码」· PHP 代码 · 共 265 行

PHP
265
字号
<?php
/***************************************************************************
 *                            functions_delete.php
 *                            -------------------
 *   begin                : Sat, Jul 20, 2002
 *   copyright            : (C) 2002 Meik Sievertsen
 *   email                : acyd.burn@gmx.de
 *
 *   $Id: functions_delete.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 processing the Deletion Process
//

//
// Delete Attachment(s) from post(s) (intern)
//
function delete_attachment($post_id_array = -1, $attach_id_array = -1, $page = -1)
{
	global $db;

	//
	// Generate Array, if it's not an array
	//
	if ( ($post_id_array == -1) && ($attach_id_array == -1) && ($page == -1) )
	{
		return;
	}

	if ( ($post_id_array == -1) && ($attach_id_array != -1) )
	{
		$post_id_array = array();

		if (!is_array($attach_id_array))
		{
			$attach_id = intval($attach_id_array);

			$attach_id_array = array();
			$attach_id_array[] = $attach_id;
		}
	
		// Get the post_ids to fill the array
		if ($page == PAGE_PRIVMSGS)
		{
			$p_id = 'privmsgs_id';
		}
		else
		{
			$p_id = 'post_id';
		}

		$sql = "SELECT " . $p_id . " FROM " . ATTACHMENTS_TABLE . " WHERE attach_id IN (" . implode(', ', $attach_id_array) . ")
		GROUP BY " . $p_id;

		if ( !($result = attach_sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not select ids', '', __LINE__, __FILE__, $sql);
		}

		$post_list = $db->sql_fetchrowset($result);
		$num_post_list = $db->sql_numrows($result);

		for ($i = 0; $i < $num_post_list; $i++)
		{
			$post_id_array[] = $post_list[$i][$p_id];
		}

		if ($num_post_list == 0)
		{
			return;
		}
	}
		
	if (!is_array($post_id_array))
	{
		if (empty($post_id_array))
		{
			return;
		}

		$post_id = intval($post_id_array);

		$post_id_array = array();
		$post_id_array[] = $post_id;
	}
		
	if (count($post_id_array) == 0)
	{
		return;
	}

	//
	// First of all, determine the post id and attach_id
	//
	if ($attach_id_array == -1)
	{
		$attach_id_array = array();

		// Get the attach_ids to fill the array
		if ($page == PAGE_PRIVMSGS)
		{
			$whereclause = "WHERE privmsgs_id IN (" . implode(', ', $post_id_array) . ")";
		}
		else
		{
			$whereclause = "WHERE post_id IN (" . implode(', ', $post_id_array) . ")";
		}
			
		$sql = "SELECT attach_id FROM " . ATTACHMENTS_TABLE . " " . $whereclause . " GROUP BY attach_id";

		if ( !($result = attach_sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not select Attachment Ids', '', __LINE__, __FILE__, $sql);
		}

		$attach_list = $db->sql_fetchrowset($result);
		$num_attach_list = $db->sql_numrows($result);

		for ($i = 0; $i < $num_attach_list; $i++)
		{
			$attach_id_array[] = $attach_list[$i]['attach_id'];
		}

		if ($num_attach_list == 0)
		{
			return;
		}
	}
	
	if (!is_array($attach_id_array))
	{
		$attach_id = intval($attach_id_array);

		$attach_id_array = array();
		$attach_id_array[] = $attach_id;
	}

	if (count($attach_id_array) == 0)
	{
		return;
	}

	if ($page == PAGE_PRIVMSGS)
	{
		$sql_id = 'privmsgs_id';
	}
	else
	{
		$sql_id = 'post_id';
	}

	$sql = "DELETE FROM " . ATTACHMENTS_TABLE . " WHERE attach_id IN (" . implode(', ', $attach_id_array) . ") AND " . $sql_id . " IN (" . implode(', ', $post_id_array) . ")";          $delete_attachment_sql = "DELETE FROM " . ATTACHMENTS_TABLE . " WHERE attach_id IN (" . implode(', ', $attach_id_array) . ") AND " . $sql_id . " IN (" . implode(', ', $post_id_array) . ")"; 
    
	if ( !(attach_sql_query($sql)) )   
	{   
		message_die(GENERAL_ERROR, $lang['Error_deleted_attachments'], '', __LINE__, __FILE__, $sql);   
	} 
	
	for ($i = 0; $i < count($attach_id_array); $i++)
	{
		$sql = "SELECT attach_id FROM " . ATTACHMENTS_TABLE . " WHERE attach_id = " . $attach_id_array[$i];
			
		if ( !($result = attach_sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not select Attachment Ids', '', __LINE__, __FILE__, $sql);
		}

		if ($db->sql_numrows($result) == 0)
		{
			$sql = 'SELECT attach_id, physical_filename, thumbnail
			FROM ' . ATTACHMENTS_DESC_TABLE . '
			WHERE attach_id = ' . $attach_id_array[$i];
	
			if ( !($result = attach_sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Couldn\'t query attach description table', '', __LINE__, __FILE__, $sql);
			}
		
			if ( $db->sql_numrows($result) != 0)
			{
				$attachments = $db->sql_fetchrowset($result);
				$num_attach = $db->sql_numrows($result);

				//
				// delete attachments
				//
				for ($j = 0; $j < $num_attach; $j++)
				{
					unlink_attach($attachments[$j]['physical_filename']);
					if (intval($attachments[$j]['thumbnail']) == 1)
					{
						unlink_attach($attachments[$j]['physical_filename'], MODE_THUMBNAIL);
					}
					
					$sql = 'DELETE FROM ' . ATTACHMENTS_DESC_TABLE . '
					WHERE attach_id = ' . $attachments[$j]['attach_id'];

					if ( !(attach_sql_query($sql)) )
					{
						message_die(GENERAL_ERROR, $lang['Error_deleted_attachments'], '', __LINE__, __FILE__, $sql);
					}
				}

			}
		}
	}
	
	//
	// Now Sync the Topic/PM
	//
	if ($page == PAGE_PRIVMSGS)
	{
		for ($i = 0; $i < count($post_id_array); $i++)
		{
			$sql = "SELECT attach_id FROM " . ATTACHMENTS_TABLE . " WHERE privmsgs_id = " . $post_id_array[$i];

			if ( !($result = attach_sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Couldn\'t query Attachments Table', '', __LINE__, __FILE__, $sql);
			}

			if ($db->sql_numrows($result) == 0)
			{
				$sql = "UPDATE " . PRIVMSGS_TABLE . " SET privmsgs_attachment = 0 WHERE privmsgs_id = " . $post_id_array[$i];

				if ( !($result = attach_sql_query($sql)) )
				{
					message_die(GENERAL_ERROR, 'Couldn\'t update Private Message Attachment Switch', '', __LINE__, __FILE__, $sql);
				}
			}
		}
	}
	else
	{
		$sql = "SELECT topic_id FROM " . POSTS_TABLE . " WHERE post_id IN (" . implode(', ', $post_id_array) . ") GROUP BY topic_id";
		
		if ( !($result = attach_sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Couldn\'t select Topic ID', '', __LINE__, __FILE__, $sql);
		}
	
		$row = $db->sql_fetchrowset($result);
		$num_rows = $db->sql_numrows($result);
						
		for ($i = 0; $i < $num_rows; $i++)
		{
			attachment_sync_topic($row[$i]['topic_id']);
		}
	}
}

?>

⌨️ 快捷键说明

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