<commit_before><?php

namespace Pocs\Controller;

use Pocs\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

use Pocs\Entity\Frontend;

class Api extends Controller
{
    /**
     * Init API controller.
     */
    public function initialize()
    {
        $this->app->before(array($this, 'before'));
        $this->app->post('/api/comments', array($this, 'add'));
        $this->app->get('/api/comments', array($this, 'get'));
        $this->app->match('/api/comments', array($this, 'options'))
          ->method('OPTIONS');
    }

    /**
     * Make sure to prepare the json when recieving one.
     */
    public function before(Request $request) {
      if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
            $data = json_decode($request->getContent(), true);
            $request->request->replace(is_array($data) ? $data : array());
        }
    }

    /**
     * Handle the POST Request.
     *
     * @param Symfony\Component\HttpFoundation\Request
     *   The request.
     *
     * @return Symfony\Component\HttpFoundation\JsonResponse
     *   A Jsonresponse.
     */
    public function add(Request $request)
    {
        if (!$this->isValidCommentRequest($request)) {
             return $this->returnErrorResponse("Invalid arguments.");
        }
        $frontend = $this->getFrontendByApikey($request->get('key'));
        if (!$frontend) {
            return $this->returnErrorResponse('The Api Key isn\'t valid');
        }
        $url = $request->get('href');
        $position = strpos($url, $frontend['base_url']);
        $path = substr($url, $position + strlen($frontend['base_url']));
        if (FALSE === $position) {
            return $this->returnErrorResponse(
                'The Api Key doesn\'t match the referer url'
            );
        }
        if (preg_match('#\#/$#', $path)) {
            $path = substr($path, 0, -2);
        }
        if (!$path) {
            $path = '/';
        }

        $stmt = $this->app['db']->executeQuery(
            'SELECT id FROM urls WHERE frontend_id = ? AND url = ?',
            array($frontend['id'], $path)
        );

        if (!$url = $stmt->fetch()) {
            $this->app['db']->insert('urls', array(
                'frontend_id' => $frontend['id'],
                'url' => $path,
            ));
            $urlId = $this->app['db']->lastInsertId();
        } else {
            $urlId = $url['id'];
        }

        $comment = $request->get('newComment');
        $this->app['db']->insert('comments', array(
            'comment' => $comment['com'],
            'user_name' => $comment['name'],
            'user_email' => $comment['email'],
            'url_id' => $urlId,
            'date'=> date('c'),
        ));

        $commentId = $this->app['db']->lastInsertId();

        $response = new JsonResponse();
        $response->headers->set('Access-Control-Allow-Origin', '*');
        $response->setData(array(
            'status' => 'success', 'message' => 'Comment added successfully')
        );
        return $response;
    }

    /**
     * Handle the GET request.
     */
    public function get(Request $request) {
        $frontend = $this->getFrontendByApikey($request->query->get('key'));
        if (!$frontend) {
            return $this->returnErrorResponse('The Api Key isn\'t valid',
                $request->query->get('callback')
            );
        }
        if (!isset($_SERVER['HTTP_REFERER'])) {
            return $this->returnErrorResponse('The referer doesn\'t match',
                $request->query->get('callback')
            );
        }
        $url = $_SERVER['HTTP_REFERER'];
        $position = strpos($url, $frontend['base_url']);

        if ($position === false) {
            return $this->returnErrorResponse(
                'The Api Key doesn\'t match the referer url',
                $request->query->get('callback')
            );

        }
        $path = substr($url, $position + strlen($frontend['base_url']));
        if (!$path) {
            $path = '/';
        }

        $stmt = $this->app['db']->executeQuery(
            'SELECT id FROM urls WHERE frontend_id = ? AND url = ?',
            array($frontend['id'], $path)
        );

        $data = array(
            'status' => 'success',
            'title' => '',
            'comments' => array(),
        );
        $url = $stmt->fetch();
        if ($url) {
            $commentssql = $stmt = $this->app['db']->executeQuery(
                'SELECT id, user_name, comment, date, user_email
                 FROM comments
                 WHERE url_id = ?
                 ORDER BY id DESC',
                array($url['id'])
            );
            if ($comments = $commentssql->fetchAll()) {
                for ($i = 0, $nb = count($comments); $i < $nb; ++$i) {
                    $data['comments'][] = array(
                        'id' => $comments[$i]['id'],
                        'comment' => $comments[$i]['comment'],
                        'user' => $comments[$i]['user_name'],
                        'date' => $comments[$i]['date'],
                        'gravatar' => 'http://www.gravatar.com/avatar/'
                          . md5($comments[$i]['user_email']),
                    );
                }<commit_msg>Transform nl in br in get response<commit_after><?php

namespace Pocs\Controller;

use Pocs\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;

use Pocs\Entity\Frontend;

class Api extends Controller
{
    /**
     * Init API controller.
     */
    public function initialize()
    {
        $this->app->before(array($this, 'before'));
        $this->app->post('/api/comments', array($this, 'add'));
        $this->app->get('/api/comments', array($this, 'get'));
        $this->app->match('/api/comments', array($this, 'options'))
          ->method('OPTIONS');
    }

    /**
     * Make sure to prepare the json when recieving one.
     */
    public function before(Request $request) {
      if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
            $data = json_decode($request->getContent(), true);
            $request->request->replace(is_array($data) ? $data : array());
        }
    }

    /**
     * Handle the POST Request.
     *
     * @param Symfony\Component\HttpFoundation\Request
     *   The request.
     *
     * @return Symfony\Component\HttpFoundation\JsonResponse
     *   A Jsonresponse.
     */
    public function add(Request $request)
    {
        if (!$this->isValidCommentRequest($request)) {
             return $this->returnErrorResponse("Invalid arguments.");
        }
        $frontend = $this->getFrontendByApikey($request->get('key'));
        if (!$frontend) {
            return $this->returnErrorResponse('The Api Key isn\'t valid');
        }
        $url = $request->get('href');
        $position = strpos($url, $frontend['base_url']);
        $path = substr($url, $position + strlen($frontend['base_url']));
        if (FALSE === $position) {
            return $this->returnErrorResponse(
                'The Api Key doesn\'t match the referer url'
            );
        }
        if (preg_match('#\#/$#', $path)) {
            $path = substr($path, 0, -2);
        }
        if (!$path) {
            $path = '/';
        }

        $stmt = $this->app['db']->executeQuery(
            'SELECT id FROM urls WHERE frontend_id = ? AND url = ?',
            array($frontend['id'], $path)
        );

        if (!$url = $stmt->fetch()) {
            $this->app['db']->insert('urls', array(
                'frontend_id' => $frontend['id'],
                'url' => $path,
            ));
            $urlId = $this->app['db']->lastInsertId();
        } else {
            $urlId = $url['id'];
        }

        $comment = $request->get('newComment');
        $this->app['db']->insert('comments', array(
            'comment' => $comment['com'],
            'user_name' => $comment['name'],
            'user_email' => $comment['email'],
            'url_id' => $urlId,
            'date'=> date('c'),
        ));

        $commentId = $this->app['db']->lastInsertId();

        $response = new JsonResponse();
        $response->headers->set('Access-Control-Allow-Origin', '*');
        $response->setData(array(
            'status' => 'success', 'message' => 'Comment added successfully')
        );
        return $response;
    }

    /**
     * Handle the GET request.
     */
    public function get(Request $request) {
        $frontend = $this->getFrontendByApikey($request->query->get('key'));
        if (!$frontend) {
            return $this->returnErrorResponse('The Api Key isn\'t valid',
                $request->query->get('callback')
            );
        }
        if (!isset($_SERVER['HTTP_REFERER'])) {
            return $this->returnErrorResponse('The referer doesn\'t match',
                $request->query->get('callback')
            );
        }
        $url = $_SERVER['HTTP_REFERER'];
        $position = strpos($url, $frontend['base_url']);

        if ($position === false) {
            return $this->returnErrorResponse(
                'The Api Key doesn\'t match the referer url',
                $request->query->get('callback')
            );

        }
        $path = substr($url, $position + strlen($frontend['base_url']));
        if (!$path) {
            $path = '/';
        }

        $stmt = $this->app['db']->executeQuery(
            'SELECT id FROM urls WHERE frontend_id = ? AND url = ?',
            array($frontend['id'], $path)
        );

        $data = array(
            'status' => 'success',
            'title' => '',
            'comments' => array(),
        );
        $url = $stmt->fetch();
        if ($url) {
            $commentssql = $stmt = $this->app['db']->executeQuery(
                'SELECT id, user_name, comment, date, user_email
                 FROM comments
                 WHERE url_id = ?
                 ORDER BY id DESC',
                array($url['id'])
            );
            if ($comments = $commentssql->fetchAll()) {
                for ($i = 0, $nb = count($comments); $i < $nb; ++$i) {
                    $data['comments'][] = array(
                        'id' => $comments[$i]['id'],
                        'comment' => nl2br($comments[$i]['comment']),
                        'user' => $comments[$i]['user_name'],
                        'date' => $comments[$i]['date'],
                        'gravatar' => 'http://www.gravatar.com/avatar/'
                          . md5($comments[$i]['user_email']),
                    );
                }
{'fraction_non_alphanumeric': 0.17556973564266182, 'fraction_numerical': 0.0010027347310847766, 'mean_word_length': 1.8109146810146042, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1525055', 'n_tokens_mistral': 3105, 'n_tokens_neox': 3059, 'n_words': 598}