/dbadmin.com/public/libraries/Message.class.php

https://github.com/slikk66/DbAdmin · PHP · 749 lines · 283 code · 64 blank · 402 comment · 27 complexity · a53ef18c7aa0096449e0e4125a2cdd64 MD5 · raw file

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