Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.87 KB | None | 0 0
  1. <?php
  2.  
  3.     namespace YetAnotherPanel;
  4.  
  5.     final class Engine extends Core implements i_Engine
  6.     {
  7.        
  8.         private $mySQL_Server = '',
  9.                 $mySQL_Username = '',
  10.                 $mySQL_Password = '',
  11.                 $mySQL_Database = '',
  12.                 $mySQL_Link = '',
  13.                 $mySQL_Connected = false;
  14.                
  15.         protected static $params = array(), $queryCount = 0, $rawQuery = '';
  16.        
  17.         final public function __construct( array $dbArray )
  18.         {
  19.            
  20.             $this->mySQL_Server = $dbArray['Server'];
  21.             $this->mySQL_Username = $dbArray['Username'];
  22.             $this->mySQL_Password = $dbArray['Password'];
  23.             $this->mySQL_Database = $dbArray['Database'];
  24.            
  25.             try
  26.             {
  27.  
  28.                 $this->connect();
  29.                
  30.             }
  31.             catch( Error $e ){}
  32.            
  33.         }
  34.        
  35.         public function randomChars( $len = 20, $isNumeric = false, $withSpecials = true )
  36.         {
  37.            
  38.             if( $isNumeric )
  39.             {
  40.                
  41.                 $poss = '1234567890';
  42.                
  43.             }
  44.             else
  45.             {
  46.                
  47.                 $poss = '1234567890QWERTYUIOASDFGHJKLZXCVBNMqwertyuioasdfghjklzxcvbnm';
  48.                
  49.                 if( $withSpecials )
  50.                 {
  51.                    
  52.                     $poss .= '!£$%^&*';
  53.                    
  54.                 }
  55.                
  56.             }
  57.            
  58.             for( $i = 1; $i <= $len; $i++ )
  59.             {
  60.                
  61.                 $randomChars .= substr($poss, rand( 1, strlen( $poss ) - 1 ), 1 );
  62.                                
  63.             }
  64.            
  65.             return $randomChars;
  66.            
  67.         }
  68.        
  69.         final public function connect()
  70.         {
  71.            
  72.             if( !$this->mySQL_Link = @$this->functions['connect']( $this->mySQL_Server, $this->mySQL_Username, $this->mySQL_Password ) )
  73.             {
  74.                
  75.                 throw new Error('Can not connect to mySQL server. <br /><br />'. $this->functions['error'] );
  76.                
  77.             }
  78.            
  79.             if( !@$this->functions['select_db']( $this->mySQL_Database, $this->mySQL_Link ) )
  80.             {
  81.                
  82.                 throw new Error('Can not connect to database. <br /><br />'. $this->functions['error'] );
  83.                
  84.             }
  85.            
  86.             unset( $mySQL_Server, $mySQL_Username, $mySQL_Password, $mySQL_Database );
  87.            
  88.             $this->mySQL_Connected = true;
  89.            
  90.         }
  91.        
  92.         final public static function rawQuery($Query, $params = array() )
  93.         {
  94.                        
  95.             $find = '/\{([0-9]+)(,)?( )?([ibds]{1})?\}/s';
  96.                        
  97.             if( preg_match( $find, $Query ) )
  98.             {
  99.                
  100.                 self::$params = $params;
  101.                
  102.                 while( (bool) (int) preg_match( $find, $Query ) )
  103.                 {
  104.                    
  105.                     $Query = preg_replace_callback(
  106.                
  107.                                         $find,
  108.                                         create_function('$ma', 'return YetAnotherPanel\Engine::returnParam( $ma[ 1 ], $ma[ 4 ] );' ),
  109.                                         $Query
  110.                
  111.                                     );
  112.                    
  113.                 }
  114.            
  115.                
  116.             }
  117.            
  118.             self::$rawQuery = self::filterPreParams( $Query );
  119.            
  120.             try
  121.             {
  122.                
  123.                 $Query = @mysql_query( self::$rawQuery );
  124.                    
  125.                 if( @mysql_error() )
  126.                 {
  127.                        
  128.                     throw new Error('Could not execute mySQL query. <br /><br /><strong>Raw Query</strong>: <br /><br />'.self::$rawQuery . ' <br /><br /><strong>Error</strong>: <br /><br />' . mysql_error() );
  129.                        
  130.                 }
  131.                    
  132.             }
  133.             catch( Error $e ){}
  134.                
  135.             return $Query;
  136.            
  137.         }
  138.        
  139.         final private static function filterPreParams( $query )
  140.         {
  141.            
  142.             $find = array( '{prefix}', '{now}' );
  143.  
  144.             $replace = array( 'yap_', 'UNIX_TIMESTAMP()' );
  145.            
  146.             return str_replace( $find, $replace, $query );
  147.            
  148.         }
  149.        
  150.         final public static function returnParam( $arrayNumber = '', $arrayType = 's' )
  151.         {
  152.            
  153.             $param = self::$params[ $arrayNumber ];
  154.                        
  155.             switch( $arrayType )
  156.             {
  157.                
  158.                 default:
  159.                 case 's':
  160.                    
  161.                     $param = mysql_real_escape_string( $param );
  162.                    
  163.                     $param = htmlentities( $param );
  164.                    
  165.                 break;
  166.                
  167.                 case 'i':
  168.                    
  169.                     if( !is_numeric( $param ) )
  170.                     {
  171.                        
  172.                         return '"0"';
  173.                        
  174.                     }
  175.                    
  176.                 break;
  177.                
  178.                 case 'b':
  179.                    
  180.                     if( !is_bool( $param ) )
  181.                     {
  182.                        
  183.                         return '"0"';
  184.                        
  185.                     }
  186.                    
  187.                 break;
  188.                
  189.                 case 'd':
  190.                    
  191.                     if( !is_double( $param ) )
  192.                     {
  193.                        
  194.                         return '"0"';
  195.                        
  196.                     }
  197.                    
  198.                    
  199.                 break;
  200.                
  201.             }
  202.            
  203.             return '"'. self::$params[ $arrayNumber ] . '"';
  204.            
  205.         }
  206.        
  207.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement