summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rwxr-xr-xplugins/af_readability/init.php4
-rwxr-xr-xplugins/af_zz_imgproxy/init.php78
-rwxr-xr-xplugins/cache_starred_images/init.php228
-rw-r--r--plugins/import_export/import_export.js123
-rwxr-xr-xplugins/import_export/init.php522
5 files changed, 155 insertions, 800 deletions
diff --git a/plugins/af_readability/init.php b/plugins/af_readability/init.php
index 8bb368389..7f3c6db4d 100755
--- a/plugins/af_readability/init.php
+++ b/plugins/af_readability/init.php
@@ -48,7 +48,7 @@ class Af_Readability extends Plugin {
function hook_prefs_tab($args) {
if ($args != "prefFeeds") return;
- print "<div dojoType='dijit.layout.AccordionPane'
+ print "<div dojoType='dijit.layout.AccordionPane'
title=\"<i class='material-icons'>extension</i> ".__('Readability settings (af_readability)')."\">";
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
@@ -178,7 +178,7 @@ class Af_Readability extends Plugin {
// this is the worst hack yet :(
if (strtolower($tmpdoc->encoding) != 'utf-8') {
- $tmp = preg_replace("/<meta.*?charset.*?\/>/i", "", $tmp);
+ $tmp = preg_replace("/<meta.*?charset.*?\/?>/i", "", $tmp);
$tmp = mb_convert_encoding($tmp, 'utf-8', $tmpdoc->encoding);
}
diff --git a/plugins/af_zz_imgproxy/init.php b/plugins/af_zz_imgproxy/init.php
index b172d4563..ddc30936f 100755
--- a/plugins/af_zz_imgproxy/init.php
+++ b/plugins/af_zz_imgproxy/init.php
@@ -4,6 +4,9 @@ class Af_Zz_ImgProxy extends Plugin {
/* @var PluginHost $host */
private $host;
+ /* @var DiskCache $cache */
+ private $cache;
+
function about() {
return array(1.0,
"Load insecure images via built-in proxy",
@@ -18,6 +21,7 @@ class Af_Zz_ImgProxy extends Plugin {
function init($host) {
$this->host = $host;
+ $this->cache = new DiskCache("images");
$host->add_hook($host::HOOK_RENDER_ARTICLE, $this);
$host->add_hook($host::HOOK_RENDER_ARTICLE_CDM, $this);
@@ -50,16 +54,12 @@ class Af_Zz_ImgProxy extends Plugin {
return;
}
- $local_filename = CACHE_DIR . "/images/" . sha1($url);
-
- if ($_REQUEST["debug"] == "1") { print $url . "\n" . $local_filename; die; }
-
- header("Content-Disposition: inline; filename=\"".basename($local_filename)."\"");
-
- if (file_exists($local_filename)) {
-
- send_local_file($local_filename);
+ $local_filename = sha1($url);
+ if ($this->cache->exists($local_filename)) {
+ header("Location: " . $this->cache->getUrl($local_filename));
+ return;
+ //$this->cache->send($local_filename);
} else {
$data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]);
@@ -67,10 +67,10 @@ class Af_Zz_ImgProxy extends Plugin {
$disable_cache = $this->host->get($this, "disable_cache");
- if (!$disable_cache && strlen($data) > MIN_CACHE_FILE_SIZE) {
- if (file_put_contents($local_filename, $data)) {
- $mimetype = mime_content_type($local_filename);
- header("Content-type: $mimetype");
+ if (!$disable_cache) {
+ if ($this->cache->put($local_filename, $data)) {
+ header("Location: " . $this->cache->getUrl($local_filename));
+ return;
}
}
@@ -110,36 +110,40 @@ class Af_Zz_ImgProxy extends Plugin {
}
}
- function rewrite_url_if_needed($url, $all_remote = false) {
- $scheme = parse_url($url, PHP_URL_SCHEME);
+ private function rewrite_url_if_needed($url, $all_remote = false) {
+ /* we don't need to handle URLs where local cache already exists, tt-rss rewrites those automatically */
+ if (!$this->cache->exists(sha1($url))) {
- if ($all_remote) {
- $host = parse_url($url, PHP_URL_HOST);
- $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST);
+ $scheme = parse_url($url, PHP_URL_SCHEME);
- $is_remote = $host != $self_host;
- } else {
- $is_remote = false;
- }
+ if ($all_remote) {
+ $host = parse_url($url, PHP_URL_HOST);
+ $self_host = parse_url(get_self_url_prefix(), PHP_URL_HOST);
+
+ $is_remote = $host != $self_host;
+ } else {
+ $is_remote = false;
+ }
- if (($scheme != 'https' && $scheme != "") || $is_remote) {
- if (strpos($url, "data:") !== 0) {
- $parts = parse_url($url);
-
- foreach (explode(" " , $this->ssl_known_whitelist) as $host) {
- if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) {
- $parts['scheme'] = 'https';
- $url = build_url($parts);
- if ($all_remote && $is_remote) {
- break;
- } else {
- return $url;
+ if (($scheme != 'https' && $scheme != "") || $is_remote) {
+ if (strpos($url, "data:") !== 0) {
+ $parts = parse_url($url);
+
+ foreach (explode(" " , $this->ssl_known_whitelist) as $host) {
+ if (substr(strtolower($parts['host']), -strlen($host)) === strtolower($host)) {
+ $parts['scheme'] = 'https';
+ $url = build_url($parts);
+ if ($all_remote && $is_remote) {
+ break;
+ } else {
+ return $url;
+ }
}
}
- }
- return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
- urlencode($url);
+ return get_self_url_prefix() . "/public.php?op=pluginhandler&plugin=af_zz_imgproxy&pmethod=imgproxy&url=" .
+ urlencode($url);
+ }
}
}
diff --git a/plugins/cache_starred_images/init.php b/plugins/cache_starred_images/init.php
index 714d4cb9b..ae369f56e 100755
--- a/plugins/cache_starred_images/init.php
+++ b/plugins/cache_starred_images/init.php
@@ -1,90 +1,80 @@
<?php
-class Cache_Starred_Images extends Plugin implements IHandler {
+class Cache_Starred_Images extends Plugin {
/* @var PluginHost $host */
private $host;
- private $cache_dir;
+ /* @var DiskCache $cache */
+ private $cache;
private $max_cache_attempts = 5; // per-article
function about() {
return array(1.0,
- "Automatically cache Starred articles' images and HTML5 video files",
- "fox",
- true);
- }
-
- /**
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- function csrf_ignore($method) {
- return false;
- }
-
- /**
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- function before($method) {
- return true;
- }
-
- function after() {
- return true;
+ "Automatically cache media files in Starred articles",
+ "fox");
}
function init($host) {
$this->host = $host;
+ $this->cache = new DiskCache("starred-images");
- $this->cache_dir = CACHE_DIR . "/starred-images/";
-
- if (!is_dir($this->cache_dir)) {
- mkdir($this->cache_dir);
- }
+ if ($this->cache->makeDir())
+ chmod($this->cache->getDir(), 0777);
- if (is_dir($this->cache_dir)) {
-
- if (!is_writable($this->cache_dir))
- chmod($this->cache_dir, 0777);
-
- if (is_writable($this->cache_dir)) {
- $host->add_hook($host::HOOK_UPDATE_TASK, $this);
- $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
- $host->add_hook($host::HOOK_SANITIZE, $this);
- $host->add_handler("public", "cache_starred_images_getimage", $this);
-
- } else {
- user_error("Starred cache directory is not writable.", E_USER_WARNING);
- }
+ if (!$this->cache->exists(".no-auto-expiry"))
+ $this->cache->touch(".no-auto-expiry");
+ if ($this->cache->isWritable()) {
+ $host->add_hook($host::HOOK_HOUSE_KEEPING, $this);
+ $host->add_hook($host::HOOK_ENCLOSURE_ENTRY, $this);
+ $host->add_hook($host::HOOK_SANITIZE, $this);
} else {
- user_error("Unable to create starred cache directory.", E_USER_WARNING);
+ user_error("Starred cache directory ".$this->cache->getDir()." is not writable.", E_USER_WARNING);
}
}
- function cache_starred_images_getimage() {
- ob_end_clean();
+ /**
+ * @SuppressWarnings(PHPMD.UnusedLocalVariable)
+ */
+ function hook_house_keeping() {
+ /* since HOOK_UPDATE_TASK is not available to user plugins, this hook is a next best thing */
+
+ Debug::log("caching media of starred articles for user " . $this->host->get_owner_uid() . "...");
+
+ $sth = $this->pdo->prepare("SELECT content, ttrss_entries.title,
+ ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
+ FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
+ (ttrss_user_entries.feed_id = ttrss_feeds.id)
+ WHERE ref_id = ttrss_entries.id AND
+ marked = true AND
+ site_url != '' AND
+ ttrss_user_entries.owner_uid = ? AND
+ plugin_data NOT LIKE '%starred_cache_images%'
+ ORDER BY ".sql_random_function()." LIMIT 100");
- $hash = basename($_REQUEST["hash"]);
+ if ($sth->execute([$this->host->get_owner_uid()])) {
- if ($hash) {
+ $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
- $filename = $this->cache_dir . "/" . basename($hash);
+ while ($line = $sth->fetch()) {
+ Debug::log("processing article " . $line["title"], Debug::$LOG_VERBOSE);
- if (file_exists($filename)) {
- header("Content-Disposition: attachment; filename=\"$hash\"");
+ if ($line["site_url"]) {
+ $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
- send_local_file($filename);
- } else {
- header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
- echo "File not found.";
+ if ($success) {
+ $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
+
+ $usth->execute([$plugin_data, $line['id']]);
+ }
+ }
}
}
- }
- /**
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- function hook_house_keeping() {
- $files = glob($this->cache_dir . "/*.{png,mp4,status}", GLOB_BRACE);
+ /* actual housekeeping */
+
+ Debug::log("expiring " . $this->cache->getDir() . "...");
+
+ $files = glob($this->cache->getDir() . "/*.{png,mp4,status}", GLOB_BRACE);
$last_article_id = 0;
$article_exists = 1;
@@ -107,6 +97,16 @@ class Cache_Starred_Images extends Plugin implements IHandler {
}
}
+ function hook_enclosure_entry($enc, $article_id) {
+ $local_filename = $article_id . "-" . sha1($enc["content_url"]);
+
+ if ($this->cache->exists($local_filename)) {
+ $enc["content_url"] = $this->cache->getUrl($local_filename);
+ }
+
+ return $enc;
+ }
+
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
@@ -120,15 +120,12 @@ class Cache_Starred_Images extends Plugin implements IHandler {
if ($entry->hasAttribute('src')) {
$src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
- $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
- $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
+ $local_filename = $article_id . "-" . sha1($src);
- if (file_exists($local_filename)) {
- $entry->setAttribute("src", get_self_url_prefix() .
- "/public.php?op=cache_starred_images_getimage&method=image&hash=" .
- $article_id . "-" . sha1($src) . $extension);
+ if ($this->cache->exists($local_filename)) {
+ $entry->setAttribute("src", $this->cache->getUrl($local_filename));
+ $entry->removeAttribute("srcset");
}
-
}
}
}
@@ -136,44 +133,42 @@ class Cache_Starred_Images extends Plugin implements IHandler {
return $doc;
}
- function hook_update_task() {
- $res = $this->pdo->query("SELECT content, ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data
- FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON
- (ttrss_user_entries.feed_id = ttrss_feeds.id)
- WHERE ref_id = ttrss_entries.id AND
- marked = true AND
- (UPPER(content) LIKE '%<IMG%' OR UPPER(content) LIKE '%<VIDEO%') AND
- site_url != '' AND
- plugin_data NOT LIKE '%starred_cache_images%'
- ORDER BY ".sql_random_function()." LIMIT 100");
+ private function cache_url($article_id, $url) {
+ $local_filename = $article_id . "-" . sha1($url);
- $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?");
+ if (!$this->cache->exists($local_filename)) {
+ Debug::log("cache_images: downloading: $url to $local_filename", Debug::$LOG_VERBOSE);
- while ($line = $res->fetch()) {
- if ($line["site_url"]) {
- $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]);
+ $data = fetch_file_contents(["url" => $url, "max_size" => MAX_CACHE_FILE_SIZE]);
- if ($success) {
- $plugin_data = "starred_cache_images,${line['owner_uid']}:" . $line["plugin_data"];
+ if ($data)
+ return $this->cache->put($local_filename, $data);;
- $usth->execute([$plugin_data, $line['id']]);
- }
- }
+ } else {
+ //Debug::log("cache_images: local file exists for $url", Debug::$LOG_VERBOSE);
+
+ return true;
}
+
+ return false;
}
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
- function cache_article_images($content, $site_url, $owner_uid, $article_id) {
- libxml_use_internal_errors(true);
+ private function cache_article_images($content, $site_url, $owner_uid, $article_id) {
+ $status_filename = $article_id . "-" . sha1($site_url) . ".status";
- $status_filename = $this->cache_dir . $article_id . "-" . sha1($site_url) . ".status";
+ /* housekeeping might run as a separate user, in this case status/media might not be writable */
+ if (!$this->cache->isWritable($status_filename)) {
+ Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE);
+ return false;
+ }
- Debug::log("status: $status_filename", Debug::$LOG_EXTENDED);
+ Debug::log("status: $status_filename", Debug::$LOG_VERBOSE);
- if (file_exists($status_filename))
- $status = json_decode(file_get_contents($status_filename), true);
+ if ($this->cache->exists($status_filename))
+ $status = json_decode($this->cache->get($status_filename), true);
else
$status = [];
@@ -182,47 +177,48 @@ class Cache_Starred_Images extends Plugin implements IHandler {
// only allow several download attempts for article
if ($status["attempt"] > $this->max_cache_attempts) {
Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE);
- return;
+ return false;
}
- if (!file_put_contents($status_filename, json_encode($status))) {
+ if (!$this->cache->put($status_filename, json_encode($status))) {
user_error("unable to write status file: $status_filename", E_USER_WARNING);
- return;
+ return false;
}
$doc = new DOMDocument();
- $doc->loadHTML('<?xml encoding="UTF-8">' . $content);
- $xpath = new DOMXPath($doc);
-
- $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
- $success = false;
$has_images = false;
+ $success = false;
+
+ if ($doc->loadHTML('<?xml encoding="UTF-8">' . $content)) {
+ $xpath = new DOMXPath($doc);
+ $entries = $xpath->query('(//img[@src])|(//video/source[@src])');
- foreach ($entries as $entry) {
+ foreach ($entries as $entry) {
- if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
+ if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) {
- $has_images = true;
- $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
+ $has_images = true;
- $extension = $entry->tagName == 'source' ? '.mp4' : '.png';
+ $src = rewrite_relative_url($site_url, $entry->getAttribute('src'));
- $local_filename = $this->cache_dir . $article_id . "-" . sha1($src) . $extension;
+ if ($this->cache_url($article_id, $src)) {
+ $success = true;
+ }
+ }
+ }
+ }
- Debug::log("cache_images: downloading: $src to $local_filename", Debug::$LOG_VERBOSE);
+ $esth = $this->pdo->prepare("SELECT content_url FROM ttrss_enclosures WHERE post_id = ? AND
+ (content_type LIKE '%image%' OR content_type LIKE '%video%')");
- if (!file_exists($local_filename)) {
- $file_content = fetch_file_contents(["url" => $src, "max_size" => MAX_CACHE_FILE_SIZE]);
+ if ($esth->execute([$article_id])) {
+ while ($enc = $esth->fetch()) {
- if ($file_content) {
- if (strlen($file_content) > MIN_CACHE_FILE_SIZE) {
- file_put_contents($local_filename, $file_content);
- }
+ $has_images = true;
+ $url = rewrite_relative_url($site_url, $enc["content_url"]);
- $success = true;
- }
- } else {
+ if ($this->cache_url($article_id, $url)) {
$success = true;
}
}
diff --git a/plugins/import_export/import_export.js b/plugins/import_export/import_export.js
deleted file mode 100644
index 8dc5f7570..000000000
--- a/plugins/import_export/import_export.js
+++ /dev/null
@@ -1,123 +0,0 @@
-function exportData() {
- try {
-
- var query = "backend.php?op=pluginhandler&plugin=import_export&method=exportData";
-
- if (dijit.byId("dataExportDlg"))
- dijit.byId("dataExportDlg").destroyRecursive();
-
- var exported = 0;
-
- dialog = new dijit.Dialog({
- id: "dataExportDlg",
- title: __("Export Data"),
- style: "width: 600px",
- prepare: function() {
-
- Notify.progress("Loading, please wait...");
-
- new Ajax.Request("backend.php", {
- parameters: "op=pluginhandler&plugin=import_export&method=exportrun&offset=" + exported,
- onComplete: function(transport) {
- try {
- var rv = JSON.parse(transport.responseText);
-
- if (rv && rv.exported != undefined) {
- if (rv.exported > 0) {
-
- exported += rv.exported;
-
- $("export_status_message").innerHTML =
- "<img src='images/indicator_tiny.gif'> " +
- "Exported %d articles, please wait...".replace("%d",
- exported);
-
- setTimeout('dijit.byId("dataExportDlg").prepare()', 2000);
-
- } else {
-
- $("export_status_message").innerHTML =
- ngettext("Finished, exported %d article. You can download the data <a class='visibleLink' href='%u'>here</a>.", "Finished, exported %d articles. You can download the data <a class='visibleLink' href='%u'>here</a>.", exported)
- .replace("%d", exported)
- .replace("%u", "backend.php?op=pluginhandler&plugin=import_export&subop=exportget");
-
- exported = 0;
-
- }
-
- } else {
- $("export_status_message").innerHTML =
- "Error occured, could not export data.";
- }
- } catch (e) {
- App.Error.report(e);
- }
-
- Notify.close();
-
- } });
-
- },
- execute: function() {
- if (this.validate()) {
-
-
-
- }
- },
- href: query});
-
- dialog.show();
-
-
- } catch (e) {
- App.Error.report(e);
- }
-}
-
-function dataImportComplete(iframe) {
- try {
- if (!iframe.contentDocument.body.innerHTML) return false;
-
- Element.hide(iframe);
-
- Notify.close();
-
- if (dijit.byId('dataImportDlg'))
- dijit.byId('dataImportDlg').destroyRecursive();
-
- var content = iframe.contentDocument.body.innerHTML;
-
- dialog = new dijit.Dialog({
- id: "dataImportDlg",
- title: __("Data Import"),
- style: "width: 600px",
- onCancel: function() {
-
- },
- content: content});
-
- dialog.show();
-
- } catch (e) {
- App.Error.report(e);
- }
-}
-
-function importData() {
-
- var file = $("export_file");
-
- if (file.value.length == 0) {
- alert(__("Please choose the file first."));
- return false;
- } else {
- Notify.progress("Importing, please wait...", true);
-
- Element.show("data_upload_iframe");
-
- return true;
- }
-}
-
-
diff --git a/plugins/import_export/init.php b/plugins/import_export/init.php
deleted file mode 100755
index 714574ab4..000000000
--- a/plugins/import_export/init.php
+++ /dev/null
@@ -1,522 +0,0 @@
-<?php
-class Import_Export extends Plugin implements IHandler {
- private $host;
-
- function init($host) {
- $this->host = $host;
-
- $host->add_hook($host::HOOK_PREFS_TAB, $this);
- $host->add_command("xml-import", "import articles from XML", $this, ":", "FILE");
- }
-
- function about() {
- return array(1.0,
- "Imports and exports user data using neutral XML format",
- "fox");
- }
-
- function xml_import($args) {
-
- $filename = $args['xml_import'];
-
- if (!is_file($filename)) {
- print "error: input filename ($filename) doesn't exist.\n";
- return;
- }
-
- Debug::log("please enter your username:");
-
- $username = trim(read_stdin());
-
- Debug::log("importing $filename for user $username...\n");
-
- $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?");
- $sth->execute($username);
-
- if ($row = $sth->fetch()) {
- $owner_uid = $row['id'];
-
- $this->perform_data_import($filename, $owner_uid);
- } else {
- print "error: could not find user $username.\n";
- return;
- }
- }
-
- function get_prefs_js() {
- return file_get_contents(dirname(__FILE__) . "/import_export.js");
- }
-
- function hook_prefs_tab($args) {
- if ($args != "prefFeeds") return;
-
- print "<div dojoType='dijit.layout.AccordionPane'
- title=\"<i class='material-icons'>import_export</i> ".__('Import and export')."\">";
-
- print_notice(__("You can export and import your Starred and Archived articles for safekeeping or when migrating between tt-rss instances of same version."));
-
- print "<p>";
-
- print "<button dojoType='dijit.form.Button' class='alt-primary' onclick='return exportData()'>".
- __('Export my data')."</button> ";
-
- print "<hr>";
-
- print "<iframe id='data_upload_iframe'
- name='data_upload_iframe' onload='dataImportComplete(this)'
- style='width: 400px; height: 100px; display: none;'></iframe>";
-
- print "<form name='import_form' style='display : block' target='data_upload_iframe'
- enctype='multipart/form-data' method='POST'
- action='backend.php'>
- <label class='dijitButton'>".__("Choose file...")."
- <input style='display : none' id='export_file' name='export_file' type='file'>&nbsp;
- </label>
- <input type='hidden' name='op' value='pluginhandler'>
- <input type='hidden' name='plugin' value='import_export'>
- <input type='hidden' name='method' value='dataimport'>
- <button dojoType='dijit.form.Button' onclick='return importData();' class='alt-primary' type='submit'>" .
- __('Import') . "</button>";
-
- print "</form>";
-
- print "</p>";
-
- print "</div>"; # pane
- }
-
- function csrf_ignore($method) {
- return in_array($method, array("exportget"));
- }
-
- /**
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- function before($method) {
- return $_SESSION["uid"] != false;
- }
-
- function after() {
- return true;
- }
-
- /**
- * @SuppressWarnings(unused)
- */
- function exportget() {
- $exportname = CACHE_DIR . "/export/" .
- sha1($_SESSION['uid'] . $_SESSION['login']) . ".xml";
-
- if (file_exists($exportname)) {
- header("Content-type: text/xml");
-
- $timestamp_suffix = date("Y-m-d", filemtime($exportname));
-
- if (function_exists('gzencode')) {
- header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml.gz");
- echo gzencode(file_get_contents($exportname));
- } else {
- header("Content-Disposition: attachment; filename=TinyTinyRSS_exported_${timestamp_suffix}.xml");
- echo file_get_contents($exportname);
- }
- } else {
- echo "File not found.";
- }
- }
-
- function exportrun() {
- $offset = (int) $_REQUEST['offset'];
- $exported = 0;
- $limit = 250;
-
- if ($offset < 10000 && is_writable(CACHE_DIR . "/export")) {
-
- $sth = $this->pdo->prepare("SELECT
- ttrss_entries.guid,
- ttrss_entries.title,
- content,
- marked,
- published,
- score,
- note,
- link,
- tag_cache,
- label_cache,
- ttrss_feeds.title AS feed_title,
- ttrss_feeds.feed_url AS feed_url,
- ttrss_entries.updated
- FROM
- ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id),
- ttrss_entries
- WHERE
- (marked = true OR feed_id IS NULL) AND
- ref_id = ttrss_entries.id AND
- ttrss_user_entries.owner_uid = ?
- ORDER BY ttrss_entries.id LIMIT $limit OFFSET $offset");
-
- $sth->execute([$_SESSION['uid']]);
-
- $exportname = sha1($_SESSION['uid'] . $_SESSION['login']);
-
- if ($offset == 0) {
- $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "w");
- fputs($fp, "<articles schema-version=\"".SCHEMA_VERSION."\">");
- } else {
- $fp = fopen(CACHE_DIR . "/export/$exportname.xml", "a");
- }
-
- if ($fp) {
-
- $exported = 0;
- while ($line = $sth->fetch(PDO::FETCH_ASSOC)) {
- ++$exported;
-
- fputs($fp, "<article>\n");
-
- foreach ($line as $k => $v) {
-
- fputs($fp, " ");
-
- if (is_bool($v))
- $v = (int) $v;
-
- if (!$v || is_numeric($v)) {
- fputs($fp, "<$k>$v</$k>\n");
- } else {
- $v = str_replace("]]>", "]]]]><![CDATA[>", $v);
- fputs($fp, "<$k><![CDATA[$v]]></$k>\n");
- }
- }
-
- fputs($fp, "</article>\n");
- }
-
- if ($exported < $limit && $exported > 0) {
- fputs($fp, "</articles>");
- }
-
- fclose($fp);
- }
-
- }
-
- print json_encode(array("exported" => $exported));
- }
-
- function perform_data_import($filename, $owner_uid) {
-
- $num_imported = 0;
- $num_processed = 0;
- $num_feeds_created = 0;
-
- libxml_disable_entity_loader(false);
-
- $doc = new DOMDocument();
-
- if (!$doc_loaded = @$doc->load($filename)) {
- $contents = file_get_contents($filename);
-
- if ($contents) {
- $data = @gzuncompress($contents);
- }
-
- if (!$data) {
- $data = @gzdecode($contents);
- }
-
- if ($data)
- $doc_loaded = $doc->loadXML($data);
- }
-
- libxml_disable_entity_loader(true);
-
- if ($doc_loaded) {
-
- $xpath = new DOMXpath($doc);
-
- $container = $doc->firstChild;
-
- if ($container && $container->hasAttribute('schema-version')) {
- $schema_version = $container->getAttribute('schema-version');
-
- if ($schema_version != SCHEMA_VERSION) {
- print "<p>" .__("Could not import: incorrect schema version.") . "</p>";
- return;
- }
-
- } else {
- print "<p>" . __("Could not import: unrecognized document format.") . "</p>";
- return;
- }
-
- $articles = $xpath->query("//article");
-
- foreach ($articles as $article_node) {
- if ($article_node->childNodes) {
-
- $ref_id = 0;
-
- $article = array();
-
- foreach ($article_node->childNodes as $child) {
- if ($child->nodeName == 'content' || $child->nodeName == 'label_cache') {
- $article[$child->nodeName] = $child->nodeValue;
- } else {
- $article[$child->nodeName] = clean($child->nodeValue);
- }
- }
-
- //print_r($article);
-
- if ($article['guid']) {
-
- ++$num_processed;
-
- $this->pdo->beginTransaction();
-
- //print 'GUID:' . $article['guid'] . "\n";
-
- $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries
- WHERE guid = ?");
- $sth->execute([$article['guid']]);
-
- if ($row = $sth->fetch()) {
- $ref_id = $row['id'];
- } else {
- $sth = $this->pdo->prepare(
- "INSERT INTO ttrss_entries
- (title,
- guid,
- link,
- updated,
- content,
- content_hash,
- no_orig_date,
- date_updated,
- date_entered,
- comments,
- num_comments,
- author)
- VALUES
- (?, ?, ?, ?, ?, ?,
- false,
- NOW(),
- NOW(),
- '',
- '0',
- '')");
-
- $sth->execute([
- $article['title'],
- $article['guid'],
- $article['link'],
- $article['updated'],
- $article['content'],
- sha1($article['content'])
- ]);
-
- $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries
- WHERE guid = ?");
- $sth->execute([$article['guid']]);
-
- if ($row = $sth->fetch()) {
- $ref_id = $row['id'];
- }
- }
-
- //print "Got ref ID: $ref_id\n";
-
- if ($ref_id) {
-
- $feed = NULL;
-
- if ($article['feed_url'] && $article['feed_title']) {
-
- $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
- WHERE feed_url = ? AND owner_uid = ?");
- $sth->execute([$article['feed_url'], $owner_uid]);
-
- if ($row = $sth->fetch()) {
- $feed = $row['id'];
- } else {
- // try autocreating feed in Uncategorized...
-
- $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds (owner_uid,
- feed_url, title) VALUES (?, ?, ?)");
- $res = $sth->execute([$owner_uid, $article['feed_url'], $article['feed_title']]);
-
- if ($res) {
- $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds
- WHERE feed_url = ? AND owner_uid = ?");
- $sth->execute([$article['feed_url'], $owner_uid]);
-
- if ($row = $sth->fetch()) {
- ++$num_feeds_created;
-
- $feed = $row['id'];
- }
- }
- }
- }
-
- if ($feed)
- $feed_qpart = "feed_id = " . (int) $feed;
- else
- $feed_qpart = "feed_id IS NULL";
-
- //print "$ref_id / $feed / " . $article['title'] . "\n";
-
- $sth = $this->pdo->prepare("SELECT int_id FROM ttrss_user_entries
- WHERE ref_id = ? AND owner_uid = ? AND $feed_qpart");
- $sth->execute([$ref_id, $owner_uid]);
-
- if (!$sth->fetch()) {
-
- $score = (int) $article['score'];
-
- $tag_cache = $article['tag_cache'];
- $note = $article['note'];
-
- //print "Importing " . $article['title'] . "<br/>";
-
- ++$num_imported;
-
- $sth = $this->pdo->prepare(
- "INSERT INTO ttrss_user_entries
- (ref_id, owner_uid, feed_id, unread, last_read, marked,
- published, score, tag_cache, label_cache, uuid, note)
- VALUES (?, ?, ?, false,
- NULL, ?, ?, ?, ?, '', '', ?)");
-
- $res = $sth->execute([
- $ref_id,
- $owner_uid,
- $feed,
- (int)sql_bool_to_bool($article['marked']),
- (int)sql_bool_to_bool($article['published']),
- $score,
- $tag_cache,
- $note]);
-
- if ($res) {
-
- if (DB_TYPE == "pgsql") {
- $ts_lang = get_pref('DEFAULT_SEARCH_LANGUAGE', $owner_uid);
- // TODO: maybe use per-feed setting if available?
-
- if (!$ts_lang)
- $ts_lang = 'simple';
-
- $sth = $this->pdo->prepare("UPDATE ttrss_entries
- SET tsvector_combined = to_tsvector(:ts_lang, :ts_content)
- WHERE id = :id");
-
- $sth->execute([
- "id" => $ref_id,
- "ts_lang" => $ts_lang,
- "ts_content" => mb_substr(strip_tags($article['title'] . " " . $article['content']), 0, 900000)
- ]);
- }
-
- $label_cache = json_decode($article['label_cache'], true);
-
- if (is_array($label_cache) && $label_cache["no-labels"] != 1) {
- foreach ($label_cache as $label) {
- Labels::create($label[1],
- $label[2], $label[3], $owner_uid);
-
- Labels::add_article($ref_id, $label[1], $owner_uid);
- }
- }
- }
- }
- }
-
- $this->pdo->commit();
- }
- }
- }
-
- print "<p>" .
- __("Finished: ").
- vsprintf(_ngettext("%d article processed, ", "%d articles processed, ", $num_processed), $num_processed).
- vsprintf(_ngettext("%d imported, ", "%d imported, ", $num_imported), $num_imported).
- vsprintf(_ngettext("%d feed created.", "%d feeds created.", $num_feeds_created), $num_feeds_created).
- "</p>";
-
- } else {
-
- print "<p>" . __("Could not load XML document.") . "</p>";
-
- }
- }
-
- function exportData() {
-
- print "<form onsubmit='return false'>";
-
- print "<p style='text-align : center' id='export_status_message'>You need to prepare exported data first by clicking the button below.</p>";
-
- print "<footer class='text-center'>";
- print "<button dojoType='dijit.form.Button'
- type='submit' class='alt-primary'
- onclick=\"dijit.byId('dataExportDlg').prepare()\">".
- __('Prepare data')."</button>";
-
- print "<button dojoType='dijit.form.Button'
- onclick=\"dijit.byId('dataExportDlg').hide()\">".
- __('Close this window')."</button>";
-
- print "</footer>";
-
- print "</form>";
- }
-
- function dataImport() {
- header("Content-Type: text/html"); # required for iframe
-
- print "<footer class='text-center'>";
-
- if ($_FILES['export_file']['error'] != 0) {
- print_error(T_sprintf("Upload failed with error code %d (%s)",
- $_FILES['export_file']['error'],
- get_upload_error_message($_FILES['export_file']['error'])));
- } else {
-
- if (is_uploaded_file($_FILES['export_file']['tmp_name'])) {
- $tmp_file = tempnam(CACHE_DIR . '/upload', 'export');
-
- $result = move_uploaded_file($_FILES['export_file']['tmp_name'],
- $tmp_file);
-
- if (!$result) {
- print_error(__("Unable to move uploaded file."));
- return;
- }
- } else {
- print_error(__('Error: please upload OPML file.'));
- return;
- }
-
- if (is_file($tmp_file)) {
- $this->perform_data_import($tmp_file, $_SESSION['uid']);
- unlink($tmp_file);
- } else {
- print_error(__('No file uploaded.'));
- return;
- }
- }
-
- print "<button dojoType='dijit.form.Button'
- onclick=\"dijit.byId('dataImportDlg').hide()\">".
- __('Close this window')."</button>";
-
- print "</div>";
-
- }
-
- function api_version() {
- return 2;
- }
-
-}