I'm new to this regex stuff.. I'm sure it makes sense but I've yet to fully grasp it.
I run a site and use a HtmlPurifier to filter user input. In order to allow playlist code I had to create a regex.
One of my users mentioned losing their background image on their playlist. The reason is because I'm not checking to see if there is a background image in their playlist. Let me show you an example of what I am talking about.
Here is the usual code that the user will be pasting in my textarea:
<object width="435" height="270">
<param name="movie" value="http://www.musiclist.us/mc/mp3player_new.swf"></param>
<param name="allowscriptaccess" value="never"></param>
<param name="wmode" value="transparent"></param>
<param name="flashvars" value="config=http%3A%2F%2Fwww.indimusic.us%2Fext%2Fpc%2Fconfig_black_shuffle.xml&mywidth=435&myheight=270&playlist_url=http%3A%2F%2Fwww.musiclist.us%2Fpl.php%3Fplaylist%3D27578280%26t%3D1265099482&wid=os"></param>
<embed style="width:435px; visibility:visible; height:270px;" allowScriptAccess="never" src="http://www.musiclist.us/mc/mp3player_new.swf" flashvars="config=http%3A%2F%2Fwww.indimusic.us%2Fext%2Fpc%2Fconfig_black_shuffle.xml&mywidth=435&myheight=270&playlist_url=http%3A%2F%2Fwww.musiclist.us%2Fpl.php%3Fplaylist%3D27578280%26t%3D1265099482&wid=os" width="435" height="270" name="mp3player" wmode="transparent" type="application/x-shockwave-flash" border="0"/>
</object>
And here is the code if the user has a custom background image
<object width="435" height="270">
<param name="movie" value="http://www.playlistproject.net/mc/mp3player_new.swf"></param>
<param name="allowscriptaccess" value="never"></param>
<param name="wmode" value="transparent"></param>
<param name="flashvars" value="config=http%3A%2F%2Fwww.indimusic.us%2Fext%2Fpc%2Fskins%2Fconfig_white.xml&mywidth=435&myheight=270&playlist_url=http%3A%2F%2Fwww.playlistproject.net%2Fpl.php%3Fplaylist%3D27578280%26t%3D1265100408&skinurl=http%3A%2F%2Fi75.photobucket.com%2Falbums%2Fi302%2Fohhbaby_iloveyou%2Fmyspace%2F250nqeu.jpg+&wid=os"></param>
<embed style="width:435px; visibility:visible; height:270px;" allowScriptAccess="never" src="http://www.playlistproject.net/mc/mp3player_new.swf" flashvars="config=http%3A%2F%2Fwww.indimusic.us%2Fext%2Fpc%2Fskins%2Fconfig_white.xml&mywidth=435&myheight=270&playlist_url=http%3A%2F%2Fwww.playlistproject.net%2Fpl.php%3Fplaylist%3D27578280%26t%3D1265100408&skinurl=http%3A%2F%2Fi75.photobucket.com%2Falbums%2Fi302%2Fohhbaby_iloveyou%2Fmyspace%2F250nqeu.jpg+&wid=os" width="435" height="270" name="mp3player" wmode="transparent" type="application/x-shockwave-flash" border="0"/>
</object>
The code with the background image is basically the same as the first one but has this parameter added:
skinurl=http%3A%2F%2Fi75.photobucket.com%2Falbums%2Fi302%2Fohhbaby_iloveyou%2Fmyspace%2F250nqeu.jpg
Now here is the class that I am using which includes my current regex
class HTMLPurifier_Filter_Playlist extends HTMLPurifier_Filter
{
public $name="Playlist";
public function preFilter($html, $config, $context)
{
$pre_regex = '/<embed(.+)config_([a-zA-Z]+[_]*[a-zA-Z]*)(.+)playlist%3D([0-9]+)(.+)/';
$pre_replace = '<span class="playlist-$2">$4</span>';
return preg_replace($pre_regex, $pre_replace, $html);
}
public function postFilter($html, $config, $context)
{
$post_regex = '/<span class="playlist-([a-zA-Z]+[_]*[a-zA-Z]*)">([0-9]+)<\/span>/';
$post_replace = '<object width="435" height="270"> <param name="movie" value="http://www.musiclist.us/mc/mp3player_new.swf"></param> <param name="allowscriptaccess" value="never"></param> <param name="wmode" value="transparent"></param> <param name="flashvars" value="config=http%3A%2F%2Fwww.indimusic.us%2Fext%2Fpc%2Fconfig_$1.xml&mywidth=435&myheight=270&playlist_url=http%3A%2F%2Fwww.musiclist.us%2Fpl.php%3Fplaylist%3D$2%26t%3D1265099482&wid=os"></param> <embed style="width:435px; visibility:visible; height:270px;" allowScriptAccess="never" src="http://www.musiclist.us/mc/mp3player_new.swf" flashvars="config=http%3A%2F%2Fwww.indimusic.us%2Fext%2Fpc%2Fconfig_$1.xml&mywidth=435&myheight=270&playlist_url=http%3A%2F%2Fwww.musiclist.us%2Fpl.php%3Fplaylist%3D$2%26t%3D1265099482&wid=os" width="435" height="270" name="mp3player" wmode="transparent" type="application/x-shockwave-flash" border="0"/> </object>';
return preg_replace($post_regex, $post_replace, $html);
}
}
If I simply change $pre_regex to include the 'skinurl' parameter then any time someone pastes code into the textarea that doesnt have the skinurl parameter the code will be wiped out since it won't match the regex requirements.
If I don't change the $pre_regex then the skinurl=xxxxxx won't be put into a variable for use if the user has a background image on their playlist.
I know there must be an easy fix that I can't think of. I'm hoping some of you more experiences coders can give me a nudge.
Thanks in advance!