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

/**
 * Attributes Controller
 *
 * @property Attribute $Attribute
 */
class AttributesController extends AppController {

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

	public $paginate = array(
			'limit' => 60,
			'maxLimit' => 9999,  // LATER we will bump here on a problem once we have more than 9999 events
	);

	public $helpers = array('Js' => array('Jquery'));

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

		// permit reuse of CSRF tokens on the search page.
		if ('search' == $this->request->params['action']) {
			$this->Security->csrfUseOnce = false;
		}
		$this->Security->validatePost = false;

		// convert uuid to id if present in the url, and overwrite id field
		if (isset($this->params->query['uuid'])) {
			$params = array(
					'conditions' => array('Attribute.uuid' => $this->params->query['uuid']),
					'recursive' => 0,
					'fields' => 'Attribute.id'
					);
			$result = $this->Attribute->find('first', $params);
			if (isset($result['Attribute']) && isset($result['Attribute']['id'])) {
				$id = $result['Attribute']['id'];
				$this->params->addParams(array('pass' => array($id))); // FIXME find better way to change id variable if uuid is found. params->url and params->here is not modified accordingly now
			}
		}

		// do not show private to other groups
		if ('true' == Configure::read('CyDefSIG.private')) {
			// if not admin or own org, check private as well..
			if (!$this->_IsAdmin()) {
				$this->paginate = Set::merge($this->paginate,array(
				'conditions' =>
						array("OR" => array(
						array('Event.org =' => $this->Auth->user('org')),
						array("AND" => array('Event.org !=' => $this->Auth->user('org')), array('Event.private !=' => 1), array('Attribute.private !=' => 1)))),
				)
				);
			}
		}

		// do not show cluster outside server
		if ('true' == Configure::read('CyDefSIG.private')) {
			if ($this->_isRest()) {
					$this->paginate = Set::merge($this->paginate,array(
					'conditions' =>
							array("AND" => array('Event.cluster !=' => true),array('Attribute.cluster !=' => true)),
							//array("AND" => array(array('Event.private !=' => 2))),
					));
			}
		}
	}

	public function isAuthorized($user) {
		// Admins can access everything
		if (parent::isAuthorized($user)) {
			return true;
		}
		// Only on own attributes for these actions
		if (in_array($this->action, array('delete'))) {	// TODO ACL, removed 'edit' override
			$attributeid = $this->request->params['pass'][0];
			return $this->Attribute->isOwnedByOrg($attributeid, $this->Auth->user('org'));
		}
		// Only on own events for these actions
		if (in_array($this->action, array('add', 'add_attachment'))) {
			$this->loadModel('Event');
			$eventid = $this->request->params['pass'][0];
			return $this->Event->isOwnedByOrg($eventid, $this->Auth->user('org'));
		}
		// the other pages are allowed by logged in users
		return true;
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Attribute->recursive = 0;
		$this->set('attributes', $this->paginate());

		$this->set('attrDescriptions', $this->Attribute->fieldDescriptions);
		$this->set('typeDefinitions', $this->Attribute->typeDefinitions);
		$this->set('categoryDefinitions', $this->Attribute->categoryDefinitions);
	}

/**
 * add method
 *
 * @return void
 */
	public function add($eventId = null) {
		if ($this->request->is('post')) {
			$this->loadModel('Event');
			// only own attributes verified by isAuthorized

			// Give error if someone tried to submit a attribute with attachment or malware-sample type.
			// TODO change behavior attachment options - this is bad ... it should rather by a messagebox or should be filtered out on the view level
			if (isset($this->request->data['Attribute']['type']) && $this->Attribute->typeIsAttachment($this->request->data['Attribute']['type'])) {
				$this->Session->setFlash(__('Attribute has not been added: attachments are added by "Add attachment" button', true), 'default', array(), 'error');
				$this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id']));
			}

			// remove the published flag from the event
			$this->Event->id = $this->request->data['Attribute']['event_id'];
			$this->Event->saveField('published', 0);

			//
			// multiple attributes in batch import
			//
			if ($this->request->data['Attribute']['batch_import'] == 1) {
				// make array from value field
				$attributes = explode("\n", $this->request->data['Attribute']['value']);

				$fails = "";	 // will be used to keep a list of the lines that failed or succeeded
				$successes = "";
				foreach ($attributes as $key => $attribute) {
					$attribute = trim($attribute);
					if (strlen($attribute) == 0 )
					continue; // don't do anything for empty lines

					$this->Attribute->create();
					$this->request->data['Attribute']['value'] = $attribute;  // set the value as the content of the single line
					if ('true' == Configure::read('CyDefSIG.private')) {
						$this->request->data = $this->Attribute->massageData(&$this->request->data);
					}
					if ($this->Attribute->save($this->request->data)) {
						$successes .= " " . ($key + 1);
					} else {
						$fails .= " " . ($key + 1);
					}

				}
				// we added all the attributes,
				if ($fails) {
					// list the ones that failed
					$this->Session->setFlash(__('The lines' . $fails . ' could not be saved. Please, try again.', true), 'default', array(), 'error');
				}
				if ($successes) {
					// list the ones that succeeded
					$this->Session->setFlash(__('The lines' . $successes . ' have been saved', true));
				}

				$this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id']));

			} else {
				//
				// single attribute
				//
				// create the attribute
				$this->Attribute->create();

				if ('true' == Configure::read('CyDefSIG.private')) {
					$this->request->data = $this->Attribute->massageData(&$this->request->data);
				}

				if ($this->Attribute->save($this->request->data)) {
					// inform the user and redirect
					$this->Session->setFlash(__('The attribute has been saved'));
					$this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id']));
				} else {
					$this->Session->setFlash(__('The attribute could not be saved. Please, try again.'));
				}
			}
		} else {
			// set the event_id in the form
			$this->request->data['Attribute']['event_id'] = $eventId;
		}

		// combobox for types
		$types = array_keys($this->Attribute->typeDefinitions);
		$types = $this->_arrayToValuesIndexArray($types);
		$this->set('types',$types);
		// combobox for categories
		$categories = array('' => array('(choose one)' => '(choose one)'), 'categories' => array());
		array_pop($this->Attribute->validate['category']['rule'][1]); // remove that last 'empty' item
		$categories['categories'] = array_merge($categories['categories'], $this->_arrayToValuesIndexArray($this->Attribute->validate['category']['rule'][1]));
		$this->set('categories',$categories);
		// combobox for distribution
		$distributions = array_keys($this->Attribute->distributionDescriptions);
		$distributions = $this->_arrayToValuesIndexArray($distributions);
		$this->set('distributions',$distributions);
		// tooltip for distribution
		$this->set('distributionDescriptions', $this->Attribute->distributionDescriptions);

		$this->set('attrDescriptions', $this->Attribute->fieldDescriptions);
		$this->set('typeDefinitions', $this->Attribute->typeDefinitions);
		$this->set('categoryDefinitions', $this->Attribute->categoryDefinitions);
	}
<commit_msg>dropdowns<commit_after><?php
App::uses('AppController', 'Controller');
App::uses('Folder', 'Utility');
App::uses('File', 'Utility');

/**
 * Attributes Controller
 *
 * @property Attribute $Attribute
 */
class AttributesController extends AppController {

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

	public $paginate = array(
			'limit' => 60,
			'maxLimit' => 9999,  // LATER we will bump here on a problem once we have more than 9999 events
	);

	public $helpers = array('Js' => array('Jquery'));

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

		// permit reuse of CSRF tokens on the search page.
		if ('search' == $this->request->params['action']) {
			$this->Security->csrfUseOnce = false;
		}
		$this->Security->validatePost = false;

		// convert uuid to id if present in the url, and overwrite id field
		if (isset($this->params->query['uuid'])) {
			$params = array(
					'conditions' => array('Attribute.uuid' => $this->params->query['uuid']),
					'recursive' => 0,
					'fields' => 'Attribute.id'
					);
			$result = $this->Attribute->find('first', $params);
			if (isset($result['Attribute']) && isset($result['Attribute']['id'])) {
				$id = $result['Attribute']['id'];
				$this->params->addParams(array('pass' => array($id))); // FIXME find better way to change id variable if uuid is found. params->url and params->here is not modified accordingly now
			}
		}

		// do not show private to other groups
		if ('true' == Configure::read('CyDefSIG.private')) {
			// if not admin or own org, check private as well..
			if (!$this->_IsAdmin()) {
				$this->paginate = Set::merge($this->paginate,array(
				'conditions' =>
						array("OR" => array(
						array('Event.org =' => $this->Auth->user('org')),
						array("AND" => array('Event.org !=' => $this->Auth->user('org')), array('Event.private !=' => 1), array('Attribute.private !=' => 1)))),
				)
				);
			}
		}

		// do not show cluster outside server
		if ('true' == Configure::read('CyDefSIG.private')) {
			if ($this->_isRest()) {
					$this->paginate = Set::merge($this->paginate,array(
					'conditions' =>
							array("AND" => array('Event.cluster !=' => true),array('Attribute.cluster !=' => true)),
							//array("AND" => array(array('Event.private !=' => 2))),
					));
			}
		}
	}

	public function isAuthorized($user) {
		// Admins can access everything
		if (parent::isAuthorized($user)) {
			return true;
		}
		// Only on own attributes for these actions
		if (in_array($this->action, array('delete'))) {	// TODO ACL, removed 'edit' override
			$attributeid = $this->request->params['pass'][0];
			return $this->Attribute->isOwnedByOrg($attributeid, $this->Auth->user('org'));
		}
		// Only on own events for these actions
		if (in_array($this->action, array('add', 'add_attachment'))) {
			$this->loadModel('Event');
			$eventid = $this->request->params['pass'][0];
			return $this->Event->isOwnedByOrg($eventid, $this->Auth->user('org'));
		}
		// the other pages are allowed by logged in users
		return true;
	}

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Attribute->recursive = 0;
		$this->set('attributes', $this->paginate());

		$this->set('attrDescriptions', $this->Attribute->fieldDescriptions);
		$this->set('typeDefinitions', $this->Attribute->typeDefinitions);
		$this->set('categoryDefinitions', $this->Attribute->categoryDefinitions);
	}

/**
 * add method
 *
 * @return void
 */
	public function add($eventId = null) {
		if ($this->request->is('post')) {
			$this->loadModel('Event');
			// only own attributes verified by isAuthorized

			// Give error if someone tried to submit a attribute with attachment or malware-sample type.
			// TODO change behavior attachment options - this is bad ... it should rather by a messagebox or should be filtered out on the view level
			if (isset($this->request->data['Attribute']['type']) && $this->Attribute->typeIsAttachment($this->request->data['Attribute']['type'])) {
				$this->Session->setFlash(__('Attribute has not been added: attachments are added by "Add attachment" button', true), 'default', array(), 'error');
				$this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id']));
			}

			// remove the published flag from the event
			$this->Event->id = $this->request->data['Attribute']['event_id'];
			$this->Event->saveField('published', 0);

			//
			// multiple attributes in batch import
			//
			if ($this->request->data['Attribute']['batch_import'] == 1) {
				// make array from value field
				$attributes = explode("\n", $this->request->data['Attribute']['value']);

				$fails = "";	 // will be used to keep a list of the lines that failed or succeeded
				$successes = "";
				foreach ($attributes as $key => $attribute) {
					$attribute = trim($attribute);
					if (strlen($attribute) == 0 )
					continue; // don't do anything for empty lines

					$this->Attribute->create();
					$this->request->data['Attribute']['value'] = $attribute;  // set the value as the content of the single line
					if ('true' == Configure::read('CyDefSIG.private')) {
						$this->request->data = $this->Attribute->massageData(&$this->request->data);
					}
					if ($this->Attribute->save($this->request->data)) {
						$successes .= " " . ($key + 1);
					} else {
						$fails .= " " . ($key + 1);
					}

				}
				// we added all the attributes,
				if ($fails) {
					// list the ones that failed
					$this->Session->setFlash(__('The lines' . $fails . ' could not be saved. Please, try again.', true), 'default', array(), 'error');
				}
				if ($successes) {
					// list the ones that succeeded
					$this->Session->setFlash(__('The lines' . $successes . ' have been saved', true));
				}

				$this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id']));

			} else {
				//
				// single attribute
				//
				// create the attribute
				$this->Attribute->create();

				if ('true' == Configure::read('CyDefSIG.private')) {
					$this->request->data = $this->Attribute->massageData(&$this->request->data);
				}

				if ($this->Attribute->save($this->request->data)) {
					// inform the user and redirect
					$this->Session->setFlash(__('The attribute has been saved'));
					$this->redirect(array('controller' => 'events', 'action' => 'view', $this->request->data['Attribute']['event_id']));
				} else {
					$this->Session->setFlash(__('The attribute could not be saved. Please, try again.'));
				}
			}
		} else {
			// set the event_id in the form
			$this->request->data['Attribute']['event_id'] = $eventId;
		}

		// combobox for types
		$types = array_keys($this->Attribute->typeDefinitions);
		$types = $this->_arrayToValuesIndexArray($types);
		$this->set('types',$types);
		// combobos for categories
		$categories = $this->Attribute->validate['category']['rule'][1];
		array_pop($categories);
		$categories = $this->_arrayToValuesIndexArray($categories);
		$this->set('categories',compact('categories'));
		// combobox for distribution
		$distributions = array_keys($this->Attribute->distributionDescriptions);
		$distributions = $this->_arrayToValuesIndexArray($distributions);
		$this->set('distributions',$distributions);
		// tooltip for distribution
		$this->set('distributionDescriptions', $this->Attribute->distributionDescriptions);

		$this->set('attrDescriptions', $this->Attribute->fieldDescriptions);
		$this->set('typeDefinitions', $this->Attribute->typeDefinitions);
		$this->set('categoryDefinitions', $this->Attribute->categoryDefinitions);
	}

{'fraction_non_alphanumeric': 0.1944139134932437, 'fraction_numerical': 0.0030387276136290167, 'mean_word_length': 3.8626218170386672, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 352, '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': '6945821', 'n_tokens_mistral': 5383, 'n_tokens_neox': 4543, 'n_words': 1114}