Ejemplo n.º 1
0
 function querySingle($query, $all = false)
 {
     $this->error = "";
     if ($this->versi == 3) {
         return $this->db->querySingle($query, $all);
     } else {
         return sqlite_single_query($this->db, $query, $all);
     }
 }
Ejemplo n.º 2
0
function insert_in_db($tag)
{
    global $array, $idx;
    $sql = '';
    foreach ($array as $key => $data) {
        if (sqlite_single_query($idx, "SELECT name FROM changelog WHERE name='{$key}'")) {
            $sql .= "UPDATE changelog SET {$tag}='{$data[1]}' WHERE name='{$key}';";
        } else {
            $sql .= "INSERT INTO changelog (name, {$tag}) VALUES ('{$key}', '{$data[1]}');";
        }
        $sql .= "REPLACE INTO last_seen_values (name, defaultval, permissions) VALUES ('{$key}', '" . sqlite_escape_string(expand_macros($data[0])) . "', '{$data['1']}');";
    }
    if ($sql) {
        sqlite_query($idx, $sql);
    }
}
Ejemplo n.º 3
0
<?php

chdir(dirname(__FILE__));
// pres2 hack
// sample IP
$_SERVER['REMOTE_ADDR'] = "24.100.195.79";
$ip_int = sprintf("%u", ip2long($_SERVER['REMOTE_ADDR']));
$country = sqlite_single_query("\nSELECT country_name \nFROM ip_ranges ir \nINNER JOIN country_data cd ON ir.country_code=cd.id\nWHERE {$ip_int} BETWEEN ip_start AND ip_end", sqlite_open("./ip.db"));
echo "User is located in {$country}";
Ejemplo n.º 4
0
 function write_data($table_name)
 {
     global $db;
     static $proper;
     if (is_null($proper)) {
         $proper = version_compare(PHP_VERSION, '5.1.3', '>=');
     }
     if ($proper) {
         $col_types = sqlite_fetch_column_types($db->db_connect_id, $table_name);
     } else {
         $sql = "SELECT sql\n\t\t\t\tFROM sqlite_master\n\t\t\t\tWHERE type = 'table'\n\t\t\t\t\tAND name = '" . $table_name . "'";
         $table_data = sqlite_single_query($db->db_connect_id, $sql);
         $table_data = preg_replace('#CREATE\\s+TABLE\\s+"?' . $table_name . '"?#i', '', $table_data);
         $table_data = trim($table_data);
         preg_match('#\\((.*)\\)#s', $table_data, $matches);
         $table_cols = explode(',', trim($matches[1]));
         foreach ($table_cols as $declaration) {
             $entities = preg_split('#\\s+#', trim($declaration));
             $column_name = preg_replace('/"?([^"]+)"?/', '\\1', $entities[0]);
             // Hit a primary key, those are not what we need :D
             if (empty($entities[1]) || strtolower($entities[0]) === 'primary' && strtolower($entities[1]) === 'key') {
                 continue;
             }
             $col_types[$column_name] = $entities[1];
         }
     }
     $sql = "SELECT *\n\t\t\tFROM {$table_name}";
     $result = sqlite_unbuffered_query($db->db_connect_id, $sql);
     $rows = sqlite_fetch_all($result, SQLITE_ASSOC);
     $sql_insert = 'INSERT INTO ' . $table_name . ' (' . implode(', ', array_keys($col_types)) . ') VALUES (';
     foreach ($rows as $row) {
         foreach ($row as $column_name => $column_data) {
             if (is_null($column_data)) {
                 $row[$column_name] = 'NULL';
             } else {
                 if ($column_data == '') {
                     $row[$column_name] = "''";
                 } else {
                     if (strpos($col_types[$column_name], 'text') !== false || strpos($col_types[$column_name], 'char') !== false || strpos($col_types[$column_name], 'blob') !== false) {
                         $row[$column_name] = sanitize_data_generic(str_replace("'", "''", $column_data));
                     }
                 }
             }
         }
         $this->flush($sql_insert . implode(', ', $row) . ");\n");
     }
 }
Ejemplo n.º 5
0
         }
     }
     /* check for/create the queue file */
     if (file_exists($queuefile) && filesize($queuefile) < 3000) {
         unlink($queuefile);
     }
     clearstatcache();
     if (!file_exists($queuefile)) {
         $db = sqlite_open($queuefile);
         sqlite_query($db, "BEGIN;\n\t\t\tCREATE TABLE notes( \n\t\t\t\tid INTEGER PRIMARY KEY, \n\t\t\t\tpage CHAR(100), \n\t\t\t\tlang CHAR(5),\n\t\t\t\tdate INT(11), \n\t\t\t\temail CHAR(700), \n\t\t\t\tdisplay CHAR(700),\n\t\t\t\tcomment CHAR(4000)); \n\t\t\tCOMMIT;");
         sqlite_close($db);
     }
     /* check for/create the last_id file while we're at it */
     if (!file_exists($last_id) || !file_get_contents($last_id) || file_get_contents($last_id == '0')) {
         $db = sqlite_open($notesfile);
         $estimate = sqlite_single_query($db, "SELECT COUNT(*) FROM notes");
         $setid = sqlite_query($db, "SELECT id FROM notes WHERE id > '{$estimate}'", SQLITE_ASSOC);
         if ($setid && sqlite_num_rows($setid) > 0) {
             while (sqlite_valid($setid)) {
                 $rowid = sqlite_fetch_single($setid);
             }
         } else {
             $rowid = $estimate;
         }
         file_put_contents($last_id, $rowid);
         sqlite_close($db);
     }
 }
 /* ============================ PREVIEW ONLY ========================= */
 if (isset($_POST['preview'])) {
     print "<br />\n<p>\nThis is what your entry will look like, roughly:\n</p>\n";
Ejemplo n.º 6
0
 /**
  * Removes items from the cache by conditions & garbage collector.
  * @param  array  conditions
  * @return void
  */
 public function clean(array $conds)
 {
     $all = !empty($conds[Cache::ALL]);
     $collector = empty($conds);
     // cleaning using file iterator
     if ($all || $collector) {
         $now = time();
         $base = $this->dir . DIRECTORY_SEPARATOR . 'c';
         $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->dir), RecursiveIteratorIterator::CHILD_FIRST);
         foreach ($iterator as $entry) {
             $path = (string) $entry;
             if (strncmp($path, $base, strlen($base))) {
                 // skip files out of cache
                 continue;
             }
             if ($entry->isDir()) {
                 // collector: remove empty dirs
                 @rmdir($path);
                 // @ - removing dirs is not necessary
                 continue;
             }
             if ($all) {
                 $this->delete($path);
             } else {
                 // collector
                 $meta = $this->readMeta($path, LOCK_SH);
                 if (!$meta) {
                     continue;
                 }
                 if (!empty($meta[self::META_EXPIRE]) && $meta[self::META_EXPIRE] < $now) {
                     $this->delete($path, $meta[self::HANDLE]);
                     continue;
                 }
                 fclose($meta[self::HANDLE]);
             }
         }
         if ($all && extension_loaded('sqlite')) {
             sqlite_exec("DELETE FROM cache", $this->getDb());
         }
         return;
     }
     // cleaning using journal
     if (!empty($conds[Cache::TAGS])) {
         $db = $this->getDb();
         foreach ((array) $conds[Cache::TAGS] as $tag) {
             $tmp[] = "'" . sqlite_escape_string($tag) . "'";
         }
         $query[] = "tag IN (" . implode(',', $tmp) . ")";
     }
     if (isset($conds[Cache::PRIORITY])) {
         $query[] = "priority <= " . (int) $conds[Cache::PRIORITY];
     }
     if (isset($query)) {
         $db = $this->getDb();
         $query = implode(' OR ', $query);
         $files = sqlite_single_query("SELECT file FROM cache WHERE {$query}", $db, FALSE);
         foreach ($files as $file) {
             $this->delete($file);
         }
         sqlite_exec("DELETE FROM cache WHERE {$query}", $db);
     }
 }
Ejemplo n.º 7
0
<?php

define('sqlite_r', sqlite_open("./ip.db"));
function safe_query($query)
{
    $res = sqlite_query($query, sqlite_r);
    if (!$res) {
        $err_code = sqlite_last_error(sqlite_r);
        printf("Query Failed %d:%s\n", $err_code, sqlite_error_string($err_code));
        exit;
    }
    return $res;
}
sqlite_query("BEGIN", sqlite_r);
$fp = fopen("./ip-to-country.csv", "r");
$query_str = '';
while ($row = fgetcsv($fp, 4096)) {
    foreach ($row as $key => $val) {
        $row[$key] = sqlite_escape_string($val);
    }
    if (!($country_id = sqlite_single_query("SELECT id FROM country_data WHERE cc_code_2='{$row[2]}'", sqlite_r))) {
        $res = safe_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]}')");
        $country_id = sqlite_last_insert_rowid(sqlite_r);
    }
    $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});";
}
safe_query($query_str, sqlite_r);
sqlite_query("COMMIT", sqlite_r);
fclose($fp);
sqlite_close(sqlite_r);
// close database
Ejemplo n.º 8
0
<?php

chdir(dirname(__FILE__));
// pres2 hack
$db = sqlite_open("./ip.db");
$result = sqlite_single_query("SELECT country_name FROM country_data WHERE country_name LIKE 'U%'", $db);
foreach ($result as $country) {
    echo $country . "<br />\n";
}
Ejemplo n.º 9
0
 public function Insert($Datos, $tabla, $AntiHack = true)
 {
     $this->Datos = $Datos;
     $this->Cadena1 = "INSERT INTO " . $tabla . " ( ";
     $this->Cadena2 = " VALUES ( ";
     $this->total_valores = count($this->Datos);
     $i = 0;
     foreach ($this->Datos as $nombre => $valor) {
         if ($i < $this->total_valores - 1) {
             $this->Cadena1 .= "`" . $nombre . "` , ";
             $this->Cadena2 .= "'" . $valor . "' , ";
         } else {
             $this->Cadena1 .= "`" . $nombre . "` ) ";
             $this->Cadena2 .= "'" . $valor . "' ) ";
         }
         $i++;
     }
     $this->Sentencia = $this->Cadena1 . $this->Cadena2;
     if ($AntiHack) {
         $this->Sentencia = $this->AntiHack($this->Sentencia);
     }
     if ($this->DbType == 'mysql') {
         $this->resultado_insert = @mysql_query($this->Sentencia);
         if ($this->resultado_insert) {
             $this->resultado6['msg'] = "Dato Insertado Correctamente";
             $this->resultado6['bandera'] = true;
             $this->resultado6['affected_rows'] = mysql_affected_rows();
             $this->resultado6['insert_id'] = mysql_insert_id();
         } else {
             $this->resultado6['msg'] = "Dato Insertado Erroneamente : " . mysql_error();
             $this->resultado6['bandera'] = false;
         }
         @mysql_free_result($this->resultado_insert);
         return $this->resultado6;
     }
     if ($this->DbType == 'sqlite') {
         $this->resultado_insert = @sqlite_single_query($this->SqliteDB, $this->Sentencia);
         print_r($this->resultado_insert);
         if ($this->resultado_insert) {
             $this->resultado['msg'] = "Dato Insertado Correctamente";
             $this->resultado['bandera'] = true;
             $this->resultado['insert_id'] = @sqlite_last_insert_rowid($this->resultado_insert);
         } else {
             $this->resultado['msg'] = "Dato Insertado Erroneamente";
             $this->resultado['bandera'] = false;
             $this->resultado['insert_id'] = @sqlite_last_insert_rowid($this->resultado_insert);
         }
         return $this->resultado;
     }
 }
Ejemplo n.º 10
0
<pre>
<?php 
$db = sqlite_open(dirname(__FILE__) . "/db.sqlite");
print_r(sqlite_single_query("SELECT login FROM auth_tbl", $db));
sqlite_close($db);
?>
</pre>
Ejemplo n.º 11
0
 protected function _returnResultRow($sql, $class = null, $lockRows = false)
 {
     $this->_connect();
     if (!($query = sqlite_query($this->_conn, $sql))) {
         return null;
     }
     if (sqlite_num_rows($query) <= 0) {
         return null;
     }
     $row = sqlite_single_query($query, true);
     if (is_null($class)) {
         return $row;
     }
     return $this->_returnEntity($row, $class);
 }
Ejemplo n.º 12
0
<?php

function most_similar(&$context, $string, $source_str)
{
    /* Calculate the similarity between two strings */
    $sim = similar_text($string, $source_str);
    if (empty($context) || $sim > $context['sim']) {
        $context = array('sim' => $sim, 'title' => $string);
    }
}
function most_similar_finalize(&$context)
{
    return $context['title'];
}
$db = sqlite_open(dirname(__FILE__) . "/db2.sqlite");
/* bool sqlite_create_aggregate (resource dbhandle, string function_name, mixed step_func, mixed finalize_func [, int num_args]) */
sqlite_create_aggregate($db, 'similar_text', 'most_similar', 'most_similar_finalize', 2);
$title = 'mesg #2';
echo "Most Similar title to '{$title}' according to similar_text() is: ";
echo sqlite_single_query("SELECT similar_text(title, '{$title}') FROM messages", $db);
echo "<br />\n";
Ejemplo n.º 13
0
function check_if_exists($cc)
{
    return sqlite_single_query("SELECT id FROM country_data WHERE cc_code_2='{$cc}'", sqlite_r);
}
Ejemplo n.º 14
0
<?php

require "./fav_settings.php";
require "./fav_strings.php";
$conn = sqlite_popen($sqlite_file);
if ($_GET['id'] > 0) {
    $qry = "SELECT name FROM Fav WHERE id=" . $_GET['id'];
    $container = sqlite_single_query($conn, $qry);
} elseif ($_GET['id'] == -1) {
    $container = $MyFav_Order_CatOrd;
} else {
    $container = $MyFav_Order_RootOrd;
}
$qry = "SELECT id,name FROM Fav WHERE " . ($_GET['id'] == -1 ? 'cat = 1' : 'catid=' . $_GET['id']) . (!$_GET['id'] ? ' AND cat = 0' : '') . " ORDER BY ord,id";
$rs = sqlite_query($conn, $qry);
$rcnt = sqlite_num_rows($rs);
$MM_RedirectUrl = $homeURL . $SidebarSuffix1;
if (isset($_SESSION['isLogined']) && isset($_POST["MM_reorder"]) && $_POST["MM_reorder"] == "form1") {
    $Command1_CommandText = '';
    $order = explode("|", $_POST['newOrder']);
    array_pop($order);
    foreach ($order as $ord => $id) {
        $Command1_CommandText .= "UPDATE Fav SET ord = " . sqlite_escape_string($ord) . " WHERE id = " . sqlite_escape_string($id) . ";";
    }
    sqlite_exec($Command1_CommandText, $conn);
    header("Location: " . $MM_RedirectUrl);
    exit;
}
echo '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Ejemplo n.º 15
0
    for ($i = 0; $i < sqlite_num_fields($rh); $i++) {
        $n = sqlite_field_name($rh, $i);
        $c = sqlite_column($rh, $i);
        var_dump($c);
        $c = sqlite_column($rh, $n);
        var_dump($c);
    }
    // fetch_all
    $r = sqlite_fetch_all($rh);
    var_dump($r);
    // fetch single
    sqlite_rewind($rh);
    $r = sqlite_fetch_string($rh);
    var_dump($r);
} else {
    echo "bad query: " . sqlite_error_string($db);
}
// array_query
$val = sqlite_array_query($db, 'SELECT * FROM mytable');
var_dump($val);
// single query
$val = sqlite_single_query($db, 'SELECT * FROM mytable');
var_dump($val);
$val = sqlite_single_query($db, 'SELECT * FROM mytable LIMIT 1', true);
var_dump($val);
$val = sqlite_single_query($db, 'SELECT * FROM mytable LIMIT 1', false);
var_dump($val);
$r = sqlite_close($db);
?>