<commit_before><?php
App::uses('AppController', 'Controller');
App::uses('Xml', 'Utility');

class FeedsController extends AppController
{
    public $components = array('Security' ,'RequestHandler');   // XXX ACL component

    public $paginate = array(
            'limit' => 60,
            'recursive' => -1,
            'contain' => array('Tag', 'SharingGroup'),
            'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events
            'order' => array(
                    'Feed.default' => 'DESC',
                    'Feed.id' => 'ASC'
            ),
    );

    public $uses = array('Feed');

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Security->unlockedActions[] = 'previewIndex';
        $this->Security->unlockedActions[] = 'feedCoverage';
        if (!$this->_isSiteAdmin() && $this->Auth->user('org_id') != Configure::read('MISP.host_org_id')) {
            throw new MethodNotAllowedException(__('You don\'t have the required privileges to do that.'));
        }
    }

    public function index()
    {
        if (!$this->_isSiteAdmin() && !$this->Auth->user('org_id') == Configure::read('MISP.host_org_id')) {
            throw NotAllowedException('You don\'t have access to this feature.');
        }
        $this->Feed->load_default_feeds();
        $scope = isset($this->passedArgs['scope']) ? $this->passedArgs['scope'] : 'all';
        if ($scope !== 'all') {
            if ($scope == 'enabled') {
                $this->paginate['conditions'][] = array(
                    'OR' => array(
                        'Feed.enabled' => 1,
                        'Feed.caching_enabled' => 1
                    )
                );
            } else {
                $this->paginate['conditions'][] = array(
                    'Feed.default' => $scope == 'custom' ? 0 : 1
                );
            }
        }
        $data = $this->paginate();
        $this->loadModel('Event');
        foreach ($data as $key => $value) {
            if ($value['Feed']['event_id'] != 0 && $value['Feed']['fixed_event']) {
                $event = $this->Event->find('first', array('conditions' => array('Event.id' => $value['Feed']['event_id']), 'recursive' => -1, 'fields' => array('Event.id')));
                if (empty($event)) {
                    $data[$key]['Feed']['event_error'] = true;
                }
            }
        }
        if ($this->_isSiteAdmin()) {
            $data = $this->Feed->attachFeedCacheTimestamps($data);
        }
        if ($this->_isRest()) {
            foreach ($data as $k => $v) {
                unset($data[$k]['SharingGroup']);
                if (empty($data[$k]['Tag']['id'])) {
                    unset($data[$k]['Tag']);
                }
            }
            return $this->RestResponse->viewData($data, $this->response->type());
        }
        $this->set('scope', $scope);
        $this->set('feeds', $data);
        $this->loadModel('Event');
        $this->set('feed_types', $this->Feed->feed_types);
        $this->set('distributionLevels', $this->Event->distributionLevels);
    }

    public function view($feedId)
    {
        if (!$this->_isSiteAdmin() && !$this->Auth->user('org_id') == Configure::read('MISP.host_org_id')) {
            throw NotAllowedException('You don\'t have access to this feature.');
        }
        $feed = $this->Feed->find('first', array(
            'conditions' => array('Feed.id' => $feedId),
            'recursive' => -1,
            'contain' => array('Tag')
        ));
        $feed['Feed']['cached_elements'] = $this->Feed->getCachedElements($feed['Feed']['id']);
        $feed['Feed']['coverage_by_other_feeds'] = $this->Feed->getFeedCoverage($feed['Feed']['id'], 'feed', 'all') . '%';
        if ($this->_isRest()) {
            if (empty($feed['Tag']['id'])) {
                unset($feed['Tag']);
            }
            return $this->RestResponse->viewData($feed, $this->response->type());
        }
        $feeds = $this->Feed->getAllCachingEnabledFeeds($feed['Feed']['id'], true);
        $this->set('other_feeds', $feeds);
        $this->set('feed', $feed);
    }
    
    public function feedCoverage($feedId)
    {
        if (!$this->_isSiteAdmin() && !$this->Auth->user('org_id') == Configure::read('MISP.host_org_id')) {
            throw NotAllowedException('You don\'t have access to this feature.');
        }
        $feed = $this->Feed->find('first', array(
            'conditions' => array('Feed.id' => $feedId),
            'recursive' => -1,
            'contain' => array('Tag')
        ));
        $result = $this->Feed->getFeedCoverage($feed['Feed']['id'], 'feed', $this->request->data);
        return $this->RestResponse->viewData($result, $this->response->type());

    }

    public function importFeeds()
    {
        if ($this->request->is('post')) {<commit_msg>fix: [API] Feed index shouldn't adhere to pagination defaults of the UI, fixes MISP/misp-book#149<commit_after><?php
App::uses('AppController', 'Controller');
App::uses('Xml', 'Utility');

class FeedsController extends AppController
{
    public $components = array('Security' ,'RequestHandler');   // XXX ACL component

    public $paginate = array(
            'limit' => 60,
            'recursive' => -1,
            'contain' => array('Tag', 'SharingGroup'),
            'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events
            'order' => array(
                    'Feed.default' => 'DESC',
                    'Feed.id' => 'ASC'
            ),
    );

    public $uses = array('Feed');

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Security->unlockedActions[] = 'previewIndex';
        $this->Security->unlockedActions[] = 'feedCoverage';
        if (!$this->_isSiteAdmin() && $this->Auth->user('org_id') != Configure::read('MISP.host_org_id')) {
            throw new MethodNotAllowedException(__('You don\'t have the required privileges to do that.'));
        }
    }

    public function index()
    {
        if (!$this->_isSiteAdmin() && !$this->Auth->user('org_id') == Configure::read('MISP.host_org_id')) {
            throw NotAllowedException('You don\'t have access to this feature.');
        }
        $this->Feed->load_default_feeds();
        $scope = isset($this->passedArgs['scope']) ? $this->passedArgs['scope'] : 'all';
        if ($scope !== 'all') {
            if ($scope == 'enabled') {
                $this->paginate['conditions'][] = array(
                    'OR' => array(
                        'Feed.enabled' => 1,
                        'Feed.caching_enabled' => 1
                    )
                );
            } else {
                $this->paginate['conditions'][] = array(
                    'Feed.default' => $scope == 'custom' ? 0 : 1
                );
            }
        }
        if ($this->_isRest()) {
            $keepFields = array('conditions', 'contain', 'recursive', 'sort');
            $searchParams = array();
            foreach ($keepFields as $field) {
                if (!empty($this->paginate[$field])) {
                    $searchParams[$field] = $this->paginate[$field];
                }
            }

            $data = $this->Feed->find('all', $searchParams);
        } else {
            $data = $this->paginate();
        }
        $this->loadModel('Event');
        foreach ($data as $key => $value) {
            if ($value['Feed']['event_id'] != 0 && $value['Feed']['fixed_event']) {
                $event = $this->Event->find('first', array('conditions' => array('Event.id' => $value['Feed']['event_id']), 'recursive' => -1, 'fields' => array('Event.id')));
                if (empty($event)) {
                    $data[$key]['Feed']['event_error'] = true;
                }
            }
        }
        if ($this->_isSiteAdmin()) {
            $data = $this->Feed->attachFeedCacheTimestamps($data);
        }
        if ($this->_isRest()) {
            foreach ($data as $k => $v) {
                unset($data[$k]['SharingGroup']);
                if (empty($data[$k]['Tag']['id'])) {
                    unset($data[$k]['Tag']);
                }
            }
            return $this->RestResponse->viewData($data, $this->response->type());
        }
        $this->set('scope', $scope);
        $this->set('feeds', $data);
        $this->loadModel('Event');
        $this->set('feed_types', $this->Feed->feed_types);
        $this->set('distributionLevels', $this->Event->distributionLevels);
    }

    public function view($feedId)
    {
        if (!$this->_isSiteAdmin() && !$this->Auth->user('org_id') == Configure::read('MISP.host_org_id')) {
            throw NotAllowedException('You don\'t have access to this feature.');
        }
        $feed = $this->Feed->find('first', array(
            'conditions' => array('Feed.id' => $feedId),
            'recursive' => -1,
            'contain' => array('Tag')
        ));
        $feed['Feed']['cached_elements'] = $this->Feed->getCachedElements($feed['Feed']['id']);
        $feed['Feed']['coverage_by_other_feeds'] = $this->Feed->getFeedCoverage($feed['Feed']['id'], 'feed', 'all') . '%';
        if ($this->_isRest()) {
            if (empty($feed['Tag']['id'])) {
                unset($feed['Tag']);
            }
            return $this->RestResponse->viewData($feed, $this->response->type());
        }
        $feeds = $this->Feed->getAllCachingEnabledFeeds($feed['Feed']['id'], true);
        $this->set('other_feeds', $feeds);
        $this->set('feed', $feed);
    }

    public function feedCoverage($feedId)
    {
        if (!$this->_isSiteAdmin() && !$this->Auth->user('org_id') == Configure::read('MISP.host_org_id')) {
            throw NotAllowedException('You don\'t have access to this feature.');
        }
        $feed = $this->Feed->find('first', array(
            'conditions' => array('Feed.id' => $feedId),
            'recursive' => -1,
            'contain' => array('Tag')
        ));
        $result = $this->Feed->getFeedCoverage($feed['Feed']['id'], 'feed', $this->request->data);
        return $this->RestResponse->viewData($result, $this->response->type());

    }

    public function importFeeds()
    {
        if ($this->request->is('post')) {
{'fraction_non_alphanumeric': 0.19795188870640518, 'fraction_numerical': 0.003960969954593759, 'mean_word_length': 2.0745470745470747, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 198, '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': '6026652', 'n_tokens_mistral': 3062, 'n_tokens_neox': 2962, 'n_words': 538}