ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
TemplateHelper.php
Go to the documentation of this file.
1 <?php
7 namespace Whoops\Util;
8 
14 
19 {
24  private $variables = [];
25 
29  private $htmlDumper;
30 
35 
39  private $cloner;
40 
45 
46  public function __construct()
47  {
48  // root path for ordinary composer projects
49  $this->applicationRootPath = dirname(dirname(dirname(dirname(dirname(dirname(__DIR__))))));
50  }
51 
58  public function escape($raw)
59  {
60  $flags = ENT_QUOTES;
61 
62  // HHVM has all constants defined, but only ENT_IGNORE
63  // works at the moment
64  if (defined("ENT_SUBSTITUTE") && !defined("HHVM_VERSION")) {
65  $flags |= ENT_SUBSTITUTE;
66  } else {
67  // This is for 5.3.
68  // The documentation warns of a potential security issue,
69  // but it seems it does not apply in our case, because
70  // we do not blacklist anything anywhere.
71  $flags |= ENT_IGNORE;
72  }
73 
74  $raw = str_replace(chr(9), ' ', $raw);
75 
76  return htmlspecialchars($raw, $flags, "UTF-8");
77  }
78 
86  public function escapeButPreserveUris($raw)
87  {
88  $escaped = $this->escape($raw);
89  return preg_replace(
90  "@([A-z]+?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@",
91  "<a href=\"$1\" target=\"_blank\" rel=\"noreferrer noopener\">$1</a>",
92  $escaped
93  );
94  }
95 
103  public function breakOnDelimiter($delimiter, $s)
104  {
105  $parts = explode($delimiter, $s);
106  foreach ($parts as &$part) {
107  $part = '<div class="delimiter">' . $part . '</div>';
108  }
109 
110  return implode($delimiter, $parts);
111  }
112 
119  public function shorten($path)
120  {
121  if ($this->applicationRootPath != "/") {
122  $path = str_replace($this->applicationRootPath, '&hellip;', $path);
123  }
124 
125  return $path;
126  }
127 
128  private function getDumper()
129  {
130  if (!$this->htmlDumper && class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) {
131  $this->htmlDumperOutput = new HtmlDumperOutput();
132  // re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump.
133  $this->htmlDumper = new HtmlDumper($this->htmlDumperOutput);
134 
135  $styles = [
136  'default' => 'color:#FFFFFF; line-height:normal; font:12px "Inconsolata", "Fira Mono", "Source Code Pro", Monaco, Consolas, "Lucida Console", monospace !important; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:99999; word-break: normal',
137  'num' => 'color:#BCD42A',
138  'const' => 'color: #4bb1b1;',
139  'str' => 'color:#BCD42A',
140  'note' => 'color:#ef7c61',
141  'ref' => 'color:#A0A0A0',
142  'public' => 'color:#FFFFFF',
143  'protected' => 'color:#FFFFFF',
144  'private' => 'color:#FFFFFF',
145  'meta' => 'color:#FFFFFF',
146  'key' => 'color:#BCD42A',
147  'index' => 'color:#ef7c61',
148  ];
149  $this->htmlDumper->setStyles($styles);
150  }
151 
152  return $this->htmlDumper;
153  }
154 
161  public function dump($value)
162  {
163  $dumper = $this->getDumper();
164 
165  if ($dumper) {
166  // re-use the same DumpOutput instance, so it won't re-render the global styles/scripts on each dump.
167  // exclude verbose information (e.g. exception stack traces)
168  if (class_exists('Symfony\Component\VarDumper\Caster\Caster')) {
169  $cloneVar = $this->getCloner()->cloneVar($value, Caster::EXCLUDE_VERBOSE);
170  // Symfony VarDumper 2.6 Caster class dont exist.
171  } else {
172  $cloneVar = $this->getCloner()->cloneVar($value);
173  }
174 
175  $dumper->dump(
176  $cloneVar,
177  $this->htmlDumperOutput
178  );
179 
180  $output = $this->htmlDumperOutput->getOutput();
181  $this->htmlDumperOutput->clear();
182 
183  return $output;
184  }
185 
186  return htmlspecialchars(print_r($value, true));
187  }
188 
195  public function dumpArgs(Frame $frame)
196  {
197  // we support frame args only when the optional dumper is available
198  if (!$this->getDumper()) {
199  return '';
200  }
201 
202  $html = '';
203  $numFrames = count($frame->getArgs());
204 
205  if ($numFrames > 0) {
206  $html = '<ol class="linenums">';
207  foreach ($frame->getArgs() as $j => $frameArg) {
208  $html .= '<li>'. $this->dump($frameArg) .'</li>';
209  }
210  $html .= '</ol>';
211  }
212 
213  return $html;
214  }
215 
222  public function slug($original)
223  {
224  $slug = str_replace(" ", "-", $original);
225  $slug = preg_replace('/[^\w\d\-\_]/i', '', $slug);
226  return strtolower($slug);
227  }
228 
237  public function render($template, array $additionalVariables = null)
238  {
239  $variables = $this->getVariables();
240 
241  // Pass the helper to the template:
242  $variables["tpl"] = $this;
243 
244  if ($additionalVariables !== null) {
245  $variables = array_replace($variables, $additionalVariables);
246  }
247 
248  call_user_func(function () {
249  extract(func_get_arg(1));
250  require func_get_arg(0);
251  }, $template, $variables);
252  }
253 
260  public function setVariables(array $variables)
261  {
262  $this->variables = $variables;
263  }
264 
271  public function setVariable($variableName, $variableValue)
272  {
273  $this->variables[$variableName] = $variableValue;
274  }
275 
284  public function getVariable($variableName, $defaultValue = null)
285  {
286  return isset($this->variables[$variableName]) ?
287  $this->variables[$variableName] : $defaultValue;
288  }
289 
295  public function delVariable($variableName)
296  {
297  unset($this->variables[$variableName]);
298  }
299 
305  public function getVariables()
306  {
307  return $this->variables;
308  }
309 
315  public function setCloner($cloner)
316  {
317  $this->cloner = $cloner;
318  }
319 
325  public function getCloner()
326  {
327  if (!$this->cloner) {
328  $this->cloner = new VarCloner();
329  }
330  return $this->cloner;
331  }
332 
339  {
340  $this->applicationRootPath = $applicationRootPath;
341  }
342 
348  public function getApplicationRootPath()
349  {
351  }
352 }
setCloner($cloner)
Set the cloner used for dumping variables.
$template
breakOnDelimiter($delimiter, $s)
Makes sure that the given string breaks on the delimiter.
slug($original)
Convert a string to a slug version of itself.
Used as output callable for Symfony::dump()
$delimiter
Definition: showstats.php:16
escape($raw)
Escapes a string for output in an HTML document.
Exposes useful tools for working with/in templates.
delVariable($variableName)
Unsets a single template variable, by its name.
$s
Definition: pwgen.php:45
setVariables(array $variables)
Sets the variables to be passed to all templates rendered by this template helper.
getVariable($variableName, $defaultValue=null)
Gets a single template variable, by its name, or $defaultValue if the variable does not exist...
if(!is_dir( $entity_dir)) exit("Fatal Error ([A-Za-z0-9]+)\+" &#(? foreach( $entity_files as $file) $output
render($template, array $additionalVariables=null)
Given a template path, render it within its own scope.
setVariable($variableName, $variableValue)
Sets a single template variable, by its name:
escapeButPreserveUris($raw)
Escapes a string for output in an HTML document, but preserves URIs within it, and converts them to c...
getCloner()
Get the cloner used for dumping variables.
getApplicationRootPath()
Return the application root path.
dump($value)
Format the given value into a human readable string.
Create styles array
The data for the language used.
shorten($path)
Replace the part of the path that all files have in common.
getVariables()
Returns all variables for this helper.
setApplicationRootPath($applicationRootPath)
Set the application root path.
Whoops - php errors for cool kids.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
$html
Definition: example_001.php:87
dumpArgs(Frame $frame)
Format the args of the given Frame as a human readable html string.