note 80627 added to function.php-check-syntax

From: Date: Fri, 25 Jan 2008 18:16:52 +0000
Subject: note 80627 added to function.php-check-syntax
Groups: php.notes 
Request: Send a blank email to php-notes+get-135413@lists.php.net to get a copy of this message
My previous code was buggy sorry, here is an update (thanks phprockstheworld). I can't find a way to break the dead code sandbox. Who can ? <?php function eval_syntax($code) { $braces = 0; $inString = 0; // We need to know if braces are correctly balanced. // This is not trivial due to variable interpolation // which occurs in heredoc, backticked and double quoted strings foreach (token_get_all('<?php ' . $code) as $token) { if (is_array($token)) { switch ($token[0]) { case T_CURLY_OPEN: case T_DOLLAR_OPEN_CURLY_BRACES: case T_START_HEREDOC: ++$inString; break; case T_END_HEREDOC: --$inString; break; } } else if ($inString & 1) { switch ($token) { case '`': case '"': --$inString; break; } } else { switch ($token) { case '`': case '"': ++$inString; break; case '{': ++$braces; break; case '}': if ($inString) --$inString; else { --$braces; if ($braces < 0) return false; } break; } } } if ($braces) return false; // Unbalanced braces would break the eval below else { ob_start(); // Catch potential parse error messages $code = eval('if(0){' . $code . '}'); // Put $code in a dead code sandbox to prevent its execution ob_end_clean(); return false !== $code; } } ---- Server IP: 194.246.101.61 Probable Submitter: 82.239.245.173 ---- Manual Page -- http://www.php.net/manual/en/function.php-check-syntax.php Edit -- https://master.php.net/note/edit/80627 Del: integrated -- https://master.php.net/note/delete/80627/integrated Del: useless -- https://master.php.net/note/delete/80627/useless Del: bad code -- https://master.php.net/note/delete/80627/bad+code Del: spam -- https://master.php.net/note/delete/80627/spam Del: non-english -- https://master.php.net/note/delete/80627/non-english Del: in docs -- https://master.php.net/note/delete/80627/in+docs Del: other reasons-- https://master.php.net/note/delete/80627 Reject -- https://master.php.net/note/reject/80627 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135413) next »