plugin-wonderful/classes/PublisherInfo.php

149 lines
4.8 KiB
PHP
Raw Normal View History

2009-01-17 18:36:29 +00:00
<?php
2009-02-07 00:10:58 +00:00
define("PW_ADBOXES_PROJECT_WONDERFUL", 0);
/**
* Information about an ad publisher.
*/
2009-01-17 18:36:29 +00:00
class PublisherInfo {
2009-02-07 00:10:58 +00:00
var $parser, $current_type, $current_adbox, $is_valid, $memberid, $adboxes, $current_string;
2009-01-17 18:36:29 +00:00
function PublisherInfo() {
foreach (array('memberid', 'adboxes') as $param) {
$this->{$param} = null;
}
}
2009-02-07 00:10:58 +00:00
function parse($string, $type = PW_ADBOXES_PROJECT_WONDERFUL) {
2009-01-17 18:36:29 +00:00
$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');
2009-02-07 00:10:58 +00:00
$this->current_type = $type;
2009-01-17 18:36:29 +00:00
$this->is_valid = true;
2009-02-11 23:24:42 +00:00
if (($result = xml_parse($this->parser, $string, true)) != 1) {
$this->is_valid = false;
}
2009-01-17 18:36:29 +00:00
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();
2009-02-11 23:24:42 +00:00
$translated_attributes = array('TYPE' => 'adtype');
foreach (array('ADBOXID', 'SITENAME', 'TYPE', 'URL', 'DIMENSIONS', 'RATING', 'CATEGORY') as $field) {
2009-01-17 18:36:29 +00:00
if (!isset($attributes[$field])) {
$this->is_valid = false; break;
} else {
2009-02-11 23:24:42 +00:00
$target_field = strtolower($field);
if (isset($translated_attributes[$field])) { $target_field = $translated_attributes[$field]; }
$new_attributes[$target_field] = $attributes[$field];
2009-01-17 18:36:29 +00:00
}
}
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; }
2009-02-11 23:24:42 +00:00
foreach (array('scheme', 'host') as $part) {
2009-01-17 18:36:29 +00:00
if (!isset($result[$part])) { $this->is_valid = false; break; }
}
if ($this->is_valid) {
2009-02-07 00:10:58 +00:00
$new_attributes['type'] = $this->current_type;
2009-01-17 18:36:29 +00:00
$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;
}
2009-02-07 00:10:58 +00:00
/**
* Get information to create widgets.
* @return array The widget information.
*/
function get_sidebar_widget_info() {
if ($this->is_valid) {
$widgets = array();
foreach ($this->adboxes as $adbox) {
$widgets[] = array(
"id" => "project_wonderful_{$this->memberid}_{$adbox->adboxid}",
2009-02-11 23:24:42 +00:00
"name" => sprintf(__('PW %1$s %2$s %3$s (%4$s)', 'comicpress-manager'), $adbox->adtype, $adbox->dimensions, $adbox->sitename, $adbox->adboxid),
2009-02-07 00:10:58 +00:00
"options" => array("adboxid" => $adbox->adboxid)
);
}
return $widgets;
} else {
return false;
}
}
/**
* Inject ads into body copy.
* @param string $copy The body copy to inject the ads into.
* @return string The body copy with injected ads.
*/
function inject_ads_into_body_copy($copy, $use_standardcode = false) {
foreach ($this->adboxes as $adbox) {
foreach (array('adboxid', 'template_tag_id') as $field) {
$code = $use_standardcode ? "standardcode" : "advancedcode";
$copy = str_replace("PW(" . $adbox->{$field} . ")", $adbox->{$code}, $copy);
}
}
$copy = preg_replace('#PW\\\\\(([^\\\]*)\\\\\)#', 'PW(\1)', $copy); // backslash alert!
return $copy;
}
2009-01-17 18:36:29 +00:00
}
?>