init.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. class Af_Enclosure_Fix_Type extends Plugin {
  3. /* @var PluginHost $host */
  4. private $host;
  5. function about() {
  6. return array(1.0,
  7. "Overrides content type for enclosures based on server-provided data",
  8. "fox");
  9. }
  10. function save() {
  11. $enable_share_anything = checkbox_to_sql_bool($_POST["enable_share_anything"]);
  12. $this->host->set($this, "enable_share_anything", $enable_share_anything);
  13. echo __("Data saved.");
  14. }
  15. function init($host)
  16. {
  17. $this->host = $host;
  18. if (version_compare(PHP_VERSION, '5.6.0', '<')) {
  19. return;
  20. }
  21. $host->add_hook($host::HOOK_ENCLOSURE_IMPORTED, $this);
  22. $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
  23. $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
  24. $host->add_filter_action($this, "action_fix_enclosure_type", __("Fix media enclosures"));
  25. }
  26. /** @noinspection PhpUnused */
  27. function hook_prefs_edit_feed($feed_id) {
  28. print "<header>".__("Override enclosure content type")."</header>";
  29. print "<section>";
  30. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  31. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  32. $key = array_search($feed_id, $enabled_feeds);
  33. $checked = $key !== FALSE ? "checked" : "";
  34. print "<fieldset>";
  35. print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_enclosure_fix_type_enabled'
  36. name='af_enclosure_fix_type_enabled' $checked>&nbsp;".__('Enable for this feed')."</label>";
  37. print "</fieldset>";
  38. print "</section>";
  39. }
  40. /** @noinspection PhpUnused */
  41. function hook_prefs_save_feed($feed_id) {
  42. $enabled_feeds = $this->filter_unknown_feeds($this->host->get($this, "enabled_feeds"));
  43. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  44. $enable = checkbox_to_sql_bool($_POST["af_enclosure_fix_type_enabled"]);
  45. $key = array_search($feed_id, $enabled_feeds);
  46. if ($enable) {
  47. if ($key === FALSE) {
  48. array_push($enabled_feeds, $feed_id);
  49. }
  50. } else {
  51. if ($key !== FALSE) {
  52. unset($enabled_feeds[$key]);
  53. }
  54. }
  55. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  56. }
  57. /** @noinspection PhpUnused */
  58. function hook_enclosure_imported($enc, $feed) {
  59. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  60. if (!is_array($enabled_feeds) || array_search($feed, $enabled_feeds) === FALSE)
  61. return $enc;
  62. $headers = @get_headers($enc->link, 1);
  63. if (is_array($headers) && isset($headers["Content-Type"])) {
  64. $enc->type = $headers["Content-Type"];
  65. }
  66. return $enc;
  67. }
  68. private function filter_unknown_feeds($enabled_feeds) {
  69. $tmp = array();
  70. foreach ($enabled_feeds as $feed) {
  71. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
  72. $sth->execute([$feed, $_SESSION['uid']]);
  73. if ($row = $sth->fetch()) {
  74. array_push($tmp, $feed);
  75. }
  76. }
  77. return $tmp;
  78. }
  79. function api_version() {
  80. return 2;
  81. }
  82. }