Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

context.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 10.87 KiB


001  <?php
002  /**
003  *
004  * This file is part of the phpBB Forum Software package.
005  *
006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
007  * @license GNU General Public License, version 2 (GPL-2.0)
008  *
009  * For full copyright and license information, please see
010  * the docs/CREDITS.txt file.
011  *
012  */
013   
014  namespace phpbb\template;
015   
016  /**
017  * Stores variables assigned to template.
018  */
019  class context
020  {
021      /**
022      * variable that holds all the data we'll be substituting into
023      * the compiled templates. Takes form:
024      * --> $this->tpldata[block][iteration#][child][iteration#][child2][iteration#][variablename] == value
025      * if it's a root-level variable, it'll be like this:
026      * --> $this->tpldata[.][0][varname] == value
027      *
028      * @var array
029      */
030      private $tpldata = array('.' => array(0 => array()));
031   
032      /**
033      * @var array Reference to template->tpldata['.'][0]
034      */
035      private $rootref;
036   
037      /**
038      * @var bool
039      */
040      private $num_rows_is_set;
041   
042      public function __construct()
043      {
044          $this->clear();
045      }
046   
047      /**
048      * Clears template data set.
049      */
050      public function clear()
051      {
052          $this->tpldata = array('.' => array(0 => array()));
053          $this->rootref = &$this->tpldata['.'][0];
054          $this->num_rows_is_set = false;
055      }
056   
057      /**
058      * Assign a single scalar value to a single key.
059      *
060      * Value can be a string, an integer or a boolean.
061      *
062      * @param string $varname Variable name
063      * @param string $varval Value to assign to variable
064      * @return true
065      */
066      public function assign_var($varname, $varval)
067      {
068          $this->rootref[$varname] = $varval;
069   
070          return true;
071      }
072   
073      /**
074      * Append text to the string value stored in a key.
075      *
076      * Text is appended using the string concatenation operator (.).
077      *
078      * @param string $varname Variable name
079      * @param string $varval Value to append to variable
080      * @return true
081      */
082      public function append_var($varname, $varval)
083      {
084          $this->rootref[$varname] = (isset($this->rootref[$varname]) ? $this->rootref[$varname] : '') . $varval;
085   
086          return true;
087      }
088   
089      /**
090      * Returns a reference to template data array.
091      *
092      * This function is public so that template renderer may invoke it.
093      * Users should alter template variables via functions in \phpbb\template\template.
094      *
095      * Note: modifying returned array will affect data stored in the context.
096      *
097      * @return array template data
098      */
099      public function &get_data_ref()
100      {
101          // returning a reference directly is not
102          // something php is capable of doing
103          $ref = &$this->tpldata;
104   
105          if (!$this->num_rows_is_set)
106          {
107              /*
108              * We do not set S_NUM_ROWS while adding a row, to reduce the complexity
109              * If we would set it on adding, each subsequent adding would cause
110              * n modifications, resulting in a O(n!) complexity, rather then O(n)
111              */
112              foreach ($ref as $loop_name => &$loop_data)
113              {
114                  if ($loop_name === '.')
115                  {
116                      continue;
117                  }
118   
119                  $this->set_num_rows($loop_data);
120              }
121              $this->num_rows_is_set = true;
122          }
123   
124          return $ref;
125      }
126   
127      /**
128      * Set S_NUM_ROWS for each row in this template block
129      *
130      * @param array $loop_data
131      */
132      protected function set_num_rows(&$loop_data)
133      {
134          $s_num_rows = sizeof($loop_data);
135          foreach ($loop_data as &$mod_block)
136          {
137              foreach ($mod_block as $sub_block_name => &$sub_block)
138              {
139                  // If the key name is lowercase and the data is an array,
140                  // it could be a template loop. So we set the S_NUM_ROWS there
141                  // aswell.
142                  if ($sub_block_name === strtolower($sub_block_name) && is_array($sub_block))
143                  {
144                      $this->set_num_rows($sub_block);
145                  }
146              }
147   
148              // Check whether we are inside a block before setting the variable
149              if (isset($mod_block['S_BLOCK_NAME']))
150              {
151                  $mod_block['S_NUM_ROWS'] = $s_num_rows;
152              }
153          }
154      }
155   
156      /**
157      * Returns a reference to template root scope.
158      *
159      * This function is public so that template renderer may invoke it.
160      * Users should not need to invoke this function.
161      *
162      * Note: modifying returned array will affect data stored in the context.
163      *
164      * @return array template data
165      */
166      public function &get_root_ref()
167      {
168          // rootref is already a reference
169          return $this->rootref;
170      }
171   
172      /**
173      * Assign key variable pairs from an array to a specified block
174      *
175      * @param string $blockname Name of block to assign $vararray to
176      * @param array $vararray A hash of variable name => value pairs
177      * @return true
178      */
179      public function assign_block_vars($blockname, array $vararray)
180      {
181          $this->num_rows_is_set = false;
182          if (strpos($blockname, '.') !== false)
183          {
184              // Nested block.
185              $blocks = explode('.', $blockname);
186              $blockcount = sizeof($blocks) - 1;
187   
188              $str = &$this->tpldata;
189              for ($i = 0; $i < $blockcount; $i++)
190              {
191                  $str = &$str[$blocks[$i]];
192                  $str = &$str[sizeof($str) - 1];
193              }
194   
195              $s_row_count = isset($str[$blocks[$blockcount]]) ? sizeof($str[$blocks[$blockcount]]) : 0;
196              $vararray['S_ROW_COUNT'] = $vararray['S_ROW_NUM'] = $s_row_count;
197   
198              // Assign S_FIRST_ROW
199              if (!$s_row_count)
200              {
201                  $vararray['S_FIRST_ROW'] = true;
202              }
203   
204              // Assign S_BLOCK_NAME
205              $vararray['S_BLOCK_NAME'] = $blocks[$blockcount];
206   
207              // Now the tricky part, we always assign S_LAST_ROW and remove the entry before
208              // This is much more clever than going through the complete template data on display (phew)
209              $vararray['S_LAST_ROW'] = true;
210              if ($s_row_count > 0)
211              {
212                  unset($str[$blocks[$blockcount]][($s_row_count - 1)]['S_LAST_ROW']);
213              }
214   
215              // Now we add the block that we're actually assigning to.
216              // We're adding a new iteration to this block with the given
217              // variable assignments.
218              $str[$blocks[$blockcount]][] = $vararray;
219          }
220          else
221          {
222              // Top-level block.
223              $s_row_count = (isset($this->tpldata[$blockname])) ? sizeof($this->tpldata[$blockname]) : 0;
224              $vararray['S_ROW_COUNT'] = $vararray['S_ROW_NUM'] = $s_row_count;
225   
226              // Assign S_FIRST_ROW
227              if (!$s_row_count)
228              {
229                  $vararray['S_FIRST_ROW'] = true;
230              }
231   
232              // Assign S_BLOCK_NAME
233              $vararray['S_BLOCK_NAME'] = $blockname;
234   
235              // We always assign S_LAST_ROW and remove the entry before
236              $vararray['S_LAST_ROW'] = true;
237              if ($s_row_count > 0)
238              {
239                  unset($this->tpldata[$blockname][($s_row_count - 1)]['S_LAST_ROW']);
240              }
241   
242              // Add a new iteration to this block with the variable assignments we were given.
243              $this->tpldata[$blockname][] = $vararray;
244          }
245   
246          return true;
247      }
248   
249      /**
250      * Assign key variable pairs from an array to a whole specified block loop
251      *
252      * @param string $blockname Name of block to assign $block_vars_array to
253      * @param array $block_vars_array An array of hashes of variable name => value pairs
254      * @return true
255      */
256      public function assign_block_vars_array($blockname, array $block_vars_array)
257      {
258          foreach ($block_vars_array as $vararray)
259          {
260              $this->assign_block_vars($blockname, $vararray);
261          }
262   
263          return true;
264      }
265   
266      /**
267      * Change already assigned key variable pair (one-dimensional - single loop entry)
268      *
269      * An example of how to use this function:
270      * {@example alter_block_array.php}
271      *
272      * @param    string    $blockname    the blockname, for example 'loop'
273      * @param    array    $vararray    the var array to insert/add or merge
274      * @param    mixed    $key        Key to search for
275      *
276      * array: KEY => VALUE [the key/value pair to search for within the loop to determine the correct position]
277      *
278      * int: Position [the position to change or insert at directly given]
279      *
280      * If key is false the position is set to 0
281      * If key is true the position is set to the last entry
282      *
283      * @param    string    $mode        Mode to execute (valid modes are 'insert' and 'change')
284      *
285      *    If insert, the vararray is inserted at the given position (position counting from zero).
286      *    If change, the current block gets merged with the vararray (resulting in new key/value pairs be added and existing keys be replaced by the new \value).
287      *
288      * Since counting begins by zero, inserting at the last position will result in this array: array(vararray, last positioned array)
289      * and inserting at position 1 will result in this array: array(first positioned array, vararray, following vars)
290      *
291      * @return bool false on error, true on success
292      */
293      public function alter_block_array($blockname, array $vararray, $key = false, $mode = 'insert')
294      {
295          $this->num_rows_is_set = false;
296          if (strpos($blockname, '.') !== false)
297          {
298              // Nested block.
299              $blocks = explode('.', $blockname);
300              $blockcount = sizeof($blocks) - 1;
301   
302              $block = &$this->tpldata;
303              for ($i = 0; $i < $blockcount; $i++)
304              {
305                  if (($pos = strpos($blocks[$i], '[')) !== false)
306                  {
307                      $name = substr($blocks[$i], 0, $pos);
308   
309                      if (strpos($blocks[$i], '[]') === $pos)
310                      {
311                          $index = sizeof($block[$name]) - 1;
312                      }
313                      else
314                      {
315                          $index = min((int) substr($blocks[$i], $pos + 1, -1), sizeof($block[$name]) - 1);
316                      }
317                  }
318                  else
319                  {
320                      $name = $blocks[$i];
321                      $index = sizeof($block[$name]) - 1;
322                  }
323                  $block = &$block[$name];
324                  $block = &$block[$index];
325              }
326   
327              $block = &$block[$blocks[$i]]; // Traverse the last block
328          }
329          else
330          {
331              // Top-level block.
332              $block = &$this->tpldata[$blockname];
333          }
334   
335          // Change key to zero (change first position) if false and to last position if true
336          if ($key === false || $key === true)
337          {
338              $key = ($key === false) ? 0 : sizeof($block);
339          }
340   
341          // Get correct position if array given
342          if (is_array($key))
343          {
344              // Search array to get correct position
345              list($search_key, $search_value) = @each($key);
346   
347              $key = null;
348              foreach ($block as $i => $val_ary)
349              {
350                  if ($val_ary[$search_key] === $search_value)
351                  {
352                      $key = $i;
353                      break;
354                  }
355              }
356   
357              // key/value pair not found
358              if ($key === null)
359              {
360                  return false;
361              }
362          }
363   
364          // Insert Block
365          if ($mode == 'insert')
366          {
367              // Make sure we are not exceeding the last iteration
368              if ($key >= sizeof($this->tpldata[$blockname]))
369              {
370                  $key = sizeof($this->tpldata[$blockname]);
371                  unset($this->tpldata[$blockname][($key - 1)]['S_LAST_ROW']);
372                  $vararray['S_LAST_ROW'] = true;
373              }
374              else if ($key === 0)
375              {
376                  unset($this->tpldata[$blockname][0]['S_FIRST_ROW']);
377                  $vararray['S_FIRST_ROW'] = true;
378              }
379   
380              // Assign S_BLOCK_NAME
381              $vararray['S_BLOCK_NAME'] = $blockname;
382   
383              // Re-position template blocks
384              for ($i = sizeof($block); $i > $key; $i--)
385              {
386                  $block[$i] = $block[$i-1];
387   
388                  $block[$i]['S_ROW_COUNT'] = $block[$i]['S_ROW_NUM'] = $i;
389              }
390   
391              // Insert vararray at given position
392              $block[$key] = $vararray;
393              $block[$key]['S_ROW_COUNT'] = $block[$key]['S_ROW_NUM'] = $key;
394   
395              return true;
396          }
397   
398          // Which block to change?
399          if ($mode == 'change')
400          {
401              if ($key == sizeof($block))
402              {
403                  $key--;
404              }
405   
406              $block[$key] = array_merge($block[$key], $vararray);
407   
408              return true;
409          }
410   
411          return false;
412      }
413   
414      /**
415      * Reset/empty complete block
416      *
417      * @param string $blockname Name of block to destroy
418      * @return true
419      */
420      public function destroy_block_vars($blockname)
421      {
422          $this->num_rows_is_set = false;
423          if (strpos($blockname, '.') !== false)
424          {
425              // Nested block.
426              $blocks = explode('.', $blockname);
427              $blockcount = sizeof($blocks) - 1;
428   
429              $str = &$this->tpldata;
430              for ($i = 0; $i < $blockcount; $i++)
431              {
432                  $str = &$str[$blocks[$i]];
433                  $str = &$str[sizeof($str) - 1];
434              }
435   
436              unset($str[$blocks[$blockcount]]);
437          }
438          else
439          {
440              // Top-level block.
441              unset($this->tpldata[$blockname]);
442          }
443   
444          return true;
445      }
446  }
447