2016-04-18 07:04:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CProject {
|
|
|
|
|
|
|
|
private $dir;
|
2016-04-18 08:02:53 +00:00
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
public $projname;
|
|
|
|
public $shortdesc = '(no description)';
|
|
|
|
public $subtag = '';
|
|
|
|
public $lastupdate = 0;
|
2016-04-18 08:02:53 +00:00
|
|
|
public $numreleases = 0;
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
private $longdesc = '';
|
|
|
|
private $prefix_html = '';
|
|
|
|
private $images = array();
|
|
|
|
private $downloads = array();
|
|
|
|
private $downloads_hashes = array();
|
2016-04-18 08:02:53 +00:00
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
public $downloads_section_was_replaced = false;
|
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
public $lifespan = 0;
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
public $tags = array();
|
|
|
|
|
|
|
|
public $homeimage = null;
|
|
|
|
|
|
|
|
public function __construct($dirname, $projname) {
|
|
|
|
$this->dir = BASEDIR.'data/'.$dirname.'/';
|
|
|
|
$this->projname = $projname;
|
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
$matches = [];
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
// Identify resources in folder
|
|
|
|
|
|
|
|
$ls = scandir($this->dir);
|
|
|
|
$found_real_lastupdate = false;
|
|
|
|
foreach($ls as $file) {
|
2016-04-18 08:49:56 +00:00
|
|
|
if ($file[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-18 07:04:52 +00:00
|
|
|
|
|
|
|
if ($file == 'README.txt') {
|
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
$this->longdesc = file_get_contents($this->dir.'README.txt');
|
2016-04-18 08:49:56 +00:00
|
|
|
$this->longdesc = str_replace("\r", "", $this->longdesc); // filter windows CR
|
2016-04-19 07:20:00 +00:00
|
|
|
$this->longdesc = preg_replace("~[\s\t]*$~s", "", $this->longdesc); // filter trailing spaces at line endings
|
2016-04-19 07:34:52 +00:00
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
// Guess 'last update' time
|
|
|
|
if (preg_match('~\n(\d\d\d\d-\d\d-\d\d)~', $this->longdesc, $matches)) {
|
|
|
|
// Use first date entry (assumed to be a CHANGELOG)
|
|
|
|
$this->lastupdate = strtotime($matches[1]);
|
|
|
|
$found_real_lastupdate = true;
|
|
|
|
}
|
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
// Find number of releases
|
|
|
|
preg_match_all('~\n(\d\d\d\d-\d\d-\d\d)~', $this->longdesc, $matches, PREG_SET_ORDER);
|
|
|
|
$this->numreleases = count($matches);
|
|
|
|
|
|
|
|
// Find support lifespan (newest minus youngest)
|
|
|
|
$eldest = time();
|
|
|
|
$newest = 0;
|
|
|
|
foreach($matches as $match) {
|
|
|
|
$stamp = strtotime($match[1]);
|
|
|
|
$eldest = min($stamp, $eldest);
|
|
|
|
$newest = max($stamp, $newest);
|
|
|
|
}
|
|
|
|
$this->lifespan = floor(max(0, $newest - $eldest) / 3600); // could divide by 86400 but it doesn't matter
|
2016-04-18 07:04:52 +00:00
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
// Find 'written in'
|
2016-04-18 08:49:56 +00:00
|
|
|
if (preg_match('~Written in ([^\\n]+)~', $this->longdesc, $matches)) {
|
2016-04-18 07:04:52 +00:00
|
|
|
$this->subtag = rtrim($matches[1], ' .');
|
|
|
|
}
|
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
// Find tags
|
2016-04-18 08:49:56 +00:00
|
|
|
if (preg_match('~Tags: ([^\\n]+)~', $this->longdesc, $matches)) {
|
2016-04-18 07:04:52 +00:00
|
|
|
$this->tags = array_map('trim', explode(',', $matches[1]));
|
|
|
|
}
|
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
// Extract short description
|
2016-04-18 07:04:52 +00:00
|
|
|
$parts = explode("\n", $this->longdesc);
|
|
|
|
$this->shortdesc = array_shift($parts);
|
|
|
|
$this->shortdesc[0] = strtolower($this->shortdesc[0]); // cosmetic lowercase
|
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
// Filters for longdesc
|
2016-04-18 07:04:52 +00:00
|
|
|
|
|
|
|
$prefix_html = '';
|
2016-04-18 08:49:56 +00:00
|
|
|
$this->longdesc = preg_replace_callback('~\nWritten in ([^\\n]+)~ms', function($matches) use (&$prefix_html) {
|
2016-04-18 07:04:52 +00:00
|
|
|
$prefix_html .= (
|
|
|
|
(SHIELDS_PREFIX ? mkshield('build', 'success', 'brightgreen').' ' : '').
|
|
|
|
mkshield('written in', rtrim($matches[1], '.'), 'blue')
|
|
|
|
);
|
|
|
|
return '';
|
|
|
|
}, $this->longdesc);
|
|
|
|
|
|
|
|
while(strpos($this->longdesc, "\n\n\n") !== false) {
|
|
|
|
$this->longdesc = str_replace("\n\n\n", "\n\n", $this->longdesc);
|
|
|
|
}
|
2016-04-19 07:20:00 +00:00
|
|
|
$this->longdesc = rtrim($this->longdesc, "\n")."\n";
|
2016-04-18 07:04:52 +00:00
|
|
|
|
|
|
|
$this->prefix_html = $prefix_html;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $found_real_lastupdate) {
|
|
|
|
$this->lastupdate = max(
|
|
|
|
$this->lastupdate,
|
|
|
|
// filectime($this->dir.$file),
|
|
|
|
($file == 'README.txt' ? filectime($this->dir.$file) : filemtime($this->dir.$file)) // Don't count README updates
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_image($file)) {
|
|
|
|
$this->images[] = $file;
|
|
|
|
} else {
|
|
|
|
$this->downloads[] = $file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
natcasesort($this->downloads);
|
|
|
|
$this->downloads = array_reverse($this->downloads);
|
|
|
|
|
|
|
|
for($i = 0, $e = count($this->downloads); $i !== $e; ++$i) {
|
|
|
|
$this->downloads_hashes[] = (
|
|
|
|
sha1_file($this->dir.$this->downloads[$i])
|
|
|
|
);
|
|
|
|
}
|
2016-04-18 08:49:56 +00:00
|
|
|
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function genHomeImage() {
|
|
|
|
if (count($this->images)) {
|
|
|
|
|
|
|
|
$this->homeimage = mkthumbnail(
|
|
|
|
$this->dir.$this->images[0],
|
|
|
|
null, // raw handle
|
|
|
|
INDEX_THUMB_W, INDEX_THUMB_H
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
protected function filterLongDescArea() {
|
|
|
|
|
|
|
|
// If *all* downloads can be assigned to a changelog release entry,
|
|
|
|
// then move the files
|
|
|
|
// A release entry is marked by any string following the date field.
|
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
// Add one more NL than we really want, for regex reasons
|
|
|
|
$this->longdesc .= "\n";
|
|
|
|
|
|
|
|
preg_match_all('~^(\d\d\d\d-\d\d-\d\d)\s?:? (.+?)\n\n~ms', $this->longdesc, $matches, PREG_SET_ORDER);
|
2016-04-18 08:49:56 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
// Ensure changelog exists
|
|
|
|
if (! count($matches)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find all tags
|
|
|
|
$known_tags = [];
|
|
|
|
foreach($matches as $i => $match) {
|
|
|
|
$tag = trim(explode("\n", $match[2])[0]);
|
|
|
|
$known_tags[$tag] = $i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure all release entries have tags
|
|
|
|
if (count($matches) != count($known_tags)) {
|
|
|
|
error_log("[".$this->projname."] not all release entries have tags\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
// Ensure all downloads can be assigned to tags.
|
|
|
|
// In the event of a download matching multiple tags, it'll
|
|
|
|
// be assigned to the newest (topmost) entry
|
2016-04-18 08:49:56 +00:00
|
|
|
$found_idx = [];
|
2016-04-19 07:20:00 +00:00
|
|
|
$render_per_tag = [];
|
2016-04-19 07:34:52 +00:00
|
|
|
foreach(array_keys($known_tags) as $tagname) {
|
|
|
|
$render_per_tag[$tagname] = [];
|
|
|
|
}
|
2016-04-18 08:49:56 +00:00
|
|
|
foreach($this->downloads as $idx => $filename) {
|
|
|
|
foreach(array_keys($known_tags) as $tagname) {
|
|
|
|
if (stripos($filename, $tagname) !== false) {
|
2016-04-19 07:20:00 +00:00
|
|
|
$found_idx[$idx] = $tagname;
|
|
|
|
|
|
|
|
$render_per_tag[$tagname][$idx] = $filename;
|
|
|
|
break; // next file
|
2016-04-18 08:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count($found_idx) != count($this->downloads)) {
|
|
|
|
error_log("[".$this->projname."] not all downloads have matching tags");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make HTML modifications
|
|
|
|
foreach($known_tags as $tag_name => $tag_idx) {
|
|
|
|
$find = rtrim($matches[$tag_idx][0]);
|
|
|
|
$this->longdesc = str_replace(
|
|
|
|
$find,
|
|
|
|
$find.'${{TAG_'.$tag_idx.'}}',
|
|
|
|
$this->longdesc
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
$this->longdesc = substr($this->longdesc, 0, strlen($this->longdesc)-1); // Strip the extra NL we added
|
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
$this->longdesc = text2html($this->longdesc);
|
|
|
|
foreach($known_tags as $tag_name => $tag_idx) {
|
2016-04-19 07:20:00 +00:00
|
|
|
$this->longdesc = str_replace(
|
|
|
|
'${{TAG_'.$tag_idx.'}}',
|
|
|
|
$this->renderDownloadsBlock($render_per_tag[$tag_name], false),
|
|
|
|
$this->longdesc
|
|
|
|
);
|
2016-04-18 08:49:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
$this->longdesc = str_replace("</ul>\n<br />", "</ul>", $this->longdesc);
|
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
// Skip displaying the global downloads area
|
|
|
|
// This flag also indicates that the content has been pre-HTMLified
|
|
|
|
$this->downloads_section_was_replaced = true;
|
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
// Successful upgrade
|
2016-04-18 08:49:56 +00:00
|
|
|
|
|
|
|
} while(false);
|
2016-04-19 07:20:00 +00:00
|
|
|
|
|
|
|
if (! $this->downloads_section_was_replaced) {
|
|
|
|
$this->longdesc = substr($this->longdesc, 0, strlen($this->longdesc)-1); // Strip the extra NL we added
|
|
|
|
}
|
2016-04-18 08:49:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 08:02:53 +00:00
|
|
|
public function numDownloads() {
|
|
|
|
return count($this->downloads);
|
|
|
|
}
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
public function write() {
|
|
|
|
|
|
|
|
// Generate image thumbnails
|
|
|
|
|
|
|
|
foreach($this->images as $idx => $image) {
|
2016-04-18 07:42:40 +00:00
|
|
|
$outfile = BASEDIR.'wwwroot/img/'.$this->projname.'_'.$idx;
|
2016-04-18 07:04:52 +00:00
|
|
|
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) {
|
|
|
|
$cmkdir = @mkdir( BASEDIR.'wwwroot/srv/'.$this->downloads_hashes[$idx] );
|
|
|
|
|
|
|
|
if (! $cmkdir) {
|
|
|
|
fputs(
|
|
|
|
STDOUT,
|
|
|
|
"WARNING: Couldn't create directory ".$this->downloads_hashes[$idx].
|
|
|
|
" for file '${filename}'".
|
|
|
|
" in project '".$this->projname."'!\n"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(
|
|
|
|
$this->dir.$filename,
|
|
|
|
BASEDIR.'wwwroot/srv/'.$this->downloads_hashes[$idx].'/'.$filename
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate index page
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$this->index();
|
|
|
|
$idxfile = template($this->projname.' | '.SITE_TITLE, ob_get_clean());
|
|
|
|
file_put_contents(BASEDIR.'wwwroot/'.$this->projname.'.html', $idxfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClassAttr() {
|
|
|
|
if (count($this->tags)) {
|
|
|
|
return 'taggedWith-'.implode(' taggedWith-', $this->tags);
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
public function renderDownloadsBlock($render_downloads, $include_header=false) {
|
2016-04-18 08:49:56 +00:00
|
|
|
if (! count($render_downloads)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
?>
|
|
|
|
<?php if ($include_header) { ?>
|
|
|
|
<strong>DOWNLOAD</strong>
|
|
|
|
<?php } ?>
|
|
|
|
<ul class="<?=$include_header ? 'downloads-large' : 'downloads-small' ?>">
|
|
|
|
<?php foreach($render_downloads as $idx => $filename) { ?>
|
|
|
|
<li>
|
|
|
|
<a href="srv/<?=hesc($this->downloads_hashes[$idx])?>/<?=hesc(rawurlencode($filename))?>"><?=hesc($filename)?></a>
|
|
|
|
<small>
|
|
|
|
<?=hesc(fbytes(filesize(BASEDIR.'wwwroot/srv/'.$this->downloads_hashes[$idx].'/'.$filename)))?>
|
|
|
|
</small>
|
|
|
|
</li>
|
|
|
|
<?php } ?>
|
|
|
|
</ul>
|
|
|
|
<?php
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
public function index() {
|
2016-04-18 08:49:56 +00:00
|
|
|
|
|
|
|
$this->filterLongDescArea();
|
|
|
|
$longdesc_html = $this->downloads_section_was_replaced ? $this->longdesc : text2html($this->longdesc);
|
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
?>
|
|
|
|
<h2><?=hesc(str_replace('_', ' ', $this->projname))?></h2>
|
|
|
|
|
|
|
|
<div class="projinfo">
|
|
|
|
|
|
|
|
<div class="projbody projbody_<?=(count($this->images) ? 'half' : 'full')?>w">
|
|
|
|
|
|
|
|
<?php if (strlen($this->prefix_html)) { ?>
|
|
|
|
<p style="margin-top:0;"><?=$this->prefix_html?></p>
|
|
|
|
<?php } ?>
|
|
|
|
|
|
|
|
<strong><?=hesc(strtoupper(ARTICLE_HEADER))?></strong>
|
2016-04-18 08:49:56 +00:00
|
|
|
|
2016-04-19 07:20:00 +00:00
|
|
|
<div class="content-paragraph">
|
|
|
|
<?=$longdesc_html?>
|
|
|
|
</div>
|
2016-04-18 07:04:52 +00:00
|
|
|
|
|
|
|
<?=file_get_contents(BASEDIR.'/footer.htm')?>
|
|
|
|
|
2016-04-18 08:49:56 +00:00
|
|
|
<?php if (! $this->downloads_section_was_replaced) { ?>
|
2016-04-19 07:20:00 +00:00
|
|
|
<?=$this->renderDownloadsBlock($this->downloads, true)?>
|
2016-04-18 07:04:52 +00:00
|
|
|
<?php } ?>
|
2016-04-18 08:49:56 +00:00
|
|
|
|
2016-04-18 07:04:52 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php if (count($this->images)) { ?>
|
|
|
|
<div class="projimg">
|
|
|
|
<?php foreach($this->images as $idx => $origname) { ?>
|
2016-04-18 08:02:53 +00:00
|
|
|
<a href="img/<?=hesc(urlencode($this->projname))?>_<?=$idx?>.<?=str_ext($origname)?>"><img src="img/<?=hesc(urlencode($this->projname))?>_<?=$idx?>_thumb.jpg" class="thumbimage"></a>
|
2016-04-18 07:04:52 +00:00
|
|
|
<?php } ?>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div style="clear:both;"></div>
|
|
|
|
|
|
|
|
<?php } ?>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<?php
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|