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->file_exists($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 file_exists($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: 206: 207: 208:
209: public function dir_exists($path) {
210: global $client;
211:
212: $path = cApiDbfs::stripPath($path);
213:
214: if ($path == '') {
215: return true;
216: }
217:
218: $client = (int) $client;
219:
220: $this->select("dirname = '" . $path . "' AND filename = '.' AND idclient = " . $client . " LIMIT 1");
221: if ($this->next()) {
222: return true;
223: } else {
224: return false;
225: }
226: }
227:
228: 229: 230: 231: 232:
233: public function parent_dir($path) {
234: $path = dirname($path);
235:
236: return $path;
237: }
238:
239: 240: 241: 242: 243: 244: 245:
246: public function create($path, $mimetype = '', $content = '') {
247: global $client, $auth;
248:
249: $client = (int) $client;
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;
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->parent_dir($dir);
276:
277: if ($parent != '.') {
278: if (!$this->dir_exists($parent)) {
279: $this->create($parent . '/.');
280: }
281: }
282: }
283:
284: if ($dir && !$this->dir_exists($dir) || $file != '.') {
285: $item = parent::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: return ($item);
301: }
302:
303: 304: 305: 306: 307:
308: public function setContent($path, $content) {
309: global $client;
310:
311: $client = (int) $client;
312: $path = cApiDbfs::stripPath($path);
313: $dirname = dirname($path);
314: $filename = basename($path);
315:
316: if ($dirname == '.') {
317: $dirname = '';
318: }
319:
320: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
321: if (($item = $this->next()) !== false) {
322: $item->set('content', $content);
323: $item->set('size', strlen($content));
324: $item->store();
325: }
326: }
327:
328: 329: 330: 331: 332:
333: public function getSize($path) {
334: global $client;
335:
336: $client = (int) $client;
337: $path = cApiDbfs::stripPath($path);
338: $dirname = dirname($path);
339: $filename = basename($path);
340:
341: if ($dirname == '.') {
342: $dirname = '';
343: }
344:
345: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
346: if (($item = $this->next()) !== false) {
347: return $item->get('size');
348: }
349: }
350:
351: 352: 353: 354: 355:
356: public function getContent($path) {
357: global $client;
358:
359: $client = (int) $client;
360: $dirname = dirname($path);
361: $filename = basename($path);
362:
363: if ($dirname == '.') {
364: $dirname = '';
365: }
366:
367: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
368: if (($item = $this->next()) !== false) {
369: return ($item->get("content"));
370: }
371: }
372:
373: 374: 375: 376:
377: public function remove($path) {
378: global $client;
379:
380: $client = (int) $client;
381: $path = cApiDbfs::stripPath($path);
382: $dirname = dirname($path);
383: $filename = basename($path);
384:
385: if ($dirname == '.') {
386: $dirname = '';
387: }
388:
389: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
390: if (($item = $this->next()) !== false) {
391: $this->delete($item->get('iddbfs'));
392: }
393: }
394:
395: 396: 397: 398: 399: 400: 401: 402:
403: public function checkTimeManagement($sPath, $oProperties) {
404: global $contenido;
405: if ($contenido) {
406: return true;
407: }
408: $sPath = cSecurity::toString($sPath);
409: $bAvailable = true;
410: $iTimeMng = cSecurity::toInteger($oProperties->getValue('upload', $sPath, 'file', 'timemgmt'));
411: if ($iTimeMng == 0) {
412: return true;
413: }
414: $sStartDate = $oProperties->getValue('upload', $sPath, 'file', 'datestart');
415: $sEndDate = $oProperties->getValue('upload', $sPath, 'file', 'dateend');
416:
417: $iNow = time();
418:
419: if ($iNow < $this->dateToTimestamp($sStartDate) || ($iNow > $this->dateToTimestamp($sEndDate) && (int) $this->dateToTimestamp($sEndDate) > 0)) {
420:
421: return false;
422: }
423: return $bAvailable;
424: }
425:
426: 427: 428: 429: 430: 431:
432: public function dateToTimestamp($sDate) {
433: return strtotime($sDate);
434: }
435: }
436:
437: 438: 439: 440: 441: 442:
443: class cApiDbfs extends Item {
444:
445: 446: 447: 448: 449:
450: const PROTOCOL_DBFS = 'dbfs:';
451:
452: 453: 454: 455: 456:
457: public function __construct($mId = false) {
458: global $cfg;
459: parent::__construct($cfg['tab']['dbfs'], 'iddbfs');
460: if ($mId !== false) {
461: $this->loadByPrimaryKey($mId);
462: }
463: }
464:
465: public function store() {
466: global $auth;
467:
468: $this->set('modified', date('Y-m-d H:i:s'), false);
469: $this->set('modifiedby', $auth->auth['uid']);
470:
471: return parent::store();
472: }
473:
474: 475: 476: 477: 478: 479:
480: public static function stripPath($path) {
481: $path = self::stripProtocol($path);
482: if (substr($path, 0, 1) == '/') {
483: $path = substr($path, 1);
484: }
485: return $path;
486: }
487:
488: 489: 490: 491: 492: 493:
494: public static function stripProtocol($path) {
495: if (self::isDbfs($path)) {
496: $path = substr($path, strlen(cApiDbfs::PROTOCOL_DBFS));
497: }
498: return $path;
499: }
500:
501: 502: 503: 504: 505: 506:
507: public static function isDbfs($file) {
508: return (substr($file, 0, 5) == self::PROTOCOL_DBFS);
509: }
510: }
511: