summaryrefslogtreecommitdiffstats
path: root/code/pages.inc.php
blob: c696836751c9dabc2530b0c4d7476fe2721a636a (plain)
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
<?PHP
  require_once('code/db.inc.php');
  require_once('code/user.inc.php');
  require_once('code/templates.inc.php');
  require_once('code/xmlparser.inc.php');
  require_once('code/modules.inc.php');
  
  class Pages {
    var $pages = array(
      'Login.c' => null,
      'Logout.c' => null,
      'Pages.c' => null,
      'Pages:Copy.c' => null,
      'Pages:Delete.c' => null,
      'Pages:Edit.c' => null,
      'Pages:Handle.c' => null,
      'Pages:New.c' => null,
      'Pages:Privs.c' => null,
      'Pages:Rename.c' => null,
      'Users.c' => null,
      'Users:Delete.c' => null,
      'Users:Group.c' => null,
      'Users:Handle.c' => null,
      'Users:New.c' => null,
      'Users:Password.c' => null,
      'Users:Rename.c' => null,
      'Login.n' => null,
      'default.e' => null,
      'phpexec.e' => null
    );
    
    function Get($page, $type, $extra = null) {
      if(!$this->Exists($page, $type))
        return array('title' => $page,
                     'content' => ErrorMessage('PageNotFound', array('page' => $page)));
      
      if(!$this->HasReadAccess($page, $type))
        return array('title' => $page,
                     'content' => ErrorMessage('Forbidden', array('page' => $page)));
      
      $pagedata = $this->GetPageData($page, $type);
      
      $data = $pagedata['data'];
      if($extra) $data = array_merge($data, $extra);
      $data['_page'] = $page;
      $data['_type'] = $type;
      
      return $GLOBALS['templates'][$pagedata['template']]->Get($data);
    }
    
    function GetEditor($page, $type) {
      if(!$this->Exists($page, $type))
        return array('title' => $page,
                     'content' => ErrorMessage('PageNotFound', array('page' => $page)));
      
      if(!$this->HasWriteAccess($page, $type))
        return array('title' => $page,
                     'content' => ErrorMessage('Forbidden', array('page' => $page)));
      
      $pagedata = $this->GetPageData($page, $type);
      
      $data = $pagedata['data'];
      $data['_page'] = $page;
      $data['_type'] = $type;
      
      return $this->Get($pagedata['template'], 'e', array('_data' => $data));
    }
    
    function Exists($page, $type) {
      if(array_key_exists($page . '.' . $type, $this->pages))
        return true;
      
      if(array_key_exists($page . '.' . $type, $GLOBALS['modules']->pages))
        return true;
      
      $res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE name = ? AND type = ?', array($page, $type));
      
      return ($res->RecordCount() > 0);
    }
    
    function HasReadAccess($page, $type) {
      if(!$this->Exists($page, $type)) return false;
      if($GLOBALS['user']->IsAdmin()) return true;
      
      $gid = $GLOBALS['user']->gid;
      
      $access = $this->GetAccess($page, $type);
      
      return ((hexdec($access[0][$gid/4]) & (1 << ($gid%4))) != 0);
    }
    
    function HasWriteAccess($page, $type) {
      if(!$this->Exists($page, $type)) return false;
      if($GLOBALS['user']->IsAdmin()) return true;
      
      $gid = $GLOBALS['user']->gid;
      
      $access = $this->GetAccess($page, $type);
      
      return ((hexdec($access[1][$gid/4]) & (1 << ($gid%4))) != 0);
    }
    
    function GetPageData($page, $type) {
      if(!$this->Exists($page, $type)) return null;
      
      if(!$this->pages[$page . '.' . $type]) {
        $res = $GLOBALS['db']->Execute('SELECT template, data FROM pages WHERE name = ? AND type = ?', array($page, $type));
      
        if($res->RecordCount()) {
          parse_str($res->fields[1], $data);
          $data = array_map('Unquote', $data);
          
          $this->pages[$page . '.' . $type] = array('name' => $page, 'type' => $type, 'template' => $res->fields[0],
                                                    'access' => '', 'data' => $data);
        }
        else {
          $filename = $GLOBALS['modules']->pages[$page . '.' . $type];
          if(!$filename) $filename = 'pages/' . strtr($page, array(':' => '/')) . '.' . $type . '.xml';
          
          $xmldata = $GLOBALS['xmlparser']->ParseFile($filename);
          if(!$xmldata) return null;
          
          $info = $GLOBALS['xmlparser']->FindTag($xmldata, 'info');
          if(!$info) return null;
          
          $template = $GLOBALS['xmlparser']->FindTag($info, 'template');
          if(!$template) return null;
          if(count($template['children']) != 1) return;
          if(!is_string($template['children'][0])) return;
          $template = $template['children'][0];
          
          $access = $GLOBALS['xmlparser']->FindTag($info, 'access');
          if(!$access) return null;
          if(count($access['children']) != 1) return;
          if(!is_string($access['children'][0])) return;
          $access = explode(':', $access['children'][0]);
          
          $rawdata = $GLOBALS['xmlparser']->FindTag($xmldata, 'data');
          
          $data = array();
          
          foreach($rawdata['children'] as $field) {
            if(!is_array($field)) continue;
            if(count($field['children']) != 1) continue;
            
            $data[$field['tag']] = $field['children'][0];
          }
          
          $this->pages[$page . '.' . $type] = array('name' => $page, 'type' => $type, 'template' => $template,
                                                    'access' => $access, 'data' => $data);
        }
        
        $res = $GLOBALS['db']->Execute('SELECT readaccess, writeaccess FROM privs WHERE name = ? AND type = ?', array($page, $type));
      
        if($res->RecordCount())
          $this->pages[$page . '.' . $type]['access'] = array($res->fields[0], $res->fields[1]);
      }
      
      return $this->pages[$page . '.' . $type];
    }
    
    function Add($name, $type, $template) {
      if($this->Exists($name, $type)) return false;
      
      $GLOBALS['db']->Execute('INSERT INTO pages (name, template, data, type) VALUES (?, ?, "", ?)',
                              array($name, $template, $type));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Edit($page, $type, $data) {
      $string = '';
      
      foreach($data as $key => $val)
        $string .= urlencode($key) . '=' . urlencode($val) . '&';
      
      $res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE name = ? AND type = ?',
                                     array($page, $type));
      
      if($res->RecordCount()) {
        $GLOBALS['db']->Execute('UPDATE pages SET data = ? WHERE name = ? AND type = ?',
                              array(substr($string, 0, -1), $page, $type));
      
        return true;
      }
      
      $pagedata = $this->GetPageData($page, $type);
      
      $GLOBALS['db']->Execute('INSERT INTO privs (name, type, readaccess, writeaccess) VALUES (?, ?, ?, ?)',
                              array($page, $type, $pagedata['access'][0], $pagedata['access'][1]));
      $GLOBALS['db']->Execute('INSERT INTO pages (name, template, data, type) VALUES (?, ?, ?, ?)',
                              array($page, $pagedata['template'], $string, $type));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Rename($page, $type, $new_name) {
      if($this->Exists($new_name, $type)) return false;
      
      $GLOBALS['db']->Execute('UPDATE privs SET name = ? WHERE name = ? AND type = ?', array($new_name, $page, $type));
      $GLOBALS['db']->Execute('UPDATE pages SET name = ? WHERE name = ? AND type = ?', array($new_name, $page, $type));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Copy($page, $type, $new_name) {
      if($this->Exists($new_name, $type)) return false;
      if(!$this->Exists($page, $type)) return false;
      
      $pagedata = $this->GetPageData($page, $type);
      
      $string = '';
      
      foreach($pagedata['data'] as $key => $val)
        $string .= urlencode($key) . '=' . urlencode($val) . '&';
      
      $GLOBALS['db']->Execute('INSERT INTO pages (name, template, data, type) VALUES (?, ?, ?, ?)',
                              array($new_name, $pagedata['template'], $string, $type));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Delete($page, $type) {
      $GLOBALS['db']->Execute('DELETE FROM privs WHERE name = ? AND type = ?', array($page, $type));
      $GLOBALS['db']->Execute('DELETE FROM pages WHERE name = ? AND type = ?', array($page, $type));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function GetList() {
      $res = $GLOBALS['db']->Execute('SELECT name, type FROM pages');
      
      $pages = array();
      
      while($row = $res->FetchRow())
        $pages[$row[0] . '.' . $row[1]] = null;
      
      $pages = array_keys(array_merge($pages, $this->pages, $GLOBALS['modules']->pages));
      
      sort($pages);
      
      return $pages;
    }
    
    function GetAccess($page, $type) {
      $res = $GLOBALS['db']->Execute('SELECT readaccess, writeaccess FROM privs WHERE name = ? AND type = ?', array($page, $type));
      
      if($res->RecordCount())
        return array($res->fields[0], $res->fields[1]);
      
      $pagedata = $this->GetPageData($page, $type);
      
      return $pagedata['access'];
    }
    
    function SetAccess($page, $type, $access) {
      $res = $GLOBALS['db']->Execute('SELECT id FROM privs WHERE name = ? AND type = ?', array($page, $type));
      
      if($res->RecordCount()) {
        $GLOBALS['db']->Execute('UPDATE privs SET readaccess = ?, writeaccess = ? WHERE name = ? AND type = ?', array($access[0], $access[1], $page, $type));
      
        return true;
      }
      
      $pagedata = $this->GetPageData($page, $type);
      
      $changed = false;
      
      if(strlen($access[0]) > strlen($pagedata['access'][0])) {
        if(!eregi('^' . $pagedata['access'][0] . '0+$', $access[0])) $changed = true;
      }
      elseif(strlen($access[0]) < strlen($pagedata['access'][0])) {
        if(!eregi('^' . $access[0] . '0+$', $pagedata['access'][0])) $changed = true;
      }
      else {
        if(strcasecmp($access[0], $pagedata['access'][0]) != 0) $changed = true;
      }
      
      if(!$changed) {
        if(strlen($access[1]) > strlen($pagedata['access'][1])) {
          if(eregi('^' . $pagedata['access'][1] . '0+$', $access[1])) return true;
        }
        elseif(strlen($access[1]) < strlen($pagedata['access'][1])) {
          if(eregi('^' . $access[1] . '0+$', $pagedata['access'][1])) return true;
        }
        else {
          if(strcasecmp($access[1], $pagedata['access'][1]) == 0) return true;
        }
      }
      
      $GLOBALS['db']->Execute('INSERT INTO privs (name, type, readaccess, writeaccess) VALUES (?, ?, ?, ?)', array($page, $type, $access[0], $access[1]));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
  }
  
  $GLOBALS['pages'] = new Pages;
?>