1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| <?php class sqllite { private $ResultFile = 'result.txt'; private $r_db; private $r_table = 'url2ip'; public function __construct() { try { $this->r_db = new PDO('sqlite:url2ip.db', null, null, array(PDO::ATTR_PERSISTENT => true)); $sql = "PRAGMA synchronous = OFF;"; $sql.= <<<EOF CREATE TABLE IF NOT EXISTS {$this->r_table} ( 'id' INTEGER primary key AUTOINCREMENT NOT NULL, 'domain' varchar(50) NOT NULL , 'ip' varchar(20) NOT NULL, 'server' varchar(100) );
CREATE UNIQUE INDEX 'unique_{$this->r_table}' ON 'url2ip' ('domain' ASC, 'ip' ASC); EOF; $this->r_db->exec($sql); } catch(PDOException $ex) { die("[Error] " . $ex->getMessage() . PHP_EOL . ' add/edit in php.ini' . PHP_EOL . ' extension=pdo_sqlite.so or pdo_sqlite.dll' . PHP_EOL); } } public function insert($value1, $value2, $value3 = NULL) { $db = $this->r_db; $sql = <<<EOF insert into {$this->r_table}('domain','ip','server') VALUES ('{$value1}', '{$value2}','{$value3}') ; EOF; $db->exec($sql); } public function show_result() { $sql = <<<EOF SELECT domain,ip,server from {$this->r_table} ORDER BY ip; EOF; $sql = $this->r_db->query($sql); $result = $sql->fetchAll(); return $result; } public function show_count() { $sql = <<<EOF SELECT count(*) from {$this->r_table}; EOF; $result = $this->r_db->prepare($sql); $result->execute(); $result = $result->fetch(); return $result[0]; } public function deleteTable() { $db = $this->r_db; $sql = <<<EOF drop table if exists {$this->r_table}; delete from sqlite_sequence; EOF; $db->exec($sql); } public function server_ip_exit($ip) { $sql = <<<EOF SELECT count(*) from {$this->r_table} where ip='{$ip}' and server not NULL; //该ip server无值 EOF; $result = $this->r_db->prepare($sql); $result->execute(); $result = $result->fetch(); return $result[0]; } public function get_server($ip) { $sql = <<<EOF select server from {$this->r_table} where ip='{$ip}' and server is not null limit 1; EOF; $result = $this->r_db->prepare($sql); $result->execute(); $result = $result->fetch(); return $result[0]; }
public function import($file) { if (FALSE !== ($fp = fopen($file, 'r'))) { $db = $this->r_db; $db->beginTransaction(); while (!feof($fp)) { $buffer = trim(fgets($fp, 1024)); list($host, $ip, $server) = preg_split('/\s+/', $buffer); $sql = "insert into {$this->r_table}('domain','ip','server') VALUES ('{$host}', '{$ip}','$server') ;"; $db->exec($sql); } $db->commit(); fclose($fp); return true; } } public function dump() { $results = $this->show_result(); if (!empty($results) && is_array($results)) { foreach ($results as $key => $r) { file_put_contents($this->ResultFile, str_pad($r['domain'], 50, ' ') . str_pad($r['ip'], 50, ' ') . str_pad($r['server'], 100, ' ') . PHP_EOL, FILE_APPEND); } echo 'Dump Sussess! SaveTo: ' . $this->ResultFile; } else { exit("Dump Fail..! The Result is the empty... \r\n"); } } public function __destruct() { $this->r_db = null; } } ?>
|