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

106 lines
2.1 KiB
PHP

<?php
// Code-hosting website
// ````````````````````
define('BASEDIR', __DIR__.'\\');
// clean up wwwroot
if (file_exists(BASEDIR.'wwwroot')) {
rename(BASEDIR.'wwwroot', BASEDIR.'wwwroot.old.'.uniqid());
}
mkdir(BASEDIR.'wwwroot');
// 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];
}
}
function mkthumbnail($src_file, $dest_file, $width, $height) {
$im = imagecreatefromstring(file_get_contents($src_file));
$dest = imagecreatetruecolor($width, $height);
// TODO
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']);
}
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() {
// Build file mapping
// Generate image thumbnails
// Copy data files to wwwroot
// Generate index page
}
}
// Format project
$pagetpl = @file_get_contents(BASEDIR.'page_template.htm');
if ($pagetpl === false) die('Missing page_template.htm');
foreach($projects as $dirname => $projectname) {
$pr = new CProject($dirname, $projectname);
print_r($pr);
}
// Index page