Censorship.php
author Dan Fuhry <dan@enanocms.org>
Mon, 12 Dec 2011 23:34:49 -0500
changeset 5 ce6af7a8f827
parent 4 4994af009663
child 6 3109ceb0b167
permissions -rwxr-xr-x
Getting reckless here
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
5
ce6af7a8f827 Getting reckless here
Dan Fuhry <dan@enanocms.org>
parents: 4
diff changeset
    23
	$text = preg_split('/(<a class="downloadbutton.*?<\/a>|<script[^>]*>.*?<\/script>|<(?:[a-z\/].+?|!--.+?--)>)/m', $text, NULL, PREG_SPLIT_DELIM_CAPTURE);
0
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
	{
2
c8e6db42385f Fix assumption that all blocks are at least 1 byte long
Dan Fuhry <dan@enanocms.org>
parents: 1
diff changeset
    26
		if ( strlen($block) < 1 )
c8e6db42385f Fix assumption that all blocks are at least 1 byte long
Dan Fuhry <dan@enanocms.org>
parents: 1
diff changeset
    27
			continue;
0
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    28
		if ( $block{0} == '<' )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    29
			continue;
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    30
		
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    31
		// split by words
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    32
		$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
    33
		foreach ( $block as &$word )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    34
		{
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    35
			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
    36
				continue;
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
			if ( mt_rand(0, 10) == 0 )
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    39
			{
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    40
				$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
    41
						preg_replace('/[A-z0-9]/', '█', $word)
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    42
						. '</a>'
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
			}
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
		$block = implode('', $block);
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
	$text = implode('', $text);
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    49
}
d7eb80b3c6f6 First revision... should be the only one
Dan Fuhry <dan@enanocms.org>
parents:
diff changeset
    50