bludit/bl-plugins/remote-content/plugin.php

209 lines
5.3 KiB
PHP
Raw Permalink Normal View History

2018-09-14 15:48:57 +02:00
<?php
class pluginRemoteContent extends Plugin {
public function init()
{
// Generate a random string for the webhook
$randomWebhook = uniqid();
// Key and value for the database of the plugin
$this->dbFields = array(
'webhook'=>$randomWebhook,
2018-09-16 19:26:23 +02:00
'source'=>''
2018-09-14 15:48:57 +02:00
);
}
public function form()
{
2018-10-17 22:35:30 +02:00
global $language;
2018-09-14 15:48:57 +02:00
if (extension_loaded('zip')===false) {
$this->formButtons = false;
2018-10-17 22:35:30 +02:00
return '<div class="alert alert-success">'.$language->get('the-extension-zip-is-not-installed').'</div>';
2018-09-14 15:48:57 +02:00
}
$html = '<div class="alert alert-primary" role="alert">';
$html .= $this->description();
$html .= '</div>';
$html .= '<div>';
2018-10-17 22:35:30 +02:00
$html .= '<label>'.$language->get('Webhook').'</label>';
2018-09-14 15:48:57 +02:00
$html .= '<input id="jswebhook" name="webhook" type="text" value="'.$this->getValue('webhook').'">';
$html .= '<span class="tip">'.DOMAIN_BASE.$this->getValue('webhook').'</span>';
$html .= '</div>';
$html .= '<div>';
2018-10-17 22:35:30 +02:00
$html .= '<label>'.$language->get('Source').'</label>';
2018-09-14 15:48:57 +02:00
$html .= '<input id="jssource" name="source" type="text" value="'.$this->getValue('source').'" placeholder="https://">';
2018-10-17 22:35:30 +02:00
$html .= '<span class="tip">'.$language->get('Complete URL of the zip file').'</span>';
2018-09-14 15:48:57 +02:00
$html .= '</div>';
2019-01-27 12:46:34 +01:00
$html .= '<hr>';
$html .= '<div>';
$html .= '<button type="button" id="jstryWebhook" class="btn btn-primary" onclick="tryWebhook()">'.$language->get('Try webhook').'</button>';
$html .= <<<EOF
<script>
function tryWebhook() {
var webhook = document.getElementById("jswebhook").value;
window.open(DOMAIN_BASE+webhook, '_blank');
}
</script>
EOF;
$html .= '</div>';
2018-09-14 15:48:57 +02:00
return $html;
}
public function beforeAll()
{
// Check Webhook
$webhook = $this->getValue('webhook');
if ($this->webhook($webhook)) {
2018-09-21 14:28:20 +02:00
$this->cleanUp();
2018-09-14 15:48:57 +02:00
// Download files
2018-09-16 19:26:23 +02:00
$this->downloadFiles();
2018-09-14 15:48:57 +02:00
// Delete the current content
$this->deleteContent();
// Generate the new content
$this->generateContent();
// End request
$this->response(array('status'=>'0'));
}
}
private function downloadFiles()
{
// Download the zip file
Log::set('Plugin Remote Content'.LOG_SEP.'Downloading the zip file.');
$source = $this->getValue('source');
$destinationPath = $this->workspace();
$destinationFile = $destinationPath.'content.zip';
TCP::download($source, $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;
}
// Delete the page and uploads directories from bl-content
private function deleteContent()
{
// Clean the page database
global $pages;
$pages->db = array();
Filesystem::deleteRecursive(PATH_PAGES);
Filesystem::deleteRecursive(PATH_UPLOADS);
mkdir(PATH_PAGES, DIR_PERMISSIONS, true);
mkdir(PATH_UPLOADS, DIR_PERMISSIONS, true);
mkdir(PATH_UPLOADS_PROFILES, DIR_PERMISSIONS, true);
mkdir(PATH_UPLOADS_THUMBNAILS, DIR_PERMISSIONS, true);
2018-09-14 15:48:57 +02:00
return true;
}
2018-09-21 14:28:20 +02:00
private function cleanUp()
{
$workspace = $this->workspace();
Filesystem::deleteRecursive($workspace.DS);
mkdir($workspace, DIR_PERMISSIONS, true);
2018-09-21 14:28:20 +02:00
return true;
}
2018-09-14 15:48:57 +02:00
private function generateContent()
{
global $pages;
$root = Filesystem::listDirectories($this->workspace());
$root = $root[0]; // first directory created by the unzip
// For each page inside the pages directory
// Parse the page and add to the database
if (Filesystem::directoryExists($root.DS.'pages')) {
$parentList = Filesystem::listDirectories($root.DS.'pages'.DS);
foreach ($parentList as $parentDirectory) {
$parentKey = basename($parentDirectory);
if (Filesystem::fileExists($parentDirectory.DS.'index.md')) {
$row = $this->parsePage($parentDirectory.DS.'index.md');
$row['slug'] = $parentKey;
2018-09-14 15:48:57 +02:00
$pages->add($row);
}
2018-09-14 15:48:57 +02:00
$childList = Filesystem::listDirectories($parentDirectory.DS);
foreach ($childList as $childDirectory) {
$childKey = basename($childDirectory);
if (Filesystem::fileExists($childDirectory.DS.'index.md')) {
$row = $this->parsePage($childDirectory.DS.'index.md');
$row['slug'] = $childKey;
$row['parent'] = $parentKey;
$pages->add($row);
}
2018-09-14 15:48:57 +02:00
}
}
Theme::plugins('afterPageCreate');
reindexCategories();
reindextags();
2018-09-14 15:48:57 +02:00
}
return true;
}
private function response($data=array())
{
$json = json_encode($data);
header('Content-Type: application/json');
exit($json);
}
private function parsePage($filename)
{
$lines = file($filename);
$row = array();
// Title
$title = ltrim($lines[0], '#'); // Remove the first #
$title = trim($title);
unset($lines[0]);
$row['title'] = $title;
foreach ($lines as $key=>$line) {
if (Text::startsWith($line, '<!--')) {
$line = preg_replace('/<!\-\-/', '', $line);
$line = preg_replace('/\-\->/', '', $line);
$line = trim($line);
$explode = $explode = explode(':', $line, 2);
//$field = Text::lowercase($explode[0]);
$field = $explode[0];
2018-09-14 15:48:57 +02:00
$field = trim($field);
unset($explode[0]);
$row[$field] = trim($explode[1]);
unset($lines[$key]);
} else {
break;
}
}
$row['content'] = implode($lines);
$row['username'] = 'admin';
return $row;
}
}