<?php
# This script allows a user to make post their favorite color on the website.
# This program is a demostration of basic file handling methods in SQLEFS.
# See it in action at: http://practicalproductivity.com/sqlefs/colorlog.php
### This file just handles your login details and sets the volume. ###
require_once 'sqlefs_login.php';
### Or you could specify that stuff in your script like so ###
# require_once '../sqlefs.class.php';
# $efs = new efsclass();
# $efs->connect('mysql:mysql:host=localhost;dbname=myefsdb name pass');
# $efs->cd('c:/');
$showlen = 10; # show this many entries
$namelen = 25; # max length of name
$do = isset($_REQUEST['color']) ? 'post' : 'view';
### Set the current working directory. ###
$efs->cd('/submissions/');
# view comments
if ($do == 'view') {
$content = '<h1>Visitors Favorite Colors</h1>';
$content .= '
<p>Enter your name and favorite color!</p>
<div>
<form role="form" method="post" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name" size="' . $namelen . '">
<input type="color" name="color">
<input type="submit">
</form>
</div>
';
### Retrieve a listing of files and reverse it so newest are first. ###
$files = array_reverse($efs->ls());
# list each entry. We also reverse them to see newest first
foreach ($files as $file) {
### Load our file as serialized data. ###
$submission = $efs->load($file);
$content .= '<div class="name">' . $submission['name'] . '</div>' ;
$content .= '<div class="color" style="background-color: ' . $submission['color'] . '"></div>' ;
}
}
# post a color
if ($do == 'post') {
$content = '<b>Thanks for posting!</b>';
$name = htmlspecialchars( substr($_REQUEST['name'], 0, $namelen) );
if ($name === '') {$name = 'Anonymous';}
$color = htmlspecialchars($_REQUEST['color']);
$data = array('name' => $name, 'color' => $color);
$filename = crc32($name . $color);
### Save data in serialized format. ###
$efs->save($filename, $data);
# We intend to limit the number of entries we show...
# SQLEFS orders file listings by oldest first by default,
# so we'll just delete the first files in the listing
# until we have the right number to show.
### Retrieve file listing. ###
$files = $efs->ls();
$trimcount = count($files) - $showlen;
while ($trimcount > 0) {
### Delete the file removed from the array. ###
$efs->rm( array_shift($files) );
$trimcount -= 1;
}
$content .= '<hr><a href="colorlog.php">view entries</a>';
}
?><html>
<head><head>
<style>
.name {width: 200px;text-align: center;}
.color {width: 200px;height: 25px;}
</style>
<body>
<?php echo $content; ?>
</body>
</html>
|