2013-09-21 01:22:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// Code-hosting website
|
|
|
|
// ````````````````````
|
|
|
|
|
|
|
|
define('BASEDIR', __DIR__.'\\');
|
2013-09-21 01:52:12 +00:00
|
|
|
define('SITE_TITLE', 'code.ivysaur.me');
|
|
|
|
define('PAGE_THUMB_W', 60);
|
|
|
|
define('PAGE_THUMB_H', 60);
|
2013-09-21 01:22:48 +00:00
|
|
|
|
|
|
|
// clean up wwwroot
|
|
|
|
|
|
|
|
if (file_exists(BASEDIR.'wwwroot')) {
|
|
|
|
rename(BASEDIR.'wwwroot', BASEDIR.'wwwroot.old.'.uniqid());
|
|
|
|
}
|
2013-09-21 01:52:12 +00:00
|
|
|
|
2013-09-21 01:22:48 +00:00
|
|
|
mkdir(BASEDIR.'wwwroot');
|
2013-09-21 01:52:12 +00:00
|
|
|
mkdir(BASEDIR.'wwwroot/srv');
|
2013-09-21 01:22:48 +00:00
|
|
|
|
|
|
|
// List of projects
|
|
|
|
|
|
|
|
$ls = scandir(BASEDIR.'data');
|
|
|
|
sort($ls);
|
|
|
|
$projects = array();
|
|
|
|
foreach($ls as $dirname) {
|
|
|
|
if ($dirname[0] == '.') continue;
|
|
|
|
$matches = array();
|
|
|
|
|
|
|
|
if (preg_match('~(?:\d+-)?(.+)~', $dirname, $matches)) {
|
|
|
|
$projects[$dirname] = $matches[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
/**
|
|
|
|
* Create a thumbnail of an image. It overscales and crops to fit the target
|
|
|
|
* box.
|
|
|
|
*
|
|
|
|
* @param string $src_file
|
|
|
|
* @param string $dest_file
|
|
|
|
* @param int $width
|
|
|
|
* @param int $height
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-09-21 01:22:48 +00:00
|
|
|
function mkthumbnail($src_file, $dest_file, $width, $height) {
|
2013-09-21 01:52:12 +00:00
|
|
|
list($src_width, $src_height) = getimagesize($src_file);
|
|
|
|
|
2013-09-21 01:22:48 +00:00
|
|
|
$im = imagecreatefromstring(file_get_contents($src_file));
|
|
|
|
|
|
|
|
$dest = imagecreatetruecolor($width, $height);
|
2013-09-21 01:52:12 +00:00
|
|
|
|
|
|
|
$scale = max( $width/$src_width, $height/$src_height ); // overscale + crop
|
|
|
|
|
|
|
|
print_r($scale);
|
|
|
|
|
|
|
|
$box_w = $width/$scale;
|
|
|
|
$box_h = $height/$scale;
|
|
|
|
|
|
|
|
$box_xoff = floor(($src_width - $box_w)/2);
|
|
|
|
$box_yoff = floor(($src_height - $box_h)/2);
|
|
|
|
|
|
|
|
print_r(array(
|
|
|
|
$dest, $im,
|
|
|
|
0, 0,
|
|
|
|
$box_xoff, $box_yoff,
|
|
|
|
$width, $height, $box_w, $box_h
|
|
|
|
));
|
|
|
|
|
|
|
|
imagecopyresampled(
|
|
|
|
$dest, $im,
|
|
|
|
0, 0,
|
|
|
|
$box_xoff, $box_yoff,
|
|
|
|
$width, $height, $box_w, $box_h
|
|
|
|
);
|
2013-09-21 01:22:48 +00:00
|
|
|
|
|
|
|
return imagejpeg($dest, $dest_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
function str_ext($sz) {
|
|
|
|
$dpos = strrpos($sz, '.');
|
|
|
|
return substr($sz, $dpos+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
function is_image($sz) {
|
|
|
|
return in_array(strtolower(str_ext($sz)), ['jpg', 'png', 'jpeg']);
|
|
|
|
}
|
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
function hesc($sz) {
|
|
|
|
return @htmlentities($sz, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2013-09-21 01:22:48 +00:00
|
|
|
class CProject {
|
|
|
|
|
|
|
|
private $dir;
|
|
|
|
private $projname;
|
|
|
|
private $shortdesc = '(no description)';
|
|
|
|
private $longdesc = '';
|
|
|
|
private $images = array();
|
|
|
|
private $downloads = array();
|
|
|
|
|
|
|
|
public function __construct($dirname, $projname) {
|
|
|
|
$this->dir = BASEDIR.'data/'.$dirname.'/';
|
|
|
|
$this->projname = $projname;
|
|
|
|
|
|
|
|
// Identify resources in folder
|
|
|
|
|
|
|
|
$ls = scandir($this->dir);
|
|
|
|
foreach($ls as $file) {
|
|
|
|
if ($file[0] == '.') continue;
|
|
|
|
|
|
|
|
if ($file == 'README.txt') {
|
|
|
|
$this->longdesc = file_get_contents($this->dir.'README.txt');
|
|
|
|
$this->shortdesc = array_shift(explode("\n", $this->longdesc));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_image($file)) {
|
|
|
|
$this->images[] = $file;
|
|
|
|
} else {
|
|
|
|
$this->downloads[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function write() {
|
|
|
|
|
|
|
|
// Generate image thumbnails
|
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
foreach($this->images as $idx => $image) {
|
|
|
|
$outfile = BASEDIR.'wwwroot/srv/'.$this->projname.'_'.$idx;
|
|
|
|
copy($this->dir.$image, $outfile.'.'.str_ext($image));
|
|
|
|
|
|
|
|
mkthumbnail($outfile.'.'.str_ext($image), $outfile.'_thumb.jpg', PAGE_THUMB_W, PAGE_THUMB_H);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy downloads to wwwroot
|
|
|
|
|
|
|
|
foreach($this->downloads as $idx => $filename) {
|
|
|
|
copy($this->dir.$filename, BASEDIR.'wwwroot/srv/'.$filename);
|
|
|
|
}
|
2013-09-21 01:22:48 +00:00
|
|
|
|
|
|
|
// Generate index page
|
2013-09-21 01:52:12 +00:00
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->index();
|
|
|
|
$idxfile = ob_get_clean();
|
|
|
|
file_put_contents(BASEDIR.'wwwroot/'.$this->projname.'.html', $idxfile);
|
2013-09-21 01:22:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
public function index() {
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title><?=hesc($this->projname)?> | <?=hesc(SITE_TITLE)?></title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h2><?=hesc($this->projname)?></h2>
|
|
|
|
|
|
|
|
<p><?=nl2br(hesc($this->longdesc))?></p>
|
|
|
|
|
|
|
|
<?php if (count($this->images)) { ?>
|
|
|
|
<h3>Images</h3>
|
|
|
|
<?php foreach($this->images as $idx => $origname) { ?>
|
|
|
|
<a href="srv/<?=hesc(urlencode($this->projname))?>_<?=$idx?>.<?=str_ext($origname)?>"><img src="srv/<?=hesc(urlencode($this->projname))?>_<?=$idx?>_thumb.jpg"></a>
|
|
|
|
<?php } ?>
|
|
|
|
<?php } ?>
|
|
|
|
|
|
|
|
<?php if (count($this->downloads)) { ?>
|
|
|
|
<h3>Files</h3>
|
|
|
|
<ul>
|
|
|
|
<?php foreach($this->downloads as $filename) { ?>
|
|
|
|
<li><a href="srv/<?=hesc(urlencode($filename))?>"><?=hesc($filename)?></a></li>
|
|
|
|
<?php } ?>
|
|
|
|
</ul>
|
|
|
|
<?php } ?>
|
|
|
|
</body>
|
|
|
|
</html>
|
2013-09-21 01:22:48 +00:00
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-09-21 01:22:48 +00:00
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
// Build all projects
|
2013-09-21 01:22:48 +00:00
|
|
|
|
|
|
|
foreach($projects as $dirname => $projectname) {
|
|
|
|
$pr = new CProject($dirname, $projectname);
|
|
|
|
print_r($pr);
|
2013-09-21 01:52:12 +00:00
|
|
|
|
|
|
|
$pr->write();
|
2013-09-21 01:22:48 +00:00
|
|
|
}
|
|
|
|
|
2013-09-21 01:52:12 +00:00
|
|
|
// Build index page
|
2013-09-21 01:22:48 +00:00
|
|
|
|
|
|
|
|