Example #1
0
/**
 * Show the 'Daily' page.
 *
 * @param PageBuilder $pageBuilder Template engine wrapper.
 */
function showDaily($pageBuilder)
{
    $LINKSDB = new LinkDB($GLOBALS['config']['DATASTORE'], isLoggedIn(), $GLOBALS['config']['HIDE_PUBLIC_LINKS'], $GLOBALS['redirector']);
    $day = Date('Ymd', strtotime('-1 day'));
    // Yesterday, in format YYYYMMDD.
    if (isset($_GET['day'])) {
        $day = $_GET['day'];
    }
    $days = $LINKSDB->days();
    $i = array_search($day, $days);
    if ($i === false) {
        $i = count($days) - 1;
        $day = $days[$i];
    }
    $previousday = '';
    $nextday = '';
    if ($i !== false) {
        if ($i >= 1) {
            $previousday = $days[$i - 1];
        }
        if ($i < count($days) - 1) {
            $nextday = $days[$i + 1];
        }
    }
    try {
        $linksToDisplay = $LINKSDB->filter(LinkFilter::$FILTER_DAY, $day);
    } catch (Exception $exc) {
        error_log($exc);
        $linksToDisplay = array();
    }
    // We pre-format some fields for proper output.
    foreach ($linksToDisplay as $key => $link) {
        $taglist = explode(' ', $link['tags']);
        uasort($taglist, 'strcasecmp');
        $linksToDisplay[$key]['taglist'] = $taglist;
        $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']);
        $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']);
        $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']);
    }
    /* We need to spread the articles on 3 columns.
          I did not want to use a JavaScript lib like http://masonry.desandro.com/
          so I manually spread entries with a simple method: I roughly evaluate the
          height of a div according to title and description length.
       */
    $columns = array(array(), array(), array());
    // Entries to display, for each column.
    $fill = array(0, 0, 0);
    // Rough estimate of columns fill.
    foreach ($linksToDisplay as $key => $link) {
        // Roughly estimate length of entry (by counting characters)
        // Title: 30 chars = 1 line. 1 line is 30 pixels height.
        // Description: 836 characters gives roughly 342 pixel height.
        // This is not perfect, but it's usually OK.
        $length = strlen($link['title']) + 342 * strlen($link['description']) / 836;
        if ($link['thumbnail']) {
            $length += 100;
        }
        // 1 thumbnails roughly takes 100 pixels height.
        // Then put in column which is the less filled:
        $smallest = min($fill);
        // find smallest value in array.
        $index = array_search($smallest, $fill);
        // find index of this smallest value.
        array_push($columns[$index], $link);
        // Put entry in this column.
        $fill[$index] += $length;
    }
    $data = array('linksToDisplay' => $linksToDisplay, 'linkcount' => count($LINKSDB), 'cols' => $columns, 'day' => linkdate2timestamp($day . '_000000'), 'previousday' => $previousday, 'nextday' => $nextday);
    $pluginManager = PluginManager::getInstance();
    $pluginManager->executeHooks('render_daily', $data, array('loggedin' => isLoggedIn()));
    foreach ($data as $key => $value) {
        $pageBuilder->assign($key, $value);
    }
    $pageBuilder->renderPage('daily');
    exit;
}
Example #2
0
    }
    exit;
}
$pb = new PageBuilder('FeedPage');
FeedPage::$pb = $pb;
$pb->assign('base', MyTool::getUrl());
$pb->assign('version', FEED_VERSION);
$pb->assign('pagetitle', 'KrISS feed');
$pb->assign('referer', $referer);
$pb->assign('langs', Intl::$langList);
$pb->assign('lang', Intl::$langList[Intl::$lang]);
$pb->assign('query_string', $_SERVER['QUERY_STRING']);
if (!is_dir(DATA_DIR)) {
    if (!@mkdir(DATA_DIR, 0755)) {
        $pb->assign('message', sprintf(Intl::msg('Can not create %s directory, check permissions'), DATA_DIR));
        $pb->renderPage('message');
    }
    @chmod(DATA_DIR, 0755);
    if (!is_file(DATA_DIR . '/.htaccess')) {
        if (!@file_put_contents(DATA_DIR . '/.htaccess', "Allow from none\nDeny from all\n")) {
            $pb->assign('message', sprintf(Intl::msg('Can not protect %s directory with .htaccess, check permissions'), DATA_DIR));
            $pb->renderPage('message');
        }
    }
}
// XSRF protection with token
if (!empty($_POST)) {
    if (!Session::isToken($_POST['token'])) {
        $pb->assign('message', Intl::msg('Wrong token'));
        $pb->renderPage('message');
    }
Example #3
0
function install()
{
    // On free.fr host, make sure the /sessions directory exists, otherwise login will not work.
    if (endsWith($_SERVER['HTTP_HOST'], '.free.fr') && !is_dir($_SERVER['DOCUMENT_ROOT'] . '/sessions')) {
        mkdir($_SERVER['DOCUMENT_ROOT'] . '/sessions', 0705);
    }
    // This part makes sure sessions works correctly.
    // (Because on some hosts, session.save_path may not be set correctly,
    // or we may not have write access to it.)
    if (isset($_GET['test_session']) && (!isset($_SESSION) || !isset($_SESSION['session_tested']) || $_SESSION['session_tested'] != 'Working')) {
        // Step 2: Check if data in session is correct.
        echo '<pre>Sessions do not seem to work correctly on your server.<br>';
        echo 'Make sure the variable session.save_path is set correctly in your php config, and that you have write access to it.<br>';
        echo 'It currently points to ' . session_save_path() . '<br>';
        echo 'Check that the hostname used to access Shaarli contains a dot. On some browsers, accessing your server via a hostname like \'localhost\' or any custom hostname without a dot causes cookie storage to fail. We recommend accessing your server via it\'s IP address or Fully Qualified Domain Name.<br>';
        echo '<br><a href="?">Click to try again.</a></pre>';
        die;
    }
    if (!isset($_SESSION['session_tested'])) {
        // Step 1 : Try to store data in session and reload page.
        $_SESSION['session_tested'] = 'Working';
        // Try to set a variable in session.
        header('Location: ' . index_url($_SERVER) . '?test_session');
        // Redirect to check stored data.
    }
    if (isset($_GET['test_session'])) {
        // Step 3: Sessions are OK. Remove test parameter from URL.
        header('Location: ' . index_url($_SERVER));
    }
    if (!empty($_POST['setlogin']) && !empty($_POST['setpassword'])) {
        $tz = 'UTC';
        if (!empty($_POST['continent']) && !empty($_POST['city']) && isTimeZoneValid($_POST['continent'], $_POST['city'])) {
            $tz = $_POST['continent'] . '/' . $_POST['city'];
        }
        $GLOBALS['timezone'] = $tz;
        // Everything is ok, let's create config file.
        $GLOBALS['login'] = $_POST['setlogin'];
        $GLOBALS['salt'] = sha1(uniqid('', true) . '_' . mt_rand());
        // Salt renders rainbow-tables attacks useless.
        $GLOBALS['hash'] = sha1($_POST['setpassword'] . $GLOBALS['login'] . $GLOBALS['salt']);
        $GLOBALS['title'] = empty($_POST['title']) ? 'Shared links on ' . escape(index_url($_SERVER)) : $_POST['title'];
        $GLOBALS['config']['ENABLE_UPDATECHECK'] = !empty($_POST['updateCheck']);
        try {
            writeConfig($GLOBALS, isLoggedIn());
        } catch (Exception $e) {
            error_log('ERROR while writing config file after installation.' . PHP_EOL . $e->getMessage());
            // TODO: do not handle exceptions/errors in JS.
            echo '<script>alert("' . $e->getMessage() . '");document.location=\'?\';</script>';
            exit;
        }
        echo '<script>alert("Shaarli is now configured. Please enter your login/password and start shaaring your links!");document.location=\'?do=login\';</script>';
        exit;
    }
    // Display config form:
    list($timezone_form, $timezone_js) = generateTimeZoneForm();
    $timezone_html = '';
    if ($timezone_form != '') {
        $timezone_html = '<tr><td><b>Timezone:</b></td><td>' . $timezone_form . '</td></tr>';
    }
    $PAGE = new PageBuilder();
    $PAGE->assign('timezone_html', $timezone_html);
    $PAGE->assign('timezone_js', $timezone_js);
    $PAGE->renderPage('install');
    exit;
}
Example #4
0
}());
<?php 
            }
        }
    }
    exit;
}
$pb = new PageBuilder('FeedPage');
$pb->assign('base', MyTool::getUrl());
$pb->assign('version', FEED_VERSION);
$pb->assign('pagetitle', 'KrISS feed');
$pb->assign('referer', $referer);
if (!is_dir(DATA_DIR)) {
    if (!@mkdir(DATA_DIR, 0755)) {
        $pb->assign('message', sprintf(Intl::msg('Can not create %s directory, check permissions'), DATA_DIR));
        $pb->renderPage('message');
    }
    @chmod(DATA_DIR, 0755);
    if (!is_file(DATA_DIR . '/.htaccess')) {
        if (!@file_put_contents(DATA_DIR . '/.htaccess', "Allow from none\nDeny from all\n")) {
            $pb->assign('message', sprintf(Intl::msg('Can not protect %s directory with .htaccess, check permissions'), DATA_DIR));
            $pb->renderPage('message');
        }
    }
}
// XSRF protection with token
if (!empty($_POST)) {
    if (!Session::isToken($_POST['token'])) {
        $pb->assign('message', Intl::msg('Wrong token'));
        $pb->renderPage('message');
    }