PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.1
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.1
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 1.5.5 All 68 releases
fluent-support / app / Services / Includes / FileSystem.php

FileSystem.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.4.1, at app/Services/Includes/FileSystem.php

179 lines 4.4 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\Includes;
4
5 use FluentSupport\Framework\Support\Arr;
6
7 class FileSystem
8 {
9
10 protected $subDir = '';
11
12
13 public function _setSubDir($subDir)
14 {
15 $this->subDir = $subDir;
16 return $this;
17 }
18
19 /**
20 * Read file content from custom upload dir of this application
21 * @return string [path]
22 */
23 public function _get($file)
24 {
25 $arr = explode('/', $file);
26 $fileName = end($arr);
27 return file_get_contents(
28 $this->getDir() . '/' . $fileName
29 );
30 }
31
32 /**
33 * Get custom upload dir name of this application
34 * @return string [directory path]
35 */
36 public function _getDir()
37 {
38 $uploadDir = wp_upload_dir();
39
40 return $uploadDir['basedir'] . FLUENT_SUPPORT_UPLOAD_DIR;
41 }
42
43 /**
44 * Get absolute path of file using custom upload dir name of this application
45 * @return string [file path]
46 */
47 public function _getAbsolutePathOfFile($file)
48 {
49 return $this->_getDir() . '/' . $file;
50 }
51
52 /**
53 * Upload files into custom upload dir of this application
54 * @return array
55 */
56 public function _uploadFromRequest()
57 {
58 return $this->_put(\FluentSupport\App\Services\Helper::FluentSupport('request')->files());
59 }
60
61 /**
62 * Upload files into custom upload dir of this application
63 * @param array $files
64 * @return array
65 */
66 public function _put($files)
67 {
68 if (!function_exists('wp_handle_upload')) {
69 require_once(ABSPATH . 'wp-admin/includes/file.php');
70 }
71
72 $this->overrideUploadDir();
73
74 $uploadOverrides = ['test_form' => false];
75
76 foreach ((array)$files as $file) {
77 $filesArray = $file->toArray();
78 $extraData = Arr::only($filesArray, ['name', 'size']);
79 $uploadsData = \wp_handle_upload($filesArray, $uploadOverrides);
80 $uploadedFiles[] = array_merge($extraData, $uploadsData);
81 }
82
83 return $uploadedFiles;
84 }
85
86 /**
87 * Delete a file from custom upload directory of this application
88 * @param array $files
89 * @return void
90 */
91 public function _delete($files)
92 {
93 $files = (array)$files;
94
95 foreach ($files as $file) {
96 $arr = explode('/', $file);
97 $fileName = end($arr);
98 @unlink($this->getDir() . '/' . $fileName);
99 }
100 }
101
102 /**
103 * Register filters for custom upload dir
104 */
105 public function _overrideUploadDir()
106 {
107 add_filter('wp_handle_upload_prefilter', function ($file) {
108 add_filter('upload_dir', [$this, '_setCustomUploadDir']);
109
110 add_filter('wp_handle_upload', function ($fileinfo) {
111 remove_filter('upload_dir', [$this, '_setCustomUploadDir']);
112 $filePath = $fileinfo['file'];
113 $fileinfo['file'] = basename($filePath);
114 $fileinfo['file_path'] = $filePath;
115 return $fileinfo;
116 });
117
118 return $this->_renameFileName($file);
119 });
120 }
121
122 /**
123 * Set plugin's custom upload dir
124 * @param array $param
125 * @return array $param
126 */
127 public function _setCustomUploadDir($param)
128 {
129 $folderName = '/'.FLUENT_SUPPORT_UPLOAD_DIR;
130
131 if($this->subDir) {
132 $folderName .= '/'.$this->subDir;
133 if(!is_dir($param['basedir'] . $folderName)) {
134 @mkdir($param['basedir'] . $folderName, 0755);
135 }
136 }
137
138 $param['url'] = $param['baseurl'] . $folderName;
139
140 $param['path'] = $param['basedir'] . $folderName;
141
142
143 if (!is_dir($param['path'])) {
144 @mkdir($param['path'], 0755);
145 }
146
147 return $param;
148 }
149
150 /**
151 * Rename the uploaded file name before saving
152 * @param array $file
153 * @return array $file
154 */
155 public function _renameFileName($file)
156 {
157 $prefix = 'fluent_support-' . md5(uniqid(rand())) . '___';
158
159 $file['name'] = $prefix . $file['name'];
160 return $file;
161 }
162
163 public static function __callStatic($method, $params)
164 {
165 $instance = new static;
166
167 return call_user_func_array([$instance, $method], $params);
168 }
169
170 public function __call($method, $params)
171 {
172 $hiddenMethod = "_" . $method;
173
174 $method = method_exists($this, $hiddenMethod) ? $hiddenMethod : $method;
175
176 return call_user_func_array([$this, $method], $params);
177 }
178 }
179