forked from SvenMichaelKlose/dev-coin-online-shop
/
document.php
197 lines (168 loc) · 6.59 KB
/
document.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
<?
# Document lookup.
#
# Copyright(c) 2000-2001 dev/consulting GmbH
# Copyright(c) 2011 Sven Michael Klose <pixel@copei.de>
#
# Licensed under the MIT, BSD and GPL licenses.
# About this file:
#
# document_proces() analyses the URL and invokes the scanner the first time.
# It is called from index.php.
# Contains tail of path that wasn't analysed. This is used to pass extra arguments.
unset ($path_tail);
# Hold intital document template.
unset ($document_template);
function document_set_template ($template)
{
$GLOBALS['document_template'] = $template;
}
# Converts special characters in a string to others that can be used
# in an URL.
function document_readable_url ($url)
{
$url = ereg_replace ('ä', 'ae', urldecode ($url));
$url = ereg_replace ('ö', 'oe', $url);
$url = ereg_replace ('ü', 'ue', $url);
$url = ereg_replace ('Ä', 'Ae', $url);
$url = ereg_replace ('Ö', 'Oe', $url);
$url = ereg_replace ('Ü', 'Ue', $url);
$url = ereg_replace ('&', '', $url);
$url = ereg_replace ('&', '', $url);
$url = ereg_replace (',', '', $url);
$url = ereg_replace ('\(', '', $url);
$url = ereg_replace ('\)', '', $url);
$url = ereg_replace ('/', '_2f', $url);
$url = ereg_replace (' ', '_', $url);
$url = ereg_replace ('__', '_', $url);
return urlencode ($url);
}
# Converts a path into a table/primary key pair that specifies the
# directory as well as the name of a virtual directory if specified, the
# path elements that couldn't be processed and the last path element
# that was taken into consideration for something.
# Paths are case insensitive.
#
# $table/$id: Directory where to start.
function document_path_to_directory ($path, $table, $id)
{
global $scanner, $vdir_name, $dep, $db;
# Walk through path.
$dirtype = $scanner->dirtypes[$table];
$vdir = false;
reset ($path);
for ($name = next ($path); $name; $name = next ($path)) {
# Break for object link.
if (strtoupper ($name) == 'OBJ')
break;
# Break on virtual directory.
if (isset ($vdir_name[$name])) {
$dirtype = $vdir_name[$name];
$vdir = true;
break;
}
# Check for name in referencing tables.
$desc = $dep->table[$table]; # Array of all directory names.
foreach ($desc as $ctab) {
# Read names into hash.
unset ($names);
# Get all childs of $ctab/$id.
for ($res = dbitree_get_childs ($db, $ctab, $id);
$row = $res && $res->get (), isset ($row['name']);
$names[strtolower (document_readable_url ($row['name']))] = $row);
# If subdirectory with $name exists, save the table name, id
# and directory type.
$rname = strtolower (document_readable_url ($name));
if (isset ($names[$rname]) && $row = $names[$rname]) {
$table = $ctab;
if ($row['id'])
$id = $row['id'];
$dirtype = $scanner->dirtypes[$table];
$name = '';
break;
}
}
if ($name)
$lastname = $name;
}
# Add last directory and followers to tail.
$tail = array ();
if (isset ($lastname))
$tail[] = $lastname;
while (($tmp = next ($path)) != '')
$tail[] = $tmp;
return array ($dirtype, $table, $id, $vdir, $tail, $name);
}
# Set up the document, parse and evaluate it.
function document_process ($root_table, $root_id, $root_template)
{
global $scanner, $dep, $db, $path_tail, $current_index, $current_indices, $default_document, $document_template, $debug, $list_offsets, $url_vars;
list ($dirtype, $table, $id, $vdir, $path_tail, $name) = document_path_to_directory (explode ('/', $_SERVER['PATH_INFO']), $root_table, $root_id);
# Check if the first unprocessed directory of the tail is a known
# object class and use it as the template. If it's not, a default template
# is used.
$template = '';
if (sizeof ($path_tail) > 0)
if ($db->select ('id', 'obj_classes', 'name=\'' . $path_tail[0] . '\''))
$template = $path_tail[0]; # Tail is a legal user template.
if (!$template)
if ($table == $root_table && $id == $root_id && !$vdir)
$template = $root_template; # Show index page.
else
$template = $default_document[$dirtype]; # Use default template.
# Fetch document template for this directory.
# Don't use cms_fetch_object() here because there's no cursor.
$dbobj = new DBOBJ ($db, $template, $dep, $table, $id, true);
# Export public object as file.
# This is done right here to skip all other activities and exit.
if ($name == 'OBJ') {
if (!is_array ($dbobj->active) || !$dbobj->active['id'])
die ("'$template' is not an object class.");
if (!$dbobj->active['is_public'])
panic ('Object is not marked public!');
Header ('Content-type: ' . $dbobj['mime']);
echo $dbobj->active['data']; # No run through scanner.
exit;
}
if (!isset ($dbobj->active['data']))
die ('Kein Dokument für die Eingansseite definiert - stop.');
$document_template = $dbobj->active['data'];
# Get index info.
$tmp = $table;
$rid = $id;
dbitree_get_parent ($db, $tmp, $rid);
if (!$tmp)
die ('No such path.');
# Fetch result into array.
for ($res = dbitree_get_children ($db, $table, $rid);
$tmp = $res->get ();
$set[$tmp['id_last']] = $tmp['id']);
# Sort indices into $current_indices array and find the current one.
for ($i = 1, $last = 0; isset ($set[$last]) && ($rid = $current_indices[$i] = $set[$last]); $last = $rid, $i++)
if ($rid == $id)
$current_index = $i;
# Feed list offsets into URL vars.
if (isset ($list_offsets))
foreach ($list_offsets as $key => $val)
$url_vars["list_offsets[$key]"] = $val;
# Open a new context.
$scanner->push_context (); # XXX is this really required?
cms_create_context ($dirtype, $table, $id);
# Call document handler if the current directory is virtual.
if ($vdir) {
# Explicitly create context for virtual directory.
# Call document handler.
# TODO: Document handlers for any directory type.
$func = 'document_' . strtolower ($dirtype);
$func ();
}
if ($debug)
echo "dirtype: $dirtype - tab: $table - id: $id - default template: $template<br>";
# Invoke scanner and evaluate the page.
# TODO: This could depend on the document template's mime type.
# see also the scanner in lib/scanner.class.
$document_tree = $scanner->scan ($document_template);
$out = $scanner->exec ($document_tree, $table, $id);
eval ("?>$out<?");
}
?>