| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Xml_File extends Red_FileIO |
| 4 |
{ |
| 5 |
function collect ($module) |
| 6 |
{ |
| 7 |
$this->name = $module->name; |
| 8 |
$this->id = $module->id; |
| 9 |
$this->type = $module->type; |
| 10 |
$this->options = unserialize ($module->options); |
| 11 |
|
| 12 |
if (!is_array ($this->options)) |
| 13 |
$this->options = array (); |
| 14 |
|
| 15 |
$this->groups = Red_Group::get_for_module ($module->id); |
| 16 |
if (is_array ($this->groups) && count ($this->groups) > 0) |
| 17 |
{ |
| 18 |
$pager = new RE_Pager ($_GET, admin_url( 'redirection.php' ), 'position', 'ASC', 'log'); |
| 19 |
$pager->per_page = 0; |
| 20 |
|
| 21 |
foreach ($this->groups AS $pos => $group) |
| 22 |
$this->groups[$pos]->items = Red_Item::get_by_group ($group->id, $pager); |
| 23 |
} |
| 24 |
else |
| 25 |
$this->groups = array (); |
| 26 |
|
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
function feed () |
| 31 |
{ |
| 32 |
$filename = sprintf (__ ('module_%d.xml', 'redirection'), $this->id); |
| 33 |
|
| 34 |
header ("Content-Type: text/xml"); |
| 35 |
header ("Cache-Control: no-cache, must-revalidate"); |
| 36 |
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
| 37 |
// header ('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 38 |
|
| 39 |
echo '<?xml version="1.0" encoding="utf-8"?>'; |
| 40 |
?> |
| 41 |
<redirection> |
| 42 |
<module name="<?php echo htmlspecialchars ($this->name) ?>" id="<?php echo $this->id ?>" type="<?php echo $this->type ?>"> |
| 43 |
<?php if (count ($this->options) > 0) : ?> |
| 44 |
<options> |
| 45 |
<?php foreach ($this->options AS $name => $value) : ?> |
| 46 |
<option name="<?php echo $name ?>"><?php echo htmlspecialchars ($value) ?></option> |
| 47 |
<?php endforeach; ?> |
| 48 |
</options> |
| 49 |
<?php endif; ?> |
| 50 |
|
| 51 |
<?php if (count ($this->groups) > 0) :?> |
| 52 |
<?php foreach ($this->groups AS $group) : ?> |
| 53 |
<group id="<?php echo $group->id ?>" name="<?php echo htmlspecialchars ($group->name) ?>" status="<?php echo $group->status ?>" position="<?php echo $group->position ?>" tracking="<?php echo $group->tracking ?>"> |
| 54 |
<?php if (count ($group->items) > 0) : ?> |
| 55 |
<?php foreach ($group->items AS $item) $this->output_item ($item); ?> |
| 56 |
<?php endif; ?> |
| 57 |
</group> |
| 58 |
<?php endforeach; ?> |
| 59 |
<?php endif; ?> |
| 60 |
</module> |
| 61 |
</redirection> |
| 62 |
<?php |
| 63 |
} |
| 64 |
|
| 65 |
function output_item ($item) |
| 66 |
{ |
| 67 |
$data = unserialize ($item->action_data); |
| 68 |
?> |
| 69 |
<item id="<?php echo $item->id ?>" position="<?php echo $item->position ?>" status="<?php echo $item->status ?>"> |
| 70 |
<source><?php echo htmlspecialchars ($item->url) ?></source> |
| 71 |
<match type="<?php echo $item->match_type ?>" regex="<?php echo $item->regex ?>"></match> |
| 72 |
<action type="<?php echo $item->action_type ?>" code="<?php echo $item->action_code ?>"> |
| 73 |
<?php if (is_array ($data) && count ($data) > 0) : ?>xxx |
| 74 |
<?php foreach ($data AS $key => $value) : ?> |
| 75 |
<option name="'.$key.'"> |
| 76 |
<?php if (is_array ($value)) : ?> |
| 77 |
<?php echo htmlspecialchars (serialize ($value)); ?> |
| 78 |
<?php else : ?> |
| 79 |
<?php echo htmlspecialchars ($value); ?> |
| 80 |
<?php endif; ?> |
| 81 |
</option> |
| 82 |
<?php endforeach; ?> |
| 83 |
<?php else: ?> |
| 84 |
<?php echo htmlspecialchars ($item->action_data); ?> |
| 85 |
<?php endif; ?> |
| 86 |
</action> |
| 87 |
<statistic count="<?php echo $item->last_count ?>" access="<?php echo $item->last_access ?>"/> |
| 88 |
</item> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
function load ($group, $data) |
| 93 |
{ |
| 94 |
$count = 0; |
| 95 |
if (function_exists ('simplexml_load_string')) |
| 96 |
{ |
| 97 |
global $wpdb; |
| 98 |
|
| 99 |
$xml = simplexml_load_string ($data); |
| 100 |
|
| 101 |
// Extract module |
| 102 |
$moduledata = array |
| 103 |
( |
| 104 |
'type' => (string)$xml->module['type'], |
| 105 |
'name' => sprintf (__ ('%s imported on %s at %s', 'redirection'), (string)$xml->module['name'], date ('M d Y'), date ('H:i')) |
| 106 |
); |
| 107 |
|
| 108 |
if (isset ($xml->module->options)) |
| 109 |
{ |
| 110 |
foreach ($xml->module->options->option AS $option) |
| 111 |
$options[(string)$option['name']] = trim ((string)$option); |
| 112 |
|
| 113 |
$moduledata['options'] = $options; |
| 114 |
} |
| 115 |
|
| 116 |
$module = Red_Module::create ($moduledata); |
| 117 |
|
| 118 |
// Look at groups |
| 119 |
if (count ($xml->module->group) > 0) |
| 120 |
{ |
| 121 |
foreach ($xml->module->group AS $group) |
| 122 |
{ |
| 123 |
$id = Red_Group::create (array ('module_id' => $module, 'name' => (string)$group['name'], 'status' => (string)$group['status'], 'position' => (string)$group['position'])); |
| 124 |
|
| 125 |
// Look at items |
| 126 |
if (count ($group->item) > 0) |
| 127 |
{ |
| 128 |
foreach ($group->item AS $item) |
| 129 |
{ |
| 130 |
$actiondata = array (); |
| 131 |
if (isset ($item->action->option) && count ($item->action->option) > 0) |
| 132 |
{ |
| 133 |
foreach ($item->action->option AS $option) |
| 134 |
$actiondata[(string)$option['key']] = trim ((string)$option); |
| 135 |
|
| 136 |
$actiondata = serialize ($actiondata); |
| 137 |
} |
| 138 |
else |
| 139 |
$actiondata = trim ((string)$item->action); |
| 140 |
|
| 141 |
$data = array |
| 142 |
( |
| 143 |
'group_id' => $id, |
| 144 |
'url' => trim ((string)$item->source), |
| 145 |
'position' => intval ((string)$item['position']), |
| 146 |
'status' => (string)$item['status'], |
| 147 |
'regex' => (string)$item->match['regex'], |
| 148 |
'match_type' => (string)$item->match['type'], |
| 149 |
'action_type' => (string)$item->action['type'], |
| 150 |
'action_code' => (string)$item->action['code'], |
| 151 |
'action_data' => $actiondata |
| 152 |
); |
| 153 |
|
| 154 |
foreach ($data AS $key => $value) |
| 155 |
$data[$key] = "'".$wpdb->escape ($value)."'"; |
| 156 |
|
| 157 |
// Easier to insert it directly here |
| 158 |
$wpdb->query ("INSERT INTO {$wpdb->prefix}redirection_items (".implode (',', array_keys ($data)).") VALUES (".implode (',', $data).")"); |
| 159 |
$count++; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
global $redirection; |
| 168 |
$redirection->render_error (__ ('XML importing is only available with PHP5 - you have PHP4.', 'redirection')); |
| 169 |
} |
| 170 |
|
| 171 |
return $count; |
| 172 |
} |
| 173 |
} |
| 174 |
?> |
| 175 |
|