minor memory usage improvements by referencing in foreach ($array as

&$value) loop
pull/61/head
Christophe Vandeplas 2012-06-11 11:40:31 +02:00
parent 2d335f5dbe
commit 957e4f232b
4 changed files with 28 additions and 28 deletions

View File

@ -100,9 +100,9 @@ class EventsController extends AppController {
// This is a lot faster (only additional query) than $this->Event->getRelatedEvents()
$relatedEventIds = array();
$relatedEvents = array();
foreach ($relatedAttributes as $relatedAttribute) {
foreach ($relatedAttributes as &$relatedAttribute) {
if (null == $relatedAttribute) continue;
foreach ($relatedAttribute as $item) {
foreach ($relatedAttribute as &$item) {
$relatedEventsIds[] = $item['Attribute']['event_id'];
}
}
@ -304,7 +304,7 @@ class EventsController extends AppController {
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
foreach ($servers as $server) {
foreach ($servers as &$server) {
$this->Event->uploadEventToServer($this->Event->data, $server, $HttpSocket);
}
}
@ -394,7 +394,7 @@ class EventsController extends AppController {
$body .= 'Risk : '.$event['Event']['risk']."\n";
$relatedEvents = $this->Event->getRelatedEvents($id);
if (!empty($relatedEvents)) {
foreach ($relatedEvents as $relatedEvent){
foreach ($relatedEvents as &$relatedEvent){
$body .= 'Related to : '.Configure::read('CyDefSIG.baseurl').'/events/view/'.$relatedEvent['Event']['id'].' ('.$relatedEvent['Event']['date'].')'."\n" ;
}
@ -406,7 +406,7 @@ class EventsController extends AppController {
$body_temp_other = "";
if (isset($event['Attribute'])) {
foreach ($event['Attribute'] as $attribute){
foreach ($event['Attribute'] as &$attribute){
$line = '- '.$attribute['type'].str_repeat(' ', $appendlen - 2 - strlen( $attribute['type'])).': '.$attribute['value']."\n";
if ('other' == $attribute['type']) // append the 'other' attribute types to the bottom.
$body_temp_other .= $line;
@ -435,7 +435,7 @@ class EventsController extends AppController {
'recursive' => 0,
) );
$alert_emails = Array();
foreach ($alert_users as $user) {
foreach ($alert_users as &$user) {
$alert_emails[] = $user['User']['email'];
}
// prepare the the unencrypted email
@ -463,7 +463,7 @@ class EventsController extends AppController {
)
);
// encrypt the mail for each user and send it separately
foreach ($alert_users as $user) {
foreach ($alert_users as &$user) {
// send the email
$this->Email->from = Configure::read('CyDefSIG.email');
$this->Email->to = $user['User']['email'];
@ -568,7 +568,7 @@ class EventsController extends AppController {
$body .= 'Risk : '.$event['Event']['risk']."\n";
$relatedEvents = $this->Event->getRelatedEvents($id);
if (!empty($relatedEvents)) {
foreach ($relatedEvents as $relatedEvent){
foreach ($relatedEvents as &$relatedEvent){
$body .= 'Related to : '.Configure::read('CyDefSIG.baseurl').'/events/view/'.$relatedEvent['Event']['id'].' ('.$relatedEvent['Event']['date'].')'."\n" ;
}
@ -579,7 +579,7 @@ class EventsController extends AppController {
$body .= 'Attributes :'."\n";
$body_temp_other = "";
if (!empty($event['Attribute'])) {
foreach ($event['Attribute'] as $attribute){
foreach ($event['Attribute'] as &$attribute){
$line = '- '.$attribute['type'].str_repeat(' ', $appendlen - 2 - strlen( $attribute['type'])).': '.$attribute['value']."\n";
if ('other' == $attribute['type']) // append the 'other' attribute types to the bottom.
$body_temp_other .= $line;
@ -610,7 +610,7 @@ class EventsController extends AppController {
);
}
foreach ($org_members as $reporter) {
foreach ($org_members as &$reporter) {
if (!empty($reporter['User']['gpgkey'])) {
// import the key of the user into the keyring
// this isn't really necessary, but it gives it the fingerprint necessary for the next step
@ -729,7 +729,7 @@ class EventsController extends AppController {
$items = $this->Attribute->find('all', $params);
$classtype = 'targeted-attack';
foreach ($items as $item) {
foreach ($items as &$item) {
# proto src_ip src_port direction dst_ip dst_port msg rule_content tag sid rev
$rule_format_msg = 'msg: "CyDefSIG %s, Event '.$item['Event']['id'].', '.$item['Event']['risk'].'"';
$rule_format_reference = 'reference:url,'.Configure::read('CyDefSIG.baseurl').'/events/view/'.$item['Event']['id'];
@ -935,7 +935,7 @@ class EventsController extends AppController {
print ("#<h1>This part is not finished and might be buggy. Please report any issues.</h1>\n");
print "#<pre> \n";
foreach ($rules as $rule)
foreach ($rules as &$rule)
print $rule."\n";
print "#</pre>\n";
@ -1046,7 +1046,7 @@ class EventsController extends AppController {
// explode using the dot
$explodedNames = explode('.', $name);
// for each part
foreach ($explodedNames as $explodedName) {
foreach ($explodedNames as &$explodedName) {
// count the lenght of the part, and add |length| before
$length = strlen($explodedName);
if ($length > 255) exit('ERROR: dns name is to long for RFC'); // LATER log correctly without dying

View File

@ -165,7 +165,7 @@ class ServersController extends AppController {
if (null != $event_ids) {
App::import('Controller', 'Events');
$HttpSocket = new HttpSocket();
foreach ($event_ids as $event_id) {
foreach ($event_ids as &$event_id) {
$event = $this->Event->downloadEventFromServer(
$event_id,
$this->Server->data);
@ -258,7 +258,7 @@ class ServersController extends AppController {
$this->loadModel('Attribute');
// upload each event separately and keep the results in the $successes and $fails arrays
foreach ($events as $event) {
foreach ($events as &$event) {
$result = $this->Event->uploadEventToServer(
$event,
$this->Server->data,

View File

@ -179,7 +179,7 @@ class UsersController extends AppController {
}
if ($this->request->is('post') || $this->request->is('put')) {
$fields = array();
foreach (array_keys($this->request->data['User']) as $field) {
foreach (array_keys($this->request->data['User']) as &$field) {
if($field != 'password') array_push($fields, $field);
}
if ("" != $this->request->data['User']['password'])
@ -308,7 +308,7 @@ class UsersController extends AppController {
$sig_types = array_keys($this->Attribute->type_definitions);
$graph_fields = '';
foreach ($sig_types as $sig_type) {
foreach ($sig_types as &$sig_type) {
if ($graph_fields != "") $graph_fields .= ", ";
$graph_fields .= "'".$sig_type."'";
}
@ -318,7 +318,7 @@ class UsersController extends AppController {
$graph_data=array();
$prev_row_org = "";
$i = -1;
foreach ($types_histogram as $row) {
foreach ($types_histogram as &$row) {
if ($prev_row_org != $row['Event']['org']) {
$i++; $graph_data[] = "";
$prev_row_org = $row['Event']['org'];

View File

@ -165,12 +165,12 @@ class Event extends AppModel {
// first get a list of related event_ids
// then do a single query to search for all the events with that id
$relatedEventIds = Array();
foreach ($this->data['Attribute'] as $attribute ) {
foreach ($this->data['Attribute'] as &$attribute ) {
if ($attribute['type'] == 'other')
continue; // sigs of type 'other' should not be matched against the others
$conditions = array('Attribute.value =' => $attribute['value'], 'Attribute.type =' => $attribute['type']);
$similar_attributes = $this->Attribute->find('all',array('conditions' => $conditions));
foreach ($similar_attributes as $similar_attribute) {
foreach ($similar_attributes as &$similar_attribute) {
if ($this->id == $similar_attribute['Attribute']['event_id'])
continue; // same as this event, not needed in the list
$relatedEventIds[] = $similar_attribute['Attribute']['event_id'];
@ -250,19 +250,19 @@ class Event extends AppModel {
// cleanup the array from things we do not want to expose
unset($event['Event']['org']);
// remove value1 and value2 from the output
foreach($event['Event']['Attribute'] as $key => $attribute) {
foreach($event['Event']['Attribute'] as $key => &$attribute) {
// do not keep attributes that are private
if ($event['Event']['Attribute'][$key]['private']) {
if ($attribute['private']) {
unset($event['Event']['Attribute'][$key]);
continue; // stop processing this
}
// remove value1 and value2 from the output
unset($event['Event']['Attribute'][$key]['value1']);
unset($event['Event']['Attribute'][$key]['value2']);
unset($attribute['value1']);
unset($attribute['value2']);
// also add the encoded attachment
if ($this->Attribute->typeIsAttachment($event['Event']['Attribute'][$key]['type'])) {
$encoded_file = $this->Attribute->base64EncodeAttachment($event['Event']['Attribute'][$key]);
$event['Event']['Attribute'][$key]['data'] = $encoded_file;
if ($this->Attribute->typeIsAttachment($attribute['type'])) {
$encoded_file = $this->Attribute->base64EncodeAttachment($attribute);
$attribute['data'] = $encoded_file;
}
}
@ -349,7 +349,7 @@ class Event extends AppModel {
$xml = Xml::build($response->body);
$eventArray = Xml::toArray($xml);
$event_ids=array();
foreach ($eventArray['response']['Event'] as $event) {
foreach ($eventArray['response']['Event'] as &$event) {
if (1 != $event['published']) continue; // do not keep non-published events
$event_ids[] = $event['id'];
}