Filesystem helper improved

This commit is contained in:
Diego Najar 2017-07-02 18:55:27 +02:00
parent a4e143151d
commit a9c4323353
10 changed files with 205 additions and 26 deletions

View File

@ -218,13 +218,7 @@ class Plugin {
return $this->directoryName;
}
// Returns the absolute path for PHP with the workspace for the plugin
public function workspace()
{
return PATH_PLUGINS_DATABASES.$this->directoryName.DS;
}
// Return TRUE if the installation success, otherwise FALSE.
// Return TRUE if the installation success, otherwise FALSE
public function install($position=0)
{
if($this->installed()) {
@ -243,14 +237,7 @@ class Plugin {
public function uninstall()
{
// Delete all files.
$files = Filesystem::listFiles( $this->phpPathDB() );
foreach($files as $file) {
unlink($file);
}
// Delete the directory.
rmdir(PATH_PLUGINS_DATABASES.$this->directoryName);
Filesystem::deleteRecursive(PATH_PLUGINS_DATABASES.$this->directoryName);
}
public function installed()

View File

@ -151,7 +151,7 @@ echo '<div class="bl-publish-sidebar uk-width-2-10">';
'tip'=>$L->g('To schedule the post just select the date and time'),
'label'=>$L->g('Date')
));
/*
// If the page is parent then doesn't can have a parent.
if(count($page->children())===0)
{
@ -170,7 +170,7 @@ if(count($page->children())===0)
'tip'=>''
));
}
*/
// Position input
HTML::formInputText(array(
'name'=>'position',

View File

@ -137,6 +137,7 @@ echo '<div class="bl-publish-sidebar uk-width-2-10">';
'label'=>$L->g('Date')
));
/*
// Parent input
$options = array();
$parents = $dbPages->getParents(true);
@ -153,7 +154,7 @@ echo '<div class="bl-publish-sidebar uk-width-2-10">';
'tip'=>'',
'addEmptySpace'=>true
));
*/
// Position input
HTML::formInputText(array(
'name'=>'position',

View File

@ -95,4 +95,4 @@ else
// Load plugins after the admin area is loaded.
Theme::plugins('afterAdminLoad');
}
}

View File

@ -310,4 +310,4 @@ $L = $Language;
// DEBUG: Print constants
// $arr = array_filter(get_defined_constants(), 'is_string');
// echo json_encode($arr);
// exit;
// exit;

View File

@ -58,4 +58,40 @@ class Filesystem {
{
return file_exists($filename);
}
public static function directoryExists($path)
{
return file_exists($path);
}
public static function copyRecursive($source, $destination)
{
$destination = rtrim($destination, '/');
foreach($iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $item) {
if($item->isDir()) {
@mkdir($destination.DS.$iterator->getSubPathName());
} else {
copy($item, $destination.DS.$iterator->getSubPathName());
}
}
return true;
}
public static function deleteRecursive($source)
{
foreach(new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST) as $item) {
if($item->isFile()) {
unlink($item);
} else {
rmdir($item);
}
}
return rmdir($source);
}
}

View File

@ -2,13 +2,15 @@
class TCP {
public static function http($url, $method='GET', $verifySSL=true, $timeOut=1)
public static function http($url, $method='GET', $verifySSL=true, $timeOut=1, $followRedirections=true, $binary=true, $headers=false)
{
if( function_exists('curl_version') ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// TRUE to include the header in the output
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $followRedirections);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, $binary);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verifySSL);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeOut);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);
@ -25,11 +27,12 @@ class TCP {
$options = array(
'http'=>array(
'method'=>$method,
'timeout'=>$timeOut
'timeout'=>$timeOut,
'follow_location'=>$followRedirections
),
"ssl"=>array(
"verify_peer"=>$verifySSL,
"verify_peer_name"=>$verifySSL
"verify_peer"=>false,
"verify_peer_name"=>false
)
);
$stream = stream_context_create($options);
@ -39,5 +42,10 @@ class TCP {
return $output;
}
public static function download($url, $destination)
{
$data = self::http($url, $method='GET', $verifySSL=true, $timeOut=3, $followRedirections=true, $binary=true, $headers=false);
return file_put_contents($destination, $data);
}
}

View File

@ -0,0 +1,7 @@
{
"plugin-data":
{
"name": "Remote Content",
"description": ""
}
}

View File

@ -0,0 +1,10 @@
{
"author": "Bludit",
"email": "",
"website": "https://plugins.bludit.com",
"version": "2.0",
"releaseDate": "2017-05-26",
"license": "MIT",
"compatible": "2.0",
"notes": ""
}

View File

@ -0,0 +1,130 @@
<?php
class pluginRemoteContent extends Plugin {
public function init()
{
// Fields and default values for the database of this plugin
$this->dbFields = array(
'source'=>'localbluditv2.com/bl-content.zip',
'webhook'=>'remote-content-webhook'
);
}
public function install($position=0)
{
parent::install($position);
$workspace = $this->workspace();
mkdir($workspace, 0755, true);
}
public function uninstall()
{
parent::uninstall();
$workspace = $this->workspace();
Filesystem::deleteRecursive($workspace);
}
public function beforeRulesLoad()
{
if( $this->webhook() ) {
$this->getFile();
$this->updateContent();
$this->cleanUp();
exit();
}
}
private function workspace()
{
return PATH_CONTENT.'remotecontent'.DS;
}
private function webhook()
{
global $Url;
// Check URI start with the webhook
$webhook = $this->getValue('webhook');
if( empty($webhook) ) {
return false;
}
$startString = HTML_PATH_ROOT.$webhook;
$URI = $Url->uri();
$length = mb_strlen($startString, CHARSET);
if( mb_substr($URI, 0, $length)!=$startString ) {
return false;
}
Log::set('Plugin Remote Content'.LOG_SEP.'Webhook request.');
return true;
}
private function cleanUp()
{
Log::set('Plugin Remote Content'.LOG_SEP.'Cleaning...');
$workspace = $this->workspace();
Filesystem::deleteRecursive($workspace);
mkdir($workspace, 0755, true);
return true;
}
private function updateContent()
{
// Directory where the zip file was uncompress
$destinationPath = $this->workspace();
// This helps when uncompress the zip file and the files are saved inside a directory
$listDirectories = Filesystem::listDirectories($destinationPath);
if(count($listDirectories)==1) {
$uncompressDirectory = $listDirectories[0];
} else {
$uncompressDirectory = $destinationPath;
}
$uncompressDirectory = rtrim($uncompressDirectory, '/');
// Copy page directory
if(Filesystem::directoryExists($uncompressDirectory.DS.'pages')) {
Log::set('Plugin Remote Content'.LOG_SEP.'Copying pages...');
Filesystem::copyRecursive($uncompressDirectory.DS.'pages', PATH_PAGES);
}
// Copy databases directory
if(Filesystem::directoryExists($uncompressDirectory.DS.'databases')) {
Log::set('Plugin Remote Content'.LOG_SEP.'Copying databases...');
Filesystem::copyRecursive($uncompressDirectory.DS.'databases', PATH_DATABASES);
}
// Copy uploads directory
if(Filesystem::directoryExists($uncompressDirectory.DS.'uploads')) {
Log::set('Plugin Remote Content'.LOG_SEP.'Copying uploads...');
Filesystem::copyRecursive($uncompressDirectory.DS.'uploads', PATH_UPLOADS);
}
return true;
}
private function getFile()
{
// Download the zip file
Log::set('Plugin Remote Content'.LOG_SEP.'Downloading the zip file.');
$url = $this->getValue('source');
$destinationPath = $this->workspace();
$destinationFile = $destinationPath.'content.zip';
TCP::download($url, $destinationFile);
// Uncompress the zip file
Log::set('Plugin Remote Content'.LOG_SEP.'Uncompress the zip file.');
$zip = new ZipArchive;
if($zip->open($destinationFile)===true) {
$zip->extractTo($destinationPath);
$zip->close();
}
// Delete the zip file
unlink($destinationFile);
return true;
}
}