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