PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/phpMyAdmin/libraries/Message.class.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 677 lines | 260 code | 62 blank | 355 comment | 22 complexity | e207bcb94e8443121e34cd9731996cab MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  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. * @param string $string
  130. * @param integer $number
  131. * @param array $params
  132. * @param integer $sanitize
  133. */
  134. public function __construct($string = '', $number = PMA_Message::NOTICE,
  135. $params = array(), $sanitize = PMA_Message::SANITIZE_NONE)
  136. {
  137. $this->setString($string, $sanitize & PMA_Message::SANITIZE_STRING);
  138. $this->setNumber($number);
  139. $this->setParams($params, $sanitize & PMA_Message::SANITIZE_PARAMS);
  140. }
  141. /**
  142. * magic method: return string representation for this object
  143. *
  144. * @return string
  145. */
  146. public function __toString()
  147. {
  148. return $this->getMessage();
  149. }
  150. /**
  151. * get PMA_Message of type success
  152. *
  153. * shorthand for getting a simple success message
  154. *
  155. * @static
  156. * @param string $string a localized string e.g. __('Your SQL query has been executed successfully')
  157. * @return PMA_Message
  158. */
  159. static public function success($string = '')
  160. {
  161. if (empty($string)) {
  162. $string = __('Your SQL query has been executed successfully');
  163. }
  164. return new PMA_Message($string, PMA_Message::SUCCESS);
  165. }
  166. /**
  167. * get PMA_Message of type error
  168. *
  169. * shorthand for getting a simple error message
  170. *
  171. * @static
  172. * @param string $string a localized string e.g. __('Error')
  173. * @return PMA_Message
  174. */
  175. static public function error($string = '')
  176. {
  177. if (empty($string)) {
  178. $string = __('Error');
  179. }
  180. return new PMA_Message($string, PMA_Message::ERROR);
  181. }
  182. /**
  183. * get PMA_Message of type notice
  184. *
  185. * shorthand for getting a simple notice message
  186. *
  187. * @static
  188. * @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.')
  189. * @return PMA_Message
  190. */
  191. static public function notice($string)
  192. {
  193. return new PMA_Message($string, PMA_Message::NOTICE);
  194. }
  195. /**
  196. * get PMA_Message with customized content
  197. *
  198. * shorthand for getting a customized message
  199. *
  200. * @static
  201. * @param string $message
  202. * @param integer $type
  203. * @return PMA_Message
  204. */
  205. static public function raw($message, $type = PMA_Message::NOTICE)
  206. {
  207. $r = new PMA_Message('', $type);
  208. $r->setMessage($message);
  209. return $r;
  210. }
  211. /**
  212. * get PMA_Message for number of affected rows
  213. *
  214. * shorthand for getting a customized message
  215. *
  216. * @static
  217. * @param integer $rows Number of rows
  218. * @return PMA_Message
  219. */
  220. static public function affected_rows($rows)
  221. {
  222. $message = PMA_Message::success(_ngettext('%1$d row affected.', '%1$d rows affected.', $rows));
  223. $message->addParam($rows);
  224. return $message;
  225. }
  226. /**
  227. * get PMA_Message for number of deleted rows
  228. *
  229. * shorthand for getting a customized message
  230. *
  231. * @static
  232. * @param integer $rows Number of rows
  233. * @return PMA_Message
  234. */
  235. static public function deleted_rows($rows)
  236. {
  237. $message = PMA_Message::success(_ngettext('%1$d row deleted.', '%1$d rows deleted.', $rows));
  238. $message->addParam($rows);
  239. return $message;
  240. }
  241. /**
  242. * get PMA_Message for number of inserted rows
  243. *
  244. * shorthand for getting a customized message
  245. *
  246. * @static
  247. * @param integer $rows Number of rows
  248. * @return PMA_Message
  249. */
  250. static public function inserted_rows($rows)
  251. {
  252. $message = PMA_Message::success(_ngettext('%1$d row inserted.', '%1$d rows inserted.', $rows));
  253. $message->addParam($rows);
  254. return $message;
  255. }
  256. /**
  257. * get PMA_Message of type error with custom content
  258. *
  259. * shorthand for getting a customized error message
  260. *
  261. * @static
  262. * @param string $message
  263. * @return PMA_Message
  264. */
  265. static public function rawError($message)
  266. {
  267. return PMA_Message::raw($message, PMA_Message::ERROR);
  268. }
  269. /**
  270. * get PMA_Message of type notice with custom content
  271. *
  272. * shorthand for getting a customized notice message
  273. *
  274. * @static
  275. * @param string $message
  276. * @return PMA_Message
  277. */
  278. static public function rawNotice($message)
  279. {
  280. return PMA_Message::raw($message, PMA_Message::NOTICE);
  281. }
  282. /**
  283. * get PMA_Message of type success with custom content
  284. *
  285. * shorthand for getting a customized success message
  286. *
  287. * @static
  288. * @param string $message
  289. * @return PMA_Message
  290. */
  291. static public function rawSuccess($message)
  292. {
  293. return PMA_Message::raw($message, PMA_Message::SUCCESS);
  294. }
  295. /**
  296. * returns whether this message is a success message or not
  297. * and optionaly makes this message a success message
  298. *
  299. * @param boolean $set
  300. * @return boolean whether this is a success message or not
  301. */
  302. public function isSuccess($set = false)
  303. {
  304. if ($set) {
  305. $this->setNumber(PMA_Message::SUCCESS);
  306. }
  307. return $this->getNumber() === PMA_Message::SUCCESS;
  308. }
  309. /**
  310. * returns whether this message is a notice message or not
  311. * and optionally makes this message a notice message
  312. *
  313. * @param boolean $set
  314. * @return boolean whether this is a notice message or not
  315. */
  316. public function isNotice($set = false)
  317. {
  318. if ($set) {
  319. $this->setNumber(PMA_Message::NOTICE);
  320. }
  321. return $this->getNumber() === PMA_Message::NOTICE;
  322. }
  323. /**
  324. * returns whether this message is an error message or not
  325. * and optionally makes this message an error message
  326. *
  327. * @param boolean $set
  328. * @return boolean whether this is an error message or not
  329. */
  330. public function isError($set = false)
  331. {
  332. if ($set) {
  333. $this->setNumber(PMA_Message::ERROR);
  334. }
  335. return $this->getNumber() === PMA_Message::ERROR;
  336. }
  337. /**
  338. * set raw message (overrides string)
  339. *
  340. * @param string $message
  341. * @param boolean $sanitize whether to sanitize $message or not
  342. */
  343. public function setMessage($message, $sanitize = false)
  344. {
  345. if ($sanitize) {
  346. $message = PMA_Message::sanitize($message);
  347. }
  348. $this->_message = $message;
  349. }
  350. /**
  351. * set string (does not take effect if raw message is set)
  352. *
  353. * @param string $_string
  354. * @param boolean $sanitize whether to sanitize $string or not
  355. */
  356. public function setString($_string, $sanitize = true)
  357. {
  358. if ($sanitize) {
  359. $_string = PMA_Message::sanitize($_string);
  360. }
  361. $this->_string = $_string;
  362. }
  363. /**
  364. * set message type number
  365. *
  366. * @param integer $number
  367. */
  368. public function setNumber($number)
  369. {
  370. $this->_number = $number;
  371. }
  372. /**
  373. * add parameter, usually in conjunction with strings
  374. *
  375. * usage
  376. * <code>
  377. * $message->addParam('strLocale', false);
  378. * $message->addParam('[em]some string[/em]');
  379. * $message->addParam('<img src="img" />', false);
  380. * </code>
  381. *
  382. * @param mixed $param
  383. * @param boolean $raw
  384. */
  385. public function addParam($param, $raw = true)
  386. {
  387. if ($param instanceof PMA_Message) {
  388. $this->_params[] = $param;
  389. } elseif ($raw) {
  390. $this->_params[] = htmlspecialchars($param);
  391. } else {
  392. $this->_params[] = PMA_Message::notice($param);
  393. }
  394. }
  395. /**
  396. * add another string to be concatenated on displaying
  397. *
  398. * @param string $string to be added
  399. * @param string $separator to use between this and previous string/message
  400. */
  401. public function addString($string, $separator = ' ')
  402. {
  403. $this->_added_messages[] = $separator;
  404. $this->_added_messages[] = PMA_Message::notice($string);
  405. }
  406. /**
  407. * add a bunch of messages at once
  408. *
  409. * @param array $messages to be added
  410. * @param string $separator to use between this and previous string/message
  411. */
  412. public function addMessages($messages, $separator = ' ')
  413. {
  414. foreach ($messages as $message) {
  415. $this->addMessage($message, $separator);
  416. }
  417. }
  418. /**
  419. * add another raw message to be concatenated on displaying
  420. *
  421. * @param mixed $message to be added
  422. * @param string $separator to use between this and previous string/message
  423. */
  424. public function addMessage($message, $separator = ' ')
  425. {
  426. if (strlen($separator)) {
  427. $this->_added_messages[] = $separator;
  428. }
  429. if ($message instanceof PMA_Message) {
  430. $this->_added_messages[] = $message;
  431. } else {
  432. $this->_added_messages[] = PMA_Message::rawNotice($message);
  433. }
  434. }
  435. /**
  436. * set all params at once, usually used in conjunction with string
  437. *
  438. * @param array $params
  439. * @param boolean $sanitize
  440. */
  441. public function setParams($params, $sanitize = false)
  442. {
  443. if ($sanitize) {
  444. $params = PMA_Message::sanitize($params);
  445. }
  446. $this->_params = $params;
  447. }
  448. /**
  449. * return all parameters
  450. *
  451. * @return array
  452. */
  453. public function getParams()
  454. {
  455. return $this->_params;
  456. }
  457. /**
  458. * return all added messages
  459. *
  460. * @return array
  461. */
  462. public function getAddedMessages()
  463. {
  464. return $this->_added_messages;
  465. }
  466. /**
  467. * Sanitizes $message
  468. *
  469. * @static
  470. * @param mixed $message the message(s)
  471. * @return mixed the sanitized message(s)
  472. * @access public
  473. */
  474. static public function sanitize($message)
  475. {
  476. if (is_array($message)) {
  477. foreach ($message as $key => $val) {
  478. $message[$key] = PMA_Message::sanitize($val);
  479. }
  480. return $message;
  481. }
  482. return htmlspecialchars($message);
  483. }
  484. /**
  485. * decode $message, taking into account our special codes
  486. * for formatting
  487. *
  488. * @static
  489. * @param string $message the message
  490. * @return string the decoded message
  491. * @access public
  492. */
  493. static public function decodeBB($message)
  494. {
  495. return PMA_sanitize($message, false, true);
  496. }
  497. /**
  498. * wrapper for sprintf()
  499. *
  500. * @return string formatted
  501. */
  502. static public function format()
  503. {
  504. $params = func_get_args();
  505. if (isset($params[1]) && is_array($params[1])) {
  506. array_unshift($params[1], $params[0]);
  507. $params = $params[1];
  508. }
  509. return call_user_func_array('sprintf', $params);
  510. }
  511. /**
  512. * returns unique PMA_Message::$_hash, if not exists it will be created
  513. *
  514. * @return string PMA_Message::$_hash
  515. */
  516. public function getHash()
  517. {
  518. if (null === $this->_hash) {
  519. $this->_hash = md5(
  520. $this->getNumber() .
  521. $this->_string .
  522. $this->_message
  523. );
  524. }
  525. return $this->_hash;
  526. }
  527. /**
  528. * returns compiled message
  529. *
  530. * @return string complete message
  531. */
  532. public function getMessage()
  533. {
  534. $message = $this->_message;
  535. if (0 === strlen($message)) {
  536. $string = $this->getString();
  537. if (isset($GLOBALS[$string])) {
  538. $message = $GLOBALS[$string];
  539. } elseif (0 === strlen($string)) {
  540. $message = '';
  541. } else {
  542. $message = $string;
  543. }
  544. }
  545. if (count($this->getParams()) > 0) {
  546. $message = PMA_Message::format($message, $this->getParams());
  547. }
  548. $message = PMA_Message::decodeBB($message);
  549. foreach ($this->getAddedMessages() as $add_message) {
  550. $message .= $add_message;
  551. }
  552. return $message;
  553. }
  554. /**
  555. * returns PMA_Message::$_string
  556. *
  557. * @return string PMA_Message::$_string
  558. */
  559. public function getString()
  560. {
  561. return $this->_string;
  562. }
  563. /**
  564. * returns PMA_Message::$_number
  565. *
  566. * @return integer PMA_Message::$_number
  567. */
  568. public function getNumber()
  569. {
  570. return $this->_number;
  571. }
  572. /**
  573. * returns level of message
  574. *
  575. * @return string level of message
  576. */
  577. public function getLevel()
  578. {
  579. return PMA_Message::$level[$this->getNumber()];
  580. }
  581. /**
  582. * Displays the message in HTML
  583. *
  584. */
  585. public function display()
  586. {
  587. echo $this->getDisplay();
  588. $this->isDisplayed(true);
  589. }
  590. /**
  591. * returns HTML code for displaying this message
  592. *
  593. *
  594. * @return string whole message box
  595. */
  596. public function getDisplay()
  597. {
  598. return '<div class="' . $this->getLevel() . '">'
  599. . $this->getMessage() . '</div>';
  600. }
  601. /**
  602. * sets and returns whether the message was displayed or not
  603. *
  604. * @param boolean $is_displayed
  605. * @return boolean PMA_Message::$_is_displayed
  606. */
  607. public function isDisplayed($is_displayed = false)
  608. {
  609. if ($is_displayed) {
  610. $this->_is_displayed = true;
  611. }
  612. return $this->_is_displayed;
  613. }
  614. }
  615. ?>