]>
Commit | Line | Data |
---|---|---|
de8260cb | 1 | <?php |
de8260cb AD |
2 | class API extends Handler { |
3 | ||
f1b3b3f3 | 4 | const API_LEVEL = 14; |
de8260cb AD |
5 | |
6 | const STATUS_OK = 0; | |
7 | const STATUS_ERR = 1; | |
8 | ||
9 | private $seq; | |
10 | ||
4dc3f7e7 AD |
11 | static function param_to_bool($p) { |
12 | return $p && ($p !== "f" && $p !== "false"); | |
13 | } | |
14 | ||
de8260cb AD |
15 | function before($method) { |
16 | if (parent::before($method)) { | |
12f31782 | 17 | header("Content-Type: text/json"); |
de8260cb AD |
18 | |
19 | if (!$_SESSION["uid"] && $method != "login" && $method != "isloggedin") { | |
6f7798b6 | 20 | $this->wrap(self::STATUS_ERR, array("error" => 'NOT_LOGGED_IN')); |
de8260cb AD |
21 | return false; |
22 | } | |
23 | ||
a42c55f0 | 24 | if ($_SESSION["uid"] && $method != "logout" && !get_pref('ENABLE_API_ACCESS')) { |
6f7798b6 | 25 | $this->wrap(self::STATUS_ERR, array("error" => 'API_DISABLED')); |
de8260cb AD |
26 | return false; |
27 | } | |
28 | ||
e6532439 | 29 | $this->seq = (int) clean($_REQUEST['seq']); |
de8260cb | 30 | |
de8260cb AD |
31 | return true; |
32 | } | |
33 | return false; | |
34 | } | |
35 | ||
36 | function wrap($status, $reply) { | |
37 | print json_encode(array("seq" => $this->seq, | |
38 | "status" => $status, | |
39 | "content" => $reply)); | |
40 | } | |
41 | ||
42 | function getVersion() { | |
43 | $rv = array("version" => VERSION); | |
6f7798b6 | 44 | $this->wrap(self::STATUS_OK, $rv); |
de8260cb AD |
45 | } |
46 | ||
47 | function getApiLevel() { | |
a3b5394a | 48 | $rv = array("level" => self::API_LEVEL); |
6f7798b6 | 49 | $this->wrap(self::STATUS_OK, $rv); |
de8260cb AD |
50 | } |
51 | ||
52 | function login() { | |
79bb5589 | 53 | @session_destroy(); |
5160620c AD |
54 | @session_start(); |
55 | ||
e6532439 AD |
56 | $login = clean($_REQUEST["user"]); |
57 | $password = clean($_REQUEST["password"]); | |
58 | $password_base64 = base64_decode(clean($_REQUEST["password"])); | |
de8260cb AD |
59 | |
60 | if (SINGLE_USER_MODE) $login = "admin"; | |
61 | ||
3467e1fd AD |
62 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_users WHERE login = ?"); |
63 | $sth->execute([$login]); | |
de8260cb | 64 | |
3467e1fd AD |
65 | if ($row = $sth->fetch()) { |
66 | $uid = $row["id"]; | |
de8260cb AD |
67 | } else { |
68 | $uid = 0; | |
69 | } | |
70 | ||
71 | if (!$uid) { | |
6f7798b6 | 72 | $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); |
de8260cb AD |
73 | return; |
74 | } | |
75 | ||
a42c55f0 AD |
76 | if (get_pref("ENABLE_API_ACCESS", $uid)) { |
77 | if (authenticate_user($login, $password)) { // try login with normal password | |
6f7798b6 | 78 | $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
5ba4ebc6 | 79 | "api_level" => self::API_LEVEL)); |
a42c55f0 | 80 | } else if (authenticate_user($login, $password_base64)) { // else try with base64_decoded password |
6f7798b6 | 81 | $this->wrap(self::STATUS_OK, array("session_id" => session_id(), |
5ba4ebc6 | 82 | "api_level" => self::API_LEVEL)); |
de8260cb | 83 | } else { // else we are not logged in |
a2108ee9 | 84 | user_error("Failed login attempt for $login from {$_SERVER['REMOTE_ADDR']}", E_USER_WARNING); |
6f7798b6 | 85 | $this->wrap(self::STATUS_ERR, array("error" => "LOGIN_ERROR")); |
de8260cb AD |
86 | } |
87 | } else { | |
6f7798b6 | 88 | $this->wrap(self::STATUS_ERR, array("error" => "API_DISABLED")); |
de8260cb AD |
89 | } |
90 | ||
91 | } | |
92 | ||
93 | function logout() { | |
94 | logout_user(); | |
6f7798b6 | 95 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
96 | } |
97 | ||
98 | function isLoggedIn() { | |
6f7798b6 | 99 | $this->wrap(self::STATUS_OK, array("status" => $_SESSION["uid"] != '')); |
de8260cb AD |
100 | } |
101 | ||
102 | function getUnread() { | |
e6532439 AD |
103 | $feed_id = clean($_REQUEST["feed_id"]); |
104 | $is_cat = clean($_REQUEST["is_cat"]); | |
de8260cb AD |
105 | |
106 | if ($feed_id) { | |
6f7798b6 | 107 | $this->wrap(self::STATUS_OK, array("unread" => getFeedUnread($feed_id, $is_cat))); |
de8260cb | 108 | } else { |
a230bf88 | 109 | $this->wrap(self::STATUS_OK, array("unread" => Feeds::getGlobalUnread())); |
de8260cb AD |
110 | } |
111 | } | |
112 | ||
113 | /* Method added for ttrss-reader for Android */ | |
114 | function getCounters() { | |
65af3b2c | 115 | $this->wrap(self::STATUS_OK, Counters::getAllCounters()); |
de8260cb AD |
116 | } |
117 | ||
118 | function getFeeds() { | |
e6532439 AD |
119 | $cat_id = clean($_REQUEST["cat_id"]); |
120 | $unread_only = API::param_to_bool(clean($_REQUEST["unread_only"])); | |
121 | $limit = (int) clean($_REQUEST["limit"]); | |
122 | $offset = (int) clean($_REQUEST["offset"]); | |
123 | $include_nested = API::param_to_bool(clean($_REQUEST["include_nested"])); | |
de8260cb | 124 | |
a42c55f0 | 125 | $feeds = $this->api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested); |
de8260cb | 126 | |
6f7798b6 | 127 | $this->wrap(self::STATUS_OK, $feeds); |
de8260cb AD |
128 | } |
129 | ||
130 | function getCategories() { | |
e6532439 AD |
131 | $unread_only = API::param_to_bool(clean($_REQUEST["unread_only"])); |
132 | $enable_nested = API::param_to_bool(clean($_REQUEST["enable_nested"])); | |
133 | $include_empty = API::param_to_bool(clean($_REQUEST['include_empty'])); | |
de8260cb AD |
134 | |
135 | // TODO do not return empty categories, return Uncategorized and standard virtual cats | |
136 | ||
48646336 AD |
137 | if ($enable_nested) |
138 | $nested_qpart = "parent_cat IS NULL"; | |
139 | else | |
140 | $nested_qpart = "true"; | |
141 | ||
3467e1fd | 142 | $sth = $this->pdo->prepare("SELECT |
d49dfa38 AD |
143 | id, title, order_id, (SELECT COUNT(id) FROM |
144 | ttrss_feeds WHERE | |
7be3fcd5 AD |
145 | ttrss_feed_categories.id IS NOT NULL AND cat_id = ttrss_feed_categories.id) AS num_feeds, |
146 | (SELECT COUNT(id) FROM | |
147 | ttrss_feed_categories AS c2 WHERE | |
148 | c2.parent_cat = ttrss_feed_categories.id) AS num_cats | |
d49dfa38 | 149 | FROM ttrss_feed_categories |
3467e1fd AD |
150 | WHERE $nested_qpart AND owner_uid = ?"); |
151 | $sth->execute([$_SESSION['uid']]); | |
de8260cb AD |
152 | |
153 | $cats = array(); | |
154 | ||
3467e1fd | 155 | while ($line = $sth->fetch()) { |
66ca7c30 | 156 | if ($include_empty || $line["num_feeds"] > 0 || $line["num_cats"] > 0) { |
a42c55f0 | 157 | $unread = getFeedUnread($line["id"], true); |
d49dfa38 AD |
158 | |
159 | if ($enable_nested) | |
86a8351c | 160 | $unread += Feeds::getCategoryChildrenUnread($line["id"]); |
d49dfa38 AD |
161 | |
162 | if ($unread || !$unread_only) { | |
163 | array_push($cats, array("id" => $line["id"], | |
164 | "title" => $line["title"], | |
165 | "unread" => $unread, | |
166 | "order_id" => (int) $line["order_id"], | |
167 | )); | |
168 | } | |
de8260cb AD |
169 | } |
170 | } | |
171 | ||
172 | foreach (array(-2,-1,0) as $cat_id) { | |
c0a08063 | 173 | if ($include_empty || !$this->isCategoryEmpty($cat_id)) { |
a42c55f0 | 174 | $unread = getFeedUnread($cat_id, true); |
de8260cb | 175 | |
c0a08063 AD |
176 | if ($unread || !$unread_only) { |
177 | array_push($cats, array("id" => $cat_id, | |
a230bf88 | 178 | "title" => Feeds::getCategoryTitle($cat_id), |
c0a08063 AD |
179 | "unread" => $unread)); |
180 | } | |
de8260cb AD |
181 | } |
182 | } | |
183 | ||
6f7798b6 | 184 | $this->wrap(self::STATUS_OK, $cats); |
de8260cb AD |
185 | } |
186 | ||
187 | function getHeadlines() { | |
e6532439 | 188 | $feed_id = clean($_REQUEST["feed_id"]); |
8aa3becc | 189 | if ($feed_id != "") { |
de8260cb | 190 | |
96ac72bc AD |
191 | if (is_numeric($feed_id)) $feed_id = (int) $feed_id; |
192 | ||
e6532439 | 193 | $limit = (int)clean($_REQUEST["limit"]); |
1a740cf6 | 194 | |
6421b429 | 195 | if (!$limit || $limit >= 200) $limit = 200; |
1a740cf6 | 196 | |
e6532439 AD |
197 | $offset = (int)clean($_REQUEST["skip"]); |
198 | $filter = clean($_REQUEST["filter"]); | |
199 | $is_cat = API::param_to_bool(clean($_REQUEST["is_cat"])); | |
200 | $show_excerpt = API::param_to_bool(clean($_REQUEST["show_excerpt"])); | |
201 | $show_content = API::param_to_bool(clean($_REQUEST["show_content"])); | |
de8260cb | 202 | /* all_articles, unread, adaptive, marked, updated */ |
e6532439 AD |
203 | $view_mode = clean($_REQUEST["view_mode"]); |
204 | $include_attachments = API::param_to_bool(clean($_REQUEST["include_attachments"])); | |
205 | $since_id = (int)clean($_REQUEST["since_id"]); | |
206 | $include_nested = API::param_to_bool(clean($_REQUEST["include_nested"])); | |
bd3c6723 | 207 | $sanitize_content = !isset($_REQUEST["sanitize"]) || |
4dc3f7e7 | 208 | API::param_to_bool($_REQUEST["sanitize"]); |
e6532439 AD |
209 | $force_update = API::param_to_bool(clean($_REQUEST["force_update"])); |
210 | $has_sandbox = API::param_to_bool(clean($_REQUEST["has_sandbox"])); | |
211 | $excerpt_length = (int)clean($_REQUEST["excerpt_length"]); | |
212 | $check_first_id = (int)clean($_REQUEST["check_first_id"]); | |
213 | $include_header = API::param_to_bool(clean($_REQUEST["include_header"])); | |
9997b38e AD |
214 | |
215 | $_SESSION['hasSandbox'] = $has_sandbox; | |
ffd07864 | 216 | |
19e47ad6 AD |
217 | $skip_first_id_check = false; |
218 | ||
0bbd1414 | 219 | $override_order = false; |
e6532439 | 220 | switch (clean($_REQUEST["order_by"])) { |
273c33e5 | 221 | case "title": |
81d96c0d | 222 | $override_order = "ttrss_entries.title, date_entered, updated"; |
273c33e5 | 223 | break; |
0bbd1414 | 224 | case "date_reverse": |
e9687f67 | 225 | $override_order = "score DESC, date_entered, updated"; |
19e47ad6 | 226 | $skip_first_id_check = true; |
0bbd1414 | 227 | break; |
228 | case "feed_dates": | |
229 | $override_order = "updated DESC"; | |
230 | break; | |
231 | } | |
ffd07864 | 232 | |
3e4af5b0 AD |
233 | /* do not rely on params below */ |
234 | ||
e6532439 | 235 | $search = clean($_REQUEST["search"]); |
3e4af5b0 | 236 | |
5c784e70 | 237 | list($headlines, $headlines_header) = $this->api_get_headlines($feed_id, $limit, $offset, |
0bbd1414 | 238 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $override_order, |
9090b874 | 239 | $include_attachments, $since_id, $search, |
19e47ad6 | 240 | $include_nested, $sanitize_content, $force_update, $excerpt_length, $check_first_id, $skip_first_id_check); |
de8260cb | 241 | |
5c784e70 AD |
242 | if ($include_header) { |
243 | $this->wrap(self::STATUS_OK, array($headlines_header, $headlines)); | |
244 | } else { | |
245 | $this->wrap(self::STATUS_OK, $headlines); | |
246 | } | |
de8260cb | 247 | } else { |
6f7798b6 | 248 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
de8260cb AD |
249 | } |
250 | } | |
251 | ||
252 | function updateArticle() { | |
e6532439 AD |
253 | $article_ids = explode(",", clean($_REQUEST["article_ids"])); |
254 | $mode = (int) clean($_REQUEST["mode"]); | |
255 | $data = clean($_REQUEST["data"]); | |
256 | $field_raw = (int)clean($_REQUEST["field"]); | |
de8260cb AD |
257 | |
258 | $field = ""; | |
259 | $set_to = ""; | |
260 | ||
261 | switch ($field_raw) { | |
262 | case 0: | |
263 | $field = "marked"; | |
7873d588 | 264 | $additional_fields = ",last_marked = NOW()"; |
de8260cb AD |
265 | break; |
266 | case 1: | |
267 | $field = "published"; | |
7873d588 | 268 | $additional_fields = ",last_published = NOW()"; |
de8260cb AD |
269 | break; |
270 | case 2: | |
271 | $field = "unread"; | |
7873d588 | 272 | $additional_fields = ",last_read = NOW()"; |
de8260cb AD |
273 | break; |
274 | case 3: | |
275 | $field = "note"; | |
276 | }; | |
277 | ||
278 | switch ($mode) { | |
279 | case 1: | |
280 | $set_to = "true"; | |
281 | break; | |
282 | case 0: | |
283 | $set_to = "false"; | |
284 | break; | |
285 | case 2: | |
286 | $set_to = "NOT $field"; | |
287 | break; | |
288 | } | |
289 | ||
3467e1fd | 290 | if ($field == "note") $set_to = $this->pdo->quote($data); |
de8260cb AD |
291 | |
292 | if ($field && $set_to && count($article_ids) > 0) { | |
293 | ||
3467e1fd | 294 | $article_qmarks = arr_qmarks($article_ids); |
de8260cb | 295 | |
3467e1fd AD |
296 | $sth = $this->pdo->prepare("UPDATE ttrss_user_entries SET |
297 | $field = $set_to $additional_fields | |
298 | WHERE ref_id IN ($article_qmarks) AND owner_uid = ?"); | |
299 | $sth->execute(array_merge($article_ids, [$_SESSION['uid']])); | |
de8260cb | 300 | |
3467e1fd | 301 | $num_updated = $sth->rowCount(); |
de8260cb AD |
302 | |
303 | if ($num_updated > 0 && $field == "unread") { | |
1a052109 | 304 | $sth = $this->pdo->prepare("SELECT DISTINCT feed_id FROM ttrss_user_entries |
9652fa6b AD |
305 | WHERE ref_id IN ($article_qmarks)"); |
306 | $sth->execute($article_ids); | |
de8260cb | 307 | |
3467e1fd | 308 | while ($line = $sth->fetch()) { |
2ed0d6c4 | 309 | CCache::update($line["feed_id"], $_SESSION["uid"]); |
de8260cb AD |
310 | } |
311 | } | |
312 | ||
6f7798b6 | 313 | $this->wrap(self::STATUS_OK, array("status" => "OK", |
de8260cb AD |
314 | "updated" => $num_updated)); |
315 | ||
316 | } else { | |
6f7798b6 | 317 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
de8260cb AD |
318 | } |
319 | ||
320 | } | |
321 | ||
322 | function getArticle() { | |
323 | ||
e6532439 | 324 | $article_ids = explode(",", clean($_REQUEST["article_id"])); |
73c77ab0 | 325 | $sanitize_content = !isset($_REQUEST["sanitize"]) || |
4dc3f7e7 | 326 | API::param_to_bool($_REQUEST["sanitize"]); |
de8260cb | 327 | |
3467e1fd | 328 | if ($article_ids) { |
de8260cb | 329 | |
3467e1fd AD |
330 | $article_qmarks = arr_qmarks($article_ids); |
331 | ||
332 | $sth = $this->pdo->prepare("SELECT id,guid,title,link,content,feed_id,comments,int_id, | |
429ff9af | 333 | marked,unread,published,score,note,lang, |
6f81395d | 334 | ".SUBSTRING_FOR_DATE."(updated,1,16) as updated, |
73c77ab0 J |
335 | author,(SELECT title FROM ttrss_feeds WHERE id = feed_id) AS feed_title, |
336 | (SELECT site_url FROM ttrss_feeds WHERE id = feed_id) AS site_url, | |
337 | (SELECT hide_images FROM ttrss_feeds WHERE id = feed_id) AS hide_images | |
6f81395d | 338 | FROM ttrss_entries,ttrss_user_entries |
3467e1fd | 339 | WHERE id IN ($article_qmarks) AND ref_id = id AND owner_uid = ?"); |
de8260cb | 340 | |
3467e1fd | 341 | $sth->execute(array_merge($article_ids, [$_SESSION['uid']])); |
de8260cb | 342 | |
6f81395d | 343 | $articles = array(); |
de8260cb | 344 | |
3467e1fd AD |
345 | while ($line = $sth->fetch()) { |
346 | ||
347 | $attachments = Article::get_article_enclosures($line['id']); | |
348 | ||
349 | $article = array( | |
350 | "id" => $line["id"], | |
351 | "guid" => $line["guid"], | |
352 | "title" => $line["title"], | |
353 | "link" => $line["link"], | |
354 | "labels" => Article::get_article_labels($line['id']), | |
4dc3f7e7 AD |
355 | "unread" => API::param_to_bool($line["unread"]), |
356 | "marked" => API::param_to_bool($line["marked"]), | |
357 | "published" => API::param_to_bool($line["published"]), | |
3467e1fd AD |
358 | "comments" => $line["comments"], |
359 | "author" => $line["author"], | |
360 | "updated" => (int) strtotime($line["updated"]), | |
361 | "feed_id" => $line["feed_id"], | |
362 | "attachments" => $attachments, | |
363 | "score" => (int)$line["score"], | |
364 | "feed_title" => $line["feed_title"], | |
365 | "note" => $line["note"], | |
366 | "lang" => $line["lang"] | |
367 | ); | |
368 | ||
369 | if ($sanitize_content) { | |
370 | $article["content"] = sanitize( | |
371 | $line["content"], | |
4dc3f7e7 | 372 | API::param_to_bool($line['hide_images']), |
3467e1fd AD |
373 | false, $line["site_url"], false, $line["id"]); |
374 | } else { | |
375 | $article["content"] = $line["content"]; | |
376 | } | |
6f81395d | 377 | |
3467e1fd AD |
378 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) { |
379 | $article = $p->hook_render_article_api(array("article" => $article)); | |
380 | } | |
6f81395d | 381 | |
3467e1fd | 382 | array_push($articles, $article); |
de8260cb | 383 | |
6f81395d | 384 | } |
de8260cb | 385 | |
6f81395d AD |
386 | $this->wrap(self::STATUS_OK, $articles); |
387 | } else { | |
388 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); | |
389 | } | |
de8260cb AD |
390 | } |
391 | ||
392 | function getConfig() { | |
393 | $config = array( | |
394 | "icons_dir" => ICONS_DIR, | |
395 | "icons_url" => ICONS_URL); | |
396 | ||
397 | $config["daemon_is_running"] = file_is_locked("update_daemon.lock"); | |
398 | ||
3467e1fd AD |
399 | $sth = $this->pdo->prepare("SELECT COUNT(*) AS cf FROM |
400 | ttrss_feeds WHERE owner_uid = ?"); | |
401 | $sth->execute([$_SESSION['uid']]); | |
402 | $row = $sth->fetch(); | |
de8260cb | 403 | |
3467e1fd | 404 | $config["num_feeds"] = $row["cf"]; |
de8260cb | 405 | |
6f7798b6 | 406 | $this->wrap(self::STATUS_OK, $config); |
de8260cb AD |
407 | } |
408 | ||
409 | function updateFeed() { | |
e6532439 | 410 | $feed_id = (int) clean($_REQUEST["feed_id"]); |
de8260cb | 411 | |
29c92d7b | 412 | if (!ini_get("open_basedir")) { |
e6c886bf | 413 | RSSUtils::update_rss_feed($feed_id); |
29c92d7b | 414 | } |
de8260cb | 415 | |
6f7798b6 | 416 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
417 | } |
418 | ||
419 | function catchupFeed() { | |
e6532439 AD |
420 | $feed_id = clean($_REQUEST["feed_id"]); |
421 | $is_cat = clean($_REQUEST["is_cat"]); | |
de8260cb | 422 | |
86a8351c | 423 | Feeds::catchup_feed($feed_id, $is_cat); |
de8260cb | 424 | |
6f7798b6 | 425 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
de8260cb AD |
426 | } |
427 | ||
428 | function getPref() { | |
e6532439 | 429 | $pref_name = clean($_REQUEST["pref_name"]); |
de8260cb | 430 | |
6f7798b6 | 431 | $this->wrap(self::STATUS_OK, array("value" => get_pref($pref_name))); |
de8260cb AD |
432 | } |
433 | ||
ea1c2903 | 434 | function getLabels() { |
e6532439 | 435 | $article_id = (int)clean($_REQUEST['article_id']); |
ea1c2903 AD |
436 | |
437 | $rv = array(); | |
438 | ||
3467e1fd | 439 | $sth = $this->pdo->prepare("SELECT id, caption, fg_color, bg_color |
ea1c2903 | 440 | FROM ttrss_labels2 |
3467e1fd AD |
441 | WHERE owner_uid = ? ORDER BY caption"); |
442 | $sth->execute([$_SESSION['uid']]); | |
ea1c2903 AD |
443 | |
444 | if ($article_id) | |
4a0da0e5 | 445 | $article_labels = Article::get_article_labels($article_id); |
ea1c2903 AD |
446 | else |
447 | $article_labels = array(); | |
448 | ||
3467e1fd | 449 | while ($line = $sth->fetch()) { |
ea1c2903 AD |
450 | |
451 | $checked = false; | |
452 | foreach ($article_labels as $al) { | |
7c9b5a3f | 453 | if (Labels::feed_to_label_id($al[0]) == $line['id']) { |
ea1c2903 AD |
454 | $checked = true; |
455 | break; | |
456 | } | |
457 | } | |
458 | ||
459 | array_push($rv, array( | |
7c9b5a3f | 460 | "id" => (int)Labels::label_to_feed_id($line['id']), |
ea1c2903 AD |
461 | "caption" => $line['caption'], |
462 | "fg_color" => $line['fg_color'], | |
463 | "bg_color" => $line['bg_color'], | |
464 | "checked" => $checked)); | |
465 | } | |
466 | ||
6f7798b6 | 467 | $this->wrap(self::STATUS_OK, $rv); |
ea1c2903 AD |
468 | } |
469 | ||
396bfdf9 AD |
470 | function setArticleLabel() { |
471 | ||
e6532439 AD |
472 | $article_ids = explode(",", clean($_REQUEST["article_ids"])); |
473 | $label_id = (int) clean($_REQUEST['label_id']); | |
474 | $assign = API::param_to_bool(clean($_REQUEST['assign'])); | |
396bfdf9 | 475 | |
3467e1fd | 476 | $label = Labels::find_caption(Labels::feed_to_label_id($label_id), $_SESSION["uid"]); |
396bfdf9 AD |
477 | |
478 | $num_updated = 0; | |
479 | ||
480 | if ($label) { | |
481 | ||
482 | foreach ($article_ids as $id) { | |
483 | ||
484 | if ($assign) | |
7c9b5a3f | 485 | Labels::add_article($id, $label, $_SESSION["uid"]); |
396bfdf9 | 486 | else |
7c9b5a3f | 487 | Labels::remove_article($id, $label, $_SESSION["uid"]); |
396bfdf9 AD |
488 | |
489 | ++$num_updated; | |
490 | ||
491 | } | |
492 | } | |
493 | ||
6f7798b6 | 494 | $this->wrap(self::STATUS_OK, array("status" => "OK", |
396bfdf9 AD |
495 | "updated" => $num_updated)); |
496 | ||
497 | } | |
498 | ||
79f9bef7 | 499 | function index($method) { |
1ffe3391 | 500 | $plugin = PluginHost::getInstance()->get_api_method(strtolower($method)); |
79f9bef7 AD |
501 | |
502 | if ($plugin && method_exists($plugin, $method)) { | |
503 | $reply = $plugin->$method(); | |
504 | ||
6f7798b6 | 505 | $this->wrap($reply[0], $reply[1]); |
79f9bef7 AD |
506 | |
507 | } else { | |
6f7798b6 | 508 | $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method)); |
79f9bef7 | 509 | } |
de8260cb AD |
510 | } |
511 | ||
8361e724 | 512 | function shareToPublished() { |
e6532439 AD |
513 | $title = strip_tags(clean($_REQUEST["title"])); |
514 | $url = strip_tags(clean($_REQUEST["url"])); | |
515 | $content = strip_tags(clean($_REQUEST["content"])); | |
8361e724 | 516 | |
a42c55f0 | 517 | if (Article::create_published_article($title, $url, $content, "", $_SESSION["uid"])) { |
6f7798b6 | 518 | $this->wrap(self::STATUS_OK, array("status" => 'OK')); |
8361e724 | 519 | } else { |
6f7798b6 | 520 | $this->wrap(self::STATUS_ERR, array("error" => 'Publishing failed')); |
8361e724 AD |
521 | } |
522 | } | |
04f60eb7 | 523 | |
a42c55f0 | 524 | static function api_get_feeds($cat_id, $unread_only, $limit, $offset, $include_nested = false) { |
04f60eb7 AD |
525 | |
526 | $feeds = array(); | |
527 | ||
3467e1fd AD |
528 | $pdo = Db::pdo(); |
529 | ||
530 | $limit = (int) $limit; | |
531 | $offset = (int) $offset; | |
532 | $cat_id = (int) $cat_id; | |
533 | ||
04f60eb7 AD |
534 | /* Labels */ |
535 | ||
536 | if ($cat_id == -4 || $cat_id == -2) { | |
65af3b2c | 537 | $counters = Counters::getLabelCounters(true); |
04f60eb7 AD |
538 | |
539 | foreach (array_values($counters) as $cv) { | |
540 | ||
541 | $unread = $cv["counter"]; | |
542 | ||
543 | if ($unread || !$unread_only) { | |
544 | ||
545 | $row = array( | |
1d3cbe31 | 546 | "id" => (int) $cv["id"], |
04f60eb7 AD |
547 | "title" => $cv["description"], |
548 | "unread" => $cv["counter"], | |
549 | "cat_id" => -2, | |
550 | ); | |
551 | ||
552 | array_push($feeds, $row); | |
553 | } | |
554 | } | |
555 | } | |
556 | ||
557 | /* Virtual feeds */ | |
558 | ||
559 | if ($cat_id == -4 || $cat_id == -1) { | |
560 | foreach (array(-1, -2, -3, -4, -6, 0) as $i) { | |
a42c55f0 | 561 | $unread = getFeedUnread($i); |
04f60eb7 AD |
562 | |
563 | if ($unread || !$unread_only) { | |
86a8351c | 564 | $title = Feeds::getFeedTitle($i); |
04f60eb7 AD |
565 | |
566 | $row = array( | |
567 | "id" => $i, | |
568 | "title" => $title, | |
569 | "unread" => $unread, | |
570 | "cat_id" => -1, | |
571 | ); | |
572 | array_push($feeds, $row); | |
573 | } | |
574 | ||
575 | } | |
576 | } | |
577 | ||
578 | /* Child cats */ | |
579 | ||
580 | if ($include_nested && $cat_id) { | |
3467e1fd | 581 | $sth = $pdo->prepare("SELECT |
c053b976 | 582 | id, title, order_id FROM ttrss_feed_categories |
3467e1fd AD |
583 | WHERE parent_cat = ? AND owner_uid = ? ORDER BY id, title"); |
584 | ||
585 | $sth->execute([$cat_id, $_SESSION['uid']]); | |
04f60eb7 | 586 | |
3467e1fd | 587 | while ($line = $sth->fetch()) { |
a42c55f0 | 588 | $unread = getFeedUnread($line["id"], true) + |
86a8351c | 589 | Feeds::getCategoryChildrenUnread($line["id"]); |
04f60eb7 AD |
590 | |
591 | if ($unread || !$unread_only) { | |
592 | $row = array( | |
1d3cbe31 | 593 | "id" => (int) $line["id"], |
04f60eb7 AD |
594 | "title" => $line["title"], |
595 | "unread" => $unread, | |
596 | "is_cat" => true, | |
c053b976 | 597 | "order_id" => (int) $line["order_id"] |
04f60eb7 AD |
598 | ); |
599 | array_push($feeds, $row); | |
600 | } | |
601 | } | |
602 | } | |
603 | ||
604 | /* Real feeds */ | |
605 | ||
606 | if ($limit) { | |
607 | $limit_qpart = "LIMIT $limit OFFSET $offset"; | |
608 | } else { | |
609 | $limit_qpart = ""; | |
610 | } | |
611 | ||
612 | if ($cat_id == -4 || $cat_id == -3) { | |
3467e1fd | 613 | $sth = $pdo->prepare("SELECT |
04f60eb7 AD |
614 | id, feed_url, cat_id, title, order_id, ". |
615 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
3467e1fd AD |
616 | FROM ttrss_feeds WHERE owner_uid = ? |
617 | ORDER BY cat_id, title " . $limit_qpart); | |
618 | $sth->execute([$_SESSION['uid']]); | |
04f60eb7 | 619 | |
3467e1fd | 620 | } else { |
04f60eb7 | 621 | |
3467e1fd | 622 | $sth = $pdo->prepare("SELECT |
04f60eb7 AD |
623 | id, feed_url, cat_id, title, order_id, ". |
624 | SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated | |
625 | FROM ttrss_feeds WHERE | |
3467e1fd AD |
626 | (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) |
627 | AND owner_uid = :uid | |
628 | ORDER BY cat_id, title " . $limit_qpart); | |
629 | $sth->execute([":uid" => $_SESSION['uid'], ":cat" => $cat_id]); | |
04f60eb7 AD |
630 | } |
631 | ||
3467e1fd | 632 | while ($line = $sth->fetch()) { |
04f60eb7 | 633 | |
a42c55f0 | 634 | $unread = getFeedUnread($line["id"]); |
04f60eb7 | 635 | |
fa3bcfa3 | 636 | $has_icon = Feeds::feedHasIcon($line['id']); |
04f60eb7 AD |
637 | |
638 | if ($unread || !$unread_only) { | |
639 | ||
640 | $row = array( | |
641 | "feed_url" => $line["feed_url"], | |
642 | "title" => $line["title"], | |
643 | "id" => (int)$line["id"], | |
644 | "unread" => (int)$unread, | |
645 | "has_icon" => $has_icon, | |
646 | "cat_id" => (int)$line["cat_id"], | |
ef3da31c | 647 | "last_updated" => (int) strtotime($line["last_updated"]), |
04f60eb7 AD |
648 | "order_id" => (int) $line["order_id"], |
649 | ); | |
650 | ||
651 | array_push($feeds, $row); | |
652 | } | |
653 | } | |
654 | ||
655 | return $feeds; | |
656 | } | |
657 | ||
7b55001e AD |
658 | /** |
659 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
660 | */ | |
a42c55f0 | 661 | static function api_get_headlines($feed_id, $limit, $offset, |
04f60eb7 AD |
662 | $filter, $is_cat, $show_excerpt, $show_content, $view_mode, $order, |
663 | $include_attachments, $since_id, | |
5c784e70 | 664 | $search = "", $include_nested = false, $sanitize_content = true, |
19e47ad6 | 665 | $force_update = false, $excerpt_length = 100, $check_first_id = false, $skip_first_id_check = false) { |
4baa1afa | 666 | |
3467e1fd AD |
667 | $pdo = Db::pdo(); |
668 | ||
4baa1afa AD |
669 | if ($force_update && $feed_id > 0 && is_numeric($feed_id)) { |
670 | // Update the feed if required with some basic flood control | |
671 | ||
3467e1fd | 672 | $sth = $pdo->prepare( |
4baa1afa | 673 | "SELECT cache_images,".SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated |
3467e1fd AD |
674 | FROM ttrss_feeds WHERE id = ?"); |
675 | $sth->execute([$feed_id]); | |
4baa1afa | 676 | |
3467e1fd AD |
677 | if ($row = $sth->fetch()) { |
678 | $last_updated = strtotime($row["last_updated"]); | |
4dc3f7e7 | 679 | $cache_images = API::param_to_bool($row["cache_images"]); |
4baa1afa AD |
680 | |
681 | if (!$cache_images && time() - $last_updated > 120) { | |
e6c886bf | 682 | RSSUtils::update_rss_feed($feed_id, true); |
4baa1afa | 683 | } else { |
3467e1fd AD |
684 | $sth = $pdo->prepare("UPDATE ttrss_feeds SET last_updated = '1970-01-01', last_update_started = '1970-01-01' |
685 | WHERE id = ?"); | |
686 | $sth->execute([$feed_id]); | |
4baa1afa AD |
687 | } |
688 | } | |
689 | } | |
04f60eb7 | 690 | |
f5a0fb8b AD |
691 | $params = array( |
692 | "feed" => $feed_id, | |
693 | "limit" => $limit, | |
694 | "view_mode" => $view_mode, | |
695 | "cat_view" => $is_cat, | |
696 | "search" => $search, | |
697 | "override_order" => $order, | |
698 | "offset" => $offset, | |
699 | "since_id" => $since_id, | |
700 | "include_children" => $include_nested, | |
19e47ad6 AD |
701 | "check_first_id" => $check_first_id, |
702 | "skip_first_id_check" => $skip_first_id_check | |
f5a0fb8b AD |
703 | ); |
704 | ||
aeb1abed | 705 | $qfh_ret = Feeds::queryFeedHeadlines($params); |
f5a0fb8b | 706 | |
34440201 | 707 | $result = $qfh_ret[0]; |
04f60eb7 | 708 | $feed_title = $qfh_ret[1]; |
48fefe2f | 709 | $first_id = $qfh_ret[6]; |
04f60eb7 AD |
710 | |
711 | $headlines = array(); | |
b0ce3d33 AD |
712 | |
713 | $headlines_header = array( | |
714 | 'id' => $feed_id, | |
48fefe2f | 715 | 'first_id' => $first_id, |
b0ce3d33 | 716 | 'is_cat' => $is_cat); |
4f2a2ca9 | 717 | |
ec57104d | 718 | if (!is_numeric($result)) { |
3467e1fd | 719 | while ($line = $result->fetch()) { |
34440201 AD |
720 | $line["content_preview"] = truncate_string(strip_tags($line["content"]), $excerpt_length); |
721 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) { | |
722 | $line = $p->hook_query_headlines($line, $excerpt_length, true); | |
723 | } | |
04f60eb7 | 724 | |
34440201 AD |
725 | $is_updated = ($line["last_read"] == "" && |
726 | ($line["unread"] != "t" && $line["unread"] != "1")); | |
f71a669b | 727 | |
34440201 | 728 | $tags = explode(",", $line["tag_cache"]); |
f71a669b | 729 | |
34440201 AD |
730 | $label_cache = $line["label_cache"]; |
731 | $labels = false; | |
f71a669b AD |
732 | |
733 | if ($label_cache) { | |
34440201 AD |
734 | $label_cache = json_decode($label_cache, true); |
735 | ||
736 | if ($label_cache) { | |
737 | if ($label_cache["no-labels"] == 1) | |
738 | $labels = array(); | |
739 | else | |
740 | $labels = $label_cache; | |
741 | } | |
f71a669b | 742 | } |
f71a669b | 743 | |
4a0da0e5 | 744 | if (!is_array($labels)) $labels = Article::get_article_labels($line["id"]); |
04f60eb7 | 745 | |
34440201 | 746 | $headline_row = array( |
04f60eb7 | 747 | "id" => (int)$line["id"], |
18186149 | 748 | "guid" => $line["guid"], |
4dc3f7e7 AD |
749 | "unread" => API::param_to_bool($line["unread"]), |
750 | "marked" => API::param_to_bool($line["marked"]), | |
751 | "published" => API::param_to_bool($line["published"]), | |
34440201 | 752 | "updated" => (int)strtotime($line["updated"]), |
04f60eb7 AD |
753 | "is_updated" => $is_updated, |
754 | "title" => $line["title"], | |
755 | "link" => $line["link"], | |
756 | "feed_id" => $line["feed_id"], | |
757 | "tags" => $tags, | |
758 | ); | |
759 | ||
34440201 | 760 | if ($include_attachments) |
7e5f8d9f | 761 | $headline_row['attachments'] = Article::get_article_enclosures( |
34440201 | 762 | $line['id']); |
04f60eb7 | 763 | |
34440201 AD |
764 | if ($show_excerpt) |
765 | $headline_row["excerpt"] = $line["content_preview"]; | |
04f60eb7 | 766 | |
34440201 | 767 | if ($show_content) { |
04f60eb7 | 768 | |
34440201 AD |
769 | if ($sanitize_content) { |
770 | $headline_row["content"] = sanitize( | |
771 | $line["content"], | |
4dc3f7e7 | 772 | API::param_to_bool($line['hide_images']), |
34440201 AD |
773 | false, $line["site_url"], false, $line["id"]); |
774 | } else { | |
775 | $headline_row["content"] = $line["content"]; | |
776 | } | |
04f60eb7 | 777 | } |
04f60eb7 | 778 | |
34440201 AD |
779 | // unify label output to ease parsing |
780 | if ($labels["no-labels"] == 1) $labels = array(); | |
04f60eb7 | 781 | |
34440201 | 782 | $headline_row["labels"] = $labels; |
04f60eb7 | 783 | |
34440201 AD |
784 | $headline_row["feed_title"] = $line["feed_title"] ? $line["feed_title"] : |
785 | $feed_title; | |
04f60eb7 | 786 | |
34440201 AD |
787 | $headline_row["comments_count"] = (int)$line["num_comments"]; |
788 | $headline_row["comments_link"] = $line["comments"]; | |
04f60eb7 | 789 | |
4dc3f7e7 | 790 | $headline_row["always_display_attachments"] = API::param_to_bool($line["always_display_enclosures"]); |
04f60eb7 | 791 | |
34440201 | 792 | $headline_row["author"] = $line["author"]; |
4f2a2ca9 | 793 | |
34440201 AD |
794 | $headline_row["score"] = (int)$line["score"]; |
795 | $headline_row["note"] = $line["note"]; | |
796 | $headline_row["lang"] = $line["lang"]; | |
583dbc56 | 797 | |
34440201 AD |
798 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_API) as $p) { |
799 | $headline_row = $p->hook_render_article_api(array("headline" => $headline_row)); | |
800 | } | |
b6604c96 | 801 | |
34440201 AD |
802 | array_push($headlines, $headline_row); |
803 | } | |
5c784e70 | 804 | } else if (is_numeric($result) && $result == -1) { |
48fefe2f | 805 | $headlines_header['first_id_changed'] = true; |
04f60eb7 AD |
806 | } |
807 | ||
5c784e70 | 808 | return array($headlines, $headlines_header); |
04f60eb7 AD |
809 | } |
810 | ||
efc6553d | 811 | function unsubscribeFeed() { |
e6532439 | 812 | $feed_id = (int) clean($_REQUEST["feed_id"]); |
efc6553d | 813 | |
3467e1fd AD |
814 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE |
815 | id = ? AND owner_uid = ?"); | |
816 | $sth->execute([$feed_id, $_SESSION['uid']]); | |
efc6553d | 817 | |
3467e1fd | 818 | if ($row = $sth->fetch()) { |
a42c55f0 | 819 | Pref_Feeds::remove_feed($feed_id, $_SESSION["uid"]); |
6f7798b6 | 820 | $this->wrap(self::STATUS_OK, array("status" => "OK")); |
efc6553d | 821 | } else { |
6f7798b6 | 822 | $this->wrap(self::STATUS_ERR, array("error" => "FEED_NOT_FOUND")); |
efc6553d AD |
823 | } |
824 | } | |
825 | ||
826 | function subscribeToFeed() { | |
e6532439 AD |
827 | $feed_url = clean($_REQUEST["feed_url"]); |
828 | $category_id = (int) clean($_REQUEST["category_id"]); | |
829 | $login = clean($_REQUEST["login"]); | |
830 | $password = clean($_REQUEST["password"]); | |
efc6553d AD |
831 | |
832 | if ($feed_url) { | |
86a8351c | 833 | $rc = Feeds::subscribe_to_feed($feed_url, $category_id, $login, $password); |
efc6553d | 834 | |
6f7798b6 | 835 | $this->wrap(self::STATUS_OK, array("status" => $rc)); |
efc6553d | 836 | } else { |
6f7798b6 | 837 | $this->wrap(self::STATUS_ERR, array("error" => 'INCORRECT_USAGE')); |
efc6553d AD |
838 | } |
839 | } | |
840 | ||
0bb5833b | 841 | function getFeedTree() { |
e6532439 | 842 | $include_empty = API::param_to_bool(clean($_REQUEST['include_empty'])); |
66ca7c30 | 843 | |
a42c55f0 | 844 | $pf = new Pref_Feeds($_REQUEST); |
0bb5833b AD |
845 | |
846 | $_REQUEST['mode'] = 2; | |
66ca7c30 | 847 | $_REQUEST['force_show_empty'] = $include_empty; |
0bb5833b AD |
848 | |
849 | if ($pf){ | |
850 | $data = $pf->makefeedtree(); | |
6f7798b6 | 851 | $this->wrap(self::STATUS_OK, array("categories" => $data)); |
0bb5833b | 852 | } else { |
6f7798b6 | 853 | $this->wrap(self::STATUS_ERR, array("error" => |
0bb5833b AD |
854 | 'UNABLE_TO_INSTANTIATE_OBJECT')); |
855 | } | |
856 | ||
857 | } | |
c0a08063 | 858 | |
dc5a8a21 | 859 | // only works for labels or uncategorized for the time being |
c0a08063 AD |
860 | private function isCategoryEmpty($id) { |
861 | ||
862 | if ($id == -2) { | |
3467e1fd AD |
863 | $sth = $this->pdo->prepare("SELECT COUNT(id) AS count FROM ttrss_labels2 |
864 | WHERE owner_uid = ?"); | |
865 | $sth->execute([$_SESSION['uid']]); | |
866 | $row = $sth->fetch(); | |
c0a08063 | 867 | |
3467e1fd | 868 | return $row["count"] == 0; |
c0a08063 | 869 | |
dc5a8a21 | 870 | } else if ($id == 0) { |
3467e1fd AD |
871 | $sth = $this->pdo->prepare("SELECT COUNT(id) AS count FROM ttrss_feeds |
872 | WHERE cat_id IS NULL AND owner_uid = ?"); | |
873 | $sth->execute([$_SESSION['uid']]); | |
874 | $row = $sth->fetch(); | |
dc5a8a21 | 875 | |
3467e1fd | 876 | return $row["count"] == 0; |
dc5a8a21 | 877 | |
c0a08063 AD |
878 | } |
879 | ||
880 | return false; | |
881 | } | |
882 | ||
883 | ||
891df346 | 884 | } |