| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Csv_File extends Red_FileIO { |
| 4 |
var $id; |
| 5 |
var $items; |
| 6 |
|
| 7 |
function collect( $module ) { |
| 8 |
$this->id = $module->id; |
| 9 |
|
| 10 |
$items = Red_Item::get_by_module( $module->id ); |
| 11 |
|
| 12 |
if ( count( $items ) > 0 ) { |
| 13 |
foreach ( $items AS $item ) { |
| 14 |
$this->items[] = array( |
| 15 |
'source' => $item->url, |
| 16 |
'target' => ( $item->action_type == 'url' ? $item->action_data : '' ), |
| 17 |
'last_count' => $item->last_count |
| 18 |
); |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
function feed( $filename = '', $heading = '' ) { |
| 24 |
$filename = sprintf( __( 'module_%d.csv', 'redirection' ), $this->id ); |
| 25 |
|
| 26 |
header( 'Content-Type: text/csv' ); |
| 27 |
header( 'Cache-Control: no-cache, must-revalidate' ); |
| 28 |
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 29 |
header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
| 30 |
|
| 31 |
if ( count( $this->items ) > 0 ) { |
| 32 |
echo "source,target,hits\r\n"; |
| 33 |
|
| 34 |
$stdout = fopen( 'php://output', 'w' ); |
| 35 |
|
| 36 |
foreach ( $this->items AS $line ) { |
| 37 |
fputcsv( $stdout, $line ); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
function load( $group, $data, $filename = '' ) { |
| 43 |
$count = 0; |
| 44 |
$file = fopen( $filename, 'r' ); |
| 45 |
|
| 46 |
if ( $file ) { |
| 47 |
while ( ( $csv = fgetcsv( $file, 1000, ',' ) ) ) { |
| 48 |
if ( $csv[0] != 'source' && $csv[1] != 'target') { |
| 49 |
Red_Item::create( array( |
| 50 |
'source' => trim( $csv[0] ), |
| 51 |
'target' => trim( $csv[1] ), |
| 52 |
'regex' => $this->is_regex( $csv[0] ), |
| 53 |
'group' => $group, |
| 54 |
'match' => 'url', |
| 55 |
'red_action' => 'url' |
| 56 |
) ); |
| 57 |
|
| 58 |
$count++; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $count; |
| 64 |
} |
| 65 |
|
| 66 |
function is_regex ($url) { |
| 67 |
$regex = '()[]$^?+'; |
| 68 |
$escape = false; |
| 69 |
|
| 70 |
for ( $x = 0; $x < strlen( $url ); $x++ ) { |
| 71 |
|
| 72 |
if ( $url{$x} == '\\' ) |
| 73 |
$escape = true; |
| 74 |
elseif ( strpos( $regex, $url{$x} ) !== false && !$escape ) |
| 75 |
return true; |
| 76 |
else |
| 77 |
$escape = false; |
| 78 |
} |
| 79 |
|
| 80 |
return false; |
| 81 |
} |
| 82 |
} |
| 83 |
|