Help Integrating WebPurify Image Checker


I am working on implementing the Web Purify Image Filter into our site. We currently use the WebPurify Profanity Filter to stop all bad language on the site. The Image filter is a new service, one for which they dont have a plugin developed yet. So, Ive been working at getting this thing integrated but Im not having much luck.

The code from the WebPurify Profanity Filter looks like this:

add_action( 'bp_init', 'bp_wpurify_init' );
add_action('admin_menu', 'webpurify_options_page');

function webpurify_options_page() {
add_options_page('WebPurify Options', 'WebPurify', 'manage_options','webpurifytextreplace/WebPurifyTextReplace.php');

function WebPurifyTextReplace($commentID) {
global $wpdb;

$API_KEY = get_option('webpurify_userkey');
$lang = get_option('webpurify_lang');
$repc = get_option('webpurify_r');

$table_name = $wpdb->prefix . "comments";
$getcomment = "SELECT comment_content from ".$table_name." where comment_ID = ".$commentID.";";
$content = $wpdb->get_var($getcomment);

$params = array(
'api_key' => $API_KEY,
'method' => '',
'text' => $content,
'replacesymbol' => $repc,
'lang' => $lang,
'cdata' => 1

$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);

# call the API and decode the response
$url = "".implode('&', $encoded_params);

$response = simplexml_load_file($url,'SimpleXMLElement', LIBXML_NOCDATA);
$ar = $response->text;

$update_comment = "UPDATE ".$table_name." set comment_content = '".mysql_escape_string($ar)."' where comment_ID = ".$commentID.";";
$results = $wpdb->query($update_comment);


You can view the documentation for the Profanity Filter here.

That works fine with no issues. It has another function used to clean Buddypress related posts, but it is almost identical to the one I posted.

Now, it seems like the code for the Image Filter would be very similar. The documentation for that is here.

A code example they give looks like this:

$checkurl = "[API KEY]&imgurl=<br />
$response = simplexml_load_file($checkurl,'SimpleXMLElement', LIBXML_NOCDATA);<br />

I started trying to change the code in the WebPurify Text Replacer plugin to match what I needed for the image filter. I removed what I didnt need and added what was missing. Here is the code I have so far…

add_action( 'bp_init', 'bp_wpurifyimg_init' );</p>
<p>function WebPurifyImgReplace($file) {<br />
global $wpdb;</p>
<p> $imgurl = ('bp_avatar_to_crop_src()');</p>
<p> $params = array(<br />
'api_key' => '384518ae5a67e79b6fab6213f0af9449',<br />
'method' => '',<br />
'imgurl' => $imgurl(''),<br />
<p> $encoded_params = array();<br />
foreach ($params as $k => $v){<br />
$encoded_params[] = urlencode($k).'='.urlencode($v);<br />
<p>#<br />
# call the API and decode the response<br />
#<br />
$url = "".implode('&', $encoded_params);</p>
<p> $response = simplexml_load_file($checkurl,'SimpleXMLElement', LIBXML_NOCDATA);<br />
var_dump($response);<br />
$response = $imgurl;</p>
<p>function bp_wpurifyimg_init() {<br />
add_action('bp_avatar_upload','WebPurifyImgReplace');}<br />

Now, the way the image filter works exactly is:

1. You send an IMG Check request:

The plugin would send a link like this to webpurify based on the image uploaded.<br />

2. We get a response from the Image Filter:

This is where I start to get confused… Webpurify will return this:

<?xml version="1.0" encoding="utf-8" ?><br />
<rsp stat="ok"><br />
<method></method><br />
<format>rest</format><br />
<imgid>7de93bc200ff21a26da6ddb115506e82</imgid><br />
<status>pending</status><br />
<api_key>f3412a9614845dc17d97a5d51a647a13</api_key><br />
</rsp><br />

3. Then it says:

“Once your image has been moderated our service will make a request to a callback url you provide with the results of the moderation.


We will make a GET request to your callback ie:

the imgid is the same as the imgid returned to you after using the imgcheck method.

Possible Status values:

status = 1 means the image has been approved and is clear of adult content

status = 2 means the image has been declined and contains adult content. “

4. So pretty much, I would deny the upload is the status was 2, and approve it was status of 1. How would I implement that into my current code though? I know it is horribly wrong, I was just doing some trial and error before I decided to give it a rest.

For the most part I want it to work like this:

1. User selects image to upload on avatar page and clicks upload.

2. Image is uploaded.

3. Image Filter request is sent to WebPurify.

4. Image is either approved or denied.

4a. If approved the user sees the “Crop Avatar” page.

4b. If denied the user sees an error to upload a different image.

I really hope this is possible. I dont think there are many ways for other users to help me test this because we only have the one API key, and it would then force someone else to get one to test it. If we can get it figured it out, I plan to release it as a plugin for people trying to integrate this into their social community in the future. I think it will help alot.

Thanks in advance,

Rob Webmaster