/*
* tpl_download.inc.php
*
*
* copyright 2004 Claus Muus
*
* $ID$
*/
require_once ("../cms/lib/database.inc.php");
require_once ("config.inc.php");
/**
* listen ber verfgbare Files abrufen
*
* \author Claus Muus
* \date 01/14/06
*/
class Download
{
var $new = 2; // Tage die ein file als neu gilt
var $activ = 7; // Tage die ein addon als aktiv git
var $newest = 0; // Zeitpunkt der Letzten Aktualesierung
var $distri = "MLD"; // Krzel der Distri
var $datadir = ""; // Loging und Cache Dir
var $downloaddir = "../../download/"; // Downlods Dir
var $docudir = "../../download/docs/"; // Docu Dir
var $path = ""; // Path to Files
var $wikiUrl = "http://www.vdr-wiki.de";
var $descriptions = array (); // Beschreibungen der Addons
var $dependencies = array (); // Abh�gigkeiten der Addons
var $groups = array (); // Gruppen der Addons
var $access = array (); // Download Statistik
var $active = array (); // Aktiv Statistik
var $orderBy = "name";
/**
* Initialesiert den Datenbankzugriff.
*/
function Download ($path)
{
// $db = new Database ();
$this->datadir = $GLOBALS['config']['data'];
$this->downloaddir = $GLOBALS['config']['download'].$path."/";
$this->docudir = $this->downloaddir."docs/";
$this->path = $path;
if (isset ($_REQUEST['debug'])) {
// Plugin Beschreibungen
if (($fp = @fopen ("{$this->datadir}description.txt", "r"))) {
$descriptions = unserialize (fread ($fp, filesize("{$this->datadir}description.txt")+1));
if (is_array ($descriptions)) {
$this->descriptions = $descriptions;
}
fclose($fp);
}
if (true) {
$cont = file_get_contents("{$this->wikiUrl}/wiki/index.php/Plugins");
// echo "{$this->wikiUrl}/wiki/index.php/Plugins";
// $cont = preg_replace ("/.*(.*?)<\/table>.*/s", "\\1", $cont);
// oder
// $cont = preg_replace ("/.*(.*)/s", "\\1", $cont);
// $cont = preg_replace ("/(.*)<\/table>.*/s", "\\1", $cont);
// oder
preg_match ("/id=\"A\"(.*)<\/table>/s", $cont, $erg);
$cont = $erg[1];
// echo $cont;
$cont = preg_replace ("/(wikiUrl}", $cont);
preg_match_all ("/ *(.*?)<\/a>.*?
(.*?)\n*<\/td>/s", $cont, $erg, PREG_SET_ORDER);
// print_r($erg);
foreach ($erg as $row) {
$this->descriptions[$row[2]]['name'] = $row[2];
$this->descriptions[$row[2]]['descript'] = preg_replace ("/<.*?>/s", "", $row[3]);
$this->descriptions[$row[2]]['url'] = $row[1];
}
$this->save ();
print_r ($this->descriptions);
}
} else {
// Plugin Beschreibungen
if (($fp = @fopen ("{$this->datadir}description.txt", "r"))) {
$descriptions = unserialize (fread ($fp, filesize("{$this->datadir}description.txt")+1));
if (is_array ($descriptions)) {
$this->descriptions = $descriptions;
}
fclose($fp);
}
$filetime = @filemtime ("{$this->datadir}description.txt");
if (!$filetime || ((time () - $filetime) / 60 / 60 / 24) > 1) {
$cont = file_get_contents("{$this->wikiUrl}/wiki/index.php/Plugins");
preg_match ("/id=\"A\"(.*)<\/table>/s", $cont, $erg);
$cont = $erg[1];
$cont = preg_replace ("/(wikiUrl}", $cont);
preg_match_all ("/ *(.*?)<\/a>.*? | (.*?)\n*<\/td>/s", $cont, $erg, PREG_SET_ORDER);
foreach ($erg as $row) {
$this->descriptions[$row[2]]['name'] = $row[2];
$this->descriptions[$row[2]]['descript'] = preg_replace ("/<.*?>/s", "", $row[3]);
$this->descriptions[$row[2]]['url'] = $row[1];
}
$this->save ();
}
}
// Abhaengigkeiten
if ($fp = @fopen ("{$this->downloaddir}dependencys.txt", "r")) {
while (!feof ($fp)) {
if (preg_match ("/(.*?)\t(.*)/s", fgets ($fp), $erg)) {
$this->dependencies[$erg[1]] = explode(" ", trim ($erg[2]));
}
}
fclose($fp);
}
// Gruppen
if ($fp = @fopen ("{$this->downloaddir}groups.txt", "r")) {
while (!feof ($fp)) {
if (preg_match ("/(.*?)\t(.*)/s", fgets ($fp), $erg)) {
$this->groups[$erg[1]] = trim ($erg[2]);
}
}
fclose($fp);
}
}
/**
* setzt Link zu einer Gruppe von Dateien
*/
function setGroup ($group, $href)
{
$this->descriptions['groups'][$group] = $href;
}
/**
* setzt beschreibung zu einer Datei
*/
function set ($name, $descript, $href)
{
$descript = preg_replace ("/<.*?>/s", "", $descript);
$descript = preg_replace ("/\s\s/s", " ", $descript);
$this->descriptions[$name] = array ('name' => $name, 'descript' => $descript, 'url' => $href);
}
/**
* holt neue downloads als Nachrichten
*/
function get ($count=0, $class="", $archs=null)
{
$files = array ();
if ($this->path) {
if ($class && $archs) {
$files = $this->getFiles ("", $class, $archs, "name", "!lib");
} else {
foreach ($this->getDirs () as $dir) {
$files = array_merge($files, $this->getFiles ($dir, "", null, "name", "!lib"));
}
}
$this->orderBy = "time";
uasort ($files, array ($this, "cmp"));
if ($count) {
$files = array_slice ($files, 0, $count);
}
}
return $files;
}
// function getDependencies ()
// {
// return json_encode($this->dependencies);
// }
function getVersions ()
{
$dirs = array ();
$d = dir ($GLOBALS['config']['download']);
while ($f = $d->read ()) {
if ($f != "." && $f != ".." && is_dir ($d->path.$f)) {
$dirs[] = $f;
}
}
sort ($dirs);
return $dirs;
}
function getDirs ($dir="")
{
$dirs = array ();
if (is_dir ($this->downloaddir.$dir)) {
$d = dir ($this->downloaddir.$dir);
while ($f = $d->read ()) {
if ($f != "." && $f != ".." && $f != "docs" && is_dir ($d->path.$f)) {
$dirs[] = $dir.$f;
$dirs = array_merge ($dirs, $this->getDirs ($dir.$f."/"));
}
}
}
sort ($dirs);
return $dirs;
}
function getArch ($dir, $class)
{
return @file($this->downloaddir.$class."/".$dir."/arch", FILE_IGNORE_NEW_LINES);
}
function getFiles ($dir, $class="", $archs=null, $orderBy="name", $match="")
{
$db = new Database ("data", $GLOBALS['config']['db']);
$files = array ();
$this->orderBy = $orderBy;
$archs = $archs && !$dir ? $archs : array("");
$dir = ($class && $class!=1 ? $class."/" : "") . $dir;
if (!is_dir ($this->downloaddir . $dir)) {
$newdir = "";
$basedir = preg_replace ("/(.*\/).*|.*/", "\\1", $dir);
if (is_dir ($this->downloaddir . $basedir)) {
if ($d = dir ($this->downloaddir . $basedir)) {
while ($f = $d->read ()) {
if (is_dir ($d->path.$f) && strpos ($d->path.$f, "{$this->downloaddir}$dir")===0 && $basedir.$f > $newdir) {
preg_match ("/(.*)-(.*)/", $f, $values);
if (!$class || $db->query ("SELECT id FROM state WHERE name='$values[1]' AND version LIKE '$values[2]_%' AND state>0")) {
$newdir = $basedir.$f;
}
}
}
$d->close ();
}
}
$dir = $newdir;
}
foreach ($archs as $arch) {
$arch = $arch ? "/$arch" : "";
if ($fp = @fopen ($this->downloaddir . $dir . $arch . "/dependencys.txt", "r")) {
while (!feof ($fp)) {
if (preg_match ("/(.*?)\t(.*)/s", fgets ($fp), $erg)) {
$this->dependencies[$erg[1]] = explode(" ", trim ($erg[2]));
}
}
fclose($fp);
}
}
// check for cache
$arch = implode ("_", $archs);
$this->cachename = preg_replace ("|/|", "", "{$this->path}_{$dir}_{$arch}_{$match}_{$class}");
$filename = $this->datadir."files/".$this->cachename.".txt";
$filetime = 0;
foreach ($archs as $arch) {
$filetime = max ($filetime, @filemtime ($this->downloaddir.$dir."/".$arch));
}
//if (!isset($_GET['debug2']) && (@filemtime ($filename) >= $filetime+60*10 && @filemtime ($filename) > (time()-60*60))) {
if (!isset($_GET['debug2']) && file_exists($filename) && @filemtime($filename) >= $filetime) {
$this->newest = filemtime ($filename);
$files = @unserialize (@file_get_contents($filename));
uasort ($files, array ($this, "cmp"));
return $files;
}
foreach ($archs as $arch) {
$arch = $arch ? "/$arch" : "";
if (is_dir ($this->downloaddir . $dir . $arch)) {
if ($d = dir ($this->downloaddir . $dir . $arch . "/")) {
$docudir = file_exists($this->docudir) ? $this->docudir : $d->path."/docs/";
$ducupath = file_exists($this->docudir) ? "/download/{$this->path}/docs/" : "/download/{$this->path}/$dir$arch/docs/";
while ($f = $d->read ()) {
if ($match && ($match[0]!='!' && !preg_match ("/^$match/", $f) || $match[0]=='!' && preg_match ("/^".substr($match,1)."/", $f)))
continue;
if (is_file ($d->path.$f)) {
$name = "";
$group = "";
$rev = 0;
if (preg_match ("/(.+)_(.+)-(.+)_(.+)\.(deb)/", $f, $erg)) {
// V5 Pakete
$name = $erg[1];
$typ = $erg[5];
$sys = $this->path;
$ver = $erg[2]."-".$erg[3];
$rev = $erg[3];
//$arch = $erg[4];
$url = isset ($this->descriptions[$name]['url']) ? $this->descriptions[$name]['url'] : "";
$control = shell_exec("HOME=/var/www dpkg -f ".$d->path.$f);
$this->groups[$name] = preg_replace("/.*Section: ([a-z]*).*|.*/sm", "\\1", $control);
$this->groups[$name] = !empty ($this->groups[$name]) ? $this->groups[$name] : "other";
$desc = preg_replace("/.*Description: ([^\n]*).*|.*/sm", "\\1", $control);
$this->descriptions[$name]['descript'] = $desc ? $desc : (isset($this->descriptions[$name]['descript']) ? $this->descriptions[$name]['descript'] : "");
}
if (preg_match ("/(.+)[-_](.+)-(.+)\.(opk)/", $f, $erg)) {
// V4 Pakete
$name = $erg[1];
$typ = $erg[4];
$sys = $this->path;
$ver = $erg[2]."-".$erg[3];
$rev = $erg[3];
$url = isset ($this->descriptions[$name]['url']) ? $this->descriptions[$name]['url'] : "";
$control = file_get_contents(preg_replace("/(.*)\/packages\/(.*)\.opk/", "\\1/controls/\\2", $d->path.$f));
$this->groups[$name] = preg_replace("/.*Section: ([a-z]*).*|.*/sm", "\\1", $control);
$this->groups[$name] = !empty ($this->groups[$name]) ? $this->groups[$name] : "other";
$desc = preg_replace("/.*Description: ([^\n]*).*|.*/sm", "\\1", $control);
$this->descriptions[$name]['descript'] = $desc ? $desc : (isset($this->descriptions[$name]['descript']) ? $this->descriptions[$name]['descript'] : "");
}
if (preg_match ("/{$this->distri}-(0.[^_]+)(_([a-z]{2}))?_([^_]*?)(-\d.*)\.(.{2,3})/", $f, $erg)) {
// old Installers
$name = $erg[4];
$typ = $erg[6];
$ver = $erg[1].$erg[5];
$sys = $erg[1];
$url = "/wiki/mld/installation/installation";
}
if (preg_match ("/(\d{2}){$this->distri}-(0.[^_]+)(_([a-z]{2}))?(_.*)?_([^_]+?)-(\d.*)\.(tgz|tar)/", $f, $erg)) {
// old Addons
$name = $erg[6];
$typ = $erg[8];
$ver = $erg[7];
$sys = $erg[2];
$url = isset ($this->descriptions[$name]['url']) ? $this->descriptions[$name]['url'] : "";
}
if (preg_match ("/addons(_([a-z]{2}))?_([^_]*?)-(\d.*)(\.tgz)/", $f, $erg)) {
// old Devel Files
$name = $erg[3];
$typ = $erg[5];
$ver = $erg[4];
$sys = "0.6.0";
$url = "{$this->wikiUrl}/wiki/index.php/MLD_-_Entwicklungs_Hilfsmittel";
}
if (preg_match ("/{$this->distri}-(.+)_(.+)-(.+)_(.+)\.(iso|tgz)/", $f, $erg)) {
// V2/V3 Installers
$name = $erg[2];
$typ = $erg[5];
$ver = $erg[1]."-".$erg[3]."_".$erg[4];
$rev = ($erg[5] == "iso") ? $erg[3] : $erg[4];
$sys = $erg[1];
$url = "/wiki/mld/installation/installation";
$this->groups[$name] = "";
}
if (preg_match ("/{$this->distri}-(4\..+)_(.+)_(.+)-(.+)\.(iso|tgz|gz)/", $f, $erg)) {
// V4 Installers
$name = $erg[2];
$typ = $erg[5];
$ver = $erg[1]."-".$erg[3]."-".$erg[4];
$rev = ($erg[5] == "iso") ? $erg[3] : $erg[4];
$sys = $erg[1];
$url = "/wiki/mld/installation/installation";
$this->groups[$name] = "";
}
if (preg_match ("/{$this->distri}-(5.*)_(.+)_(.+)-(.+)_.*\.(iso|tgz|img.gz|img)/", $f, $erg)) {
// V5 Installers
$name = $erg[2];
$typ = $erg[5];
$ver = $erg[1]."-".$erg[3]."-".$erg[4];
$rev = ($erg[5] == "iso") ? $erg[3] : $erg[4];
$sys = $erg[1];
$url = "/wiki/mld/installation/installation";
$this->groups[$name] = "";
}
if (preg_match ("/\d{2}_{$this->distri}-([^_]+)_(.*_)?([^_]+)-(.+)_(.+)\.(tgz)/", $f, $erg)) {
// V2 Addons
$name = $erg[3];
$rev = $erg[5];
$typ = $erg[6];
$ver = preg_replace ("/.*-/", "", $erg[2]).$erg[4]."_".$erg[5];
$sys = $erg[1];
$url = isset ($this->descriptions[$name]['url']) ? $this->descriptions[$name]['url'] : "";
}
if (preg_match ("/{$this->distri}-(3\.[^_]+)_(.*_)?([^_]+)-(.+)_(.+)\.(sfs|gz)/", $f, $erg)) {
// V3 Addons
$name = $erg[3];
$group = $erg[2];
$typ = $erg[6];
$ver = preg_replace ("/.*-/", "", $erg[2]).$erg[4]."_".$erg[5];
$rev = $erg[5];
$sys = $erg[1];
$url = isset ($this->descriptions[$name]['url']) ? $this->descriptions[$name]['url'] : "";
//$this->groups[$name] = isset ($this->groups[$name]) ? $this->groups[$name] : (strpos($group, "vdr")===0 ? "vdr-plugins" : "other");
$this->groups[$name] = isset ($this->groups[$name]) ? $this->groups[$name] : "other";
}
if ($name && !in_array ($name, $GLOBALS['config']['extra_addons'])) {
if ($db->query ("SELECT UNIX_TIMESTAMP(time) AS time, state, access FROM state WHERE filename='$f'")) {
$ftime = filemtime ($d->path.$f);
$time = $db['time'];
$state = $db['state'];
$access = $db['access'];
} else {
$ftime = filemtime ($d->path.$f);
if (file_exists ("$docudir$name/history")) {
$time = strtotime(preg_replace("/.*(\d\d\d\d-\d\d-\d\d).*|.*/", "\\1", fgets(fopen("$docudir$name/history", 'r'))));
} else {
$time = $ftime;
}
$state = 0;
$access = 0;
$db->query ("INSERT INTO state (name, version, system_version, filename, time, state, access) VALUES ('$name', '$ver', '$sys', '$f', FROM_UNIXTIME($time), 0, 0)");
}
if ($db->query ("SELECT SUM(access) AS access_sum FROM state WHERE name='$name' AND system_version LIKE '$sys%'") && $db['access_sum']) {
$files[$name]['accesssum'] = $db['access_sum'];
} else {
$files[$name]['accesssum'] = 0;
}
$oldstate = 0;
//if (!$state && $db->query ("SELECT state from state WHERE name='$name' AND system_version='$sys' AND state!=0 HAVING MAX(version)")) { // geht nicht für MLD-0.6.0
if (!$state && $db->query ("SELECT state, version FROM state WHERE name='$name' AND state!=0 ORDER BY version DESC LIMIT 1")) {
$oldstate = $db['state'];
}
if ($group && !isset ($groupstate[$group])) {
preg_match ("/(.*)-(.*)/", $group, $groupValues);
if ($db->query ("SELECT state FROM state WHERE name='$groupValues[1]' AND state>0 AND version LIKE '$groupValues[2]%'")) {
$groupstate[$group] = $db['state'];
} else {
$groupstate[$group] = 0;
}
}
if ($db->query ("SELECT COUNT(id) AS active_count FROM active WHERE name='$name' AND version LIKE '$sys%'") && $db['active_count']) {
$files[$name]['active'] = $db['active_count'];
} else {
$files[$name]['active'] = 0;
}
if (($class!=1 || ($class == 1 && (!$group && $state>0 || isset ($groupstate[$group]) && $groupstate[$group]>0 && $state>=0 && ($oldstate>0 || $state>0)))) && (empty ($files[$name]['time']) || ($files[$name]['time']<=$time && ($typ=="iso" || $files[$name]['typ']==$typ) || $typ=="iso" && $files[$name]['typ']!="iso"))) {
$files[$name]['name'] = $name;
$files[$name]['descript'] = isset ($this->descriptions[$name]['descript']) ? $this->descriptions[$name]['descript'] : (isset ($this->descriptions[preg_replace("/vdr-plugin-/", "", $name)]['descript']) ? $this->descriptions[preg_replace("/vdr-plugin-/", "", $name)]['descript'] : "");
$files[$name]['readme'] = (file_exists ("$docudir$name/readme")) ? "$ducupath$name/readme" : "";
$files[$name]['history'] = (file_exists ("$docudir$name/history")) ? "$ducupath$name/history" : "";
$files[$name]['handling'] = (file_exists ("$docudir$name/handling")) ? "$ducupath$name/handling" : "";
$files[$name]['settings'] = (file_exists ("$docudir$name/settings")) ? "$ducupath$name/settings" : "";
$files[$name]['depend'] = isset ($this->dependencies[$name]) ? $this->dependencies[$name] : array();
$files[$name]['group'] = isset ($this->groups[$name]) ? $this->groups[$name] : "";
$files[$name]['url'] = $url;
$files[$name]['href'] = "/download/{$this->path}/files" . substr ($d->path.$f, strlen ($GLOBALS['config']['download'].$this->path));
$files[$name]['hisfile'] = (file_exists ("$docudir$name/history")) ? "$docudir$name/history" : "";
$files[$name]['dir'] = $dir . $arch;
$files[$name]['path'] = $d->path;
$files[$name]['filename'] = $f;
$files[$name]['typ'] = $typ;
$files[$name]['version'] = $ver;
$files[$name]['revision'] = $rev;
$files[$name]['size'] = round (filesize ($d->path.$f) / 1024);
$files[$name]['neu'] = ((time () - $time) / 60 / 60 / 24) < $this->new;
$files[$name]['time'] = $time;
$files[$name]['ftime'] = date ("d.m.Y H:i:s", $ftime);
$files[$name]['access'] = $access;
$files[$name]['state'] = $state ? $state : $oldstate;
$files[$name]['state_actual'] = $state;
$this->newest = ($time > $this->newest) ? $time : $this->newest;
}
if ($ver) {
$v = preg_replace ("/(.*)_(.*)/", "\\2_\\1", $ver);
$files[$name]['versions'][$v]['href'] = "/download/{$this->path}/files" . substr ($d->path.$f, strlen ($GLOBALS['config']['download'].$this->path));
$files[$name]['versions'][$v]['version'] = $ver;
$files[$name]['versions'][$v]['revision'] = $rev;
$files[$name]['versions'][$v]['ftime'] = date ("d.m.Y", $time);
$files[$name]['versions'][$v]['state'] = $state;
}
}
}
}
$d->close ();
}
}
}
foreach ($files as $name => $file) {
if (empty ($files[$name]['name'])) {
unset ($files[$name]);
} else if (isset ($files[$name]['versions'])) {
krsort ($files[$name]['versions']);
}
}
// write cache
@file_put_contents($filename, @serialize ($files));
uasort ($files, array ($this, "cmp"));
return $files;
}
function getNewestDate ()
{
return date ("d.m.Y", $this->newest);
}
function save ()
{
if ($fp = @fopen ("{$this->datadir}description.txt", "w")) {
fwrite ($fp, serialize($this->descriptions));
fclose($fp);
}
}
// Addons Sortieren
function cmp ($a, $b)
{
if (in_array($this->orderBy, array('active', 'time', 'access', 'accesssum'))) {
$cmp = $b[$this->orderBy] > $a[$this->orderBy] ? 1 : ($b[$this->orderBy] < $a[$this->orderBy] ? -1 : 0);
} else {
$cmp = strcasecmp ($a[$this->orderBy], $b[$this->orderBy]);
}
return $cmp ? $cmp :strcasecmp ($a['name'], $b['name']);
}
}
?>
|