disable_messages.module 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /* @file
  3. * The disable_messages module file
  4. */
  5. /**
  6. * Implements hook_permission().
  7. */
  8. function disable_messages_permission() {
  9. return array(
  10. 'view status messages' => array(
  11. 'title' => t('View status messages'),
  12. ),
  13. 'view warning messages' => array(
  14. 'title' => t('View warning messages'),
  15. ),
  16. 'view error messages' => array(
  17. 'title' => t('View error messages'),
  18. ),
  19. 'exclude from message filtering' => array(
  20. 'title' => t('Exclude from message filtering'),
  21. ),
  22. );
  23. }
  24. /**
  25. * Implements hook_menu().
  26. */
  27. function disable_messages_menu() {
  28. $items = array();
  29. $items['admin/config/development/disable-messages'] = array(
  30. 'title' => 'Disable messages',
  31. 'description' => 'Configure display of messages to end users.',
  32. 'page callback' => 'drupal_get_form',
  33. 'page arguments' => array('disable_messages_settings_form'),
  34. 'access arguments' => array('administer site configuration'),
  35. 'type' => MENU_NORMAL_ITEM,
  36. );
  37. return $items;
  38. }
  39. /**
  40. * Implements hook_theme().
  41. */
  42. function disable_messages_theme($existing, $type, $theme, $path) {
  43. $items = array();
  44. // Expose a disable_messages_status_messages theme hook that can be
  45. // overridden by the themer to override the message display
  46. $items['disable_messages_status_messages'] = array(
  47. 'variables' => array('messages' => NULL),
  48. );
  49. return $items;
  50. }
  51. /**
  52. * Implements hook_theme_registry_alter().
  53. */
  54. function disable_messages_theme_registry_alter(&$theme_registry) {
  55. $theme_registry['status_messages']['function'] = '_theme_disable_messages_status_messages';
  56. }
  57. /**
  58. * Implementation of theme_status_messages() hook to override the core
  59. * status message output.
  60. */
  61. function _theme_disable_messages_status_messages($variables) {
  62. $display = $variables['display'];
  63. // Retrieve messages
  64. $messages = drupal_get_messages($display);
  65. // Filter messages if filtering is enabled.
  66. if (variable_get('disable_messages_enable', '1')) {
  67. $messages = disable_messages_apply_filters($messages);
  68. }
  69. // Return themed status messages
  70. return theme('disable_messages_status_messages', array('messages' => $messages));
  71. }
  72. /**
  73. * Theme function for theming status messages
  74. */
  75. function theme_disable_messages_status_messages($vars) {
  76. $messages = $vars['messages'];
  77. $output = '';
  78. $status_heading = array(
  79. 'status' => t('Status message'),
  80. 'error' => t('Error message'),
  81. 'warning' => t('Warning message'),
  82. );
  83. foreach ($messages as $type => $arr_messages) {
  84. $output .= "<div class=\"messages $type\">\n";
  85. if (!empty($status_heading[$type])) {
  86. $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
  87. }
  88. if (count($arr_messages) > 1) {
  89. $output .= " <ul>\n";
  90. foreach ($arr_messages as $message) {
  91. $output .= ' <li>' . $message . "</li>\n";
  92. }
  93. $output .= " </ul>\n";
  94. }
  95. else {
  96. $output .= array_shift($arr_messages);
  97. }
  98. $output .= "</div>\n";
  99. }
  100. return $output;
  101. }
  102. /**
  103. * Apply the filters to the messages
  104. */
  105. function disable_messages_apply_filters($messages) {
  106. global $user;
  107. // Cache the messages for debugging.
  108. $cache = $messages;
  109. // Check userid level filtering
  110. $is_user_excluded = in_array(
  111. (string)$user->uid,
  112. explode(
  113. ',',
  114. variable_get('disable_messages_exclude_users', '')
  115. ),
  116. TRUE
  117. );
  118. // Store flags for debug.
  119. $cache['excluded']['uid'] = FALSE;
  120. if ($is_user_excluded) {
  121. $cache['excluded']['uid'] = TRUE;
  122. }
  123. // UID 1 is also not excluded as this might actually be a requirement from the client.
  124. // You can exclude UID 1 specifically via the exclude users option.
  125. $is_user_excluded = $is_user_excluded || ($user->uid != 1 && user_access('exclude from message filtering'));
  126. if ($is_user_excluded && !$cache['excluded']['uid']) {
  127. $cache['excluded']['permission'] = TRUE;
  128. }
  129. // Check page level filtering
  130. $filter_by_page = variable_get('disable_messages_filter_by_page', 0);
  131. if ($filter_by_page > 0) {
  132. $filter_paths = variable_get('disable_messages_page_filter_paths', '');
  133. $path = drupal_get_path_alias($_GET['q']);
  134. // Compare with the internal and path alias (if any).
  135. $page_match = drupal_match_path($path, $filter_paths);
  136. if ($path != $_GET['q']) {
  137. $page_match = $page_match || drupal_match_path($_GET['q'], $filter_paths);
  138. }
  139. // If $filter_by_page is 1 then listed paths are excluded from any filtering
  140. // and if 2 then filtering is applied only on listed paths.
  141. if ($filter_by_page == 1) {
  142. $is_page_excluded = $page_match;
  143. }
  144. else {
  145. $is_page_excluded = !$page_match;
  146. }
  147. }
  148. else {
  149. $is_page_excluded = FALSE;
  150. }
  151. // Store flags for debug.
  152. $cache['excluded']['page'] = $is_page_excluded;
  153. // If userid is excluded from filtering don't do any filtering.
  154. if (!$is_user_excluded && !$is_page_excluded) {
  155. $regexps = variable_get('disable_messages_ignore_regex', array());
  156. foreach ($messages as $type => $arr_messages) {
  157. // Check if the user has been denied access to the specific type of messages.
  158. if (user_access('view ' . $type . ' messages')) {
  159. foreach ($arr_messages as $key => $message) {
  160. foreach ($regexps as $regex) {
  161. if (preg_match($regex, $message)) {
  162. // Keep track of the regular expression that matched the string.
  163. $cache[$type]['regex'][$key] = $regex;
  164. unset($messages[$type][$key]);
  165. break;
  166. }
  167. }
  168. }
  169. if (count($messages[$type]) == 0) {
  170. $cache[$type]['empty'] = TRUE;
  171. unset($messages[$type]);
  172. }
  173. }
  174. else {
  175. // Keep track of the fact that it was a permission issue.
  176. $cache[$type]['permission'] = FALSE;
  177. unset($messages[$type]);
  178. }
  179. }
  180. }
  181. disable_messages_cache_messages($cache);
  182. return $messages;
  183. }
  184. /**
  185. * Cache messages for debug purposes.
  186. */
  187. function disable_messages_cache_messages($messages = NULL) {
  188. static $cache;
  189. if ($messages) {
  190. $cache = $messages;
  191. }
  192. return $cache;
  193. }
  194. /**
  195. * Implements hook_page_alter().
  196. */
  197. function disable_messages_page_alter(&$page) {
  198. if (variable_get('disable_messages_enable_debug', '1')) {
  199. $page['page_bottom']['disable_messages_debug'] = array(
  200. '#type' => 'markup',
  201. '#markup' => '',
  202. '#pre_render' => array('disable_message_pre_render_debug_output'),
  203. );
  204. }
  205. }
  206. /**
  207. * Pre render function to render the debug output into the page footer
  208. * A separate pre-render function is required because the messages
  209. * wouldn't yet be processed by the time page_alter is called.
  210. */
  211. function disable_message_pre_render_debug_output(&$elements) {
  212. $style = '';
  213. if (variable_get('disable_messages_debug_visible_div', '0') == '0') {
  214. $style = 'style="display:none;"';
  215. }
  216. $elements['#children'] = '' .
  217. '<div id="disable_messages-debug-div" ' . $style . '>' .
  218. '<pre>' .
  219. check_plain(var_export(disable_messages_cache_messages(), TRUE)) .
  220. '</pre>' .
  221. '</div>';
  222. return $elements;
  223. }
  224. /**
  225. * Filter messages admin settings form
  226. */
  227. function disable_messages_settings_form() {
  228. $form = array();
  229. $form['disable_messages_enable'] = array(
  230. '#type' => 'checkbox',
  231. '#title' => t('Enable filtering'),
  232. '#default_value' => variable_get('disable_messages_enable', '1'),
  233. '#description' => t('Uncheck this checkbox to disable all message filtering. If you uncheck this, all messages will be shown to all users and no custom filtering rules will be applied.'),
  234. );
  235. $form['disable_messages_ignore_patterns'] = array(
  236. '#type' => 'textarea',
  237. '#title' => t('Messages to be disabled'),
  238. '#description' => t('Enter messages that should not be shown to end users. Regular expressions are supported. You do not have to include the opening and closing forward slashes for the regular expression. The system will automatically add /^ and $/ at the beginning and end of the pattern to ensure that the match is always a full match instead of a partial match. This will help prevent unexpected filtering of messages. So if you want to filter out a specific message ensure that you add the full message including any punctuation and additional HTML if any. Add one per line. See !PCRE documentation for details on regular expressions.', array('!PCRE' => l('PCRE', 'http://us3.php.net/manual/en/book.pcre.php', array('external' => TRUE)))),
  239. '#default_value' => variable_get('disable_messages_ignore_patterns', ''),
  240. );
  241. $form['disable_messages_ignore_case'] = array(
  242. '#type' => 'checkbox',
  243. '#title' => t('Ignore case'),
  244. '#default_value' => variable_get('disable_messages_ignore_case', '1'),
  245. '#description' => t('Check this to ignore case while matching the patterns.'),
  246. );
  247. $form['disable_messages_filter_options'] = array(
  248. '#type' => 'fieldset',
  249. '#title' => t('Page and user level filtering options'),
  250. '#collapsible' => TRUE,
  251. '#collapsed' => TRUE,
  252. );
  253. $form['disable_messages_filter_options']['role_information'] = array(
  254. '#type' => 'item',
  255. '#title' => t('Filering by role'),
  256. '#markup' => t('By default, permission to view all message types are given for all roles. You can change this in !link to limit the roles which can view a given message type.', array('!link' => l('administer permissions', 'admin/people/permissions', array('fragment' => 'module-disable_messages')))),
  257. );
  258. $options = array(
  259. t('Apply filters on all pages.'),
  260. t('Apply filters on every page except the listed pages.'),
  261. t('Apply filters only on the listed pages.')
  262. );
  263. $description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
  264. $form['disable_messages_filter_options']['disable_messages_filter_by_page'] = array(
  265. '#type' => 'radios',
  266. '#title' => t('Apply filters by page'),
  267. '#options' => $options,
  268. '#default_value' => variable_get('disable_messages_filter_by_page', 0),
  269. );
  270. $form['disable_messages_filter_options']['disable_messages_page_filter_paths'] = array(
  271. '#type' => 'textarea',
  272. '#title' => t('Pages'),
  273. '#default_value' => variable_get('disable_messages_page_filter_paths', ''),
  274. '#description' => $description,
  275. );
  276. $form['disable_messages_filter_options']['disable_messages_exclude_users'] = array(
  277. '#type' => 'textfield',
  278. '#title' => t('Users excluded from filtering'),
  279. '#size' => 40,
  280. '#default_value' => variable_get('disable_messages_exclude_users', ''),
  281. '#description' => t('Comma separated list of user ids to be excluded from any filtering. All messages will be shown to all the listed users irrespective of their permissons to view the corresponding type of message.'),
  282. );
  283. $form['disable_messages_debug'] = array(
  284. '#type' => 'fieldset',
  285. '#title' => t('Debug options'),
  286. '#collapsible' => TRUE,
  287. '#collapsed' => TRUE,
  288. );
  289. $form['disable_messages_debug']['disable_messages_enable_debug'] = array(
  290. '#type' => 'checkbox',
  291. '#title' => t('Enable debug mode'),
  292. '#default_value' => variable_get('disable_messages_enable_debug', '0'),
  293. '#description' => t('Check this to enable debug information. The debug information will be shown in an explicitly hidden div sent to the page via $closure. You can use firebug or a similar tool like that to set the visibility of this div or just view source to see the debug information. Safe to use even on production sites.'),
  294. );
  295. $form['disable_messages_debug']['disable_messages_debug_visible_div'] = array(
  296. '#type' => 'checkbox',
  297. '#title' => t('Show debug div as visible'),
  298. '#default_value' => variable_get('disable_messages_debug_visible_div', '0'),
  299. '#description' => t('Frustrated with having to view source everytime? Don\'t worry. Enable this to show the debug messages in a visible div. <strong>Remember to disable this on the production sites if you enable debug there :)</strong>.'),
  300. );
  301. $form['#submit'][] = 'disable_messages_settings_form_submit';
  302. return system_settings_form($form);
  303. }
  304. /**
  305. * Validation function for disable_messages_settings_form.
  306. */
  307. function disable_messages_settings_form_validate($form, &$form_state) {
  308. global $_disable_messages_error, $_disable_messages_error_no;
  309. $patterns = explode("\n", $form_state['values']['disable_messages_ignore_patterns']);
  310. // Override drupal error handler to handle the preg error here
  311. set_error_handler('_disable_messages_error_handler');
  312. foreach ($patterns as $pattern) {
  313. $pattern = preg_replace(array('/^\s*/', '/\s*$/'), '', $pattern);
  314. try {
  315. preg_match('/' . $pattern . '/', "This is a test string");
  316. }
  317. catch (Exception $e) {
  318. }
  319. if ($_disable_messages_error) {
  320. form_set_error('disable_messages_ignore_patterns', t('"@pattern" is not a valid regular expression. Preg error (@error_no) - @error', array('@pattern' => $pattern, '@error_no' => $_disable_messages_error_no, '@error' => $_disable_messages_error)));
  321. restore_error_handler();
  322. return;
  323. }
  324. }
  325. }
  326. /**
  327. * Custom error handler to catch the preg error while validating the regular expressions.
  328. */
  329. function _disable_messages_error_handler($errno, $errstr, $errfile, $errline) {
  330. global $_disable_messages_error, $_disable_messages_error_no;
  331. $_disable_messages_error = $errstr;
  332. $_disable_messages_error_no = $errno;
  333. // Don't do anything other than set the error string.
  334. }
  335. /**
  336. * Submit function for the admin settings form.
  337. */
  338. function disable_messages_settings_form_submit($form, &$form_state) {
  339. // Catch comma typos in the id text box
  340. $value = $form_state['values']['disable_messages_exclude_users'];
  341. $value = preg_replace(array('/[^0-9,]/', '/^,*/', '/,*$/'), '', $value);
  342. $value = preg_replace('/(,+)/', ',', $value);
  343. $form_state['values']['disable_messages_exclude_users'] = $value;
  344. // Process and save the regular expressions in another variable.
  345. $patterns = explode("\n", $form_state['values']['disable_messages_ignore_patterns']);
  346. $regexps = array();
  347. $ignore_case = (variable_get('disable_messages_ignore_case', '0') == '1') ? 'i' : '';
  348. foreach ($patterns as $pattern) {
  349. $pattern = preg_replace(array('/^\s*/', '/\s*$/'), '', $pattern);
  350. $pattern = '/^' . $pattern . '$/' . $ignore_case;
  351. $regexps[] = $pattern;
  352. }
  353. variable_set('disable_messages_ignore_regex', $regexps);
  354. }