Message.php 19 KB

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