PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / backup / export / archive.php
vikappointments / site / helpers / libraries / backup / export Last commit date
rule 5 days ago type 5 days ago archive.php 5 days ago director.php 5 days ago rule.php 5 days ago type.php 5 days ago
archive.php
177 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 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 /**
15 * Manager used to handle the backup archive into the file system.
16 *
17 * @since 1.7.1
18 */
19 class VAPBackupExportArchive
20 {
21 /**
22 * The archive path.
23 *
24 * @var string
25 */
26 private $path;
27
28 /**
29 * Class constructor.
30 *
31 * @param $path The relative path in which the archive should be created.
32 */
33 public function __construct($path)
34 {
35 $this->path = $path;
36
37 if (JFolder::exists($this->path))
38 {
39 // clean path if already occupied
40 JFolder::delete($this->path);
41 }
42
43 // create archive folder
44 if (!JFolder::create($this->path))
45 {
46 throw new Exception(sprintf('Unable to create the backup folder: %s', $this->path), 500);
47 }
48 }
49
50 /**
51 * Returns the archive path.
52 *
53 * @return string
54 */
55 public function getPath()
56 {
57 return $this->path;
58 }
59
60 /**
61 * Creates a new empty folder, only if not exists.
62 *
63 * @return boolean
64 */
65 public function addEmptyFolder($folder)
66 {
67 if (!JFolder::exists($folder))
68 {
69 // in case the folder doesn't exist, create it
70 return JFolder::create($folder);
71 }
72
73 // the folder already exists
74 return true;
75 }
76
77 /**
78 * Copies the specified file into the archive.
79 *
80 * @param string $src The source absolute path.
81 * @param string $dest The destination relative path.
82 *
83 * @return boolean True on success, false otherwise.
84 */
85 public function addFile($src, $dest)
86 {
87 // build the destination path
88 $dest = JPath::clean($this->path . '/' . $dest);
89
90 // create directory in which the file should be placed
91 if (!$this->addEmptyFolder(dirname($dest)))
92 {
93 throw new Exception(sprintf('Cannot create folder: %s', $folder), 500);
94 }
95
96 // copy the source file into the archive
97 return JFile::copy($src, $dest);
98 }
99
100 /**
101 * Opens a new file in append mode and writes there the given buffer.
102 *
103 * @param string $buffer The buffer to write.
104 * @param string $dest The destination relative path.
105 *
106 * @return mixed The written bytes on success, false otherwise.
107 */
108 public function addBuffer($buffer, $dest)
109 {
110 // build the destination path
111 $dest = JPath::clean($this->path . '/' . $dest);
112
113 // create directory in which the file should be placed
114 if (!$this->addEmptyFolder(dirname($dest)))
115 {
116 throw new Exception(sprintf('Cannot create folder: %s', $folder), 500);
117 }
118
119 $bytes = 0;
120
121 // open file in append mode
122 $fp = fopen($dest, 'a');
123
124 // divide the whole string in chunks of 2^12 bytes
125 $chunks = str_split($buffer, 4096);
126
127 // copy chunks one by one
128 foreach ($chunks as $chunk)
129 {
130 // attempt to write the buffer into the file
131 $bytes += fwrite($fp, $chunk, strlen($chunk));
132 }
133
134 // close file pointer
135 fclose($fp);
136
137 return $bytes;
138 }
139
140 /**
141 * Compresses the archive.
142 *
143 * @param string $name An optional name to use for the archive. If not specified, it will be
144 * equals to the folder to compress.
145 *
146 * @return string The archive path.
147 */
148 public function compress($name = null)
149 {
150 $destination = $this->path;
151
152 if ($name)
153 {
154 // in case a name is specified, go to the parent folder and
155 // append the file name at the end
156 $destination = dirname($destination) . DIRECTORY_SEPARATOR . $name;
157 }
158
159 if (!preg_match("/\.zip$/i", $destination))
160 {
161 // concatenate the file extension if missing
162 $destination .= '.zip';
163 }
164
165 // try to compress the archive
166 VAPLoader::import('libraries.archive.factory');
167 $status = VAPArchiveFactory::compress($this->path, $destination);
168
169 if (!$status)
170 {
171 throw new Exception(sprintf('Unable to compress the archive: %s', $destination), 500);
172 }
173
174 return $destination;
175 }
176 }
177