regex

regex.inc

  1. <?php
  2. // vim:filetype=php expandtab tabstop=2 softtabstop=2 shiftwidth=2 autoindent smartindent
  3. // $Id: nick.inc,v 1.12 2008/11/06 10:46:59 seeschloss Exp $
  4.  
  5. function tribune_regex_info() {
  6.   return t('Allows users to correct their own posts using regular expressions.');
  7. }
  8.  
  9. function tribune_regex_filter(&$post, &$help) {
  10.   global $user;
  11.  
  12.   if (tribune_variable_get('tribune_regex_allow_anonymous', true) or $user->uid) {
  13.     if (preg_match('!^((([01]?[0-9])|(2[0-3])):([0-5][0-9])(:([0-5][0-9]))?([:\^][0-9]|¹|²|³)?) s(.)(.*[^\\\])\9(.*[^\\\])\9([ig]?)$!', $post['message'], $matches)) {
  14.       $clock = $matches[1];
  15.  
  16.       $posts = tribune_get_posts_from_clock($clock);
  17.       if (count($posts)) {
  18.         $separator = $matches[9];
  19.         $from      = str_replace('\\'.$separator, $separator, $matches[10]);
  20.         $to        = str_replace('\\'.$separator, $separator, $matches[11]);
  21.         $options   = $matches[12];
  22.         $limit     = 1;
  23.  
  24.         if (strpos($options, 'g') !== false) {
  25.           $limit = -1;
  26.           $options = str_replace('g', '', $options);
  27.         }
  28.  
  29.         $from = str_replace('/', '\\/', $from);
  30.  
  31.         $expression = 's/'.$from.'/'.$to.'/'.$options;
  32.  
  33.         foreach ($posts as $id) {
  34.           $ref_post = tribune_get_post($id);
  35.  
  36.           if ($ref_post and (tribune_variable_get('tribune_regex_allow_all_posts', false) or $ref_post['uid'] == $user->uid)) {
  37.             $new_message = preg_replace('/'.$from.'/'.$options, $to, $ref_post['message'], $limit, $count);
  38.  
  39.             if ($count and $new_message) {
  40.               $ref_post['message'] = $new_message;
  41.  
  42.               if (tribune_variable_get('tribune_regex_in_place', false)) {
  43.                 $ref_post['parsed']  = print_message(tribune_slip($new_message));
  44.                 tribune_update_post($ref_post);
  45.  
  46.                 if (!tribune_variable_get('tribune_regex_show_posts', true)) {
  47.                   $post = null;
  48.                 }
  49.               } else {
  50.                 if (tribune_variable_get('tribune_regex_show_posts', true)) {
  51.                   $post['message'] .= ': '.$ref_post['message'];
  52.                 } else {
  53.                   $post['message'] = $ref_post['message'];
  54.                 }
  55.               }
  56.             }
  57.           }
  58.         }
  59.       }
  60.     }
  61.   }
  62. }
  63.  
  64. function tribune_regex_settings () {
  65.     $form = array();
  66.  
  67.     $form['tribune_regex_show_posts'] = array(
  68.         '#type' => "checkbox",
  69.         '#title' => t("Show modification posts"),
  70.         '#default_value' => tribune_variable_get('tribune_regex_show_posts', true),
  71.         '#description' => t("If this is unchecked, posts which modify another post will not be shown (this means that there will be no way whatsoever to know which posts have been modified)"),
  72.     );
  73.  
  74.     $form['tribune_regex_allow_all_posts'] = array(
  75.         '#type' => "checkbox",
  76.         '#title' => t("Allow everybody to modify all posts"),
  77.         '#default_value' => tribune_variable_get('tribune_regex_allow_all_posts', false),
  78.         '#description' => t("If this is checked, everybody (including anonymous users) will be able to modify any post. Otherwise, only registered users can change their own posts."),
  79.     );
  80.  
  81.     $form['tribune_regex_allow_anonymous'] = array(
  82.         '#type' => "checkbox",
  83.         '#title' => t("Allow corrections by anonymous users"),
  84.         '#default_value' => tribune_variable_get('tribune_regex_allow_anonymous', true),
  85.     );
  86.  
  87.     $form['tribune_regex_in_place'] = array(
  88.         '#type' => "checkbox",
  89.         '#title' => t("Modify posts in place"),
  90.         '#default_value' => tribune_variable_get('tribune_regex_in_place', false),
  91.         '#description' => t("If this is unchecked, instead of modifying posts « in place » the correction will be shown in a new post."),
  92.     );
  93.  
  94.     return tribune_settings_form($form);
  95. }
  96.