Message.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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
  20. * $message = PMA_Message::notice(__('This is a localized notice'));
  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 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. *
  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|string $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|string $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. // convert single param to array
  490. if (! is_array($params)) {
  491. $params = array($params);
  492. }
  493. $this->params = $params;
  494. }
  495. /**
  496. * return all parameters
  497. *
  498. * @return array
  499. */
  500. public function getParams()
  501. {
  502. return $this->params;
  503. }
  504. /**
  505. * return all added messages
  506. *
  507. * @return array
  508. */
  509. public function getAddedMessages()
  510. {
  511. return $this->addedMessages;
  512. }
  513. /**
  514. * Sanitizes $message
  515. *
  516. * @param mixed $message the message(s)
  517. *
  518. * @return mixed the sanitized message(s)
  519. * @access public
  520. * @static
  521. */
  522. static public function sanitize($message)
  523. {
  524. if (is_array($message)) {
  525. foreach ($message as $key => $val) {
  526. $message[$key] = PMA_Message::sanitize($val);
  527. }
  528. return $message;
  529. }
  530. return htmlspecialchars($message);
  531. }
  532. /**
  533. * decode $message, taking into account our special codes
  534. * for formatting
  535. *
  536. * @param string $message the message
  537. *
  538. * @return string the decoded message
  539. * @access public
  540. * @static
  541. */
  542. static public function decodeBB($message)
  543. {
  544. return PMA_sanitize($message, false, true);
  545. }
  546. /**
  547. * wrapper for sprintf()
  548. *
  549. * @return string formatted
  550. */
  551. static public function format()
  552. {
  553. $params = func_get_args();
  554. if (isset($params[1]) && is_array($params[1])) {
  555. array_unshift($params[1], $params[0]);
  556. $params = $params[1];
  557. }
  558. return call_user_func_array('sprintf', $params);
  559. }
  560. /**
  561. * returns unique PMA_Message::$hash, if not exists it will be created
  562. *
  563. * @return string PMA_Message::$hash
  564. */
  565. public function getHash()
  566. {
  567. if (null === $this->hash) {
  568. $this->hash = md5(
  569. $this->getNumber() .
  570. $this->string .
  571. $this->message
  572. );
  573. }
  574. return $this->hash;
  575. }
  576. /**
  577. * returns compiled message
  578. *
  579. * @return string complete message
  580. */
  581. public function getMessage()
  582. {
  583. $message = $this->message;
  584. if (0 === strlen($message)) {
  585. $string = $this->getString();
  586. if (isset($GLOBALS[$string])) {
  587. $message = $GLOBALS[$string];
  588. } elseif (0 === strlen($string)) {
  589. $message = '';
  590. } else {
  591. $message = $string;
  592. }
  593. }
  594. if ($this->isDisplayed()) {
  595. $message = $this->getMessageWithIcon($message);
  596. }
  597. if (count($this->getParams()) > 0) {
  598. $message = PMA_Message::format($message, $this->getParams());
  599. }
  600. $message = PMA_Message::decodeBB($message);
  601. foreach ($this->getAddedMessages() as $add_message) {
  602. $message .= $add_message;
  603. }
  604. return $message;
  605. }
  606. /**
  607. * returns PMA_Message::$string
  608. *
  609. * @return string PMA_Message::$string
  610. */
  611. public function getString()
  612. {
  613. return $this->string;
  614. }
  615. /**
  616. * returns PMA_Message::$number
  617. *
  618. * @return integer PMA_Message::$number
  619. */
  620. public function getNumber()
  621. {
  622. return $this->number;
  623. }
  624. /**
  625. * returns level of message
  626. *
  627. * @return string level of message
  628. */
  629. public function getLevel()
  630. {
  631. return PMA_Message::$level[$this->getNumber()];
  632. }
  633. /**
  634. * Displays the message in HTML
  635. *
  636. * @return void
  637. */
  638. public function display()
  639. {
  640. echo $this->getDisplay();
  641. $this->isDisplayed(true);
  642. }
  643. /**
  644. * returns HTML code for displaying this message
  645. *
  646. * @return string whole message box
  647. */
  648. public function getDisplay()
  649. {
  650. $this->isDisplayed(true);
  651. return '<div class="' . $this->getLevel() . '">'
  652. . $this->getMessage() . '</div>';
  653. }
  654. /**
  655. * sets and returns whether the message was displayed or not
  656. *
  657. * @param boolean $isDisplayed whether to set displayed flag
  658. *
  659. * @return boolean PMA_Message::$isDisplayed
  660. */
  661. public function isDisplayed($isDisplayed = false)
  662. {
  663. if ($isDisplayed) {
  664. $this->isDisplayed = true;
  665. }
  666. return $this->isDisplayed;
  667. }
  668. /**
  669. * Returns the message with corresponding image icon
  670. *
  671. * @param string $message the message(s)
  672. *
  673. * @return string message with icon
  674. */
  675. public function getMessageWithIcon($message)
  676. {
  677. $image = '';
  678. if ('error' == $this->getLevel()) {
  679. $image = 's_error.png';
  680. } elseif ('success' == $this->getLevel()) {
  681. $image = 's_success.png';
  682. } else {
  683. $image = 's_notice.png';
  684. }
  685. $message = PMA_Message::notice(PMA_Util::getImage($image)) . " " . $message;
  686. return $message;
  687. }
  688. }
  689. ?>