Posts

Showing posts from January, 2017

Drupal DDoS Prevention Code

// The following code will ensure that there are no more than 30 non-cached requests in 60 seconds. // Please adjust the first two variables below in a manner optimal to your server config. // Note that this only protects heavier non cached pages. // A powerful enough attack can even overload your server with requests for cached pages. function hook_init() {     $MinTime = 60;     $MaxAttmpts = 30;     $MyAppDDoSCheckUrl = variable_get('MyAppDDoSCheckUrl');     $TimeCheckNow = time();     if ($MyAppDDoSCheckUrl == '')     {         variable_set('MyAppDDoSCheckUrl',$TimeCheckNow.',1');         return;     }     $SerDDosVars = explode(',',$MyAppDDoSCheckUrl);     $TimeLast = intval($SerDDosVars[0]);     $ReqAttmt = intval($SerDDosVars[1]);         if ((($TimeCheckNow - $TimeLast) < $MinTime) && ($ReqAttmt > $MaxAttmpts))     {         header('HTTP/1.1 503 Server busy, try again later');         die('The server is cur

Toggle #input_group on fields in Drupal 7x

Here's the code for iterating through all fields in a form and setting $element [ '#input_group' ] = TRUE ;     function hook_form_alter ( & $form , $form_state , $form_id ) { if ( $form_id == 'nodetype1_node_form' ) { iterateForm ( $form , 'setIgt' ) ; } } // Generic iterate function. Can be used with any callback. function iterateForm ( & $form , callable $callback ) { foreach ( element_children ( $form ) as $key ) { $element = & $form [ $key ] ; $children = element_children ( $element ) ; if ( empty ( $children ) ) { call_user_func_array ( $callback , array ( & $element , & $key ) ) ; } else { iterateForm ( $element , $callback ) ; } } } // Custom callback function setIgt ( & $element , & $key ) { if ( $element [ '#type' ] == 'textfield' ) $element [ '#input_group' ] = TRUE ; }