PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_virtuemart/classes/Log/win.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 272 lines | 127 code | 30 blank | 115 comment | 19 complexity | fc4f492ebc96fd8de936c0229fb29500 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: win.php 1336 2008-03-31 17:06:23Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage Log
  8. * @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. This version may have been modified pursuant
  11. * to the GNU General Public License, and as distributed it includes or
  12. * is derivative of works licensed under the GNU General Public License or
  13. * other free or open source software licenses.
  14. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. /**
  19. * $Header$
  20. *
  21. * @version $ Revision: 1.17 $
  22. * @package Log
  23. */
  24. /**
  25. * The vmLog_win class is a concrete implementation of the Log abstract
  26. * class that logs messages to a separate browser window.
  27. *
  28. * The concept for this log handler is based on part by Craig Davis' article
  29. * entitled "JavaScript Power PHP Debugging:
  30. *
  31. * http://www.zend.com/zend/tut/tutorial-DebugLib.php
  32. *
  33. * @author Jon Parise <jon@php.net>
  34. * @since Log 1.7.0
  35. * @package Log
  36. *
  37. * @example win.php Using the window handler.
  38. */
  39. class vmLog_win extends vmLog
  40. {
  41. /**
  42. * The name of the output window.
  43. * @var string
  44. * @access private
  45. */
  46. var $_name = 'LogWindow';
  47. /**
  48. * The title of the output window.
  49. * @var string
  50. * @access private
  51. */
  52. var $_title = 'Log Output Window';
  53. /**
  54. * Mapping of log priorities to colors.
  55. * @var array
  56. * @access private
  57. */
  58. var $_colors = array(
  59. PEAR_LOG_EMERG => 'red',
  60. PEAR_LOG_ALERT => 'orange',
  61. PEAR_LOG_CRIT => 'yellow',
  62. PEAR_LOG_ERR => 'green',
  63. PEAR_LOG_WARNING => 'blue',
  64. PEAR_LOG_NOTICE => 'indigo',
  65. PEAR_LOG_INFO => 'violet',
  66. PEAR_LOG_DEBUG => 'black'
  67. );
  68. /**
  69. * String buffer that holds line that are pending output.
  70. * @var array
  71. * @access private
  72. */
  73. var $_buffer = array();
  74. /**
  75. * Constructs a new vmLog_win object.
  76. *
  77. * @param string $name Ignored.
  78. * @param string $ident The identity string.
  79. * @param array $conf The configuration array.
  80. * @param int $level Log messages up to and including this level.
  81. * @access public
  82. */
  83. function vmLog_win($name, $ident = '', $conf = array(),
  84. $level = PEAR_LOG_DEBUG)
  85. {
  86. $this->_id = md5(microtime());
  87. $this->_name = $name;
  88. $this->_ident = $ident;
  89. $this->_mask = vmLog::UPTO($level);
  90. if (isset($conf['title'])) {
  91. $this->_title = $conf['title'];
  92. }
  93. if (isset($conf['colors']) && is_array($conf['colors'])) {
  94. $this->_colors = $conf['colors'];
  95. }
  96. register_shutdown_function(array(&$this, '_vmLog_win'));
  97. }
  98. /**
  99. * Destructor
  100. */
  101. function _vmLog_win()
  102. {
  103. if ($this->_opened || (count($this->_buffer) > 0)) {
  104. $this->close();
  105. }
  106. }
  107. /**
  108. * The first time open() is called, it will open a new browser window and
  109. * prepare it for output.
  110. *
  111. * This is implicitly called by log(), if necessary.
  112. *
  113. * @access public
  114. */
  115. function open()
  116. {
  117. if (!$this->_opened) {
  118. $win = $this->_name;
  119. if (!empty($this->_ident)) {
  120. $identHeader = "$win.document.writeln('<th>Ident</th>')";
  121. } else {
  122. $identHeader = '';
  123. }
  124. echo <<< END_OF_SCRIPT
  125. <script language="JavaScript">
  126. $win = window.open('', '{$this->_name}', 'toolbar=no,scrollbars,width=600,height=400');
  127. $win.document.writeln('<html>');
  128. $win.document.writeln('<head>');
  129. $win.document.writeln('<title>{$this->_title}</title>');
  130. $win.document.writeln('<style type="text/css">');
  131. $win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
  132. $win.document.writeln('td,th { font-size: 8pt; }');
  133. $win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
  134. $win.document.writeln('td,th { border-right: #999999 solid 1px; }');
  135. $win.document.writeln('</style>');
  136. $win.document.writeln('</head>');
  137. $win.document.writeln('<body>');
  138. $win.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
  139. $win.document.writeln('<tr><th>Time</th>');
  140. $identHeader
  141. $win.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
  142. </script>
  143. END_OF_SCRIPT;
  144. $this->_opened = true;
  145. }
  146. return $this->_opened;
  147. }
  148. /**
  149. * Closes the output stream if it is open. If there are still pending
  150. * lines in the output buffer, the output window will be opened so that
  151. * the buffer can be drained.
  152. *
  153. * @access public
  154. */
  155. function close()
  156. {
  157. /*
  158. * If there are still lines waiting to be written, open the output
  159. * window so that we can drain the buffer.
  160. */
  161. if (!$this->_opened && (count($this->_buffer) > 0)) {
  162. $this->open();
  163. }
  164. if ($this->_opened) {
  165. $this->_writeln('</table>');
  166. $this->_writeln('</body></html>');
  167. $this->_opened = false;
  168. }
  169. return ($this->_opened === false);
  170. }
  171. /**
  172. * Writes a single line of text to the output window.
  173. *
  174. * @param string $line The line of text to write.
  175. *
  176. * @access private
  177. */
  178. function _writeln($line)
  179. {
  180. /* Add this line to our output buffer. */
  181. $this->_buffer[] = $line;
  182. /* Buffer the output until this page's headers have been sent. */
  183. if (!headers_sent()) {
  184. return;
  185. }
  186. /* If we haven't already opened the output window, do so now. */
  187. if (!$this->_opened && !$this->open()) {
  188. return false;
  189. }
  190. /* Drain the buffer to the output window. */
  191. $win = $this->_name;
  192. foreach ($this->_buffer as $line) {
  193. echo "<script language='JavaScript'>\n";
  194. echo "$win.document.writeln('" . addslashes($line) . "');\n";
  195. echo "self.focus();\n";
  196. echo "</script>\n";
  197. }
  198. /* Now that the buffer has been drained, clear it. */
  199. $this->_buffer = array();
  200. }
  201. /**
  202. * Logs $message to the output window. The message is also passed along
  203. * to any Log_observer instances that are observing this Log.
  204. *
  205. * @param mixed $message String or object containing the message to log.
  206. * @param string $priority The priority of the message. Valid
  207. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  208. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  209. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  210. * @return boolean True on success or false on failure.
  211. * @access public
  212. */
  213. function log($message, $priority = null)
  214. {
  215. /* If a priority hasn't been specified, use the default value. */
  216. if ($priority === null) {
  217. $priority = $this->_priority;
  218. }
  219. /* Abort early if the priority is above the maximum logging level. */
  220. if (!$this->_isMasked($priority)) {
  221. return false;
  222. }
  223. /* Extract the string representation of the message. */
  224. $message = $this->_extractMessage($message);
  225. list($usec, $sec) = explode(' ', microtime());
  226. /* Build the output line that contains the log entry row. */
  227. $line = '<tr align="left" valign="top">';
  228. $line .= sprintf('<td>%s.%s</td>',
  229. strftime('%T', $sec), substr($usec, 2, 2));
  230. if (!empty($this->_ident)) {
  231. $line .= '<td>' . $this->_ident . '</td>';
  232. }
  233. $line .= '<td>' . ucfirst($this->priorityToString($priority)) . '</td>';
  234. $line .= sprintf('<td style="color: %s">%s</td>',
  235. $this->_colors[$priority],
  236. preg_replace('/\r\n|\n|\r/', '<br />', $message));
  237. $line .= '</tr>';
  238. $this->_writeln($line);
  239. $this->_announce(array('priority' => $priority, 'message' => $message));
  240. return true;
  241. }
  242. }