Changeset 1929


Ignore:
Timestamp:
04/16/10 14:16:44 (5 years ago)
Author:
lowjoel
Message:

Implement the BlackBox? uploader using PDO, removing a dependency on the old mysql library.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/website/scripts/blackbox/upload.php

    r1672 r1929  
    44 
    55ob_start(); 
    6 require('../database.php'); 
     6require('../Database.php'); 
    77 
    88function GetFunctionNameFromStackTrace($line) 
     
    2323    //Check that a similar stack trace hasn't been uploaded. 
    2424    $status = 'exists'; 
     25    $pdo = new Database(); 
    2526    foreach ($stackTrace as $exceptionDepth => $exception) 
    2627    { 
     
    3637 
    3738            $stackFrames .= sprintf('(StackFrameIndex=%d AND Function=\'%s\') OR ', 
    38                 $stackIndex, mysql_real_escape_string(GetFunctionNameFromStackTrace($stackFrame))); 
     39                $stackIndex, $pdo->quote(GetFunctionNameFromStackTrace($stackFrame))); 
    3940        } 
    4041         
     
    4344         
    4445        //Query for the list of exceptions containing the given functions 
    45         $query = mysql_query(sprintf('SELECT DISTINCT(BlackBox_Exceptions.ID) FROM BlackBox_StackFrames 
     46        $statement = $pdo->query(sprintf('SELECT DISTINCT(BlackBox_Exceptions.ID) FROM BlackBox_StackFrames 
    4647            INNER JOIN BlackBox_Exceptions ON BlackBox_StackFrames.ExceptionID=BlackBox_Exceptions.ID 
    47             WHERE (%s) AND ExceptionDepth=%d AND ExceptionType=\'%s\'', 
    48             substr($stackFrames, 0, strlen($stackFrames) - 4), $exceptionDepth, 
    49             mysql_real_escape_string($exception['exception']))); 
    50          
    51         if (mysql_num_rows($query) == 0) 
     48            WHERE (%s) AND ExceptionDepth=? AND ExceptionType=?', 
     49            substr($stackFrames, 0, strlen($stackFrames) - 4))); 
     50        $statement->bindParam(1, $exceptionDepth); 
     51        $statement->bindParam(2, $exception['exception']); 
     52        $statement->execute(); 
     53 
     54        if ($statement->rowCount() == 0) 
    5255            $status = 'new'; 
    5356    } 
     
    6063function Upload($stackTrace, $crashReport) 
    6164{ 
    62     mysql_query('BEGIN TRANSACTION'); 
    63     mysql_query(sprintf('INSERT INTO BlackBox_Reports SET IPAddress=%u', ip2long($_SERVER['REMOTE_ADDR']))); 
    64     if (mysql_affected_rows() != 1) 
    65         throw new Exception('Could not insert crash report into Reports table: ' . mysql_error()); 
     65    $pdo = new Database(); 
     66    $pdo->beginTransaction(); 
    6667 
    67     $reportId = mysql_insert_id(); 
     68    $statement = $pdo->prepare('INSERT INTO BlackBox_Reports SET IPAddress=?'); 
     69    $statement->bindParam(1, sprintf('%u', ip2long($_SERVER['REMOTE_ADDR']))); 
     70    try 
     71    { 
     72        $statement->execute(); 
     73    } 
     74    catch (PDOException $e) 
     75    { 
     76        throw new Exception('Could not insert crash report into Reports table', null, $e); 
     77    } 
     78 
     79    $reportId = $pdo->lastInsertId(); 
     80    $exceptionInsert = $pdo->prepare('INSERT INTO BlackBox_Exceptions 
     81        SET ReportID=?, ExceptionType=?, ExceptionDepth=?'); 
     82    $exceptionInsert->bindParam(1, $reportId); 
     83    $stackFrameInsert = $pdo->prepare('INSERT INTO BlackBox_StackFrames SET 
     84        ExceptionID=?, StackFrameIndex=?, Function=?, File=?, Line=?'); 
    6885    foreach ($stackTrace as $exceptionDepth => $exception) 
    6986    { 
    70         mysql_query(sprintf('INSERT INTO BlackBox_Exceptions SET ReportID=%d, ExceptionType=\'%s\', ExceptionDepth=%d', 
    71             $reportId, mysql_real_escape_string($exception['exception']), $exceptionDepth)); 
    72         if (mysql_affected_rows() != 1) 
    73             throw new Exception('Could not insert exception into Exceptions table: ' . mysql_error()); 
    74  
    75         $exceptionId = mysql_insert_id(); 
     87        $exceptionInsert->bindParam(2, $exception['exception']); 
     88        $exceptionInsert->bindParam(3, $exceptionDepth); 
     89        try 
     90        { 
     91            $exceptionInsert->execute(); 
     92        } 
     93        catch (PDOException $e) 
     94        { 
     95            throw new Exception('Could not insert exception into Exceptions table', null, $e); 
     96        } 
     97         
     98        $exceptionId = $pdo->lastInsertId(); 
    7699        foreach ($exception as $stackIndex => $stackFrame) 
    77100        { 
     
    93116                $function = $matches[2]; 
    94117            } 
    95              
    96             mysql_query(sprintf('INSERT INTO BlackBox_StackFrames SET 
    97                 ExceptionID=%d, StackFrameIndex=%d, Function=\'%s\', File=%s, Line=%s', 
    98                 $exceptionId, $stackIndex, mysql_real_escape_string($function), 
    99                 empty($file) ? 'null' : sprintf('\'%s\'', mysql_real_escape_string($file)), 
    100                 $line == null ? 'null' : intval($line))); 
    101              
    102             if (mysql_affected_rows() != 1) 
    103                 throw new Exception('Could not insert stack frame into Stack Frames table: ' . mysql_error()); 
     118 
     119            $stackFrameInsert->bindParam(1, $exceptionId); 
     120            $stackFrameInsert->bindParam(2, $stackIndex); 
     121            $stackFrameInsert->bindParam(3, $function); 
     122            $stackFrameInsert->bindParam(4, $file); 
     123            $stackFrameInsert->bindParam(5, $line); 
     124            try 
     125            { 
     126                $stackFrameInsert->execute(); 
     127            } 
     128            catch (PDOException $e) 
     129            { 
     130                throw new Exception('Could not insert stack frame into Stack Frames table', null, $e); 
     131            } 
    104132        } 
    105133    } 
    106134     
    107     mysql_query('COMMIT'); 
    108      
     135    $pdo->commit(); 
     136 
    109137    //Move the temporary file to out dumps folder for later inspection 
    110138    if (!move_uploaded_file($crashReport['tmp_name'], 'dumps/' . $reportId . '.tbz')) 
Note: See TracChangeset for help on using the changeset viewer.