File: Page.php

package info (click to toggle)
bamboo 1.2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 580 kB
  • ctags: 1,338
  • sloc: php: 4,061; makefile: 44; sh: 36
file content (376 lines) | stat: -rw-r--r-- 8,804 bytes parent folder | download
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<? 

/****
 * 
 * Page.php
 *
 * An abstract web page, either dynamic or static.
 * Page knows nothing about how it is stored. It might be a file
 * or it might be in a database. 
 *
 ****/

class Page {
	
var $path;          # always absolute, in relation to siteroot.
var $absolutepath;  # real absolute path, including siteroot.
var $dirpath;       # path to the nearest directory, may be self or our parent.
var $ps;            # the page store
var $type;          # the page type. ie: raw, txt, html, mod. 
var $static;        # true or false
var $dir;           # true or false, does this page contain other pages.
var $prop;          # pointer to global Properties object. used to resolved properties.
var $dirty=array(); # array of flags of dirty properties (changed this request)
var $messages=array(); # array of strings for error messages.
var $ok=true;       # true or false, is this file ok? (ie no errors occured).
var $fnf=false;     # file not found: true if we determine this page does not refer to real content

## basic properties ##

var $name;          # page name 
var $title;         # page title
var $content;       # the content of the page
var $mtime;         # modification time of this data
var $locked;        # false if unlocked. 

function Page(&$pagestore, $path=' none ') {
	$this->init($pagestore, $path);
}

function init(&$pagestore, $path) {
	$this->ps   = &$pagestore;
	$this->prop = &$pagestore->prop;

	if ($path == ' none ') {
		$path = $_SERVER['REQUEST_URI'];
	}

	$path = urldecode($path);
	$path = stripslashes($path);
		
	# start with /
	if (!preg_match("|^/|", $path))
		$path = "/$path";

	# remove:
	#  1. query string
	#  2. site root from url
	#  3. multiple '/'
	#  4. index.php 
	#  5. trailing /
	$root = $this->ps->urlRoot;
	$path = preg_replace(
		array("'(\?.*)'", "'^$root'", "'/{2,}'", "'index.php'", "'/$'"),
		array('', '', '/', '', ''),
		$path
	);
	
	$this->path = $path;
	$this->absolutepath = "$root$path";
	$this->ps->init($this); // required
	if ($this->fnf) return;
	
	// get some basic props as references to the
	// real storage for convenience (ie laziness)
	
	$this->name     = &$this->get('name');
	$this->title    = &$this->get('title');
	$this->navtitle = &$this->get('nav-title');
	$this->static   = &$this->get('static');
	$this->dir      = &$this->get('dir');
	$this->mtime    = &$this->get('mtime');
	$this->locked   = &$this->get('locked');
	$this->type     = &$this->get('type');

	# set dirpath to be the closest directory
	if($this->dir)
		$this->dirpath = $this->path;
	else
		$this->dirpath = dirname($this->path);
}

function &get($property, $default=null) {
	if ($property == 'order') {
		$ret = $this->ps->getOrder($this);
		return $ret; 
	}
	elseif ($property=='content') {
		if (!isset($this->content))
			$this->ps->fetch($this);
		return $this->content;
	}
	elseif ($property=='size') {
		return $this->ps->getContentSize($this);
	}
	else {
		$value = &$this->prop->get($property,$this);
		if ($value == null)
			return $default;
		else
			return $value;
	}
}

function set($property,$data) {
	if ($property == 'order') {
		$this->ps->setOrder($this,$data); 
	}
	else {
		$this->dirty[$property] = true;
		if ($property=='name') {
			$data = urlize($data);
			$this->name = $data;
		}
		$this->prop->set($property,$data,$this);
	}
}

function setContent(&$data) {
	$this->dirty['content'] = true;
	$this->content = &$data;
}

/**
 *
 * pagestore is in charge of writing changes to name and content
 * properties is in charge of writing changes to all other properties
 *
 **/
function commit() {
	global $search; //hackish
	
	$pathold = $this->path;
	
	if (!$this->dirty())
		return; // no props changed.
	
	// we must save the properties first, because a name change
	// will force a reloading of the properties.
	
	// if there are other properties besides name and content
	// which are dirty, then commit the changes.
	$tmparray = $this->dirty;
	unset($tmparray['name']); unset($tmparray['content']);
	if (count($tmparray)) {
		$this->set('mtime',time());
	    $err = $this->prop->commit($this);
	}
		
	if ($this->isdirty('name') || $this->isdirty('content')) {
		$ok = $this->ps->commit($this); 
		if ($ok) {
			$this->set('mtime',time());
			$this->prop->commit($this); // update mtime
		}
	}
	
	// update the index
	if ($search != null) {
		$search->indexPage($this);
		if ($pathold != $this->path)
			$search->replacePath($pathold,$this->path);
	}
}

/////////////////////
// dirty functions

function isdirty($prop) {
	return isset($this->dirty[$prop]);
}

function undirty($prop) {
	unset($this->dirty[$prop]);
}

function dirty() {
	return count($this->dirty);
}

function getDirty() {
	return array_keys($this->dirty);
}

//
// gets data from page storage
// our strategy for fetching data is this:
// - on a fetch(), we grab the body and as much data
//   as comes free with this request.
// - later, when resolving properties, we might grab
//   data again if we did not get enough the in the fetch
//   
function fetch() {
	if (!isset($this->content))
		$this->ps->fetch($this);
}

// returns array of languages which have content.
function availableContent() {
	return $this->ps->availableContent($this);
}

function lock() {

}


function unlock() {

}


function &children() {
	return $this->ps->getChildren($this);
}

function &child($name) {
	return $this->ps->getPage($this->path . '/' . $name);
}

// make this more efficient
function childCount() {
	return count($this->ps->getChildren($this));
}

function &parent() {
	return $this->ps->getPage(dirname($this->path));
}

// return the next peer page, if it exists. null otherwise.
function &next() {
	return $this->ps->getNext($this);
}

// return the next peer page, if it exists. null otherwise.
function &prev() {
	return $this->ps->getPrev($this);
}

/*
 * returns an array of attachment info:
 */
 
function attachments() {
	$ps = &$this->ps;
	return $ps->getAttachments($this);
}

function attach($filedata) {
	$ps = &$this->ps;
	$ps->addAttachment($this,$filedata);
}

function removeAttachment($name) {
	$ps = &$this->ps;
	$ps->removeAttachment($this,$name);
}

/*
 * returns true if the user is allowed to preform the
 * action on this page.
 */	
function may($username, $action) {
	if ($action == 'edit')
		$users = $this->get("access-edit");
	else
		$users = $this->get("access-view");

	## CHANGE ME CHANGE ME -- we need some groups

	$users = split(' ', $users);
	foreach($users as $user) {	
		if ($user == $username)
			return true;
		elseif ($user == 'anonymous' || $user == '')
			return true;
		elseif ($user == 'authenticated' && $username != 'anonymous' && $username != '')
			return true;
	} 
	return false;
}


function destroy() {
	global $search; //hackish
	if ($this->path == '/')
		$this->error('You cannot delete the root page');
	elseif ($this->fnf)
		$this->error('That file does not exist');
	else {
		if ($search != null) $search->deindexPage($this);
		$this->ps->destroyPage($this);
	}
}

/*
 * copies this page to the path specified
 */
function duplicate($copypath) {
	$this->ps->duplicatePage($this,$copypath);
}

/*
 * moves this page to the path specified
 */
function move($newpath) {
	global $search;
	
	while($this->ps->exists($newpath)) {
		$newpath .= '-copy';
	}
	if ($search) {
		$search->replacePath($this->path,$newpath);
	}
	$this->ps->movePage($this,$newpath);	
}

/*
 * moves this page to be under the page
 * specified by $parentpath
 */
function setParent($parentpath) {
	global $search;
	$oldpath = $this->path;
	$newpath = "$parentpath/".basename($this->path);
	$ok = $this->ps->movePage($this,$newpath);
	if ($ok && $search!=null)
		$search->replacePath($oldpath,$newpath);
	if (!$ok) $this->ok = false;
}

// error and message handling

function error($str = '') {
	if ($str) {
		$this->messages[] = "<font color=red>$str</font>";
		$this->ok = false;
	}
	return join('<br>',$this->messages) . '<br>';
}

function msg($str = '') {
	if ($str)
		$this->messages[] = "<font color=green>$str</font>";
	return join('<br>',$this->messages) . '<br>';
}

// search

function search($tokens) {
	$content = $this->get('content');
	$ret = array();
	for($i=0;$i<count($tokens);$i++) {
		$token = "/^.*$tokens[$i].*$/im";
		preg_match_all($token,$content,$matches);
		$ret = array_merge($ret, $matches[0]);
	}
	$ret = array_slice($ret,0,5); // limit to 5 lines;
	$ret = array_unique($ret);
	return $ret;
}


}

return;
?>