ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

security_utilities.php
Go to the documentation of this file.
1 <?php
2
3 /*
4 * This source code is a part of the Ultimix Project.
5 * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6 * it's own license wich could be found from the corresponding files or sources.
7 * This source code is provided "as is" without any warranties or garanties.
8 *
9 * Have a nice day!
10 *
11 * @url http://ultimix.sorceforge.net
12 *
13 * @author Alexey "gdever" Dodonov
14 */
15
26 class security_utilities_1_0_0{
27
38 var $Security = false;
39 var $SecurityParser = false;
40 var $Settings = false;
41 var $String = false;
42 var $SupportedDataTypes = false;
43
54 function __construct()
55 {
56 try
57 {
58 $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
59 $this->String = get_package( 'string' , 'last' , __FILE__ );
60 $this->SupportedDataTypes = get_package( 'security::supported_data_types' , 'last' , __FILE__ );
61 }
62 catch( Exception $e )
63 {
64 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
65 }
66 }
67
98 function get_fields_from_array( $Prefix , $Mode , &$Array )
99 {
100 try
101 {
102 $Fields = array();
103
104 foreach( $Array as $k => $v )
105 {
106 if( strpos( $k , $Prefix ) !== false )
107 {
108 $Fields [] = $k;
109 }
110 }
111
112 return( $Fields );
113 }
114 catch( Exception $e )
115 {
116 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
117 }
118 }
119
146 function get_fields( $Prefix , $Mode )
147 {
148 try
149 {
150 $Fields = array();
151
152 if( $Mode & POST )
153 {
154 $Fields = array_merge( $this->get_fields_from_array( $Prefix , $Mode , $_POST ) , $Fields );
155 }
156 if( $Mode & GET )
157 {
158 $Fields = array_merge( $this->get_fields_from_array( $Prefix , $Mode , $_GET ) , $Fields );
159 }
160 if( $Mode & COOKIE )
161 {
162 $Fields = array_merge( $this->get_fields_from_array( $Prefix , $Mode , $_COOKIE ) , $Fields );
163 }
164 if( $Mode & SESSION )
165 {
166 $Fields = array_merge( $this->get_fields_from_array( $Prefix , $Mode , $_SESSION ) , $Fields );
167 }
168
169 return( $Fields );
170 }
171 catch( Exception $e )
172 {
173 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
174 }
175 }
176
211 function get_value( &$Array , $Field , $Type = 'set' , $DefaultValue = '_throw_exception' )
212 {
213 try
214 {
215 if( isset( $Array[ $Field ] ) && $Type !== 'set' )
216 {
217 if( $this->Security === false )
218 {
219 $this->Security = get_package( 'security' , 'last' , __FILE__ );
220 }
221 return( $this->Security->get( $Array[ $Field ] , $Type ) );
222 }
223
224 return( $this->handle_default_set( $Array , $Field , $Type , $DefaultValue ) );
225 }
226 catch( Exception $e )
227 {
228 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
229 }
230 }
231
254 private function merge_globals( $Mode )
255 {
256 try
257 {
258 $Data = array();
259
260 $Data = $Mode & POST ? array_merge( $Data , $_POST ) : $Data;
261
262 $Data = $Mode & GET ? array_merge( $Data , $_GET ) : $Data;
263
264 $Data = $Mode & COOKIE ? array_merge( $Data , $_COOKIE ) : $Data;
265
266 $Data = $Mode & SESSION ? array_merge( $Data , $_SESSION ) : $Data;
267
268 $Data = $Mode & SERVER ? array_merge( $Data , $_SERVER ) : $Data;
269
270 return( $Data );
271 }
272 catch( Exception $e )
273 {
274 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
275 }
276 }
277
312 private function handle_default_set( &$Data , $Field , $Type , $DefaultValue )
313 {
314 try
315 {
316 if( $Type === 'set' )
317 {
318 return( isset( $Data[ $Field ] ) );
319 }
320 else
321 {
322 if( $DefaultValue === '_throw_exception' )
323 {
324 throw( new Exception( "Field '$Field' was not found" ) );
325 }
326 else
327 {
328 return( $DefaultValue );
329 }
330 }
331 }
332 catch( Exception $e )
333 {
334 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
335 }
336 }
337
364 private function handle_get( $Field , $Mode )
365 {
366 try
367 {
368 if( ( $Mode & POST ) && isset( $_POST[ $Field ] ) )
369 {
370 return( $_POST[ $Field ] );
371 }
372 elseif( ( $Mode & GET ) && isset( $_GET[ $Field ] ) )
373 {
374 return( $_GET[ $Field ] );
375 }
376 elseif( ( $Mode & COOKIE ) && isset( $_COOKIE[ $Field ] ) )
377 {
378 return( $_COOKIE[ $Field ] );
379 }
380 elseif( ( $Mode & SESSION ) && isset( $_SESSION[ $Field ] ) )
381 {
382 return( $_SESSION[ $Field ] );
383 }
384 elseif( ( $Mode & SERVER ) && isset( $_SERVER[ $Field ] ) )
385 {
386 return( $_SERVER[ $Field ] );
387 }
388 return( false );
389 }
390 catch( Exception $e )
391 {
392 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
393 }
394 }
395
426 private function fetch_data( $Field , $Data , $Mode , &$Return )
427 {
428 try
429 {
430 if( $Mode & KEYS )
431 {
432 $Data = array_keys( $Data );
433 $FilterFunction = create_function( '$e' , "return( strpos( \$e , '$Field' ) === 0 );" );
434 $Data = array_filter( $Data , $FilterFunction );
435 foreach( $Data as $d )
436 {
437 $Return [] = str_replace( $Field , '' , $d );
438 }
439 }
440 else
441 {
442 foreach( $Data as $k => $v )
443 {
444 if( strpos( $k , $Field ) === 0 )
445 {
446 $Return[ str_replace( $Field , '' , $k ) ] = $v;
447 }
448 }
449 }
450 }
451 catch( Exception $e )
452 {
453 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
454 }
455 }
456
491 function get_global( $Field , $Type , $Mode , $DefaultValue = '_throw_exception' )
492 {
493 try
494 {
495 if( $this->Security === false )
496 {
497 $this->Security = get_package( 'security' , 'last' , __FILE__ );
498 }
499 if( $Mode & PREFIX_NAME )
500 {
501 $Data = $this->merge_globals( $Mode );
502 $Return = array();
503
504 $this->fetch_data( $Field , $Data , $Mode , $Return );
505
506 return( $this->Security->get( $Return , $Type ) );
507 }
508
509 if( $Type !== 'set' && ( $Value = $this->handle_get( $Field , $Mode ) ) !== false )
510 {
511 return( $this->Security->get( $Value , $Type ) );
512 }
513 $Data = $this->merge_globals( $Mode );
514 return( $this->handle_default_set( $Data , $Field , $Type , $DefaultValue ) );
515 }
516 catch( Exception $e )
517 {
518 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
519 }
520 }
521
540 function get_all_posted_data()
541 {
542 try
543 {
544 if( $this->Security === false )
545 {
546 $this->Security = get_package( 'security' , 'last' , __FILE__ );
547 }
548
549 return( $this->Security->get( $_POST , 'string' ) );
550 }
551 catch( Exception $e )
552 {
553 $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
554 }
555 }
556 }
557
558 ?>