Revision [11719]
This is an old revision of TwoMCM made by TwoMCM on 2005-11-09 10:39:30.
<?php
/**
actions/blob.php
This allows files to be inserted into the DB as blobs.
@author Matthew Peterson <paranoidgeek@gmail.com>
@author-nick twomcm
@date November 2005
@version 1.1
@changlog
1.1 Minor bugs fixed
1.0 Complete
0.1 blank
@license GPL v 2
@example List
{{blob list="yes"}}
Display
{{blob hash="SHA1 SUM"}}
*/
if($_POST['blob_sha1'] && !$_POST['blob_delete']){ // get file
$row = $this->LoadSingle("SELECT LENGTH(`data`) as `size`, `hash`, `name`, `type`, `data` ".
"FROM ".$this->config["table_prefix"]."blob WHERE `hash` = '".mysql_escape_string($_POST['blob_sha1'])."' ");
if($row){
while(@ ob_end_clean());
// clean out every thing.
header("Content-Length: ".$row['size']);
header("Pragma: no-cache");
header("Content-Type: ".$row['type']);
if($_POST['download'] )
header('Content-Disposition: attachment; filename="'.$row['name'].'"');
echo $row['data'];
die();
}else{
while(@ ob_end_clean());
echo "Sorry the system was unable to find the requested item. \n";
die();
}
}elseif(is_array($vars) && $vars['hash']){
$row = $this->LoadSingle("SELECT LENGTH(`data`) as `size`, `hash`, `name`, `type`, `upload_date` ".
"FROM ".$this->config["table_prefix"]."blob WHERE `hash` = '".mysql_escape_string($vars['hash'])."' ");
if($row){
echo $this->FormOpen();
echo "<div class=\"code\">\n";
echo "Filename: ".$row['name']."<br />\n";
echo "Type: ".$row['type']."<br />\n";
echo "Size: ".$row['size']." Bytes<br />\n";
echo "</div>\n";
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="download" style="line-height:10px;float:right;'.
'vertical-align:middle;margin-right:20px; margin-top: 0px; font-size: 10px; '.
'color:#000; font-weight: normal; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 18px;" value="download" title="Download this item" />'."\n";
echo '<input type="submit" name="view" style="line-height:10px;float:right;'.
'vertical-align:middle;margin-right:20px; margin-top: 0px; font-size: 10px; '.
'color:#000; font-weight: normal; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 18px;" value="view" title="View this item" />'."\n";
// FIXME remove unneeded style items
echo $this->FormClose();
}else{
echo "<div class=\"code\">\n";
echo "Sorry the system was unable to find the requested item. <br />\n";
echo "HASH : {$vars['hash']}\n";
echo "</div>\n";
}
}elseif(is_array($vars) && $vars['list']){ // $var-list
if($_POST['blob_delete']){
$this->Query("DELETE FROM ".$this->config["table_prefix"]."blob WHERE hash = '".$_POST['blob_sha1']."'");
unset($_POST['blob_delete']);
unset($_POST['blob_sha1']);
}
if($_FILES['blob_upload']['name'] && !$_FILES['blob_upload']['error'] ){
$fileLocation = $_FILES['blob_upload']['tmp_name'];
$fd = fopen($fileLocation, "r");
$t = fread($fd, filesize($fileLocation));
fclose($fd);
unlink($fileLocation);
if(strlen($t) <= 1000000){ // 1 million bytes
$this->Query("INSERT INTO ".$this->config["table_prefix"]."blob ".
"(`data`, `name`, `type`, `hash`, `upload_date`) ".
"VALUES ('".mysql_escape_string($t)."' ,". // data
"'".$_FILES['blob_upload']['name']."' ,". // name
"'".$_FILES['blob_upload']['type']."' ,". // type
"'".sha1($t)."', ". // hash
" NOW() " . //upload_date
")") ;
}else{
die ("Sorry the file you uploaded is too large ! You may only upload files of size 1MB or smaller.");
}
unset($t);
}
echo "<table>\n";
$rows = $this->LoadAll("SELECT LENGTH(`data`) as `size`, `hash`, `name`, `type`, `upload_date` ".
"FROM ".$this->config["table_prefix"]."blob ORDER BY `upload_date` DESC");
echo "<tr><th>Hash</th><th>Name</th><th>Type</th><th>Size</th><th>Upload Date</th><th></th><th></th><th></th></tr>\n";
foreach($rows as $row){
echo "<tr>".
"<td>{$row['hash']}</td>".
"<td>{$row['name']}</td>".
"<td>{$row['type']}</td>".
"<td>{$row['size']}</td>".
"<td>".date("Y F j, G:i:s", strtotime($row['upload_date']))."</td>";
echo '<td>';
echo $this->FormOpen();
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="view" style="font-size: 9px; '.
'vertical-align:middle; color:#000; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 16px;" value="view" title="view" />';
echo $this->FormClose();
echo '</td>'."\n"; // view
echo '<td>';
echo $this->FormOpen();
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="download" style="font-size: 9px; '.
'vertical-align:middle; color:#000; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 16px;" value="download" title="download" />';
echo $this->FormClose();
echo '</td>'."\n"; // download
echo '<td>';
echo $this->FormOpen();
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="blob_delete" style="font-size: 9px; '.
'vertical-align:middle; color:#000; font-family: Verdana, Arial, sans-serif; background-color: #FAA;'.
'text-decoration: none; height: 16px;" value="delete" title="delete" '.
'onclick="return confirm(\'Are you sure you want to do that?\')" />'; // delete + js confirm
echo $this->FormClose();
echo '</td>'."\n";
echo "</tr>\n";
}
unset($rows);
echo "</table>\n";
echo"<form action=\"".$this->href()."\" method=\"post\" enctype=\"multipart/form-data\">\n";
if (!$this->config["rewrite_mode"])
echo"<input type=\"hidden\" name=\"wakka\" value=\"".$this->MiniHref()."\">\n";
echo "<input type=\"file\" name=\"blob_upload\"></input>\n";
echo "<input type=\"submit\" value=\"submit\"></input>\n";
echo $this->FormClose();
//.. upload
}else{ // Bad setup
echo "You have not configured the 'blob' action correctly.";
}
/*
SQL for creating the table :
CREATE TABLE `wikka_blob` (
`hash` varchar(40) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`type` varchar(255) NOT NULL default '',
`upload_date` datetime NOT NULL default '0000-00-00 00:00:00',
`data` mediumblob NOT NULL,
PRIMARY KEY (`hash`)
) ENGINE=MyISAM DEFAULT
*/
?>
/**
actions/blob.php
This allows files to be inserted into the DB as blobs.
@author Matthew Peterson <paranoidgeek@gmail.com>
@author-nick twomcm
@date November 2005
@version 1.1
@changlog
1.1 Minor bugs fixed
1.0 Complete
0.1 blank
@license GPL v 2
@example List
{{blob list="yes"}}
Display
{{blob hash="SHA1 SUM"}}
*/
if($_POST['blob_sha1'] && !$_POST['blob_delete']){ // get file
$row = $this->LoadSingle("SELECT LENGTH(`data`) as `size`, `hash`, `name`, `type`, `data` ".
"FROM ".$this->config["table_prefix"]."blob WHERE `hash` = '".mysql_escape_string($_POST['blob_sha1'])."' ");
if($row){
while(@ ob_end_clean());
// clean out every thing.
header("Content-Length: ".$row['size']);
header("Pragma: no-cache");
header("Content-Type: ".$row['type']);
if($_POST['download'] )
header('Content-Disposition: attachment; filename="'.$row['name'].'"');
echo $row['data'];
die();
}else{
while(@ ob_end_clean());
echo "Sorry the system was unable to find the requested item. \n";
die();
}
}elseif(is_array($vars) && $vars['hash']){
$row = $this->LoadSingle("SELECT LENGTH(`data`) as `size`, `hash`, `name`, `type`, `upload_date` ".
"FROM ".$this->config["table_prefix"]."blob WHERE `hash` = '".mysql_escape_string($vars['hash'])."' ");
if($row){
echo $this->FormOpen();
echo "<div class=\"code\">\n";
echo "Filename: ".$row['name']."<br />\n";
echo "Type: ".$row['type']."<br />\n";
echo "Size: ".$row['size']." Bytes<br />\n";
echo "</div>\n";
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="download" style="line-height:10px;float:right;'.
'vertical-align:middle;margin-right:20px; margin-top: 0px; font-size: 10px; '.
'color:#000; font-weight: normal; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 18px;" value="download" title="Download this item" />'."\n";
echo '<input type="submit" name="view" style="line-height:10px;float:right;'.
'vertical-align:middle;margin-right:20px; margin-top: 0px; font-size: 10px; '.
'color:#000; font-weight: normal; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 18px;" value="view" title="View this item" />'."\n";
// FIXME remove unneeded style items
echo $this->FormClose();
}else{
echo "<div class=\"code\">\n";
echo "Sorry the system was unable to find the requested item. <br />\n";
echo "HASH : {$vars['hash']}\n";
echo "</div>\n";
}
}elseif(is_array($vars) && $vars['list']){ // $var-list
if($_POST['blob_delete']){
$this->Query("DELETE FROM ".$this->config["table_prefix"]."blob WHERE hash = '".$_POST['blob_sha1']."'");
unset($_POST['blob_delete']);
unset($_POST['blob_sha1']);
}
if($_FILES['blob_upload']['name'] && !$_FILES['blob_upload']['error'] ){
$fileLocation = $_FILES['blob_upload']['tmp_name'];
$fd = fopen($fileLocation, "r");
$t = fread($fd, filesize($fileLocation));
fclose($fd);
unlink($fileLocation);
if(strlen($t) <= 1000000){ // 1 million bytes
$this->Query("INSERT INTO ".$this->config["table_prefix"]."blob ".
"(`data`, `name`, `type`, `hash`, `upload_date`) ".
"VALUES ('".mysql_escape_string($t)."' ,". // data
"'".$_FILES['blob_upload']['name']."' ,". // name
"'".$_FILES['blob_upload']['type']."' ,". // type
"'".sha1($t)."', ". // hash
" NOW() " . //upload_date
")") ;
}else{
die ("Sorry the file you uploaded is too large ! You may only upload files of size 1MB or smaller.");
}
unset($t);
}
echo "<table>\n";
$rows = $this->LoadAll("SELECT LENGTH(`data`) as `size`, `hash`, `name`, `type`, `upload_date` ".
"FROM ".$this->config["table_prefix"]."blob ORDER BY `upload_date` DESC");
echo "<tr><th>Hash</th><th>Name</th><th>Type</th><th>Size</th><th>Upload Date</th><th></th><th></th><th></th></tr>\n";
foreach($rows as $row){
echo "<tr>".
"<td>{$row['hash']}</td>".
"<td>{$row['name']}</td>".
"<td>{$row['type']}</td>".
"<td>{$row['size']}</td>".
"<td>".date("Y F j, G:i:s", strtotime($row['upload_date']))."</td>";
echo '<td>';
echo $this->FormOpen();
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="view" style="font-size: 9px; '.
'vertical-align:middle; color:#000; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 16px;" value="view" title="view" />';
echo $this->FormClose();
echo '</td>'."\n"; // view
echo '<td>';
echo $this->FormOpen();
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="download" style="font-size: 9px; '.
'vertical-align:middle; color:#000; font-family: Verdana, Arial, sans-serif; background-color: #DDD;'.
'text-decoration: none; height: 16px;" value="download" title="download" />';
echo $this->FormClose();
echo '</td>'."\n"; // download
echo '<td>';
echo $this->FormOpen();
echo '<input type="hidden" name="blob_sha1" value="'.$row['hash']."\" />\n";
echo '<input type="submit" name="blob_delete" style="font-size: 9px; '.
'vertical-align:middle; color:#000; font-family: Verdana, Arial, sans-serif; background-color: #FAA;'.
'text-decoration: none; height: 16px;" value="delete" title="delete" '.
'onclick="return confirm(\'Are you sure you want to do that?\')" />'; // delete + js confirm
echo $this->FormClose();
echo '</td>'."\n";
echo "</tr>\n";
}
unset($rows);
echo "</table>\n";
echo"<form action=\"".$this->href()."\" method=\"post\" enctype=\"multipart/form-data\">\n";
if (!$this->config["rewrite_mode"])
echo"<input type=\"hidden\" name=\"wakka\" value=\"".$this->MiniHref()."\">\n";
echo "<input type=\"file\" name=\"blob_upload\"></input>\n";
echo "<input type=\"submit\" value=\"submit\"></input>\n";
echo $this->FormClose();
//.. upload
}else{ // Bad setup
echo "You have not configured the 'blob' action correctly.";
}
/*
SQL for creating the table :
CREATE TABLE `wikka_blob` (
`hash` varchar(40) NOT NULL default '',
`name` varchar(255) NOT NULL default '',
`type` varchar(255) NOT NULL default '',
`upload_date` datetime NOT NULL default '0000-00-00 00:00:00',
`data` mediumblob NOT NULL,
PRIMARY KEY (`hash`)
) ENGINE=MyISAM DEFAULT
*/
?>