Exemplo n.º 1
0
define('ALLOWED_CHARS', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
// Create SQLiteDatabase object
if (!($db = new SQLiteDatabase('db.sqlite', 0666))) {
    die("Cant connect to the file database. Make sure the db file is in the right place and the directory is writable by the web server.");
}
// TODO: make this configurable someway? .. some nicer way!
define('DB_TABLE', 'shortenedurls');
define('SHORTENER_BASE_HREF', "http://" . $_SERVER["HTTP_HOST"] . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"] . "?u=");
define('FALLBACK_URL', $_SERVER["SERVER_NAME"] . $_SERVER["SERVER_PORT"] . '/boxview/examples/');
// Request to shorten a long url
if ($_REQUEST['longurl']) {
    $url_to_shorten = get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['longurl'])) : trim($_REQUEST['longurl']);
    if (!empty($url_to_shorten) && preg_match('|^https?://|', $url_to_shorten)) {
        // check if the URL has already been shortened
        $q = 'SELECT id FROM ' . DB_TABLE . ' WHERE long_url="' . sqlite_escape_string($url_to_shorten) . '"';
        $already_shortened = $db->singleQuery($q);
        if (!empty($already_shortened)) {
            // URL has already been shortened
            $shortened_url = getShortenedURLFromID($already_shortened);
        } else {
            // URL not in database, insert building an unique id
            $q = 'SELECT max(id) FROM ' . DB_TABLE;
            $res = $db->singleQuery($q);
            $new_id = intval($res) + 1;
            $q = 'INSERT INTO ' . DB_TABLE . ' (id, long_url, created, creator) VALUES ("' . $new_id . '", "' . sqlite_escape_string($url_to_shorten) . '", "' . time() . '", "' . sqlite_escape_string($_SERVER['REMOTE_ADDR']) . '")';
            $res = $db->query($q);
            $shortened_url = getShortenedURLFromID($new_id);
        }
        // Output the shortened url
        die(SHORTENER_BASE_HREF . $shortened_url);
    }
Exemplo n.º 2
0
<?php

// open database
$db = new SQLiteDatabase("./ip.db");
// begin transaction
$db->query("BEGIN");
$fp = fopen("./ip-to-country.csv", "r");
$query_str = '';
while ($row = fgetcsv($fp, 4096)) {
    foreach ($row as $key => $val) {
        // secure data
        $row[$key] = sqlite_escape_string($val);
    }
    // check for existance of a country in db
    if (!($country_id = $db->singleQuery("SELECT id FROM country_data WHERE cc_code_2='{$row[2]}'"))) {
        // add new country
        if (!$db->query("INSERT INTO country_data \n\t\t\t(cc_code_2, cc_code_3, country_name) \n\t\t\tVALUES('{$row[2]}', '{$row[3]}', '{$row[4]}')")) {
            // fetch error
            $err_code = $db->lastError();
            printf("Query Failed %d:%s\n", $err_code, sqlite_error_string($err_code));
            exit;
        }
        // get ID for the last inserted row
        $country_id = $db->lastInsertRowid();
    }
    $query_str .= "INSERT INTO ip_ranges \n\t\t\t(ip_start, ip_end, country_code)\n\t\t\tVALUES({$row[0]}, {$row[1]}, {$country_id});";
}
// insert IP data via a chained query
$db->query($query_str);
// finalize transaction
$db->query("COMMIT");
Exemplo n.º 3
0
<?php

/*
** Скрипт возвращает последние записи в гостевой книге
*/
require_once 'gbookrecord.class.php';
define('MAX_RECORDS', 10);
$db = new SQLiteDatabase('gbook.db');
$res = $db->query('SELECT * FROM gbook ORDER BY date DESC');
$lastMod = $db->singleQuery('SELECT MAX(date) AS max_date FROM gbook');
$records = array();
$recordCount = 0;
while ($row = $res->fetch(SQLITE_ASSOC)) {
    $records[] = new GBookRecord($row['id'], $row['author'], $row['email'], $row['message'], $row['date']);
    $recordCount++;
    if ($recordCount >= MAX_RECORDS) {
        break;
    }
}
// Передаем заголовки и JSON пакет данных
header('Content-type: text/plain; charset=utf-8');
header('Cache-Control: no-store, no-cache');
header('Expires: ' . date('r'));
header('Last-Modified: ' . date('r', $lastMod));
echo json_encode($records);