Merge branch 'QA_3_3'
[phpmyadmin/dkf.git] / libraries / Message.class.php
blobc31d353319eccd5c6c93336fd2090f1baacfe852
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Message
6 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * a single message
13 * simple usage examples:
14 * <code>
15 * // display simple error message 'Error'
16 * PMA_Message::error()->display();
18 * // get simple success message 'Success'
19 * $message = PMA_Message::success();
21 * // get special notice 'Some locale notice'
22 * $message = PMA_Message::notice('strSomeLocaleNotice');
24 * // display raw warning message 'This is a warning!'
25 * PMA_Message::rawWarning('This is a warning!')->display();
26 * </code>
28 * more advanced usage example:
29 * <code>
30 * // create a localized success message
31 * $message = PMA_Message::success('strSomeLocaleMessage');
33 * // create another message, a hint, with a localized string which expects
34 * // two parameters: $strSomeFootnote = 'Read the %smanual%s'
35 * $hint = PMA_Message::notice('strSomeFootnote');
36 * // replace %d with the following params
37 * $hint->addParam('[a@./Documentation.html#cfg_Example@_blank]');
38 * $hint->addParam('[/a]');
39 * // add this hint as a footnote
40 * $hint = PMA_showHint($hint);
42 * // add the retrieved footnote reference to the original message
43 * $message->addMessage($hint);
45 * // create another message ...
46 * $more = PMA_Message::notice('strSomeMoreLocale');
47 * $more->addString('strSomeEvenMoreLocale', '<br />');
48 * $more->addParam('parameter for strSomeMoreLocale');
49 * $more->addParam('more parameter for strSomeMoreLocale');
51 * // and add it also to the original message
52 * $message->addMessage($more);
53 * // finally add another raw message
54 * $message->addMessage('some final words', ' - ');
56 * // display() will now print all messages in the same order as they are added
57 * $message->display();
58 * // strSomeLocaleMessage <sup>1</sup> strSomeMoreLocale<br />
59 * // strSomeEvenMoreLocale - some final words
60 * </code>
61 * @package phpMyAdmin
63 class PMA_Message
65 const SUCCESS = 1; // 0001
66 const NOTICE = 2; // 0010
67 const WARNING = 4; // 0100
68 const ERROR = 8; // 1000
70 const SANITIZE_NONE = 0; // 0000 0000
71 const SANITIZE_STRING = 16; // 0001 0000
72 const SANITIZE_PARAMS = 32; // 0010 0000
73 const SANITIZE_BOOTH = 48; // 0011 0000
75 /**
76 * message levels
78 * @var array
80 static public $level = array (
81 PMA_Message::SUCCESS => 'success',
82 PMA_Message::NOTICE => 'notice',
83 PMA_Message::WARNING => 'warning',
84 PMA_Message::ERROR => 'error',
87 /**
88 * The message number
90 * @access protected
91 * @var integer
93 protected $_number = PMA_Message::NOTICE;
95 /**
96 * The locale string identifier
98 * @access protected
99 * @var string
101 protected $_string = '';
104 * The formatted message
106 * @access protected
107 * @var string
109 protected $_message = '';
112 * Whether the message was already displayed
114 * @access protected
115 * @var boolean
117 protected $_is_displayed = false;
120 * Unique id
122 * @access protected
123 * @var string
125 protected $_hash = null;
128 * holds parameters
130 * @access protected
131 * @var array
133 protected $_params = array();
136 * holds additional messages
138 * @access protected
139 * @var array
141 protected $_added_messages = array();
144 * Constructor
146 * @uses PMA_Message::setNumber()
147 * @uses PMA_Message::setString()
148 * @uses PMA_Message::setParams()
149 * @uses PMA_Message::NOTICE
150 * @uses PMA_Message::SANITIZE_NONE
151 * @uses PMA_Message::SANITIZE_STRING
152 * @uses PMA_Message::SANITIZE_PARAMS
153 * @param string $string
154 * @param integer $number
155 * @param array $$params
156 * @param boolean $sanitize
158 public function __construct($string = '', $number = PMA_Message::NOTICE,
159 $params = array(), $sanitize = PMA_Message::SANITIZE_NONE)
161 $this->setString($string, $sanitize & PMA_Message::SANITIZE_STRING);
162 $this->setNumber($number);
163 $this->setParams($params, $sanitize & PMA_Message::SANITIZE_PARAMS);
167 * magic method: return string representation for this object
169 * @uses PMA_Message::getMessage()
170 * @return string
172 public function __toString()
174 return $this->getMessage();
178 * get PMA_Message of type success
180 * shorthand for getting a simple success message
182 * @static
183 * @uses PMA_Message as returned object
184 * @uses PMA_Message::SUCCESS
185 * @param string $string a localized string e.g. 'strSuccess'
186 * @return PMA_Message
188 static public function success($string = '')
190 if (empty($string)) {
191 $string = 'strSuccess';
194 return new PMA_Message($string, PMA_Message::SUCCESS);
198 * get PMA_Message of type error
200 * shorthand for getting a simple error message
202 * @static
203 * @uses PMA_Message as returned object
204 * @uses PMA_Message::ERROR
205 * @param string $string a localized string e.g. 'strError'
206 * @return PMA_Message
208 static public function error($string = '')
210 if (empty($string)) {
211 $string = 'strError';
214 return new PMA_Message($string, PMA_Message::ERROR);
218 * get PMA_Message of type warning
220 * shorthand for getting a simple warning message
222 * @static
223 * @uses PMA_Message as returned object
224 * @uses PMA_Message::WARNING
225 * @param string $string a localized string e.g. 'strSetupWarning'
226 * @return PMA_Message
228 static public function warning($string)
230 return new PMA_Message($string, PMA_Message::WARNING);
234 * get PMA_Message of type notice
236 * shorthand for getting a simple notice message
238 * @static
239 * @uses PMA_Message as returned object
240 * @uses PMA_Message::NOTICE
241 * @param string $string a localized string e.g. 'strRelationNotWorking'
242 * @return PMA_Message
244 static public function notice($string)
246 return new PMA_Message($string, PMA_Message::NOTICE);
250 * get PMA_Message with customized content
252 * shorthand for getting a customized message
254 * @static
255 * @uses PMA_Message as returned object
256 * @uses PMA_Message::setMessage()
257 * @param string $message
258 * @param integer $type
259 * @return PMA_Message
261 static public function raw($message, $type = PMA_Message::NOTICE)
263 $r = new PMA_Message('', $type);
264 $r->setMessage($message);
265 return $r;
269 * get PMA_Message for number of affected rows
271 * shorthand for getting a customized message
273 * @static
274 * @uses PMA_Message as returned object
275 * @uses PMA_Message::success()
276 * @uses PMA_Message::addParam()
277 * @param integer $rows Number of rows
278 * @return PMA_Message
280 static public function affected_rows($rows)
282 $message = PMA_Message::success(_ngettext('%1$d row affected.', '%1$d rows affected.', $rows));
283 $message->addParam($rows);
284 return $message;
288 * get PMA_Message for number of deleted rows
290 * shorthand for getting a customized message
292 * @static
293 * @uses PMA_Message as returned object
294 * @uses PMA_Message::success()
295 * @uses PMA_Message::addParam()
296 * @param integer $rows Number of rows
297 * @return PMA_Message
299 static public function deleted_rows($rows)
301 $message = PMA_Message::success(_ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows));
302 $message->addParam($rows);
303 return $message;
307 * get PMA_Message for number of inserted rows
309 * shorthand for getting a customized message
311 * @static
312 * @uses PMA_Message as returned object
313 * @uses PMA_Message::success()
314 * @uses PMA_Message::addParam()
315 * @param integer $rows Number of rows
316 * @return PMA_Message
318 static public function inserted_rows($rows)
320 $message = PMA_Message::success(_ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows));
321 $message->addParam($rows);
322 return $message;
326 * get PMA_Message of type error with custom content
328 * shorthand for getting a customized error message
330 * @static
331 * @uses PMA_Message::raw()
332 * @uses PMA_Message::ERROR
333 * @param string $message
334 * @return PMA_Message
336 static public function rawError($message)
338 return PMA_Message::raw($message, PMA_Message::ERROR);
342 * get PMA_Message of type warning with custom content
344 * shorthand for getting a customized warning message
346 * @static
347 * @uses PMA_Message::raw()
348 * @uses PMA_Message::WARNING
349 * @param string $message
350 * @return PMA_Message
352 static public function rawWarning($message)
354 return PMA_Message::raw($message, PMA_Message::WARNING);
358 * get PMA_Message of type notice with custom content
360 * shorthand for getting a customized notice message
362 * @static
363 * @uses PMA_Message::raw()
364 * @uses PMA_Message::NOTICE
365 * @param string $message
366 * @return PMA_Message
368 static public function rawNotice($message)
370 return PMA_Message::raw($message, PMA_Message::NOTICE);
374 * get PMA_Message of type success with custom content
376 * shorthand for getting a customized success message
378 * @static
379 * @uses PMA_Message::raw()
380 * @uses PMA_Message::SUCCESS
381 * @param string $message
382 * @return PMA_Message
384 static public function rawSuccess($message)
386 return PMA_Message::raw($message, PMA_Message::SUCCESS);
390 * returns whether this message is a success message or not
391 * and optionaly makes this message a success message
393 * @uses PMA_Message::SUCCESS
394 * @uses PMA_Message::setNumber()
395 * @uses PMA_Message::getNumber()
396 * @param boolean $set
397 * @return boolean whether this is a success message or not
399 public function isSuccess($set = false)
401 if ($set) {
402 $this->setNumber(PMA_Message::SUCCESS);
405 return $this->getNumber() === PMA_Message::SUCCESS;
409 * returns whether this message is a notice message or not
410 * and optionally makes this message a notice message
412 * @uses PMA_Message::NOTICE
413 * @uses PMA_Message::setNumber()
414 * @uses PMA_Message::getNumber()
415 * @param boolean $set
416 * @return boolean whether this is a notice message or not
418 public function isNotice($set = false)
420 if ($set) {
421 $this->setNumber(PMA_Message::NOTICE);
424 return $this->getNumber() === PMA_Message::NOTICE;
428 * returns whether this message is a warning message or not
429 * and optionally makes this message a warning message
431 * @uses PMA_Message::WARNING
432 * @uses PMA_Message::setNumber()
433 * @uses PMA_Message::getNumber()
434 * @param boolean $set
435 * @return boolean whether this is a warning message or not
437 public function isWarning($set = false)
439 if ($set) {
440 $this->setNumber(PMA_Message::WARNING);
443 return $this->getNumber() === PMA_Message::WARNING;
447 * returns whether this message is an error message or not
448 * and optionally makes this message an error message
450 * @uses PMA_Message::ERROR
451 * @uses PMA_Message::setNumber()
452 * @uses PMA_Message::getNumber()
453 * @param boolean $set
454 * @return boolean whether this is an error message or not
456 public function isError($set = false)
458 if ($set) {
459 $this->setNumber(PMA_Message::ERROR);
462 return $this->getNumber() === PMA_Message::ERROR;
466 * set raw message (overrides string)
468 * @uses PMA_Message::$_message to set it
469 * @uses PMA_Message::sanitize()
470 * @param string $message
471 * @param boolean $sanitize whether to sanitize $message or not
473 public function setMessage($message, $sanitize = false)
475 if ($sanitize) {
476 $message = PMA_Message::sanitize($message);
478 $this->_message = $message;
482 * set string (does not take effect if raw message is set)
484 * @uses PMA_Message::$_string to set it
485 * @uses PMA_Message::sanitize()
486 * @param string $_string
487 * @param boolean $sanitize whether to sanitize $string or not
489 public function setString($_string, $sanitize = true)
491 if ($sanitize) {
492 $_string = PMA_Message::sanitize($_string);
494 $this->_string = $_string;
498 * set message type number
500 * @uses PMA_Message::$_number to set it
501 * @param integer $number
503 public function setNumber($number)
505 $this->_number = $number;
509 * add parameter, usually in conjunction with strings
511 * usage
512 * <code>
513 * $message->addParam('strLocale', false);
514 * $message->addParam('[em]some string[/em]');
515 * $message->addParam('<img src="img" />', false);
516 * </code>
518 * @uses htmlspecialchars()
519 * @uses PMA_Message::$_params to fill
520 * @uses PMA_Message::notice()
521 * @param mixed $param
522 * @param boolean $raw
524 public function addParam($param, $raw = true)
526 if ($param instanceof PMA_Message) {
527 $this->_params[] = $param;
528 } elseif ($raw) {
529 $this->_params[] = htmlspecialchars($param);
530 } else {
531 $this->_params[] = PMA_Message::notice($param);
536 * add another string to be concatenated on displaying
538 * @uses PMA_Message::$_added_messages to fill
539 * @uses PMA_Message::notice()
540 * @param string $string to be added
541 * @param string $separator to use between this and previous string/message
543 public function addString($string, $separator = ' ')
545 $this->_added_messages[] = $separator;
546 $this->_added_messages[] = PMA_Message::notice($string);
550 * add a bunch of messages at once
552 * @uses PMA_Message::addMessage()
553 * @param array $messages to be added
554 * @param string $separator to use between this and previous string/message
556 public function addMessages($messages, $separator = ' ')
558 foreach ($messages as $message) {
559 $this->addMessage($message, $separator);
564 * add another raw message to be concatenated on displaying
566 * @uses PMA_Message::$_added_messages to fill
567 * @uses PMA_Message::rawNotice()
568 * @param mixed $message to be added
569 * @param string $separator to use between this and previous string/message
571 public function addMessage($message, $separator = ' ')
573 if (strlen($separator)) {
574 $this->_added_messages[] = $separator;
577 if ($message instanceof PMA_Message) {
578 $this->_added_messages[] = $message;
579 } else {
580 $this->_added_messages[] = PMA_Message::rawNotice($message);
585 * set all params at once, usually used in conjunction with string
587 * @uses PMA_Message::sanitize()
588 * @uses PMA_Message::$_params to set
589 * @param array $params
590 * @param boolean $sanitize
592 public function setParams($params, $sanitize = false)
594 if ($sanitize) {
595 $params = PMA_Message::sanitize($params);
597 $this->_params = $params;
601 * return all parameters
603 * @uses PMA_Message::$_params as return value
604 * @return array
606 public function getParams()
608 return $this->_params;
612 * return all added messages
614 * @uses PMA_Message::$_added_messages as return value
615 * @return array
617 public function getAddedMessages()
619 return $this->_added_messages;
623 * Sanitizes $message
625 * @static
626 * @uses is_array()
627 * @uses htmlspecialchars()
628 * @uses PMA_Message::sanitize() recursiv
629 * @param mixed the message(s)
630 * @return mixed the sanitized message(s)
631 * @access public
633 static public function sanitize($message)
635 if (is_array($message)) {
636 foreach ($message as $key => $val) {
637 $message[$key] = PMA_Message::sanitize($val);
640 return $message;
643 return htmlspecialchars($message);
647 * decode $message, taking into account our special codes
648 * for formatting
650 * @static
651 * @uses PREG_SET_ORDER
652 * @uses in_array()
653 * @uses preg_match_all()
654 * @uses preg_match()
655 * @uses preg_replace()
656 * @uses substr()
657 * @uses strtr()
658 * @param string $message the message
659 * @return string the decoded message
660 * @access public
662 static public function decodeBB($message)
664 $replace_pairs = array(
665 '[i]' => '<em>', // deprecated by em
666 '[/i]' => '</em>', // deprecated by em
667 '[em]' => '<em>',
668 '[/em]' => '</em>',
669 '[b]' => '<strong>', // deprecated by strong
670 '[/b]' => '</strong>', // deprecated by strong
671 '[strong]' => '<strong>',
672 '[/strong]' => '</strong>',
673 '[tt]' => '<code>', // deprecated by CODE or KBD
674 '[/tt]' => '</code>', // deprecated by CODE or KBD
675 '[code]' => '<code>',
676 '[/code]' => '</code>',
677 '[kbd]' => '<kbd>',
678 '[/kbd]' => '</kbd>',
679 '[br]' => '<br />',
680 '[/a]' => '</a>',
681 '[sup]' => '<sup>',
682 '[/sup]' => '</sup>',
685 $message = strtr($message, $replace_pairs);
687 $pattern = '/\[a@([^"@]*)@([^]"]*)\]/';
689 if (preg_match_all($pattern, $message, $founds, PREG_SET_ORDER)) {
690 $valid_links = array(
691 'http', // default http:// links (and https://)
692 './Do', // ./Documentation
695 foreach ($founds as $found) {
696 // only http... and ./Do... allowed
697 if (! in_array(substr($found[1], 0, 4), $valid_links)) {
698 return $message;
700 // a-z and _ allowed in target
701 if (! empty($found[2]) && preg_match('/[^a-z_]+/i', $found[2])) {
702 return $message;
706 $message = preg_replace($pattern, '<a href="\1" target="\2">', $message);
709 return $message;
713 * wrapper for sprintf()
715 * @uses sprintf()
716 * @uses func_get_args()
717 * @uses is_array()
718 * @uses array_unshift()
719 * @uses call_user_func_array()
720 * @return string formatted
722 static public function format()
724 $params = func_get_args();
725 if (is_array($params[1])) {
726 array_unshift($params[1], $params[0]);
727 $params = $params[1];
730 return call_user_func_array('sprintf', $params);
734 * returns unique PMA_Message::$_hash, if not exists it will be created
736 * @uses PMA_Message::$_hash as return value and to set it if required
737 * @uses PMA_Message::getNumber()
738 * @uses PMA_Message::$_string
739 * @uses PMA_Message::$_message
740 * @uses md5()
741 * @param string $file
742 * @return string PMA_Message::$_hash
744 public function getHash()
746 if (null === $this->_hash) {
747 $this->_hash = md5(
748 $this->getNumber() .
749 $this->_string .
750 $this->_message
754 return $this->_hash;
758 * returns compiled message
760 * @uses PMA_Message::$_message as return value
761 * @uses PMA_Message::getString()
762 * @uses PMA_Message::getParams()
763 * @uses PMA_Message::format()
764 * @uses PMA_Message::decodeBB()
765 * @uses PMA_Message::getAddedMessages()
766 * @uses strlen()
767 * @return string complete message
769 public function getMessage()
771 $message = $this->_message;
773 if (0 === strlen($message)) {
774 $string = $this->getString();
775 if (isset($GLOBALS[$string])) {
776 $message = $GLOBALS[$string];
777 } elseif (0 === strlen($string)) {
778 $message = '';
779 } else {
780 $message = $string;
784 if (count($this->getParams()) > 0) {
785 $message = PMA_Message::format($message, $this->getParams());
788 $message = PMA_Message::decodeBB($message);
790 foreach ($this->getAddedMessages() as $add_message) {
791 $message .= $add_message;
794 return $message;
798 * returns PMA_Message::$_string
800 * @uses PMA_Message::$_string as return value
801 * @return string PMA_Message::$_string
803 public function getString()
805 return $this->_string;
809 * returns PMA_Message::$_number
811 * @uses PMA_Message::$_number as return value
812 * @return integer PMA_Message::$_number
814 public function getNumber()
816 return $this->_number;
820 * returns level of message
822 * @uses PMA_Message::$level
823 * @uses PMA_Message::getNumber()
824 * @return string level of message
826 public function getLevel()
828 return PMA_Message::$level[$this->getNumber()];
832 * Displays the message in HTML
834 * @uses PMA_Message::getLevel()
835 * @uses PMA_Message::getMessage()
836 * @uses PMA_Message::isDisplayed()
838 public function display()
840 echo $this->getDisplay();
841 $this->isDisplayed(true);
845 * returns HTML code for displaying this message
847 * @return string whole message box
849 public function getDisplay()
851 return '<div class="' . $this->getLevel() . '">'
852 . $this->getMessage() . '</div>';
856 * sets and returns whether the message was displayed or not
858 * @uses PMA_Message::$_is_displayed to set it and/or return it
859 * @param boolean $is_displayed
860 * @return boolean PMA_Message::$_is_displayed
862 public function isDisplayed($is_displayed = false)
864 if ($is_displayed) {
865 $this->_is_displayed = true;
868 return $this->_is_displayed;