apiban.php
4 years ago
apilog.php
2 years ago
apiplugin.php
4 years ago
apiuser.php
2 years ago
apiuseroptions.php
2 years ago
backup.php
4 months ago
caldays.php
1 month ago
calendar.php
1 month ago
city.php
4 years ago
closure.php
1 month ago
configapp.php
4 years ago
configcldays.php
4 years ago
configcron.php
4 years ago
configemp.php
4 years ago
configsmsapi.php
4 years ago
configuration.php
4 months ago
conversion.php
4 years ago
country.php
2 years ago
coupon.php
2 years ago
couponemployee.php
4 years ago
coupongroup.php
2 years ago
couponservice.php
4 years ago
cronjob.php
2 years ago
cronjoblog.php
4 years ago
customer.php
4 months ago
customf.php
2 years ago
customfservice.php
4 years ago
customizer.php
4 years ago
empgroup.php
2 years ago
employee.php
2 years ago
empsettings.php
4 years ago
file.php
4 years ago
findreservation.php
1 month ago
group.php
2 years ago
import.php
4 years ago
index.html
4 years ago
invoice.php
1 month ago
langcustomf.php
4 years ago
langempgroup.php
4 years ago
langemployee.php
4 years ago
langgroup.php
4 years ago
langmedia.php
4 years ago
langoption.php
2 years ago
langoptiongroup.php
4 years ago
langoptionvar.php
4 years ago
langpackage.php
4 years ago
langpackgroup.php
4 years ago
langpayment.php
4 years ago
langservice.php
4 years ago
langstatuscode.php
4 years ago
langsubscr.php
4 years ago
langtax.php
2 years ago
langtaxrule.php
4 years ago
location.php
2 years ago
mailtext.php
2 years ago
makerecurrence.php
1 month ago
media.php
2 years ago
multiorder.php
1 month ago
option.php
1 year ago
optiongroup.php
2 years ago
optionvar.php
1 year ago
orderstatus.php
2 years ago
package.php
2 years ago
packageservice.php
4 years ago
packgroup.php
2 years ago
packorder.php
2 years ago
packorderitem.php
1 month ago
payment.php
2 years ago
rate.php
2 years ago
reportsemp.php
4 months ago
reportsser.php
4 months ago
reservation.php
1 month ago
resoptassoc.php
2 years ago
restriction.php
2 years ago
review.php
3 years ago
serempassoc.php
1 year ago
seroptassoc.php
4 years ago
serrateassoc.php
4 years ago
serrestrassoc.php
4 years ago
service.php
2 years ago
state.php
2 years ago
statswidget.php
2 years ago
statuscode.php
2 years ago
subscription.php
1 month ago
subscrorder.php
1 month ago
tag.php
4 years ago
tax.php
2 years ago
taxrule.php
2 years ago
updateprogram.php
4 years ago
usernote.php
2 years ago
waitinglist.php
1 month ago
webhook.php
1 year ago
worktime.php
1 month ago
file.php
268 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 | VAPLoader::import('libraries.mvc.model'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments file model. |
| 18 | * |
| 19 | * @since 1.7 |
| 20 | */ |
| 21 | class VikAppointmentsModelFile extends JModelVAP |
| 22 | { |
| 23 | /** |
| 24 | * Entirely rewrite save method because the files |
| 25 | * do not use database tables. |
| 26 | * |
| 27 | * @param mixed $data Either an array or an object of data to save. |
| 28 | * |
| 29 | * @return mixed The ID of the record on success, false otherwise. |
| 30 | */ |
| 31 | public function save($data) |
| 32 | { |
| 33 | $dispatcher = VAPFactory::getEventDispatcher(); |
| 34 | |
| 35 | try |
| 36 | { |
| 37 | /** |
| 38 | * Trigger event to allow the plugins to bind the object that |
| 39 | * is going to be saved. |
| 40 | * |
| 41 | * @param mixed &$data The array/object to bind. |
| 42 | * @param JModelVAP $model The model instance. |
| 43 | * |
| 44 | * @return boolean False to abort saving. |
| 45 | * |
| 46 | * @throws Exception It is possible to throw an exception to abort |
| 47 | * the saving process and return a readable message. |
| 48 | * |
| 49 | * @since 1.7 |
| 50 | */ |
| 51 | if ($dispatcher->false('onBeforeSaveFile', array(&$data, $this))) |
| 52 | { |
| 53 | return false; |
| 54 | } |
| 55 | } |
| 56 | catch (Exception $e) |
| 57 | { |
| 58 | // register the error thrown by the plugin and abort |
| 59 | $this->setError($e); |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // attempt to bind the source to the instance |
| 65 | if (!$this->bind($data)) |
| 66 | { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // run any sanity checks on the instance and verify that it is ready for storage |
| 71 | if (!$this->check()) |
| 72 | { |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | // attempt to store the file |
| 77 | if (!$this->store()) |
| 78 | { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Trigger event to allow the plugins to make something after |
| 84 | * saving a file. |
| 85 | * |
| 86 | * @param array $args The saved record. |
| 87 | * @param JModelVAP $model The model instance. |
| 88 | * |
| 89 | * @return void |
| 90 | * |
| 91 | * @since 1.7 |
| 92 | */ |
| 93 | $dispatcher->trigger('onAfterSaveFile', array($this->getData(), $this)); |
| 94 | |
| 95 | return $this->id; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Method to bind an associative array or object to the Table instance. This |
| 100 | * method only binds properties that are publicly accessible and optionally |
| 101 | * takes an array of properties to ignore when binding. |
| 102 | * |
| 103 | * @param array|object $src An associative array or object to bind. |
| 104 | * |
| 105 | * @return boolean True on success. |
| 106 | */ |
| 107 | protected function bind($src) |
| 108 | { |
| 109 | $src = (array) $src; |
| 110 | |
| 111 | // make sure the file path has been specified |
| 112 | if (empty($src['id'])) |
| 113 | { |
| 114 | $this->setError('Missing file path'); |
| 115 | |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | // check if the file was encoded |
| 120 | if (strpos($src['id'], VAPBASE) !== 0 && strpos($src['id'], VAPADMIN) !== 0) |
| 121 | { |
| 122 | // decode file from base64 |
| 123 | $src['id'] = base64_decode($src['id']); |
| 124 | } |
| 125 | |
| 126 | // register file |
| 127 | $this->id = $src['id']; |
| 128 | |
| 129 | // register content to save |
| 130 | $this->content = isset($src['content']) ? $src['content'] : ''; |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Method to perform sanity checks to ensure they are safe to store. |
| 137 | * |
| 138 | * @return boolean True if the instance is sane and able to be stored. |
| 139 | */ |
| 140 | public function check() |
| 141 | { |
| 142 | // DO NOT INVOKE PARENT |
| 143 | |
| 144 | // make sure the file is located within a VikAppointments folder |
| 145 | if (strpos($this->id, VAPBASE) !== 0 && strpos($this->id, VAPADMIN) !== 0) |
| 146 | { |
| 147 | // register error message |
| 148 | $this->setError('Only files within VikAppointments can be created or updated.'); |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Method to create/update a file. |
| 158 | * |
| 159 | * @return boolean True on success. |
| 160 | */ |
| 161 | public function store() |
| 162 | { |
| 163 | // open file resource |
| 164 | $handle = fopen($this->id, 'wb'); |
| 165 | |
| 166 | if (!$handle) |
| 167 | { |
| 168 | $this->setError('Unable to open file resource'); |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // iterate until the number of bytes written is equals to the content length |
| 174 | for ($bytes = 0; $bytes < strlen($this->content); $bytes += $chunk) |
| 175 | { |
| 176 | // write bytes starting from the last seek (0 initially) |
| 177 | // and get the number of written bytes, which will now |
| 178 | // be the new seek to use to catch the remaining substring |
| 179 | $chunk = fwrite($handle, substr($this->content, $bytes)); |
| 180 | |
| 181 | if ($chunk === false) |
| 182 | { |
| 183 | // unable to write file |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // close file resource |
| 189 | fclose($handle); |
| 190 | |
| 191 | // check whether the file exists |
| 192 | return is_file($this->id); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the table properties, useful to retrieve the information |
| 197 | * that have been registered while saving a record. |
| 198 | * |
| 199 | * @return array |
| 200 | */ |
| 201 | public function getData() |
| 202 | { |
| 203 | // return all public class properties |
| 204 | return $this->getProperties(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Method to delete one or more records. |
| 209 | * |
| 210 | * @param mixed $ids Either the record ID or a list of records. |
| 211 | * |
| 212 | * @return boolean True on success. |
| 213 | */ |
| 214 | public function delete($ids = null) |
| 215 | { |
| 216 | /** |
| 217 | * For security reasons, files cannot be deleted here. |
| 218 | */ |
| 219 | |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Basic item loading implementation. |
| 225 | * |
| 226 | * @param mixed $file The file path (plain or base64). |
| 227 | * @param boolean $new For inheritance. |
| 228 | * |
| 229 | * @return mixed The record object on success, null otherwise. |
| 230 | */ |
| 231 | public function getItem($file, $new = false) |
| 232 | { |
| 233 | // make sure the file exists |
| 234 | if (!$file || !is_file($file)) |
| 235 | { |
| 236 | // file not found, try to decode from base64 |
| 237 | if ($file) |
| 238 | { |
| 239 | $file = base64_decode($file); |
| 240 | } |
| 241 | |
| 242 | if (!is_file($file)) |
| 243 | { |
| 244 | // file not found |
| 245 | $this->setError(sprintf('File [%s] not found', $file)); |
| 246 | |
| 247 | return null; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | $data = new stdClass; |
| 252 | $data->id = $file; |
| 253 | $data->content = ''; |
| 254 | |
| 255 | // read file using a buffer |
| 256 | $handle = fopen($file, 'r'); |
| 257 | |
| 258 | while (!feof($handle)) |
| 259 | { |
| 260 | $data->content .= fread($handle, 8192); |
| 261 | } |
| 262 | |
| 263 | fclose($handle); |
| 264 | |
| 265 | return $data; |
| 266 | } |
| 267 | } |
| 268 |