source: extensions/AdditionalPages/additional_page.php @ 9350

Last change on this file since 9350 was 9350, checked in by patdenice, 9 years ago

Extended description now work on page title.
Language restriction is now optionnal.

File size: 3.9 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5function check_random_index_redirect()
6{
7  global $conf;
8
9  if (!empty($conf['ap_random_index_redirect']))
10  {
11    $random_index_redirect = array();
12    foreach ($conf['ap_random_index_redirect'] as $random_url => $random_url_condition)
13    {
14      if (empty($random_url_condition) or eval($random_url_condition))
15      {
16        $random_index_redirect[] = $random_url;
17      }
18    }
19    if (!empty($random_index_redirect))
20    {
21      redirect($random_index_redirect[mt_rand(0, count($random_index_redirect)-1)]);
22    }
23  }
24  return true;
25}
26
27global $template, $user;
28
29$identifier = $page['ap_homepage'] ? $conf['AP']['homepage'] : $tokens[1];
30
31// Retrieve page data
32$query = 'SELECT id, title, lang, content, users, groups, level, permalink, standalone
33FROM ' . ADD_PAGES_TABLE . '
34';
35$query .= is_numeric($identifier) ?
36  'WHERE id = '.$identifier.';' :
37  'WHERE permalink = "'.$identifier.'";';
38
39$row = pwg_db_fetch_assoc(pwg_query($query));
40
41// Page not found
42if (empty($row))
43{
44  if ($page['ap_homepage']) return;
45  page_not_found('Requested page does not exist');
46}
47
48// Redirect with permalink if exist
49if (is_numeric($identifier) and !empty($row['permalink']) and !$page['ap_homepage'])
50{
51  redirect(make_index_url().'/page/' . $row['permalink']);
52}
53
54// Access controls
55if (!is_admin() or (!is_admin() xor $page['ap_homepage']))
56{
57  // authorized language
58  if (!empty($row['lang']) and $row['lang'] != $user['language'])
59  {
60    if ($page['ap_homepage'] and check_random_index_redirect()) return;
61    page_forbidden(l10n('You are not authorized to access the requested page'), make_index_url());
62  }
63
64  // authorized level
65  if ($user['level'] < $row['level'])
66  {
67    if ($page['ap_homepage'] and check_random_index_redirect()) return;
68    page_forbidden(l10n('You are not authorized to access the requested page'), make_index_url());
69  }
70
71  // authorized users
72  if (isset($row['users']))
73  {
74    $authorized_users = explode(',', $row['users']);
75    if (!in_array($user['status'], $authorized_users))
76    {
77      if ($page['ap_homepage'] and check_random_index_redirect()) return;
78      page_forbidden(l10n('You are not authorized to access the requested page'), make_index_url());
79    }
80  }
81
82  // authorized groups
83  if (!empty($row['groups']))
84  {
85    $query = 'SELECT group_id
86FROM ' . USER_GROUP_TABLE . '
87WHERE user_id = ' . $user['id'] . '
88  AND group_id IN (' . $row['groups'] . ')
89;';
90    $groups = array_from_query($query, 'group_id');
91    if (empty($groups))
92    {
93      if ($page['ap_homepage'] and check_random_index_redirect()) return;
94      page_forbidden(l10n('You are not authorized to access the requested page'), make_index_url());
95    }
96  }
97}
98
99// Display standalone page
100if ($row['standalone'] == 'true')
101{
102  echo $row['content'];
103  exit;
104}
105
106// Page initilization
107$page['section'] = 'additional_page';
108
109$page['additional_page'] = array(
110  'id' => $row['id'],
111  'permalink' => @$row['permalink'],
112  'title' => trigger_event('AP_render_title', $row['title']),
113  'content' => trigger_event('AP_render_content', $row['content']),
114);
115
116add_event_handler('loc_end_index', 'ap_set_index');
117
118function ap_set_index()
119{
120  global $template, $page, $conf;
121
122  $template->assign(array(
123    'TITLE' => $page['additional_page']['title'],
124    'PLUGIN_INDEX_CONTENT_BEGIN' => $page['additional_page']['content'],
125    )
126  );
127
128  if ($conf['AP']['show_home'] and !$page['ap_homepage'])
129  {
130    $template->assign('PLUGIN_INDEX_ACTIONS' , '
131      <li><a href="'.make_index_url().'" title="' . l10n('return to homepage') . '">
132        <img src="' . $template->get_themeconf('icon_dir') . '/home.png" class="button" alt="' . l10n('home') . '"/></a>
133      </li>');
134  }
135  if (is_admin())
136  {
137    $template->assign('U_EDIT', PHPWG_ROOT_PATH.'admin.php?page=plugin&amp;section='.AP_DIR.'%2Fadmin%2Fadmin.php&amp;tab=edit_page&amp;edit='.$page['additional_page']['id'].'&amp;redirect=true');
138  }
139  $template->clear_assign(array('U_MODE_POSTED', 'U_MODE_CREATED'));
140}
141
142?>
Note: See TracBrowser for help on using the repository browser.