Wiki source for TwoMCM


Show raw source

=====twomcm's user page=====
These are two Wikka modules i have written.
- The first, which i call 'blob', allows you to upload files ( up to 1 MiB (MySQL packet limit)) to a wikka powered site. As well, you can embed images into pages, create a link to the file, or show a description ( incl of view/download link ). It uses two files but the action file is all that is required if you only wish to upload or show the description view for files.
- The second ( base64.php ) allows small files to be embeded into a page if they are base64 encoded ( not really suggested to be used ).
Enjoy.

Any comments would be appreciated.

Since i do not have access to Internet Exploder YMMV if you are accessing these page with //that// browser.

[[http://img82.imageshack.us/img82/7196/fileupload8kt.png | Screen shot of an example]] ( due to image editing problems, "Standard file embed" has Type: and Size: duplicated ).

=====If you want to contact me use [[paranoidgeek@gmail.com]]=====
I doubt if i will be visting this page often.

=====Blob details :=====
Blob ( as i have called it ) is [[Wikipedia:SHA1 | hash]] orientated. The three modes for displaying the file are :
1) **Description.** This gives a box with the file's details and allows has view and download buttons. Exmaple code : ""{{blob hash="SHA1 SUM"}}""
1) **Image embed**. This inserts the image as ##<img src="yadadad" />##. Also 'wrapped' with an ##<a href=" "></a>## pointing to the same URL as the img src. Requires the handerler file installed and the file's type must start with 'image' ( e.g. image/png). Example code : ""{{blob hash="SHA1 SUM" type="image"}}""
1) **Image with border**. Same as above but with a border ( using class="border" ). Example code : ""{{blob hash="SHA1 SUM" type="image_wb"}}""
1) **Link**. Just the file name wrapped with a ##<a href=" ">## pointing to the file's view URL.

----

##/action/blob.php##
%%(php)
<?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.3 Image + link
1.2 Changed so that it will only allow uploads if the user has R/W to the page.
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"}}
Image
{{blob hash="SHA1 SUM" type="image"}}
link
{{blob hash="SHA1 SUM" type="link"}}
*/

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){
if(strtolower($vars['type']) == 'link'){
echo "<a href=\"".$this->href('blob',$this->GetPageTag(),'action=view&hash='.$row['hash'])."\">";
echo "{$row['name']}";
echo "</a>";

}elseif(strtolower($vars['type']) == 'image'){
echo "<a href=\"".$this->href('blob',$this->GetPageTag(),'action=view&hash='.$row['hash'])."\">";
echo " <img src=\"".$this->href('blob',$this->GetPageTag(),'action=view&hash='.$row['hash'])."\" />";
echo "</a>";
}else{
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->HasAccess("write") ){
$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'] && $this->HasAccess("write") && !$_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>';
if($this->HasAccess("write")){
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";
if($this->HasAccess("write")){
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`)
) Type=MyISAM;

*/
?>
%%

----

##/handlers/page/blob.php##
%%(php)
<?php
/**
See /action/blob.php
*/

if($_GET['hash']){ // 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($_GET['hash'])."' ");
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($_GET['action'] == '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();
}
}else{ // Bad link
echo "You have not configured the 'blob' handler correctly.";
}
?>
%%

----

/actions/base64.php
%%(php)
<?php
/**
This allows BASE-64 encoded files to be embedded in Wikka pages.
This moddiffied version uses SHA1-hashes sent back to the script not the data

@author Matthew Peterson <paranoidgeek@gmail.com>
@date November 2005
@version 1.1
@changlog
1.1 Add view button
1.0 Changed to sending SHA to the script rather than the data.
0.9 Orginal version
@license GPL v 2
@example This wikka example containes a random bit of data. {{base64 type="image/png" filename="pix1.png" data="FJSFNSIJFDisjdsjd=2/"}}
*/

if(is_array($vars) && $vars['data']){

$vars['data'] = preg_replace('/\s+/', '', $vars['data']);

$filename = $vars['filename'] ? $vars['filename'] : 'Untitled.txt';
$size = (int)(strlen($vars['data'])*3/4);
$type = $vars['type'] ? $vars['type'] : 'text/plain';
$sha1 = sha1($vars['data']);

if($_POST['base64-sha1'] == $sha1){

while(@ ob_end_clean());
// clean out every thing.

$data = base64_decode($vars['data']);

header("Content-Length: ".strlen($data));
header("Pragma: no-cache");
header("Content-Type: $type");
if($_POST['download'] )
header("Content-Disposition: attachment; filename=\"$filename\"");

echo $data;

die();

}else{
echo $this->FormOpen();

echo "<div class=\"code\">\n";
echo "Filename: ".$filename."<br />\n";
echo "Type: ".$type."<br />\n";
echo "Size: ".$size." Bytes<br />\n";
echo "</div>\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 '<input type="hidden" name="base64-sha1" value="'.$sha1."\" />\n";

echo $this->FormClose();
}
}else{
echo "You have not configured the 'base64' action correctly.";
}
?>
%%
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki