PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/ShoutBox/ShoutBox.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 225 lines | 103 code | 25 blank | 97 comment | 12 complexity | b9718b503c91326b103edb6ee1e3a898 MD5 | raw file
  1. <?php
  2. /**
  3. * ShoutBox - Adds a parser function for embedding your own shoutbox.
  4. *
  5. * @file
  6. * @ingroup Extensions
  7. * @author Jim R. Wilson
  8. * @author Jack Phoenix <jack@countervandalism.net>
  9. * @version 0.2.1
  10. * @copyright Copyright © 2007 Jim R. Wilson
  11. * @copyright Copyright © 2010-2011 Jack Phoenix
  12. * @license The MIT License - http://www.opensource.org/licenses/mit-license.php
  13. * -----------------------------------------------------------------------
  14. * Description:
  15. * This is a MediaWiki extension which adds a parser function for embedding
  16. * your own shoutbox into a page.
  17. * Requirements:
  18. * MediaWiki 1.16+
  19. * Installation:
  20. * 1. Drop the files in $IP/extensions/ShoutBox
  21. * Note: $IP is your MediaWiki install dir.
  22. * 2. Enable the extension by adding this line to your LocalSettings.php:
  23. * require_once( $IP . '/extensions/ShoutBox/ShoutBox.php' );
  24. * Version Notes:
  25. * version 0.1:
  26. * Initial release.
  27. * -----------------------------------------------------------------------
  28. * Copyright © 2007 Jim R. Wilson
  29. *
  30. * Permission is hereby granted, free of charge, to any person obtaining a copy
  31. * of this software and associated documentation files (the "Software"), to deal
  32. * in the Software without restriction, including without limitation the rights to
  33. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  34. * the Software, and to permit persons to whom the Software is furnished to do
  35. * so, subject to the following conditions:
  36. *
  37. * The above copyright notice and this permission notice shall be included in all
  38. * copies or substantial portions of the Software.
  39. *
  40. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  41. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  42. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  43. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  44. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  45. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  46. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  47. * OTHER DEALINGS IN THE SOFTWARE.
  48. * -----------------------------------------------------------------------
  49. */
  50. # Confirm MW environment
  51. if( !defined( 'MEDIAWIKI' ) ) {
  52. die( "This is an extension to the MediaWiki package and cannot be run standalone." );
  53. }
  54. # Credits
  55. $wgExtensionCredits['parserhook'][] = array(
  56. 'path' => __FILE__,
  57. 'name' => 'ShoutBox',
  58. 'version' => '0.2.2',
  59. 'author' => array( 'Jim R. Wilson', 'Jack Phoenix' ),
  60. 'url' => 'http://www.mediawiki.org/wiki/Extension:ShoutBox',
  61. 'descriptionmsg' => 'shoutbox-desc',
  62. );
  63. // Configuration settings
  64. $wgShoutBoxMinWidth = 100;
  65. $wgShoutBoxMaxWidth = 600;
  66. $wgShoutBoxMinHeight = 100;
  67. $wgShoutBoxMaxHeight = 1024;
  68. $wgShoutBoxDefaultId = false;
  69. $wgShoutBoxCSS = false;
  70. // Internationalization file
  71. $dir = dirname( __FILE__ ) . '/';
  72. $wgExtensionMessagesFiles['ShoutBox'] = $dir . 'ShoutBox.i18n.php';
  73. // Hooked functions
  74. $wgHooks['ParserFirstCallInit'][] = 'ShoutBox::setup';
  75. $wgHooks['LanguageGetMagic'][] = 'ShoutBox::parserFunctionMagic';
  76. $wgHooks['ParserBeforeTidy'][] = 'ShoutBox::placeholderCorrections';
  77. /**
  78. * Wrapper class for encapsulating ShoutBox related parser methods
  79. */
  80. class ShoutBox {
  81. /**
  82. * Sets up parser functions.
  83. *
  84. * @param $parser Object: instance of running Parser.
  85. * @return Boolean: true
  86. */
  87. public static function setup( &$parser ) {
  88. # Setup parser hook
  89. $parser->setFunctionHook( 'shoutbox', array( 'ShoutBox', 'parserFunction' ) );
  90. return true;
  91. }
  92. /**
  93. * Adds magic words for parser functions.
  94. *
  95. * @param $magicWords Array: available magic words
  96. * @param $langCode String: language code
  97. * @return Boolean: always true
  98. */
  99. public static function parserFunctionMagic( &$magicWords, $langCode = 'en' ) {
  100. $magicWords['shoutbox'] = array( 0, 'shoutbox' );
  101. return true;
  102. }
  103. /**
  104. * Embeds ShoutBox
  105. *
  106. * @param $parser Object: Instance of running Parser.
  107. * @param $id Integer: Identifier of the ShoutBox (optional - if global default is available)
  108. * @param $width Integer: Width of box (optional)
  109. * @param $height Integer: Height of box (optional)
  110. * @return String: encoded representation of input params (to be processed later)
  111. */
  112. public static function parserFunction( $parser, $id = null, $width = null, $height = null ) {
  113. $params = array(
  114. 'id' => trim( $id ),
  115. 'width' => ( $width === null ? null : trim( $width ) ),
  116. 'height' => ( $height === null ? null : trim( $height ) )
  117. );
  118. return '<pre>@SBCONTENT@' . base64_encode( serialize( $params ) ) . '@SBCONTENT@</pre>';
  119. }
  120. /**
  121. * Performs placeholder corrections to bypass MW parser function output
  122. * restraints.
  123. *
  124. * @param $parser Object: Instance of running Parser.
  125. * @param $text String: Text of nearly fully rendered wikitext.
  126. * @return Boolean: always true
  127. */
  128. public static function placeholderCorrections( $parser, $text ) {
  129. $text = preg_replace_callback(
  130. '|<pre>@SBCONTENT@([0-9a-zA-Z\\+\\/]+=*)@SBCONTENT@</pre>|',
  131. array( 'ShoutBox', 'processShoutBoxParams' ),
  132. $text
  133. );
  134. return true;
  135. }
  136. /**
  137. * Performs placeholder corrections to bypass MW parser function output
  138. * restraints.
  139. *
  140. * @param $matches Array: An array of matches to the placeholder regular expression.
  141. * @return String: embedded shoutbox if params are sane|error message
  142. */
  143. public static function processShoutBoxParams( $matches ) {
  144. global $wgShoutBoxMinWidth, $wgShoutBoxMaxWidth;
  145. global $wgShoutBoxMinHeight, $wgShoutBoxMaxHeight;
  146. global $wgShoutBoxDefaultId, $wgShoutBoxCSS;
  147. $params = @unserialize( @base64_decode( $matches[1] ) );
  148. if( !$params ) {
  149. return '<div class="errorbox">' .
  150. wfMsg( 'shoutbox-unparsable-param-string', @htmlspecialchars( $matches[1] ) ) .
  151. '</div>';
  152. }
  153. $id = $params['id'];
  154. if( $id === null ) {
  155. $id = $wgShoutBoxDefaultId;
  156. }
  157. if( $id == null || !is_numeric( $id ) ) {
  158. return '<div class="errorbox">' .
  159. wfMsg( 'shoutbox-bad-id', @htmlspecialchars( $id ) ) .
  160. '</div>';
  161. }
  162. # Build URL and output embedded iframe
  163. $width = 150;
  164. $height = 300;
  165. # Retrieve and check width
  166. if( $params['width'] !== null ) {
  167. if (
  168. !is_numeric( $params['width'] ) ||
  169. $params['width'] < $wgShoutBoxMinWidth ||
  170. $params['width'] > $wgShoutBoxMaxWidth
  171. ) {
  172. return '<div class="errorbox">' .
  173. wfMsg( 'shoutbox-illegal-width', @htmlspecialchars( $params['width'] ) ) .
  174. '</div>';
  175. }
  176. $width = $params['width'];
  177. }
  178. # Retrieve and check height
  179. if( $params['height'] !== null ) {
  180. if (
  181. !is_numeric( $params['height'] ) ||
  182. $params['height'] < $wgShoutBoxMinWidth ||
  183. $params['height'] > $wgShoutBoxMaxWidth
  184. )
  185. {
  186. return '<div class="errorbox">' .
  187. wfMsg( 'shoutbox-illegal-height', @htmlspecialchars( $params['height'] ) ) .
  188. '</div>';
  189. }
  190. $height = $params['height'];
  191. }
  192. if( $wgShoutBoxCSS ) {
  193. $url = wfMsgForContent( 'shoutbox-url-with-css', $id, urlencode( $wgShoutBoxCSS ) );
  194. } else {
  195. $url = wfMsgForContent( 'shoutbox-url', $id );
  196. }
  197. return '<iframe src="' . $url . '" width="' . $width . '" height="' .
  198. $height . '" frameborder="0" allowTransparency="true"></iframe>';
  199. }
  200. }