plugins/SpecialLog.php
changeset 909 94c1ff984286
parent 905 1e40b33f2e3e
child 910 e5de998b934b
equal deleted inserted replaced
908:44302dd20d62 909:94c1ff984286
    37 {
    37 {
    38   global $db, $session, $paths, $template, $plugins; // Common objects
    38   global $db, $session, $paths, $template, $plugins; // Common objects
    39   global $lang;
    39   global $lang;
    40   global $output;
    40   global $output;
    41   
    41   
       
    42   // FIXME: This doesn't currently prohibit viewing of aggregate logs that might include a page for which
       
    43   // 
       
    44   
       
    45   // FIXME: This is a real hack. We're trying to get permissions on a random non-existent article, which
       
    46   // effectively forces calculation to occur based on site-wide permissions.
       
    47   $pid = '';
       
    48   for ( $i = 0; $i < 32; $i++ )
       
    49   {
       
    50     $pid .= chr(mt_rand(32, 126));
       
    51   }
       
    52   $perms = $session->fetch_page_acl($pid, 'Article');
       
    53   $perms_changed = false;
       
    54   
    42   require_once(ENANO_ROOT . '/includes/log.php');
    55   require_once(ENANO_ROOT . '/includes/log.php');
    43   $log = new LogDisplay();
    56   $log = new LogDisplay();
    44   $page = 1;
    57   $page = 1;
    45   $pagesize = 50;
    58   $pagesize = 50;
    46   
    59   $fmt = 'full';
    47   if ( $params = explode('/', $paths->getAllParams()) )
    60   
    48   {
    61   if ( $params = $paths->getAllParams() )
       
    62   {
       
    63     if ( $params === 'AddFilter' && !empty($_POST['type']) && !empty($_POST['value']) )
       
    64     {
       
    65       $type = $_POST['type'];
       
    66       if ( $type == 'within' )
       
    67         $value = strval(intval($_POST['value']['within'])) . $_POST['value']['withinunits'];
       
    68       else
       
    69         $value = $_POST['value'][$type];
       
    70         
       
    71       $value = str_replace('/', '.2f', sanitize_page_id($value));
       
    72         
       
    73       if ( empty($value) || ( $type == 'within' && intval($value) == 0 ) )
       
    74       {
       
    75         $adderror = $lang->get('log_err_addfilter_field_empty');
       
    76       }
       
    77       
       
    78       $append = ( !empty($_POST['existing_filters']) ) ? "{$_POST['existing_filters']}/" : '';
       
    79       $url = makeUrlNS('Special', "Log/{$append}{$type}={$value}");
       
    80       
       
    81       redirect($url, '', '', 0);
       
    82     }
       
    83     $params = explode('/', $params);
    49     foreach ( $params as $param )
    84     foreach ( $params as $param )
    50     {
    85     {
    51       if ( preg_match('/^([a-z]+)=(.+?)$/', $param, $match) )
    86       $param = str_replace('.2f', '/', dirtify_page_id($param));
       
    87       if ( preg_match('/^([a-z!]+)=(.+?)$/', $param, $match) )
    52       {
    88       {
    53         $name =& $match[1];
    89         $name =& $match[1];
    54         $value =& $match[2];
    90         $value =& $match[2];
    55         switch($name)
    91         switch($name)
    56         {
    92         {
    58             $page = intval($value);
    94             $page = intval($value);
    59             break;
    95             break;
    60           case 'size':
    96           case 'size':
    61             $pagesize = intval($value);
    97             $pagesize = intval($value);
    62             break;
    98             break;
       
    99           case 'fmt':
       
   100             switch($value)
       
   101             {
       
   102               case 'barenaked':
       
   103               case 'ajax':
       
   104                 $fmt = 'naked';
       
   105                 $output = new Output_Naked();
       
   106                 break;
       
   107             }
       
   108             break;
       
   109           case 'page':
       
   110             if ( get_class($perms) == 'sessionManager' )
       
   111             {
       
   112               unset($perms);
       
   113               list($pid, $ns) = RenderMan::strToPageID($value);
       
   114               $perms = $session->fetch_page_acl($pid, $ns);
       
   115               if ( !$perms->get_permissions('history_view') )
       
   116               {
       
   117                 die_friendly($lang->get('etc_access_denied_short'), '<p>' . $lang->get('log_err_access_denied') . '</p>');
       
   118               }
       
   119             }
       
   120             // no break here on purpose
    63           default:
   121           default:
    64             $log->add_criterion($name, $value);
   122             try
       
   123             {
       
   124               $log->add_criterion($name, $value);
       
   125             }
       
   126             catch ( Exception $e )
       
   127             {
       
   128             }
    65             break;
   129             break;
    66         }
   130         }
    67       }
   131       }
    68     }
   132     }
    69   }
   133   }
    70 
   134   if ( !$perms->get_permissions('history_view') )
       
   135   {
       
   136     die_friendly($lang->get('etc_access_denied_short'), '<p>' . $lang->get('log_err_access_denied') . '</p>');
       
   137   }
       
   138   
    71   $page--;
   139   $page--;
    72   $rowcount = $log->get_row_count();  
   140   $rowcount = $log->get_row_count();  
    73   $result_url = makeUrlNS('Special', 'Log/' . rtrim(preg_replace('|/?resultpage=(.+?)/?|', '/', $paths->getAllParams()), '/') . '/resultpage=%s', false, true);
   141   $result_url = makeUrlNS('Special', 'Log/' . rtrim(preg_replace('|/?resultpage=([0-9]+)/?|', '/', $paths->getAllParams()), '/') . '/resultpage=%s', false, true);
    74   $paginator = generate_paginator($page, ceil($rowcount / $pagesize), $result_url);
   142   $paginator = generate_paginator($page, ceil($rowcount / $pagesize), $result_url);
    75   
   143   
    76   $dataset = $log->get_data($page * $pagesize, $pagesize);
   144   $dataset = $log->get_data($page * $pagesize, $pagesize);
    77   
   145   
    78   $output->header();
   146   $output->header();
    79   echo $paginator;
   147   
    80   foreach ( $dataset as $row )
   148   // breadcrumbs
    81   {
   149   if ( $fmt != 'naked' )
    82     echo LogDisplay::render_row($row) . '<br />';
   150   {
    83   }
   151     echo '<div class="breadcrumbs" style="font-weight: normal;" id="log-breadcrumbs">';
       
   152     echo speciallog_generate_breadcrumbs($log->get_criteria());
       
   153     echo '</div>';
       
   154   
       
   155   // form
       
   156   ?>
       
   157   
       
   158   <!-- Begin filter add form -->
       
   159   
       
   160   <form action="<?php echo makeUrlNS('Special', 'Log/AddFilter', false, true); ?>" method="post" enctype="multipart/form-data">
       
   161     <?php
       
   162     // serialize parameters
       
   163     $params_pre = rtrim(preg_replace('#/?resultpage=[0-9]+/?#', '/', $paths->getAllParams()), '/');
       
   164     echo '<input type="hidden" name="existing_filters" value="' . htmlspecialchars($params_pre) . '" />';
       
   165     ?>
       
   166     <script type="text/javascript">//<![CDATA[
       
   167       addOnloadHook(function()
       
   168         {
       
   169           load_component('jquery');
       
   170           $('#log_addfilter_select').change(function()
       
   171             {
       
   172               var value = $(this).val();
       
   173               $('.log_addfilter').hide();
       
   174               $('#log_addform_' + value).show();
       
   175             });
       
   176           $('#log_addform_' + $('#log_addfilter_select').val()).show();
       
   177         });
       
   178     // ]]>
       
   179     </script>
       
   180     <?php
       
   181     if ( isset($adderror) )
       
   182     {
       
   183       echo '<div class="error-box">' . $adderror . '</div>';
       
   184     }
       
   185     ?>
       
   186     <div class="tblholder">
       
   187     <table border="0" cellspacing="1" cellpadding="4">
       
   188       <tr>
       
   189         <th colspan="2">
       
   190           <?php echo $lang->get('log_heading_addfilter'); ?>
       
   191         </th>
       
   192       </tr>
       
   193       <tr>
       
   194       <td class="row1" style="width: 50%; text-align: right;">
       
   195           <select name="type" id="log_addfilter_select">
       
   196             <option value="user"><?php echo $lang->get('log_form_filtertype_user'); ?></option>
       
   197             <option value="page"><?php echo $lang->get('log_form_filtertype_page'); ?></option>
       
   198             <option value="within"><?php echo $lang->get('log_form_filtertype_within'); ?></option>
       
   199             <option value="action"><?php echo $lang->get('log_form_filtertype_action'); ?></option>
       
   200           </select>
       
   201         </td>
       
   202         <td class="row1" style="width: 50%; text-align: left;">
       
   203           <div class="log_addfilter" id="log_addform_user">
       
   204             <input type="text" class="autofill username" name="value[user]" size="40" />
       
   205           </div>
       
   206           <div class="log_addfilter" id="log_addform_page">
       
   207             <input type="text" class="autofill page" name="value[page]" size="40" />
       
   208           </div>
       
   209           <div class="log_addfilter" id="log_addform_within">
       
   210             <input type="text" name="value[within]" size="7" />
       
   211             <select name="value[withinunits]">
       
   212               <option value="d"><?php echo $lang->get('etc_unit_days'); ?></option>
       
   213               <option value="w"><?php echo $lang->get('etc_unit_weeks'); ?></option>
       
   214               <option value="m"><?php echo $lang->get('etc_unit_months'); ?></option>
       
   215               <option value="y"><?php echo $lang->get('etc_unit_years'); ?></option>
       
   216             </select>
       
   217           </div>
       
   218           <div class="log_addfilter" id="log_addform_action">
       
   219             <select name="value[action]">
       
   220               <option value="rename"><?php echo $lang->get('log_formaction_rename'); ?></option>
       
   221               <option value="create"><?php echo $lang->get('log_formaction_create'); ?></option>
       
   222               <option value="delete"><?php echo $lang->get('log_formaction_delete'); ?></option>
       
   223               <option value="protect"><?php echo $lang->get('log_action_protect'); ?></option>
       
   224               <option value="edit"><?php echo $lang->get('log_action_edit'); ?></option>
       
   225             </select>
       
   226           </div>
       
   227         </td>
       
   228       </tr>
       
   229       <tr>
       
   230         <th colspan="2" class="subhead">
       
   231           <input type="submit" value="<?php echo $lang->get('log_btn_add_filter'); ?>" />
       
   232         </th>
       
   233       </tr>
       
   234     </table>
       
   235     </div>
       
   236   
       
   237   </form>
       
   238   
       
   239   <!-- End filter add form -->
       
   240   
       
   241   <?php
       
   242   
       
   243   }
       
   244   
       
   245   // start of actual log output area
       
   246   if ( $fmt != 'naked' )
       
   247   {
       
   248     echo '<div id="log-body">';
       
   249   }
       
   250   
       
   251   if ( $rowcount > 0 )
       
   252   {
       
   253     // we have some results, show pagination + result list
       
   254     echo '<h3 style="float: left;">' . $lang->get('log_heading_logdisplay') . '</h3>';
       
   255     
       
   256     echo $paginator;
       
   257     // padding
       
   258     echo '<div style="height: 10px;"></div>';
       
   259     foreach ( $dataset as $row )
       
   260     {
       
   261       echo LogDisplay::render_row($row) . '<br />';
       
   262     }
       
   263     echo $paginator;
       
   264   }
       
   265   else
       
   266   {
       
   267     // no results
       
   268     echo '<h2 class="emptymessage">' . $lang->get('log_msg_no_results') . '</h2>';
       
   269   }
       
   270   
       
   271   if ( $fmt != 'naked' )
       
   272     echo '</div> <!-- div#log-body -->';
       
   273   
    84   $output->footer();
   274   $output->footer();
    85 }
   275 }
    86 
   276 
       
   277 function speciallog_generate_breadcrumbs($criteria)
       
   278 {
       
   279   global $db, $session, $paths, $template, $plugins; // Common objects
       
   280   global $lang;
       
   281   
       
   282   if ( count($criteria) == 0 )
       
   283   {
       
   284     return $lang->get('log_msg_no_filters');
       
   285   }
       
   286   
       
   287   $html = array();
       
   288   foreach ( $criteria as $criterion )
       
   289   {
       
   290     list($type, $value) = $criterion;
       
   291     switch($type)
       
   292     {
       
   293       case 'user':
       
   294         $rank_info = $session->get_user_rank($value);
       
   295         $user_link = '<a href="' . makeUrlNS('User', $value, false, true) . '" style="' . $rank_info['rank_style'] . '" title="' . htmlspecialchars($lang->get($rank_info['rank_title'])) . '">';
       
   296         $user_link .= htmlspecialchars(str_replace('_', ' ', $value)) . '</a>';
       
   297         
       
   298         $crumb = $lang->get('log_breadcrumb_author', array('user' => $user_link));
       
   299         break;
       
   300       case 'page':
       
   301         $crumb = $lang->get('log_breadcrumb_page', array('page' => '<a href="' . makeUrl($value, false, true) . '">' . htmlspecialchars(get_page_title($value)) . '</a>'));
       
   302         break;
       
   303       case 'action':
       
   304         $crumb = $lang->get('log_breadcrumb_action', array('action' => htmlspecialchars($lang->get("log_action_{$value}"))));
       
   305         break;
       
   306       case 'within':
       
   307         $value = intval($value);
       
   308         if ( $value % 31536000 == 0 )
       
   309         {
       
   310           $n = $value / 31536000;
       
   311           $value = "$n " . $lang->get( $n > 1 ? 'etc_unit_years' : 'etc_unit_year' );
       
   312         }
       
   313         else if ( $value % 2592000 == 0 )
       
   314         {
       
   315           $n = $value / 2592000;
       
   316           $value = "$n " . $lang->get( $n > 1 ? 'etc_unit_months' : 'etc_unit_month' );
       
   317         }
       
   318         else if ( $value % 604800 == 0 )
       
   319         {
       
   320           $n = $value / 604800;
       
   321           $value = "$n " . $lang->get( $n > 1 ? 'etc_unit_weeks' : 'etc_unit_week' );
       
   322         }
       
   323         else if ( $value % 86400 == 0 )
       
   324         {
       
   325           $n = $value / 86400;
       
   326           $value = "$n " . $lang->get( $n > 1 ? 'etc_unit_days' : 'etc_unit_day' );
       
   327         }
       
   328         else
       
   329         {
       
   330           $value = "$value " . $lang->get( $value > 1 ? 'etc_unit_seconds' : 'etc_unit_second' );
       
   331         }
       
   332         $crumb = $lang->get('log_breadcrumb_within', array('time' => $value));
       
   333         break;
       
   334     }
       
   335     $html[] = $crumb . ' ' . speciallog_crumb_remove_link($criterion);
       
   336   }
       
   337   return implode(' &raquo; ', $html);
       
   338 }
       
   339 
       
   340 function speciallog_crumb_remove_link($criterion)
       
   341 {
       
   342   global $db, $session, $paths, $template, $plugins; // Common objects
       
   343   global $lang;
       
   344   
       
   345   list($type, $value) = $criterion;
       
   346   
       
   347   $params = explode('/', dirtify_page_id($paths->getAllParams()));
       
   348   foreach ( $params as $i => $param )
       
   349   {
       
   350     if ( $param === "$type=$value" )
       
   351     {
       
   352       unset($params[$i]);
       
   353       break;
       
   354     }
       
   355     else if ( $type === 'within' )
       
   356     {
       
   357       list($ptype, $pvalue) = explode('=', $param);
       
   358       if ( $ptype !== 'within' )
       
   359         continue;
       
   360       
       
   361       $lastchar = substr($pvalue, -1);
       
   362       $amt = intval($pvalue);
       
   363       switch($lastchar)
       
   364       {
       
   365         case 'd':
       
   366           $amt = $amt * 86400;
       
   367           break;
       
   368         case 'w':
       
   369           $amt = $amt * 604800;
       
   370           break;
       
   371         case 'm':
       
   372           $amt = $amt * 2592000;
       
   373           break;
       
   374         case 'y':
       
   375           $amt = $amt * 31536000;
       
   376           break;
       
   377       }
       
   378       if ( $amt === $value )
       
   379       {
       
   380         unset($params[$i]);
       
   381         break;
       
   382       }
       
   383     }
       
   384   }
       
   385   if ( count($params) > 0 )
       
   386   {
       
   387     $params = implode('/', $params);
       
   388     $url = makeUrlNS('Special', "Log/$params", false, true);
       
   389   }
       
   390   else
       
   391   {
       
   392     $url = makeUrlNS('Special', "Log", false, true);
       
   393   }
       
   394   
       
   395   return '<sup><a href="' . $url . '">(x)</a></sup>';
       
   396 }