Location: PHPKode > projects > CMSmelborp > CMSmelborp/includes/external_files.php
<?php
/*******************************************************************************
License:
	Copyright 2005 Ryan Morehart
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
	http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
	
Filename: /includes/external_files.php

Purpose: Holds template processing functions for both retrieval and saving/updating.

Processed parameters: (TYPE - NAME - DESCRIPTION - DEFAULT)
	None
*******************************************************************************/

function getFile(&$connection, $identifier)
{
	// Ensure the image name is valid
	if(!isValidFileName($connection, $identifier, true))
	{
		return false;
	}
	
	// Get the image data from database
	$file = mysql_query("SELECT * FROM external_files WHERE file_name='$identifier';", $connection);
	$file = mysql_fetch_array($file, MYSQL_ASSOC);
	
	if($file === false)
	{
		return false;
	}
	else
	{
		return $file;
	}
}

function addFileToDatabase(&$connection, $fileName, $fileType, $description, $realFileName)
{
	// Ensure the image name is valid
	if(!isValidFileName($connection, $fileName))
	{
		return false;
	}
	
	// Open image file
	$file = fopen($realFileName, 'rb');
	if($file !== false)
	{
		$fileData = fread($file, filesize($realFileName));
	}
	else
	{
		return false;
	}
	
	// Insert into database
	$success = mysql_query("INSERT INTO external_files VALUES('$fileName', '$fileType', '"
		. addslashes($description) . "', '" . addslashes($fileData) . "');", $connection);
		
	if($success === false)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function deleteFile(&$connection, $identifier)
{
	// Ensure the image name is valid
	if(!isValidFileName($connection, $identifier))
	{
		return false;
	}
	
	// Delete from database
	$success = mysql_query("DELETE FROM external_files WHERE file_name='$identifier';", $connection);
	if($success === false)
	{
		return false;
	}
	
	return true;
}

function setFileInfo(&$connection, $oldIdentifier, $newIdentifier, $description)
{
	// Ensure the File name is valid
	if(!isValidFileName($connection, $oldIdentifier) || ($newIdentifier != $oldIdentifier
		&& isValidFileName($connection, $newIdentifier) && FileExists($connection, $newIdentifier)))
	{
		return false;
	}
	
	// Update info
	$success = mysql_query("UPDATE external_files SET file_name='$newIdentifier', description='"
		. addslashes($description) . "' WHERE file_name='$oldIdentifier';", $connection);
	if($success === false)
	{
		return false;
	}

	return true;
}

function setFileContents(&$connection, $identifier, $data)
{
	// Ensure the File name is valid
	if(!isValidFileName($connection, $identifier))
	{
		return false;
	}
	
	// Update file
	$success = mysql_query("UPDATE external_files SET data='"
		. addslashes($data) . "' WHERE file_name='$identifier';", $connection);
	if($success === false)
	{
		return false;
	}

	return true;
}

function getFileList(&$connection)
{
	// Get list from database
	$images = mysql_query("SELECT * FROM external_files ORDER BY file_name;", $connection);
	$temp = fetchResults($images);
	return $temp;
}

function isValidFileName(&$connection, $identifier, $isExistent = false)
{
	// Ensure it conains only valid characters
	if(!ereg('^[_a-zA-Z0-9]+$', $identifier))
	{
		return false;
	}
	
	// Check if this name is already in the database (if we are supposed to)
	if($isExistent)
	{
		return fileExists($connection, $identifier);
	}
	else
	{
		return true;
	}
}

function fileExists(&$connection, $identifier)
{
	$exists = mysql_query("SELECT * FROM external_files WHERE file_name='$identifier';", $connection);
	
	if(mysql_fetch_array($exists, MYSQL_ASSOC) !== false)
	{
		return true;
	}
	else
	{
		return false;
	}
}
?>
Return current item: CMSmelborp