diff --git a/.gitignore b/.gitignore index fa914784..3a373671 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ dbgenerator.php bl-content/* bl-plugins/timemachine bl-plugins/timemachine-x -bl-plugins/remote-content bl-plugins/discovery bl-plugins/updater bl-kernel/bludit.pro.php diff --git a/bl-kernel/functions.php b/bl-kernel/functions.php index 3826a5c6..0360f2d4 100644 --- a/bl-kernel/functions.php +++ b/bl-kernel/functions.php @@ -265,6 +265,12 @@ function changePluginsPosition($pluginClassList) { return true; } +/* + Create a new page + + The array $args support all the keys from variable $dbFields of the class pages.class.php + If you don't pass all the keys, the default values are used, the default values are from $dbFields in the class pages.class.php +*/ function createPage($args) { global $pages; global $syslog; diff --git a/bl-kernel/pages.class.php b/bl-kernel/pages.class.php index 5bbd3926..17b9d1f5 100644 --- a/bl-kernel/pages.class.php +++ b/bl-kernel/pages.class.php @@ -51,7 +51,7 @@ class Pages extends dbJSON { // Create a new page // This function returns the key of the new page - public function add($args, $climode=false) + public function add($args) { $row = array(); @@ -116,18 +116,16 @@ class Pages extends dbJSON { $row['type'] = 'scheduled'; } - if ($climode===false) { - // Create the directory - if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory ['.PATH_PAGES.$key.']',LOG_TYPE_ERROR); - return false; - } + // Create the directory + if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory ['.PATH_PAGES.$key.']',LOG_TYPE_ERROR); + return false; + } - // Create the index.txt and save the file - if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $contentRaw) === false ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the content in the file ['.FILENAME.']',LOG_TYPE_ERROR); - return false; - } + // Create the index.txt and save the file + if( file_put_contents(PATH_PAGES.$key.DS.FILENAME, $contentRaw) === false ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the content in the file ['.FILENAME.']',LOG_TYPE_ERROR); + return false; } // Checksum MD5 @@ -145,7 +143,7 @@ class Pages extends dbJSON { return $key; } - public function edit($args, $climode=false) + public function edit($args) { // Old key // This variable is not belong to the database so is not defined in $row @@ -213,22 +211,20 @@ class Pages extends dbJSON { $row['type'] = 'scheduled'; } - if ($climode===false) { - // Move the directory from old key to new key. - if ($newKey!==$key) { - if( Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false ) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey); - return false; - } - } - - // Make the index.txt and save the file. - if (file_put_contents(PATH_PAGES.$newKey.DS.FILENAME, $contentRaw)===false) { - Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME); + // Move the directory from old key to new key. + if ($newKey!==$key) { + if( Filesystem::mv(PATH_PAGES.$key, PATH_PAGES.$newKey) === false ) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey); return false; } } + // Make the index.txt and save the file. + if (file_put_contents(PATH_PAGES.$newKey.DS.FILENAME, $contentRaw)===false) { + Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file '.FILENAME); + return false; + } + // Remove the old key unset( $this->db[$key] ); diff --git a/bl-plugins/remote-content/languages/en.json b/bl-plugins/remote-content/languages/en.json new file mode 100644 index 00000000..06a8ebd4 --- /dev/null +++ b/bl-plugins/remote-content/languages/en.json @@ -0,0 +1,12 @@ +{ + "plugin-data": + { + "name": "Remote Content", + "description": "" + }, + "webhook": "Webhook", + "source": "Source", + "keep-content": "Keep content", + "complete-url-of-the-zip-file": "Complete URL of the zip file.", + "if-you-want-to-keep-the-content-generate-via-the-user-interface-enable-this-field": "If you want to keep the content generate via the user interface enable this field." +} \ No newline at end of file diff --git a/bl-plugins/remote-content/languages/es.json b/bl-plugins/remote-content/languages/es.json new file mode 100644 index 00000000..508446a1 --- /dev/null +++ b/bl-plugins/remote-content/languages/es.json @@ -0,0 +1,12 @@ +{ + "plugin-data": + { + "name": "Contenido remoto", + "description": "" + }, + "webhook": "Webhook", + "source": "Source", + "keep-content": "Keep content", + "complete-url-of-the-zip-file": "Complete URL of the zip file.", + "if-you-want-to-keep-the-content-generate-via-the-user-interface-enable-this-field": "If you want to keep the content generate via the user interface enable this field." +} \ 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..9aa58a43 --- /dev/null +++ b/bl-plugins/remote-content/metadata.json @@ -0,0 +1,10 @@ +{ + "author": "Bludit", + "email": "", + "website": "https://plugins.bludit.com/plugin/remote-content", + "version": "3.0", + "releaseDate": "2018-09-14", + "license": "MIT", + "compatible": "3.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..f5f5cabd --- /dev/null +++ b/bl-plugins/remote-content/plugin.php @@ -0,0 +1,176 @@ +dbFields = array( + 'webhook'=>$randomWebhook, + 'source'=>'https://github.com/bludit/remote-content-example/archive/master.zip' + ); + } + + public function form() + { + global $Language; + + if (extension_loaded('zip')===false) { + $this->formButtons = false; + return '
'.$Language->get('the-extension-zip-is-not-installed').'
'; + } + + $html = ''; + + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= ''.DOMAIN_BASE.$this->getValue('webhook').''; + $html .= '
'; + + $html .= '
'; + $html .= ''; + $html .= ''; + $html .= ''.$Language->get('Complete URL of the zip file').''; + $html .= '
'; + + return $html; + } + + public function beforeAll() + { + // Check Webhook + $webhook = $this->getValue('webhook'); + if ($this->webhook($webhook)) { + // Download files + //$this->downloadFiles(); + + // 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, 0755, true); + mkdir(PATH_UPLOADS, 0755, true); + + return true; + } + + 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')) { + $pageList = Filesystem::listDirectories($root.DS.'pages'.DS); + foreach ($pageList as $directory) { + if (Filesystem::fileExists($directory.DS.'index.md')) { + // Parse the page from the file + $row = $this->parsePage($directory.DS.'index.md'); + + // Add the page to the database + $pages->add($row); + + // Call the plugins after page created + Theme::plugins('afterPageCreate'); + + // Reindex databases + reindexCategories(); + reindextags(); + } + } + } + + 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, '