ultimix
permit_access.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 
27 
38  var $NativeTable = '`umx_permit`';
39 
50  var $CachedMultyFS = false;
51  var $Database = false;
52  var $DatabaseAlgorithms = false;
53  var $Link = false;
54  var $LinkDictionary = false;
55  var $Security = false;
56  var $SecurityParser = false;
57  var $UserAlgorithms = false;
58 
69  var $PermitsCache = array();
70 
85  function __construct()
86  {
87  try
88  {
89  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
90  $this->Database = get_package( 'database' , 'last' , __FILE__ );
91  $this->DatabaseAlgorithms = get_package( 'database::database_algorithms' , 'last' , __FILE__ );
92  $this->Link = get_package( 'link' , 'last' , __FILE__ );
93  $this->LinkDictionary = get_package( 'link::link_dictionary' , 'last' , __FILE__ );
94  $this->Security = get_package( 'security' , 'last' , __FILE__ );
95  $this->SecurityParser = get_package( 'security::security_parser' , 'last' , __FILE__ );
96  $this->UserAlgorithms = get_package( 'user::user_algorithms' , 'last' , __FILE__ );
97  }
98  catch( Exception $e )
99  {
100  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
101  }
102  }
103 
114  var $AddLimitations = '1 = 1';
115 
126  function set_add_limitations( $theAddLimitation )
127  {
128  try
129  {
130  if( $this->AddLimitations === '1 = 1' )
131  {
132  $this->AddLimitations = $theAddLimitation;
133  }
134  else
135  {
136  throw( new Exception( '"AddLimitations" was already set' ) );
137  }
138  }
139  catch( Exception $e )
140  {
141  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
142  }
143  }
144 
167  function unsafe_select( $Condition = '1 = 1' )
168  {
169  try
170  {
171  $this->Database->query_as( DB_OBJECT );
172 
173  $Condition = "( $this->AddLimitations ) AND $Condition";
174  $Records = $this->Database->select( '*' , $this->NativeTable , $Condition );
175 
176  foreach( $Records as $k => $v )
177  {
178  $Records[ $k ]->permit = htmlspecialchars_decode( $Records[ $k ]->permit , ENT_QUOTES );
179  $Records[ $k ]->comment = htmlspecialchars_decode( $Records[ $k ]->comment , ENT_QUOTES );
180  }
181 
182  return( $Records );
183  }
184  catch( Exception $e )
185  {
186  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
187  }
188  }
189 
212  function get_permit_by_name( $Permit )
213  {
214  try
215  {
216  $Permit = $this->Security->get( $Permit , 'command' );
217 
218  $Items = $this->unsafe_select( "( $this->AddLimitations ) AND permit LIKE '$Permit'" );
219 
220  if( count( $Items ) == 0 )
221  {
222  throw( new Exception( "Permit $Permit was not found" ) );
223  }
224 
225  return( $Items[ 0 ] );
226  }
227  catch( Exception $e )
228  {
229  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
230  }
231  }
232 
271  function select( $Start = false , $Limit = false ,
272  $Field = false , $Order = false , $Condition = '1 = 1' )
273  {
274  try
275  {
276  $Condition = $this->DatabaseAlgorithms->select_condition(
277  $Start , $Limit , $Field , $Order , $Condition , $this->NativeTable
278  );
279 
280  return( $this->unsafe_select( $Condition ) );
281  }
282  catch( Exception $e )
283  {
284  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
285  }
286  }
287 
306  function create( $Record )
307  {
308  try
309  {
310  $Record = $this->SecurityParser->parse_parameters( $Record , 'permit:command;comment:string' );
311 
312  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
313 
314  $id = $this->DatabaseAlgorithms->create( $this->NativeTable , $Fields , $Values );
315  }
316  catch( Exception $e )
317  {
318  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
319  }
320  }
321 
340  function delete( $id )
341  {
342  try
343  {
344  $id = $this->Security->get( $id , 'integer_list' );
345 
346  $this->Database->delete( $this->NativeTable , "( $this->AddLimitations ) AND id IN ( $id )" );
347 
348  $this->Database->commit();
349  }
350  catch( Exception $e )
351  {
352  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
353  }
354  }
355 
378  function select_list( $id )
379  {
380  try
381  {
382  $id = $this->Security->get( $id , 'integer_list' );
383 
384  return( $this->unsafe_select( $this->NativeTable.".id IN ( $id )" ) );
385  }
386  catch( Exception $e )
387  {
388  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
389  }
390  }
391 
414  function update( $id , $Record )
415  {
416  try
417  {
418  $id = $this->Security->get( $id , 'integer_list' );
419 
420  $Record = $this->SecurityParser->parse_parameters(
421  $Record , 'permit:command;comment:string' , 'allow_not_set'
422  );
423 
424  list( $Fields , $Values ) = $this->DatabaseAlgorithms->compile_fields_values( $Record );
425 
426  if( isset( $Fields[ 0 ] ) )
427  {
428  $Condition = "( $this->AddLimitations ) AND id IN ( $id )";
429 
430  $this->Database->update( $this->NativeTable , $Fields , $Values , $Condition );
431 
432  $this->Database->commit();
433  }
434  }
435  catch( Exception $e )
436  {
437  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
438  }
439  }
440 
476  function get_permits_for_object( $id , $Type , $Default )
477  {
478  try
479  {
480  $id = $this->Security->get( $id , 'integer' );
481  $Type = $this->Security->get( $Type , 'command' );
482 
483  $Links = $this->Link->get_links( $id , false , $Type , 'permit' );
484 
485  if( isset( $Links[ 0 ] ) === false )
486  {
487  return( $Default );
488  }
489  else
490  {
491  $ids = get_field_ex( $Links , 'object2_id' );
492 
493  $Permits = $this->select_list( implode( ',' , $ids ) );
494 
495  return( get_field_ex( $Permits , 'permit' ) );
496  }
497  }
498  catch( Exception $e )
499  {
500  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
501  }
502  }
503 
526  function get_permits_for_user_group( $uid )
527  {
528  try
529  {
530  $uid = ( $uid === false ) ? $this->UserAlgorithms->get_id() : $this->Security->get( $uid , 'integer' );
531 
532  $Type = $this->LinkDictionary->get_link_type( 'user' , 'group' );
533  $this->Database->query_as( DB_OBJECT );
534  $Items = $this->Database->select(
535  '`umx_group`.id' , '`umx_group` , umx_link' ,
536  "umx_group.id = umx_link.object2_id AND umx_link.object1_id = $uid AND type = $Type"
537  );
538 
539  $Permits = array();
540 
541  foreach( $Items as $i )
542  {
543  $Permits = array_merge( $Permits , $this->get_permits_for_object( $i->id , 'group' , array() ) );
544  }
545 
546  return( array_unique( $Permits ) );
547  }
548  catch( Exception $e )
549  {
550  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
551  }
552  }
553 
580  function add_permit_for_object( $Permit , $Object , $ObjectType = 'page' )
581  {
582  try
583  {
584  $this->PermitsCache = array();
585 
586  $Permit = $this->Security->get( $Permit , 'command' );
587  $Object = $this->Security->get( $Object , 'string' );
588  $ObjectType = $this->Security->get( $ObjectType , 'command' );
589 
590  $Permits = array();
591 
592  if( $ObjectType == 'user' || $ObjectType == 'group' || $ObjectType == 'page' )
593  {
594  $Permit = $this->unsafe_select( "permit LIKE '$Permit'" );
595  $Permit = $Permit[ 0 ];
596  $this->Link->create_link( $Object , $Permit->id , $ObjectType , 'permit' , true );
597  return;
598  }
599 
600  throw( new Exception( "Undefined \"$ObjectType\"" ) );
601  }
602  catch( Exception $e )
603  {
604  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
605  }
606  }
607 
634  function set_permit_for_object( $Permit , $Object , $ObjectType = 'page' )
635  {
636  try
637  {
638  $this->PermitsCache = array();
639 
640  $Permit = $this->Security->get( $Permit , 'command' );
641  $Permit = $this->unsafe_select( "permit LIKE '$Permit'" );
642  if( isset( $Permit[ 0 ] ) === false )
643  {
644  throw( new Exception( "Permit \"$Permit\" was not found" ) );
645  }
646 
647  $Permit = $Permit[ 0 ];
648  $Object = $this->Security->get( $Object , 'string' );
649  $ObjectType = $this->Security->get( $ObjectType , 'command' );
650 
651  $this->Link->create_link( $Object , get_field( $Permit , 'id' ) , $ObjectType , 'permit' , true );
652  }
653  catch( Exception $e )
654  {
655  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
656  }
657  }
658 
685  function toggle_permit_for_object( $Permit , $Object , $ObjectType = 'page' )
686  {
687  try
688  {
689  $this->PermitsCache = array();
690 
691  $Permit = $this->Security->get( $Permit , 'command' );
692  $Permit = $this->unsafe_select( "permit LIKE '$Permit'" );
693  if( isset( $Permit[ 0 ] ) === false )
694  {
695  throw( new Exception( "Permit \"$Permit\" was not found" ) );
696  }
697 
698  $Permit = $Permit[ 0 ];
699  $Object = $this->Security->get( $Object , 'string' );
700  $ObjectType = $this->Security->get( $ObjectType , 'command' );
701 
702  if( is_array( $Object ) === false )
703  {
704  $Object = array( $Object );
705  }
706 
707  $this->PermitAccessUtilities->toggle_permits();
708  }
709  catch( Exception $e )
710  {
711  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
712  }
713  }
714 
741  function delete_permit_for_object( $Permit , $Object , $ObjectType = 'page' )
742  {
743  try
744  {
745  $this->PermitsCache = array();
746 
747  $Permit = $this->Security->get( $Permit , 'command' );
748  $Permit = $this->unsafe_select( "permit LIKE '$Permit'" );
749  if( isset( $Permit[ 0 ] ) === false )
750  {
751  throw( new Exception( "Permit \"$Permit\" was not found" ) );
752  }
753 
754  $Permit = $Permit[ 0 ];
755  $Object = $this->Security->get( $Object , 'string' );
756  $ObjectType = $this->Security->get( $ObjectType , 'command' );
757 
758  $this->Link->delete_link( $Object , get_field( $Permit , 'id' ) , $ObjectType , 'permit' );
759  }
760  catch( Exception $e )
761  {
762  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
763  }
764  }
765 
788  function get_permits_for_page( $Object )
789  {
790  try
791  {
792  $Object = $this->Security->get( $Object , 'string' );
793 
794  if( $this->CachedMultyFS->file_exists( dirname( __FILE__ )."/data/p$Object" ) )
795  {
796  $Permits = $this->CachedMultyFS->file_get_contents( dirname( __FILE__ )."/data/p$Object" );
797  return( explode( ',' , $Permits ) );
798  }
799  else
800  {
801  return( array( 'admin' ) );
802  }
803  }
804  catch( Exception $e )
805  {
806  $Args = func_get_args();_throw_exception_object( __METHOD__ , $Args , $e );
807  }
808  }
809  }
810 
811 ?>