function testErreurs() { foreach ($this->errortestcase as $source => $datas) { try { $proc = new jPreProcessor(); $res = $proc->parseFile(PP_DATA_DIR . $source); $this->fail($source . ' : pas d\'erreur !'); } catch (jExceptionPreProc $e) { $err = false; if ($e->getCode() != $datas[0]) { $this->fail($source . ' : mauvais code erreur (' . $e->getCode() . ')'); $err = true; } if ($e->sourceFilename != PP_DATA_DIR . $datas[1]) { $s = substr($e->sourceFilename, -strlen(PP_DATA_DIR . $datas[1])); if ($s != PP_DATA_DIR . $datas[1]) { $this->fail($source . ' : mauvais fichier source indiqué (' . $e->sourceFilename . ')'); $err = true; } } if ($e->sourceLine != $datas[2]) { $this->fail($source . ' : mauvais numero de ligne du source (' . $e->sourceLine . '!=' . $datas[2] . ')'); $err = true; } if (!$err) { $this->pass($source . ' : ok'); } } catch (Exception $e) { $this->fail($source . ' : exception inattendue'); } } }
protected function readInclude($currentFilename, $currentLine, $path, $raw, $options = array()) { if (!($path[0] == '/' || preg_match('/^\\w\\:\\.+$/', $path))) { $path = realpath(dirname($currentFilename) . '/' . $path); if ($path == '') { throw new jExceptionPreProc($currentFilename, $currentLine, self::ERR_INVALID_FILENAME, $path); } } if (file_exists($path) && !is_dir($path)) { if ($raw) { $tline = file_get_contents($path); } else { $preproc = new jPreProcessor(); $preproc->_doSaveVariables = false; $preproc->setVars($this->_variables); $tline = $preproc->parseFile($path); $this->_variables = $preproc->_variables; $preproc = null; } } else { throw new jExceptionPreProc($currentFilename, $currentLine, self::ERR_INVALID_FILENAME, $path); } if (count($options)) { $tline = $this->applyIncludeOptions($tline, $options); } return $tline; }
exit(1); } array_shift($_SERVER['argv']); // shift the script name $restrictedDirectory = ''; $options = array('verbose' => false); if (substr($_SERVER['argv'][0], 0, 1) == '-') { $sw = substr(array_shift($_SERVER['argv']), 1); $options['verbose'] = strpos($sw, 'v') !== false; if (strpos($sw, 'd') !== false) { $restrictedDirectory = array_shift($_SERVER['argv']); } } list($sourcefile, $distfile) = $_SERVER['argv']; if ($restrictedDirectory != '') { $s = realpath($sourcefile); if (strpos($s, $restrictedDirectory) !== 0) { exit(1); } } try { $proc = new jPreProcessor(); $proc->setVars($_SERVER); $dist = $proc->parseFile($sourcefile); } catch (Exception $e) { echo $e; exit(1); } jBuildUtils::createDir(dirname($distfile)); file_put_contents($distfile, $dist); exit(0);
/** * read the given manifest file and copy files * @param string $ficlist manifest file name * @param string $sourcepath main directory where it reads files * @param string $distpath main directory were files are copied */ public static function process($ficlist, $sourcepath, $distpath, $preprocvars, $preprocmanifest = false) { $stripcomment = self::$stripComment; $verbose = self::$verbose; $preproc = new jPreProcessor(); $sourcedir = jBuildUtils::normalizeDir($sourcepath); $distdir = jBuildUtils::normalizeDir($distpath); if ($preprocmanifest) { $preproc->setVars($preprocvars); try { $content = $preproc->parseFile($ficlist); } catch (Exception $e) { throw new Exception("cannot preprocess the manifest file " . $ficlist . " (" . $e . ")\n"); } $script = explode("\n", $content); } else { $script = file($ficlist); } $currentdestdir = ''; $currentsrcdir = ''; foreach ($script as $nbline => $line) { $nbline++; if (preg_match(';^(cd|sd|dd|\\*|!|\\*!|c|\\*c|cch)?\\s+([a-zA-Z0-9\\/.\\-_]+)\\s*(?:\\(([a-zA-Z0-9\\%\\/.\\-_]*)\\))?\\s*$;m', $line, $m)) { if ($m[1] == 'dd') { $currentdestdir = jBuildUtils::normalizeDir($m[2]); jBuildUtils::createDir($distdir . $currentdestdir, self::$usedVcs); } elseif ($m[1] == 'sd') { $currentsrcdir = jBuildUtils::normalizeDir($m[2]); } elseif ($m[1] == 'cd') { $currentsrcdir = jBuildUtils::normalizeDir($m[2]); $currentdestdir = jBuildUtils::normalizeDir($m[2]); jBuildUtils::createDir($distdir . $currentdestdir, self::$usedVcs); } else { $doPreprocessing = strpos($m[1], '*') !== false; $doCompression = strpos($m[1], 'c') !== false && $m[1] != 'cch' || $stripcomment && strpos($m[1], '!') === false; if ($m[2] == '') { throw new Exception("{$ficlist} : file required on line {$nbline} \n"); } if (!isset($m[3]) || $m[3] == '') { $m[3] = $m[2]; } $destfile = $distdir . $currentdestdir . $m[3]; $sourcefile = $sourcedir . $currentsrcdir . $m[2]; $addIntoRepo = !file_exists($destfile); if ($doPreprocessing) { if ($verbose) { echo "process {$sourcefile} \tto\t{$destfile} \n"; } $preproc->setVars($preprocvars); try { $contents = $preproc->parseFile($sourcefile); } catch (Exception $e) { throw new Exception("{$ficlist} : line {$nbline}, cannot process file " . $m[2] . " (" . $e . ")\n"); } if ($doCompression) { if (preg_match("/\\.php\$/", $destfile)) { if ($verbose) { echo " strip php comment "; } $contents = self::stripPhpComments($contents); if ($verbose) { echo "OK\n"; } } else { if (preg_match("/\\.js\$/", $destfile)) { if ($verbose) { echo "compress javascript file \n"; } $packer = new JavaScriptPacker($contents, 0, true, false); $contents = $packer->pack(); } } } file_put_contents($destfile, $contents); } elseif ($doCompression && preg_match("/\\.php\$/", $destfile)) { if ($verbose) { echo "strip comment in {$sourcefile}\tto\t" . $destfile . "\n"; } $src = file_get_contents($sourcefile); file_put_contents($destfile, self::stripPhpComments($src)); } elseif ($doCompression && preg_match("/\\.js\$/", $destfile)) { if ($verbose) { echo "compress javascript file " . $destfile . "\n"; } $script = file_get_contents($sourcefile); $packer = new JavaScriptPacker($script, 0, true, false); file_put_contents($destfile, $packer->pack()); } elseif ($m[1] == 'cch') { if (strpos($m[3], '%charset%') === false) { throw new Exception("{$ficlist} : line {$nbline}, dest file " . $m[3] . " doesn't contains %charset% pattern.\n"); } if ($verbose) { echo "convert charset\tsources\t" . $sourcedir . $currentsrcdir . $m[2] . " " . $m[3] . "\n"; } $encoding = preg_split('/[\\s,]+/', self::$targetPropertiesFilesCharset); $content = file_get_contents($sourcefile); if (self::$sourcePropertiesFilesDefaultCharset != '') { $encode = self::$sourcePropertiesFilesDefaultCharset; } else { $encode = mb_detect_encoding($content); } foreach ($encoding as $val) { $encodefile = str_replace('%charset%', $val, $destfile); if ($verbose) { echo "\tencode into " . $encodefile . "\n"; } $addIntoRepo = !file_exists($encodefile); $file = fopen($encodefile, "w"); fwrite($file, mb_convert_encoding($content, $val, $encode)); fclose($file); if ($addIntoRepo) { $d = getcwd(); chdir(dirname($encodefile)); if (self::$usedVcs == 'svn') { exec("svn add {$encodefile}"); } else { if (self::$usedVcs == 'hg') { exec("hg add {$encodefile}"); } } chdir($d); } } $addIntoRepo = false; } else { if ($verbose) { echo "copy " . $sourcedir . $currentsrcdir . $m[2] . "\tto\t" . $destfile . "\n"; } if (!copy($sourcefile, $destfile)) { throw new Exception("{$ficlist} : cannot copy file " . $m[2] . ", line {$nbline} \n"); } } if ($addIntoRepo) { $d = getcwd(); chdir(dirname($destfile)); if (self::$usedVcs == 'svn') { exec("svn add {$destfile}"); } else { if (self::$usedVcs == 'hg') { exec("hg add {$destfile}"); } } chdir($d); } } } elseif (preg_match("!^\\s*(\\#.*)?\$!", $line)) { // we ignore comments } else { throw new Exception("{$ficlist} : syntax error on line {$nbline} \n"); } } }
public function parseFile($filename) { $this->errorCode = 0; $this->errorLine = 0; $this->_blockstack = array(); // on sauve les variables pour les retrouver intact aprés le parsing // de façon à pouvoir rééxecuter plusieurs fois run sur des contenus différents if ($this->_doSaveVariables) { $this->_savedVariables = $this->_variables; } $source = explode("\n", file_get_contents($filename)); $result = ''; $nb = -1; // on parcours chaque ligne du source while (count($source)) { $nb++; $sline = array_shift($source); $tline = $sline; $isOpen = !(end($this->_blockstack) & self::BLOCK_NO); if (preg_match('/^\\#(ifdef|define|ifndef|elifdef|undef)\\s+(\\w+)\\s*$/m', $sline, $m)) { switch ($m[1]) { case 'ifdef': if (!$isOpen) { array_push($this->_blockstack, self::BLOCK_IF_NO); } else { if (isset($this->_variables[$m[2]]) && $this->_variables[$m[2]] !== '') { array_push($this->_blockstack, self::BLOCK_IF_YES); } else { array_push($this->_blockstack, self::BLOCK_IF_NO); } } $tline = false; break; case 'define': // define avec un seul argument. if ($isOpen) { $this->_variables[$m[2]] = true; } $tline = false; break; case 'undef': if ($isOpen) { unset($this->_variables[$m[2]]); } $tline = false; break; case 'ifndef': if (!$isOpen) { array_push($this->_blockstack, self::BLOCK_IF_NO); } else { if (isset($this->_variables[$m[2]]) && $this->_variables[$m[2]] !== '') { array_push($this->_blockstack, self::BLOCK_IF_NO); } else { array_push($this->_blockstack, self::BLOCK_IF_YES); } } $tline = false; break; case 'elifdef': $end = array_pop($this->_blockstack); if (!($end & self::BLOCK_IF)) { throw new jExceptionPreProc($filename, $nb, self::ERR_IF_MISSING); } if (end($this->_blockstack) & self::BLOCK_NO) { array_push($this->_blockstack, self::BLOCK_IF_NO); } elseif ($end & self::BLOCK_YES || $end & self::BLOCK_YES_PREVIOUS) { array_push($this->_blockstack, self::BLOCK_IF_NO + self::BLOCK_YES_PREVIOUS); } else { if (isset($this->_variables[$m[2]]) && $this->_variables[$m[2]] !== '') { array_push($this->_blockstack, self::BLOCK_IF_YES); } else { array_push($this->_blockstack, self::BLOCK_IF_NO); } } $tline = false; break; } /*echo $m[1],':'; var_dump($this->_blockstack); echo "\n";*/ } elseif (preg_match('/^\\#(define)\\s+(\\w+)\\s+(.+)$/m', $sline, $m)) { // define avec deux arguments if ($isOpen) { $this->_variables[$m[2]] = trim($m[3]); } $tline = false; } elseif (preg_match('/^\\#(expand)\\s(.*)$/m', $sline, $m)) { if ($isOpen) { $tline = preg_replace('/\\_\\_(\\w*)\\_\\_/e', '(isset($this->_variables["\\1"])&&$this->_variables["\\1"]!==\'\'?$this->_variables["\\1"]:"__\\1__")', $m[2]); } else { $tline = false; } } elseif (preg_match('/^\\#if\\s(.*)$/m', $sline, $m)) { if (!$isOpen) { array_push($this->_blockstack, self::BLOCK_IF_NO); } else { $val = $this->evalExpression($m[1], $filename, $nb); if ($val) { array_push($this->_blockstack, self::BLOCK_IF_YES); } else { array_push($this->_blockstack, self::BLOCK_IF_NO); } } $tline = false; } elseif (preg_match('/^\\#ifnot\\s(.*)$/m', $sline, $m)) { if (!$isOpen) { array_push($this->_blockstack, self::BLOCK_IF_NO); } else { $val = $this->evalExpression($m[1], $filename, $nb); if ($val) { array_push($this->_blockstack, self::BLOCK_IF_NO); } else { array_push($this->_blockstack, self::BLOCK_IF_YES); } } $tline = false; } elseif (preg_match('/^\\#elseif\\s(.*)$/m', $sline, $m)) { $end = array_pop($this->_blockstack); if (!($end & self::BLOCK_IF)) { throw new jExceptionPreProc($filename, $nb, self::ERR_IF_MISSING); } if (end($this->_blockstack) & self::BLOCK_NO) { array_push($this->_blockstack, self::BLOCK_IF_NO); } elseif ($end & self::BLOCK_YES || $end & self::BLOCK_YES_PREVIOUS) { array_push($this->_blockstack, self::BLOCK_IF_NO + self::BLOCK_YES_PREVIOUS); } else { $val = $this->evalExpression($m[1], $filename, $nb); if ($val) { array_push($this->_blockstack, self::BLOCK_IF_YES); } else { array_push($this->_blockstack, self::BLOCK_IF_NO); } } $tline = false; } elseif (preg_match('/^\\#(endif|else)\\s*$/m', $sline, $m)) { if ($m[1] == 'endif') { $end = array_pop($this->_blockstack); if (!($end & self::BLOCK_IF || $end & self::BLOCK_ELSE)) { throw new jExceptionPreProc($filename, $nb, self::ERR_IF_MISSING); } $tline = false; } elseif ($m[1] == 'else') { $end = array_pop($this->_blockstack); if ($end === self::BLOCK_IF_YES) { array_push($this->_blockstack, self::BLOCK_ELSE_NO); } elseif ($end & self::BLOCK_IF_NO) { if (end($this->_blockstack) & self::BLOCK_NO || $end & self::BLOCK_YES_PREVIOUS) { array_push($this->_blockstack, self::BLOCK_ELSE_NO); } else { array_push($this->_blockstack, self::BLOCK_ELSE_YES); } } else { throw new jExceptionPreProc($filename, $nb, self::ERR_IF_MISSING); } $tline = false; } } elseif (preg_match('/^\\#include(php|raw)?\\s+([\\w\\/\\.\\:\\-]+)\\s*$/m', $sline, $m)) { if ($isOpen) { $path = $m[2]; if (!($path[0] == '/' || preg_match('/^\\w\\:\\.+$/', $path))) { $path = realpath(dirname($filename) . '/' . $path); if ($path == '') { throw new jExceptionPreProc($filename, $nb, self::ERR_INVALID_FILENAME, $m[2]); } } if (file_exists($path) && !is_dir($path)) { if ($m[1] == 'raw') { $tline = file_get_contents($path); } else { $preproc = new jPreProcessor(); $preproc->_doSaveVariables = false; $preproc->setVars($this->_variables); $tline = $preproc->parseFile($path); $this->_variables = $preproc->_variables; $preproc = null; } } else { throw new jExceptionPreProc($filename, $nb, self::ERR_INVALID_FILENAME, $m[2]); } if ($m[1] == 'php') { if (preg_match('/^\\s*\\<\\?(?:php)?(.*)/sm', $tline, $ms)) { $tline = $ms[1]; } if (preg_match('/(.*)\\?\\>\\s*$/sm', $tline, $ms)) { $tline = $ms[1]; } } } else { $tline = false; } } elseif (strlen($sline) && $sline[0] == '#') { if (strlen($sline) > 1 && $sline[1] == '#') { if (!$isOpen) { $tline = false; } else { $tline = substr($sline, 1); } } else { throw new jExceptionPreProc($filename, $nb, self::ERR_SYNTAX); } } else { if (!$isOpen) { $tline = false; } } if ($tline !== false) { if ($result == '') { $result .= $tline; } else { $result .= "\n" . $tline; } } } if (count($this->_blockstack)) { throw new jExceptionPreProc($filename, $nb, self::ERR_ENDIF_MISSING); } if ($this->_doSaveVariables) { $this->_variables = $this->_savedVariables; } return $result; }