Advertisement
astropos

UserSpice Auditor Suggestion

Jan 23rd, 2016
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.09 KB | None | 0 0
  1. // Simple auditor for UserSpice 3.0.4b
  2.  
  3. Provides auditing for:
  4. UserID of the user making the action
  5. Field for remote IP (not implemented here)
  6. UserID of the OTHER user (eg admin changed title for user 12)
  7. 32char text for the event (eg the relevant LANG entry)
  8. 128char text for the action
  9. int reference to an item (presumably the pivot point of your killer app)
  10.  
  11. Create the table:
  12.  
  13. // note table prefix.
  14.  
  15. CREATE TABLE IF NOT EXISTS `uc_audit` (
  16.   `audit_id` int(11) NOT NULL AUTO_INCREMENT,
  17.   `audit_userid` int(11) NOT NULL,
  18.   `audit_ip` varchar(16) NOT NULL,
  19.   `audit_othus` int(11) NOT NULL,
  20.   `audit_event` varchar(32) NOT NULL,
  21.   `audit_action` varchar(128) NOT NULL,
  22.   `audit_itemid` int(11) NOT NULL,
  23.   `audit_timestamp` int(11) NOT NULL,
  24.   PRIMARY KEY (`audit_id`)
  25. ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  26.  
  27.  
  28. In models/funcs.php (or your user defined funcs)
  29.  
  30. // Audit
  31.  function writeUSAudit($userid,$othus,$event,$action,$itemid=0)
  32.     {
  33.     global $mysqli,$db_table_prefix;
  34.     $time = time();
  35.     $stmt = $mysqli->prepare("INSERT INTO ".$db_table_prefix."audit (
  36.     audit_userid,audit_othus,audit_event,audit_action,audit_itemid,audit_timestamp
  37.     )
  38.     VALUES (
  39.     ?,
  40.     ?,
  41.     ?,
  42.     ?,
  43.     ?,
  44.     ?
  45.     )");
  46.     $stmt->bind_param("iissii", $userid,$othus,$event,$action,$itemid,$time);
  47.     $result = $stmt->execute();
  48.     $stmt->close();
  49.     return $result;
  50.     }
  51.    
  52.    
  53. // Usage eg admin_user.php
  54.    
  55.  if (updateDisplayName($userId, $displayname)){
  56.     $successes[] = lang("ACCOUNT_DISPLAYNAME_UPDATED", array($displayname));
  57.     writeUSAudit($loggedInUser->user_id,$userId,"Changed name ",lang("ACCOUNT_DISPLAYNAME_UPDATED", array($displayname)));
  58.    
  59.    
  60. // Read and display it as required
  61. // In models/funcs.php (or your user defined funcs)
  62.  
  63. //Retrieve information for admin audit
  64. // no error checking for empty results.
  65.  
  66.     function fetchAllAudit($since=0)
  67.         {
  68.         global $mysqli,$db_table_prefix;
  69.        
  70.         $datemod = ($since == 0) ? '' : $since; // not implemented yet - notifications(!)
  71.        
  72.         $stmt = $mysqli->prepare("SELECT
  73.                 id,
  74.                 display_name,
  75.                 audit_id,
  76.                 audit_userid,
  77.                 audit_ip,
  78.                 audit_othus,
  79.                 audit_event,
  80.                 audit_action,
  81.                 audit_itemid,
  82.                 audit_timestamp
  83.                
  84.             FROM ".$db_table_prefix."audit LEFT JOIN ".$db_table_prefix."users ON audit_userid = id ORDER BY audit_id DESC") ;
  85.             $stmt->execute();
  86.             $stmt->bind_result($userid, $displayname,$auditid, $audituserid, $ip, $module, $event, $action, $itemid, $timestamp);
  87.  
  88.             while ($stmt->fetch()){
  89.                 $row[] = array('id' => $userid,'display_name' => $displayname,'audit_id' => $auditid, 'audit_userid' => $audituserid,  'audit_ip' => $ip, 'audit_othus' => $module, 'audit_event' => $event, 'audit_action' => $action, 'audit_itemid' => $itemid, 'audit_timestamp' => $timestamp);
  90.             }
  91.             $stmt->close();
  92.             return ($row);
  93.         }
  94.  
  95.  
  96. <?php
  97. //show audit for admin only
  98. if ($loggedInUser->checkPermission(array(2))){
  99. ?>
  100.       <div id="audtable" class="table-responsive ">
  101.             <table class="table">
  102.               <thead>
  103.                 <tr>
  104.                   <th>Name</th>
  105.                   <th>Event</th>
  106.                   <th>Action</th>
  107.                   <th>Item</th>
  108.                   <th>For</th>
  109.                   <th>Date</th>
  110.                 </tr>
  111.               </thead>
  112.               <tbody>
  113.               <?php
  114.                 $auditData = fetchAllAudit();
  115.                 //Cycle through audit data
  116.                   foreach ($auditData as $v1)
  117.                   {
  118.                   $audate = date("D jS M Y G:i:s", $v1['audit_timestamp']);
  119.                   $adisp_name = ($v1['display_name'] == "") ? "Unknown" : $v1['display_name']; // eg failed login - no userID
  120.                   $adisp_rowc = ($v1['audit_othus'] == '666') ? "alert alert-danger" : ''; // eg failed login - code 666
  121.                     echo '
  122.                     <tr class="'.$adisp_rowc.'">
  123.                     <td><a href="admin_user.php?id='.$v1['id'].'">'.$adisp_name.'</a></td>
  124.                     <td>'.$v1['audit_event'].'</td>
  125.                     <td>'.$v1['audit_action'].'</td>
  126.                     <td>'.$v1['audit_itemid'].'</td>
  127.                     <td>'.$v1['audit_othus'].'</td>
  128.                     <td>'.$audate.'</td>
  129.                     </tr>
  130.                     ';
  131.                     }
  132.                 ?>
  133.               </tbody>
  134.             </table>
  135.           </div>
  136. <?php } ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement