PluginProbe
VikBooking Hotel Booking Engine & PMS / trunk
VikBooking Hotel Booking Engine & PMS vtrunk
1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 All 36 releases
vikbooking / libraries / adapter / filesystem / file.php

file.php in VikBooking Hotel Booking Engine & PMS trunk, at libraries/adapter/filesystem/file.php

171 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 * @package VikWP - Libraries
4 * @subpackage adapter.filesystem
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 JLoader::import('adapter.filesystem.path');
15 JLoader::import('adapter.filesystem.folder');
16
17 /**
18 * This class provides a common interface for
19 * Files handling in the Wordpress CMS plugins.
20 *
21 * @since 10.0
22 */
23 class JFile
24 {
25 /**
26 * Makes file name safe to use.
27 *
28 * @param string $file The name of the file (not full path).
29 *
30 * @return string The sanitised string.
31 */
32 public static function makeSafe($file)
33 {
34 // remove any trailing dots, as those aren't ever valid file names.
35 $file = rtrim($file, '.');
36
37 $regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
38
39 return trim(preg_replace($regex, '', $file));
40 }
41
42 /**
43 * Moves an uploaded file to a destination folder.
44 *
45 * @param string $src The name of the php (temporary) uploaded file.
46 * @param string $dest The path (including filename) to move the uploaded file to.
47 *
48 * @return boolean True on success.
49 */
50 public static function upload($src, $dest)
51 {
52 $ret = false;
53
54 // ensure that the paths are valid and clean
55 $src = JPath::clean($src);
56 $dest = JPath::clean($dest);
57
58 // create the destination directory if it does not exist
59 $baseDir = dirname($dest);
60
61 if (!file_exists($baseDir))
62 {
63 JFolder::create($baseDir);
64 }
65
66 if (is_writeable($baseDir) && move_uploaded_file($src, $dest))
67 {
68 // short circuit to prevent file permission errors
69 if (JPath::setPermissions($dest))
70 {
71 $ret = true;
72 }
73 }
74
75 return $ret;
76 }
77
78 /**
79 * Write contents to a file.
80 *
81 * @param string $file The full file path.
82 * @param string $buffer The buffer to write.
83 *
84 * @return boolean True on success.
85 */
86 public static function write($file, $buffer)
87 {
88 @set_time_limit((int) ini_get('max_execution_time'));
89
90 $file = JPath::clean($file);
91
92 // If the destination directory doesn't exist we need to create it
93 if (!file_exists(dirname($file)))
94 {
95 if (JFolder::create(dirname($file)) == false)
96 {
97 return false;
98 }
99 }
100
101 return is_int(file_put_contents($file, $buffer));
102 }
103
104 /**
105 * Copy a source file to a destination.
106 *
107 * @param string $src The full file path.
108 * @param string $dest The full destination path.
109 *
110 * @return boolean True on success.
111 */
112 public static function copy($src, $dest)
113 {
114 return @copy(JPath::clean($src), JPath::clean($dest));
115 }
116
117 /**
118 * Delete one or multiple files.
119 *
120 * @param mixed $file The file path-name or array of file path-names.
121 *
122 * @return boolean True on success.
123 */
124 public static function delete($file)
125 {
126 if (is_array($file))
127 {
128 $files = $file;
129 }
130 else
131 {
132 $files = array($file);
133 }
134
135 foreach ($files as $file)
136 {
137 $file = JPath::clean($file);
138
139 if (is_file($file))
140 {
141 // Try making the file writable first. If it's read-only, it can't be deleted
142 // on Windows, even if the parent folder is writable
143 @chmod($file, 0777);
144
145 // The file should be removable as long as the owner is www-data
146 if (!@unlink($file))
147 {
148 // impossible to remove the file, stop the process immediatelly
149 return false;
150 }
151 }
152 }
153
154 return true;
155 }
156
157 /**
158 * Wrapper for the standard file_exists function
159 *
160 * @param string $file File path
161 *
162 * @return boolean True if path is a file.
163 *
164 * @since 10.1.23
165 */
166 public static function exists($file)
167 {
168 return is_file(JPath::clean($file));
169 }
170 }
171