Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
error.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Document
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * DocumentError class, provides an easy interface to parse and display an error page
14  *
15  * @package Joomla.Platform
16  * @subpackage Document
17  * @since 11.1
18  */
20 {
21  /**
22  * Error Object
23  *
24  * @var object
25  * @since 11.1
26  */
27  protected $_error;
28 
29  /**
30  * Class constructor
31  *
32  * @param array $options Associative array of attributes
33  *
34  * @since 11.1
35  */
36  public function __construct($options = array())
37  {
38  parent::__construct($options);
39 
40  // Set mime type
41  $this->_mime = 'text/html';
42 
43  // Set document type
44  $this->_type = 'error';
45  }
46 
47  /**
48  * Set error object
49  *
50  * @param object $error Error object to set
51  *
52  * @return boolean True on success
53  *
54  * @since 11.1
55  */
56  public function setError($error)
57  {
58  if ($error instanceof Exception)
59  {
60  $this->_error = & $error;
61  return true;
62  }
63  else
64  {
65  return false;
66  }
67  }
68 
69  /**
70  * Render the document
71  *
72  * @param boolean $cache If true, cache the output
73  * @param array $params Associative array of attributes
74  *
75  * @return string The rendered data
76  *
77  * @since 11.1
78  */
79  public function render($cache = false, $params = array())
80  {
81  // If no error object is set return null
82  if (!isset($this->_error))
83  {
84  return;
85  }
86 
87  // Set the status header
88  JFactory::getApplication()->setHeader('status', $this->_error->getCode() . ' ' . str_replace("\n", ' ', $this->_error->getMessage()));
89  $file = 'error.php';
90 
91  // Check template
92  $directory = isset($params['directory']) ? $params['directory'] : 'templates';
93  $template = isset($params['template']) ? JFilterInput::getInstance()->clean($params['template'], 'cmd') : 'system';
94 
95  if (!file_exists($directory . '/' . $template . '/' . $file))
96  {
97  $template = 'system';
98  }
99 
100  // Set variables
101  $this->baseurl = JUri::base(true);
102  $this->template = $template;
103  $this->debug = isset($params['debug']) ? $params['debug'] : false;
104  $this->error = $this->_error;
105 
106  // Load
107  $data = $this->_loadTemplate($directory . '/' . $template, $file);
108 
109  parent::render();
110  return $data;
111  }
112 
113  /**
114  * Load a template file
115  *
116  * @param string $directory The name of the template
117  * @param string $filename The actual filename
118  *
119  * @return string The contents of the template
120  *
121  * @since 11.1
122  */
123  public function _loadTemplate($directory, $filename)
124  {
125  $contents = '';
126 
127  // Check to see if we have a valid template file
128  if (file_exists($directory . '/' . $filename))
129  {
130  // Store the file path
131  $this->_file = $directory . '/' . $filename;
132 
133  // Get the file content
134  ob_start();
135  require_once $directory . '/' . $filename;
136  $contents = ob_get_contents();
137  ob_end_clean();
138  }
139 
140  return $contents;
141  }
142 
143  /**
144  * Render the backtrace
145  *
146  * @return string The contents of the backtrace
147  *
148  * @since 11.1
149  */
150  public function renderBacktrace()
151  {
152  $contents = null;
153  $backtrace = $this->_error->getTrace();
154  if (is_array($backtrace))
155  {
156  ob_start();
157  $j = 1;
158  echo '<table cellpadding="0" cellspacing="0" class="Table">';
159  echo ' <tr>';
160  echo ' <td colspan="3" class="TD"><strong>Call stack</strong></td>';
161  echo ' </tr>';
162  echo ' <tr>';
163  echo ' <td class="TD"><strong>#</strong></td>';
164  echo ' <td class="TD"><strong>Function</strong></td>';
165  echo ' <td class="TD"><strong>Location</strong></td>';
166  echo ' </tr>';
167  for ($i = count($backtrace) - 1; $i >= 0; $i--)
168  {
169  echo ' <tr>';
170  echo ' <td class="TD">' . $j . '</td>';
171  if (isset($backtrace[$i]['class']))
172  {
173  echo ' <td class="TD">' . $backtrace[$i]['class'] . $backtrace[$i]['type'] . $backtrace[$i]['function'] . '()</td>';
174  }
175  else
176  {
177  echo ' <td class="TD">' . $backtrace[$i]['function'] . '()</td>';
178  }
179  if (isset($backtrace[$i]['file']))
180  {
181  echo ' <td class="TD">' . $backtrace[$i]['file'] . ':' . $backtrace[$i]['line'] . '</td>';
182  }
183  else
184  {
185  echo ' <td class="TD">&#160;</td>';
186  }
187  echo ' </tr>';
188  $j++;
189  }
190  echo '</table>';
191  $contents = ob_get_contents();
192  ob_end_clean();
193  }
194  return $contents;
195  }
196 }