1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20:
21: class Horde_Vfs_SqlFile extends Horde_Vfs_File
22: {
23:
24: const FILE = 1;
25:
26:
27: const FOLDER = 2;
28:
29: 30: 31: 32: 33:
34: protected $_db = false;
35:
36: 37: 38: 39: 40:
41: public function __construct($params = array())
42: {
43: throw new Horde_Vfs_Exception('The SqlFile VFS driver needs to be refactored to a real composite driver.');
44: }
45:
46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57:
58: public function write($path, $name, $tmpFile, $autocreate = false)
59: {
60: 61:
62: $data = file_get_contents($tmpFile);
63: return $this->writeData($path, $name, $data, $autocreate);
64: }
65:
66: 67: 68: 69: 70: 71: 72: 73: 74: 75:
76: public function writeData($path, $name, $data, $autocreate = false)
77: {
78: $this->_checkQuotaWrite('string', $data);
79:
80: $fp = @fopen($this->_getNativePath($path, $name), 'w');
81: if (!$fp) {
82: if ($autocreate) {
83: $this->autocreatePath($path);
84: $fp = @fopen($this->_getNativePath($path, $name), 'w');
85: if (!$fp) {
86: throw new Horde_Vfs_Exception('Unable to open VFS file for writing.');
87: }
88: } else {
89: throw new Horde_Vfs_Exception('Unable to open VFS file for writing.');
90: }
91: }
92:
93: if (!@fwrite($fp, $data)) {
94: throw new Horde_Vfs_Exception('Unable to write VFS file data.');
95: }
96:
97: if ($this->_writeSQLData($path, $name, $autocreate) instanceof PEAR_Error) {
98: @unlink($this->_getNativePath($path, $name));
99: throw new Horde_Vfs_Exception('Unable to write VFS file data.');
100: }
101: }
102:
103: 104: 105: 106: 107: 108: 109: 110: 111: 112:
113: public function move($path, $name, $dest, $autocreate = false)
114: {
115: $orig = $this->_getNativePath($path, $name);
116: if (preg_match('|^' . preg_quote($orig) . '/?$|', $dest)) {
117: throw new Horde_Vfs_Exception('Cannot move file(s) - destination is within source.');
118: }
119:
120: if ($autocreate) {
121: $this->autocreatePath($dest);
122: }
123:
124: foreach ($this->listFolder($dest, null, false) as $file) {
125: if ($file['name'] == $name) {
126: throw new Horde_Vfs_Exception('Unable to move VFS file.');
127: }
128: }
129:
130: if (strpos($dest, $this->_getSQLNativePath($path, $name)) !== false) {
131: throw new Horde_Vfs_Exception('Unable to move VFS file.');
132: }
133:
134: $this->rename($path, $name, $dest, $name);
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146:
147: public function copy($path, $name, $dest, $autocreate = false)
148: {
149: $orig = $this->_getNativePath($path, $name);
150: if (preg_match('|^' . preg_quote($orig) . '/?$|', $dest)) {
151: throw new Horde_Vfs_Exception('Cannot copy file(s) - source and destination are the same.');
152: }
153:
154: $this->_connect();
155:
156: if ($autocreate) {
157: $this->autocreatePath($dest);
158: }
159:
160: foreach ($this->listFolder($dest, null, false) as $file) {
161: if ($file['name'] == $name) {
162: throw new Horde_Vfs_Exception('Unable to copy VFS file.');
163: }
164: }
165:
166: if (strpos($dest, $this->_getSQLNativePath($path, $name)) !== false) {
167: throw new Horde_Vfs_Exception('Unable to copy VFS file.');
168: }
169:
170: if (is_dir($orig)) {
171: return $this->_recursiveCopy($path, $name, $dest);
172: }
173:
174: $this->_checkQuotaWrite('file', $orig);
175:
176: if (!@copy($orig, $this->_getNativePath($dest, $name))) {
177: throw new Horde_Vfs_Exception('Unable to copy VFS file.');
178: }
179:
180: $id = $this->_db->nextId($this->_params['table']);
181:
182: $query = sprintf('INSERT INTO %s (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner) VALUES (?, ?, ?, ?, ?, ?)',
183: $this->_params['table']);
184: $values = array($id, self::FILE, $dest, $name, time(), $this->_params['user']);
185:
186: $result = $this->_db->query($query, $values);
187:
188: if ($result instanceof PEAR_Error) {
189: unlink($this->_getNativePath($dest, $name));
190: throw new Horde_Vfs_Exception($result->getMessage());
191: }
192: }
193:
194: 195: 196: 197: 198: 199: 200: 201:
202: public function createFolder($path, $name)
203: {
204: $this->_connect();
205:
206: $id = $this->_db->nextId($this->_params['table']);
207: $result = $this->_db->query(sprintf('INSERT INTO %s (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified, vfs_owner)
208: VALUES (?, ?, ?, ?, ?, ?)',
209: $this->_params['table']),
210: array($id, self::FOLDER, $path, $name, time(), $this->_params['user']));
211: if ($result instanceof PEAR_Error) {
212: throw new Horde_Vfs_Exception($result->getMessage());
213: }
214:
215: if (!@mkdir($this->_getNativePath($path, $name))) {
216: $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_id = ?',
217: $this->_params['table']),
218: array($id));
219: throw new Horde_Vfs_Exception('Unable to create VFS directory.');
220: }
221: }
222:
223: 224: 225: 226: 227: 228: 229: 230: 231: 232:
233: public function rename($oldpath, $oldname, $newpath, $newname)
234: {
235: $this->_connect();
236:
237: if (strpos($newpath, '/') === false) {
238: $parent = '';
239: $path = $newpath;
240: } else {
241: list($parent, $path) = explode('/', $newpath, 2);
242: }
243:
244: if (!$this->isFolder($parent, $path)) {
245: $this->autocreatePath($newpath);
246: }
247:
248: $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ?, vfs_modified = ? WHERE vfs_path = ? AND vfs_name = ?', $this->_params['table']), array($newpath, $newname, time(), $oldpath, $oldname));
249:
250: if ($this->_db->affectedRows() == 0) {
251: throw new Horde_Vfs_Exception('Unable to rename VFS file.');
252: }
253:
254: if (is_a($this->_recursiveSQLRename($oldpath, $oldname, $newpath, $newname), 'PEAR_Error')) {
255: $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ? WHERE vfs_path = ? AND vfs_name = ?', $this->_params['table']), array($oldpath, $oldname, $newpath, $newname));
256: throw new Horde_Vfs_Exception('Unable to rename VFS directory.');
257: }
258:
259: if (!@is_dir($this->_getNativePath($newpath))) {
260: $this->autocreatePath($newpath);
261: }
262:
263: if (!@rename($this->_getNativePath($oldpath, $oldname), $this->_getNativePath($newpath, $newname))) {
264: $this->_db->query(sprintf('UPDATE %s SET vfs_path = ?, vfs_name = ? WHERE vfs_path = ? AND vfs_name = ?', $this->_params['table']), array($oldpath, $oldname, $newpath, $newname));
265: return PEAR::raiseError(Horde_Vfs_Translation::t("Unable to rename VFS file."));
266: }
267: }
268:
269: 270: 271: 272: 273: 274: 275: 276: 277:
278: public function deleteFolder($path, $name, $recursive = false)
279: {
280: $this->_connect();
281:
282: if ($recursive) {
283: $this->emptyFolder($path . '/' . $name);
284: } else {
285: $list = $this->listFolder($path . '/' . $name);
286: if (count($list)) {
287: throw new Horde_Vfs_Exception(sprintf('Unable to delete %s, the directory is not empty', $path . '/' . $name));
288: }
289: }
290:
291: $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_path = ? AND vfs_name = ?', $this->_params['table']), array(self::FOLDER, $path, $name));
292:
293: if ($this->_db->affectedRows() == 0 || ($result instanceof PEAR_Error)) {
294: throw new Horde_Vfs_Exception('Unable to delete VFS directory.');
295: }
296:
297: if ($this->_recursiveSQLDelete($path, $name) instanceof PEAR_Error ||
298: $this->_recursiveLFSDelete($path, $name) instanceof PEAR_Error) {
299: throw new Horde_Vfs_Exception('Unable to delete VFS directory recursively.');
300: }
301: }
302:
303: 304: 305: 306: 307: 308: 309: 310:
311: public function deleteFile($path, $name)
312: {
313: $this->_checkQuotaDelete($path, $name);
314: $this->_connect();
315:
316: $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_path = ? AND vfs_name = ?',
317: $this->_params['table']),
318: array(self::FILE, $path, $name));
319:
320: if ($this->_db->affectedRows() == 0) {
321: throw new Horde_Vfs_Exception('Unable to delete VFS file.');
322: }
323:
324: if ($result instanceof PEAR_Error) {
325: throw new Horde_Vfs_Exception($result->getMessage());
326: }
327:
328: if (!@unlink($this->_getNativePath($path, $name))) {
329: throw new Horde_Vfs_Exception('Unable to delete VFS file.');
330: }
331: }
332:
333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344:
345: protected function _listFolder($path, $filter = null, $dotfiles = true,
346: $dironly = false)
347: {
348: $this->_connect();
349:
350: $files = array();
351:
352: $fileList = $this->_db->getAll(sprintf('SELECT vfs_name, vfs_type, vfs_modified, vfs_owner FROM %s
353: WHERE vfs_path = ?',
354: $this->_params['table']),
355: array($path));
356: if ($fileList instanceof PEAR_Error) {
357: throw new Horde_Vfs_Exception($fileList->getMessage());
358: }
359:
360: foreach ($fileList as $line) {
361:
362: if (!$dotfiles && substr($line[0], 0, 1) == '.') {
363: continue;
364: }
365:
366: $file['name'] = $line[0];
367:
368: if ($line[1] == self::FILE) {
369: $name = explode('.', $line[0]);
370:
371: if (count($name) == 1) {
372: $file['type'] = '**none';
373: } else {
374: $file['type'] = Horde_String::lower($name[count($name) - 1]);
375: }
376:
377: $file['size'] = filesize($this->_getNativePath($path, $line[0]));
378: } elseif ($line[1] == self::FOLDER) {
379: $file['type'] = '**dir';
380: $file['size'] = -1;
381: }
382:
383: $file['date'] = $line[2];
384: $file['owner'] = $line[3];
385: $file['perms'] = '';
386: $file['group'] = '';
387:
388:
389: if ($this->_filterMatch($filter, $file['name'])) {
390: unset($file);
391: continue;
392: }
393: if ($dironly && $file['type'] !== '**dir') {
394: unset($file);
395: continue;
396: }
397:
398: $files[$file['name']] = $file;
399: unset($file);
400: }
401:
402: return $files;
403: }
404:
405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416:
417: public function listFolders($path = '', $filter = array(),
418: $dotfolders = true)
419: {
420: $this->_connect();
421:
422: $sql = sprintf('SELECT vfs_name, vfs_path FROM %s WHERE vfs_path = ? AND vfs_type = ?',
423: $this->_params['table']);
424:
425: $folderList = $this->_db->getAll($sql, array($path, self::FOLDER));
426: if ($folderList instanceof PEAR_Error) {
427: throw new Horde_Vfs_Exception($folderList->getMessage());
428: }
429:
430: $folders = array();
431: foreach ($folderList as $line) {
432: $folder['val'] = $this->_getNativePath($line[1], $line[0]);
433: $folder['abbrev'] = '';
434: $folder['label'] = '';
435:
436: $count = substr_count($folder['val'], '/');
437:
438: $x = 0;
439: while ($x < $count) {
440: $folder['abbrev'] .= ' ';
441: $folder['label'] .= ' ';
442: $x++;
443: }
444:
445: $folder['abbrev'] .= $line[0];
446: $folder['label'] .= $line[0];
447:
448: $strlen = Horde_String::length($folder['label']);
449: if ($strlen > 26) {
450: $folder['abbrev'] = substr($folder['label'], 0, ($count * 4));
451: $length = (29 - ($count * 4)) / 2;
452: $folder['abbrev'] .= substr($folder['label'], ($count * 4), $length);
453: $folder['abbrev'] .= '...';
454: $folder['abbrev'] .= substr($folder['label'], -1 * $length, $length);
455: }
456:
457: $found = false;
458: foreach ($filter as $fltr) {
459: if ($folder['val'] == $fltr) {
460: $found = true;
461: }
462: }
463:
464: if (!$found) {
465: $folders[$folder['val']] = $folder;
466: }
467: }
468:
469: ksort($folders);
470: return $folders;
471: }
472:
473: 474: 475: 476: 477: 478: 479: 480: 481:
482: protected function _recursiveCopy($path, $name, $dest)
483: {
484: $this->createFolder($dest, $name);
485:
486: $file_list = $this->listFolder($this->_getSQLNativePath($path, $name));
487: foreach ($file_list as $file) {
488: $this->copy($this->_getSQLNativePath($path, $name), $file['name'], $this->_getSQLNativePath($dest, $name));
489: }
490: }
491:
492: 493: 494: 495: 496: 497: 498: 499: 500:
501: protected function _writeSQLData($path, $name, $autocreate = false)
502: {
503: $this->_connect();
504:
505:
506: if ($this->exists($path, $name)) {
507: $query = 'UPDATE ' . $this->_params['table'] .
508: ' SET vfs_modified = ?' .
509: ' WHERE vfs_path = ? AND vfs_name = ?';
510: $values = array(time(), $path, $name);
511: } else {
512: $id = $this->_db->nextId($this->_params['table']);
513:
514: $query = 'INSERT INTO ' . $this->_params['table'] .
515: ' (vfs_id, vfs_type, vfs_path, vfs_name, vfs_modified,' .
516: ' vfs_owner) VALUES (?, ?, ?, ?, ?, ?)';
517: $values = array($id, self::FILE, $path, $name, time(),
518: $this->_params['user']);
519: }
520: return $this->_db->query($query, $values);
521: }
522:
523: 524: 525: 526: 527: 528: 529: 530: 531: 532:
533: protected function _recursiveSQLRename($oldpath, $oldname, $newpath,
534: $newname)
535: {
536: $folderList = $this->_db->getCol(sprintf('SELECT vfs_name FROM %s WHERE vfs_type = ? AND vfs_path = ?',
537: $this->_params['table']),
538: 0,
539: array(self::FOLDER, $this->_getSQLNativePath($oldpath, $oldname)));
540:
541: foreach ($folderList as $folder) {
542: $this->_recursiveSQLRename($this->_getSQLNativePath($oldpath, $oldname), $folder, $this->_getSQLNativePath($newpath, $newname), $folder);
543: }
544:
545: $result = $this->_db->query(sprintf('UPDATE %s SET vfs_path = ? WHERE vfs_path = ?',
546: $this->_params['table']),
547: array($this->_getSQLNativePath($newpath, $newname),
548: $this->_getSQLNativePath($oldpath, $oldname)));
549:
550: if ($result instanceof PEAR_Error) {
551: throw new Horde_Vfs_Exception($result->getMessage());
552: }
553: }
554:
555: 556: 557: 558: 559: 560: 561: 562: 563:
564: protected function _recursiveSQLDelete($path, $name)
565: {
566: $result = $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_path = ?', $this->_params['table']), array(self::FILE, $this->_getSQLNativePath($path, $name)));
567: if ($result instanceof PEAR_Error) {
568: throw new Horde_Vfs_Exception($result->getMessage());
569: }
570:
571: $folderList = $this->_db->getCol(sprintf('SELECT vfs_name FROM %s WHERE vfs_type = ? AND vfs_path = ?', $this->_params['table']), 0, array(self::FOLDER, $this->_getSQLNativePath($path, $name)));
572:
573: foreach ($folderList as $folder) {
574: $this->_recursiveSQLDelete($this->_getSQLNativePath($path, $name), $folder);
575: }
576:
577: $this->_db->query(sprintf('DELETE FROM %s WHERE vfs_type = ? AND vfs_name = ? AND vfs_path = ?', $this->_params['table']), array(self::FOLDER, $name, $path));
578: }
579:
580: 581: 582: 583: 584: 585: 586: 587:
588: protected function _recursiveLFSDelete($path, $name)
589: {
590: $dir = $this->_getNativePath($path, $name);
591: $dh = @opendir($dir);
592:
593: while (false !== ($file = readdir($dh))) {
594: if ($file != '.' && $file != '..') {
595: if (is_dir($dir . '/' . $file)) {
596: $this->_recursiveLFSDelete(!strlen($path) ? $name : $path . '/' . $name, $file);
597: } else {
598: @unlink($dir . '/' . $file);
599: }
600: }
601: }
602: @closedir($dh);
603:
604: rmdir($dir);
605: }
606:
607: 608: 609: 610: 611:
612: protected function _connect()
613: {
614: if ($this->_db !== false) {
615: return;
616: }
617:
618: $required = array('db', 'vfsroot');
619: foreach ($required as $val) {
620: if (!isset($this->_params[$val])) {
621: throw new Horde_Vfs_Exception(sprintf('Required "%s" not specified in VFS configuration.', $val));
622: }
623: }
624:
625: $this->_params = array_merge(array(
626: 'table' => 'horde_vfs',
627: ), $this->_params);
628:
629: $this->_db = $this->_params['db'];
630: }
631:
632: 633: 634: 635: 636: 637: 638: 639: 640:
641: protected function _getNativePath($path, $name)
642: {
643: if (strlen($name)) {
644: $name = '/' . $name;
645: }
646:
647: if (strlen($path)) {
648: if (isset($this->_params['home']) &&
649: preg_match('|^~/?(.*)$|', $path, $matches)) {
650: $path = $this->_params['home'] . '/' . $matches[1];
651: }
652:
653: return $this->_params['vfsroot'] . '/' . $path . $name;
654: }
655:
656: return $this->_params['vfsroot'] . $name;
657: }
658:
659: 660: 661: 662: 663: 664: 665: 666: 667:
668: protected function _getSQLNativePath($path, $name)
669: {
670: return strlen($path)
671: ? $path . '/' . $name
672: : $name;
673: }
674:
675: }
676: