Commit a90b4d4e authored by Pierre Marchand's avatar Pierre Marchand

integrate textures

parent d670794d
<?php
// home.php
require_once('textures.php');
get_header();
while(have_posts())
......@@ -15,5 +18,10 @@ while(have_posts())
</div>';
}
$T = new Texture('/home/pierre/System/src/osp.workshop.open-textures',
'http://localhost/~pierre/404/wp-content/themes/404/secure_image.php');
$T->write();
get_footer();
?>
<?php
// secure_image.php
$path = $_GET['p'];
$hash = $_GET['h'];
if(file_exists($path))
{
$sum = md5_file($path);
if($hash === $sum)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mt = finfo_file($finfo, $path);
header("Content-length: ".filesize($path));
header("Content-type: " . $mt);
readfile($path);
finfo_close($finfo);
}
}
?>
\ No newline at end of file
<?php
// textures.php
function walk_dir($path, $pat)
{
$stop = false;
/* get the absolute path and ensure it has a trailing slash */
$path = realpath($path);
if (substr($path, -1) !== DIRECTORY_SEPARATOR)
$path .= DIRECTORY_SEPARATOR;
$queue = array($path => 1);
$done = array();
$index = 0;
$ret_array = array();
while(!empty($queue))
{
/* get one element from the queue */
foreach($queue as $path => $unused)
{
unset($queue[$path]);
$done[$path] = null;
break;
}
unset($unused);
$dh = @opendir($path);
if (!$dh) continue;
while(($filename = readdir($dh)) !== false)
{
/* dont recurse back up levels */
if ($filename == '.' || $filename == '..')
continue;
$filename_base = $filename;
$filename = $path . $filename;
/* queue directories for later search */
if (is_dir($filename))
{
/* ensure the path has a trailing slash */
if (substr($filename, -1) !== DIRECTORY_SEPARATOR)
$filename .= DIRECTORY_SEPARATOR;
/* check if we have already queued this path, or have done it */
if (array_key_exists($filename, $queue) || array_key_exists($filename, $done))
continue;
/* queue the file */
$queue[$filename] = null;
}
else
{
if (preg_match($pat, $filename_base) === 1)
{
$ret_array[-1] = $filename;
}
}
}
closedir($dh);
}
return $ret_array;
}
class Texture
{
public function __construct($root, $web_srv)
{
$this->root = $root;
$this->web_srv = $web_srv ;
$this->collect_images();
}
public function collect_images()
{
$this->files = walk_dir($this->root, '/.*\.(jpeg|png|jpg)$/i');
}
function make_url($path)
{
$md = md5_file($path);
return $this->web_srv . '?p=' . urlencode($path) . '&h=' . urlencode($md);
// $bname = basename($path);
// $cpath = $this->cache_dir . $bname;
// if(file_exists($cpath))
// {
// $md1 = md5_file($path);
// $md2 = md5_file($cpath);
// if($md1 === $md2)
// {
// return $this->web_root . $bname;
// }
// else
// {
// copy($path, $cpath);
// return $this->web_root . $bname;
// }
// }
// else
// {
// copy($path, $cpath);
// return $this->web_root . $bname;
// }
}
public function write($class='git_image', $before='', $after='')
{
foreach($this->files as $path)
{
echo $before.'<img class="'.$class.'" src="'.$this->make_url($path).'" />'.$after;
}
}
}
?>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment