/
PerchPages.class.php
executable file
·197 lines (184 loc) · 4.94 KB
/
PerchPages.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
class PerchPage extends PerchApp
{
public $root = '';
protected $table = 'pages';
protected $pk = 'ID';
protected $singular_classname = 'PerchPagesItem';
private $registered = array();
function __construct() {
parent::__construct();
$this->root = str_replace(PERCH_LOGINPATH, '', PERCH_PATH);
}
public static function fetch()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
public function add($data)
{
if (!$data || !is_array($data)) {
return false;
}
$db = PerchDB::fetch();
return $db->insert($this->table, $data);
}
public function copy_template($template = false, $location = false)
{
if ($template === false || $location === false) {
return false;
}
$file = PERCH_PATH.'/templates/perchpages/' . $template;
$dir = dirname($location);
if (is_readable($file)) {
if (is_writable($dir)) {
return copy($file, $location);
}
}
return false;
}
public function get_list($filter_mode = false, $sort = true)
{
$db = PerchDB::fetch();
$sql = 'SELECT * FROM '.$this->table;
// Extend this later...
// switch ($filter_mode) {
// default:
// # code...
// break;
// }
$results = $db->get_rows($sql);
if ($sort && PerchUtil::count($results) > 0) {
$results = PerchUtil::array_sort($results, 'Location');
}
return $this->return_instances($results);
}
public function get_pages($column = false, $table = false)
{
if ($column === false || $table === false) {
return false;
}
$db = PerchDB::fetch();
$sql = 'SELECT DISTINCT ' . $column .
' FROM ' . PERCH_DB_PREFIX . $table .
' WHERE ' . $column . ' != `*`';
$rows = $db->get_rows($sql);
if (PerchUtil::count($rows) > 0) {
$out = array();
foreach($rows as $row) {
$out[] = $row[$column];
}
return $out;
}
return false;
}
public function get_templates()
{
$a = array();
if (is_dir(PERCH_PATH.'/templates/perchpages')) {
if ($dh = opendir(PERCH_PATH.'/templates/perchpages')) {
while (($file = readdir($dh)) !== false) {
if(substr($file, 0, 1) != '.') {
$extension = PerchUtil::file_extension($file);
if ($extension == 'html' || $extension == 'htm') {
$a[] = array('filename'=>$file, 'path'=>PERCH_PATH.'/templates/perchpages' . $file, 'label'=>$this->template_display_name($file));
}
}
}
closedir($dh);
}
}
return $a;
}
public function get_writableDirs($dir = false, $level = false)
{ // it is beastly long...
if ($dir === false) {
$dir = $this->root;
}
if ($level === false) {
$level = -1; //becuase we will increment it later...
}
$a = array();
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$level++;
while (($file = readdir($dh)) !== false) {
if(substr($file, 0, 1) != '.') {
$filepath = $dir . '/' . $file;
if (is_dir($filepath)) {
if ($filepath !== PERCH_PATH) {
$writeable = false;
if (is_writable($filepath)) {
$writeable = true;
$prefix = '';
for ($i = $level; $i >= 0; $i--) {
$prefix .= '- ';
}
$a[] = array('value' => $filepath,
'label' => $prefix . $file);
}
$contents = $this->get_writableDirs($filepath, $level);
if (!empty($contents) && $writeable) { // I *NEED* a disabled option!
// I could be writable, but not my parent!
// For now, parent must be writeable too. =[
$a[] = array('value' => $contents[0]['value'],
'label' => $contents[0]['label']);
}
}
}
}
}
closedir($dh);
} else {
return false;
}
} else {
return false;
}
return $a;
}
public function list_writableDirs()
{
$dirs = $this->get_writableDirs();
array_unshift($dirs, array(
'value'=>$this->root,
'label'=>'Root')
);
return $dirs;
}
public function refresh_display_name($filename)
{
$filename = substr($filename, strrpos($filename, DIRECTORY_SEPARATOR) + 1);
$filename = substr($filename, 0, strrpos($filename, '.'));
$filename = str_replace('_', ' ', $filename);
$filename = str_replace('-', ' - ', $filename);
$filename = ucwords($filename);
return $filename;
}
public function template_display_name($filename)
{
$filename = substr($filename, 0, strrpos($filename, '.'));
$filename = str_replace('_', ' ', $filename);
$filename = str_replace('-', ' - ', $filename);
$filename = ucwords($filename);
return $filename;
}
public function test_mysql() {
$db = PerchDB::fetch();
$res = $db->get_table_meta($this->table);
if ($res === false) {
$sql = 'CREATE TABLE `' . $this->table . '` (
`ID` int(10) NOT NULL auto_increment,
`Alias` varchar(255) character set utf8 NOT NULL,
`Location` varchar(255) character set utf8 NOT NULL,
`Template` varchar(255) character set utf8 NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1';
$db->execute($sql);
}
}
}
?>