mediawiki-extensions-InputBox/InputBox.classes.php

652 lines
18 KiB
PHP
Raw Normal View History

2008-10-27 20:33:18 +00:00
<?php
/**
* Classes for InputBox extension
*
* @file
* @ingroup Extensions
*/
// InputBox class
class InputBox {
/* Fields */
private $mParser;
2008-11-13 08:50:13 +00:00
private $mType = '';
private $mWidth = 50;
private $mPreload = '';
private $mPreloadparams = [];
2008-11-13 08:50:13 +00:00
private $mEditIntro = '';
private $mUseVE = '';
private $mSummary = '';
private $mNosummary = '';
private $mMinor = '';
2008-11-13 08:50:13 +00:00
private $mPage = '';
private $mBR = 'yes';
private $mDefaultText = '';
private $mPlaceholderText = '';
2008-11-13 08:50:13 +00:00
private $mBGColor = 'transparent';
private $mButtonLabel = '';
private $mSearchButtonLabel = '';
private $mFullTextButton = '';
private $mLabelText = '';
private $mHidden = '';
private $mNamespaces = '';
private $mID = '';
private $mInline = false;
private $mPrefix = '';
private $mDir = '';
private $mSearchFilter = '';
2008-10-27 20:33:18 +00:00
/* Functions */
2008-10-27 20:33:18 +00:00
public function __construct( $parser ) {
$this->mParser = $parser;
// Default value for dir taken from the page language (bug 37018)
$this->mDir = $this->mParser->getTargetLanguage()->getDir();
// Split caches by language, to make sure visitors do not see a cached
// version in a random language (since labels are in the user language)
$this->mParser->getOptions()->getUserLangObj();
$this->mParser->getOutput()->addModuleStyles( [
'ext.inputBox.styles',
'mediawiki.ui.input',
'mediawiki.ui.checkbox',
] );
2008-10-27 20:33:18 +00:00
}
2008-10-27 20:33:18 +00:00
public function render() {
// Handle various types
switch ( $this->mType ) {
2008-10-27 20:33:18 +00:00
case 'create':
case 'comment':
$this->mParser->getOutput()->addModules( 'ext.inputBox' );
2008-10-27 20:33:18 +00:00
return $this->getCreateForm();
case 'move':
return $this->getMoveForm();
case 'commenttitle':
return $this->getCommentForm();
2008-10-27 20:33:18 +00:00
case 'search':
return $this->getSearchForm( 'search' );
case 'fulltext':
return $this->getSearchForm( 'fulltext' );
2008-10-27 20:33:18 +00:00
case 'search2':
return $this->getSearchForm2();
default:
return Xml::tags( 'div', null,
Xml::element( 'strong',
[ 'class' => 'error' ],
strlen( $this->mType ) > 0
? wfMessage( 'inputbox-error-bad-type', $this->mType )->text()
: wfMessage( 'inputbox-error-no-type' )->text()
2008-10-27 20:33:18 +00:00
)
);
}
}
/*
* Returns the action name and value to use in inputboxes which redirects to edit pages.
* Decides, if the link should redirect to VE edit page (veaction=edit) or to wikitext editor
* (action=edit).
*
* @return Array Array with name and value data
*/
private function getEditActionArgs() {
// default is wikitext editor
$args = [
'name' => 'action',
'value' => 'edit',
];
// check, if VE is installed and VE editor is requested
if ( ExtensionRegistry::getInstance()->isLoaded( 'VisualEditor' ) && $this->mUseVE ) {
$args = [
'name' => 'veaction',
'value' => 'edit',
];
}
return $args;
}
/**
* Get common classes, that could be added and depend on, if
* a line break between a button and an input field is added or not.
*
* @return String
*/
private function getLinebreakClasses() {
return strtolower( $this->mBR ) === '<br />' ? 'mw-inputbox-input ' : '';
}
2008-10-27 20:33:18 +00:00
/**
* Generate search form
* @param $type
* @return string HTML
2008-10-27 20:33:18 +00:00
*/
public function getSearchForm( $type ) {
global $wgContLang, $wgNamespaceAliases;
2008-10-27 20:33:18 +00:00
// Use button label fallbacks
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMessage( 'inputbox-tryexact' )->text();
2008-10-27 20:33:18 +00:00
}
if ( !$this->mSearchButtonLabel ) {
$this->mSearchButtonLabel = wfMessage( 'inputbox-searchfulltext' )->text();
2008-10-27 20:33:18 +00:00
}
if ( $this->mID !== '' ) {
$idArray = [ 'id' => Sanitizer::escapeId( $this->mID ) ];
} else {
$idArray = [];
}
// We need a unqiue id to link <label> to checkboxes, but also
// want multiple <inputbox>'s to not be invalid html
$idRandStr = Sanitizer::escapeId( '-' . $this->mID . wfRandom(), 'noninitial' );
2008-10-27 20:33:18 +00:00
// Build HTML
$htmlOut = Xml::openElement( 'div',
[
'class' => 'mw-inputbox-centered',
'style' => $this->bgColorStyle(),
]
2008-10-27 20:33:18 +00:00
);
$htmlOut .= Xml::openElement( 'form',
[
2008-10-27 20:33:18 +00:00
'name' => 'searchbox',
'class' => 'searchbox',
'action' => SpecialPage::getTitleFor( 'Search' )->getLocalUrl(),
] + $idArray
2008-10-27 20:33:18 +00:00
);
$htmlOut .= Xml::element( 'input',
[
'class' => $this->getLinebreakClasses() . 'searchboxInput mw-ui-input mw-ui-input-inline',
2008-10-27 20:33:18 +00:00
'name' => 'search',
'type' => $this->mHidden ? 'hidden' : 'text',
'value' => $this->mDefaultText,
'placeholder' => $this->mPlaceholderText,
'size' => $this->mWidth,
'dir' => $this->mDir,
]
2008-10-27 20:33:18 +00:00
);
if ( $this->mPrefix != '' ) {
$htmlOut .= Html::hidden( 'prefix', $this->mPrefix );
}
if ( $this->mSearchFilter != '' ) {
$htmlOut .= Html::hidden( 'searchfilter', $this->mSearchFilter );
}
$htmlOut .= $this->mBR;
2008-10-27 20:33:18 +00:00
// Determine namespace checkboxes
$namespacesArray = explode( ',', $this->mNamespaces );
if ( $this->mNamespaces ) {
$namespaces = $wgContLang->getNamespaces();
$nsAliases = array_merge( $wgContLang->getNamespaceAliases(), $wgNamespaceAliases );
$showNamespaces = [];
$checkedNS = [];
// Check for valid namespaces
foreach ( $namespacesArray as $userNS ) {
$userNS = trim( $userNS ); // no whitespace
// Namespace needs to be checked if flagged with "**"
if ( strpos( $userNS, '**' ) ) {
$userNS = str_replace( '**', '', $userNS );
$checkedNS[$userNS] = true;
}
$mainMsg = wfMessage( 'inputbox-ns-main' )->inContentLanguage()->text();
if ( $userNS == 'Main' || $userNS == $mainMsg ) {
$i = 0;
} elseif ( array_search( $userNS, $namespaces ) ) {
$i = array_search( $userNS, $namespaces );
} elseif ( isset( $nsAliases[$userNS] ) ) {
$i = $nsAliases[$userNS];
} else {
continue; // Namespace not recognized, skip
}
$showNamespaces[$i] = $userNS;
if ( isset( $checkedNS[$userNS] ) && $checkedNS[$userNS] ) {
$checkedNS[$i] = true;
}
}
// Show valid namespaces
foreach ( $showNamespaces as $i => $name ) {
$checked = [];
// Namespace flagged with "**" or if it's the only one
if ( ( isset( $checkedNS[$i] ) && $checkedNS[$i] ) || count( $showNamespaces ) == 1 ) {
$checked = [ 'checked' => 'checked' ];
2008-10-27 20:33:18 +00:00
}
if ( count( $showNamespaces ) == 1 ) {
// Checkbox
$htmlOut .= Xml::element( 'input',
[
'type' => 'hidden',
'name' => 'ns' . $i,
'value' => 1,
'id' => 'mw-inputbox-ns' . $i . $idRandStr
] + $checked
);
} else {
// Checkbox
$htmlOut .= ' <div class="mw-inputbox-element mw-ui-checkbox">';
$htmlOut .= Xml::element( 'input',
[
'type' => 'checkbox',
'name' => 'ns' . $i,
'value' => 1,
'id' => 'mw-inputbox-ns' . $i . $idRandStr
] + $checked
);
// Label
$htmlOut .= Xml::label( $name, 'mw-inputbox-ns' . $i . $idRandStr );
$htmlOut .= '</div> ';
2008-10-27 20:33:18 +00:00
}
}
// Line break
$htmlOut .= $this->mBR;
} elseif ( $type == 'search' ) {
2008-10-27 20:33:18 +00:00
// Go button
$htmlOut .= Xml::element( 'input',
[
2008-10-27 20:33:18 +00:00
'type' => 'submit',
'name' => 'go',
'class' => 'mw-ui-button',
'value' => $this->mButtonLabel
]
2008-10-27 20:33:18 +00:00
);
Remove most named character references from output Recommit of r66254 to trunk. This was just find extensions phase3 -iname '*.php' \! -iname '*.i18n.php' \! -iname 'Messages*.php' \! -iname '*_Messages.php' -exec sed -i 's/&nbsp;/\&#160;/g;s/&mdash;/―/g;s/&bull;/•/g;s/&aacute;/á/g;s/&acute;/´/g;s/&agrave;/à/g;s/&alpha;/α/g;s/&auml;/ä/g;s/&ccedil;/ç/g;s/&copy;/©/g;s/&darr;/↓/g;s/&deg;/°/g;s/&eacute;/é/g;s/&ecirc;/ê/g;s/&euml;/ë/g;s/&egrave;/è/g;s/&euro;/€/g;s/&harr;//g;s/&hellip;/…/g;s/&iacute;/í/g;s/&igrave;/ì/g;s/&larr;/←/g;s/&ldquo;/“/g;s/&middot;/·/g;s/&minus;/−/g;s/&ndash;/–/g;s/&oacute;/ó/g;s/&ocirc;/ô/g;s/&oelig;/œ/g;s/&ograve;/ò/g;s/&otilde;/õ/g;s/&ouml;/ö/g;s/&pound;/£/g;s/&prime;/′/g;s/&Prime;/″/g;s/&raquo;/»/g;s/&rarr;/→/g;s/&rdquo;/”/g;s/&Sigma;/Σ/g;s/&times;/×/g;s/&uacute;/ú/g;s/&uarr;/↑/g;s/&uuml;/ü/g;s/&yen;/¥/g' {} + followed by reading over every single line of the resulting diff and fixing a whole bunch of false positives. The reason for this change is given in <http://lists.wikimedia.org/pipermail/wikitech-l/2010-April/047617.html>. I cleared it with Tim and Brion on IRC before committing. It might cause a few problems, but I tried to be careful; please report any issues. I skipped all messages files. I plan to make a follow-up commit that alters wfMsgExt() with 'escapenoentities' to sanitize all the entities. That way, the only messages that will be problems will be ones that output raw HTML, and we want to get rid of those anyway. This should get rid of all named entities everywhere except messages. I skipped a few things like &nbsp that I noticed in manual inspection, because they weren't well-formed XML anyway. Also, to everyone who uses non-breaking spaces when they could use a normal space, or nothing at all, or CSS padding: I still hate you. Die.
2010-05-30 17:33:59 +00:00
$htmlOut .= '&#160;';
2008-10-27 20:33:18 +00:00
}
2008-10-27 20:33:18 +00:00
// Search button
$htmlOut .= Xml::element( 'input',
[
2008-10-27 20:33:18 +00:00
'type' => 'submit',
'name' => 'fulltext',
'class' => 'mw-ui-button',
'value' => $this->mSearchButtonLabel
]
2008-10-27 20:33:18 +00:00
);
// Hidden fulltext param for IE (bug 17161)
if ( $type == 'fulltext' ) {
$htmlOut .= Html::hidden( 'fulltext', 'Search' );
}
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::closeElement( 'form' );
$htmlOut .= Xml::closeElement( 'div' );
2008-10-27 20:33:18 +00:00
// Return HTML
return $htmlOut;
}
2011-10-26 03:49:06 +00:00
/**
2008-10-27 20:33:18 +00:00
* Generate search form version 2
*/
public function getSearchForm2() {
// Use button label fallbacks
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMessage( 'inputbox-tryexact' )->text();
2008-10-27 20:33:18 +00:00
}
if ( $this->mID !== '' ) {
$unescapedID = $this->mID;
} else {
// The label element needs a unique id, use
// random number to avoid multiple input boxes
// having conflicts.
$unescapedID = wfRandom();
}
$id = Sanitizer::escapeId( $unescapedID, 'noninitial' );
2008-10-27 20:33:18 +00:00
$htmlLabel = '';
if ( isset( $this->mLabelText ) && strlen( trim( $this->mLabelText ) ) ) {
$this->mLabelText = $this->mParser->recursiveTagParse( $this->mLabelText );
$htmlLabel = Xml::openElement( 'label', [ 'for' => 'bodySearchInput' . $id ] );
$htmlLabel .= $this->mLabelText;
$htmlLabel .= Xml::closeElement( 'label' );
2008-10-27 20:33:18 +00:00
}
$htmlOut = Xml::openElement( 'form',
[
2008-10-27 20:33:18 +00:00
'name' => 'bodySearch' . $id,
'id' => 'bodySearch' . $id,
'class' => 'bodySearch' . ( $this->mInline ? ' mw-inputbox-inline' : '' ),
'action' => SpecialPage::getTitleFor( 'Search' )->getLocalUrl(),
]
2008-10-27 20:33:18 +00:00
);
$htmlOut .= Xml::openElement( 'div',
[
'class' => 'bodySearchWrap' . ( $this->mInline ? ' mw-inputbox-inline' : '' ),
'style' => $this->bgColorStyle(),
]
2008-10-27 20:33:18 +00:00
);
$htmlOut .= $htmlLabel;
$htmlOut .= Xml::element( 'input',
[
'type' => $this->mHidden ? 'hidden' : 'text',
2008-10-27 20:33:18 +00:00
'name' => 'search',
'class' => 'mw-ui-input mw-ui-input-inline',
'size' => $this->mWidth,
'id' => 'bodySearchInput' . $id,
'dir' => $this->mDir,
]
2008-10-27 20:33:18 +00:00
);
$htmlOut .= '&#160;' . Xml::element( 'input',
[
2008-10-27 20:33:18 +00:00
'type' => 'submit',
'name' => 'go',
'value' => $this->mButtonLabel,
'class' => 'mw-ui-button',
]
2008-10-27 20:33:18 +00:00
);
// Better testing needed here!
if ( !empty( $this->mFullTextButton ) ) {
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::element( 'input',
[
2008-10-27 20:33:18 +00:00
'type' => 'submit',
'name' => 'fulltext',
'class' => 'mw-ui-button',
'value' => $this->mSearchButtonLabel
]
2008-10-27 20:33:18 +00:00
);
}
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::closeElement( 'div' );
$htmlOut .= Xml::closeElement( 'form' );
// Return HTML
return $htmlOut;
}
/**
* Generate create page form
*/
public function getCreateForm() {
global $wgScript;
2008-10-27 20:33:18 +00:00
if ( $this->mType == "comment" ) {
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMessage( 'inputbox-postcomment' )->text();
2008-10-27 20:33:18 +00:00
}
} else {
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMessage( 'inputbox-createarticle' )->text();
2008-10-27 20:33:18 +00:00
}
}
2008-10-27 20:33:18 +00:00
$htmlOut = Xml::openElement( 'div',
[
'class' => 'mw-inputbox-centered',
'style' => $this->bgColorStyle(),
]
2008-10-27 20:33:18 +00:00
);
$createBoxParams = [
'name' => 'createbox',
'class' => 'createbox',
'action' => $wgScript,
'method' => 'get'
];
if ( $this->mID !== '' ) {
$createBoxParams['id'] = Sanitizer::escapeId( $this->mID );
}
$htmlOut .= Xml::openElement( 'form', $createBoxParams );
$editArgs = $this->getEditActionArgs();
$htmlOut .= Html::hidden( $editArgs['name'], $editArgs['value'] );
$htmlOut .= Html::hidden( 'preload', $this->mPreload );
foreach ( $this->mPreloadparams as $preloadparams ) {
$htmlOut .= Html::hidden( 'preloadparams[]', $preloadparams );
}
$htmlOut .= Html::hidden( 'editintro', $this->mEditIntro );
$htmlOut .= Html::hidden( 'summary', $this->mSummary );
$htmlOut .= Html::hidden( 'nosummary', $this->mNosummary );
$htmlOut .= Html::hidden( 'prefix', $this->mPrefix );
$htmlOut .= Html::hidden( 'minor', $this->mMinor );
if ( $this->mType == 'comment' ) {
$htmlOut .= Html::hidden( 'section', 'new' );
2008-10-27 20:33:18 +00:00
}
$htmlOut .= Xml::openElement( 'input',
[
'type' => $this->mHidden ? 'hidden' : 'text',
2008-10-27 20:33:18 +00:00
'name' => 'title',
'class' => $this->getLinebreakClasses() .
'mw-ui-input mw-ui-input-inline createboxInput',
'value' => $this->mDefaultText,
'placeholder' => $this->mPlaceholderText,
'size' => $this->mWidth,
'dir' => $this->mDir,
]
2008-10-27 20:33:18 +00:00
);
$htmlOut .= $this->mBR;
2008-10-27 20:33:18 +00:00
$htmlOut .= Xml::openElement( 'input',
[
2008-10-27 20:33:18 +00:00
'type' => 'submit',
'name' => 'create',
'class' => 'mw-ui-button mw-ui-progressive createboxButton',
'value' => $this->mButtonLabel
]
2008-10-27 20:33:18 +00:00
);
$htmlOut .= Xml::closeElement( 'form' );
$htmlOut .= Xml::closeElement( 'div' );
2008-10-27 20:33:18 +00:00
// Return HTML
return $htmlOut;
}
/**
* Generate move page form
*/
public function getMoveForm() {
global $wgScript;
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMessage( 'inputbox-movearticle' )->text();
}
$htmlOut = Xml::openElement( 'div',
[
'class' => 'mw-inputbox-centered',
'style' => $this->bgColorStyle(),
]
);
$moveBoxParams = [
'name' => 'movebox',
'class' => 'mw-movebox',
'action' => $wgScript,
'method' => 'get'
];
if ( $this->mID !== '' ) {
$moveBoxParams['id'] = Sanitizer::escapeId( $this->mID );
}
$htmlOut .= Xml::openElement( 'form', $moveBoxParams );
$htmlOut .= Html::hidden( 'title',
SpecialPage::getTitleFor( 'Movepage', $this->mPage )->getPrefixedText() );
$htmlOut .= Html::hidden( 'wpReason', $this->mSummary );
$htmlOut .= Html::hidden( 'prefix', $this->mPrefix );
$htmlOut .= Xml::openElement( 'input',
[
'type' => $this->mHidden ? 'hidden' : 'text',
'name' => 'wpNewTitle',
'class' => $this->getLinebreakClasses() . 'mw-moveboxInput mw-ui-input mw-ui-input-inline',
'value' => $this->mDefaultText,
'placeholder' => $this->mPlaceholderText,
'size' => $this->mWidth,
'dir' => $this->mDir,
]
);
$htmlOut .= $this->mBR;
$htmlOut .= Xml::openElement( 'input',
[
'type' => 'submit',
'class' => 'mw-ui-button mw-ui-progressive',
'value' => $this->mButtonLabel
]
);
$htmlOut .= Xml::closeElement( 'form' );
$htmlOut .= Xml::closeElement( 'div' );
// Return HTML
return $htmlOut;
}
/**
* Generate new section form
*/
public function getCommentForm() {
global $wgScript;
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMessage( 'inputbox-postcommenttitle' )->text();
}
$htmlOut = Xml::openElement( 'div',
[
'class' => 'mw-inputbox-centered',
'style' => $this->bgColorStyle(),
]
);
$commentFormParams = [
'name' => 'commentbox',
'class' => 'commentbox',
'action' => $wgScript,
'method' => 'get'
];
if ( $this->mID !== '' ) {
$commentFormParams['id'] = Sanitizer::escapeId( $this->mID );
}
$htmlOut .= Xml::openElement( 'form', $commentFormParams );
$editArgs = $this->getEditActionArgs();
$htmlOut .= Html::hidden( $editArgs['name'], $editArgs['value'] );
$htmlOut .= Html::hidden( 'preload', $this->mPreload );
foreach ( $this->mPreloadparams as $preloadparams ) {
$htmlOut .= Html::hidden( 'preloadparams[]', $preloadparams );
}
$htmlOut .= Html::hidden( 'editintro', $this->mEditIntro );
$htmlOut .= Xml::openElement( 'input',
[
2008-11-17 21:54:38 +00:00
'type' => $this->mHidden ? 'hidden' : 'text',
'name' => 'preloadtitle',
'class' => $this->getLinebreakClasses() . 'commentboxInput mw-ui-input mw-ui-input-inline',
'value' => $this->mDefaultText,
'placeholder' => $this->mPlaceholderText,
'size' => $this->mWidth,
'dir' => $this->mDir,
]
);
$htmlOut .= Html::hidden( 'section', 'new' );
$htmlOut .= Html::hidden( 'title', $this->mPage );
$htmlOut .= $this->mBR;
$htmlOut .= Xml::openElement( 'input',
[
'type' => 'submit',
'name' => 'create',
'class' => 'mw-ui-button mw-ui-progressive',
'value' => $this->mButtonLabel
]
);
$htmlOut .= Xml::closeElement( 'form' );
$htmlOut .= Xml::closeElement( 'div' );
// Return HTML
return $htmlOut;
}
2008-10-27 20:33:18 +00:00
/**
* Extract options from a blob of text
*
* @param string $text Tag contents
*/
public function extractOptions( $text ) {
// Parse all possible options
$values = [];
foreach ( explode( "\n", $text ) as $line ) {
if ( strpos( $line, '=' ) === false ) {
2008-10-27 20:33:18 +00:00
continue;
}
2008-10-27 20:33:18 +00:00
list( $name, $value ) = explode( '=', $line, 2 );
$name = strtolower( trim( $name ) );
$value = Sanitizer::decodeCharReferences( trim( $value ) );
if ( $name == 'preloadparams[]' ) {
// We have to special-case this one because it's valid for it to appear more than once.
$this->mPreloadparams[] = $value;
} else {
$values[ $name ] = $value;
}
2008-10-27 20:33:18 +00:00
}
// Validate the dir value.
if ( isset( $values['dir'] ) && !in_array( $values['dir'], [ 'ltr', 'rtl' ] ) ) {
unset( $values['dir'] );
}
2008-11-13 08:50:13 +00:00
// Build list of options, with local member names
$options = [
2008-11-13 08:50:13 +00:00
'type' => 'mType',
'width' => 'mWidth',
'preload' => 'mPreload',
'page' => 'mPage',
'editintro' => 'mEditIntro',
'useve' => 'mUseVE',
'summary' => 'mSummary',
'nosummary' => 'mNosummary',
'minor' => 'mMinor',
2008-11-13 08:50:13 +00:00
'break' => 'mBR',
'default' => 'mDefaultText',
'placeholder' => 'mPlaceholderText',
2008-11-13 08:50:13 +00:00
'bgcolor' => 'mBGColor',
'buttonlabel' => 'mButtonLabel',
'searchbuttonlabel' => 'mSearchButtonLabel',
'fulltextbutton' => 'mFullTextButton',
'namespaces' => 'mNamespaces',
'labeltext' => 'mLabelText',
'hidden' => 'mHidden',
'id' => 'mID',
'inline' => 'mInline',
'prefix' => 'mPrefix',
'dir' => 'mDir',
'searchfilter' => 'mSearchFilter'
];
foreach ( $options as $name => $var ) {
if ( isset( $values[$name] ) ) {
2008-11-13 08:50:13 +00:00
$this->$var = $values[$name];
2008-10-27 20:33:18 +00:00
}
}
2008-10-27 20:33:18 +00:00
// Insert a line break if configured to do so
$this->mBR = ( strtolower( $this->mBR ) == "no" ) ? ' ' : '<br />';
2008-10-27 20:33:18 +00:00
// Validate the width; make sure it's a valid, positive integer
$this->mWidth = intval( $this->mWidth <= 0 ? 50 : $this->mWidth );
// Validate background color
if ( !$this->isValidColor( $this->mBGColor ) ) {
$this->mBGColor = 'transparent';
}
2008-10-27 20:33:18 +00:00
}
/**
* Do a security check on the bgcolor parameter
*/
public function isValidColor( $color ) {
$regex = <<<REGEX
/^ (
[a-zA-Z]* | # color names
\# [0-9a-f]{3} | # short hexadecimal
\# [0-9a-f]{6} | # long hexadecimal
rgb \s* \( \s* (
\d+ \s* , \s* \d+ \s* , \s* \d+ | # rgb integer
[0-9.]+% \s* , \s* [0-9.]+% \s* , \s* [0-9.]+% # rgb percent
) \s* \)
) $ /xi
REGEX;
return (bool)preg_match( $regex, $color );
}
private function bgColorStyle() {
if ( $this->mBGColor != 'transparent' ) {
return 'background-color: ' . $this->mBGColor . ';';
}
return '';
}
2008-10-27 20:33:18 +00:00
}