-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrefresh.php
70 lines (61 loc) · 2.42 KB
/
refresh.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
this script is called from `index.php` via curl
and it's an hack to do async operation in PHP
usually you can do the following....
ignore_user_abort(TRUE);
set_time_limit(0);
header("Status: 200\r\n", true);
header("Content-Length: 0\r\n", true);
header("Connection: Close\r\n", true);
ob_end_flush();
flush();
to close connection and proceed with your long operation
but using mod_fcgid this is not possibile and you need to use
register_shutdown_function (called also when client disconnect)
and disconnect the client using a small timeout (see index.php)
**/
function run_me_on_disconnect() {
// needed because register_shutdown_function changes
// current working directory to ServerRoot
chdir(__DIR__);
$conf = include('conf.php');
include_once('SimplePie.compiled.php');
// list of feeds to merge...
$feeds = file('./feeds');
$rss = '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel>';
$rss .= "<title>{$conf['title']}</title>";
$rss .= "<link>{$conf['link']}</link>";
$rss .= "<description>{$conf['description']}</description>";
$rss .= "<language>{$conf['lang']}</language>";
$feed = new SimplePie();
$feed->set_feed_url($feeds);
$feed->set_cache_duration($conf['cache_duration']);
$feed->init();
if ($feed->error()) {
file_put_contents('error.log', $feed->error());
echo $feed->error();
}
$items = [];
foreach($feed->get_items(0, $conf['itemlimit']) as $item) {
$itemObj = [ "title" => $item->get_title(), "date" => $item->get_date("d M y"),
"feed_title" => $item->get_feed()->get_title(), "link" => $item->get_permalink(),
"guid" => $item->get_id()
];
array_push($items, $itemObj);
$rss .= '<item><pubDate>' . $item->get_date('r') . '</pubDate>';
$rss .= '<title>[' . $item->get_feed()->get_title() . '] ' . $item->get_title() . '</title>';
$rss .= '<link>' . $item->get_permalink() . '</link>';
$rss .= '<guid isPermaLink="false">' . $item->get_id() . '</guid>';
$rss .= '<description><![CDATA[' . $item->get_description() . ']]></description></item>';
}
$rss .= '</channel></rss>';
file_put_contents('feed.xml', $rss);
file_put_contents('feed.json', json_encode($items));
}
if(php_sapi_name() == "cli") {
run_me_on_disconnect();
} else {
register_shutdown_function('run_me_on_disconnect');
}
?>