/html/ops/phpMyAdmin-3.2.4-all-languages/libraries/Message.class.php

https://github.com/jackygrahamez/DrugDiscovery-Home · PHP · 815 lines · 296 code · 69 blank · 450 comment · 26 complexity · e8496eb36b07235e4c1e6b68d3be3e59 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds class PMA_Message
  5. *
  6. * @author Sebastian Mendel <info@sebastianmendel.de>
  7. * @version $Id: Error.class.php 10738 2007-10-08 16:02:58Z cybot_tm $
  8. * @package phpMyAdmin
  9. */
  10. /**
  11. * a single message
  12. *
  13. * simple usage examples:
  14. * <code>
  15. * // display simple error message 'Error'
  16. * PMA_Message::error()->display();
  17. *
  18. * // get simple success message 'Success'
  19. * $message = PMA_Message::success();
  20. *
  21. * // get special notice 'Some locale notice'
  22. * $message = PMA_Message::notice('strSomeLocaleNotice');
  23. *
  24. * // display raw warning message 'This is a warning!'
  25. * PMA_Message::rawWarning('This is a warning!')->display();
  26. * </code>
  27. *
  28. * more advanced usage example:
  29. * <code>
  30. * // create a localized success message
  31. * $message = PMA_Message::success('strSomeLocaleMessage');
  32. *
  33. * // create another message, a hint, whith a localized string which expects
  34. * // two parameters: $strSomeFootnote = 'Read the %smanual%s'
  35. * $hint = PMA_Message::notice('strSomeFootnote');
  36. * // replace %d with the following params
  37. * $hint->addParam('[a@./Documentation.html#cfg_Example@_blank]');
  38. * $hint->addParam('[/a]');
  39. * // add this hint as a footnote
  40. * $hint = PMA_showHint($hint);
  41. *
  42. * // add the retrieved footnote reference to the original message
  43. * $message->addMessage($hint);
  44. *
  45. * // create another message ...
  46. * $more = PMA_Message::notice('strSomeMoreLocale');
  47. * $more->addString('strSomeEvenMoreLocale', '<br />');
  48. * $more->addParam('parameter for strSomeMoreLocale');
  49. * $more->addParam('more parameter for strSomeMoreLocale');
  50. *
  51. * // and add it also to the orignal message
  52. * $message->addMessage($more);
  53. * // finally add another raw message
  54. * $message->addMessage('some final words', ' - ');
  55. *
  56. * // display() will now print all messages in the same order as they are added
  57. * $message->display();
  58. * // strSomeLocaleMessage <sup>1</sup> strSomeMoreLocale<br />
  59. * // strSomeEvenMoreLocale - some final words
  60. * </code>
  61. * @package phpMyAdmin
  62. */
  63. class PMA_Message
  64. {
  65. const SUCCESS = 1; // 0001
  66. const NOTICE = 2; // 0010
  67. const WARNING = 4; // 0100
  68. const ERROR = 8; // 1000
  69. const SANITIZE_NONE = 0; // 0000 0000
  70. const SANITIZE_STRING = 16; // 0001 0000
  71. const SANITIZE_PARAMS = 32; // 0010 0000
  72. const SANITIZE_BOOTH = 48; // 0011 0000
  73. /**
  74. * message levels
  75. *
  76. * @var array
  77. */
  78. static public $level = array (
  79. PMA_Message::SUCCESS => 'success',
  80. PMA_Message::NOTICE => 'notice',
  81. PMA_Message::WARNING => 'warning',
  82. PMA_Message::ERROR => 'error',
  83. );
  84. /**
  85. * The message number
  86. *
  87. * @access protected
  88. * @var integer
  89. */
  90. protected $_number = PMA_Message::NOTICE;
  91. /**
  92. * The locale string identifier
  93. *
  94. * @access protected
  95. * @var string
  96. */
  97. protected $_string = '';
  98. /**
  99. * The formated message
  100. *
  101. * @access protected
  102. * @var string
  103. */
  104. protected $_message = '';
  105. /**
  106. * Whether the message was already displayed
  107. *
  108. * @access protected
  109. * @var boolean
  110. */
  111. protected $_is_displayed = false;
  112. /**
  113. * Unique id
  114. *
  115. * @access protected
  116. * @var string
  117. */
  118. protected $_hash = null;
  119. /**
  120. * holds parameters
  121. *
  122. * @access protected
  123. * @var array
  124. */
  125. protected $_params = array();
  126. /**
  127. * holds additional messages
  128. *
  129. * @access protected
  130. * @var array
  131. */
  132. protected $_added_messages = array();
  133. /**
  134. * Constructor
  135. *
  136. * @uses PMA_Message::setNumber()
  137. * @uses PMA_Message::setString()
  138. * @uses PMA_Message::setParams()
  139. * @uses PMA_Message::NOTICE
  140. * @uses PMA_Message::SANITIZE_NONE
  141. * @uses PMA_Message::SANITIZE_STRING
  142. * @uses PMA_Message::SANITIZE_PARAMS
  143. * @param string $string
  144. * @param integer $number
  145. * @param array $$params
  146. * @param boolean $sanitize
  147. */
  148. public function __construct($string = '', $number = PMA_Message::NOTICE,
  149. $params = array(), $sanitize = PMA_Message::SANITIZE_NONE)
  150. {
  151. $this->setString($string, $sanitize & PMA_Message::SANITIZE_STRING);
  152. $this->setNumber($number);
  153. $this->setParams($params, $sanitize & PMA_Message::SANITIZE_PARAMS);
  154. }
  155. /**
  156. * magic method: return string representation for this object
  157. *
  158. * @uses PMA_Message::getMessage()
  159. * @return string
  160. */
  161. public function __toString()
  162. {
  163. return $this->getMessage();
  164. }
  165. /**
  166. * get PMA_Message of type success
  167. *
  168. * shorthand for getting a simple success message
  169. *
  170. * @static
  171. * @uses PMA_Message as returned object
  172. * @uses PMA_Message::SUCCESS
  173. * @param string $string
  174. * @return PMA_Message
  175. */
  176. static public function success($string = '')
  177. {
  178. if (empty($string)) {
  179. $string = 'strSuccess';
  180. }
  181. return new PMA_Message($string, PMA_Message::SUCCESS);
  182. }
  183. /**
  184. * get PMA_Message of type error
  185. *
  186. * shorthand for getting a simple error message
  187. *
  188. * @static
  189. * @uses PMA_Message as returned object
  190. * @uses PMA_Message::ERROR
  191. * @param string $string
  192. * @return PMA_Message
  193. */
  194. static public function error($string = '')
  195. {
  196. if (empty($string)) {
  197. $string = 'strError';
  198. }
  199. return new PMA_Message($string, PMA_Message::ERROR);
  200. }
  201. /**
  202. * get PMA_Message of type warning
  203. *
  204. * shorthand for getting a simple warning message
  205. *
  206. * @static
  207. * @uses PMA_Message as returned object
  208. * @uses PMA_Message::WARNING
  209. * @param string $string
  210. * @return PMA_Message
  211. */
  212. static public function warning($string)
  213. {
  214. return new PMA_Message($string, PMA_Message::WARNING);
  215. }
  216. /**
  217. * get PMA_Message of type notice
  218. *
  219. * shorthand for getting a simple notice message
  220. *
  221. * @static
  222. * @uses PMA_Message as returned object
  223. * @uses PMA_Message::NOTICE
  224. * @param string $string
  225. * @return PMA_Message
  226. */
  227. static public function notice($string)
  228. {
  229. return new PMA_Message($string, PMA_Message::NOTICE);
  230. }
  231. /**
  232. * get PMA_Message with customized content
  233. *
  234. * shorthand for getting a customized message
  235. *
  236. * @static
  237. * @uses PMA_Message as returned object
  238. * @uses PMA_Message::setMessage()
  239. * @param string $message
  240. * @param integer $type
  241. * @return PMA_Message
  242. */
  243. static public function raw($message, $type = PMA_Message::NOTICE)
  244. {
  245. $r = new PMA_Message('', $type);
  246. $r->setMessage($message);
  247. return $r;
  248. }
  249. /**
  250. * get PMA_Message of type error with custom content
  251. *
  252. * shorthand for getting a customized error message
  253. *
  254. * @static
  255. * @uses PMA_Message::raw()
  256. * @uses PMA_Message::ERROR
  257. * @param string $message
  258. * @return PMA_Message
  259. */
  260. static public function rawError($message)
  261. {
  262. return PMA_Message::raw($message, PMA_Message::ERROR);
  263. }
  264. /**
  265. * get PMA_Message of type warning with custom content
  266. *
  267. * shorthand for getting a customized warning message
  268. *
  269. * @static
  270. * @uses PMA_Message::raw()
  271. * @uses PMA_Message::WARNING
  272. * @param string $message
  273. * @return PMA_Message
  274. */
  275. static public function rawWarning($message)
  276. {
  277. return PMA_Message::raw($message, PMA_Message::WARNING);
  278. }
  279. /**
  280. * get PMA_Message of type notice with custom content
  281. *
  282. * shorthand for getting a customized notice message
  283. *
  284. * @static
  285. * @uses PMA_Message::raw()
  286. * @uses PMA_Message::NOTICE
  287. * @param string $message
  288. * @return PMA_Message
  289. */
  290. static public function rawNotice($message)
  291. {
  292. return PMA_Message::raw($message, PMA_Message::NOTICE);
  293. }
  294. /**
  295. * get PMA_Message of type success with custom content
  296. *
  297. * shorthand for getting a customized success message
  298. *
  299. * @static
  300. * @uses PMA_Message::raw()
  301. * @uses PMA_Message::SUCCESS
  302. * @param string $message
  303. * @return PMA_Message
  304. */
  305. static public function rawSuccess($message)
  306. {
  307. return PMA_Message::raw($message, PMA_Message::SUCCESS);
  308. }
  309. /**
  310. * returns whether this message is a success message or not
  311. * and optionaly makes this message a success message
  312. *
  313. * @uses PMA_Message::SUCCESS
  314. * @uses PMA_Message::setNumber()
  315. * @uses PMA_Message::getNumber()
  316. * @param boolean $set
  317. * @return boolean whether this is a success message or not
  318. */
  319. public function isSuccess($set = false)
  320. {
  321. if ($set) {
  322. $this->setNumber(PMA_Message::SUCCESS);
  323. }
  324. return $this->getNumber() === PMA_Message::SUCCESS;
  325. }
  326. /**
  327. * returns whether this message is a notice message or not
  328. * and optionaly makes this message a notice message
  329. *
  330. * @uses PMA_Message::NOTICE
  331. * @uses PMA_Message::setNumber()
  332. * @uses PMA_Message::getNumber()
  333. * @param boolean $set
  334. * @return boolean whether this is a notice message or not
  335. */
  336. public function isNotice($set = false)
  337. {
  338. if ($set) {
  339. $this->setNumber(PMA_Message::NOTICE);
  340. }
  341. return $this->getNumber() === PMA_Message::NOTICE;
  342. }
  343. /**
  344. * returns whether this message is a warning message or not
  345. * and optionaly makes this message a warning message
  346. *
  347. * @uses PMA_Message::WARNING
  348. * @uses PMA_Message::setNumber()
  349. * @uses PMA_Message::getNumber()
  350. * @param boolean $set
  351. * @return boolean whether this is a warning message or not
  352. */
  353. public function isWarning($set = false)
  354. {
  355. if ($set) {
  356. $this->setNumber(PMA_Message::WARNING);
  357. }
  358. return $this->getNumber() === PMA_Message::WARNING;
  359. }
  360. /**
  361. * returns whether this message is an error message or not
  362. * and optionaly 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]somes tring[/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() recursiv
  530. * @param mixed 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 PREG_SET_ORDER
  550. * @uses in_array()
  551. * @uses preg_match_all()
  552. * @uses preg_match()
  553. * @uses preg_replace()
  554. * @uses substr()
  555. * @uses strtr()
  556. * @param string $message the message
  557. * @return string the decoded message
  558. * @access public
  559. */
  560. static public function decodeBB($message)
  561. {
  562. $replace_pairs = array(
  563. '[i]' => '<em>', // deprecated by em
  564. '[/i]' => '</em>', // deprecated by em
  565. '[em]' => '<em>',
  566. '[/em]' => '</em>',
  567. '[b]' => '<strong>', // deprecated by strong
  568. '[/b]' => '</strong>', // deprecated by strong
  569. '[strong]' => '<strong>',
  570. '[/strong]' => '</strong>',
  571. '[tt]' => '<code>', // deprecated by CODE or KBD
  572. '[/tt]' => '</code>', // deprecated by CODE or KBD
  573. '[code]' => '<code>',
  574. '[/code]' => '</code>',
  575. '[kbd]' => '<kbd>',
  576. '[/kbd]' => '</kbd>',
  577. '[br]' => '<br />',
  578. '[/a]' => '</a>',
  579. '[sup]' => '<sup>',
  580. '[/sup]' => '</sup>',
  581. );
  582. $message = strtr($message, $replace_pairs);
  583. $pattern = '/\[a@([^"@]*)@([^]"]*)\]/';
  584. if (preg_match_all($pattern, $message, $founds, PREG_SET_ORDER)) {
  585. $valid_links = array(
  586. 'http', // default http:// links (and https://)
  587. './Do', // ./Documentation
  588. );
  589. foreach ($founds as $found) {
  590. // only http... and ./Do... allowed
  591. if (! in_array(substr($found[1], 0, 4), $valid_links)) {
  592. return $message;
  593. }
  594. // a-z and _ allowed in target
  595. if (! empty($found[2]) && preg_match('/[^a-z_]+/i', $found[2])) {
  596. return $message;
  597. }
  598. }
  599. $message = preg_replace($pattern, '<a href="\1" target="\2">', $message);
  600. }
  601. return $message;
  602. }
  603. /**
  604. * wrapper for sprintf()
  605. *
  606. * @uses sprintf()
  607. * @uses func_get_args()
  608. * @uses is_array()
  609. * @uses array_unshift()
  610. * @uses call_user_func_array()
  611. * @return string formated
  612. */
  613. static public function format()
  614. {
  615. $params = func_get_args();
  616. if (is_array($params[1])) {
  617. array_unshift($params[1], $params[0]);
  618. $params = $params[1];
  619. }
  620. return call_user_func_array('sprintf', $params);
  621. }
  622. /**
  623. * returns unique PMA_Message::$_hash, if not exists it will be created
  624. *
  625. * @uses PMA_Message::$_hash as return value and to set it if required
  626. * @uses PMA_Message::getNumber()
  627. * @uses PMA_Message::$_string
  628. * @uses PMA_Message::$_message
  629. * @uses md5()
  630. * @param string $file
  631. * @return string PMA_Message::$_hash
  632. */
  633. public function getHash()
  634. {
  635. if (null === $this->_hash) {
  636. $this->_hash = md5(
  637. $this->getNumber() .
  638. $this->_string .
  639. $this->_message
  640. );
  641. }
  642. return $this->_hash;
  643. }
  644. /**
  645. * returns compiled message
  646. *
  647. * @uses PMA_Message::$_message as return value
  648. * @uses PMA_Message::getString()
  649. * @uses PMA_Message::getParams()
  650. * @uses PMA_Message::format()
  651. * @uses PMA_Message::decodeBB()
  652. * @uses PMA_Message::getAddedMessages()
  653. * @uses strlen()
  654. * @return string complete message
  655. */
  656. public function getMessage()
  657. {
  658. $message = $this->_message;
  659. if (0 === strlen($message)) {
  660. $string = $this->getString();
  661. if (isset($GLOBALS[$string])) {
  662. $message = $GLOBALS[$string];
  663. } elseif (0 === strlen($string)) {
  664. $message = '';
  665. } else {
  666. $message = $string;
  667. }
  668. }
  669. if (count($this->getParams()) > 0) {
  670. $message = PMA_Message::format($message, $this->getParams());
  671. }
  672. $message = PMA_Message::decodeBB($message);
  673. foreach ($this->getAddedMessages() as $add_message) {
  674. $message .= $add_message;
  675. }
  676. return $message;
  677. }
  678. /**
  679. * returns PMA_Message::$_string
  680. *
  681. * @uses PMA_Message::$_string as return value
  682. * @return string PMA_Message::$_string
  683. */
  684. public function getString()
  685. {
  686. return $this->_string;
  687. }
  688. /**
  689. * returns PMA_Message::$_number
  690. *
  691. * @uses PMA_Message::$_number as return value
  692. * @return integer PMA_Message::$_number
  693. */
  694. public function getNumber()
  695. {
  696. return $this->_number;
  697. }
  698. /**
  699. * returns level of message
  700. *
  701. * @uses PMA_Message::$level
  702. * @uses PMA_Message::getNumber()
  703. * @return string level of message
  704. */
  705. public function getLevel()
  706. {
  707. return PMA_Message::$level[$this->getNumber()];
  708. }
  709. /**
  710. * Displays the message in HTML
  711. *
  712. * @uses PMA_Message::getLevel()
  713. * @uses PMA_Message::getMessage()
  714. * @uses PMA_Message::isDisplayed()
  715. */
  716. public function display()
  717. {
  718. echo $this->getDisplay();
  719. $this->isDisplayed(true);
  720. }
  721. /**
  722. * returns HTML code for displaying this message
  723. *
  724. * @return string whole message box
  725. */
  726. public function getDisplay()
  727. {
  728. return '<div class="' . $this->getLevel() . '">'
  729. . $this->getMessage() . '</div>';
  730. }
  731. /**
  732. * sets and returns whether the message was displayed or not
  733. *
  734. * @uses PMA_Message::$_is_displayed to set it and/or return it
  735. * @param boolean $is_displayed
  736. * @return boolean PMA_Message::$_is_displayed
  737. */
  738. public function isDisplayed($is_displayed = false)
  739. {
  740. if ($is_displayed) {
  741. $this->_is_displayed = true;
  742. }
  743. return $this->_is_displayed;
  744. }
  745. }
  746. ?>