PageRenderTime 29ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/uthando/php/PEAR/HTML/Progress2/Error.php

https://github.com/Rem122/Uthando-CMS
PHP | 413 lines | 224 code | 33 blank | 156 comment | 30 complexity | 3e33bf5fba0fc5ee550ef71492660c20 MD5 | raw file
Possible License(s): MIT, GPL-3.0
  1. <?php
  2. /**
  3. * Copyright (c) 2005-2008, Laurent Laville <pear@laurent-laville.org>
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of the authors nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * PHP versions 4 and 5
  33. *
  34. * @category HTML
  35. * @package HTML_Progress2
  36. * @author Laurent Laville <pear@laurent-laville.org>
  37. * @copyright 2005-2008 Laurent Laville
  38. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  39. * @version CVS: $Id: Error.php,v 1.11 2008/03/20 21:27:55 farell Exp $
  40. * @link http://pear.php.net/package/HTML_Progress2
  41. * @since File available since Release 2.0.0RC1
  42. */
  43. require_once 'PEAR.php';
  44. /**
  45. * This class creates a progress error object, extending the PEAR_Error class.
  46. *
  47. * @category HTML
  48. * @package HTML_Progress2
  49. * @author Laurent Laville <pear@laurent-laville.org>
  50. * @copyright 2005-2008 Laurent Laville
  51. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  52. * @version Release: 2.4.1
  53. * @link http://pear.php.net/package/HTML_Progress2
  54. * @since Class available since Release 2.0.0RC1
  55. */
  56. class HTML_Progress2_Error extends PEAR_Error
  57. {
  58. /**
  59. * Constructor (ZE1)
  60. *
  61. * @param string $message (optional) Final error message
  62. * @param int $code (optional) Error code
  63. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  64. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  65. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  66. * @param mixed $options (optional) error level, _OR_ in the case of
  67. * PEAR_ERROR_CALLBACK, the callback function
  68. * @param string $userinfo (optional) additional user/debug info
  69. *
  70. * @since version 2.0.0 (2005-10-01)
  71. * @access public
  72. */
  73. function HTML_Progress2_Error($message = null,
  74. $code = null,
  75. $mode = null, $options = null,
  76. $userinfo = null)
  77. {
  78. $this->__construct($message, $code, $mode, $options, $userinfo);
  79. }
  80. /**
  81. * Constructor (ZE2)
  82. *
  83. * @param string $message (optional) Final error message
  84. * @param int $code (optional) Error code
  85. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  86. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  87. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  88. * @param mixed $options (optional) error level, _OR_ in the case of
  89. * PEAR_ERROR_CALLBACK, the callback function
  90. * @param string $userinfo (optional) additional user/debug info
  91. *
  92. * @since version 2.0.0 (2005-10-01)
  93. * @access public
  94. */
  95. function __construct($message = null,
  96. $code = null,
  97. $mode = null, $options = null,
  98. $userinfo = null)
  99. {
  100. if ($mode === null) {
  101. $mode = PEAR_ERROR_RETURN;
  102. }
  103. $this->message = $message;
  104. $this->code = $code;
  105. $this->mode = $mode;
  106. $this->userinfo = $userinfo;
  107. $this->backtrace = debug_backtrace();
  108. if ($mode & PEAR_ERROR_CALLBACK) {
  109. $this->level = E_USER_NOTICE;
  110. $this->callback = $options;
  111. } else {
  112. if ($options === null) {
  113. switch ($userinfo['level']) {
  114. case 'exception':
  115. case 'error':
  116. $options = E_USER_ERROR;
  117. break;
  118. case 'warning':
  119. $options = E_USER_WARNING;
  120. break;
  121. default:
  122. $options = E_USER_NOTICE;
  123. }
  124. }
  125. $this->level = $options;
  126. $this->callback = null;
  127. }
  128. if ($this->mode & PEAR_ERROR_PRINT) {
  129. echo $this->_display($userinfo);
  130. }
  131. if ($this->mode & PEAR_ERROR_TRIGGER) {
  132. trigger_error($this->getMessage(), $this->level);
  133. }
  134. if ($this->mode & PEAR_ERROR_DIE) {
  135. $this->log();
  136. die();
  137. }
  138. if ($this->mode & PEAR_ERROR_CALLBACK) {
  139. if (is_callable($this->callback)) {
  140. call_user_func($this->callback, $this);
  141. } else {
  142. $this->log();
  143. }
  144. }
  145. }
  146. /**
  147. * Get error level from an error object
  148. *
  149. * @return int error level
  150. * @since version 2.0.0 (2005-10-01)
  151. * @access public
  152. */
  153. function getLevel()
  154. {
  155. return $this->level;
  156. }
  157. /**
  158. * Default callback function/method from an error object
  159. *
  160. * @return void
  161. * @since version 2.0.0 (2005-10-01)
  162. * @access public
  163. */
  164. function log()
  165. {
  166. $userinfo = $this->getUserInfo();
  167. $display_errors = ini_get('display_errors');
  168. $log_errors = ini_get('log_errors');
  169. if ($display_errors) {
  170. echo $this->_display($userinfo);
  171. }
  172. if ($log_errors) {
  173. $this->_log($userinfo);
  174. }
  175. }
  176. /**
  177. * Returns the context of execution formatted.
  178. *
  179. * @param string $format the context of execution format
  180. *
  181. * @return string
  182. * @since version 2.0.0 (2005-10-01)
  183. * @access public
  184. */
  185. function sprintContextExec($format)
  186. {
  187. $userinfo = $this->getUserInfo();
  188. if (isset($userinfo['context'])) {
  189. $context = $userinfo['context'];
  190. } else {
  191. $context = $this->getBacktrace();
  192. $context = @array_pop($context);
  193. }
  194. if ($context) {
  195. $file = $context['file'];
  196. $line = $context['line'];
  197. if (isset($context['class'])) {
  198. $func = $context['class'];
  199. $func .= $context['type'];
  200. $func .= $context['function'];
  201. } elseif (isset($context['function'])) {
  202. $func = $context['function'];
  203. } else {
  204. $func = '';
  205. }
  206. return sprintf($format, $file, $line, $func);
  207. }
  208. return '';
  209. }
  210. /**
  211. * Print an error message
  212. *
  213. * @param array $userinfo hash of parameters
  214. *
  215. * @return void
  216. * @since version 2.0.0 (2005-10-01)
  217. * @access private
  218. */
  219. function _display($userinfo)
  220. {
  221. $displayDefault = array(
  222. 'eol' => "<br/>\n",
  223. 'lineFormat' => '<b>%1$s</b>: %2$s %3$s',
  224. 'contextFormat' => 'in <b>%3$s</b> ' .
  225. '(file <b>%1$s</b> on line <b>%2$s</b>)'
  226. );
  227. $displayConf = $userinfo['display'];
  228. $display = array_merge($displayDefault, $displayConf);
  229. $contextExec = $this->sprintContextExec($display['contextFormat']);
  230. return sprintf($display['lineFormat'] . $display['eol'],
  231. ucfirst($userinfo['level']),
  232. $this->getMessage(), $contextExec);
  233. }
  234. /**
  235. * Send an error message somewhere
  236. *
  237. * @param array $userinfo hash of parameters
  238. *
  239. * @return void
  240. * @since version 2.0.0 (2005-10-01)
  241. * @access private
  242. */
  243. function _log($userinfo)
  244. {
  245. $logDefault = array(
  246. 'eol' => "\n",
  247. 'lineFormat' => '%1$s %2$s [%3$s] %4$s %5$s',
  248. 'contextFormat' => 'in %3$s (file %1$s on line %2$s)',
  249. 'timeFormat' => '%b %d %H:%M:%S',
  250. 'ident' => $_SERVER['REMOTE_ADDR'],
  251. 'message_type' => 3,
  252. 'destination' => get_class($this) . '.log',
  253. 'extra_headers' => ''
  254. );
  255. $logConf = $userinfo['log'];
  256. $log = array_merge($logDefault, $logConf);
  257. $message_type = $log['message_type'];
  258. $destination = '';
  259. $extra_headers = '';
  260. $send = true;
  261. switch ($message_type) {
  262. case 0: // LOG_TYPE_SYSTEM:
  263. break;
  264. case 1: // LOG_TYPE_MAIL:
  265. $destination = $log['destination'];
  266. $extra_headers = $log['extra_headers'];
  267. break;
  268. case 3: // LOG_TYPE_FILE:
  269. $destination = $log['destination'];
  270. break;
  271. default:
  272. $send = false;
  273. }
  274. if ($send) {
  275. $time = explode(' ', microtime());
  276. $time = $time[1] + $time[0];
  277. $timestamp = isset($userinfo['time']) ? $userinfo['time'] : $time;
  278. $contextExec = $this->sprintContextExec($log['contextFormat']);
  279. $message = sprintf($log['lineFormat'] . $log['eol'],
  280. strftime($log['timeFormat'], $timestamp),
  281. $log['ident'],
  282. $userinfo['level'],
  283. $this->getMessage(),
  284. $contextExec);
  285. error_log(strip_tags($message), $message_type, $destination,
  286. $extra_headers);
  287. }
  288. }
  289. /**
  290. * Default internal error handler
  291. * Dies if the error is an exception (and would have died anyway)
  292. *
  293. * @param int $code a numeric error code.
  294. * Valid are HTML_PROGRESS_ERROR_* constants
  295. * @param string $level error level ('exception', 'error', 'warning', ...)
  296. *
  297. * @return mixed
  298. * @since version 2.0.0 (2005-10-01)
  299. * @access private
  300. */
  301. function _handleError($code, $level)
  302. {
  303. if ($level == 'exception') {
  304. return PEAR_ERROR_DIE;
  305. } else {
  306. return null;
  307. }
  308. }
  309. /**
  310. * User callback to generate error messages for any instance
  311. *
  312. * @param int $code a numeric error code.
  313. * Valid are HTML_PROGRESS_ERROR_* constants
  314. * @param mixed $userinfo if you need to pass along parameters
  315. * for dynamic messages
  316. *
  317. * @return string
  318. * @since version 2.0.0 (2005-10-01)
  319. * @access private
  320. */
  321. function _msgCallback($code, $userinfo)
  322. {
  323. $errorMessages = HTML_Progress2_Error::_getErrorMessage();
  324. if (isset($errorMessages[$code])) {
  325. $mainmsg = $errorMessages[$code];
  326. } else {
  327. $mainmsg = $errorMessages[HTML_PROGRESS_ERROR_UNKNOWN];
  328. }
  329. if (is_array($userinfo)) {
  330. foreach ($userinfo as $name => $val) {
  331. if (is_array($val)) {
  332. // @ is needed in case $val is a multi-dimensional array
  333. $val = @implode(', ', $val);
  334. }
  335. if (is_object($val)) {
  336. if (method_exists($val, '__toString')) {
  337. $val = $val->__toString();
  338. } else {
  339. continue;
  340. }
  341. }
  342. $mainmsg = str_replace('%' . $name . '%', $val, $mainmsg);
  343. }
  344. }
  345. return $mainmsg;
  346. }
  347. /**
  348. * Error Message Template array
  349. *
  350. * @return string
  351. * @since version 2.0.0 (2005-10-01)
  352. * @access private
  353. */
  354. function _getErrorMessage()
  355. {
  356. $messages = array(
  357. HTML_PROGRESS2_ERROR_UNKNOWN =>
  358. 'unknown error',
  359. HTML_PROGRESS2_ERROR_INVALID_INPUT =>
  360. 'invalid input, parameter #%paramnum% '
  361. . '"%var%" was expecting '
  362. . '"%expected%", instead got "%was%"',
  363. HTML_PROGRESS2_ERROR_INVALID_CALLBACK =>
  364. 'invalid callback, parameter #%paramnum% '
  365. . '"%var%" expecting %element%,'
  366. . ' instead got "%was%" does not exists',
  367. HTML_PROGRESS2_ERROR_DEPRECATED =>
  368. 'method is deprecated '
  369. . 'use %newmethod% instead of %oldmethod%',
  370. HTML_PROGRESS2_ERROR_INVALID_OPTION =>
  371. '%element% option "%prop%" is not allowed',
  372. HTML_PROGRESS2_ERROR_INVALID_RESOURCE =>
  373. 'invalid resource, parameter #%paramnum% '
  374. . '"%var%" expecting %expected%,'
  375. . ' instead got "%resource%" does not exists'
  376. );
  377. return $messages;
  378. }
  379. }
  380. ?>