/gui/tools/pma/libraries/Message.class.php

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