/my/libraries/Message.class.php

https://github.com/cabenitez/factuweb · PHP · 802 lines · 296 code · 69 blank · 437 comment · 26 complexity · 5190849215fec6d0998fb14f60e352f7 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds class PMA_Message
  5. *
  6. * @version $Id: Error.class.php 10738 2007-10-08 16:02:58Z cybot_tm $
  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. *
  22. * // display raw warning message 'This is a warning!'
  23. * PMA_Message::rawWarning('This is a warning!')->display();
  24. * </code>
  25. *
  26. * more advanced usage example:
  27. * <code>
  28. * $message = PMA_Message::success('strSomeLocaleMessage');
  29. *
  30. * $hint = PMA_Message::notice('strSomeFootnote');
  31. * $hint->addParam('[a@./Documentation.html#cfg_Example@_blank]');
  32. * $hint->addParam('[/a]');
  33. * $hint = PMA_showHint($hint);
  34. *
  35. * $message->addMessage($hint);
  36. *
  37. * $more = PMA_Message::notice('strSomeMoreLocale');
  38. * $more->addString('strSomeEvenMoreLocale', '<br />');
  39. * $more->addParam('parameter for strSomeMoreLocale');
  40. * $more->addParam('more parameter for strSomeMoreLocale');
  41. *
  42. * $message->addMessage($more);
  43. * $message->addMessage('some final words', ' - ');
  44. *
  45. * $message->display();
  46. * // strSomeLocaleMessage <sup>1</sup> strSomeMoreLocale<br />
  47. * // strSomeEvenMoreLocale - some final words
  48. * </code>
  49. */
  50. class PMA_Message
  51. {
  52. const SUCCESS = 1; // 0001
  53. const NOTICE = 2; // 0010
  54. const WARNING = 4; // 0100
  55. const ERROR = 8; // 1000
  56. const SANITIZE_NONE = 0; // 0000 0000
  57. const SANITIZE_STRING = 16; // 0001 0000
  58. const SANITIZE_PARAMS = 32; // 0010 0000
  59. const SANITIZE_BOOTH = 48; // 0011 0000
  60. /**
  61. * message levels
  62. *
  63. * @var array
  64. */
  65. static public $level = array (
  66. PMA_Message::SUCCESS => 'success',
  67. PMA_Message::NOTICE => 'notice',
  68. PMA_Message::WARNING => 'warning',
  69. PMA_Message::ERROR => 'error',
  70. );
  71. /**
  72. * The message number
  73. *
  74. * @access protected
  75. * @var integer
  76. */
  77. protected $_number = PMA_Message::NOTICE;
  78. /**
  79. * The locale string identifier
  80. *
  81. * @access protected
  82. * @var string
  83. */
  84. protected $_string = '';
  85. /**
  86. * The formated message
  87. *
  88. * @access protected
  89. * @var string
  90. */
  91. protected $_message = '';
  92. /**
  93. * Whether the message was already displayed
  94. *
  95. * @access protected
  96. * @var boolean
  97. */
  98. protected $_is_displayed = false;
  99. /**
  100. * Unique id
  101. *
  102. * @access protected
  103. * @var string
  104. */
  105. protected $_hash = null;
  106. /**
  107. * holds parameters
  108. *
  109. * @access protected
  110. * @var array
  111. */
  112. protected $_params = array();
  113. /**
  114. * holds additional messages
  115. *
  116. * @access protected
  117. * @var array
  118. */
  119. protected $_added_messages = array();
  120. /**
  121. * Constructor
  122. *
  123. * @uses PMA_Message::setNumber()
  124. * @uses PMA_Message::setString()
  125. * @uses PMA_Message::setParams()
  126. * @uses PMA_Message::NOTICE
  127. * @uses PMA_Message::SANITIZE_NONE
  128. * @uses PMA_Message::SANITIZE_STRING
  129. * @uses PMA_Message::SANITIZE_PARAMS
  130. * @param string $string
  131. * @param integer $number
  132. * @param array $$params
  133. * @param boolean $sanitize
  134. */
  135. public function __construct($string = '', $number = PMA_Message::NOTICE,
  136. $params = array(), $sanitize = PMA_Message::SANITIZE_NONE)
  137. {
  138. $this->setString($string, $sanitize & PMA_Message::SANITIZE_STRING);
  139. $this->setNumber($number);
  140. $this->setParams($params, $sanitize & PMA_Message::SANITIZE_PARAMS);
  141. }
  142. /**
  143. * magic method: return string representation for this object
  144. *
  145. * @uses PMA_Message::getMessage()
  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. * @static
  158. * @uses PMA_Message as returned object
  159. * @uses PMA_Message::SUCCESS
  160. * @param string $string
  161. * @return PMA_Message
  162. */
  163. static public function success($string = '')
  164. {
  165. if (empty($string)) {
  166. $string = 'strSuccess';
  167. }
  168. return new PMA_Message($string, PMA_Message::SUCCESS);
  169. }
  170. /**
  171. * get PMA_Message of type error
  172. *
  173. * shorthand for getting a simple error message
  174. *
  175. * @static
  176. * @uses PMA_Message as returned object
  177. * @uses PMA_Message::ERROR
  178. * @param string $string
  179. * @return PMA_Message
  180. */
  181. static public function error($string = '')
  182. {
  183. if (empty($string)) {
  184. $string = 'strError';
  185. }
  186. return new PMA_Message($string, PMA_Message::ERROR);
  187. }
  188. /**
  189. * get PMA_Message of type warning
  190. *
  191. * shorthand for getting a simple warning message
  192. *
  193. * @static
  194. * @uses PMA_Message as returned object
  195. * @uses PMA_Message::WARNING
  196. * @param string $string
  197. * @return PMA_Message
  198. */
  199. static public function warning($string)
  200. {
  201. return new PMA_Message($string, PMA_Message::WARNING);
  202. }
  203. /**
  204. * get PMA_Message of type notice
  205. *
  206. * shorthand for getting a simple notice message
  207. *
  208. * @static
  209. * @uses PMA_Message as returned object
  210. * @uses PMA_Message::NOTICE
  211. * @param string $string
  212. * @return PMA_Message
  213. */
  214. static public function notice($string)
  215. {
  216. return new PMA_Message($string, PMA_Message::NOTICE);
  217. }
  218. /**
  219. * get PMA_Message with customized content
  220. *
  221. * shorthand for getting a customized message
  222. *
  223. * @static
  224. * @uses PMA_Message as returned object
  225. * @uses PMA_Message::setMessage()
  226. * @param string $message
  227. * @param integer $type
  228. * @return PMA_Message
  229. */
  230. static public function raw($message, $type = PMA_Message::NOTICE)
  231. {
  232. $r = new PMA_Message('', $type);
  233. $r->setMessage($message);
  234. return $r;
  235. }
  236. /**
  237. * get PMA_Message of type error with custom content
  238. *
  239. * shorthand for getting a customized error message
  240. *
  241. * @static
  242. * @uses PMA_Message::raw()
  243. * @uses PMA_Message::ERROR
  244. * @param string $message
  245. * @return PMA_Message
  246. */
  247. static public function rawError($message)
  248. {
  249. return PMA_Message::raw($message, PMA_Message::ERROR);
  250. }
  251. /**
  252. * get PMA_Message of type warning with custom content
  253. *
  254. * shorthand for getting a customized warning message
  255. *
  256. * @static
  257. * @uses PMA_Message::raw()
  258. * @uses PMA_Message::WARNING
  259. * @param string $message
  260. * @return PMA_Message
  261. */
  262. static public function rawWarning($message)
  263. {
  264. return PMA_Message::raw($message, PMA_Message::WARNING);
  265. }
  266. /**
  267. * get PMA_Message of type notice with custom content
  268. *
  269. * shorthand for getting a customized notice message
  270. *
  271. * @static
  272. * @uses PMA_Message::raw()
  273. * @uses PMA_Message::NOTICE
  274. * @param string $message
  275. * @return PMA_Message
  276. */
  277. static public function rawNotice($message)
  278. {
  279. return PMA_Message::raw($message, PMA_Message::NOTICE);
  280. }
  281. /**
  282. * get PMA_Message of type success with custom content
  283. *
  284. * shorthand for getting a customized success message
  285. *
  286. * @static
  287. * @uses PMA_Message::raw()
  288. * @uses PMA_Message::SUCCESS
  289. * @param string $message
  290. * @return PMA_Message
  291. */
  292. static public function rawSuccess($message)
  293. {
  294. return PMA_Message::raw($message, PMA_Message::SUCCESS);
  295. }
  296. /**
  297. * returns whether this message is a success message or not
  298. * and optionaly makes this message a success message
  299. *
  300. * @uses PMA_Message::SUCCESS
  301. * @uses PMA_Message::setNumber()
  302. * @uses PMA_Message::getNumber()
  303. * @param boolean $set
  304. * @return boolean whether this is a success message or not
  305. */
  306. public function isSuccess($set = false)
  307. {
  308. if ($set) {
  309. $this->setNumber(PMA_Message::SUCCESS);
  310. }
  311. return $this->getNumber() & PMA_Message::SUCCESS;
  312. }
  313. /**
  314. * returns whether this message is a notice message or not
  315. * and optionaly makes this message a notice message
  316. *
  317. * @uses PMA_Message::NOTICE
  318. * @uses PMA_Message::setNumber()
  319. * @uses PMA_Message::getNumber()
  320. * @param boolean $set
  321. * @return boolean whether this is a notice message or not
  322. */
  323. public function isNotice($set = false)
  324. {
  325. if ($set) {
  326. $this->setNumber(PMA_Message::NOTICE);
  327. }
  328. return $this->getNumber() & PMA_Message::NOTICE;
  329. }
  330. /**
  331. * returns whether this message is a warning message or not
  332. * and optionaly makes this message a warning message
  333. *
  334. * @uses PMA_Message::WARNING
  335. * @uses PMA_Message::setNumber()
  336. * @uses PMA_Message::getNumber()
  337. * @param boolean $set
  338. * @return boolean whether this is a warning message or not
  339. */
  340. public function isWarning($set = false)
  341. {
  342. if ($set) {
  343. $this->setNumber(PMA_Message::WARNING);
  344. }
  345. return $this->getNumber() & PMA_Message::WARNING;
  346. }
  347. /**
  348. * returns whether this message is an error message or not
  349. * and optionaly makes this message an error message
  350. *
  351. * @uses PMA_Message::ERROR
  352. * @uses PMA_Message::setNumber()
  353. * @uses PMA_Message::getNumber()
  354. * @param boolean $set
  355. * @return boolean whether this is an error message or not
  356. */
  357. public function isError($set = false)
  358. {
  359. if ($set) {
  360. $this->setNumber(PMA_Message::ERROR);
  361. }
  362. return $this->getNumber() & PMA_Message::ERROR;
  363. }
  364. /**
  365. * set raw message (overrides string)
  366. *
  367. * @uses PMA_Message::$_message to set it
  368. * @uses PMA_Message::sanitize()
  369. * @param string $message
  370. * @param boolean $sanitize whether to sanitize $message or not
  371. */
  372. public function setMessage($message, $sanitize = false)
  373. {
  374. if ($sanitize) {
  375. $message = PMA_Message::sanitize($message);
  376. }
  377. $this->_message = $message;
  378. }
  379. /**
  380. * set string (does not take effect if raw message is set)
  381. *
  382. * @uses PMA_Message::$_string to set it
  383. * @uses PMA_Message::sanitize()
  384. * @param string $_string
  385. * @param boolean $sanitize whether to sanitize $string or not
  386. */
  387. public function setString($_string, $sanitize = true)
  388. {
  389. if ($sanitize) {
  390. $_string = PMA_Message::sanitize($_string);
  391. }
  392. $this->_string = $_string;
  393. }
  394. /**
  395. * set message type number
  396. *
  397. * @uses PMA_Message::$_number to set it
  398. * @param integer $number
  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]somes tring[/em]');
  411. * $message->addParam('<img src="img" />', false);
  412. * </code>
  413. *
  414. * @uses htmlspecialchars()
  415. * @uses PMA_Message::$_params to fill
  416. * @uses PMA_Message::notice()
  417. * @param mixed $param
  418. * @param boolean $raw
  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. * @uses PMA_Message::$_added_messages to fill
  434. * @uses PMA_Message::notice()
  435. * @param string $string to be added
  436. * @param string $separator to use between this and previous string/message
  437. */
  438. public function addString($string, $separator = ' ')
  439. {
  440. $this->_added_messages[] = $separator;
  441. $this->_added_messages[] = PMA_Message::notice($string);
  442. }
  443. /**
  444. * add a bunch of messages at once
  445. *
  446. * @uses PMA_Message::addMessage()
  447. * @param array $messages to be added
  448. * @param string $separator to use between this and previous string/message
  449. */
  450. public function addMessages($messages, $separator = ' ')
  451. {
  452. foreach ($messages as $message) {
  453. $this->addMessage($message, $separator);
  454. }
  455. }
  456. /**
  457. * add another raw message to be concatenated on displaying
  458. *
  459. * @uses PMA_Message::$_added_messages to fill
  460. * @uses PMA_Message::rawNotice()
  461. * @param mixed $message to be added
  462. * @param string $separator to use between this and previous string/message
  463. */
  464. public function addMessage($message, $separator = ' ')
  465. {
  466. if (strlen($separator)) {
  467. $this->_added_messages[] = $separator;
  468. }
  469. if ($message instanceof PMA_Message) {
  470. $this->_added_messages[] = $message;
  471. } else {
  472. $this->_added_messages[] = PMA_Message::rawNotice($message);
  473. }
  474. }
  475. /**
  476. * set all params at once, usually used in conjunction with string
  477. *
  478. * @uses PMA_Message::sanitize()
  479. * @uses PMA_Message::$_params to set
  480. * @param array $params
  481. * @param boolean $sanitize
  482. */
  483. public function setParams($params, $sanitize = false)
  484. {
  485. if ($sanitize) {
  486. $params = PMA_Message::sanitize($params);
  487. }
  488. $this->_params = $params;
  489. }
  490. /**
  491. * return all parameters
  492. *
  493. * @uses PMA_Message::$_params as return value
  494. * @return array
  495. */
  496. public function getParams()
  497. {
  498. return $this->_params;
  499. }
  500. /**
  501. * return all added messages
  502. *
  503. * @uses PMA_Message::$_added_messages as return value
  504. * @return array
  505. */
  506. public function getAddedMessages()
  507. {
  508. return $this->_added_messages;
  509. }
  510. /**
  511. * Sanitizes $message
  512. *
  513. * @static
  514. * @uses is_array()
  515. * @uses htmlspecialchars()
  516. * @uses PMA_Message::sanitize() recursiv
  517. * @param mixed the message(s)
  518. * @return mixed the sanitized message(s)
  519. * @access public
  520. */
  521. static public function sanitize($message)
  522. {
  523. if (is_array($message)) {
  524. foreach ($message as $key => $val) {
  525. $message[$key] = PMA_Message::sanitize($val);
  526. }
  527. return $message;
  528. }
  529. return htmlspecialchars($message);
  530. }
  531. /**
  532. * decode $message, taking into account our special codes
  533. * for formatting
  534. *
  535. * @static
  536. * @uses PREG_SET_ORDER
  537. * @uses in_array()
  538. * @uses preg_match_all()
  539. * @uses preg_match()
  540. * @uses preg_replace()
  541. * @uses substr()
  542. * @uses strtr()
  543. * @param string $message the message
  544. * @return string the decoded message
  545. * @access public
  546. */
  547. static public function decodeBB($message)
  548. {
  549. $replace_pairs = array(
  550. '[i]' => '<em>', // deprecated by em
  551. '[/i]' => '</em>', // deprecated by em
  552. '[em]' => '<em>',
  553. '[/em]' => '</em>',
  554. '[b]' => '<strong>', // deprecated by strong
  555. '[/b]' => '</strong>', // deprecated by strong
  556. '[strong]' => '<strong>',
  557. '[/strong]' => '</strong>',
  558. '[tt]' => '<code>', // deprecated by CODE or KBD
  559. '[/tt]' => '</code>', // deprecated by CODE or KBD
  560. '[code]' => '<code>',
  561. '[/code]' => '</code>',
  562. '[kbd]' => '<kbd>',
  563. '[/kbd]' => '</kbd>',
  564. '[br]' => '<br />',
  565. '[/a]' => '</a>',
  566. '[sup]' => '<sup>',
  567. '[/sup]' => '</sup>',
  568. );
  569. $message = strtr($message, $replace_pairs);
  570. $pattern = '/\[a@([^"@]*)@([^]"]*)\]/';
  571. if (preg_match_all($pattern, $message, $founds, PREG_SET_ORDER)) {
  572. $valid_links = array(
  573. 'http', // default http:// links (and https://)
  574. './Do', // ./Documentation
  575. );
  576. foreach ($founds as $found) {
  577. // only http... and ./Do... allowed
  578. if (! in_array(substr($found[1], 0, 4), $valid_links)) {
  579. return $message;
  580. }
  581. // a-z and _ allowed in target
  582. if (! empty($found[2]) && preg_match('/[^a-z_]+/i', $found[2])) {
  583. return $message;
  584. }
  585. }
  586. $message = preg_replace($pattern, '<a href="\1" target="\2">', $message);
  587. }
  588. return $message;
  589. }
  590. /**
  591. * wrapper for sprintf()
  592. *
  593. * @uses sprintf()
  594. * @uses func_get_args()
  595. * @uses is_array()
  596. * @uses array_unshift()
  597. * @uses call_user_func_array()
  598. * @return string formated
  599. */
  600. static public function format()
  601. {
  602. $params = func_get_args();
  603. if (is_array($params[1])) {
  604. array_unshift($params[1], $params[0]);
  605. $params = $params[1];
  606. }
  607. return call_user_func_array('sprintf', $params);
  608. }
  609. /**
  610. * returns unique PMA_Message::$_hash, if not exists it will be created
  611. *
  612. * @uses PMA_Message::$_hash as return value and to set it if required
  613. * @uses PMA_Message::getNumber()
  614. * @uses PMA_Message::$_string
  615. * @uses PMA_Message::$_message
  616. * @uses md5()
  617. * @param string $file
  618. * @return string PMA_Message::$_hash
  619. */
  620. public function getHash()
  621. {
  622. if (null === $this->_hash) {
  623. $this->_hash = md5(
  624. $this->getNumber() .
  625. $this->_string .
  626. $this->_message
  627. );
  628. }
  629. return $this->_hash;
  630. }
  631. /**
  632. * returns compiled message
  633. *
  634. * @uses PMA_Message::$_message as return value
  635. * @uses PMA_Message::getString()
  636. * @uses PMA_Message::getParams()
  637. * @uses PMA_Message::format()
  638. * @uses PMA_Message::decodeBB()
  639. * @uses PMA_Message::getAddedMessages()
  640. * @uses strlen()
  641. * @return string complete message
  642. */
  643. public function getMessage()
  644. {
  645. $message = $this->_message;
  646. if (0 === strlen($message)) {
  647. $string = $this->getString();
  648. if (isset($GLOBALS[$string])) {
  649. $message = $GLOBALS[$string];
  650. } elseif (0 === strlen($string)) {
  651. $message = '';
  652. } else {
  653. $message = $string;
  654. }
  655. }
  656. if (count($this->getParams()) > 0) {
  657. $message = PMA_Message::format($message, $this->getParams());
  658. }
  659. $message = PMA_Message::decodeBB($message);
  660. foreach ($this->getAddedMessages() as $add_message) {
  661. $message .= $add_message;
  662. }
  663. return $message;
  664. }
  665. /**
  666. * returns PMA_Message::$_string
  667. *
  668. * @uses PMA_Message::$_string as return value
  669. * @return string PMA_Message::$_string
  670. */
  671. public function getString()
  672. {
  673. return $this->_string;
  674. }
  675. /**
  676. * returns PMA_Message::$_number
  677. *
  678. * @uses PMA_Message::$_number as return value
  679. * @return integer PMA_Message::$_number
  680. */
  681. public function getNumber()
  682. {
  683. return $this->_number;
  684. }
  685. /**
  686. * returns level of message
  687. *
  688. * @uses PMA_Message::$level
  689. * @uses PMA_Message::getNumber()
  690. * @return string level of message
  691. */
  692. public function getLevel()
  693. {
  694. return PMA_Message::$level[$this->getNumber()];
  695. }
  696. /**
  697. * Displays the message in HTML
  698. *
  699. * @uses PMA_Message::getLevel()
  700. * @uses PMA_Message::getMessage()
  701. * @uses PMA_Message::isDisplayed()
  702. */
  703. public function display()
  704. {
  705. echo $this->getDisplay();
  706. $this->isDisplayed(true);
  707. }
  708. /**
  709. * returns HTML code for displaying this message
  710. *
  711. * @return string whole message box
  712. */
  713. public function getDisplay()
  714. {
  715. return '<div class="' . $this->getLevel() . '">'
  716. . $this->getMessage() . '</div>';
  717. }
  718. /**
  719. * sets and returns whether the message was displayed or not
  720. *
  721. * @uses PMA_Message::$_is_displayed to set it and/or return it
  722. * @param boolean $is_displayed
  723. * @return boolean PMA_Message::$_is_displayed
  724. */
  725. public function isDisplayed($is_displayed = false)
  726. {
  727. if ($is_displayed){
  728. $this->_is_displayed = $is_displayed;
  729. }
  730. return $this->_is_displayed;
  731. }
  732. }
  733. ?>