migrated ApsInstaller and ApsUpdater, fixed typo in traffic-cron (thx to Sephi), refs #1287

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2013-11-20 15:02:49 +01:00
parent cf2eea43c9
commit ee19c268cc
5 changed files with 340 additions and 360 deletions

View File

@@ -24,10 +24,9 @@
* move url for distributionserver into panel
*/
class ApsUpdater extends ApsParser
{
class ApsUpdater extends ApsParser {
private $settings = array();
private $db = false;
private $RequestDomain = '';
private $RootUrl = '';
private $RootDir = '';
@@ -41,13 +40,10 @@ class ApsUpdater extends ApsParser
/**
* constructor of class. setup some basic variables needed by class
*
* @param db instance of the database class
* @param cronlog instance of the froxlor logger
*/
public function __construct($cronlog) {
public function __construct($db, $cronlog)
{
$this->db = $db;
$this->RequestDomain = 'apscatalog.com';
$this->RootUrl = '/1/';
$this->RootDir = dirname(dirname(dirname(dirname(__FILE__)))) . '/';
@@ -57,126 +53,114 @@ class ApsUpdater extends ApsParser
/**
* Main function of class which handles all around the update mechanism
*/
public function UpdateHandler() {
public function UpdateHandler()
{
$this->_cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Changing directory to '" . $this->RootDir . "'");
chdir($this->RootDir);
//return if allow_url_fopen is disabled
if(!ini_get('allow_url_fopen'))
{
if (!ini_get('allow_url_fopen')) {
$this->_cronlog->logAction(CRON_ACTION, LOG_ERROR, "The APS updater cronjob requires that allow_url_fopen is enabled for the PHP CLI binary!");
echo "The APS updater cronjob requires that allow_url_fopen is enabled for the PHP CLI binary!\n";
return;
}
//return if no task exists
$Result_stmt = Database::query("SELECT * FROM `" . TABLE_APS_TASKS . "` WHERE `Task` IN (" . TASK_SYSTEM_UPDATE . ", " . TASK_SYSTEM_DOWNLOAD . ")");
$Result = $this->db->query('SELECT * FROM `' . TABLE_APS_TASKS . '` WHERE `Task` IN (' . TASK_SYSTEM_UPDATE . ', ' . TASK_SYSTEM_DOWNLOAD . ')');
if($this->db->num_rows($Result) == 0)
{
if (Database::num_rows() == 0) {
$this->_cronlog->logAction(CRON_ACTION, LOG_NOTICE, "No tasks for ApsUpdater");
return;
}
//query first task -> updater can only do one job within a run
$Task = $this->db->fetch_array($Result);
$this->db->query('DELETE FROM `' . TABLE_APS_TASKS . '` WHERE `Task` = ' . $Task['Task']);
$Task = $Result_stmt->fetch(PDO::FETCH_ASSOC);
$del_stmt = Database::prepare("
DELETE FROM `" . TABLE_APS_TASKS . "` WHERE `Task` = :task
");
Database::pexecute($del_stmt, array('task' => $Task['Task']));
//fetch all vendors
$this->_cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Fetching all Vendors from '" . $this->RootUrl . "'");
$Vendors = self::FetchSubUrls($this->RootUrl);
if($Vendors !== false)
{
foreach($Vendors as $Vendor)
{
if ($Vendors !== false) {
foreach ($Vendors as $Vendor) {
//fetch all applications from vendors
$this->_cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Fetching all from Vendor '" . $Vendor. "'");
$Applications = self::FetchSubUrls($this->RootUrl . $Vendor);
if($Applications !== false)
{
foreach($Applications as $Application)
{
if ($Applications !== false) {
foreach ($Applications as $Application) {
//get newest version of package which is already installed
$this->_cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Checking application '" . substr($Application, 0, -1) . "'");
$CurrentVersion = '';
$Result = $this->db->query('SELECT * FROM `' . TABLE_APS_PACKAGES . '` WHERE `Name` = "' . $this->db->escape(substr($Application, 0, -1)) . '"');
$Result_stmt = Database::prepare("
SELECT * FROM `" . TABLE_APS_PACKAGES . "` WHERE `Name` = :name
");
Database::pexecute($Result_stmt, array('name' => substr($Application, 0, -1)));
$numrows_result = Database::num_rows();
while($Row = $this->db->fetch_array($Result))
{
if(version_compare($Row['Version'] . '-' . $Row['Release'], $CurrentVersion) == 1)
{
while ($Row = $Result_stmt->fetch(PDO::FETCH_ASSOC)) {
if (version_compare($Row['Version'] . '-' . $Row['Release'], $CurrentVersion) == 1) {
$CurrentVersion = $Row['Version'] . '-' . $Row['Release'];
}
}
if($this->db->num_rows($Result) != 0)
{
if ($numrows_result!= 0) {
//package already installed in system, search for newer version
if($Task['Task'] != TASK_SYSTEM_UPDATE)continue;
if ($Task['Task'] != TASK_SYSTEM_UPDATE) {
continue;
}
//fetch different versions of application from distribution server
$NewerVersion = '';
$Versions = self::FetchSubUrls($this->RootUrl . $Vendor . $Application);
if($Versions !== false)
{
foreach($Versions as $Version)
{
if ($Versions !== false) {
foreach ($Versions as $Version) {
$OnlineVersion = substr($Version, 0, -1);
//is package newer than current version?
if(version_compare($OnlineVersion, $CurrentVersion) == 1)
{
if (version_compare($OnlineVersion, $CurrentVersion) == 1) {
//is new package newer than another one found before?
if(version_compare($OnlineVersion, $NewerVersion) == 1)
{
if (version_compare($OnlineVersion, $NewerVersion) == 1) {
$NewerVersion = $OnlineVersion;
}
}
}
if($NewerVersion != '')
{
if ($NewerVersion != '') {
//download package as an update
self::DownloadPackage($this->RootUrl . $Vendor . $Application . $NewerVersion, substr($Application, 0, -1), $NewerVersion);
continue;
}
}
}
else
{
if($Task['Task'] != TASK_SYSTEM_DOWNLOAD)continue;
} else {
if ($Task['Task'] != TASK_SYSTEM_DOWNLOAD) {
continue;
}
//new packages
$NewVersion = '';
$Versions = self::FetchSubUrls($this->RootUrl . $Vendor . $Application);
foreach($Versions as $Version)
{
foreach ($Versions as $Version) {
$OnlineVersion = substr($Version, 0, -1);
//is package newer than another one found before?
if(version_compare($OnlineVersion, $NewVersion) == 1)
{
if (version_compare($OnlineVersion, $NewVersion) == 1) {
$NewVersion = $OnlineVersion;
}
}
if($NewVersion != '')
{
if ($NewVersion != '') {
//download package as a new one
self::DownloadPackage($this->RootUrl . $Vendor . $Application . $NewVersion, substr($Application, 0, -1), $NewVersion);
continue;
}
@@ -195,13 +179,11 @@ class ApsUpdater extends ApsParser
* @param version string identifying the application version
* @return success true/error false
*/
private function DownloadPackage($Url, $Application, $Version) {
private function DownloadPackage($Url, $Application, $Version)
{
$Downloads = self::FetchSubUrls($Url . '/');
//make url valid
$path = dirname($Url);
$file = urlencode(basename($Url));
$file_url = 'http://' . $this->RequestDomain . $path . '/' . $file . '.aps' . $Downloads[0];
@@ -210,31 +192,23 @@ class ApsUpdater extends ApsParser
$this->_cronlog->logAction(CRON_ACTION, LOG_NOTICE, "Downloading '" . $file_url . "'");
$Content = @file_get_contents($file_url);
if($Content != false)
{
if ($Content != false) {
//open file to write contents on disk
$FileHandle = fopen($this->RootDir . 'temp/' . $Application . '-' . $Version . '.app.zip', 'wb');
if($FileHandle == true)
{
if ($FileHandle == true) {
//write results to disk
fwrite($FileHandle, $Content);
fclose($FileHandle);
//set right permissions
chmod($this->RootDir . 'temp/' . $Application . '-' . $Version . '.app.zip', 0664);
return true;
}
else
{
} else {
return false;
}
}
else
{
} else {
return false;
}
}
@@ -246,51 +220,40 @@ class ApsUpdater extends ApsParser
* @param url url to fetch sub links from
* @return error false/success array with relative sub links
*/
private function FetchSubUrls($Url) {
private function FetchSubUrls($Url)
{
$Return = array();
//make url valid
$Url = str_replace(' ', '%20', $Url);
$file_url = 'http://' . $this->RequestDomain . $Url;
//get content from website url
$Content = @file($file_url);
if($Content !== false)
{
foreach($Content as $Temp)
{
if ($Content !== false) {
foreach ($Content as $Temp) {
//skip empty lines
if($Temp != "\r\n"
&& $Temp != "\r"
&& $Temp != "\n"
&& $Temp != "")
{
if ($Temp != "\r\n"
&& $Temp != "\r"
&& $Temp != "\n"
&& $Temp != ""
) {
//remove unwanted characters
$Temp = trim($Temp);
//grep URLs which match defined format
if(preg_match("/^<a href=\"(.+)\".+class=\"(vendor|application|version|packager)\"/", $Temp, $Matches))
{
if(!in_array(urldecode($Matches[1]), $Return))$Return[] = urldecode($Matches[1]);
if (preg_match("/^<a href=\"(.+)\".+class=\"(vendor|application|version|packager)\"/", $Temp, $Matches)) {
if (!in_array(urldecode($Matches[1]), $Return)) {
$Return[] = urldecode($Matches[1]);
}
}
}
}
return $Return;
}
else
{
} else {
return false;
}
}
}
?>