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

class ServersController extends AppController {

	public $components = array('Security' ,'RequestHandler');	// XXX ACL component

	public $paginate = array(
			'limit' => 60,
			'recursive' => -1,
			'contain' => array(
					'User' => array(
							'fields' => array('User.id', 'User.org_id', 'User.email'),
					),
					'Organisation' => array(
							'fields' => array('Organisation.name', 'Organisation.id'),
					),
					'RemoteOrg' => array(
							'fields' => array('RemoteOrg.name', 'RemoteOrg.id'),
					),
			),
			'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events
			'order' => array(
					'Server.url' => 'ASC'
			),
	);

	public $uses = array('Server', 'Event');

	public function beforeFilter() {
		parent::beforeFilter();

		// permit reuse of CSRF tokens on some pages.
		switch ($this->request->params['action']) {
			case 'push':
			case 'pull':
			case 'getVersion':
			case 'testConnection':
				$this->Security->csrfUseOnce = false;
		}
	}

	public function index() {
		if (!$this->_isSiteAdmin()) {
			if (!$this->userRole['perm_sync'] && !$this->userRole['perm_admin']) $this->redirect(array('controller' => 'events', 'action' => 'index'));
			$this->paginate['conditions'] = array('Server.org_id LIKE' => $this->Auth->user('org_id'));
		}
		$this->set('servers', $this->paginate());
		$collection = array();
		$collection['orgs'] = $this->Server->Organisation->find('list', array(
				'fields' => array('id', 'name'),
		));
		$this->loadModel('Tag');
		$collection['tags'] = $this->Tag->find('list', array(
				'fields' => array('id', 'name'),
		));
		$this->set('collection', $collection);

	}

	public function previewIndex($id) {
		if (isset($this->passedArgs['pages'])) $currentPage = $this->passedArgs['pages'];
		else $currentPage = 1;
		$urlparams = '';
		$passedArgs = array();
		if (!$this->_isSiteAdmin()) {
			throw new MethodNotAllowedException('You are not authorised to do that.');
		}
		$server = $this->Server->find('first', array('conditions' => array('Server.id' => $id), 'recursive' => -1, 'fields' => array('Server.id', 'Server.url', 'Server.name')));
		if (empty($server)) throw new NotFoundException('Invalid server ID.');
		$validFilters = $this->Server->validEventIndexFilters;
		foreach ($validFilters as $k => $filter) {
			if (isset($this->passedArgs[$filter])) {
				$passedArgs[$filter] = $this->passedArgs[$filter];
				if ($k != 0) $urlparams .= '/';
				$urlparams .= $filter . ':' . $this->passedArgs[$filter];
			}
		}
		$events = $this->Server->previewIndex($id, $this->Auth->user(), array_merge($this->passedArgs, $passedArgs));
		$this->loadModel('Event');
		$threat_levels = $this->Event->ThreatLevel->find('all');
		$this->set('threatLevels', Set::combine($threat_levels, '{n}.ThreatLevel.id', '{n}.ThreatLevel.name'));
		$pageCount = count($events);
		App::uses('CustomPaginationTool', 'Tools');
		$customPagination = new CustomPaginationTool();
		$params = $customPagination->createPaginationRules($events, $this->passedArgs, $this->alias);<commit_msg>fix: sort server preview events by timestamp, fixes #1558<commit_after><?php
App::uses('AppController', 'Controller');
App::uses('Xml', 'Utility');

class ServersController extends AppController {

	public $components = array('Security' ,'RequestHandler');	// XXX ACL component

	public $paginate = array(
			'limit' => 60,
			'recursive' => -1,
			'contain' => array(
					'User' => array(
							'fields' => array('User.id', 'User.org_id', 'User.email'),
					),
					'Organisation' => array(
							'fields' => array('Organisation.name', 'Organisation.id'),
					),
					'RemoteOrg' => array(
							'fields' => array('RemoteOrg.name', 'RemoteOrg.id'),
					),
			),
			'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events
			'order' => array(
					'Server.url' => 'ASC'
			),
	);

	public $uses = array('Server', 'Event');

	public function beforeFilter() {
		parent::beforeFilter();

		// permit reuse of CSRF tokens on some pages.
		switch ($this->request->params['action']) {
			case 'push':
			case 'pull':
			case 'getVersion':
			case 'testConnection':
				$this->Security->csrfUseOnce = false;
		}
	}

	public function index() {
		if (!$this->_isSiteAdmin()) {
			if (!$this->userRole['perm_sync'] && !$this->userRole['perm_admin']) $this->redirect(array('controller' => 'events', 'action' => 'index'));
			$this->paginate['conditions'] = array('Server.org_id LIKE' => $this->Auth->user('org_id'));
		}
		$this->set('servers', $this->paginate());
		$collection = array();
		$collection['orgs'] = $this->Server->Organisation->find('list', array(
				'fields' => array('id', 'name'),
		));
		$this->loadModel('Tag');
		$collection['tags'] = $this->Tag->find('list', array(
				'fields' => array('id', 'name'),
		));
		$this->set('collection', $collection);

	}

	public function previewIndex($id) {
		if (isset($this->passedArgs['pages'])) $currentPage = $this->passedArgs['pages'];
		else $currentPage = 1;
		$urlparams = '';
		$passedArgs = array();
		if (!$this->_isSiteAdmin()) {
			throw new MethodNotAllowedException('You are not authorised to do that.');
		}
		$server = $this->Server->find('first', array('conditions' => array('Server.id' => $id), 'recursive' => -1, 'fields' => array('Server.id', 'Server.url', 'Server.name')));
		if (empty($server)) throw new NotFoundException('Invalid server ID.');
		$validFilters = $this->Server->validEventIndexFilters;
		foreach ($validFilters as $k => $filter) {
			if (isset($this->passedArgs[$filter])) {
				$passedArgs[$filter] = $this->passedArgs[$filter];
				if ($k != 0) $urlparams .= '/';
				$urlparams .= $filter . ':' . $this->passedArgs[$filter];
			}
		}
		$combinedArgs = array_merge($this->passedArgs, $passedArgs);
		if (!isset($combinedArgs['sort'])) {
			$combinedArgs['sort'] = 'timestamp';
			$combinedArgs['direction'] = 'desc';
		}
		$events = $this->Server->previewIndex($id, $this->Auth->user(), $combinedArgs);
		$this->loadModel('Event');
		$threat_levels = $this->Event->ThreatLevel->find('all');
		$this->set('threatLevels', Set::combine($threat_levels, '{n}.ThreatLevel.id', '{n}.ThreatLevel.name'));
		$pageCount = count($events);
		App::uses('CustomPaginationTool', 'Tools');
		$customPagination = new CustomPaginationTool();
		$params = $customPagination->createPaginationRules($events, $this->passedArgs, $this->alias);
{'fraction_non_alphanumeric': 0.2158881376767056, 'fraction_numerical': 0.004917025199754148, 'mean_word_length': 4.516101694915254, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 133, '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': '6184042', 'n_tokens_mistral': 2431, 'n_tokens_neox': 2110, 'n_words': 431}