/[drupal]/contributions/modules/customerror/customerror.module
ViewVC logotype

Contents of /contributions/modules/customerror/customerror.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.17 - (show annotations) (download) (as text)
Wed May 21 23:29:37 2008 UTC (18 months ago) by kbahey
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--1
Changes since 1.16: +29 -22 lines
File MIME type: text/x-php
#244801 by coltrane, port to 6.x
1 <?php
2
3 //$Id: customerror.module,v 1.16 2007/12/27 20:11:38 kbahey Exp $
4
5 // Copyright 2005 Khalid Baheyeldin http://2bits.com
6
7
8 /**
9 * @file
10 * Enables custom 404 (not found) and 403 (access denied) pages in Drupal
11 * with no need for creating real nodes under taxonomies
12 */
13
14 function _customerror_enum_errors() {
15 // This is where the error codes and their default descriptions are
16 // stored. Add here as necessary.
17 $errors = array(
18 404 => t('requested page not found'),
19 403 => t('access denied')
20 );
21
22 return $errors;
23 }
24
25 function _customerror_fetch_error($error_code) {
26 $errors = _customerror_enum_errors();
27
28 $default_desc = t('unknown error: @error_code', array('@error_code' => $error_code));
29
30 $r = $default_desc;
31
32 foreach ($errors as $code => $desc) {
33 if ($error_code == $code) {
34 $r = $desc;
35 }
36 }
37 return $r;
38 }
39
40 /**
41 * Implementation of hook_help().
42 */
43 function customerror_help($path, $arg) {
44 switch ($path) {
45 case 'admin/modules#description':
46 $output = t('Enables the creation of custom error pages for 404 and 403 errors.');
47 break;
48 case 'admin/settings/customerr':
49 $output = t('Enables the creation of custom error pages for 404 and 403 errors.');
50 break;
51 }
52 return $output;
53 }
54
55 function customerror_admin_settings()
56 {
57 $form = array(
58 'customerror_form_description' => array(
59 '#type' => 'markup',
60 '#value' => t('Enter the error pages that will be seen by your visitors when they get the respective errors. You can enter any HTML text. You can point the users to the FAQ, inform them that you reorganized the site, ask them to report the error, login or register, ...etc.')
61 )
62 );
63
64 $errors = _customerror_enum_errors();
65 foreach($errors as $error_code => $error_desc) {
66 $form['customerror_' . $error_code . '_title'] = array(
67 '#type' => 'textfield',
68 '#title' => t('Title for @error_code', array('@error_code' => $error_code)),
69 '#default_value' => variable_get('customerror_' . $error_code . '_title', $error_desc),
70 '#size' => 70,
71 '#maxlength' => 70,
72 '#description' => t('Title of @error_code error page', array('@error_code' => $error_code . ' error page')),
73 );
74 $form['customerror_' . $error_code] = array(
75 '#type' => 'textarea',
76 '#title' => t('Description for ') . $error_code,
77 '#default_value' => variable_get('customerror_' . $error_code, $error_desc),
78 '#rows' => 10,
79 '#description' => t('This text will be displayed if a @error_code (@error_desc) error occurs.', array('@error_code' => $error_code, '@error_desc' => $error_desc)),
80 );
81 $form['customerror_' . $error_code . '_php'] = array(
82 '#type' => 'checkbox',
83 '#title' => t('Allow PHP code to be executed for @error_code', array( '@error_code' => $error_code)),
84 '#default_value' => variable_get('customerror_' . $error_code . '_php', FALSE),
85 '#description' => t('This allows you to include PHP code (enclosed in &lt;?php ?&gt; tags) for the @error_code (@error_desc) message. Note that this can be dangerous in some situations. Make sure that you are aware of the implications.', array('@error_code' => $error_code, '@error_desc' => $error_desc)),
86 );
87 }
88
89 $form['customerror_redirect'] = array(
90 '#type' => 'textarea',
91 '#title' => t('Redirect list'),
92 '#default_value' => variable_get('customerror_redirect', ''),
93 '#rows' => 10,
94 '#description' => t('These are custom redirect pairs, one per line. Each pair is a key which is a regular expression, and a destination path for it, separated by a space.'),
95 );
96
97 return system_settings_form($form);
98 }
99
100 function customerror_menu() {
101 $items = array();
102
103 $items['admin/settings/customerror'] = array(
104 'title' => 'Custom error',
105 'description' => 'Administer custom error.',
106 'page callback' => 'drupal_get_form',
107 'page arguments' => array('customerror_admin_settings'),
108 'access arguments' => array('administer site configuration'),
109 'type' => MENU_NORMAL_ITEM,
110 );
111 $items['customerror'] = array(
112 'title' => 'customerror',
113 'access callback' => TRUE,
114 'page callback' => 'customerror_page',
115 'type' => MENU_CALLBACK,
116 'weight' => 0 );
117
118 return $items;
119 }
120
121 /**
122 * Implementation of hook_simpletest().
123 */
124 function customerror_simpletest() {
125 return array_keys(file_scan_directory(
126 drupal_get_path('module', 'customerror') . '/tests', '\.test$'));
127 }
128
129 /**
130 * Implementation of hook_page().
131 */
132 function customerror_page() {
133 $error_code = arg(1);
134
135 switch($error_code) {
136 case 403:
137 case 404:
138 customerror_check_redirect();
139 drupal_set_title(variable_get('customerror_'. $error_code .'_title', _customerror_fetch_error($error_code)));
140 $output = theme('customerror', $error_code, variable_get('customerror_' . $error_code, _customerror_fetch_error($error_code)));
141 $output = (variable_get('customerror_' . $error_code . '_php', FALSE)) ? drupal_eval($output) : $output;
142 break;
143 default:
144 drupal_set_title(t('undefined error: ') . $error_code);
145 $output = _customerror_fetch_error($error_code);
146 break;
147 }
148 return $output;
149 }
150
151 /**
152 * Implementation of hook_theme().
153 */
154 function customerror_theme() {
155 return array(
156 'customerror' => array(
157 'arguments' => array('error_code', 'content')
158 ),
159 );
160 }
161
162 /**
163 * Themeable function
164 */
165 function theme_customerror($error_code, $content) {
166 return $content;
167 }
168
169 function customerror_user($op, $edit, $user) {
170 switch($op) {
171 case 'login':
172 // Check if we have a destination saved in the session
173 $destination = $_SESSION['destination'];
174 if ($destination) {
175 // If there is one, then set the REQUEST destination to it
176 $_REQUEST['destination'] = $destination;
177 // And clear the one in the session
178 unset($_SESSION['destination']);
179 // user.module then does a drupal_goto() for us after we return from here
180 }
181 }
182 }
183 function customerror_check_redirect() {
184 $destination = $_REQUEST['destination'];
185 if (isset($destination)) {
186 $list = explode("\n", variable_get('customerror_redirect', ''));
187 if (count($list)) {
188 foreach($list as $item) {
189 list($src, $dst) = explode(' ', $item);
190 if (isset($src) && isset($dst)) {
191 $src = str_replace("/", "\\/", $src);
192 $dst = str_replace("\r", "", $dst);
193 if (preg_match('/'. $src .'/', $destination)) {
194 $_REQUEST['destination'] = $dst;
195 drupal_goto($dst);
196 }
197 }
198 }
199 }
200 }
201 }

  ViewVC Help
Powered by ViewVC 1.1.2