diff --git a/bl-kernel/abstract/plugin.class.php b/bl-kernel/abstract/plugin.class.php index 7c96c2c1..04e2cc27 100644 --- a/bl-kernel/abstract/plugin.class.php +++ b/bl-kernel/abstract/plugin.class.php @@ -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() diff --git a/bl-kernel/admin/views/edit-page.php b/bl-kernel/admin/views/edit-page.php index 8e62cf26..93c7f8c4 100644 --- a/bl-kernel/admin/views/edit-page.php +++ b/bl-kernel/admin/views/edit-page.php @@ -151,7 +151,7 @@ echo '
'; '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', diff --git a/bl-kernel/admin/views/new-page.php b/bl-kernel/admin/views/new-page.php index 0a79ed1d..02fe9047 100644 --- a/bl-kernel/admin/views/new-page.php +++ b/bl-kernel/admin/views/new-page.php @@ -137,6 +137,7 @@ echo '
'; 'label'=>$L->g('Date') )); +/* // Parent input $options = array(); $parents = $dbPages->getParents(true); @@ -153,7 +154,7 @@ echo '
'; 'tip'=>'', 'addEmptySpace'=>true )); - +*/ // Position input HTML::formInputText(array( 'name'=>'position', diff --git a/bl-kernel/boot/admin.php b/bl-kernel/boot/admin.php index 54f41f57..9cffaf5b 100644 --- a/bl-kernel/boot/admin.php +++ b/bl-kernel/boot/admin.php @@ -95,4 +95,4 @@ else // Load plugins after the admin area is loaded. Theme::plugins('afterAdminLoad'); -} +} \ No newline at end of file diff --git a/bl-kernel/boot/init.php b/bl-kernel/boot/init.php index 908ef9b3..08823eb5 100644 --- a/bl-kernel/boot/init.php +++ b/bl-kernel/boot/init.php @@ -310,4 +310,4 @@ $L = $Language; // DEBUG: Print constants // $arr = array_filter(get_defined_constants(), 'is_string'); // echo json_encode($arr); -// exit; \ No newline at end of file +// exit; diff --git a/bl-kernel/helpers/filesystem.class.php b/bl-kernel/helpers/filesystem.class.php index b3848d60..fec8cca6 100644 --- a/bl-kernel/helpers/filesystem.class.php +++ b/bl-kernel/helpers/filesystem.class.php @@ -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); + } } \ No newline at end of file diff --git a/bl-kernel/helpers/tcp.class.php b/bl-kernel/helpers/tcp.class.php index 6fde7ade..8784f645 100644 --- a/bl-kernel/helpers/tcp.class.php +++ b/bl-kernel/helpers/tcp.class.php @@ -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); + } } \ No newline at end of file diff --git a/bl-plugins/remote-content/languages/en_US.json b/bl-plugins/remote-content/languages/en_US.json new file mode 100644 index 00000000..535a22ae --- /dev/null +++ b/bl-plugins/remote-content/languages/en_US.json @@ -0,0 +1,7 @@ +{ + "plugin-data": + { + "name": "Remote Content", + "description": "" + } +} \ No newline at end of file diff --git a/bl-plugins/remote-content/metadata.json b/bl-plugins/remote-content/metadata.json new file mode 100644 index 00000000..a5c64fc0 --- /dev/null +++ b/bl-plugins/remote-content/metadata.json @@ -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": "" +} \ No newline at end of file diff --git a/bl-plugins/remote-content/plugin.php b/bl-plugins/remote-content/plugin.php new file mode 100644 index 00000000..86d22db4 --- /dev/null +++ b/bl-plugins/remote-content/plugin.php @@ -0,0 +1,130 @@ +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; + } +} \ No newline at end of file