Help Integrating WebPurify Image Checker

Hi,

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' => 'webpurify.live.replace',
      '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 = "http://www.webpurify.com/services/rest/?".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 = "http://api1.webpurify.com/services/rest/?method=webpurify.live.imgcheck&api_key=[API KEY]&imgurl=http://farm1.static.flickr.com/30/59010752_4d16aca1ec_o.jpg<br />
$response = simplexml_load_file($checkurl,'SimpleXMLElement', LIBXML_NOCDATA);<br />
var_dump($response);

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' => 'webpurify.live.imgcheck',<br />
	  'imgurl' => $imgurl(''),<br />
    );</p>
<p>    $encoded_params = array();<br />
    foreach ($params as $k => $v){<br />
        $encoded_params[] = urlencode($k).'='.urlencode($v);<br />
    }</p>
<p>#<br />
# call the API and decode the response<br />
#<br />
    $url = "http://www.webpurify.com/services/rest/?".implode('&', $encoded_params);</p>
<p>    $response = simplexml_load_file($checkurl,'SimpleXMLElement', LIBXML_NOCDATA);<br />
	var_dump($response);<br />
	$response = $imgurl;</p>
<p>}</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.

https://api1.webpurify.com/services/rest/?method=webpurify.live.imgcheck&api_key=value&imgurl=http://farm1.static.flickr.com/30/59010752_4d16aca1ec_o.jpg<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>webpurify.live.imgcheck</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.

Responses
We will make a GET request to your callback ie: http://www.yourcallback.com/moderate.php?imgid=2342fdsfsf4ww4fsdfssf&status=1

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
Friendesha.com Webmaster

  • Friendesha

    Response from the WebPurify team:

    Hello Rob,

    The image filter works through a two step process.

    After you submit the URL of the image to the service, XML is returned with a unique ID for the image.

    that will look like so:

    <?xml version="1.0" encoding="utf-8" ?>

    <rsp stat="ok">
    <method>webpurify.live.imgcheck</method>
    <format>rest</format>
    <imgid>7de93bc200ff21a26da6ddb115506e82</imgid>
    <status>pending</status>

    <api_key>f3412a9614845dc17d97a5d51a647a13</api_key>
    </rsp>

    Once the image is moderated (which can take between 2 and 5 minutes)

    Our service makes a request to a script you will have to host. Our request is a GET that have two arguments "status" and "imgid"

    The imgid will be the ID we return to you after step 1.

    The status, the result will be:

    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.

    So, your code and database will have to store the image id and only allow the image to be displayed on your site once the image has been returned a status of 1.

    I hope this helps,

    Please let us know if you have any questions.

    Sincerely,

    WP Support

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.