PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Services / Csv / CsvWriter.php

CsvWriter.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Services/Csv/CsvWriter.php

100 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services\Csv;
4
5 use FluentSupport\App\Services\Includes\FileSystem;
6
7 class CsvWriter
8 {
9 protected $path;
10 protected $fileName;
11 protected $filePath;
12 protected $delimiter = ',';
13 protected $enclosure = '"';
14 protected $file;
15 protected $fileMode = 'w+';
16
17 public function __construct()
18 {
19 $this->fileName = uniqid().'.csv';
20 $this->path = $this->_getDir().'/_tempCSV';
21 $this->_mkdir();
22 $this->file = $this->_make_file();
23 }
24
25 public function _getDir()
26 {
27 $uploadDir = wp_upload_dir();
28
29 return $uploadDir['basedir'] .'/'. FLUENT_SUPPORT_UPLOAD_DIR;
30 }
31
32 public function _mkdir()
33 {
34 if (!is_dir($this->path)) {
35 mkdir($this->path, 0777, true);
36 }
37 }
38
39 public function _make_file(){
40 $this->filePath = $this->path.'/'.$this->fileName;
41 file_exists($this->filePath) ?? unlink($this->filePath);
42
43 $f = fopen($this->filePath, $this->fileMode);
44
45 if ($f === false) {
46 die('Error opening the file ' . esc_html($this->filePath));
47 }
48
49 return $f;
50 }
51
52 public function insertOne($row){
53 $this->file = fopen($this->filePath, 'a+');
54 fputcsv($this->file, array_map([$this, 'sanitizeCell'], $row), $this->delimiter, $this->enclosure);
55 fclose($this->file);
56 }
57
58 public function insertAll($data){
59 $this->file = fopen($this->filePath, 'a+');
60 foreach ($data as $row) {
61 fputcsv($this->file, array_map([$this, 'sanitizeCell'], $row), $this->delimiter, $this->enclosure);
62 }
63 fclose($this->file);
64 }
65
66 /**
67 * Neutralize spreadsheet formula injection (CWE-1236) by prefixing an
68 * apostrophe onto any string cell that starts with a character a
69 * spreadsheet application would interpret as the start of a formula.
70 */
71 public function sanitizeCell($value)
72 {
73 if (!is_string($value) || $value === '') {
74 return $value;
75 }
76
77 if (preg_match('/^[=+\-@\t\r]/', $value)) {
78 return "'" . $value;
79 }
80
81 return $value;
82 }
83
84 public function output($filename)
85 {
86 if (!is_null($filename) && file_exists($this->filePath)) {
87 $filename = preg_replace('/[\x00-\x1F]/', '', sanitize_file_name($filename));
88 header('Cache-control: private');
89 header('Content-Type: application/octet-stream');
90 header('Content-Length: '.filesize($this->filePath));
91 header('Content-Disposition: filename='.$filename);
92 //Read the size of the file
93 readfile($this->filePath);
94 unlink($this->filePath);
95 die();
96 }
97 die('Error opening the file ' . esc_html($this->filePath));
98 }
99 }
100