You are here

function url_alter_check_php_syntax in URL alter 6

Validate PHP code syntax.

Parameters

$code: A string with PHP code, not including the opening or closing PHP tags.

Return value

TRUE if the code was valid, otherwise an error message or FALSE.

1 call to url_alter_check_php_syntax()
url_alter_validate_php_element in ./url_alter.admin.inc

File

./url_alter.admin.inc, line 47
Administrative page callbacks for the url_alter module.

Code

function url_alter_check_php_syntax($code) {
  if (!strlen($code)) {
    return TRUE;
  }
  elseif (preg_match('/<\\?php|\\?>/', $code, $matches)) {
    return t('Do not use the PHP tag %code in your code.', array(
      '%code' => $matches[0],
    ));
  }
  else {
    $success = TRUE;
    $code .= "\nreturn " . var_export($success, TRUE) . ";";
    ob_start();
    $result = @eval($code);
    ob_end_clean();
    if ($result === $success) {
      return TRUE;
    }
    else {
      return is_string($result) ? $result : FALSE;
    }
  }
}