]> git.wh0rd.org - tt-rss.git/blobdiff - classes/pref/feeds.php
implement upload-related support for open_basedir
[tt-rss.git] / classes / pref / feeds.php
index cd208335bb5be546eb64aea8caaab7ebfa4b6d73..f57cc37d6fdebf2ea52cd88189679bafc114cfd1 100644 (file)
@@ -3,7 +3,8 @@ class Pref_Feeds extends Handler_Protected {
 
        function csrf_ignore($method) {
                $csrf_ignored = array("index", "getfeedtree", "add", "editcats", "editfeed",
-                       "savefeedorder", "uploadicon", "feedswitherrors", "inactivefeeds");
+                       "savefeedorder", "uploadicon", "feedswitherrors", "inactivefeeds",
+                       "batchsubscribe");
 
                return array_search($method, $csrf_ignored) !== false;
        }
@@ -33,8 +34,10 @@ class Pref_Feeds extends Handler_Protected {
 
                if ($search) $search_qpart = " AND LOWER(title) LIKE LOWER('%$search%')";
 
-               $show_empty_cats = $_REQUEST['mode'] != 2 && !$search &&
-                       get_pref($this->link, '_PREFS_SHOW_EMPTY_CATS');
+               // first one is set by API
+               $show_empty_cats = $_REQUEST['force_show_empty'] ||
+                       ($_REQUEST['mode'] != 2 && !$search &&
+                               get_pref($this->link, '_PREFS_SHOW_EMPTY_CATS'));
 
                $items = array();
 
@@ -55,7 +58,7 @@ class Pref_Feeds extends Handler_Protected {
 
                        $cat['items'] = $this->get_category_items($line['id']);
 
-                       $cat['param'] = vsprintf(ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
+                       $cat['param'] = vsprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
 
                        if (count($cat['items']) > 0 || $show_empty_cats)
                                array_push($items, $cat);
@@ -183,8 +186,9 @@ class Pref_Feeds extends Handler_Protected {
                }
 
                if ($enable_cats) {
-                       $show_empty_cats = $_REQUEST['mode'] != 2 && !$search &&
-                               get_pref($this->link, '_PREFS_SHOW_EMPTY_CATS');
+                       $show_empty_cats = $_REQUEST['force_show_empty'] ||
+                               ($_REQUEST['mode'] != 2 && !$search &&
+                               get_pref($this->link, '_PREFS_SHOW_EMPTY_CATS'));
 
                        $result = db_query($this->link, "SELECT id, title FROM ttrss_feed_categories
                                WHERE owner_uid = " . $_SESSION["uid"] . " AND parent_cat IS NULL ORDER BY order_id, title");
@@ -202,7 +206,7 @@ class Pref_Feeds extends Handler_Protected {
 
                                $cat['items'] = $this->get_category_items($line['id']);
 
-                               $cat['param'] = vsprintf(ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
+                               $cat['param'] = vsprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
 
                                if (count($cat['items']) > 0 || $show_empty_cats)
                                        array_push($root['items'], $cat);
@@ -244,13 +248,13 @@ class Pref_Feeds extends Handler_Protected {
                                array_push($cat['items'], $feed);
                        }
 
-                       $cat['param'] = vsprintf(ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
+                       $cat['param'] = vsprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
 
                        if (count($cat['items']) > 0 || $show_empty_cats)
                                array_push($root['items'], $cat);
 
                        $root['param'] += count($cat['items']);
-                       $root['param'] = vsprintf(ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
+                       $root['param'] = vsprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
 
                } else {
                        $feed_result = db_query($this->link, "SELECT id, title, last_error,
@@ -275,7 +279,7 @@ class Pref_Feeds extends Handler_Protected {
                                array_push($root['items'], $feed);
                        }
 
-                       $root['param'] = vsprintf(ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
+                       $root['param'] = vsprintf(_ngettext('(%d feed)', '(%d feeds)', count($cat['items'])), count($cat['items']));
                }
 
                $fl = array();
@@ -459,7 +463,7 @@ class Pref_Feeds extends Handler_Protected {
                        WHERE id = '$feed_id' AND owner_uid = ". $_SESSION["uid"]);
 
                if (db_num_rows($result) != 0) {
-                       unlink(ICONS_DIR . "/$feed_id.ico");
+                       @unlink(ICONS_DIR . "/$feed_id.ico");
                }
 
                return;
@@ -468,7 +472,22 @@ class Pref_Feeds extends Handler_Protected {
        function uploadicon() {
                header("Content-type: text/html");
 
-               $icon_file = $_FILES['icon_file']['tmp_name'];
+               $tmp_file = false;
+
+               if (is_uploaded_file($_FILES['icon_file']['tmp_name'])) {
+                       $tmp_file = tempnam(CACHE_DIR . '/upload', 'icon');
+
+                       $result = move_uploaded_file($_FILES['icon_file']['tmp_name'],
+                               $tmp_file);
+
+                       if (!$result) {
+                               return;
+                       }
+               } else {
+                       return;
+               }
+
+               $icon_file = $tmp_file;
                $feed_id = db_escape_string($this->link, $_REQUEST["feed_id"]);
 
                if (is_file($icon_file) && $feed_id) {
@@ -478,8 +497,8 @@ class Pref_Feeds extends Handler_Protected {
                                        WHERE id = '$feed_id' AND owner_uid = ". $_SESSION["uid"]);
 
                                if (db_num_rows($result) != 0) {
-                                       unlink(ICONS_DIR . "/$feed_id.ico");
-                                       move_uploaded_file($icon_file, ICONS_DIR . "/$feed_id.ico");
+                                       @unlink(ICONS_DIR . "/$feed_id.ico");
+                                       rename($icon_file, ICONS_DIR . "/$feed_id.ico");
                                        $rc = 0;
                                } else {
                                        $rc = 2;
@@ -491,6 +510,8 @@ class Pref_Feeds extends Handler_Protected {
                        $rc = 2;
                }
 
+               @unlink($icon_file);
+
                print "<script type=\"text/javascript\">";
                print "parent.uploadIconHandler($rc);";
                print "</script>";
@@ -739,7 +760,9 @@ class Pref_Feeds extends Handler_Protected {
 
                $feed_ids = db_escape_string($this->link, $_REQUEST["ids"]);
 
-               print "<div class=\"dialogNotice\">" . __("Enable the options you wish to apply using checkboxes on the right:") . "</div>";
+               print_notice("Enable the options you wish to apply using checkboxes on the right:");
+
+               print "<p>";
 
                print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"ids\" value=\"$feed_ids\">";
                print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-feeds\">";
@@ -1273,6 +1296,8 @@ class Pref_Feeds extends Handler_Protected {
                        dojoType=\"dijit.MenuItem\">".__('Reset sort order')."</div>";
                print "<div onclick=\"batchSubscribe()\"
                        dojoType=\"dijit.MenuItem\">".__('Batch subscribe')."</div>";
+               print "<div dojoType=\"dijit.MenuItem\" onclick=\"removeSelectedFeeds()\">"
+                       .__('Unsubscribe')."</div> ";
                print "</div></div>";
 
                if (get_pref($this->link, 'ENABLE_FEED_CATS')) {
@@ -1281,8 +1306,6 @@ class Pref_Feeds extends Handler_Protected {
                        print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
                        print "<div onclick=\"createCategory()\"
                                dojoType=\"dijit.MenuItem\">".__('Add category')."</div>";
-                       print "<div onclick=\"toggleHiddenFeedCats()\"
-                               dojoType=\"dijit.MenuItem\">".__('(Un)hide empty categories')."</div>";
                        print "<div onclick=\"resetCatOrder()\"
                                dojoType=\"dijit.MenuItem\">".__('Reset sort order')."</div>";
                        print "<div onclick=\"removeSelectedCategories()\"
@@ -1294,8 +1317,8 @@ class Pref_Feeds extends Handler_Protected {
                print $error_button;
                print $inactive_button;
 
-               print "<button dojoType=\"dijit.form.Button\" onclick=\"removeSelectedFeeds()\">"
-                       .__('Unsubscribe')."</button dojoType=\"dijit.form.Button\"> ";
+               print "<button onclick=\"toggleHiddenFeedCats()\"
+                       dojoType=\"dijit.form.Button\">".__('(Un)hide empty categories')."</button>";
 
                if (defined('_ENABLE_FEED_DEBUGGING')) {
 
@@ -1394,7 +1417,7 @@ class Pref_Feeds extends Handler_Protected {
 
                print __("Published OPML does not include your Tiny Tiny RSS settings, feeds that require authentication or feeds hidden from Popular feeds.") . "</p>";
 
-               print "<button dojoType=\"dijit.form.Button\" onclick=\"return displayDlg('pubOPMLUrl')\">".
+               print "<button dojoType=\"dijit.form.Button\" onclick=\"return displayDlg('".__("Public OPML URL")."','pubOPMLUrl')\">".
                        __('Display published OPML URL')."</button> ";
 
                global $pluginhost;
@@ -1431,7 +1454,7 @@ class Pref_Feeds extends Handler_Protected {
                $rss_url = '-2::' . htmlspecialchars(get_self_url_prefix() .
                                "/public.php?op=rss&id=-2&view-mode=all_articles");;
 
-               print "<button dojoType=\"dijit.form.Button\" onclick=\"return displayDlg('generatedFeed', '$rss_url')\">".
+               print "<button dojoType=\"dijit.form.Button\" onclick=\"return displayDlg('".__("View as RSS")."','generatedFeed', '$rss_url')\">".
                        __('Display URL')."</button> ";
 
                print "<button dojoType=\"dijit.form.Button\" onclick=\"return clearFeedAccessKeys()\">".
@@ -1520,7 +1543,7 @@ class Pref_Feeds extends Handler_Protected {
                        GROUP BY ttrss_feeds.title, ttrss_feeds.id, ttrss_feeds.site_url, ttrss_feeds.feed_url
                        ORDER BY last_article");
 
-               print "<div class=\"dialogNotice\">" . __("These feeds have not been updated with new content for 3 months (oldest first):") . "</div>";
+               print "<h2" .__("These feeds have not been updated with new content for 3 months (oldest first):") . "</h2>";
 
                print "<div dojoType=\"dijit.Toolbar\">";
                print "<div dojoType=\"dijit.form.DropDownButton\">".
@@ -1586,7 +1609,8 @@ class Pref_Feeds extends Handler_Protected {
        }
 
        function feedsWithErrors() {
-               print "<div class=\"dialogNotice\">" . __("These feeds have not been updated because of errors:") . "</div>";
+               print "<h2>" . __("These feeds have not been updated because of errors:") .
+                       "</h2>";
 
                $result = db_query($this->link, "SELECT id,title,feed_url,last_error,site_url
                FROM ttrss_feeds WHERE last_error != '' AND owner_uid = ".$_SESSION["uid"]);
@@ -1696,18 +1720,30 @@ class Pref_Feeds extends Handler_Protected {
 
                        /* prepare feed if necessary */
 
+                       $result = db_query($link, "SELECT feed_url FROM ttrss_feeds WHERE id = $id
+                               AND owner_uid = $owner_uid");
+
+                       $feed_url = db_escape_string($link, db_fetch_result($result, 0, "feed_url"));
+
                        $result = db_query($link, "SELECT id FROM ttrss_archived_feeds
-                               WHERE id = '$id'");
+                               WHERE feed_url = '$feed_url' AND owner_uid = $owner_uid");
 
                        if (db_num_rows($result) == 0) {
+                               $result = db_query($link, "SELECT MAX(id) AS id FROM ttrss_archived_feeds");
+                               $new_feed_id = (int)db_fetch_result($result, 0, "id") + 1;
+
                                db_query($link, "INSERT INTO ttrss_archived_feeds
                                        (id, owner_uid, title, feed_url, site_url)
-                               SELECT id, owner_uid, title, feed_url, site_url from ttrss_feeds
-                               WHERE id = '$id'");
+                               SELECT $new_feed_id, owner_uid, title, feed_url, site_url from ttrss_feeds
+                               WHERE id = '$id'");
+
+                               $archive_id = $new_feed_id;
+                       } else {
+                               $archive_id = db_fetch_result($result, 0, "id");
                        }
 
                        db_query($link, "UPDATE ttrss_user_entries SET feed_id = NULL,
-                               orig_feed_id = '$id' WHERE feed_id = '$id' AND
+                               orig_feed_id = '$archive_id' WHERE feed_id = '$id' AND
                                        marked = true AND owner_uid = $owner_uid");
 
                        /* Remove access key for the feed */
@@ -1734,5 +1770,138 @@ class Pref_Feeds extends Handler_Protected {
                }
        }
 
+       function batchSubscribe() {
+               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-feeds\">";
+               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"batchaddfeeds\">";
+
+               print "<table width='100%'><tr><td>
+                       ".__("Add one valid RSS feed per line (no feed detection is done)")."
+               </td><td align='right'>";
+               if (get_pref($this->link, 'ENABLE_FEED_CATS')) {
+                       print __('Place in category:') . " ";
+                       print_feed_cat_select($this->link, "cat", false, 'dojoType="dijit.form.Select"');
+               }
+               print "</td></tr><tr><td colspan='2'>";
+               print "<textarea
+                       style='font-size : 12px; width : 100%; height: 200px;'
+                       placeHolder=\"".__("Feeds to subscribe, One per line")."\"
+                       dojoType=\"dijit.form.SimpleTextarea\" required=\"1\" name=\"feeds\"></textarea>";
+
+               print "</td></tr><tr><td colspan='2'>";
+
+               print "<div id='feedDlg_loginContainer' style='display : none'>
+                               " .
+                               " <input dojoType=\"dijit.form.TextBox\" name='login'\"
+                                       placeHolder=\"".__("Login")."\"
+                                       style=\"width : 10em;\"> ".
+                               " <input
+                                       placeHolder=\"".__("Password")."\"
+                                       dojoType=\"dijit.form.TextBox\" type='password'
+                                       style=\"width : 10em;\" name='pass'\">".
+                               "</div>";
+
+               print "</td></tr><tr><td colspan='2'>";
+
+               print "<div style=\"clear : both\">
+                       <input type=\"checkbox\" name=\"need_auth\" dojoType=\"dijit.form.CheckBox\" id=\"feedDlg_loginCheck\"
+                                       onclick='checkboxToggleElement(this, \"feedDlg_loginContainer\")'>
+                               <label for=\"feedDlg_loginCheck\">".
+                               __('Feeds require authentication.')."</div>";
+
+               print "</form>";
+
+               print "</td></tr></table>";
+
+               print "<div class=\"dlgButtons\">
+                       <button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('batchSubDlg').execute()\">".__('Subscribe')."</button>
+                       <button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('batchSubDlg').hide()\">".__('Cancel')."</button>
+                       </div>";
+       }
+
+       function batchAddFeeds() {
+               $cat_id = db_escape_string($this->link, $_REQUEST['cat']);
+               $feeds = explode("\n", $_REQUEST['feeds']);
+               $login = db_escape_string($this->link, $_REQUEST['login']);
+               $pass = db_escape_string($this->link, $_REQUEST['pass']);
+
+               foreach ($feeds as $feed) {
+                       $feed = db_escape_string($this->link, trim($feed));
+
+                       if (validate_feed_url($feed)) {
+
+                               db_query($this->link, "BEGIN");
+
+                               if ($cat_id == "0" || !$cat_id) {
+                                       $cat_qpart = "NULL";
+                               } else {
+                                       $cat_qpart = "'$cat_id'";
+                               }
+
+                               $result = db_query($this->link,
+                                       "SELECT id FROM ttrss_feeds
+                                       WHERE feed_url = '$feed' AND owner_uid = ".$_SESSION["uid"]);
+
+                               if (db_num_rows($result) == 0) {
+                                       $result = db_query($this->link,
+                                               "INSERT INTO ttrss_feeds
+                                                       (owner_uid,feed_url,title,cat_id,auth_login,auth_pass,update_method)
+                                               VALUES ('".$_SESSION["uid"]."', '$feed',
+                                                       '[Unknown]', $cat_qpart, '$login', '$pass', 0)");
+                               }
+
+                               db_query($this->link, "COMMIT");
+                       }
+               }
+       }
+
+       function regenOPMLKey() {
+               $this->update_feed_access_key($this->link, 'OPML:Publish',
+               false, $_SESSION["uid"]);
+
+               $new_link = Opml::opml_publish_url($this->link);
+
+               print json_encode(array("link" => $new_link));
+       }
+
+       function regenFeedKey() {
+               $feed_id = db_escape_string($this->link, $_REQUEST['id']);
+               $is_cat = db_escape_string($this->link, $_REQUEST['is_cat']) == "true";
+
+               $new_key = $this->update_feed_access_key($this->link, $feed_id, $is_cat);
+
+               print json_encode(array("link" => $new_key));
+       }
+
+
+       private function update_feed_access_key($link, $feed_id, $is_cat, $owner_uid = false) {
+               if (!$owner_uid) $owner_uid = $_SESSION["uid"];
+
+               $sql_is_cat = bool_to_sql_bool($is_cat);
+
+               $result = db_query($link, "SELECT access_key FROM ttrss_access_keys
+                       WHERE feed_id = '$feed_id'      AND is_cat = $sql_is_cat
+                       AND owner_uid = " . $owner_uid);
+
+               if (db_num_rows($result) == 1) {
+                       $key = db_escape_string($this->link, sha1(uniqid(rand(), true)));
+
+                       db_query($link, "UPDATE ttrss_access_keys SET access_key = '$key'
+                               WHERE feed_id = '$feed_id' AND is_cat = $sql_is_cat
+                               AND owner_uid = " . $owner_uid);
+
+                       return $key;
+
+               } else {
+                       return get_feed_access_key($link, $feed_id, $is_cat, $owner_uid);
+               }
+       }
+
+       // Silent
+       function clearKeys() {
+               db_query($this->link, "DELETE FROM ttrss_access_keys WHERE
+                       owner_uid = " . $_SESSION["uid"]);
+       }
+
+
 }
 ?>