function dl_read_stat_per_path($path = "%", $sortBy, $sort, $listType, $limit)
{
    $config = dl_get_config();
    if (!isset($sortBy)) {
        $sortBy = $config["sortBy"];
    }
    if (!isset($sort)) {
        $sort = $config["sortOrder"];
    }
    if (!isset($limit)) {
        $limit = $config["top_max"];
    }
    $db = __do_db_connect();
    $sth = $db->prepare("SELECT url, counter FROM dl_statistics WHERE url LIKE :path ORDER by  {$sortBy} {$sort}");
    if ($sth) {
        $generic_path = "";
        if ($path == "%") {
            $generic_path = "%";
        } else {
            $generic_path = $path . '%';
        }
        $sth->bindParam(':path', $generic_path);
        if (!$sth->execute()) {
            print_r($sth->errorInfo());
            die("Error executing statement ");
        }
        $result = $sth->fetchAll();
        # Tidy array up, I only want named keys
        $full_result = array();
        foreach ($result as $elem => &$line) {
            if (file_exists('/mnt/usb/LibraryBox' . $line['url'])) {
                unset($line[0]);
                unset($line[1]);
                $url_expl = explode('/', $line['url']);
                $line['filename'] = end($url_expl);
                $line['url_encoded'] = rawurlencode($line['url']);
                $line['filename_encoded'] = rawurlencode(get_utf8_encoded($line['filename']));
                $full_result[] = $line;
            }
        }
        if ($listType == "top") {
            return array_slice($full_result, 0, $limit);
        }
        return $full_result;
    } else {
        print_r($db->errorInfo());
        die("\n no valid statement could be found");
    }
}
<?php

/*********************************

  Simple script, which counts downloads of a specific file
  based on the download URL

  Matthias Strubel  - matthias.strubel@aod-rpg.de
  (C) 2013 - GPL3


**********************************/
include "dl_statistics.func.php";
#$SQLITE_FILE = "sqlite:/opt/piratebox/share/dl_statistics.sqlite";
$redirect_url = $_GET['DL_URL'];
$db = __do_db_connect();
$sel_sth = $db->prepare("SELECT url, counter FROM dl_statistics WHERE url = :url ");
if (!$sel_sth->execute(array(':url' => $redirect_url))) {
    die("Error getting stat. line: " . $sel_sthi->errorInfo());
}
$up_sth = "";
$cnt = 0;
if ($row = $sel_sth->fetch(PDO::FETCH_ASSOC)) {
    $cnt = $row['counter'] + 1;
    $up_sth = $db->prepare("UPDATE dl_statistics SET counter = :cnt WHERE url = :url");
} else {
    // Seems no hit, so we try to insert it.
    $cnt = 1;
    $up_sth = $db->prepare("INSERT INTO dl_statistics ( url , counter ) VALUES ( :url , :cnt ) ");
}
if (!$up_sth->execute(array(':url' => $redirect_url, ':cnt' => $cnt))) {