<commit_before><?php

Namespace Bolt\Controllers;

use Silex;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Async implements ControllerProviderInterface
{
    public function connect(Silex\Application $app)
    {
        $ctr = $app['controllers_factory'];

        $ctr->get("/dashboardnews", array($this, 'dashboardnews'))
            ->before(array($this, 'before'))
            ->bind('dashboardnews')
        ;

        $ctr->get("/latestactivity", array($this, 'latestactivity'))
            ->before(array($this, 'before'))
            ->bind('latestactivity')
        ;

        $ctr->get("/filesautocomplete", array($this, 'filesautocomplete'))
            ->before(array($this, 'before'))
        ;

        $ctr->get("/readme/{extension}", array($this, 'readme'))
            ->before(array($this, 'before'))
            ->bind('readme')
        ;

        $ctr->get("/widget/{key}", array($this, 'widget'))
            ->before(array($this, 'before'))
            ->bind('widget')
        ;

        $ctr->post("/markdownify", array($this, 'markdownify'))
            ->before(array($this, 'before'))
            ->bind('markdownify')
        ;

        $ctr->get("/makeuri", array($this, 'makeuri'))
            ->before(array($this, 'before'))
        ;

        $ctr->get("/lastmodified/{contenttypeslug}", array($this, 'lastmodified'))
            ->before(array($this, 'before'))
            ->bind('lastmodified')
        ;

        $ctr->get("/filebrowser/{contenttype}", array($this, 'filebrowser'))
            ->before(array($this, 'before'))
            ->assert('contenttype', '.*')
            ->bind('contenttype')
        ;


        $ctr->get("/browse/{path}", array($this, 'browse'))
            ->before(array($this, 'before'))
            ->assert('path', '.+')
            ->bind('asyncbrowse')
        ;



        return $ctr;

    }
    /**
     * News.
     */
    function dashboardnews(Silex\Application $app) {

        $news = $app['cache']->fetch('dashboardnews'); // Two hours.

        $name = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];

        // If not cached, get fresh news..
        if ($news == false) {

            $app['log']->add("News: fetch from remote server..", 1);

            $driver = !empty($app['config']['general']['database']['driver']) ? $app['config']['general']['database']['driver'] : 'sqlite';

            $url = sprintf('http://news.bolt.cm/?v=%s&p=%s&db=%s&name=%s',
                rawurlencode($app->getVersion()),
                phpversion(),
                $driver,
                base64_encode($name)
            );

            $curlOptions = array('CURLOPT_CONNECTTIMEOUT' => 5);
            // If there's a proxy ...
            if (!empty($app['config']['general']['httpProxy'])) {
                $curlOptions['CURLOPT_PROXY'] = $app['config']['general']['httpProxy']['host'];
                $curlOptions['CURLOPT_PROXYTYPE'] = 'CURLPROXY_HTTP';
                $curlOptions['CURLOPT_PROXYUSERPWD'] = $app['config']['general']['httpProxy']['user'] . ':' . $app['config']['general']['httpProxy']['password'];
            }
            $guzzleclient = new \Guzzle\Http\Client($url, array('curl.options' => $curlOptions));

            $news = $guzzleclient->get("/")->send()->getBody(true);
            $news = json_decode($news);

            // For now, just use the most current item.
            $news = current($news);

            $app['cache']->save('dashboardnews', $news, 7200);

        } else {
            $app['log']->add("News: get from cache..", 1);
        }

        $body = $app['twig']->render('dashboard-news.twig', array('news' => $news ));

        return new Response($body, 200, array('Cache-Control' => 's-maxage=3600, public'));

    }

    /**
     * Get the 'latest activity' for the dashboard..
     */
    function latestactivity(Silex\Application $app) {

        $activity = $app['log']->getActivity(8, 3);

        $body = $app['twig']->render('dashboard-activity.twig', array('activity' => $activity));

        return new Response($body, 200, array('Cache-Control' => 's-maxage=3600, public'));

    }

    function filesautocomplete(Silex\Application $app, Request $request) {
<commit_msg>Fix when news.bolt.cm is not reachable or gives invalid JSON Fixes #409<commit_after><?php

Namespace Bolt\Controllers;

use Guzzle\Http\Exception\RequestException;
use Silex;
use Silex\ControllerProviderInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Async implements ControllerProviderInterface
{
    public function connect(Silex\Application $app)
    {
        $ctr = $app['controllers_factory'];

        $ctr->get("/dashboardnews", array($this, 'dashboardnews'))
            ->before(array($this, 'before'))
            ->bind('dashboardnews')
        ;

        $ctr->get("/latestactivity", array($this, 'latestactivity'))
            ->before(array($this, 'before'))
            ->bind('latestactivity')
        ;

        $ctr->get("/filesautocomplete", array($this, 'filesautocomplete'))
            ->before(array($this, 'before'))
        ;

        $ctr->get("/readme/{extension}", array($this, 'readme'))
            ->before(array($this, 'before'))
            ->bind('readme')
        ;

        $ctr->get("/widget/{key}", array($this, 'widget'))
            ->before(array($this, 'before'))
            ->bind('widget')
        ;

        $ctr->post("/markdownify", array($this, 'markdownify'))
            ->before(array($this, 'before'))
            ->bind('markdownify')
        ;

        $ctr->get("/makeuri", array($this, 'makeuri'))
            ->before(array($this, 'before'))
        ;

        $ctr->get("/lastmodified/{contenttypeslug}", array($this, 'lastmodified'))
            ->before(array($this, 'before'))
            ->bind('lastmodified')
        ;

        $ctr->get("/filebrowser/{contenttype}", array($this, 'filebrowser'))
            ->before(array($this, 'before'))
            ->assert('contenttype', '.*')
            ->bind('contenttype')
        ;


        $ctr->get("/browse/{path}", array($this, 'browse'))
            ->before(array($this, 'before'))
            ->assert('path', '.+')
            ->bind('asyncbrowse')
        ;



        return $ctr;

    }
    /**
     * News.
     */
    function dashboardnews(Silex\Application $app) {

        $news = $app['cache']->fetch('dashboardnews'); // Two hours.

        $name = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];

        // If not cached, get fresh news..
        if ($news == false) {

            $app['log']->add("News: fetch from remote server..", 1);

            $driver = !empty($app['config']['general']['database']['driver']) ? $app['config']['general']['database']['driver'] : 'sqlite';

            $url = sprintf('http://news.bolt.cm/?v=%s&p=%s&db=%s&name=%s',
                rawurlencode($app->getVersion()),
                phpversion(),
                $driver,
                base64_encode($name)
            );

            $curlOptions = array('CURLOPT_CONNECTTIMEOUT' => 5);
            // If there's a proxy ...
            if (!empty($app['config']['general']['httpProxy'])) {
                $curlOptions['CURLOPT_PROXY'] = $app['config']['general']['httpProxy']['host'];
                $curlOptions['CURLOPT_PROXYTYPE'] = 'CURLPROXY_HTTP';
                $curlOptions['CURLOPT_PROXYUSERPWD'] = $app['config']['general']['httpProxy']['user'] . ':' . $app['config']['general']['httpProxy']['password'];
            }
            $guzzleclient = new \Guzzle\Http\Client($url, array('curl.options' => $curlOptions));

            try {
                $newsData = $guzzleclient->get("/")->send()->getBody(true);
                $news = json_decode($newsData);
                if ($news) {
                    // For now, just use the most current item.
                    $news = current($news);

                    $app['cache']->save('dashboardnews', $news, 7200);
                } else {
                    $app['log']->add("News: got invalid JSON feed", 1);
                }

            } catch(RequestException $re) {
                $app['log']->add("News: got exception: ".$re->getMessage(), 1);
            }

        } else {
            $app['log']->add("News: get from cache..", 1);
        }

        $body = $app['twig']->render('dashboard-news.twig', array('news' => $news ));

        return new Response($body, 200, array('Cache-Control' => 's-maxage=3600, public'));

    }

    /**
     * Get the 'latest activity' for the dashboard..
     */
    function latestactivity(Silex\Application $app) {

        $activity = $app['log']->getActivity(8, 3);

        $body = $app['twig']->render('dashboard-activity.twig', array('activity' => $activity));

        return new Response($body, 200, array('Cache-Control' => 's-maxage=3600, public'));

    }

    function filesautocomplete(Silex\Application $app, Request $request) {

{'fraction_non_alphanumeric': 0.19605695509309967, 'fraction_numerical': 0.006024096385542169, 'mean_word_length': 2.3011569052783805, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7371400', 'n_tokens_mistral': 2722, 'n_tokens_neox': 2594, 'n_words': 438}