PluginProbe
Redirection / 5.3.3
Redirection v5.3.3
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / fileio / csv.php

csv.php in Redirection 5.3.3, at fileio/csv.php

142 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Red_Csv_File extends Red_FileIO {
4 const CSV_SOURCE = 0;
5 const CSV_TARGET = 1;
6 const CSV_REGEX = 2;
7 const CSV_CODE = 3;
8
9 public function force_download() {
10 parent::force_download();
11
12 header( 'Content-Type: text/csv' );
13 header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'csv' ) . '"' );
14 }
15
16 public function get_data( array $items, array $groups ) {
17 $lines = [ implode( ',', array( 'source', 'target', 'regex', 'code', 'type', 'match', 'hits', 'title', 'status' ) ) ];
18
19 foreach ( $items as $line ) {
20 $lines[] = $this->item_as_csv( $line );
21 }
22
23 return implode( PHP_EOL, $lines ) . PHP_EOL;
24 }
25
26 public function item_as_csv( $item ) {
27 $data = $item->match->get_data();
28 $data = isset( $data['url'] ) ? $data = $data['url'] : '/unknown';
29
30 $csv = array(
31 $item->get_url(),
32 $data,
33 $item->is_regex() ? 1 : 0,
34 $item->get_action_code(),
35 $item->get_action_type(),
36 $item->get_hits(),
37 $item->get_title(),
38 $item->is_enabled() ? 'active' : 'disabled'
39 );
40
41 $csv = array_map( array( $this, 'escape_csv' ), $csv );
42 return implode( ',', $csv );
43 }
44
45 public function escape_csv( $item ) {
46 if ( is_numeric( $item ) ) {
47 return $item;
48 }
49
50 return '"' . str_replace( '"', '""', $item ) . '"';
51 }
52
53 public function load( $group, $filename, $data ) {
54 ini_set( 'auto_detect_line_endings', true );
55
56 $file = fopen( $filename, 'r' );
57
58 ini_set( 'auto_detect_line_endings', false );
59
60 if ( $file ) {
61 $separators = [
62 ',',
63 ';',
64 '|',
65 ];
66
67 foreach ( $separators as $separator ) {
68 fseek( $file, 0 );
69 $count = $this->load_from_file( $group, $file, $separator );
70
71 if ( $count > 0 ) {
72 return $count;
73 }
74 }
75 }
76
77 return 0;
78 }
79
80 public function load_from_file( $group_id, $file, $separator ) {
81 global $wpdb;
82
83 $count = 0;
84
85 while ( ( $csv = fgetcsv( $file, 5000, $separator ) ) ) {
86 $item = $this->csv_as_item( $csv, $group_id );
87
88 if ( $item ) {
89 $created = Red_Item::create( $item );
90
91 // The query log can use up all the memory
92 $wpdb->queries = [];
93
94 if ( ! is_wp_error( $created ) ) {
95 $count++;
96 }
97 }
98 }
99
100
101 return $count;
102 }
103
104 private function get_valid_code( $code ) {
105 if ( get_status_header_desc( $code ) !== '' ) {
106 return intval( $code, 10 );
107 }
108
109 return 301;
110 }
111
112 public function csv_as_item( $csv, $group ) {
113 if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) {
114 return array(
115 'url' => trim( $csv[ self::CSV_SOURCE ] ),
116 'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ),
117 'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ),
118 'group_id' => $group,
119 'match_type' => 'url',
120 'action_type' => 'url',
121 'action_code' => isset( $csv[ self::CSV_CODE ] ) ? $this->get_valid_code( $csv[ self::CSV_CODE ] ) : 301,
122 );
123 }
124
125 return false;
126 }
127
128 private function parse_regex( $value ) {
129 return intval( $value, 10 ) === 1 ? true : false;
130 }
131
132 private function is_regex( $url ) {
133 $regex = '()[]$^*';
134
135 if ( strpbrk( $url, $regex ) === false ) {
136 return false;
137 }
138
139 return true;
140 }
141 }
142