ultimix
page_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 $CachedMultyFS = false;
39  var $Security = false;
40 
51  function __construct()
52  {
53  try
54  {
55  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );;
56  $this->Security = get_package( 'security' , 'last' , __FILE__ );
57  }
58  catch( Exception $e )
59  {
60  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
61  }
62  }
63 
74  var $AddLimitations = '1 = 1';
75 
94  function set_add_limitations( $theAddLimitation )
95  {
96  try
97  {
98  if( $this->AddLimitations === '1 = 1' )
99  {
100  $this->AddLimitations = $theAddLimitation;
101  }
102  else
103  {
104  throw( new Exception( '"AddLimitations" was already set' ) );
105  }
106  }
107  catch( Exception $e )
108  {
109  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
110  }
111  }
112 
135  function get_raw_page( $PageName )
136  {
137  try
138  {
139  $PageName = $this->Security->get( $PageName , 'command' );
140 
141  $FileName = dirname( __FILE__ ).'/data/pa_'.$PageName;
142  if( $this->CachedMultyFS->file_exists( $FileName ) )
143  {
144  $RawData = $this->CachedMultyFS->file_get_contents( $FileName , 'cleaned' );
145  }
146  else
147  {
148  $RawData = '';
149  }
150 
151  return( $RawData );
152  }
153  catch( Exception $e )
154  {
155  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
156  }
157  }
158 
182  function parse_raw_page( $RawData )
183  {
184  try
185  {
186  $Appliance = array();
187 
188  $RawData = explode( "\n" , $RawData );
189  $c = count( $RawData );
190 
191  for( $i = 0 ; $i < $c ; $i++ )
192  {
193  $RawDataInfo = explode( '#' , $RawData[ $i ] );
194  $Appliance [] = array(
195  'package' => @$RawDataInfo[ 0 ] , 'package_version' => @$RawDataInfo[ 1 ] ,
196  'options' => @$RawDataInfo[ 2 ] , 'placeholder' => @$RawDataInfo[ 3 ]
197  );
198  }
199 
200  return( $Appliance );
201  }
202  catch( Exception $e )
203  {
204  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
205  }
206  }
207 
235  function get_package_appliance( $PageName , $Parse = true )
236  {
237  try
238  {
239  $RawData = $this->get_raw_page( $PageName );
240 
241  if( $Parse == true )
242  {
243  if( strlen( $RawData ) !== 0 )
244  {
245  return( $this->parse_raw_page( $RawData ) );
246  }
247 
248  return( array() );
249  }
250 
251  return( $RawData );
252  }
253  catch( Exception $e )
254  {
255  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
256  }
257  }
258 
281  function set_package_appliance( $PageName , $Packages )
282  {
283  try
284  {
285  $PageName = $this->Security->get( $PageName , 'command' );
286  $Packages = $this->Security->get( $Packages , 'unsafe_string' );
287 
288  $this->CachedMultyFS->file_put_contents( dirname( __FILE__ ).'/data/pa_'.$PageName , $Packages );
289  }
290  catch( Exception $e )
291  {
292  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
293  }
294  }
295 
322  private function create_object( &$RawData , $PageName )
323  {
324  try
325  {
326  $RawData = explode( '#' , $RawData );
327 
328  if( count( $RawData ) <= 3 )
329  {
330  $RawData[ 3 ] = '';
331  }
332  if( count( $RawData ) <= 4 )
333  {
334  $RawData[ 4 ] = '';
335  $RawData[ 5 ] = '';
336  }
337 
338  return(
339  array( 'title' => $RawData[ 0 ] , 'alias' => $PageName , 'template' => $RawData[ 1 ] ,
340  'template_version' => $RawData[ 2 ] , 'options' => $RawData[ 3 ] ,
341  'keywords' => $RawData[ 4 ] , 'description' => $RawData[ 5 ] )
342  );
343  }
344  catch( Exception $e )
345  {
346  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
347  }
348  }
349 
374  function get_page_description( $PageName )
375  {
376  try
377  {
378  $PageName = $this->Security->get( $PageName , 'command' );
379 
380  if( $this->CachedMultyFS->file_exists( dirname( __FILE__ ).'/data/'.$PageName ) )
381  {
382  $RawData = $this->CachedMultyFS->file_get_contents( dirname( __FILE__ ).'/data/'.$PageName );
383 
384  return( $this->create_object( $RawData , $PageName ) );
385  }
386 
387  return( false );
388  }
389  catch( Exception $e )
390  {
391  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
392  }
393  }
394 
395  //TODO: fetch pages from DB in the get_list_of_pages method
396 
415  function get_list_of_pages()
416  {
417  try
418  {
419  $ListOfPages = array();
420  $MountedStorages = $this->CachedMultyFS->get_mounted_storages();
421  $MountedStorages [] = dirname( __FILE__ ).'/data';
422  foreach( $MountedStorages as $ms )
423  {
424  if( $Handle = @opendir( $ms ) )
425  {
426  while( false !== ( $File = readdir( $Handle ) ) )
427  {
428  $Flag = strpos( $File , 'pa_' ) !== 0 && strpos( $File , 'index.html' ) !== 0;
429 
430  if( is_file( dirname( __FILE__ ).'/data/'.$File ) && $Flag )
431  {
432  $ListOfPages [] = $this->get_page_description( $File );
433  }
434  }
435  closedir( $Handle );
436  }
437  }
438  return( $ListOfPages );
439  }
440  catch( Exception $e )
441  {
442  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
443  }
444  }
445 
472  private function filter_pages( &$ListOfPages , $Limit )
473  {
474  try
475  {
476  $Ret = array();
477  $SearchString = $this->Security->get_gp( 'search_string' , 'string' , '' );
478  $c = 0;
479  foreach( $ListOfPages as $i => $l )
480  {
481  if( $SearchString !== '' && !( strpos( $l[ 'title' ] , $SearchString ) !== false ||
482  strpos( $l[ 'alias' ] , $SearchString ) !== false ||
483  strpos( $l[ 'template' ] , $SearchString ) !== false ||
484  strpos( $l[ 'template_version' ] , $SearchString ) !== false ) )
485  {
486  continue;
487  }
488  if( $c < $Limit )
489  {
490  $Ret [] = $l;
491  $c++;
492  }
493  }
494  return( $Ret );
495  }
496  catch( Exception $e )
497  {
498  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
499  }
500  }
501 
536  function select( $Start , $Limit , $Field = false , $Order = false , $Condition = false )
537  {
538  try
539  {
540  $ListOfPages = $this->get_list_of_pages();
541 
542  if( $Order !== false )
543  {
544  $SortSign = $Order === 'ascending' ? '<' : '>';
545  $SortFunc = create_function(
546  '$a , $b' , "if( \$a[ '$Field' ] == \$b[ '$Field' ] )".
547  "return(0);return( \$a[ '$Field' ] $SortSign \$b[ '$Field' ] ? -1 : 1 );"
548  );
549  usort( $ListOfPages , $SortFunc );
550  }
551 
552  return( $this->filter_pages( $ListOfPages , $Limit ) );
553  }
554  catch( Exception $e )
555  {
556  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
557  }
558  }
559 
582  function page_exists( $PageName )
583  {
584  try
585  {
586  $PageName = $this->Security->get( $PageName , 'command' );
587 
588  return( $this->CachedMultyFS->file_exists( dirname( __FILE__ )."/data/".$PageName ) );
589  }
590  catch( Exception $e )
591  {
592  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
593  }
594  }
595 
618  private function compile_create_data( &$Record )
619  {
620  try
621  {
622  $Title = $this->Security->get( get_field( $Record , 'page_title' ) , 'string' );
623  $Template = $this->Security->get( get_field( $Record , 'page_template' ) , 'string' );
624  $TemplateVersion = get_field( $Record , 'page_template_version' , 'last' );
625  $TemplateVersion = $this->Security->get( $TemplateVersion , 'string' );
626 
627  return( "$Title#$Template#$TemplateVersion#$Settings" );
628  }
629  catch( Exception $e )
630  {
631  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
632  }
633  }
634 
657  function create( &$Record )
658  {
659  try
660  {
661  $PageName = $this->Security->get( get_field( $Record , 'page_alias' ) , 'command' );
662 
663  if( $this->page_exists( $PageName ) )
664  {
665  return( $PageName );
666  }
667 
668  $Page = $this->compile_create_data( $Record );
669 
670  $this->CachedMultyFS->file_put_contents( dirname( __FILE__ )."/data/".$PageName , $Page );
671  $this->CachedMultyFS->file_put_contents( dirname( __FILE__ )."/data/pa_".$PageName , '' );
672 
673  return( $PageName );
674  }
675  catch( Exception $e )
676  {
677  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
678  }
679  }
680 
715  function update( $PageName , $Title , $Template , $TemplateVersion , $PredefinedPackages )
716  {
717  try
718  {
719  $PageName = $this->Security->get( $PageName , 'command' );
720  $Page = $this->get_page_description( $PageName );
721 
722  $Title = $this->Security->get( $Title , 'string' );
723  $Template = $this->Security->get( $Template , 'string' );
724  $TemplateVersion = $this->Security->get( $TemplateVersion , 'command' );
725  $PredefinedPackages = $this->Security->get( $PredefinedPackages , 'string' );
726 
727  $Path = dirname( __FILE__ )."/data/$PageName";
728  $this->CachedMultyFS->file_put_contents( $Path , "$Title#$Template#$TemplateVersion#$Options" );
729  }
730  catch( Exception $e )
731  {
732  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
733  }
734  }
735 
754  function delete( $PageNames )
755  {
756  try
757  {
758  $PageNames = explode( ',' , $PageNames );
759 
760  foreach( $PageNames as $i => $PageName )
761  {
762  $PageName = $this->Security->get( $PageName , 'command' );
763 
764  @unlink( dirname( __FILE__ ).'/data/'.$PageName );
765  @unlink( dirname( __FILE__ ).'/data/pa_'.$PageName );
766 
767  $EventManager = get_package( 'event_manager' , 'last' , __FILE__ );
768  $EventManager->trigger_event( 'on_after_delete_page' , array( 'page_name' => $PageName ) );
769  }
770  }
771  catch( Exception $e )
772  {
773  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
774  }
775  }
776 
811  function add_package( $PageName , $PackageName , $PackageVersion , $Params , $PlaceHolder )
812  {
813  try
814  {
815  $PageName = $this->Security->get( $PageName , 'command' );
816 
817  $CachedFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
818  $CachedFS->file_put_contents(
819  dirname( __FILE__ ).'/data/pa_'.$PageName ,
820  $PackageName.'#'.$PackageVersion.'#'.$Params.'#'.$PlaceHolder."\r\n" , FILE_APPEND
821  );
822  }
823  catch( Exception $e )
824  {
825  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
826  }
827  }
828 
851  function select_list( $PageNames )
852  {
853  try
854  {
855  if( is_string( $PageNames ) )
856  {
857  $PageNames = explode( ',' , $PageNames );
858  }
859 
860  $Pages = array();
861 
862  foreach( $PageNames as $i => $PageName )
863  {
864  $Pages [] = $this->get_page_description( $PageName );
865  }
866 
867  return( $Pages );
868  }
869  catch( Exception $e )
870  {
871  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
872  }
873  }
874  }
875 
876 ?>