Public paste
framework startup... stupid lil idea... Oo
By: fruffl | Date: Nov 24 2009 06:02 | Format: PHP | Expires: never | Size: 13.43 KB | Hits: 1396

  1. <?php
  2.  
  3.         define('FRUFFL_ACCESS', true);
  4.         require_once('./FRUFFL/FRUFFL.php');
  5.        
  6.         $FRUFFL = new FRUFFL();
  7.        
  8.  
  9.         $FRUFFL->set_config(new FRUFFL_CONFIG(array(
  10.                                 'FRUFFL_AUTOLOAD' => 'PAGE',
  11.                                 'test' => '123',
  12.                                 'subversion' => 1005,
  13.                                 'revision' => 11.5
  14.                         )));
  15.        
  16.        
  17.        
  18.         echo "<pre>";
  19.         print_r(FRUFFL_REGISTER::getAll());
  20.         print_r($FRUFFL);
  21.  
  22. ?>
  23.  
  24. <?php
  25.  
  26.         if(!defined('FRUFFL_ACCESS')) die("FRUFFL_ACCESS isn't defined.");
  27.  
  28.  
  29.  
  30.  
  31.         class FRUFFL_EXCEPTION EXTENDS EXCEPTION
  32.         {      
  33.        
  34.                 protected static        $errors                         = array(
  35.                                                                                 1010 => 'Unable to set variable &quot;%1$s&quot;. It was already set.',
  36.                                                                                 1100 => 'The constant &quot;%1$s&quot; is already defined.',
  37.                                                                                 1110 => 'Directory &quot;%1$s&quot;, defined in &quot;%2$s&quot;, not found.',
  38.                                                                                 1120 => 'File &quot;%1$s&quot;, defined in &quot;%2$s&quot;, not found.',
  39.                                                                                 1130 => 'The given value &quot;%1$s&quot;, defined in your init_config at key &quot;%2$s&quot;, isn't a valid argument.',
  40.                                                                                 1140 => 'The given key &quot;%1$s&quot;, defined in your init_config, is not a valid config-argument.',
  41.                                                                                
  42.                                                                                 0000 => 'Error in <b>%1$s</b> detected:<br />',
  43.                                                                                 9999 => 'Invalid error-code given: &quot;%1$d&quot;',
  44.                                                                                 );
  45.                
  46.                
  47.                
  48.                
  49.                 final public static function e($error_code, $location)
  50.                 {
  51.                
  52.                         $args = func_get_args();
  53.                         unset($args[0]);
  54.                         unset($args[1]);
  55.                
  56.                         try
  57.                         {
  58.                                 if(array_key_exists($error_code, self::$errors))
  59.                                 {
  60.                                         $error = vsprintf(self::$errors[$error_code],  $args);
  61.                                 }
  62.                                 else
  63.                                 {
  64.                                         throw new FRUFFL_EXCEPTION
  65.                                         (
  66.                                                 self::e
  67.                                                 (
  68.                                                         9999,
  69.                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  70.                                                         $error_code
  71.                                                 ),
  72.                                                 9999
  73.                                         );
  74.                                 }
  75.                         }
  76.                         catch(self $e)
  77.                         {
  78.                                 self::w($e);
  79.                         }
  80.  
  81.                         return sprintf(self::$errors[0], $location['method']).$error;
  82.                                        
  83.                 }
  84.                
  85.                
  86.                
  87.                
  88.                 final public static function w($e)
  89.                 {
  90.                
  91.                         $fire_b = '<div style="background: #ccc; color: #222; font-size: 11px; font-family: monospace; padding: 20px;">%1$s</div>';
  92.                         $fire_l = '<div>%1$s:</div><div style="padding-left: 50px;">%2$s</div>';
  93.                         $fire_t = '<table><tr>%1$s</tr></table>';
  94.                         $fire_r = '<td>%1$s::%2$s</td>';
  95.                                                
  96.                         foreach(($r = $e->getTrace()) as $b)
  97.                         {
  98.                                 $_t = '';
  99.                                 foreach($b as $k => $v)                        
  100.                                         if($k != 'args')
  101.                                         {
  102.                                                 $t .= sprintf($fire_l, $k, $v);
  103.                                         }
  104.                                         else
  105.                                         {                                      
  106.                                                 $_a = '';                                      
  107.                                                 foreach($b['args'] as $s => $a)
  108.                                                         if(is_array($a))
  109.                                                                 foreach($a as $c => $b)
  110.                                                                         $_a .= sprintf($fire_l, $c, nl2br($b));
  111.                                                 $t .= sprintf($fire_l, 'args', $_a);
  112.                                         }
  113.                                        
  114.                                 $t .= sprintf($fire_b, $_t);
  115.                         }
  116.                                
  117.                        
  118.                         echo sprintf($fire_b,
  119.                                         sprintf($fire_l, 'Error-Code', nl2br($e->getCode())).
  120.                                         sprintf($fire_l, 'Line', nl2br($e->getLine())).
  121.                                         sprintf($fire_l, 'File', nl2br($e->getFile())).
  122.                                         sprintf($fire_l, 'Exception', get_class($e)).
  123.                                         sprintf($fire_l, 'Message', nl2br($e->getMessage())).
  124.                                         sprintf($fire_l, 'String', nl2br($e->getTraceAsString())).
  125.                                         sprintf($fire_l, 'Backtrace', $t)
  126.                                         );
  127.                                                
  128.                 }
  129.                
  130.         }              
  131.        
  132.        
  133.        
  134.         class FRUFFL_INIT_EXCEPTION             EXTENDS FRUFFL_EXCEPTION{}
  135.         class FRUFFL_CONFIG_EXCEPTION           EXTENDS FRUFFL_EXCEPTION{}
  136.         class FRUFFL_CONST_EXCEPTION            EXTENDS FRUFFL_EXCEPTION{}
  137.         class FRUFFL_REGISTER_EXCEPTION         EXTENDS FRUFFL_EXCEPTION{}     
  138.        
  139.        
  140.        
  141.        
  142.         abstract class FRUFFL_REGISTER
  143.         {
  144.        
  145.                 protected static                $register                       = array();
  146.                 protected static                $encrypt_key                    = 1;
  147.                 protected static                $encrypt_method                 = 'sha1';
  148.                
  149.                
  150.                
  151.                 final protected static function crypt($key)
  152.                 {
  153.                
  154.                         if(self::$encrypt_key)
  155.                         {
  156.                                 if(function_exists(self::$encrypt_method))
  157.                                 {
  158.                                         $func = self::$encrypt_method;
  159.                                         $crypt = $func($key);
  160.                                         return $crypt;
  161.                                 }                              
  162.                                 return $key;
  163.                         }              
  164.                         return $key;
  165.                
  166.                 }
  167.    
  168.    
  169.                
  170.    
  171.                 final public static function set($key, $value)
  172.                 {
  173.                
  174.                         try
  175.                         {
  176.                                 if (!isset(self::$register[self::crypt($key)]))
  177.                                 {
  178.                                         self::$register[self::crypt($key)] = $value;
  179.                                         return true;
  180.                                 }
  181.                                 else
  182.                                 {
  183.                                         throw new FRUFFL_REGISTER_EXCEPTION
  184.                                         (
  185.                                                 FRUFFL_REGISTER_EXCEPTION::e
  186.                                                 (
  187.                                                         ($ec = 1010),
  188.                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  189.                                                         $key
  190.                                                 ),
  191.                                                 $ec
  192.                                         );
  193.                                 }
  194.                         }
  195.                         catch(FRUFFL_REGISTER_EXCEPTION $e)
  196.                         {
  197.                                 FRUFFL_REGISTER_EXCEPTION::w($e);
  198.                         }
  199.                 }
  200.  
  201.  
  202.  
  203.                
  204.                 final public static function get($key)
  205.                 {
  206.                         if (isset(self::$register[self::crypt($key)]))
  207.                                 return self::$register[self::crypt($key)];
  208.                         return null;
  209.                 }
  210.  
  211.  
  212.  
  213.                
  214.                 final public static function getAll()
  215.                 {
  216.                         return self::$register;
  217.                 }
  218.  
  219.  
  220.  
  221.  
  222.                 final public static function remove($key)
  223.                 {
  224.                         if (isset(self::$register[self::crypt($key)]))
  225.                         {
  226.                                 unset(self::$register[self::crypt($key)]);
  227.                                 return true;
  228.                         }
  229.                         return false;
  230.                 }
  231.                
  232.                
  233.                
  234.                
  235.                 final public static function removeAll()
  236.                 {
  237.                         self::$register = array();
  238.                         return;
  239.                 }
  240.                
  241.         }
  242.        
  243.        
  244.        
  245.        
  246.         INTERFACE _FRUFFL_CONFIG
  247.         {
  248.                 public function __construct(array $init_config = array());
  249.         }
  250.        
  251.        
  252.        
  253.        
  254.         class FRUFFL_CONFIG IMPLEMENTS _FRUFFL_CONFIG
  255.         {
  256.        
  257.                 const                   VERSION                         = 1000;
  258.                
  259.                 protected               $_____valid_config_arguments    = array('int'           => array('subversion'),
  260.                                                                                 'string'        => array('FRUFFL_AUTOLOAD' => array('PAGE','IMAGE','BINICON','CAPTCHA','XML'), 'test' => '123'),
  261.                                                                                 'array'         => array(),
  262.                                                                                 'float'         => array('revision'),
  263.                                                                                 );
  264.                                                                                
  265.                                                                                
  266.                                                                                
  267.                                                                                
  268.                 public function __construct(array $init_config = array('FRUFFL_AUTOLOAD' => 'PAGE'))
  269.                 {
  270.                         if(FRUFFL_REGISTER::get(($__ = 'FRUFFL_EXECUTE_INIT_CONST')))
  271.                                 if($this->init_const())
  272.                                         FRUFFL_REGISTER::remove($__);
  273.                                        
  274.                         $this->init_config($init_config);
  275.                 }
  276.                
  277.                
  278.                
  279.                 protected function init_config(array $i)
  280.                 {
  281.                
  282.                         if(is_array($i))
  283.                         {                      
  284.                                 $c = array();
  285.                                
  286.                                 try
  287.                                 {
  288.                                         foreach($this->_____valid_config_arguments as $o => $p)
  289.                                                 if(is_array($p) && count($p) > 0)
  290.                                                         foreach($p as $k => $v)
  291.                                                                 if(is_string($k))
  292.                                                                 {
  293.                                                                         if(is_array($v))
  294.                                                                         {
  295.                                                                                 if(in_array($i[$k], $v))
  296.                                                                                 {
  297.                                                                                         $c[$o][$k] = $i[$k];
  298.                                                                                         unset($i[$k]);
  299.                                                                                 }
  300.                                                                                 else
  301.                                                                                 {
  302.                                                                                         throw new FRUFFL_CONFIG_EXCEPTION
  303.                                                                                         (
  304.                                                                                                 FRUFFL_CONFIG_EXCEPTION::e
  305.                                                                                                 (
  306.                                                                                                         ($ec = 1130),
  307.                                                                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  308.                                                                                                         $i[$k],
  309.                                                                                                         $k
  310.                                                                                                 ),
  311.                                                                                                 $ec
  312.                                                                                         );
  313.                                                                                 }
  314.                                                                         }
  315.                                                                         else
  316.                                                                         {
  317.                                                                                 if($v == $i[$k])
  318.                                                                                 {
  319.                                                                                         $c[$o][$k] = $i[$k];
  320.                                                                                         unset($i[$k]);
  321.                                                                                 }
  322.                                                                                 else
  323.                                                                                 {
  324.                                                                                         throw new FRUFFL_CONFIG_EXCEPTION
  325.                                                                                         (
  326.                                                                                                 FRUFFL_CONFIG_EXCEPTION::e
  327.                                                                                                 (
  328.                                                                                                         ($ec = 1130),
  329.                                                                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  330.                                                                                                         $i[$k],
  331.                                                                                                         $k
  332.                                                                                                 ),
  333.                                                                                                 $ec
  334.                                                                                         );
  335.                                                                                 }
  336.                                                                         }
  337.                                                                 }
  338.                                                                 else
  339.                                                                 {
  340.                                                                         if(array_key_exists($v, $i))
  341.                                                                         {
  342.                                                                                 $c[$o][$v] = $i[$v];
  343.                                                                                 unset($i[$v]);
  344.                                                                         }
  345.                                                                 }
  346.                                        
  347.                                         if(is_array($i) && (count($i) > 0))
  348.                                                 foreach($i as $k => $v)
  349.                                                         throw new FRUFFL_CONFIG_EXCEPTION
  350.                                                         (
  351.                                                                 FRUFFL_CONFIG_EXCEPTION::e
  352.                                                                 (
  353.                                                                         ($ec = 1140),
  354.                                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  355.                                                                         $k
  356.                                                                 ),
  357.                                                                 $ec
  358.                                                         );
  359.                                 }
  360.                                 catch(FRUFFL_CONFIG_EXCEPTION $e)
  361.                                 {
  362.                                         FRUFFL_CONFIG_EXCEPTION::w($e);
  363.                                 }
  364.  
  365.                                 foreach($c as $o => $p)
  366.                                         foreach($p as $k => $v)
  367.                                         {
  368.                                                 FRUFFL_REGISTER::remove($k);
  369.                                                 switch($o)
  370.                                                 {
  371.                                                         case 'int':
  372.                                                                 $config[$k] = intval($v);
  373.                                                                 FRUFFL_REGISTER::set($k, intval($v));
  374.                                                                 break;
  375.                                                         case 'string':
  376.                                                                 if(is_string($v)) FRUFFL_REGISTER::set($k, $v);
  377.                                                                 break;
  378.                                                         case 'array':
  379.                                                                 if(is_array($v)) FRUFFL_REGISTER::set($k, $v);
  380.                                                                 if(is_string($v)) FRUFFL_REGISTER::set($k, explode(',', str_replace(' ', null, $v)));
  381.                                                                 break;
  382.                                                         case 'float':
  383.                                                                 $config[$k] = FRUFFL_REGISTER::set($k, floatval($v));
  384.                                                                 break;
  385.                                                 }
  386.                                         }
  387.                                        
  388.                                 unset($c);
  389.  
  390.                         }
  391.  
  392.                 }
  393.                
  394.  
  395.                
  396.                
  397.                 protected function init_const()
  398.                 {
  399.                                
  400.                         $c = array(
  401.                                 'FRUFFL_GLOBAL_VERSION'                 =>      self::VERSION,
  402.                                 'FRUFFL_GLOBAL_SYSTEM_ROOT'             =>      rtrim(str_replace(array("/","\"), DIRECTORY_SEPARATOR,
  403.                                                                                         dirname(dirname(__FILE__))), '/\').DIRECTORY_SEPARATOR,
  404.                                 'FRUFFL_GLOBAL_SYSTEM_DIR'              =>      rtrim(str_replace(array("/","\"), DIRECTORY_SEPARATOR,
  405.                                                                                         dirname(__FILE__)), '/\').DIRECTORY_SEPARATOR.self::VERSION.DIRECTORY_SEPARATOR,
  406.                                 'FRUFFL_GLOBAL_ROOT_PAGE'                       =>      rtrim(str_replace(array("/","\"), DIRECTORY_SEPARATOR,
  407.                                                                                         $_SERVER[SCRIPT_FILENAME]), '/\')
  408.                         );
  409.                        
  410.                         $c = array_merge($c, array(
  411.                                 'FRUFFL_DIR_SYSTEM_SOURCE'              =>      $c['FRUFFL_GLOBAL_SYSTEM_DIR']  ."SOURCE"       .DIRECTORY_SEPARATOR,
  412.                                 'FRUFFL_DIR_SYSTEM_CUSTOM'              =>      $c['FRUFFL_GLOBAL_SYSTEM_DIR']  ."CUSTOM"       .DIRECTORY_SEPARATOR,
  413.                                 'FRUFFL_DIR_SYSTEM_MEDIA'                       =>      $c['FRUFFL_GLOBAL_SYSTEM_DIR']  ."MEDIA"        .DIRECTORY_SEPARATOR,
  414.                         ));
  415.                        
  416.                         $c = array_merge($c, array(
  417.                                 'FRUFFL_DIR_SYSTEM_SOURCE_AUTOLOADER'   =>      $c['FRUFFL_DIR_SYSTEM_SOURCE']  ."autoloader"   .DIRECTORY_SEPARATOR,
  418.                                 'FRUFFL_DIR_SYSTEM_SOURCE_BINARIES'     =>      $c['FRUFFL_DIR_SYSTEM_SOURCE']  ."binaries"     .DIRECTORY_SEPARATOR,
  419.                                 'FRUFFL_DIR_SYSTEM_SOURCE_INCLUDES'     =>      $c['FRUFFL_DIR_SYSTEM_SOURCE']  ."includes"     .DIRECTORY_SEPARATOR,
  420.                                 'FRUFFL_DIR_SYSTEM_SOURCE_LIBRARIES'    =>      $c['FRUFFL_DIR_SYSTEM_SOURCE']  ."libraries"    .DIRECTORY_SEPARATOR,
  421.                                 'FRUFFL_DIR_SYSTEM_SOURCE_STATICS'      =>      $c['FRUFFL_DIR_SYSTEM_SOURCE']  ."statics"      .DIRECTORY_SEPARATOR,
  422.                                 'FRUFFL_DIR_SYSTEM_CUSTOM_AUTOLOADER'   =>      $c['FRUFFL_DIR_SYSTEM_CUSTOM']  ."autoloader"   .DIRECTORY_SEPARATOR,
  423.                                 'FRUFFL_DIR_SYSTEM_CUSTOM_BINARIES'     =>      $c['FRUFFL_DIR_SYSTEM_CUSTOM']  ."binaries"     .DIRECTORY_SEPARATOR,
  424.                                 'FRUFFL_DIR_SYSTEM_CUSTOM_INCLUDES'     =>      $c['FRUFFL_DIR_SYSTEM_CUSTOM']  ."includes"     .DIRECTORY_SEPARATOR,
  425.                                 'FRUFFL_DIR_SYSTEM_CUSTOM_LIBRARIES'    =>      $c['FRUFFL_DIR_SYSTEM_CUSTOM']  ."libraries"    .DIRECTORY_SEPARATOR,
  426.                                 'FRUFFL_DIR_SYSTEM_MEDIA_LANGUAGES'     =>      $c['FRUFFL_DIR_SYSTEM_MEDIA']   ."languages"    .DIRECTORY_SEPARATOR,
  427.                                 'FRUFFL_DIR_SYSTEM_MEDIA_TEMPLATES'     =>      $c['FRUFFL_DIR_SYSTEM_MEDIA']   ."templates"    .DIRECTORY_SEPARATOR,
  428.                         ));
  429.                        
  430.                        
  431.                         foreach ($c as $k => $v)
  432.                         {
  433.                        
  434.                                 try
  435.                                 {                              
  436.                                         if(substr($k, 0, 12) == 'FRUFFL_GLOBAL_')
  437.                                         {
  438.                                                 FRUFFL_REGISTER::set($k, $v);
  439.                                         }
  440.                                         else
  441.                                         if(substr($k, 0, 9) == 'FRUFFL_DIR_')
  442.                                         {
  443.                                                 if(is_dir($v))
  444.                                                 {
  445.                                                         FRUFFL_REGISTER::set($k, $v);
  446.                                                 }
  447.                                                 else
  448.                                                 {
  449.                                                         throw new FRUFFL_CONST_EXCEPTION
  450.                                                         (
  451.                                                                 FRUFFL_CONST_EXCEPTION::e
  452.                                                                 (
  453.                                                                         ($ec = 1110),
  454.                                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  455.                                                                         $v,
  456.                                                                         $k
  457.                                                                 ),
  458.                                                                 $ec
  459.                                                         );
  460.                                                 }
  461.                                         }
  462.                                         else
  463.                                         if(substr($k, 0, 10) == 'FRUFFL_FILE_')
  464.                                         {
  465.                                                 if(is_file($v))
  466.                                                 {
  467.                                                         FRUFFL_REGISTER::set($k, $v);
  468.                                                 }
  469.                                                 else
  470.                                                 {
  471.                                                         throw new FRUFFL_CONST_EXCEPTION
  472.                                                         (
  473.                                                                 FRUFFL_CONST_EXCEPTION::e
  474.                                                                 (
  475.                                                                         ($ec = 1120),
  476.                                                                         array('class' => __CLASS__, 'method' => __METHOD__, 'function' => __FUNCTION__),
  477.                                                                         $v,
  478.                                                                         $k
  479.                                                                 ),
  480.                                                                 $ec
  481.                                                         );
  482.                                                 }
  483.                                         }
  484.                                 }
  485.                                 catch(FRUFFL_CONST_EXCEPTION $e)
  486.                                 {
  487.                                         FRUFFL_CONST_EXCEPTION::w($e);
  488.                                 }
  489.                         }
  490.                         return true;   
  491.                        
  492.                 }                      
  493.                
  494.         }
  495.        
  496.        
  497.        
  498.         class FRUFFL
  499.         {
  500.                
  501.                 protected               $config;
  502.                                                                
  503.  
  504.  
  505.  
  506.                 public function __construct()
  507.                 {      
  508.                
  509.                         FRUFFL_REGISTER::set('FRUFFL_EXECUTE_INIT_CONST', true);
  510.  
  511.                         $this->set_config(new FRUFFL_CONFIG(array(
  512.                                                 'FRUFFL_AUTOLOAD' => 'PAGE',
  513.                                                 'test' => '123',
  514.                                                 'subversion' => 1005,
  515.                                                 'revision' => 11.5
  516.                                         )));           
  517.                        
  518.                        
  519.                         if($this->load('shell')) $this->{'__SHELL'} = new SHELL;
  520.                        
  521.                 }
  522.                
  523.                
  524.                
  525.                
  526.                 public function set_config(_FRUFFL_CONFIG $config)
  527.                 {
  528.                         $this->config = $config;
  529.                 }
  530.                
  531.                
  532.                
  533.                
  534.                 public function __get($key)
  535.                 {
  536.                
  537.                         if (!empty($key))
  538.                         {
  539.                                 if(isset($this->$key))
  540.                                 {
  541.                                         return $this->$key;
  542.                                 }
  543.                                 else
  544.                                 {
  545.                                         return FRUFFL_REGISTER::get($key);
  546.                                 }
  547.                         }
  548.                        
  549.                 }
  550.                
  551.                
  552.                
  553.                
  554.                 public function load($class, $type = 'SOURCE')
  555.                 {
  556.                                        
  557.                         $f = ($_ = FRUFFL_REGISTER::get('FRUFFL_DIR_SYSTEM_'.strtoupper($type).'_INCLUDES')).'FRUFFL.%1$s.%2$s.php';
  558.                         if(!$_)
  559.                                 return false;
  560.                        
  561.                         $c = strtolower($class);
  562.                        
  563.                         if(is_file(($_ = sprintf($f, 'file', $c))))
  564.                         {
  565.                                 require_once $_;
  566.                         }
  567.                         if(is_file(($_ = sprintf($f, 'interface', $c))))
  568.                         {
  569.                                 require_once $_;
  570.                         }
  571.                         if(is_file(($_ = sprintf($f, 'package', $c))))
  572.                         {
  573.                                 require_once $_;
  574.                         }
  575.                         if(is_file(($_ = sprintf($f, 'abstract', $c))))
  576.                         {
  577.                                 require_once $_;
  578.                         }
  579.                         if(is_file(($_ = sprintf($f, 'class', $c))))
  580.                         {
  581.                                 require_once $_;
  582.                         }
  583.                        
  584.                         if(class_exists($c)) return true;
  585.                        
  586.                 }
  587.                
  588.                
  589.         }
  590. ?>