PluginProbe
Redirection / 5.3.2
Redirection v5.3.2
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.2, at fileio/csv.php

138 lines 3.1 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', 'type', 'code', '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'] : '*';
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 return '"' . str_replace( '"', '""', $item ) . '"';
47 }
48
49 public function load( $group, $filename, $data ) {
50 ini_set( 'auto_detect_line_endings', true );
51
52 $file = fopen( $filename, 'r' );
53
54 ini_set( 'auto_detect_line_endings', false );
55
56 if ( $file ) {
57 $separators = [
58 ',',
59 ';',
60 '|',
61 ];
62
63 foreach ( $separators as $separator ) {
64 fseek( $file, 0 );
65 $count = $this->load_from_file( $group, $file, $separator );
66
67 if ( $count > 0 ) {
68 return $count;
69 }
70 }
71 }
72
73 return 0;
74 }
75
76 public function load_from_file( $group_id, $file, $separator ) {
77 global $wpdb;
78
79 $count = 0;
80
81 while ( ( $csv = fgetcsv( $file, 5000, $separator ) ) ) {
82 $item = $this->csv_as_item( $csv, $group_id );
83
84 if ( $item ) {
85 $created = Red_Item::create( $item );
86
87 // The query log can use up all the memory
88 $wpdb->queries = [];
89
90 if ( ! is_wp_error( $created ) ) {
91 $count++;
92 }
93 }
94 }
95
96
97 return $count;
98 }
99
100 private function get_valid_code( $code ) {
101 if ( get_status_header_desc( $code ) !== '' ) {
102 return intval( $code, 10 );
103 }
104
105 return 301;
106 }
107
108 public function csv_as_item( $csv, $group ) {
109 if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) {
110 return array(
111 'url' => trim( $csv[ self::CSV_SOURCE ] ),
112 'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ),
113 'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ),
114 'group_id' => $group,
115 'match_type' => 'url',
116 'action_type' => 'url',
117 'action_code' => isset( $csv[ self::CSV_CODE ] ) ? $this->get_valid_code( $csv[ self::CSV_CODE ] ) : 301,
118 );
119 }
120
121 return false;
122 }
123
124 private function parse_regex( $value ) {
125 return intval( $value, 10 ) === 1 ? true : false;
126 }
127
128 private function is_regex( $url ) {
129 $regex = '()[]$^*';
130
131 if ( strpbrk( $url, $regex ) === false ) {
132 return false;
133 }
134
135 return true;
136 }
137 }
138