aboutsummaryrefslogtreecommitdiffhomepage
path: root/cron.php
blob: 8fbf421aeb9b50672616d86df9d68d82b625f98e (plain) (blame)
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
<?php
error_reporting(E_ALL);
include_once 'inc/poche/global.inc.php';
include_once 'inc/poche/config.inc.php';

if (php_sapi_name() === 'cli') {
    $options_cli = getopt('', array(
        'limit::',
        'user-id::',
        'token::',
    ));
}
else {
    $options_cli = $_GET;
}

$limit = ! empty($options_cli['limit']) && ctype_digit($options_cli['limit']) ? (int) $options_cli['limit'] : 10;
$user_id = ! empty($options_cli['user-id']) && ctype_digit($options_cli['user-id']) ? (int) $options_cli['user-id'] : null;
$token = ! empty($options_cli['token']) ? $options_cli['token'] : null;

if (is_null($user_id)) {
    die('You must give a user id');
}

if (is_null($token)) {
    die('You must give a token');
}

$store = new Database();
$config = $store->getConfigUser($user_id);

if ($token != $config['token']) {
    die(_('Uh, there is a problem with the cron.'));
}

$items = $store->retrieveUnfetchedEntries($user_id, $limit);

foreach ($items as $item) {
    $url = new Url(base64_encode($item['url']));
    $content = Tools::getPageContent($url);

    $title = ($content['rss']['channel']['item']['title'] != '') ? $content['rss']['channel']['item']['title'] : _('Untitled');
    $body = $content['rss']['channel']['item']['description'];

    // // clean content from prevent xss attack
    $config = HTMLPurifier_Config::createDefault();
    $purifier = new HTMLPurifier($config);
    $title = $purifier->purify($title);
    $body = $purifier->purify($body);


    $store->updateContentAndTitle($item['id'], $title, $body, $user_id);
}