| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | class Ajde_Core_Autoloader_Combinator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     public static function generate($class, $extends, $source_class, $source_extends) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |         $reflector = new ReflectionClass($source_class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         $source_contents = $reflector->getFileName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         $source_contents = file_get_contents($source_contents); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         $contents = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |             '<?php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |             '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             $source_contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $contents = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             "abstract class $source_class ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             "abstract class $class ", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             $contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $contents = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             " extends $source_extends", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             " extends $extends", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $return = eval($contents); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         if ($return === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |             var_dump(self::php_syntax_error($contents)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             throw new Ajde_Exception('Dynamic class creation of '.$class.' failed'); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @see http://stackoverflow.com/a/3224985/938297 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Check the syntax of some PHP code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $code PHP code to check. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return bool|array If false, then check was successful, otherwise an array(message,line) of errors is | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      *                    returned. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public static function php_syntax_error($code) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $braces = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $inString = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach (token_get_all('<?php '.$code) as $token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             if (is_array($token)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 switch ($token[0]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                     case T_CURLY_OPEN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                     case T_DOLLAR_OPEN_CURLY_BRACES: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                     case T_START_HEREDOC: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                         ++$inString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     case T_END_HEREDOC: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                         --$inString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                         break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 if ($inString & 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     switch ($token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         case '`': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         case '\'': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         case '"': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                             --$inString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     switch ($token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                         case '`': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                         case '\'': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                         case '"': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                             ++$inString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                         case '{': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                             ++$braces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                         case '}': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                             if ($inString) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                                 --$inString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                                 --$braces; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                                 if ($braces < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                                     break 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                             break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $inString = @ini_set('log_errors', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $token = @ini_set('display_errors', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ob_start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $braces || $code = "if(0){{$code}\n}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if (eval($code) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             if ($braces) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 $braces = PHP_INT_MAX; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 false !== strpos($code, CR) && $code = strtr(str_replace(CRLF, LF, $code), CR, LF); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $braces = substr_count($code, LF); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $code = ob_get_clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $code = strip_tags($code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             if (preg_match("'syntax error, (.+) in .+ on line \d+)$'s", $code, $code)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $code[2] = (int) $code[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 $code = $code[2] <= $braces | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     ? [$code[1], $code[2]] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                     : ['unexpected $end'.substr($code[1], 14), $braces]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $code = ['syntax error', 0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |             ob_end_clean(); | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |             $code = false; | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |         @ini_set('display_errors', $token); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |         @ini_set('log_errors', $inString); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         return $code; | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  |