Remove some useless comments
[phpmyadmin.git] / libraries / classes / Message.php
blob17609d2fbab1d9d83f1b10c943a0c7ba46e7d466
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use function array_unshift;
8 use function count;
9 use function htmlspecialchars;
10 use function is_array;
11 use function is_float;
12 use function is_int;
13 use function md5;
14 use function sprintf;
15 use function strlen;
17 /**
18 * a single message
20 * simple usage examples:
21 * <code>
22 * // display simple error message 'Error'
23 * echo Message::error()->getDisplay();
25 * // get simple success message 'Success'
26 * $message = Message::success();
28 * // get special notice
29 * $message = Message::notice(__('This is a localized notice'));
30 * </code>
32 * more advanced usage example:
33 * <code>
34 * // create another message, a hint, with a localized string which expects
35 * $hint = Message::notice('Read the %smanual%s');
36 * // replace placeholders with the following params
37 * $hint->addParam('[doc@cfg_Example]');
38 * $hint->addParam('[/doc]');
39 * // add this hint as a tooltip
40 * $hint = showHint($hint);
42 * // add the retrieved tooltip reference to the original message
43 * $message->addMessage($hint);
44 * </code>
46 class Message
48 public const SUCCESS = 1; // 0001
49 public const NOTICE = 2; // 0010
50 public const ERROR = 8; // 1000
52 public const SANITIZE_NONE = 0; // 0000 0000
53 public const SANITIZE_STRING = 16; // 0001 0000
54 public const SANITIZE_PARAMS = 32; // 0010 0000
55 public const SANITIZE_BOOTH = 48; // 0011 0000
57 /**
58 * message levels
60 * @var array
62 public static $level = [
63 self::SUCCESS => 'success',
64 self::NOTICE => 'notice',
65 self::ERROR => 'error',
68 /**
69 * The message number
71 * @access protected
72 * @var int
74 protected $number = self::NOTICE;
76 /**
77 * The locale string identifier
79 * @access protected
80 * @var string
82 protected $string = '';
84 /**
85 * The formatted message
87 * @access protected
88 * @var string
90 protected $message = '';
92 /**
93 * Whether the message was already displayed
95 * @access protected
96 * @var bool
98 protected $isDisplayed = false;
101 * Whether to use BB code when displaying.
103 * @access protected
104 * @var bool
106 protected $useBBCode = true;
109 * Unique id
111 * @access protected
112 * @var string
114 protected $hash = null;
117 * holds parameters
119 * @access protected
120 * @var array
122 protected $params = [];
125 * holds additional messages
127 * @access protected
128 * @var array
130 protected $addedMessages = [];
133 * @param string $string The message to be displayed
134 * @param int $number A numeric representation of the type of message
135 * @param array $params An array of parameters to use in the message
136 * @param int $sanitize A flag to indicate what to sanitize, see
137 * constant definitions above
139 public function __construct(
140 string $string = '',
141 int $number = self::NOTICE,
142 array $params = [],
143 int $sanitize = self::SANITIZE_NONE
145 $this->setString($string, $sanitize & self::SANITIZE_STRING);
146 $this->setNumber($number);
147 $this->setParams($params, $sanitize & self::SANITIZE_PARAMS);
151 * magic method: return string representation for this object
153 public function __toString(): string
155 return $this->getMessage();
159 * get Message of type success
161 * shorthand for getting a simple success message
163 * @param string $string A localized string
164 * e.g. __('Your SQL query has been
165 * executed successfully')
167 * @return Message
169 * @static
171 public static function success(string $string = ''): self
173 if (empty($string)) {
174 $string = __('Your SQL query has been executed successfully.');
177 return new Message($string, self::SUCCESS);
181 * get Message of type error
183 * shorthand for getting a simple error message
185 * @param string $string A localized string e.g. __('Error')
187 * @return Message
189 * @static
191 public static function error(string $string = ''): self
193 if (empty($string)) {
194 $string = __('Error');
197 return new Message($string, self::ERROR);
201 * get Message of type notice
203 * shorthand for getting a simple notice message
205 * @param string $string A localized string
206 * e.g. __('The additional features for working with
207 * linked tables have been deactivated. To find out
208 * why click %shere%s.')
210 * @return Message
212 * @static
214 public static function notice(string $string): self
216 return new Message($string, self::NOTICE);
220 * get Message with customized content
222 * shorthand for getting a customized message
224 * @param string $message A localized string
225 * @param int $type A numeric representation of the type of message
227 * @return Message
229 * @static
231 public static function raw(string $message, int $type = self::NOTICE): self
233 $r = new Message('', $type);
234 $r->setMessage($message);
235 $r->setBBCode(false);
237 return $r;
241 * get Message for number of affected rows
243 * shorthand for getting a customized message
245 * @param int $rows Number of rows
247 * @return Message
249 * @static
251 public static function getMessageForAffectedRows(int $rows): self
253 $message = self::success(
254 _ngettext('%1$d row affected.', '%1$d rows affected.', $rows)
256 $message->addParam($rows);
258 return $message;
262 * get Message for number of deleted rows
264 * shorthand for getting a customized message
266 * @param int $rows Number of rows
268 * @return Message
270 * @static
272 public static function getMessageForDeletedRows(int $rows): self
274 $message = self::success(
275 _ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows)
277 $message->addParam($rows);
279 return $message;
283 * get Message for number of inserted rows
285 * shorthand for getting a customized message
287 * @param int $rows Number of rows
289 * @return Message
291 * @static
293 public static function getMessageForInsertedRows(int $rows): self
295 $message = self::success(
296 _ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows)
298 $message->addParam($rows);
300 return $message;
304 * get Message of type error with custom content
306 * shorthand for getting a customized error message
308 * @param string $message A localized string
310 * @return Message
312 * @static
314 public static function rawError(string $message): self
316 return self::raw($message, self::ERROR);
320 * get Message of type notice with custom content
322 * shorthand for getting a customized notice message
324 * @param string $message A localized string
326 * @return Message
328 * @static
330 public static function rawNotice(string $message): self
332 return self::raw($message, self::NOTICE);
336 * get Message of type success with custom content
338 * shorthand for getting a customized success message
340 * @param string $message A localized string
342 * @return Message
344 * @static
346 public static function rawSuccess(string $message): self
348 return self::raw($message, self::SUCCESS);
352 * returns whether this message is a success message or not
353 * and optionally makes this message a success message
355 * @param bool $set Whether to make this message of SUCCESS type
357 * @return bool whether this is a success message or not
359 public function isSuccess(bool $set = false): bool
361 if ($set) {
362 $this->setNumber(self::SUCCESS);
365 return $this->getNumber() === self::SUCCESS;
369 * returns whether this message is a notice message or not
370 * and optionally makes this message a notice message
372 * @param bool $set Whether to make this message of NOTICE type
374 * @return bool whether this is a notice message or not
376 public function isNotice(bool $set = false): bool
378 if ($set) {
379 $this->setNumber(self::NOTICE);
382 return $this->getNumber() === self::NOTICE;
386 * returns whether this message is an error message or not
387 * and optionally makes this message an error message
389 * @param bool $set Whether to make this message of ERROR type
391 * @return bool Whether this is an error message or not
393 public function isError(bool $set = false): bool
395 if ($set) {
396 $this->setNumber(self::ERROR);
399 return $this->getNumber() === self::ERROR;
403 * Set whether we should use BB Code when rendering.
405 * @param bool $useBBCode Use BB Code?
407 public function setBBCode(bool $useBBCode): void
409 $this->useBBCode = $useBBCode;
413 * set raw message (overrides string)
415 * @param string $message A localized string
416 * @param bool $sanitize Whether to sanitize $message or not
418 public function setMessage(string $message, bool $sanitize = false): void
420 if ($sanitize) {
421 $message = self::sanitize($message);
423 $this->message = $message;
427 * set string (does not take effect if raw message is set)
429 * @param string $string string to set
430 * @param bool|int $sanitize whether to sanitize $string or not
432 public function setString(string $string, $sanitize = true): void
434 if ($sanitize) {
435 $string = self::sanitize($string);
437 $this->string = $string;
441 * set message type number
443 * @param int $number message type number to set
445 public function setNumber(int $number): void
447 $this->number = $number;
451 * add string or Message parameter
453 * usage
454 * <code>
455 * $message->addParam('[em]some string[/em]');
456 * </code>
458 * @param mixed $param parameter to add
460 public function addParam($param): void
462 if ($param instanceof self || is_float($param) || is_int($param)) {
463 $this->params[] = $param;
464 } else {
465 $this->params[] = htmlspecialchars((string) $param);
470 * add parameter as raw HTML, usually in conjunction with strings
472 * usage
473 * <code>
474 * $message->addParamHtml('<img src="img">');
475 * </code>
477 * @param string $param parameter to add
479 public function addParamHtml(string $param): void
481 $this->params[] = self::notice($param);
485 * add a bunch of messages at once
487 * @param Message[] $messages to be added
488 * @param string $separator to use between this and previous string/message
490 public function addMessages(array $messages, string $separator = ' '): void
492 foreach ($messages as $message) {
493 $this->addMessage($message, $separator);
498 * add a bunch of messages at once
500 * @param string[] $messages to be added
501 * @param string $separator to use between this and previous string/message
503 public function addMessagesString(array $messages, string $separator = ' '): void
505 foreach ($messages as $message) {
506 $this->addText($message, $separator);
511 * Real implementation of adding message
513 * @param Message $message to be added
514 * @param string $separator to use between this and previous string/message
516 private function addMessageToList(self $message, string $separator): void
518 if (! empty($separator)) {
519 $this->addedMessages[] = $separator;
521 $this->addedMessages[] = $message;
525 * add another raw message to be concatenated on displaying
527 * @param self $message to be added
528 * @param string $separator to use between this and previous string/message
530 public function addMessage(self $message, string $separator = ' '): void
532 $this->addMessageToList($message, $separator);
536 * add another raw message to be concatenated on displaying
538 * @param string $message to be added
539 * @param string $separator to use between this and previous string/message
541 public function addText(string $message, string $separator = ' '): void
543 $this->addMessageToList(self::notice(htmlspecialchars($message)), $separator);
547 * add another html message to be concatenated on displaying
549 * @param string $message to be added
550 * @param string $separator to use between this and previous string/message
552 public function addHtml(string $message, string $separator = ' '): void
554 $this->addMessageToList(self::rawNotice($message), $separator);
558 * set all params at once, usually used in conjunction with string
560 * @param array $params parameters to set
561 * @param bool|int $sanitize whether to sanitize params
563 public function setParams(array $params, $sanitize = false): void
565 if ($sanitize) {
566 $params = self::sanitize($params);
568 $this->params = $params;
572 * return all parameters
574 * @return array
576 public function getParams(): array
578 return $this->params;
582 * return all added messages
584 * @return array
586 public function getAddedMessages(): array
588 return $this->addedMessages;
592 * Sanitizes $message
594 * @param mixed $message the message(s)
596 * @return mixed the sanitized message(s)
598 * @access public
599 * @static
601 public static function sanitize($message)
603 if (is_array($message)) {
604 foreach ($message as $key => $val) {
605 $message[$key] = self::sanitize($val);
608 return $message;
611 return htmlspecialchars((string) $message);
615 * decode $message, taking into account our special codes
616 * for formatting
618 * @param string $message the message
620 * @return string the decoded message
622 * @access public
623 * @static
625 public static function decodeBB(string $message): string
627 return Sanitize::sanitizeMessage($message, false, true);
631 * wrapper for sprintf()
633 * @param mixed[] ...$params Params
635 * @return string formatted
637 public static function format(...$params): string
639 if (isset($params[1]) && is_array($params[1])) {
640 array_unshift($params[1], $params[0]);
641 $params = $params[1];
644 return sprintf(...$params);
648 * returns unique Message::$hash, if not exists it will be created
650 * @return string Message::$hash
652 public function getHash(): string
654 if ($this->hash === null) {
655 $this->hash = md5(
656 $this->getNumber() .
657 $this->string .
658 $this->message
662 return $this->hash;
666 * returns compiled message
668 * @return string complete message
670 public function getMessage(): string
672 $message = $this->message;
674 if (strlen($message) === 0) {
675 $string = $this->getString();
676 if (strlen($string) === 0) {
677 $message = '';
678 } else {
679 $message = $string;
683 if ($this->isDisplayed()) {
684 $message = $this->getMessageWithIcon($message);
686 if (count($this->getParams()) > 0) {
687 $message = self::format($message, $this->getParams());
690 if ($this->useBBCode) {
691 $message = self::decodeBB($message);
694 foreach ($this->getAddedMessages() as $add_message) {
695 $message .= $add_message;
698 return $message;
702 * Returns only message string without image & other HTML.
704 public function getOnlyMessage(): string
706 return $this->message;
710 * returns Message::$string
712 * @return string Message::$string
714 public function getString(): string
716 return $this->string;
720 * returns Message::$number
722 * @return int Message::$number
724 public function getNumber(): int
726 return $this->number;
730 * returns level of message
732 * @return string level of message
734 public function getLevel(): string
736 return self::$level[$this->getNumber()];
740 * returns HTML code for displaying this message
742 * @return string whole message box
744 public function getDisplay(): string
746 $this->isDisplayed(true);
748 $context = 'primary';
749 $level = $this->getLevel();
750 if ($level === 'error') {
751 $context = 'danger';
752 } elseif ($level === 'success') {
753 $context = 'success';
756 $template = new Template();
758 return $template->render('message', [
759 'context' => $context,
760 'message' => $this->getMessage(),
765 * sets and returns whether the message was displayed or not
767 * @param bool $isDisplayed whether to set displayed flag
769 * @return bool Message::$isDisplayed
771 public function isDisplayed(bool $isDisplayed = false): bool
773 if ($isDisplayed) {
774 $this->isDisplayed = true;
777 return $this->isDisplayed;
781 * Returns the message with corresponding image icon
783 * @param string $message the message(s)
785 * @return string message with icon
787 public function getMessageWithIcon(string $message): string
789 if ($this->getLevel() === 'error') {
790 $image = 's_error';
791 } elseif ($this->getLevel() === 'success') {
792 $image = 's_success';
793 } else {
794 $image = 's_notice';
796 $message = self::notice(Html\Generator::getImage($image)) . ' ' . $message;
798 return $message;