yellow/system/plugins/update.php

548 lines
19 KiB
PHP
Raw Normal View History

2016-06-27 13:28:10 +00:00
<?php
// Copyright (c) 2013-2016 Datenstrom, http://datenstrom.se
// This file may be used and distributed under the terms of the public license.
// Update plugin
class YellowUpdate
{
2016-08-22 09:54:13 +00:00
const VERSION = "0.6.10";
2016-06-27 13:28:10 +00:00
var $yellow; //access to API
// Handle initialisation
function onLoad($yellow)
{
$this->yellow = $yellow;
$this->yellow->config->setDefault("updatePluginsUrl", "https://github.com/datenstrom/yellow-plugins");
$this->yellow->config->setDefault("updateThemesUrl", "https://github.com/datenstrom/yellow-themes");
$this->yellow->config->setDefault("updateVersionFile", "version.ini");
2016-07-10 18:44:02 +00:00
$this->yellow->config->setDefault("updateInformationFile", "update.ini");
2016-06-27 13:28:10 +00:00
}
// Handle request
function onRequest($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
if($this->isInstallation())
{
$statusCode = $this->processRequestInstallation($serverScheme, $serverName, $base, $location, $fileName);
} else {
2016-08-18 19:57:07 +00:00
$statusCode = $this->processRequestPending($serverScheme, $serverName, $base, $location, $fileName);
2016-06-27 13:28:10 +00:00
}
return $statusCode;
}
// Handle command
function onCommand($args)
{
list($command) = $args;
switch($command)
{
case "update": $statusCode = $this->updateCommand($args); break;
2016-07-19 16:28:52 +00:00
default: $statusCode = $this->updateCommandPending($args); break;
2016-06-27 13:28:10 +00:00
}
return $statusCode;
}
// Handle command help
function onCommandHelp()
{
return "update [FEATURE]";
}
2016-08-18 19:57:07 +00:00
// Update website
2016-06-27 13:28:10 +00:00
function updateCommand($args)
{
list($command, $feature) = $args;
2016-07-19 16:28:52 +00:00
list($statusCode, $data) = $this->getSoftwareUpdate($feature);
if(!empty($data))
2016-06-27 13:28:10 +00:00
{
2016-07-19 16:28:52 +00:00
foreach($data as $key=>$value)
2016-06-27 13:28:10 +00:00
{
2016-07-20 09:44:17 +00:00
list($version) = explode(',', $value);
2016-07-19 16:28:52 +00:00
echo "$key $version\n";
2016-06-27 13:28:10 +00:00
}
2016-07-19 22:12:05 +00:00
if($statusCode==200) $statusCode = $this->download($data);
if($statusCode==200) $statusCode = $this->update();
if($statusCode!=200) echo "ERROR updating files: ".$this->yellow->page->get("pageError")."\n";
2016-08-18 19:57:07 +00:00
echo "Yellow $command: Website ".($statusCode!=200 ? "not " : "")."updated\n";
2016-06-27 13:28:10 +00:00
} else {
2016-07-19 22:12:05 +00:00
if($statusCode!=200) echo "ERROR updating files: ".$this->yellow->page->get("pageError")."\n";
2016-06-27 13:28:10 +00:00
echo "Yellow $command: No updates available\n";
2016-07-10 18:44:02 +00:00
}
return $statusCode;
}
2016-07-15 16:35:11 +00:00
2016-07-19 16:28:52 +00:00
// Update pending software
function updateCommandPending($args)
2016-07-15 16:35:11 +00:00
{
2016-08-22 09:54:13 +00:00
$statusCode = 0;
if($this->isSoftwarePending())
2016-07-15 16:35:11 +00:00
{
2016-08-22 09:54:13 +00:00
$statusCode = $this->update();
if($statusCode!=0)
{
if($statusCode!=200) echo "ERROR updating files: ".$this->yellow->page->get("pageError")."\n";
echo "Yellow has ".($statusCode!=200 ? "not " : "")."been updated: Please run command again\n";
}
2016-07-15 16:35:11 +00:00
}
return $statusCode;
}
2016-07-19 16:28:52 +00:00
2016-08-18 19:57:07 +00:00
// Download available software
2016-07-19 16:28:52 +00:00
function download($data)
{
$statusCode = 0;
$path = $this->yellow->config->get("pluginDir");
2016-07-29 22:41:19 +00:00
$fileExtension = $this->yellow->config->get("downloadExtension");
2016-07-19 16:28:52 +00:00
foreach($data as $key=>$value)
{
$fileName = strtoloweru("$path$key.zip");
list($version, $url) = explode(',', $value);
list($statusCode, $fileData) = $this->getSoftwareFile($url);
2016-07-29 22:41:19 +00:00
if(empty($fileData) || !$this->yellow->toolbox->createFile($fileName.$fileExtension, $fileData))
2016-07-19 16:28:52 +00:00
{
$statusCode = 500;
2016-08-19 11:16:37 +00:00
$this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
2016-07-19 16:28:52 +00:00
break;
}
}
2016-07-19 22:12:05 +00:00
if($statusCode==200)
2016-07-19 16:28:52 +00:00
{
foreach($data as $key=>$value)
{
$fileName = strtoloweru("$path$key.zip");
2016-07-29 22:41:19 +00:00
if(!$this->yellow->toolbox->renameFile($fileName.$fileExtension, $fileName))
2016-07-19 16:28:52 +00:00
{
$statusCode = 500;
2016-08-19 11:16:37 +00:00
$this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
2016-07-19 16:28:52 +00:00
}
}
}
return $statusCode;
}
2016-07-10 18:44:02 +00:00
2016-07-15 16:35:11 +00:00
// Update downloaded software
2016-07-10 18:44:02 +00:00
function update()
{
$statusCode = 0;
2016-07-15 16:35:11 +00:00
foreach($this->yellow->plugins->plugins as $key=>$value)
2016-07-10 18:44:02 +00:00
{
2016-07-15 16:35:11 +00:00
if(method_exists($value["obj"], "onUpdate"))
{
$statusCode = $value["obj"]->onUpdate($this->yellow->getRequestHandler());
2016-07-19 22:12:05 +00:00
if($statusCode!=0) break;
2016-07-15 16:35:11 +00:00
}
2016-07-10 18:44:02 +00:00
}
2016-07-19 22:12:05 +00:00
if($statusCode==0)
2016-07-10 18:44:02 +00:00
{
2016-07-15 16:35:11 +00:00
$path = $this->yellow->config->get("pluginDir");
foreach($this->yellow->toolbox->getDirectoryEntries($path, "/^.*\.zip$/", true, false) as $entry)
{
if(defined("DEBUG") && DEBUG>=2) echo "YellowUpdate::update file:$entry<br/>\n";
$statusCode = max($statusCode, $this->updateSoftwareArchive($entry));
}
$path = $this->yellow->config->get("themeDir");
foreach($this->yellow->toolbox->getDirectoryEntries($path, "/^.*\.zip$/", true, false) as $entry)
{
if(defined("DEBUG") && DEBUG>=2) echo "YellowUpdate::update file:$entry<br/>\n";
$statusCode = max($statusCode, $this->updateSoftwareArchive($entry));
}
2016-07-10 18:44:02 +00:00
}
return $statusCode;
}
// Update software from archive
function updateSoftwareArchive($path)
{
$statusCode = 0;
$zip = new ZipArchive();
2016-07-19 22:12:05 +00:00
if($zip->open($path)===true)
2016-07-10 18:44:02 +00:00
{
$fileNameInformation = $this->yellow->config->get("updateInformationFile");
for($i=0; $i<$zip->numFiles; ++$i)
{
$fileName = $zip->getNameIndex($i);
2016-07-19 16:28:52 +00:00
if(empty($pathBase))
{
preg_match("#^(.*\/).*?$#", $fileName, $matches);
$pathBase = $matches[1];
}
2016-07-19 22:12:05 +00:00
if($fileName==$pathBase.$fileNameInformation)
2016-07-10 18:44:02 +00:00
{
$fileData = $zip->getFromIndex($i);
break;
}
}
foreach($this->yellow->toolbox->getTextLines($fileData) as $line)
{
if(preg_match("/^\#/", $line)) continue;
preg_match("/^\s*(.*?)\s*:\s*(.*?)\s*$/", $line, $matches);
if(lcfirst($matches[1])=="plugin" || lcfirst($matches[1])=="theme") $software = $matches[2];
if(!empty($software) && !empty($matches[1]) && !empty($matches[2]))
{
list($fileName, $flags) = explode(',', $matches[2], 2);
2016-07-19 16:28:52 +00:00
$fileData = $zip->getFromName($pathBase.$fileName);
2016-07-15 16:35:11 +00:00
$metaData = $zip->statName($pathBase.$fileName);
$modified = $metaData ? $metaData["mtime"] : 0;
2016-07-19 16:28:52 +00:00
$statusCode = $this->updateSoftwareFile($matches[1], $fileData, $modified, $flags, $software);
2016-07-19 22:12:05 +00:00
if($statusCode!=200) break;
2016-07-10 18:44:02 +00:00
}
}
$zip->close();
if($statusCode==200 && !$this->yellow->toolbox->deleteFile($path))
{
$statusCode = 500;
$this->yellow->page->error($statusCode, "Can't delete file '$path'!");
}
}
return $statusCode;
}
// Update software file
2016-07-19 16:28:52 +00:00
function updateSoftwareFile($fileName, $fileData, $modified, $flags, $software)
2016-07-10 18:44:02 +00:00
{
$statusCode = 200;
$fileName = $this->yellow->toolbox->normaliseTokens($fileName);
if($this->yellow->lookup->isValidFile($fileName) && !empty($flags))
{
$create = $update = $delete = false;
2016-07-19 16:28:52 +00:00
if(preg_match("/create/i", $flags) && !is_file($fileName) && !empty($fileData)) $create = true;
if(preg_match("/update/i", $flags) && is_file($fileName) && !empty($fileData)) $update = true;
2016-07-15 16:35:11 +00:00
if(preg_match("/delete/i", $flags) && is_file($fileName)) $delete = true;
2016-07-10 18:44:02 +00:00
if(preg_match("/optional/i", $flags) && $this->isSoftware($software)) $create = $update = $delete = false;
2016-07-15 16:35:11 +00:00
if($create)
2016-07-10 18:44:02 +00:00
{
2016-07-19 16:28:52 +00:00
if(!$this->yellow->toolbox->createFile($fileName, $fileData, true) ||
2016-07-10 18:44:02 +00:00
!$this->yellow->toolbox->modifyFile($fileName, $modified))
{
$statusCode = 500;
2016-08-19 11:16:37 +00:00
$this->yellow->page->error($statusCode, "Can't write file '$fileName'!");
2016-07-10 18:44:02 +00:00
}
}
2016-07-15 16:35:11 +00:00
if($update)
2016-07-10 18:44:02 +00:00
{
if(!$this->yellow->toolbox->deleteFile($fileName, $this->yellow->config->get("trashDir")) ||
2016-07-19 16:28:52 +00:00
!$this->yellow->toolbox->createFile($fileName, $fileData) ||
2016-07-10 18:44:02 +00:00
!$this->yellow->toolbox->modifyFile($fileName, $modified))
{
$statusCode = 500;
$this->yellow->page->error($statusCode, "Can't update file '$fileName'!");
}
}
2016-07-15 16:35:11 +00:00
if($delete)
2016-07-10 18:44:02 +00:00
{
if(!$this->yellow->toolbox->deleteFile($fileName, $this->yellow->config->get("trashDir")))
{
$statusCode = 500;
$this->yellow->page->error($statusCode, "Can't delete file '$fileName'!");
}
}
if(defined("DEBUG") && DEBUG>=3) echo "YellowUpdate::updateSoftwareFile file:$fileName flags:$flags<br/>\n";
2016-06-27 13:28:10 +00:00
}
return $statusCode;
}
2016-07-29 22:41:19 +00:00
// Update installation files
function updateInstallation($feature)
{
$ok = true;
$path = $this->yellow->config->get("pluginDir");
$regex = "/^.*\\".$this->yellow->config->get("downloadExtension")."$/";
foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false) as $entry)
{
2016-08-07 10:51:23 +00:00
if(stristr(basename($entry), $feature))
2016-07-29 22:41:19 +00:00
{
if($this->updateSoftwareArchive($entry)!=200) $ok = false;
2016-07-30 00:58:14 +00:00
}
}
if($ok)
{
foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false) as $entry)
{
$this->yellow->toolbox->deleteFile($entry);
2016-07-29 22:41:19 +00:00
}
}
return $ok;
}
2016-08-18 19:57:07 +00:00
// Process request to install pending software
function processRequestPending($serverScheme, $serverName, $base, $location, $fileName)
2016-06-27 13:28:10 +00:00
{
2016-07-10 18:44:02 +00:00
$statusCode = 0;
2016-08-22 09:54:13 +00:00
if($this->isContentFile($fileName) && $this->isSoftwarePending())
2016-07-10 18:44:02 +00:00
{
$statusCode = $this->update();
2016-07-19 22:12:05 +00:00
if($statusCode==200)
2016-07-10 18:44:02 +00:00
{
$statusCode = 303;
$location = $this->yellow->lookup->normaliseUrl($serverScheme, $serverName, $base, $location);
$this->yellow->sendStatus($statusCode, $location);
}
}
return $statusCode;
2016-06-27 13:28:10 +00:00
}
// Process request to install website
function processRequestInstallation($serverScheme, $serverName, $base, $location, $fileName)
{
$statusCode = 0;
2016-08-22 09:54:13 +00:00
if($this->isContentFile($fileName) && $this->isInstallation())
2016-06-27 13:28:10 +00:00
{
$this->yellow->pages->pages["root/"] = array();
$this->yellow->page = new YellowPage($this->yellow);
$this->yellow->page->setRequestInformation($serverScheme, $serverName, $base, $location, $fileName);
2016-08-13 15:48:18 +00:00
$this->yellow->page->parseData($this->getRawDataInstallation($this->yellow->getRequestLanguage()), false, 404);
2016-06-27 13:28:10 +00:00
$this->yellow->page->parserSafeMode = false;
$this->yellow->page->parseContent();
$name = trim(preg_replace("/[^\pL\d\-\. ]/u", "-", $_REQUEST["name"]));
$email = trim($_REQUEST["email"]);
$password = trim($_REQUEST["password"]);
$language = trim($_REQUEST["language"]);
2016-07-29 22:41:19 +00:00
$feature = trim($_REQUEST["feature"]);
2016-06-27 13:28:10 +00:00
$status = trim($_REQUEST["status"]);
2016-07-19 22:12:05 +00:00
if($status=="install")
2016-06-27 13:28:10 +00:00
{
$status = "ok";
$fileNameHome = $this->yellow->lookup->findFileFromLocation("/");
$fileData = strreplaceu("\r\n", "\n", $this->yellow->toolbox->readFile($fileNameHome));
if($fileData==$this->getRawDataHome("en") && $language!="en")
{
$status = $this->yellow->toolbox->createFile($fileNameHome, $this->getRawDataHome($language)) ? "ok" : "error";
2016-07-19 22:12:05 +00:00
if($status=="error") $this->yellow->page->error(500, "Can't write file '$fileNameHome'!");
2016-06-27 13:28:10 +00:00
}
}
2016-07-19 22:12:05 +00:00
if($status=="ok")
2016-06-27 13:28:10 +00:00
{
if(!empty($email) && !empty($password) && $this->yellow->plugins->isExisting("webinterface"))
{
$fileNameUser = $this->yellow->config->get("configDir").$this->yellow->config->get("webinterfaceUserFile");
$status = $this->yellow->plugins->get("webinterface")->users->update($fileNameUser, $email, $password, $name, $language) ? "ok" : "error";
2016-07-19 22:12:05 +00:00
if($status=="error") $this->yellow->page->error(500, "Can't write file '$fileNameUser'!");
2016-06-27 13:28:10 +00:00
}
}
2016-07-19 22:12:05 +00:00
if($status=="ok")
2016-07-29 22:41:19 +00:00
{
if(!empty($feature))
{
$status = $this->updateInstallation($feature) ? "ok" : "error";
if($status=="error") $this->yellow->page->error(500, "Can't install feature '$feature'!");
}
}
if($status=="ok")
2016-06-27 13:28:10 +00:00
{
2016-07-19 22:12:05 +00:00
if($this->yellow->config->get("sitename")=="Yellow") $_REQUEST["sitename"] = $name;
2016-06-27 13:28:10 +00:00
$fileNameConfig = $this->yellow->config->get("configDir").$this->yellow->config->get("configFile");
$status = $this->yellow->config->update($fileNameConfig, $this->getConfigData()) ? "done" : "error";
2016-07-19 22:12:05 +00:00
if($status=="error") $this->yellow->page->error(500, "Can't write file '$fileNameConfig'!");
2016-06-27 13:28:10 +00:00
}
2016-07-19 22:12:05 +00:00
if($status=="done")
2016-06-27 13:28:10 +00:00
{
$statusCode = 303;
$location = $this->yellow->lookup->normaliseUrl($serverScheme, $serverName, $base, $location);
$this->yellow->sendStatus($statusCode, $location);
} else {
$statusCode = $this->yellow->sendPage();
}
}
return $statusCode;
}
// Return raw data for installation page
2016-08-13 15:48:18 +00:00
function getRawDataInstallation($language)
2016-06-27 13:28:10 +00:00
{
2016-08-13 15:48:18 +00:00
$fileName = strreplaceu("(.*)", "installation", $this->yellow->config->get("configDir").$this->yellow->config->get("webinterfaceNewFile"));
2016-06-27 13:28:10 +00:00
$rawData = $this->yellow->toolbox->readFile($fileName);
if(empty($rawData))
{
$this->yellow->text->setLanguage($language);
$rawData = "---\nTitle:".$this->yellow->text->get("webinterfaceInstallationTitle")."\nLanguage:$language\nNavigation:navigation\n---\n";
2016-06-27 21:26:26 +00:00
$rawData .= "<form class=\"installation-form\" action=\"".$this->yellow->page->getLocation(true)."\" method=\"post\">\n";
2016-06-27 13:28:10 +00:00
$rawData .= "<p><label for=\"name\">".$this->yellow->text->get("webinterfaceSignupName")."</label><br /><input class=\"form-control\" type=\"text\" maxlength=\"64\" name=\"name\" id=\"name\" value=\"\"></p>\n";
$rawData .= "<p><label for=\"email\">".$this->yellow->text->get("webinterfaceSignupEmail")."</label><br /><input class=\"form-control\" type=\"text\" maxlength=\"64\" name=\"email\" id=\"email\" value=\"\"></p>\n";
$rawData .= "<p><label for=\"password\">".$this->yellow->text->get("webinterfaceSignupPassword")."</label><br /><input class=\"form-control\" type=\"password\" maxlength=\"64\" name=\"password\" id=\"password\" value=\"\"></p>\n";
2016-07-19 22:12:05 +00:00
if(count($this->yellow->text->getLanguages())>1)
2016-06-27 13:28:10 +00:00
{
$rawData .= "<p>";
foreach($this->yellow->text->getLanguages() as $language)
{
$checked = $language==$this->yellow->text->language ? " checked=\"checked\"" : "";
$rawData .= "<label for=\"$language\"><input type=\"radio\" name=\"language\" id=\"$language\" value=\"$language\"$checked> ".$this->yellow->text->getTextHtml("languageDescription", $language)."</label><br />";
}
$rawData .= "</p>\n";
}
2016-07-29 22:41:19 +00:00
if(count($this->getFeatures())>1)
{
$rawData .= "<p>".$this->yellow->text->get("webinterfaceInstallationFeature")."<p>";
foreach($this->getFeatures() as $feature)
{
$checked = $feature=="website" ? " checked=\"checked\"" : "";
$rawData .= "<label for=\"$feature\"><input type=\"radio\" name=\"feature\" id=\"$feature\" value=\"$feature\"$checked> ".ucfirst($feature)."</label><br />";
}
$rawData .= "</p>\n";
}
2016-06-27 13:28:10 +00:00
$rawData .= "<input class=\"btn\" type=\"submit\" value=\"".$this->yellow->text->get("webinterfaceOkButton")."\" />\n";
$rawData .= "<input type=\"hidden\" name=\"status\" value=\"install\" />\n";
$rawData .= "</form>\n";
}
return $rawData;
}
// Return raw data for home page
function getRawDataHome($language)
{
$rawData = "---\nTitle: Home\n---\n".strreplaceu("\\n", "\n", $this->yellow->text->getText("webinterfaceInstallationHomePage", $language));
return $rawData;
}
2016-07-29 22:41:19 +00:00
// Return configuration data for installation
2016-06-27 13:28:10 +00:00
function getConfigData()
{
$data = array();
foreach($_REQUEST as $key=>$value)
{
if(!$this->yellow->config->isExisting($key)) continue;
$data[$key] = trim($value);
}
$data["# serverScheme"] = $this->yellow->toolbox->getServerScheme();
$data["# serverName"] = $this->yellow->toolbox->getServerName();
$data["# serverBase"] = $this->yellow->toolbox->getServerBase();
$data["# serverTime"] = $this->yellow->toolbox->getServerTime();
$data["installationMode"] = "0";
return $data;
}
2016-07-19 16:28:52 +00:00
2016-07-29 22:41:19 +00:00
// Return installation features
function getFeatures()
{
$data = array("website");
$path = $this->yellow->config->get("pluginDir");
$regex = "/^.*\\".$this->yellow->config->get("downloadExtension")."$/";
foreach($this->yellow->toolbox->getDirectoryEntries($path, $regex, true, false, false) as $entry)
{
2016-07-30 00:58:14 +00:00
if(preg_match("/^(.*?)-(.*?)\./", $entry, $matches))
2016-07-29 22:41:19 +00:00
{
2016-07-30 00:58:14 +00:00
array_push($data, $matches[2]);
2016-07-29 22:41:19 +00:00
}
}
return $data;
}
2016-07-19 16:28:52 +00:00
// Return software update
function getSoftwareUpdate($feature)
{
$data = array();
list($statusCode, $dataCurrent) = $this->getSoftwareVersion();
list($statusCode, $dataLatest) = $this->getSoftwareVersion(true, true);
foreach($dataCurrent as $key=>$value)
{
2016-07-20 09:44:17 +00:00
list($version) = explode(',', $dataLatest[$key]);
2016-07-19 16:28:52 +00:00
if(empty($feature))
{
2016-07-19 22:12:05 +00:00
if(strnatcasecmp($dataCurrent[$key], $version)<0) $data[$key] = $dataLatest[$key];
2016-07-19 16:28:52 +00:00
} else {
2016-08-07 10:51:23 +00:00
if(stristr($key, $feature) && $version) $data[$key] = $dataLatest[$key];
2016-07-19 16:28:52 +00:00
}
}
return array($statusCode, $data);
}
2016-06-27 13:28:10 +00:00
// Return software version
2016-07-19 16:28:52 +00:00
function getSoftwareVersion($latest = false, $rawFormat = false)
2016-06-27 13:28:10 +00:00
{
$data = array();
if($latest)
{
2016-07-19 16:28:52 +00:00
$urlPlugins = $this->yellow->config->get("updatePluginsUrl")."/raw/master/".$this->yellow->config->get("updateVersionFile");
$urlThemes = $this->yellow->config->get("updateThemesUrl")."/raw/master/".$this->yellow->config->get("updateVersionFile");
list($statusCodePlugins, $fileDataPlugins) = $this->getSoftwareFile($urlPlugins, $rawFormat);
list($statusCodeThemes, $fileDataThemes) = $this->getSoftwareFile($urlThemes, $rawFormat);
2016-06-27 13:28:10 +00:00
$statusCode = max($statusCodePlugins, $statusCodeThemes);
2016-07-19 22:12:05 +00:00
if($statusCode==200)
2016-07-19 16:28:52 +00:00
{
foreach($this->yellow->toolbox->getTextLines($fileDataPlugins."\n".$fileDataThemes) as $line)
{
preg_match("/^\s*(.*?)\s*:\s*(.*?)\s*$/", $line, $matches);
if(!empty($matches[1]) && !empty($matches[2]))
{
2016-07-20 09:44:17 +00:00
list($version) = explode(',', $matches[2]);
2016-07-19 16:28:52 +00:00
$data[$matches[1]] = $rawFormat ? $matches[2] : $version;
}
}
}
2016-06-27 13:28:10 +00:00
} else {
$statusCode = 200;
foreach($this->yellow->plugins->getData() as $key=>$value) $data[$key] = $value;
foreach($this->yellow->themes->getData() as $key=>$value) $data[$key] = $value;
}
return array($statusCode, $data);
}
2016-07-19 16:28:52 +00:00
// Return software file
function getSoftwareFile($url)
2016-06-27 13:28:10 +00:00
{
2016-07-19 16:28:52 +00:00
$fileData = "";
2016-06-27 13:28:10 +00:00
if(extension_loaded("curl"))
{
2016-07-19 16:28:52 +00:00
$urlRequest = $url;
if(preg_match("#^https://github.com/(.+)/raw/(.+)$#", $url, $matches))
{
$urlRequest = "https://raw.githubusercontent.com/".$matches[1]."/".$matches[2];
}
2016-06-27 13:28:10 +00:00
$curlHandle = curl_init();
2016-07-19 16:28:52 +00:00
curl_setopt($curlHandle, CURLOPT_URL, $urlRequest);
2016-07-19 22:12:05 +00:00
curl_setopt($curlHandle, CURLOPT_USERAGENT, "Mozilla/5.0 (compatible; YellowCore/".YellowCore::VERSION).")";
2016-06-27 13:28:10 +00:00
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlHandle, CURLOPT_CONNECTTIMEOUT, 30);
$rawData = curl_exec($curlHandle);
$statusCode = curl_getinfo($curlHandle, CURLINFO_HTTP_CODE);
curl_close($curlHandle);
2016-07-19 22:12:05 +00:00
if($statusCode==200)
2016-06-27 13:28:10 +00:00
{
2016-07-19 16:28:52 +00:00
$fileData = $rawData;
2016-07-19 22:12:05 +00:00
} else if($statusCode==0) {
2016-07-19 16:28:52 +00:00
$statusCode = 444;
$this->yellow->page->error($statusCode, "No response from server!");
} else {
$this->yellow->page->error($statusCode, "Can't download file '$url'!");
2016-06-27 13:28:10 +00:00
}
2016-07-19 16:28:52 +00:00
if(defined("DEBUG") && DEBUG>=3) echo "YellowUpdate::getSoftwareFile status:$statusCode url:$url<br/>\n";
2016-06-27 13:28:10 +00:00
} else {
$statusCode = 500;
2016-07-19 16:28:52 +00:00
$this->yellow->page->error($statusCode, "Plugin 'update' requires cURL library!");
2016-06-27 13:28:10 +00:00
}
2016-07-19 16:28:52 +00:00
return array($statusCode, $fileData);
2016-06-27 13:28:10 +00:00
}
2016-07-10 18:44:02 +00:00
// Check if software exists
function isSoftware($software)
{
$data = $this->yellow->plugins->getData();
return !is_null($data[$software]);
}
2016-08-22 09:54:13 +00:00
// Check if pending software exists
function isSoftwarePending()
{
$path = $this->yellow->config->get("pluginDir");
$foundPlugins = count($this->yellow->toolbox->getDirectoryEntries($path, "/^.*\.zip$/", true, false))>0;
$path = $this->yellow->config->get("themeDir");
$foundThemes = count($this->yellow->toolbox->getDirectoryEntries($path, "/^.*\.zip$/", true, false))>0;
return $foundPlugins || $foundThemes;
}
2016-07-10 18:44:02 +00:00
// Check if installation requested
2016-06-27 13:28:10 +00:00
function isInstallation()
{
2016-07-10 18:44:02 +00:00
return $this->yellow->config->get("installationMode") && PHP_SAPI!="cli";
}
// Check if content file
function isContentFile($fileName)
{
$contentDirLength = strlenu($this->yellow->config->get("contentDir"));
2016-07-19 22:12:05 +00:00
return substru($fileName, 0, $contentDirLength)==$this->yellow->config->get("contentDir");
2016-06-27 13:28:10 +00:00
}
}
2016-07-19 22:12:05 +00:00
$yellow->plugins->register("update", "YellowUpdate", YellowUpdate::VERSION, 1);
2016-06-27 13:28:10 +00:00
?>