Message.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds class Message
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. use PhpMyAdmin\Sanitize;
  11. use PhpMyAdmin\Util;
  12. /**
  13. * a single message
  14. *
  15. * simple usage examples:
  16. * <code>
  17. * // display simple error message 'Error'
  18. * Message::error()->display();
  19. *
  20. * // get simple success message 'Success'
  21. * $message = Message::success();
  22. *
  23. * // get special notice
  24. * $message = Message::notice(__('This is a localized notice'));
  25. * </code>
  26. *
  27. * more advanced usage example:
  28. * <code>
  29. * // create another message, a hint, with a localized string which expects
  30. * $hint = Message::notice('Read the %smanual%s');
  31. * // replace placeholders with the following params
  32. * $hint->addParam('[doc@cfg_Example]');
  33. * $hint->addParam('[/doc]');
  34. * // add this hint as a tooltip
  35. * $hint = showHint($hint);
  36. *
  37. * // add the retrieved tooltip reference to the original message
  38. * $message->addMessage($hint);
  39. * </code>
  40. *
  41. * @package PhpMyAdmin
  42. */
  43. class Message
  44. {
  45. public const SUCCESS = 1; // 0001
  46. public const NOTICE = 2; // 0010
  47. public const ERROR = 8; // 1000
  48. public const SANITIZE_NONE = 0; // 0000 0000
  49. public const SANITIZE_STRING = 16; // 0001 0000
  50. public const SANITIZE_PARAMS = 32; // 0010 0000
  51. public const SANITIZE_BOOTH = 48; // 0011 0000
  52. /**
  53. * message levels
  54. *
  55. * @var array
  56. */
  57. public static $level = [
  58. Message::SUCCESS => 'success',
  59. Message::NOTICE => 'notice',
  60. Message::ERROR => 'error',
  61. ];
  62. /**
  63. * The message number
  64. *
  65. * @access protected
  66. * @var integer
  67. */
  68. protected $number = Message::NOTICE;
  69. /**
  70. * The locale string identifier
  71. *
  72. * @access protected
  73. * @var string
  74. */
  75. protected $string = '';
  76. /**
  77. * The formatted message
  78. *
  79. * @access protected
  80. * @var string
  81. */
  82. protected $message = '';
  83. /**
  84. * Whether the message was already displayed
  85. *
  86. * @access protected
  87. * @var boolean
  88. */
  89. protected $isDisplayed = false;
  90. /**
  91. * Whether to use BB code when displaying.
  92. *
  93. * @access protected
  94. * @var boolean
  95. */
  96. protected $useBBCode = true;
  97. /**
  98. * Unique id
  99. *
  100. * @access protected
  101. * @var string
  102. */
  103. protected $hash = null;
  104. /**
  105. * holds parameters
  106. *
  107. * @access protected
  108. * @var array
  109. */
  110. protected $params = [];
  111. /**
  112. * holds additional messages
  113. *
  114. * @access protected
  115. * @var array
  116. */
  117. protected $addedMessages = [];
  118. /**
  119. * Constructor
  120. *
  121. * @param string $string The message to be displayed
  122. * @param integer $number A numeric representation of the type of message
  123. * @param array $params An array of parameters to use in the message
  124. * @param integer $sanitize A flag to indicate what to sanitize, see
  125. * constant definitions above
  126. */
  127. public function __construct(
  128. string $string = '',
  129. int $number = Message::NOTICE,
  130. array $params = [],
  131. int $sanitize = Message::SANITIZE_NONE
  132. ) {
  133. $this->setString($string, $sanitize & self::SANITIZE_STRING);
  134. $this->setNumber($number);
  135. $this->setParams($params, $sanitize & self::SANITIZE_PARAMS);
  136. }
  137. /**
  138. * magic method: return string representation for this object
  139. *
  140. * @return string
  141. */
  142. public function __toString(): string
  143. {
  144. return $this->getMessage();
  145. }
  146. /**
  147. * get Message of type success
  148. *
  149. * shorthand for getting a simple success message
  150. *
  151. * @param string $string A localized string
  152. * e.g. __('Your SQL query has been
  153. * executed successfully')
  154. *
  155. * @return Message
  156. * @static
  157. */
  158. public static function success(string $string = ''): self
  159. {
  160. if (empty($string)) {
  161. $string = __('Your SQL query has been executed successfully.');
  162. }
  163. return new Message($string, self::SUCCESS);
  164. }
  165. /**
  166. * get Message of type error
  167. *
  168. * shorthand for getting a simple error message
  169. *
  170. * @param string $string A localized string e.g. __('Error')
  171. *
  172. * @return Message
  173. * @static
  174. */
  175. public static function error(string $string = ''): self
  176. {
  177. if (empty($string)) {
  178. $string = __('Error');
  179. }
  180. return new Message($string, self::ERROR);
  181. }
  182. /**
  183. * get Message of type notice
  184. *
  185. * shorthand for getting a simple notice message
  186. *
  187. * @param string $string A localized string
  188. * e.g. __('The additional features for working with
  189. * linked tables have been deactivated. To find out
  190. * why click %shere%s.')
  191. *
  192. * @return Message
  193. * @static
  194. */
  195. public static function notice(string $string): self
  196. {
  197. return new Message($string, self::NOTICE);
  198. }
  199. /**
  200. * get Message with customized content
  201. *
  202. * shorthand for getting a customized message
  203. *
  204. * @param string $message A localized string
  205. * @param integer $type A numeric representation of the type of message
  206. *
  207. * @return Message
  208. * @static
  209. */
  210. public static function raw(string $message, int $type = Message::NOTICE): self
  211. {
  212. $r = new Message('', $type);
  213. $r->setMessage($message);
  214. $r->setBBCode(false);
  215. return $r;
  216. }
  217. /**
  218. * get Message for number of affected rows
  219. *
  220. * shorthand for getting a customized message
  221. *
  222. * @param integer $rows Number of rows
  223. *
  224. * @return Message
  225. * @static
  226. */
  227. public static function getMessageForAffectedRows(int $rows): self
  228. {
  229. $message = self::success(
  230. _ngettext('%1$d row affected.', '%1$d rows affected.', $rows)
  231. );
  232. $message->addParam($rows);
  233. return $message;
  234. }
  235. /**
  236. * get Message for number of deleted rows
  237. *
  238. * shorthand for getting a customized message
  239. *
  240. * @param integer $rows Number of rows
  241. *
  242. * @return Message
  243. * @static
  244. */
  245. public static function getMessageForDeletedRows(int $rows): self
  246. {
  247. $message = self::success(
  248. _ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows)
  249. );
  250. $message->addParam($rows);
  251. return $message;
  252. }
  253. /**
  254. * get Message for number of inserted rows
  255. *
  256. * shorthand for getting a customized message
  257. *
  258. * @param integer $rows Number of rows
  259. *
  260. * @return Message
  261. * @static
  262. */
  263. public static function getMessageForInsertedRows(int $rows): self
  264. {
  265. $message = self::success(
  266. _ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows)
  267. );
  268. $message->addParam($rows);
  269. return $message;
  270. }
  271. /**
  272. * get Message of type error with custom content
  273. *
  274. * shorthand for getting a customized error message
  275. *
  276. * @param string $message A localized string
  277. *
  278. * @return Message
  279. * @static
  280. */
  281. public static function rawError(string $message): self
  282. {
  283. return self::raw($message, self::ERROR);
  284. }
  285. /**
  286. * get Message of type notice with custom content
  287. *
  288. * shorthand for getting a customized notice message
  289. *
  290. * @param string $message A localized string
  291. *
  292. * @return Message
  293. * @static
  294. */
  295. public static function rawNotice(string $message): self
  296. {
  297. return self::raw($message, self::NOTICE);
  298. }
  299. /**
  300. * get Message of type success with custom content
  301. *
  302. * shorthand for getting a customized success message
  303. *
  304. * @param string $message A localized string
  305. *
  306. * @return Message
  307. * @static
  308. */
  309. public static function rawSuccess(string $message): self
  310. {
  311. return self::raw($message, self::SUCCESS);
  312. }
  313. /**
  314. * returns whether this message is a success message or not
  315. * and optionally makes this message a success message
  316. *
  317. * @param boolean $set Whether to make this message of SUCCESS type
  318. *
  319. * @return boolean whether this is a success message or not
  320. */
  321. public function isSuccess(bool $set = false): bool
  322. {
  323. if ($set) {
  324. $this->setNumber(self::SUCCESS);
  325. }
  326. return $this->getNumber() === self::SUCCESS;
  327. }
  328. /**
  329. * returns whether this message is a notice message or not
  330. * and optionally makes this message a notice message
  331. *
  332. * @param boolean $set Whether to make this message of NOTICE type
  333. *
  334. * @return boolean whether this is a notice message or not
  335. */
  336. public function isNotice(bool $set = false): bool
  337. {
  338. if ($set) {
  339. $this->setNumber(self::NOTICE);
  340. }
  341. return $this->getNumber() === self::NOTICE;
  342. }
  343. /**
  344. * returns whether this message is an error message or not
  345. * and optionally makes this message an error message
  346. *
  347. * @param boolean $set Whether to make this message of ERROR type
  348. *
  349. * @return boolean Whether this is an error message or not
  350. */
  351. public function isError(bool $set = false): bool
  352. {
  353. if ($set) {
  354. $this->setNumber(self::ERROR);
  355. }
  356. return $this->getNumber() === self::ERROR;
  357. }
  358. /**
  359. * Set whether we should use BB Code when rendering.
  360. *
  361. * @param boolean $useBBCode Use BB Code?
  362. *
  363. * @return void
  364. */
  365. public function setBBCode(bool $useBBCode): void
  366. {
  367. $this->useBBCode = $useBBCode;
  368. }
  369. /**
  370. * set raw message (overrides string)
  371. *
  372. * @param string $message A localized string
  373. * @param boolean $sanitize Whether to sanitize $message or not
  374. *
  375. * @return void
  376. */
  377. public function setMessage(string $message, bool $sanitize = false): void
  378. {
  379. if ($sanitize) {
  380. $message = self::sanitize($message);
  381. }
  382. $this->message = $message;
  383. }
  384. /**
  385. * set string (does not take effect if raw message is set)
  386. *
  387. * @param string $string string to set
  388. * @param boolean|int $sanitize whether to sanitize $string or not
  389. *
  390. * @return void
  391. */
  392. public function setString(string $string, $sanitize = true): void
  393. {
  394. if ($sanitize) {
  395. $string = self::sanitize($string);
  396. }
  397. $this->string = $string;
  398. }
  399. /**
  400. * set message type number
  401. *
  402. * @param integer $number message type number to set
  403. *
  404. * @return void
  405. */
  406. public function setNumber(int $number): void
  407. {
  408. $this->number = $number;
  409. }
  410. /**
  411. * add string or Message parameter
  412. *
  413. * usage
  414. * <code>
  415. * $message->addParam('[em]some string[/em]');
  416. * </code>
  417. *
  418. * @param mixed $param parameter to add
  419. *
  420. * @return void
  421. */
  422. public function addParam($param): void
  423. {
  424. if ($param instanceof self || is_float($param) || is_int($param)) {
  425. $this->params[] = $param;
  426. } else {
  427. $this->params[] = htmlspecialchars((string) $param);
  428. }
  429. }
  430. /**
  431. * add parameter as raw HTML, usually in conjunction with strings
  432. *
  433. * usage
  434. * <code>
  435. * $message->addParamHtml('<img src="img">');
  436. * </code>
  437. *
  438. * @param string $param parameter to add
  439. *
  440. * @return void
  441. */
  442. public function addParamHtml(string $param): void
  443. {
  444. $this->params[] = self::notice($param);
  445. }
  446. /**
  447. * add a bunch of messages at once
  448. *
  449. * @param Message[] $messages to be added
  450. * @param string $separator to use between this and previous string/message
  451. *
  452. * @return void
  453. */
  454. public function addMessages(array $messages, string $separator = ' '): void
  455. {
  456. foreach ($messages as $message) {
  457. $this->addMessage($message, $separator);
  458. }
  459. }
  460. /**
  461. * add a bunch of messages at once
  462. *
  463. * @param string[] $messages to be added
  464. * @param string $separator to use between this and previous string/message
  465. *
  466. * @return void
  467. */
  468. public function addMessagesString(array $messages, string $separator = ' '): void
  469. {
  470. foreach ($messages as $message) {
  471. $this->addText($message, $separator);
  472. }
  473. }
  474. /**
  475. * Real implementation of adding message
  476. *
  477. * @param Message $message to be added
  478. * @param string $separator to use between this and previous string/message
  479. *
  480. * @return void
  481. */
  482. private function addMessageToList(self $message, string $separator): void
  483. {
  484. if (! empty($separator)) {
  485. $this->addedMessages[] = $separator;
  486. }
  487. $this->addedMessages[] = $message;
  488. }
  489. /**
  490. * add another raw message to be concatenated on displaying
  491. *
  492. * @param self $message to be added
  493. * @param string $separator to use between this and previous string/message
  494. *
  495. * @return void
  496. */
  497. public function addMessage(self $message, string $separator = ' '): void
  498. {
  499. $this->addMessageToList($message, $separator);
  500. }
  501. /**
  502. * add another raw message to be concatenated on displaying
  503. *
  504. * @param string $message to be added
  505. * @param string $separator to use between this and previous string/message
  506. *
  507. * @return void
  508. */
  509. public function addText(string $message, string $separator = ' '): void
  510. {
  511. $this->addMessageToList(self::notice(htmlspecialchars($message)), $separator);
  512. }
  513. /**
  514. * add another html message to be concatenated on displaying
  515. *
  516. * @param string $message to be added
  517. * @param string $separator to use between this and previous string/message
  518. *
  519. * @return void
  520. */
  521. public function addHtml(string $message, string $separator = ' '): void
  522. {
  523. $this->addMessageToList(self::rawNotice($message), $separator);
  524. }
  525. /**
  526. * set all params at once, usually used in conjunction with string
  527. *
  528. * @param array|string $params parameters to set
  529. * @param bool|int $sanitize whether to sanitize params
  530. *
  531. * @return void
  532. */
  533. public function setParams($params, $sanitize = false): void
  534. {
  535. if ($sanitize) {
  536. $params = self::sanitize($params);
  537. }
  538. $this->params = $params;
  539. }
  540. /**
  541. * return all parameters
  542. *
  543. * @return array|string
  544. */
  545. public function getParams()
  546. {
  547. return $this->params;
  548. }
  549. /**
  550. * return all added messages
  551. *
  552. * @return array
  553. */
  554. public function getAddedMessages(): array
  555. {
  556. return $this->addedMessages;
  557. }
  558. /**
  559. * Sanitizes $message
  560. *
  561. * @param mixed $message the message(s)
  562. *
  563. * @return mixed the sanitized message(s)
  564. * @access public
  565. * @static
  566. */
  567. public static function sanitize($message)
  568. {
  569. if (is_array($message)) {
  570. foreach ($message as $key => $val) {
  571. $message[$key] = self::sanitize($val);
  572. }
  573. return $message;
  574. }
  575. return htmlspecialchars((string) $message);
  576. }
  577. /**
  578. * decode $message, taking into account our special codes
  579. * for formatting
  580. *
  581. * @param string $message the message
  582. *
  583. * @return string the decoded message
  584. * @access public
  585. * @static
  586. */
  587. public static function decodeBB(string $message): string
  588. {
  589. return Sanitize::sanitizeMessage($message, false, true);
  590. }
  591. /**
  592. * wrapper for sprintf()
  593. *
  594. * @param mixed[] ...$params Params
  595. * @return string formatted
  596. */
  597. public static function format(...$params): string
  598. {
  599. if (isset($params[1]) && is_array($params[1])) {
  600. array_unshift($params[1], $params[0]);
  601. $params = $params[1];
  602. }
  603. return sprintf(...$params);
  604. }
  605. /**
  606. * returns unique Message::$hash, if not exists it will be created
  607. *
  608. * @return string Message::$hash
  609. */
  610. public function getHash(): string
  611. {
  612. if (null === $this->hash) {
  613. $this->hash = md5(
  614. $this->getNumber() .
  615. $this->string .
  616. $this->message
  617. );
  618. }
  619. return $this->hash;
  620. }
  621. /**
  622. * returns compiled message
  623. *
  624. * @return string complete message
  625. */
  626. public function getMessage(): string
  627. {
  628. $message = $this->message;
  629. if (strlen($message) === 0) {
  630. $string = $this->getString();
  631. if (strlen($string) === 0) {
  632. $message = '';
  633. } else {
  634. $message = $string;
  635. }
  636. }
  637. if ($this->isDisplayed()) {
  638. $message = $this->getMessageWithIcon($message);
  639. }
  640. if (count($this->getParams()) > 0) {
  641. $message = self::format($message, $this->getParams());
  642. }
  643. if ($this->useBBCode) {
  644. $message = self::decodeBB($message);
  645. }
  646. foreach ($this->getAddedMessages() as $add_message) {
  647. $message .= $add_message;
  648. }
  649. return $message;
  650. }
  651. /**
  652. * Returns only message string without image & other HTML.
  653. *
  654. * @return string
  655. */
  656. public function getOnlyMessage(): string
  657. {
  658. return $this->message;
  659. }
  660. /**
  661. * returns Message::$string
  662. *
  663. * @return string Message::$string
  664. */
  665. public function getString(): string
  666. {
  667. return $this->string;
  668. }
  669. /**
  670. * returns Message::$number
  671. *
  672. * @return integer Message::$number
  673. */
  674. public function getNumber(): int
  675. {
  676. return $this->number;
  677. }
  678. /**
  679. * returns level of message
  680. *
  681. * @return string level of message
  682. */
  683. public function getLevel(): string
  684. {
  685. return self::$level[$this->getNumber()];
  686. }
  687. /**
  688. * Displays the message in HTML
  689. *
  690. * @return void
  691. */
  692. public function display(): void
  693. {
  694. echo $this->getDisplay();
  695. }
  696. /**
  697. * returns HTML code for displaying this message
  698. *
  699. * @return string whole message box
  700. */
  701. public function getDisplay(): string
  702. {
  703. $this->isDisplayed(true);
  704. return '<div class="' . $this->getLevel() . '">'
  705. . $this->getMessage() . '</div>';
  706. }
  707. /**
  708. * sets and returns whether the message was displayed or not
  709. *
  710. * @param boolean $isDisplayed whether to set displayed flag
  711. *
  712. * @return boolean Message::$isDisplayed
  713. */
  714. public function isDisplayed(bool $isDisplayed = false): bool
  715. {
  716. if ($isDisplayed) {
  717. $this->isDisplayed = true;
  718. }
  719. return $this->isDisplayed;
  720. }
  721. /**
  722. * Returns the message with corresponding image icon
  723. *
  724. * @param string $message the message(s)
  725. *
  726. * @return string message with icon
  727. */
  728. public function getMessageWithIcon(string $message): string
  729. {
  730. if ('error' === $this->getLevel()) {
  731. $image = 's_error';
  732. } elseif ('success' === $this->getLevel()) {
  733. $image = 's_success';
  734. } else {
  735. $image = 's_notice';
  736. }
  737. $message = self::notice(Util::getImage($image)) . " " . $message;
  738. return $message;
  739. }
  740. }