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 = cSecurity::escapeDB($path, null);
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: $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->file_exists($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 file_exists($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 dir_exists($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: public function parent_dir($path) {
228: $path = dirname($path);
229:
230: return $path;
231: }
232:
233: public function create($path, $mimetype = '', $content = '') {
234: global $client, $auth;
235:
236: $client = (int) $client;
237:
238: if (substr($path, 0, 1) == '/') {
239: $path = substr($path, 1);
240: }
241:
242: $dir = dirname($path);
243: $file = basename($path);
244:
245: if ($dir == '.') {
246: $dir = '';
247: }
248:
249: if ($file == '') {
250: return;
251: }
252:
253: if ($file != '.') {
254: if ($dir != '') {
255:
256: $this->select("dirname = '" . $dir . "' AND filename = '.' AND idclient = " . $client . " LIMIT 1");
257: if (!$this->next()) {
258: $this->create($dir . '/.');
259: }
260: }
261: } else {
262: $parent = $this->parent_dir($dir);
263:
264: if ($parent != '.') {
265: if (!$this->dir_exists($parent)) {
266: $this->create($parent . '/.');
267: }
268: }
269: }
270:
271: if ($dir && !$this->dir_exists($dir) || $file != '.') {
272: $item = parent::createNewItem();
273: $item->set('idclient', $client);
274: $item->set('dirname', $dir);
275: $item->set('filename', $file);
276: $item->set('size', strlen($content));
277:
278: if ($mimetype != '') {
279: $item->set('mimetype', $mimetype);
280: }
281:
282: $item->set('content', $content);
283: $item->set('created', date('Y-m-d H:i:s'), false);
284: $item->set('author', $auth->auth['uid']);
285: $item->store();
286: }
287: return ($item);
288: }
289:
290: public function setContent($path, $content) {
291: global $client;
292:
293: $client = (int) $client;
294: $path = cApiDbfs::stripPath($path);
295: $dirname = dirname($path);
296: $filename = basename($path);
297:
298: if ($dirname == '.') {
299: $dirname = '';
300: }
301:
302: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
303: if (($item = $this->next()) !== false) {
304: $item->set('content', $content);
305: $item->set('size', strlen($content));
306: $item->store();
307: }
308: }
309:
310: public function getSize($path) {
311: global $client;
312:
313: $client = (int) $client;
314: $path = cApiDbfs::stripPath($path);
315: $dirname = dirname($path);
316: $filename = basename($path);
317:
318: if ($dirname == '.') {
319: $dirname = '';
320: }
321:
322: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
323: if (($item = $this->next()) !== false) {
324: return $item->get('size');
325: }
326: }
327:
328: public function getContent($path) {
329: global $client;
330:
331: $client = (int) $client;
332: $dirname = dirname($path);
333: $filename = basename($path);
334:
335: if ($dirname == '.') {
336: $dirname = '';
337: }
338:
339: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
340: if (($item = $this->next()) !== false) {
341: return ($item->get("content"));
342: }
343: }
344:
345: public function remove($path) {
346: global $client;
347:
348: $client = (int) $client;
349: $path = cApiDbfs::stripPath($path);
350: $dirname = dirname($path);
351: $filename = basename($path);
352:
353: if ($dirname == ".") {
354: $dirname = "";
355: }
356:
357: $this->select("dirname = '" . $dirname . "' AND filename = '" . $filename . "' AND idclient = " . $client . " LIMIT 1");
358: if (($item = $this->next()) !== false) {
359: $this->delete($item->get('iddbfs'));
360: }
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: public function checkTimeManagement($sPath, $oProperties) {
371: global $contenido;
372: if ($contenido) {
373: return true;
374: }
375: $sPath = cSecurity::toString($sPath);
376: $bAvailable = true;
377: $iTimeMng = cSecurity::toInteger($oProperties->getValue('upload', $sPath, 'file', 'timemgmt'));
378: if ($iTimeMng == 0) {
379: return true;
380: }
381: $sStartDate = $oProperties->getValue('upload', $sPath, 'file', 'datestart');
382: $sEndDate = $oProperties->getValue('upload', $sPath, 'file', 'dateend');
383:
384: $iNow = time();
385:
386: if ($iNow < $this->dateToTimestamp($sStartDate) || ($iNow > $this->dateToTimestamp($sEndDate) && (int) $this->dateToTimestamp($sEndDate) > 0)) {
387:
388: return false;
389: }
390: return $bAvailable;
391: }
392:
393: 394: 395: 396: 397: 398:
399: public function dateToTimestamp($sDate) {
400: return strtotime($sDate);
401: }
402: }
403:
404: 405: 406: 407: 408: 409:
410: class cApiDbfs extends Item {
411:
412: const PROTOCOL_DBFS = 'dbfs:';
413:
414: 415: 416: 417: 418:
419: public function __construct($mId = false) {
420: global $cfg;
421: parent::__construct($cfg['tab']['dbfs'], 'iddbfs');
422: if ($mId !== false) {
423: $this->loadByPrimaryKey($mId);
424: }
425: }
426:
427: public function store() {
428: global $auth;
429:
430: $this->set('modified', date('Y-m-d H:i:s'), false);
431: $this->set('modifiedby', $auth->auth['uid']);
432:
433: return parent::store();
434: }
435:
436: 437: 438: 439: 440: 441:
442: public static function stripPath($path) {
443: $path = self::stripProtocol($path);
444: if (substr($path, 0, 1) == '/') {
445: $path = substr($path, 1);
446: }
447: return $path;
448: }
449:
450: 451: 452: 453: 454: 455:
456: public static function stripProtocol($path) {
457: if (self::isDbfs($path)) {
458: $path = substr($path, strlen(cApiDbfs::PROTOCOL_DBFS));
459: }
460: return $path;
461: }
462:
463: 464: 465: 466: 467: 468:
469: public static function isDbfs($file) {
470: return (substr($file, 0, 5) == self::PROTOCOL_DBFS);
471: }
472:
473: }
474: