This repository has been archived on 2020-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
codesite/rebuild.php

197 lines
4.4 KiB
PHP

<?php
// Code-hosting website
// ````````````````````
define('BASEDIR', __DIR__.'\\');
define('SITE_TITLE', 'code.ivysaur.me');
define('PAGE_THUMB_W', 60);
define('PAGE_THUMB_H', 60);
// clean up wwwroot
if (file_exists(BASEDIR.'wwwroot')) {
rename(BASEDIR.'wwwroot', BASEDIR.'wwwroot.old.'.uniqid());
}
mkdir(BASEDIR.'wwwroot');
mkdir(BASEDIR.'wwwroot/srv');
// 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];
}
}
/**
* 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
*/
function mkthumbnail($src_file, $dest_file, $width, $height) {
list($src_width, $src_height) = getimagesize($src_file);
$im = imagecreatefromstring(file_get_contents($src_file));
$dest = imagecreatetruecolor($width, $height);
$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
);
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']);
}
function hesc($sz) {
return @htmlentities($sz, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
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
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);
}
// Generate index page
ob_start();
$this->index();
$idxfile = ob_get_clean();
file_put_contents(BASEDIR.'wwwroot/'.$this->projname.'.html', $idxfile);
}
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>
<?php
}
}
// Build all projects
foreach($projects as $dirname => $projectname) {
$pr = new CProject($dirname, $projectname);
print_r($pr);
$pr->write();
}
// Build index page