| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Csv_File extends Red_FileIO |
| 4 |
{ |
| 5 |
function collect ($module) |
| 6 |
{ |
| 7 |
$pager = new RE_Pager ($_GET, $_SERVER['REQUEST_URI'], 'name', 'DESC', 'log'); |
| 8 |
$pager->per_page = 0; |
| 9 |
$this->id = $module->id; |
| 10 |
|
| 11 |
$items = Red_Item::get_by_module ($pager, $module->id); |
| 12 |
if (count ($items) > 0) |
| 13 |
{ |
| 14 |
foreach ($items AS $item) |
| 15 |
$this->items[] = array ('source' => $item->url, 'target' => ($item->action_type == 'url' ? $item->action_data : ''), 'last_count' => $item->last_count); |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
function feed ($filename = '', $heading = '') |
| 20 |
{ |
| 21 |
$filename = sprintf (__ ('module_%d.csv', 'redirection'), $this->id); |
| 22 |
|
| 23 |
header ("Content-Type: text/csv"); |
| 24 |
header ("Cache-Control: no-cache, must-revalidate"); |
| 25 |
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); |
| 26 |
header ('Content-Disposition: attachment; filename="'.$filename.'"'); |
| 27 |
|
| 28 |
if (count ($this->items) > 0) |
| 29 |
{ |
| 30 |
echo "source,target,hits\r\n"; |
| 31 |
|
| 32 |
foreach ($this->items AS $line) |
| 33 |
echo implode (",", array_map (array (&$this, 'escape'), $line))."\r\n"; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
function escape ($value) |
| 38 |
{ |
| 39 |
// Escape any special values |
| 40 |
$double = false; |
| 41 |
if (strpos ($value, ',') !== false || $value == '') |
| 42 |
$double = true; |
| 43 |
|
| 44 |
if (strpos ($value, '"') !== false) |
| 45 |
{ |
| 46 |
$double = true; |
| 47 |
$value = str_replace ('"', '""', $value); |
| 48 |
} |
| 49 |
|
| 50 |
if ($double) |
| 51 |
$value = '"'.$value.'"'; |
| 52 |
return $value; |
| 53 |
} |
| 54 |
|
| 55 |
function parse_csv ($string, $separator = ',') |
| 56 |
{ |
| 57 |
$string = str_replace('""', "'", $string); |
| 58 |
$bits = explode ('"',$string); |
| 59 |
$elements = array (); |
| 60 |
|
| 61 |
for ($i = 0; $i < count ($bits) ; $i++) |
| 62 |
{ |
| 63 |
if (($i % 2) == 1) |
| 64 |
$elements[] = $bits[$i]; |
| 65 |
else |
| 66 |
{ |
| 67 |
$rest = $bits[$i]; |
| 68 |
$rest = preg_replace ('/^'.$separator.'/', '', $rest); |
| 69 |
$rest = preg_replace ('/'.$separator.'$/', '', $rest); |
| 70 |
|
| 71 |
$elements = array_merge ($elements, explode ($separator, $rest)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $elements; |
| 76 |
} |
| 77 |
|
| 78 |
function load( $group, $data, $filename ) { |
| 79 |
$count = 0; |
| 80 |
$file = fopen( $filename, 'r' ); |
| 81 |
|
| 82 |
if ( $file ) { |
| 83 |
while ( ( $csv = fgetcsv( $file, 1000, ',' ) ) ) { |
| 84 |
if ( $csv[0] != 'source' && $csv[1] != 'target') { |
| 85 |
Red_Item::create( array( |
| 86 |
'source' => trim( $csv[0] ), |
| 87 |
'target' => trim( $csv[1] ), |
| 88 |
'regex' => $this->is_regex( $csv[0] ), |
| 89 |
'group' => $group, |
| 90 |
'match' => 'url', |
| 91 |
'red_action' => 'url' |
| 92 |
) ); |
| 93 |
|
| 94 |
$count++; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $count; |
| 100 |
} |
| 101 |
|
| 102 |
function is_regex ($url) |
| 103 |
{ |
| 104 |
$regex = '()[]$^?+'; |
| 105 |
$escape = false; |
| 106 |
|
| 107 |
for ($x = 0; $x < strlen ($url); $x++) |
| 108 |
{ |
| 109 |
if ($url{$x} == '\\') |
| 110 |
$escape = true; |
| 111 |
else if (strpos ($regex, $url{$x}) !== false && !$escape) |
| 112 |
return true; |
| 113 |
else |
| 114 |
$escape = false; |
| 115 |
} |
| 116 |
|
| 117 |
return false; |
| 118 |
} |
| 119 |
} |
| 120 |
?> |