PluginProbe
Redirection / 5.4
Redirection v5.4
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.4, at fileio/csv.php

177 lines 3.8 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', '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 $group = Red_Group::get( $group_id );
94 if ( ! $group ) {
95 return 0;
96 }
97
98 while ( ( $csv = fgetcsv( $file, 5000, $separator ) ) ) {
99 $item = $this->csv_as_item( $csv, $group );
100
101 if ( $item && $this->item_is_valid( $item ) ) {
102 $created = Red_Item::create( $item );
103
104 // The query log can use up all the memory
105 $wpdb->queries = [];
106
107 if ( ! is_wp_error( $created ) ) {
108 $count++;
109 }
110 }
111 }
112
113 return $count;
114 }
115
116 private function item_is_valid( array $csv ) {
117 if ( strlen( $csv['url'] ) === 0 ) {
118 return false;
119 }
120
121 if ( $csv['action_data']['url'] === $csv['url'] ) {
122 return false;
123 }
124
125 return true;
126 }
127
128 private function get_valid_code( $code ) {
129 if ( get_status_header_desc( $code ) !== '' ) {
130 return intval( $code, 10 );
131 }
132
133 return 301;
134 }
135
136 private function get_action_type( $code ) {
137 if ( $code > 400 && $code < 500 ) {
138 return 'error';
139 }
140
141 return 'url';
142 }
143
144 public function csv_as_item( $csv, Red_Group $group ) {
145 if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) {
146 $code = isset( $csv[ self::CSV_CODE ] ) ? $this->get_valid_code( $csv[ self::CSV_CODE ] ) : 301;
147
148 return array(
149 'url' => trim( $csv[ self::CSV_SOURCE ] ),
150 'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ),
151 'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ),
152 'group_id' => $group->get_id(),
153 'match_type' => 'url',
154 'action_type' => $this->get_action_type( $code ),
155 'action_code' => $code,
156 'status' => $group->is_enabled() ? 'enabled' : 'disabled',
157 );
158 }
159
160 return false;
161 }
162
163 private function parse_regex( $value ) {
164 return intval( $value, 10 ) === 1 ? true : false;
165 }
166
167 private function is_regex( $url ) {
168 $regex = '()[]$^*';
169
170 if ( strpbrk( $url, $regex ) === false ) {
171 return false;
172 }
173
174 return true;
175 }
176 }
177