Censorship.php
author Dan Fuhry <dan@enanocms.org>
Mon, 12 Dec 2011 23:12:47 -0500
changeset 1 bfc5494ee70c
parent 0 d7eb80b3c6f6
child 2 c8e6db42385f
permissions -rwxr-xr-x
OK, I'll exclude the activism/stop-sopa page
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     1
<?php
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     2
/**!info**
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     3
{
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     4
  "Plugin Name"  : "Censorship",
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     5
  "Plugin URI"   : "http://enanocms.org/plugin/censorship",
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     6
  "Description"  : "Protest SOPA",
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     7
  "Author"       : "Dan Fuhry",
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     8
  "Version"      : "1.0",
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
     9
  "Author URI"   : "http://enanocms.org/"
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    10
}
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    11
**!*/
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    12
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    13
$plugins->attachHook('render_wikiformat_post', 'anti_sopa_censor($text);');
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    14
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    15
function anti_sopa_censor(&$text)
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    16
{
1
bfc5494ee70c OK, I'll exclude the activism/stop-sopa page
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
    17
	// don't censor the stop-sopa page
bfc5494ee70c OK, I'll exclude the activism/stop-sopa page
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
    18
	global $db, $session, $paths, $template, $plugins; // Common objects
bfc5494ee70c OK, I'll exclude the activism/stop-sopa page
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
    19
	if ( $paths->page_id == 'activism/stop-sopa' && $paths->namespace == 'Article' )
bfc5494ee70c OK, I'll exclude the activism/stop-sopa page
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
    20
		return;
bfc5494ee70c OK, I'll exclude the activism/stop-sopa page
Dan Fuhry <dan@enanocms.org>
parents: 0
diff changeset
    21
	
0
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    22
	// save HTML tags
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    23
	$text = preg_split('/(<(?:[a-z\/].+?|!--.+?--)>|<script[^>]*>[\w\W]*?<\/script>)/', $text, NULL, PREG_SPLIT_DELIM_CAPTURE);
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    24
	foreach ( $text as &$block )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    25
	{
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    26
		if ( $block{0} == '<' )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    27
			continue;
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    28
		
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    29
		// split by words
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    30
		$block = preg_split('/(\s+)/', $block, NULL, PREG_SPLIT_DELIM_CAPTURE);
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    31
		foreach ( $block as &$word )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    32
		{
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    33
			if ( preg_match('/^\s+$/', $word) || strlen($word) < 1 || !preg_match('/\w/', $word) )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    34
				continue;
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    35
			
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    36
			if ( mt_rand(0, 10) == 0 )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    37
			{
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    38
				$word = '<a href="http://enanocms.org/activism/stop-sopa" onclick="window.open(this.href); return false;" title="This word redacted by the US Government" style="color: black; background-color: black; background-image: none; padding-right: 0; text-decoration: none;">' .
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    39
						preg_replace('/[A-z0-9]/', '█', $word)
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    40
						. '</a>'
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    41
						;
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    42
			}
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    43
		}
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    44
		$block = implode('', $block);
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    45
	}
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    46
	$text = implode('', $text);
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    47
}
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    48