summaryrefslogtreecommitdiffstats
path: root/code/pages.inc.php
blob: 7bdd907341d26d4f51ad7738feeee855152b7241 (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
<?PHP
  require_once('code/db.inc.php');
  require_once('code/user.inc.php');
  require_once('code/templates.inc.php');
  
  class Pages {
    function Get($page, $type = null, $extra = null) {
      if(!$this->Exists($page, $type))
        return array('title' => $page,
                     'content' => ErrorMessage('PageNotFound', array('page' => $page)));
      
      if($type) $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE name = ? AND type = ?', array($page, $type));
      else $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE id = ?', $page);
      
      if(!$this->HasAccess($page, $type))
        return array('title' => $res->fields[1],
                     'content' => ErrorMessage('Forbidden', array('page' => $res->fields[1])));
      
      $data = null;
      parse_str($res->fields[4], $data);
      $data = array_map('Unquote', $data);
      if($extra) $data = array_merge($data, $extra);
      $data['_id'] = $res->fields[0];
      $data['_page'] = $res->fields[1];
      $data['_type'] = $res->fields[2];
      
      return $GLOBALS['templates'][$res->fields[3]]->Get($data);
    }
    
    function GetEditor($page, $type = null) {
      if(!$this->Exists($page, $type))
        return array('title' => $page,
                     'content' => ErrorMessage('PageNotFound', array('page' => $page)));
      
      if($type) $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE name = ? AND type = ?', array($page, $type));
      else $res = $GLOBALS['db']->Execute('SELECT id, name, type, template, data FROM pages WHERE id = ?', $page);
      
      if(!$GLOBALS['user']->IsAdmin())
        return array('title' => $res->fields[1],
                     'content' => ErrorMessage('Forbidden', array('page' => $res->fields[1])));
      
      parse_str($res->fields[4], $data = null);
      $data = array_map('Unquote', $data);
      $data['_id'] = $res->fields[0];
      $data['_page'] = $res->fields[1];
      $data['_type'] = $res->fields[2];
      
      return $this->Get($res->fields[3], 'e', array('_data' => $data));
    }
    
    function GetName($id) {
      $res = $GLOBALS['db']->Execute('SELECT name FROM pages WHERE id = ?', $id);
      
      return $res->fields[0];
    }
    
    function GetType($id) {
      $res = $GLOBALS['db']->Execute('SELECT type FROM pages WHERE id = ?', $id);
      
      return $res->fields[0];
    }
    
    function Exists($page, $type = null) {
      if($type) $res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE name = ? AND type = ?', array($page, $type));
      else $res = $GLOBALS['db']->Execute('SELECT id FROM pages WHERE id = ?', $page);
      
      return ($res->RecordCount() > 0);
    }
    
    function HasAccess($page, $type = null) {
      $gid = $GLOBALS['user']->gid;
      if($type) $res = $GLOBALS['db']->Execute('SELECT access FROM pages WHERE name = ? AND type = ?', array($page, $type));
      else $res = $GLOBALS['db']->Execute('SELECT access FROM pages WHERE id = ?', $page);
      
      if(!$res->RecordCount()) return false;
      return ($GLOBALS['user']->IsAdmin() || (ord($res->fields[0][$gid/8]) & (1 << ($gid%8))) != 0);
    }
    
    function Add($name, $template, $type) {
      if($this->Exists($name, $type)) return 0;
      
      $GLOBALS['db']->Execute('INSERT INTO pages (name, template, access, data, type) VALUES (?, ?, 0, "", ?)',
                              array($name, $templates, $type));
      
      return $GLOBALS['db']->Insert_ID();
    }
    
    function Edit($page, $data, $type = null) {
      $string = '';
      
      foreach($data as $key => $val)
        $string .= urlencode($key) . '=' . urlencode($val) . '&';
      
      if($type) $GLOBALS['db']->Execute('UPDATE pages SET data = ? WHERE name = ? AND type = ?',
                                        array(substr($string, 0, -1), $page, $type));
      else $GLOBALS['db']->Execute('UPDATE pages SET data = ? WHERE id = ?',
                                   array(substr($string, 0, -1), $page));
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Rename($page, $new_name, $type = null) {
      if($type) {
      	if($this->Exists($new_name, $type)) return;
      
        $GLOBALS['db']->Execute('UPDATE pages SET name = ? WHERE name = ? AND type = ?', array($new_name, $page, $type));
      }
      else {
      	if($this->Exists($new_name, $this->GetType($type))) return;
        
        $GLOBALS['db']->Execute('UPDATE pages SET name = ? WHERE id = ?', array($new_name, $page));
      }
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
    
    function Copy($page, $new_name, $type = null) {
      if($type) {
        if($this->Exists($new_name, $type)) return 0;
      	
        $res = $GLOBALS['db']->Execute('SELECT * FROM pages WHERE name = ? AND type = ?', array($page, $type));
      }
      else {
        if($this->Exists($new_name, $this->GetType($type))) return 0;
        
        $res = $GLOBALS['db']->Execute('SELECT template, data, type FROM pages WHERE id = ?', $page);
      }
      
      $GLOBALS['db']->Execute('INSERT INTO pages (name, template, access, data, type) VALUES (?, ?, 0, ?, ?)',
                              array($new_name, $res->fields[0], $res->fields[1], $res->fields[2]));
      
      return $GLOBALS['db']->Insert_ID();
    }
    
    function Delete($page, $type = null) {
      if($type) $GLOBALS['db']->Execute('DELETE FROM pages WHERE name = ? AND type = ?', array($page, $type));
      else $GLOBALS['db']->Execute('DELETE FROM pages WHERE id = ?', $page);
      
      return ($GLOBALS['db']->Affected_Rows() > 0);
    }
  }
  
  $GLOBALS['pages'] = new Pages;
?>