Convert Excel column name correctly
[phpmyadmin/arisferyanto.git] / libraries / Message.class.php
blobe7947b85465f3c40fe8aba1ebb9f8fa00fdf79df
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Message
6 * @author Sebastian Mendel <info@sebastianmendel.de>
7 * @version $Id$
8 * @package phpMyAdmin
9 */
11 /**
12 * a single message
14 * simple usage examples:
15 * <code>
16 * // display simple error message 'Error'
17 * PMA_Message::error()->display();
19 * // get simple success message 'Success'
20 * $message = PMA_Message::success();
22 * // get special notice 'Some locale notice'
23 * $message = PMA_Message::notice('strSomeLocaleNotice');
25 * // display raw warning message 'This is a warning!'
26 * PMA_Message::rawWarning('This is a warning!')->display();
27 * </code>
29 * more advanced usage example:
30 * <code>
31 * // create a localized success message
32 * $message = PMA_Message::success('strSomeLocaleMessage');
34 * // create another message, a hint, with a localized string which expects
35 * // two parameters: $strSomeFootnote = 'Read the %smanual%s'
36 * $hint = PMA_Message::notice('strSomeFootnote');
37 * // replace %d with the following params
38 * $hint->addParam('[a@./Documentation.html#cfg_Example@_blank]');
39 * $hint->addParam('[/a]');
40 * // add this hint as a footnote
41 * $hint = PMA_showHint($hint);
43 * // add the retrieved footnote reference to the original message
44 * $message->addMessage($hint);
46 * // create another message ...
47 * $more = PMA_Message::notice('strSomeMoreLocale');
48 * $more->addString('strSomeEvenMoreLocale', '<br />');
49 * $more->addParam('parameter for strSomeMoreLocale');
50 * $more->addParam('more parameter for strSomeMoreLocale');
52 * // and add it also to the original message
53 * $message->addMessage($more);
54 * // finally add another raw message
55 * $message->addMessage('some final words', ' - ');
57 * // display() will now print all messages in the same order as they are added
58 * $message->display();
59 * // strSomeLocaleMessage <sup>1</sup> strSomeMoreLocale<br />
60 * // strSomeEvenMoreLocale - some final words
61 * </code>
62 * @package phpMyAdmin
64 class PMA_Message
66 const SUCCESS = 1; // 0001
67 const NOTICE = 2; // 0010
68 const WARNING = 4; // 0100
69 const ERROR = 8; // 1000
71 const SANITIZE_NONE = 0; // 0000 0000
72 const SANITIZE_STRING = 16; // 0001 0000
73 const SANITIZE_PARAMS = 32; // 0010 0000
74 const SANITIZE_BOOTH = 48; // 0011 0000
76 /**
77 * message levels
79 * @var array
81 static public $level = array (
82 PMA_Message::SUCCESS => 'success',
83 PMA_Message::NOTICE => 'notice',
84 PMA_Message::WARNING => 'warning',
85 PMA_Message::ERROR => 'error',
88 /**
89 * The message number
91 * @access protected
92 * @var integer
94 protected $_number = PMA_Message::NOTICE;
96 /**
97 * The locale string identifier
99 * @access protected
100 * @var string
102 protected $_string = '';
105 * The formatted message
107 * @access protected
108 * @var string
110 protected $_message = '';
113 * Whether the message was already displayed
115 * @access protected
116 * @var boolean
118 protected $_is_displayed = false;
121 * Unique id
123 * @access protected
124 * @var string
126 protected $_hash = null;
129 * holds parameters
131 * @access protected
132 * @var array
134 protected $_params = array();
137 * holds additional messages
139 * @access protected
140 * @var array
142 protected $_added_messages = array();
145 * Constructor
147 * @uses PMA_Message::setNumber()
148 * @uses PMA_Message::setString()
149 * @uses PMA_Message::setParams()
150 * @uses PMA_Message::NOTICE
151 * @uses PMA_Message::SANITIZE_NONE
152 * @uses PMA_Message::SANITIZE_STRING
153 * @uses PMA_Message::SANITIZE_PARAMS
154 * @param string $string
155 * @param integer $number
156 * @param array $$params
157 * @param boolean $sanitize
159 public function __construct($string = '', $number = PMA_Message::NOTICE,
160 $params = array(), $sanitize = PMA_Message::SANITIZE_NONE)
162 $this->setString($string, $sanitize & PMA_Message::SANITIZE_STRING);
163 $this->setNumber($number);
164 $this->setParams($params, $sanitize & PMA_Message::SANITIZE_PARAMS);
168 * magic method: return string representation for this object
170 * @uses PMA_Message::getMessage()
171 * @return string
173 public function __toString()
175 return $this->getMessage();
179 * get PMA_Message of type success
181 * shorthand for getting a simple success message
183 * @static
184 * @uses PMA_Message as returned object
185 * @uses PMA_Message::SUCCESS
186 * @param string $string a localized string e.g. 'strSuccess'
187 * @return PMA_Message
189 static public function success($string = '')
191 if (empty($string)) {
192 $string = 'strSuccess';
195 return new PMA_Message($string, PMA_Message::SUCCESS);
199 * get PMA_Message of type error
201 * shorthand for getting a simple error message
203 * @static
204 * @uses PMA_Message as returned object
205 * @uses PMA_Message::ERROR
206 * @param string $string a localized string e.g. 'strError'
207 * @return PMA_Message
209 static public function error($string = '')
211 if (empty($string)) {
212 $string = 'strError';
215 return new PMA_Message($string, PMA_Message::ERROR);
219 * get PMA_Message of type warning
221 * shorthand for getting a simple warning message
223 * @static
224 * @uses PMA_Message as returned object
225 * @uses PMA_Message::WARNING
226 * @param string $string a localized string e.g. 'strSetupWarning'
227 * @return PMA_Message
229 static public function warning($string)
231 return new PMA_Message($string, PMA_Message::WARNING);
235 * get PMA_Message of type notice
237 * shorthand for getting a simple notice message
239 * @static
240 * @uses PMA_Message as returned object
241 * @uses PMA_Message::NOTICE
242 * @param string $string a localized string e.g. 'strRelationNotWorking'
243 * @return PMA_Message
245 static public function notice($string)
247 return new PMA_Message($string, PMA_Message::NOTICE);
251 * get PMA_Message with customized content
253 * shorthand for getting a customized message
255 * @static
256 * @uses PMA_Message as returned object
257 * @uses PMA_Message::setMessage()
258 * @param string $message
259 * @param integer $type
260 * @return PMA_Message
262 static public function raw($message, $type = PMA_Message::NOTICE)
264 $r = new PMA_Message('', $type);
265 $r->setMessage($message);
266 return $r;
270 * get PMA_Message of type error with custom content
272 * shorthand for getting a customized error message
274 * @static
275 * @uses PMA_Message::raw()
276 * @uses PMA_Message::ERROR
277 * @param string $message
278 * @return PMA_Message
280 static public function rawError($message)
282 return PMA_Message::raw($message, PMA_Message::ERROR);
286 * get PMA_Message of type warning with custom content
288 * shorthand for getting a customized warning message
290 * @static
291 * @uses PMA_Message::raw()
292 * @uses PMA_Message::WARNING
293 * @param string $message
294 * @return PMA_Message
296 static public function rawWarning($message)
298 return PMA_Message::raw($message, PMA_Message::WARNING);
302 * get PMA_Message of type notice with custom content
304 * shorthand for getting a customized notice message
306 * @static
307 * @uses PMA_Message::raw()
308 * @uses PMA_Message::NOTICE
309 * @param string $message
310 * @return PMA_Message
312 static public function rawNotice($message)
314 return PMA_Message::raw($message, PMA_Message::NOTICE);
318 * get PMA_Message of type success with custom content
320 * shorthand for getting a customized success message
322 * @static
323 * @uses PMA_Message::raw()
324 * @uses PMA_Message::SUCCESS
325 * @param string $message
326 * @return PMA_Message
328 static public function rawSuccess($message)
330 return PMA_Message::raw($message, PMA_Message::SUCCESS);
334 * returns whether this message is a success message or not
335 * and optionaly makes this message a success message
337 * @uses PMA_Message::SUCCESS
338 * @uses PMA_Message::setNumber()
339 * @uses PMA_Message::getNumber()
340 * @param boolean $set
341 * @return boolean whether this is a success message or not
343 public function isSuccess($set = false)
345 if ($set) {
346 $this->setNumber(PMA_Message::SUCCESS);
349 return $this->getNumber() === PMA_Message::SUCCESS;
353 * returns whether this message is a notice message or not
354 * and optionally makes this message a notice message
356 * @uses PMA_Message::NOTICE
357 * @uses PMA_Message::setNumber()
358 * @uses PMA_Message::getNumber()
359 * @param boolean $set
360 * @return boolean whether this is a notice message or not
362 public function isNotice($set = false)
364 if ($set) {
365 $this->setNumber(PMA_Message::NOTICE);
368 return $this->getNumber() === PMA_Message::NOTICE;
372 * returns whether this message is a warning message or not
373 * and optionally makes this message a warning message
375 * @uses PMA_Message::WARNING
376 * @uses PMA_Message::setNumber()
377 * @uses PMA_Message::getNumber()
378 * @param boolean $set
379 * @return boolean whether this is a warning message or not
381 public function isWarning($set = false)
383 if ($set) {
384 $this->setNumber(PMA_Message::WARNING);
387 return $this->getNumber() === PMA_Message::WARNING;
391 * returns whether this message is an error message or not
392 * and optionally makes this message an error message
394 * @uses PMA_Message::ERROR
395 * @uses PMA_Message::setNumber()
396 * @uses PMA_Message::getNumber()
397 * @param boolean $set
398 * @return boolean whether this is an error message or not
400 public function isError($set = false)
402 if ($set) {
403 $this->setNumber(PMA_Message::ERROR);
406 return $this->getNumber() === PMA_Message::ERROR;
410 * set raw message (overrides string)
412 * @uses PMA_Message::$_message to set it
413 * @uses PMA_Message::sanitize()
414 * @param string $message
415 * @param boolean $sanitize whether to sanitize $message or not
417 public function setMessage($message, $sanitize = false)
419 if ($sanitize) {
420 $message = PMA_Message::sanitize($message);
422 $this->_message = $message;
426 * set string (does not take effect if raw message is set)
428 * @uses PMA_Message::$_string to set it
429 * @uses PMA_Message::sanitize()
430 * @param string $_string
431 * @param boolean $sanitize whether to sanitize $string or not
433 public function setString($_string, $sanitize = true)
435 if ($sanitize) {
436 $_string = PMA_Message::sanitize($_string);
438 $this->_string = $_string;
442 * set message type number
444 * @uses PMA_Message::$_number to set it
445 * @param integer $number
447 public function setNumber($number)
449 $this->_number = $number;
453 * add parameter, usually in conjunction with strings
455 * usage
456 * <code>
457 * $message->addParam('strLocale', false);
458 * $message->addParam('[em]some string[/em]');
459 * $message->addParam('<img src="img" />', false);
460 * </code>
462 * @uses htmlspecialchars()
463 * @uses PMA_Message::$_params to fill
464 * @uses PMA_Message::notice()
465 * @param mixed $param
466 * @param boolean $raw
468 public function addParam($param, $raw = true)
470 if ($param instanceof PMA_Message) {
471 $this->_params[] = $param;
472 } elseif ($raw) {
473 $this->_params[] = htmlspecialchars($param);
474 } else {
475 $this->_params[] = PMA_Message::notice($param);
480 * add another string to be concatenated on displaying
482 * @uses PMA_Message::$_added_messages to fill
483 * @uses PMA_Message::notice()
484 * @param string $string to be added
485 * @param string $separator to use between this and previous string/message
487 public function addString($string, $separator = ' ')
489 $this->_added_messages[] = $separator;
490 $this->_added_messages[] = PMA_Message::notice($string);
494 * add a bunch of messages at once
496 * @uses PMA_Message::addMessage()
497 * @param array $messages to be added
498 * @param string $separator to use between this and previous string/message
500 public function addMessages($messages, $separator = ' ')
502 foreach ($messages as $message) {
503 $this->addMessage($message, $separator);
508 * add another raw message to be concatenated on displaying
510 * @uses PMA_Message::$_added_messages to fill
511 * @uses PMA_Message::rawNotice()
512 * @param mixed $message to be added
513 * @param string $separator to use between this and previous string/message
515 public function addMessage($message, $separator = ' ')
517 if (strlen($separator)) {
518 $this->_added_messages[] = $separator;
521 if ($message instanceof PMA_Message) {
522 $this->_added_messages[] = $message;
523 } else {
524 $this->_added_messages[] = PMA_Message::rawNotice($message);
529 * set all params at once, usually used in conjunction with string
531 * @uses PMA_Message::sanitize()
532 * @uses PMA_Message::$_params to set
533 * @param array $params
534 * @param boolean $sanitize
536 public function setParams($params, $sanitize = false)
538 if ($sanitize) {
539 $params = PMA_Message::sanitize($params);
541 $this->_params = $params;
545 * return all parameters
547 * @uses PMA_Message::$_params as return value
548 * @return array
550 public function getParams()
552 return $this->_params;
556 * return all added messages
558 * @uses PMA_Message::$_added_messages as return value
559 * @return array
561 public function getAddedMessages()
563 return $this->_added_messages;
567 * Sanitizes $message
569 * @static
570 * @uses is_array()
571 * @uses htmlspecialchars()
572 * @uses PMA_Message::sanitize() recursiv
573 * @param mixed the message(s)
574 * @return mixed the sanitized message(s)
575 * @access public
577 static public function sanitize($message)
579 if (is_array($message)) {
580 foreach ($message as $key => $val) {
581 $message[$key] = PMA_Message::sanitize($val);
584 return $message;
587 return htmlspecialchars($message);
591 * decode $message, taking into account our special codes
592 * for formatting
594 * @static
595 * @uses PREG_SET_ORDER
596 * @uses in_array()
597 * @uses preg_match_all()
598 * @uses preg_match()
599 * @uses preg_replace()
600 * @uses substr()
601 * @uses strtr()
602 * @param string $message the message
603 * @return string the decoded message
604 * @access public
606 static public function decodeBB($message)
608 $replace_pairs = array(
609 '[i]' => '<em>', // deprecated by em
610 '[/i]' => '</em>', // deprecated by em
611 '[em]' => '<em>',
612 '[/em]' => '</em>',
613 '[b]' => '<strong>', // deprecated by strong
614 '[/b]' => '</strong>', // deprecated by strong
615 '[strong]' => '<strong>',
616 '[/strong]' => '</strong>',
617 '[tt]' => '<code>', // deprecated by CODE or KBD
618 '[/tt]' => '</code>', // deprecated by CODE or KBD
619 '[code]' => '<code>',
620 '[/code]' => '</code>',
621 '[kbd]' => '<kbd>',
622 '[/kbd]' => '</kbd>',
623 '[br]' => '<br />',
624 '[/a]' => '</a>',
625 '[sup]' => '<sup>',
626 '[/sup]' => '</sup>',
629 $message = strtr($message, $replace_pairs);
631 $pattern = '/\[a@([^"@]*)@([^]"]*)\]/';
633 if (preg_match_all($pattern, $message, $founds, PREG_SET_ORDER)) {
634 $valid_links = array(
635 'http', // default http:// links (and https://)
636 './Do', // ./Documentation
639 foreach ($founds as $found) {
640 // only http... and ./Do... allowed
641 if (! in_array(substr($found[1], 0, 4), $valid_links)) {
642 return $message;
644 // a-z and _ allowed in target
645 if (! empty($found[2]) && preg_match('/[^a-z_]+/i', $found[2])) {
646 return $message;
650 $message = preg_replace($pattern, '<a href="\1" target="\2">', $message);
653 return $message;
657 * wrapper for sprintf()
659 * @uses sprintf()
660 * @uses func_get_args()
661 * @uses is_array()
662 * @uses array_unshift()
663 * @uses call_user_func_array()
664 * @return string formatted
666 static public function format()
668 $params = func_get_args();
669 if (is_array($params[1])) {
670 array_unshift($params[1], $params[0]);
671 $params = $params[1];
674 return call_user_func_array('sprintf', $params);
678 * returns unique PMA_Message::$_hash, if not exists it will be created
680 * @uses PMA_Message::$_hash as return value and to set it if required
681 * @uses PMA_Message::getNumber()
682 * @uses PMA_Message::$_string
683 * @uses PMA_Message::$_message
684 * @uses md5()
685 * @param string $file
686 * @return string PMA_Message::$_hash
688 public function getHash()
690 if (null === $this->_hash) {
691 $this->_hash = md5(
692 $this->getNumber() .
693 $this->_string .
694 $this->_message
698 return $this->_hash;
702 * returns compiled message
704 * @uses PMA_Message::$_message as return value
705 * @uses PMA_Message::getString()
706 * @uses PMA_Message::getParams()
707 * @uses PMA_Message::format()
708 * @uses PMA_Message::decodeBB()
709 * @uses PMA_Message::getAddedMessages()
710 * @uses strlen()
711 * @return string complete message
713 public function getMessage()
715 $message = $this->_message;
717 if (0 === strlen($message)) {
718 $string = $this->getString();
719 if (isset($GLOBALS[$string])) {
720 $message = $GLOBALS[$string];
721 } elseif (0 === strlen($string)) {
722 $message = '';
723 } else {
724 $message = $string;
728 if (count($this->getParams()) > 0) {
729 $message = PMA_Message::format($message, $this->getParams());
732 $message = PMA_Message::decodeBB($message);
734 foreach ($this->getAddedMessages() as $add_message) {
735 $message .= $add_message;
738 return $message;
742 * returns PMA_Message::$_string
744 * @uses PMA_Message::$_string as return value
745 * @return string PMA_Message::$_string
747 public function getString()
749 return $this->_string;
753 * returns PMA_Message::$_number
755 * @uses PMA_Message::$_number as return value
756 * @return integer PMA_Message::$_number
758 public function getNumber()
760 return $this->_number;
764 * returns level of message
766 * @uses PMA_Message::$level
767 * @uses PMA_Message::getNumber()
768 * @return string level of message
770 public function getLevel()
772 return PMA_Message::$level[$this->getNumber()];
776 * Displays the message in HTML
778 * @uses PMA_Message::getLevel()
779 * @uses PMA_Message::getMessage()
780 * @uses PMA_Message::isDisplayed()
782 public function display()
784 echo $this->getDisplay();
785 $this->isDisplayed(true);
789 * returns HTML code for displaying this message
791 * @return string whole message box
793 public function getDisplay()
795 return '<div class="' . $this->getLevel() . '">'
796 . $this->getMessage() . '</div>';
800 * sets and returns whether the message was displayed or not
802 * @uses PMA_Message::$_is_displayed to set it and/or return it
803 * @param boolean $is_displayed
804 * @return boolean PMA_Message::$_is_displayed
806 public function isDisplayed($is_displayed = false)
808 if ($is_displayed) {
809 $this->_is_displayed = true;
812 return $this->_is_displayed;