initial commit

This commit is contained in:
John Bintz 2009-01-17 13:36:29 -05:00
commit 91df87a151
3 changed files with 192 additions and 0 deletions

99
classes/PublisherInfo.php Normal file
View File

@ -0,0 +1,99 @@
<?php
class PublisherInfo {
var $parser, $current_adbox, $is_valid, $memberid, $adboxes, $current_string;
function PublisherInfo() {
foreach (array('memberid', 'adboxes') as $param) {
$this->{$param} = null;
}
}
function parse($string) {
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'start_element_handler', 'end_element_handler');
xml_set_character_data_handler($this->parser, 'character_data_handler');
$this->is_valid = true;
if (($result = xml_parse($this->parser, $string, true)) != 1) { $this->is_valid = false; }
xml_parser_free($this->parser);
if ($this->is_valid) {
foreach (array('memberid', 'adboxes') as $required_parameter) {
if (is_null($this->{$required_parameter})) {
$this->is_valid = false; break;
}
}
}
return $this->is_valid;
}
function start_element_handler($parser, $name, $attributes) {
$this->current_string = "";
switch ($name) {
case "PW:MEMBER":
$valid = false;
if (isset($attributes['MEMBERID'])) {
if (is_numeric($attributes['MEMBERID'])) {
$this->memberid = $attributes['MEMBERID'];
$valid = true;
}
}
$this->is_valid = $valid;
break;
case "PW:ADBOXES":
$this->adboxes = array();
break;
case "PW:ADBOX":
$new_attributes = array();
foreach (array('ADBOXID', 'SITENAME', 'URL', 'DIMENSIONS', 'RATING', 'CATEGORY') as $field) {
if (!isset($attributes[$field])) {
$this->is_valid = false; break;
} else {
$new_attributes[strtolower($field)] = $attributes[$field];
}
}
if ($this->is_valid) {
if (!is_numeric($attributes['ADBOXID'])) { $this->is_valid = false; break; }
if (preg_match('#^[0-9]+x[0-9]+$#', $attributes['DIMENSIONS']) == 0) { $this->is_valid = false; break; }
if (($result = parse_url($attributes['URL'])) === false) { $this->is_valid = false; break; }
foreach (array('scheme', 'host', 'path') as $part) {
if (!isset($result[$part])) { $this->is_valid = false; break; }
}
if ($this->is_valid) {
$this->current_adbox = (object)$new_attributes;
}
}
break;
}
}
function end_element_handler($parser, $name) {
switch ($name) {
case "PW:ADBOX":
foreach (array("description", "tags", "standardcode", "advancedcode") as $element) {
if (!isset($this->current_adbox->{$element})) {
$this->is_valid = false; break;
}
}
if ($this->is_valid) { $this->adboxes[] = $this->current_adbox; }
break;
case "PW:DESCRIPTION":
case "PW:TAGS":
case "PW:STANDARDCODE":
case "PW:ADVANCEDCODE":
$short_name = strtolower(substr($name, 3));
$this->current_adbox->{$short_name} = $this->current_string;
break;
}
}
function character_data_handler($parser, $data) {
$this->current_string .= $data;
}
}
?>

28
plugin_wonderful.php Normal file
View File

@ -0,0 +1,28 @@
<?php
/*
Plugin Name: Plugin Wonderful
Plugin URI: http://www.coswellproductions.com
Description: Easily embed a Project Wonderful advertiser's PW ads
Version: 0.1
Author: John Bintz
Author URI: http://www.coswellproductions.org/wordpress/
Copyright 2009 John Bintz (email : jcoswell@coswellproductions.org)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
?>

View File

@ -0,0 +1,65 @@
<?php
require_once('../classes/PublisherInfo.php');
class TestPublisherInfo extends PHPUnit_Framework_TestCase {
private $parser;
public function setup() {
$this->parser = new PublisherInfo();
}
public static function badDataProvider() {
return array(
array("</test>"),
array("?xml version=\"1.0\""),
array('<pw:member></pw:member>'),
array('<pw:member memberid="1"></pw:member>'),
array('<pw:member memberid="meow"><pw:adboxes></pw:adboxes></pw:member>'),
array('<pw:member memberid="1"><pw:adboxes><pw:adbox /></pw:adboxes></pw:member>'),
array('<pw:member memberid="1"><pw:adboxes><pw:adbox adboxid="1" sitename="a" url="http://meow" dimensions="1x1" rating="a" category="a" /></pw:adboxes></pw:member>'),
array('<pw:member memberid="1"><pw:adboxes><pw:adbox adboxid="meow" sitename="a" url="http://meow" dimensions="1x1" rating="a" category="a"><pw:description>a</pw:description><pw:tags>a</pw:tags><pw:standardcode>a</pw:standardcode><pw:advancedcode>a</pw:advancedcode></pw:adbox></pw:adboxes></pw:member>'),
array('<pw:member memberid="1"><pw:adboxes><pw:adbox adboxid="1" sitename="a" url="http://meow" dimensions="a" rating="a" category="a"><pw:description>a</pw:description><pw:tags>a</pw:tags><pw:standardcode>a</pw:standardcode><pw:advancedcode>a</pw:advancedcode></pw:adbox></pw:adboxes></pw:member>'),
array('<pw:member memberid="1"><pw:adboxes><pw:adbox adboxid="1" sitename="a" url="a" dimensions="1x1" rating="a" category="a"><pw:description>a</pw:description><pw:tags>a</pw:tags><pw:standardcode>a</pw:standardcode><pw:advancedcode>a</pw:advancedcode></pw:adbox></pw:adboxes></pw:member>')
);
}
/**
* @dataProvider badDataProvider
*/
public function testBadPWData($string) {
$this->assertFalse($this->parser->parse($string));
}
public static function goodDataProvider() {
return array(
array('<pw:member memberid="1"><pw:adboxes></pw:adboxes></pw:member>'),
array('<pw:member memberid="1"><pw:adboxes><pw:adbox adboxid="1" sitename="a" url="http://meow.raow/" dimensions="1x1" rating="a" category="a"><pw:description>a</pw:description><pw:tags>a</pw:tags><pw:standardcode>a</pw:standardcode><pw:advancedcode>a</pw:advancedcode></pw:adbox></pw:adboxes></pw:member>')
);
}
/**
* @dataProvider goodDataProvider
*/
public function testGoodPWData($string) {
$this->assertTrue($this->parser->parse($string));
}
public function testPWAPI() {
$this->parser->parse('<pw:member memberid="1"><pw:adboxes><pw:adbox adboxid="1" sitename="a" url="http://meow.raow/" dimensions="1x1" rating="a" category="a"><pw:description>a</pw:description><pw:tags>a</pw:tags><pw:standardcode>a</pw:standardcode><pw:advancedcode>a</pw:advancedcode></pw:adbox></pw:adboxes></pw:member>');
$this->assertEquals(1, $this->parser->memberid);
$this->assertEquals(1, count($this->parser->adboxes));
$this->assertEquals(1, $this->parser->adboxes[0]->adboxid);
$this->assertEquals("a", $this->parser->adboxes[0]->sitename);
$this->assertEquals("http://meow.raow/", $this->parser->adboxes[0]->url);
$this->assertEquals("1x1", $this->parser->adboxes[0]->dimensions);
$this->assertEquals("a", $this->parser->adboxes[0]->rating);
$this->assertEquals("a", $this->parser->adboxes[0]->description);
$this->assertEquals("a", $this->parser->adboxes[0]->tags);
$this->assertEquals("a", $this->parser->adboxes[0]->standardcode);
$this->assertEquals("a", $this->parser->adboxes[0]->advancedcode);
}
}
?>