1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: cInclude('includes', 'functions.file.php');
19:
20: 21: 22: 23: 24: 25:
26: class cApiDbfsCollection extends ItemCollection {
27:
28: 29: 30:
31: public function __construct() {
32: global $cfg;
33: parent::__construct($cfg['tab']['dbfs'], 'iddbfs');
34: $this->_setItemClass('cApiDbfs');
35:
36:
37: $this->_setJoinPartner('cApiClientCollection');
38: }
39:
40: 41: 42: 43: 44:
45: public function outputFile($path) {
46: global $cfg, $client, $auth;
47:
48: $path = $this->escape($path);
49: $client = (int) $client;
50: $path = cApiDbfs::stripPath($path);
51: $dir = dirname($path);
52: $file = basename($path);
53:
54: if ($dir == '.') {
55: $dir = '';
56: }
57:
58: $this->select("dirname = '" . $dir . "' AND filename = '" . $file . "' AND idclient = " . $client . " LIMIT 1");
59:
60: if (($item = $this->next()) !== false) {
61: $properties = new cApiPropertyCollection();
62:
63: $protocol = cApiDbfs::PROTOCOL_DBFS;
64:
65: if ($properties->getValue('upload', $protocol . $dir . '/' . $file, 'file', 'protected') == '1') {
66: if ($auth->auth['uid'] == 'nobody') {
67: header('HTTP/1.0 403 Forbidden');
68: return;
69: }
70: }
71: $mimetype = $item->get('mimetype');
72:
73: header('Cache-Control: ');
74: header('Pragma: ');
75: header("Content-Type: $mimetype");
76: header('Etag: ' . md5(mt_rand()));
77:
78:
79:
80: $contentDispositionHeader = true;
81: foreach ($cfg['dbfs']['skip_content_disposition_header_for_mimetypes'] as $mt) {
82: if (strtolower($mt) == strtolower($mimetype)) {
83: $contentDispositionHeader = false;
84: break;
85: }
86: }
87: if ($contentDispositionHeader) {
88: header('Content-Disposition: attachment; filename=' . $file);
89: }
90:
91: echo $item->get('content');
92: }
93: }
94:
95: 96: 97: 98: 99: 100:
101: public function writeFromFile($localfile, $targetfile) {
102: $targetfile = cApiDbfs::stripPath($targetfile);
103: $stat = cFileHandler::info($localfile);
104: $mimetype = $stat['mime'];
105:
106: $this->write($targetfile, cFileHandler::read($localfile), $mimetype);
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function writeToFile($sourcefile, $localfile) {
116: $sourcefile = cApiDbfs::stripPath($sourcefile);
117:
118: cFileHandler::write($localfile, $this->read($sourcefile));
119: }
120:
121: 122: 123: 124: 125: 126: 127:
128: public function write($file, $content = '', $mimetype = '') {
129: $file = cApiDbfs::stripPath($file);
130:
131: if (!$this->fileExists($file)) {
132: $this->create($file, $mimetype);
133: }
134: $this->setContent($file, $content);
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function hasFiles($path) {
145: global $client;
146:
147: $path = cApiDbfs::stripPath($path);
148: $client = (int) $client;
149:
150:
151: $this->select("dirname LIKE '" . $path . "/%' AND idclient = " . $client . " LIMIT 1");
152: if ($this->count() > 0) {
153: return true;
154: }
155:
156: $this->select("dirname LIKE '" . $path . "%' AND idclient = " . $client . " LIMIT 2");
157: if ($this->count() > 1) {
158: return true;
159: } else {
160: return false;
161: }
162: }
163:
164: 165: 166: 167: 168: 169:
170: public function read($file) {
171: return ($this->getContent($file));
172: }
173:
174: 175: 176: 177: 178: 179: 180:
181: public function fileExists($path) {
182: global $client;
183:
184: $path = cApiDbfs::stripPath($path);
185: $dir = dirname($path);
186: $file = basename($path);
187:
188: if ($dir == '.') {
189: $dir = '';
190: }
191:
192: $client = (int) $client;
193:
194: $this->select("dirname = '" . $dir . "' AND filename = '" . $file . "' AND idclient = " . $client . " LIMIT 1");
195: if ($this->next()) {
196: return true;
197: } else {
198: return false;
199: }
200: }
201:
202: 203: 204:
205: public function file_exists($path) {
206: cDeprecated('The method file_exists() is deprecated. Use fileExists() instead.');
207: return $this->fileExists($path);
208: }
209:
210: 211: 212: 213: 214: 215: 216:
217: public function dirExists($path) {
218: global $client;
219:
220: $path = cApiDbfs::stripPath($path);
221:
222: if ($path == '') {
223: return true;
224: }
225:
226: $client = (int) $client;
227:
228: $this->select("dirname = '" . $path . "' AND filename = '.' AND idclient = " . $client . " LIMIT 1");
229: if ($this->next()) {
230: return true;
231: } else {
232: return false;
233: }
234: }
235:
236: 237: 238:
239: public function dir_exists($path) {
240: cDeprecated('The method dir_exists() is deprecated. Use dirExists() instead.');
241: return $this->dirExists($path);
242: }
243:
244: 245: 246: 247: 248:
249: public function parentDir($path) {
250: $path = dirname($path);
251:
252: return $path;
253: }
254:
255: 256: 257:
258: public function parent_dir($path) {
259: cDeprecated('The method parent_dir() is deprecated. Use parentDir() instead.');
260: return $this->parentDir($path);
261: }
262:
263: 264: 265: 266: 267: 268: 269:
270: public function create($path, $mimetype = '', $content = '') {
271: global $client, $auth;
272:
273: $client = (int) $client;
274: $item = false;
275:
276: if (substr($path, 0, 1) == '/') {
277: $path = substr($path, 1);
278: }
279:
280: $dir = dirname($path);
281: $file = basename($path);
282:
283: if ($dir == '.') {
284: $dir = '';
285: }
286:
287: if ($file == '') {
288: return $item;
289: }
290:
291: if ($file != '.') {
292: if ($dir != '') {
293:
294: $this->select("dirname = '" . $dir . "' AND filename = '.' AND idclient = " . $client . " LIMIT 1");
295: if (!$this->next()) {
296: $this->create($dir . '/.');
297: }
298: }
299: } else {
300: $parent = $this->parentDir($dir);
301:
302: if ($parent != '.') {
303: if (!$this->dirExists($parent)) {
304: $this->create($parent . '/.');
305: }
306: }
307: }
308:
309: if ($dir && !$this->dirExists($dir) || $file != '.') {
310: $item = $this->createNewItem();
311: $item->set('idclient', $client);
312: $item->set('dirname', $dir);
313: $item->set('filename', $file);
314: $item->set('size', strlen($content));
315:
316: if ($mimetype != '') {
317: $item->set('mimetype', $mimetype);
318: }
319:
320: $item->set('content', $content);
321: $item->set('created', date('Y-m-d H:i:s'), false);
322: $item->set('author', $auth->auth['uid']);
323: $item->store();
324: }
325:
326: return $item;
327: }
328:
329: 330: 331: 332: 333:
334: public function setContent($path, $content) {
335: global $client;
336:
337: $client = (int) $client;
338: $path = cApiDbfs::stripPath($path);
339: $dirname = dirname($path);
340: $filename = basename($path);
341:
342: if ($dirname == '.') {
343: $dirname = '';
344: }
345:
346: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
347: if (($item = $this->next()) !== false) {
348: $item->set('content', $content);
349: $item->set('size', strlen($content));
350: $item->store();
351: }
352: }
353:
354: 355: 356: 357: 358:
359: public function getSize($path) {
360: global $client;
361:
362: $client = (int) $client;
363: $path = cApiDbfs::stripPath($path);
364: $dirname = dirname($path);
365: $filename = basename($path);
366:
367: if ($dirname == '.') {
368: $dirname = '';
369: }
370:
371: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
372: if (($item = $this->next()) !== false) {
373: return $item->get('size');
374: }
375: }
376:
377: 378: 379: 380: 381:
382: public function getContent($path) {
383: global $client;
384:
385: $client = (int) $client;
386: $dirname = dirname($path);
387: $filename = basename($path);
388:
389: if ($dirname == '.') {
390: $dirname = '';
391: }
392:
393: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
394: if (($item = $this->next()) !== false) {
395: return ($item->get("content"));
396: }
397: }
398:
399: 400: 401: 402:
403: public function remove($path) {
404: global $client;
405:
406: $client = (int) $client;
407: $path = cApiDbfs::stripPath($path);
408: $dirname = dirname($path);
409: $filename = basename($path);
410:
411: if ($dirname == '.') {
412: $dirname = '';
413: }
414:
415: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
416: if (($item = $this->next()) !== false) {
417: $this->delete($item->get('iddbfs'));
418: }
419: }
420:
421: 422: 423: 424: 425: 426: 427: 428:
429: public function checkTimeManagement($sPath, $oProperties) {
430: global $contenido;
431: if ($contenido) {
432: return true;
433: }
434: $sPath = cSecurity::toString($sPath);
435: $bAvailable = true;
436: $iTimeMng = cSecurity::toInteger($oProperties->getValue('upload', $sPath, 'file', 'timemgmt'));
437: if ($iTimeMng == 0) {
438: return true;
439: }
440: $sStartDate = $oProperties->getValue('upload', $sPath, 'file', 'datestart');
441: $sEndDate = $oProperties->getValue('upload', $sPath, 'file', 'dateend');
442:
443: $iNow = time();
444:
445: if ($iNow < $this->dateToTimestamp($sStartDate) || ($iNow > $this->dateToTimestamp($sEndDate) && (int) $this->dateToTimestamp($sEndDate) > 0)) {
446:
447: return false;
448: }
449: return $bAvailable;
450: }
451:
452: 453: 454: 455: 456: 457:
458: public function dateToTimestamp($sDate) {
459: return strtotime($sDate);
460: }
461: }
462:
463: 464: 465: 466: 467: 468:
469: class cApiDbfs extends Item {
470:
471: 472: 473: 474: 475:
476: const PROTOCOL_DBFS = 'dbfs:';
477:
478: 479: 480: 481: 482:
483: public function __construct($mId = false) {
484: global $cfg;
485: parent::__construct($cfg['tab']['dbfs'], 'iddbfs');
486: if ($mId !== false) {
487: $this->loadByPrimaryKey($mId);
488: }
489: }
490:
491: public function store() {
492: global $auth;
493:
494: $this->set('modified', date('Y-m-d H:i:s'), false);
495: $this->set('modifiedby', $auth->auth['uid']);
496:
497: return parent::store();
498: }
499:
500: 501: 502: 503: 504: 505: 506: 507: 508:
509: public function setField($sField, $mValue, $bSafe = true) {
510: if ('content' === $sField) {
511:
512: return parent::setField($sField, $mValue, false);
513: } else {
514: return parent::setField($sField, $mValue, $bSafe);
515: }
516: }
517:
518: 519: 520: 521: 522: 523: 524: 525:
526: public function getField($sField, $bSafe = true) {
527: if ('content' === $sField) {
528:
529: return parent::getField($sField, false);
530: } else {
531: return parent::getField($sField, $bSafe);
532: }
533: }
534:
535: 536: 537: 538: 539: 540:
541: public static function stripPath($path) {
542: $path = self::stripProtocol($path);
543: if (substr($path, 0, 1) == '/') {
544: $path = substr($path, 1);
545: }
546: return $path;
547: }
548:
549: 550: 551: 552: 553: 554:
555: public static function stripProtocol($path) {
556: if (self::isDbfs($path)) {
557: $path = substr($path, strlen(cApiDbfs::PROTOCOL_DBFS));
558: }
559: return $path;
560: }
561:
562: 563: 564: 565: 566: 567:
568: public static function isDbfs($file) {
569: return (substr($file, 0, 5) == self::PROTOCOL_DBFS);
570: }
571: }
572: