PluginProbe
Redirection / 5.3.10
Redirection v5.3.10
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.10, at fileio/csv.php

172 lines 3.7 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
29 if ( isset( $data['url'] ) ) {
30 $data = $data['url'];
31 } else {
32 $data = '/unknown';
33 }
34
35 if ( $item->get_action_code() > 400 && $item->get_action_code() < 500 ) {
36 $data = '';
37 }
38
39 $csv = array(
40 $item->get_url(),
41 $data,
42 $item->is_regex() ? 1 : 0,
43 $item->get_action_code(),
44 $item->get_action_type(),
45 $item->get_hits(),
46 $item->get_title(),
47 $item->is_enabled() ? 'active' : 'disabled',
48 );
49
50 $csv = array_map( array( $this, 'escape_csv' ), $csv );
51 return implode( ',', $csv );
52 }
53
54 public function escape_csv( $item ) {
55 if ( is_numeric( $item ) ) {
56 return $item;
57 }
58
59 return '"' . str_replace( '"', '""', $item ) . '"';
60 }
61
62 public function load( $group, $filename, $data ) {
63 ini_set( 'auto_detect_line_endings', true );
64
65 $file = fopen( $filename, 'r' );
66
67 ini_set( 'auto_detect_line_endings', false );
68
69 if ( $file ) {
70 $separators = [
71 ',',
72 ';',
73 '|',
74 ];
75
76 foreach ( $separators as $separator ) {
77 fseek( $file, 0 );
78 $count = $this->load_from_file( $group, $file, $separator );
79
80 if ( $count > 0 ) {
81 return $count;
82 }
83 }
84 }
85
86 return 0;
87 }
88
89 public function load_from_file( $group_id, $file, $separator ) {
90 global $wpdb;
91
92 $count = 0;
93
94 while ( ( $csv = fgetcsv( $file, 5000, $separator ) ) ) {
95 $item = $this->csv_as_item( $csv, $group_id );
96
97 if ( $item && $this->item_is_valid( $item ) ) {
98 $created = Red_Item::create( $item );
99
100 // The query log can use up all the memory
101 $wpdb->queries = [];
102
103 if ( ! is_wp_error( $created ) ) {
104 $count++;
105 }
106 }
107 }
108
109 return $count;
110 }
111
112 private function item_is_valid( array $csv ) {
113 if ( strlen( $csv['url'] ) === 0 ) {
114 return false;
115 }
116
117 if ( $csv['action_data']['url'] === $csv['url'] ) {
118 return false;
119 }
120
121 return true;
122 }
123
124 private function get_valid_code( $code ) {
125 if ( get_status_header_desc( $code ) !== '' ) {
126 return intval( $code, 10 );
127 }
128
129 return 301;
130 }
131
132 private function get_action_type( $code ) {
133 if ( $code > 400 && $code < 500 ) {
134 return 'error';
135 }
136
137 return 'url';
138 }
139
140 public function csv_as_item( $csv, $group ) {
141 if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) {
142 $code = isset( $csv[ self::CSV_CODE ] ) ? $this->get_valid_code( $csv[ self::CSV_CODE ] ) : 301;
143
144 return array(
145 'url' => trim( $csv[ self::CSV_SOURCE ] ),
146 'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ),
147 'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ),
148 'group_id' => $group,
149 'match_type' => 'url',
150 'action_type' => $this->get_action_type( $code ),
151 'action_code' => $code,
152 );
153 }
154
155 return false;
156 }
157
158 private function parse_regex( $value ) {
159 return intval( $value, 10 ) === 1 ? true : false;
160 }
161
162 private function is_regex( $url ) {
163 $regex = '()[]$^*';
164
165 if ( strpbrk( $url, $regex ) === false ) {
166 return false;
167 }
168
169 return true;
170 }
171 }
172