00001 <?php
00002
00003 define( "START_TAG" , "/"."*NODEBUG{*"."/" );
00004 define( "END_TAG" , "/"."*}NODEBUG*"."/" );
00005
00015 class PhpCompiler
00016 {
00024 var $_UseMakeLibrary;
00025
00033 var $_UseNoDebugCode;
00034
00042 var $_UsePhpScrub;
00043
00053 function & PhpCompiler()
00054 {
00055 $this->_UseMakeLibrary = FALSE;
00056 $this->_UseNoDebugCode = TRUE;
00057 $this->_UsePhpScrub = TRUE;
00058 return($this);
00059 }
00060
00074 function ChangeMode($NewModes=array())
00075 {
00076 $name_conversion = array(
00077 "library" => "_UseMakeLibrary",
00078 "nodebug" => "_UseNoDebugCode",
00079 "whitespace" => "_UsePhpScrub",
00080 );
00081
00082 if(!is_array($NewModes))
00083 {
00084 return(FALSE);
00085 }
00086
00087 foreach($NewModes as $Name => $Value)
00088 {
00089 if(!isset($name_conversion[$Name]))
00090 {
00091 return(FALSE);
00092 }
00093
00094 if(!is_bool($Value))
00095 {
00096 return(FALSE);
00097 }
00098
00099 $Name = $name_conversion[$Name];
00100 $this->$Name = $Value;
00101 }
00102
00103 return(TRUE);
00104 }
00105
00116 function GetModes()
00117 {
00118 return(array(
00119 array(
00120 "Name" => "library",
00121 "Default" => FALSE,
00122 "Description" =>
00123 "Remove extra newlines and php tags"
00124 ),
00125 array(
00126 "Name" => "nodebug",
00127 "Default" => TRUE,
00128 "Description" =>
00129 "Remove /*NODEBUG{*/.../*}NODEBUG*/ blocks"
00130 ),
00131 array(
00132 "Name" => "whitespace",
00133 "Default" => TRUE,
00134 "Description" =>
00135 "Pass through 'php -w'"
00136 ),
00137 ));
00138 }
00139
00149 function GetErrors()
00150 {
00151 return(array());
00152 }
00153
00167 function Compile($Input)
00168 {
00169 if(!is_string($Input))
00170 {
00171 return("");
00172 }
00173
00174 if($this->_UseNoDebugCode)
00175 {
00176 $Input = $this->NoDebugCode($Input);
00177 }
00178 if($this->_UsePhpScrub)
00179 {
00180 $Input = $this->PhpScrub($Input);
00181 }
00182 if($this->_UseMakeLibrary)
00183 {
00184 $Input = $this->MakeLibrary($Input);
00185 }
00186
00187 return($Input);
00188 }
00189
00219 function MakeLibrary($Input)
00220 {
00221 $replace = array(
00222 '/\n/s' => '' , # Remove all newlines
00223 '/\<\?'.'php/s' => '' , # Remove all open tags
00224 '/\?\>/s' => '' , # Remove all close tags
00225 '/^/s' => '<?'.'php ' , # Add beginning open tag
00226 '/$/s' => ' ?'.'>' , # Add ending close tag
00227 );
00228
00229 $Input = preg_replace(
00230 array_keys($replace),
00231 array_values($replace),
00232 $Input
00233 );
00234
00235 return($Input);
00236 }
00237
00259 function NoDebugCode($Input)
00260 {
00261 $strlen = strlen($Input);
00262 $inblock = FALSE;
00263 $return = "";
00264 $block = "";
00265 for($x = 0; $x < $strlen; $x++)
00266 {
00267 if(START_TAG == substr($Input, $x, strlen(START_TAG)))
00268 {
00269 if($block)
00270 {
00271 $return .= $block;
00272 $block = "";
00273 }
00274
00275 $inblock = TRUE;
00276 }
00277 if(END_TAG == substr($Input, $x, strlen(END_TAG)))
00278 {
00279 if($inblock)
00280 {
00281 $inblock = FALSE;
00282 $block = "";
00283 $x += strlen(END_TAG);
00284 }
00285 }
00286 if(!$inblock)
00287 {
00288 $return .= $Input[$x];
00289 }
00290 else
00291 {
00292 $block .= $Input[$x];
00293 }
00294 }
00295
00296 if($return !== $Input)
00297 {
00298 return($this->NoDebugCode($return));
00299 }
00300
00301 return($return);
00302 }
00303
00318 function PhpScrub($Input)
00319 {
00320 $Input = escapeshellarg($Input);
00321 $Command = "echo $Input | php -w ";
00322 $Return = shell_exec($Command);
00323 return($Return);
00324 }
00325 }
00326
00327 return("PhpCompiler");
00328
00329 ?>