· 8 years ago · Apr 12, 2018, 02:56 PM
1<?php /* coding: utf-8 -*-
2/**
3 * Virtual Filesystem
4 *
5 * This file includes all functions and classes to handle VFS
6 *
7 * @author Arnar Mar Sig <antab@opex.is>
8 * @version $Id: vfs.php 6 2007-07-05 00:35:22Z antab $
9 * @package core
10*/
11
12/**
13 * Virtual Filesystem directory object.
14 *
15 * This object is like PHP's Dir object and is accessable in the same way,
16 * except it also handles custom information and permissions.
17 * @package core
18 */
19class VFS_dir {
20 /**
21 * Directory path
22 */
23 public $path;
24 /**
25 * Direcotry resource
26 */
27 public $handle;
28 /**
29 * Directory database
30 */
31 private $db;
32
33
34 /**
35 * Object constructor, opens the dir and throws a execption on error
36 * @param string $path Directory path to open
37 */
38 public function __construct($path) {
39 $this->path = $path;
40
41 if (($this->handle = opendir($this->path)) === FALSE) {
42 throw new Execption();
43 }
44 // Open the custom file database.
45 $this->db = new PDO("sqlite:{$this->path}/.vfs.db");
46 $this->db->exec('PRAGMA encoding = "UTF-8";');
47
48 // Check if the file table exists. If it dosen't, create it.
49 $tmp = $this->db->query('PRAGMA table_info(`files`)');
50 $count = $tmp->rowCount();
51 unset($tmp);
52 if ($count == 0) {
53 $this->db->exec('CREATE TABLE `files` (`id` INTEGER PRIMARY KEY, `name` TEXT, `author` INTEGER, `added` INTEGER);');
54 }
55 }
56
57 /**
58 * Just make sure the resource handle is closed when we die.
59 */
60 public function __destruct() {
61 closedir($this->handle);
62 }
63
64 /**
65 * Read the next entry and return an VFS_dir_entry object
66 * @return VFS_dir_entry|false
67 */
68 public function read() {
69 if (($file = readdir($this->handle)) === FALSE) {
70 return FALSE;
71 } else if ($file == '.vfs.db' && ($file = readdir($this->handle)) === FALSE) {
72 return FALSE;
73 }
74 $entry = new VFS_dir_entry();
75 $entry->name = $file;
76 $entry->path = $this->path;
77 if ($this->db) {
78 $db_res = $this->db->query('SELECT owner,group,access FROM files WHERE name=?', file);
79 if (($row = $db_res->fetch(PDO::FETCH_NUM)) !== FALSE) {
80 $entry->owner = $row[0];
81 $entry->group = $row[1];
82 $entry->access = $access;
83 }
84 unset($db_res);
85 }
86 return $entry;
87 }
88
89 /**
90 * rewind to the first entry.
91 */
92 public function rewind() {
93 rewinddir($this->handle);
94 }
95
96 /**
97 * Close the dir resource.
98 */
99 public function close() {
100 closedir($this->handle);
101 }
102}
103
104
105/**
106 * Virtual filesystem directory entry
107 * @package core
108 */
109class VFS_dir_entry {
110 public $name;
111 public $path;
112 public $owner;
113 public $group;
114 public $access;
115
116 /**
117 * Return the entry name when converted to string (we want to be compatable with PHP's Dir).
118 * @return string
119 */
120 public function __toString() {
121 return $name;
122 }
123}
124
125/**
126 * Virtual filesystem
127 * @package core
128 */
129class VFS {
130 /**
131 * Returns a fullpath to a file given, First its checked if a local copy exists, if not the global one is returnd.
132 * @param string $path Path to check
133 * @return string
134 */
135 public function realpath($file) {
136 return (file_exists(SITE_HOME . $file) ? SITE_HOME : CMS_HOME) . $file;
137 }
138
139 /**
140 * Returns a VFS_dir object if a directory is open, FALSE otherwise.
141 * @param string $path Path to open
142 * @return VFS_dir
143 */
144 public function dir($path) {
145 try {
146 $dir = new VFS_dir($path);
147 } catch (Execption $e) {
148 return FALSE;
149 }
150 return $dir;
151 }
152}