📄 txtdb.inc.php
字号:
unset($this -> tbls[$table]['values'][$id]);$this -> tbls[$table]['rows']--;$num++;if(!$whole) break;}}}return $num;}//-|END|[records operation]--------------------------------------------------------------------//+|START|[access private]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++/*** Connect to txtdb.(连接到txtdb)*/function connect($path, $cache_dir = 'cache/'){if(substr($path, -1) != "/") $path .= "/";if(substr($cache_dir, -1) != "/") $cache_dir .= "/";if(!is_dir($path)){trigger_error ("Setting Datebase directory ($path) is not a valid path.", E_USER_ERROR);return false;}elseif(!is_writable($path)){trigger_error ("Datebase directory ($path) is not writable, try chmod 0777 if 0666 does not work.", E_USER_ERROR);return false;}if(!is_dir($cache_dir)){trigger_error ("Setting cache directory ($cache_dir) is not a valid path.", E_USER_ERROR);return false;}elseif(!is_writable($cache_dir)){trigger_error ("Cache directory ($cache_dir) is not writable, try chmod 0777 if 0666 does not work.", E_USER_ERROR);return false;}$this -> db['cache_dir'] = $cache_dir;$this -> db['connect'] = true;$this -> db['path'] = $path;$this -> db['opens'] = 0;return true;}/*** Open table(打开表)*/function open($table, $writeable = false){if(!$this -> db['connect']){trigger_error ('Unknow txtDb server ', E_USER_ERROR);return false;}if($this -> tbls[$table]['open']){if((int)$this -> tbls[$table]['writeable'] < (int)$writeable){$this -> _make_writeable($table);}$this -> db['tbl'] = $table;return true;}if(!$this -> exists($table)){trigger_error('Table [' . $table . '] not exists', E_USER_ERROR);return false;}$this -> tbls[$table]['path'] = $this -> db['path'] . $table . $this -> exten;if($writeable){$data = file($this -> tbls[$table]['path']);$this -> tbls[$table]['DBHEADER'] = array_shift($data);$this -> tbls[$table]['records'] = $data;$this -> tbls[$table]['fields'] = @explode(DBSEPARATOR, str_replace("\n", '', str_replace("\r", '', $this -> tbls[$table]['DBHEADER'])));array_shift($this -> tbls[$table]['fields']);array_shift($this -> tbls[$table]['fields']);$this -> tbls[$table]['rows'] = count($this -> tbls[$table]['records']);$this -> tbls[$table]['auto_id'] = array_shift($this -> tbls[$table]['fields']);$this -> tbls[$table]['max_len'] = array_shift($this -> tbls[$table]['fields']);}else{$this -> fp[$table] = @fopen($this -> tbls[$table]['path'], "r+");@flock($this -> fp[$table], LOCK_EX);$this -> tbls[$table]['DBHEADER'] = @fgets($this -> fp[$table], 1024);$this -> tbls[$table]['fields'] = @explode(DBSEPARATOR, str_replace("\n", '', str_replace("\r", '', $this -> tbls[$table]['DBHEADER'])));array_shift($this -> tbls[$table]['fields']);$this -> tbls[$table]['rows'] = array_shift($this -> tbls[$table]['fields']);$this -> tbls[$table]['auto_id'] = array_shift($this -> tbls[$table]['fields']);$this -> tbls[$table]['max_len'] = array_shift($this -> tbls[$table]['fields']);}$this -> tbls[$table]['writeable'] = $writeable;$this -> tbls[$table]['cols'] = count($this -> tbls[$table]['fields']);$this -> tbls[$table]['values'] = array();$this -> tbls[$table]['open'] = true;$this -> db['tbl'] = $table;$this -> db['opens']++;return true;}/*** Close table (关闭表)*/function close($table = null){if(is_null($table)) $table = $this -> db['tbl'];unset($this -> tbls[$table]);if($table == $this -> db['tbl']){unset($this -> db['tbl']);}if(is_resource($this -> fp[$table])){@fclose($this -> fp[$table]);}$this -> db['opens']--;}/*** Make table writeable(将只读表转换为可写)*/function _make_writeable($table){@flock($this -> fp[$table], LOCK_UN);$data = file($this -> tbls[$table]['path']);@flock($this -> fp[$table], LOCK_EX);$this -> tbls[$table]['DBHEADER'] = array_shift($data);unset($this -> tbls[$table]['records']);$this -> tbls[$table]['records'] = $data;$this -> tbls[$table]['writeable'] = true;}/*** Table exists (表是否存在)*/function exists($table){return file_exists($this -> db['path'] . $table . $this -> exten);}/*** Get table last modify time (取表最后修改时间)*/function get_table_mtime($table = null){if(is_null($table)) $table = $this -> db['tbl'];$file = $this -> db['path'] . $table . $this -> exten;return @filemtime($file);}/*** Get cache last modify time (取cache最后修改时间)*/function _get_cache_mtime($cache){$file = $this -> db['cache_dir'] . $cache . $this -> exten;return file_exists($file) ? @filemtime($file) : 0;}/*** Get Cache as Query (取得cache)*/function _get_cache($cache_name){$cache_path = $this->db['cache_dir'].'txtdb['.$cache_name.'].cache.php';if(($cache = @fopen($cache_path, 'r'))){$data = @fread($cache, filesize($cache_path));@fclose($cache);return unserialize(substr($data,strlen(DBHEADER)));}return null;}/*** Set Cache as Query (设置缓存)*/function _set_cache($data, $cache_name){$cache_file_path = $this->db['cache_dir'].'txtdb['.$cache_name.'].cache.php';if(($cache = @fopen($cache_file_path, 'w'))){@flock($cache, LOCK_EX);$data=DBHEADER.serialize($data);fputs($cache, $data);fclose($cache);}}/*** Get an limit Array (取得查询用的limit限定)*/function _get_limit($limit, $table){$_rows = $this -> tbls[$table]['rows'];if(is_null($limit)){return array('begin' => 0, 'end' => $_rows-1);}if(!preg_match("/^\d+(\,\d+)$/", $limit)){trigger_error("Illegal limit expression!", E_USER_ERROR);}if($_rows==0) return array('begin' => '0', 'end' => '-1');list($_begin, $number) = explode(",", $limit);$_begin = intval($_begin);$_end = $_begin + intval($number)-1;if($_begin < 0) $_begin = 0;if($_end > $_rows) $_end = $_rows-1;if($_begin >= $_rows or $_end < 0){trigger_error("Illegal limit expression!", E_USER_ERROR);}return array('begin' => $_begin, 'end' => $_end);}/*** Sort an Array by $field and order by $flag (根据某字段对多维数组排序)*/function _sort($source, $field, $flag){if(!is_array($source) or count($source) <= 1) return $source;$ids = array_keys($source);foreach($ids as $id){$besort[$id] = $source[$id][$field];}strtolower($flag) == "desc" ? arsort($besort, 0) : asort($besort, 0);$order = array_keys($besort);foreach($order as $id){$target[$id] = $source[$id];}return $target;}/*** Build an Array by record id (根据记录号返回数组)*/function _build_values($id, $table){if(is_array($this -> tbls[$table]['values'][$id])) return $this -> tbls[$table]['values'][$id];$values = @explode(DBSEPARATOR, str_replace("\n", '', $this -> tbls[$table]['records'][$id]));foreach($this -> tbls[$table]['fields'] as $key => $field){$this -> tbls[$table]['values'][$id][$field] = $values[$key];}return $this -> tbls[$table]['values'][$id];}/*** Write to file (将数据写入表文件)*/function _write($data, $table){$this -> fp[$table] = @fopen($this -> tbls[$table]['path'], "w");@flock($this -> fp[$table], LOCK_EX);return fwrite($this -> fp[$table], $data);// @fclose ($this->fp[$table]); //fclose非常耗费时间的说}/*** Filter File Name (过滤不能作为文件名的字符)*/function _filter_file_name(& $fname){$filter = array('<', '>', '?', '/', '\\', '|', '*', '"');$filtered = array('[', ']', '!', '~', '^', '1', 'x', '`');$fname = str_replace($filter, $filtered, $fname);}/*** String Convert (输入字符输出处理)*/function _filter_nr(& $string){if(is_array($string)){$keys = array_keys($string);foreach($keys as $k){$this -> _filter_nr($string[$k]);}}else{$string = str_replace(array('\\', DBSEPARATOR, "\n", "\r"),array('\', '&#' . ord(DBSEPARATOR), '
', '
'), $string);}return $string;}/*** String unConvert (特殊字符输出处理)*/function _unfilter_nr(& $string){if(is_array($string)){$keys = array_keys($string);foreach($keys as $k){$this -> _unfilter_nr($string[$k]);}}else{$string = str_replace(array('\', '
', "
", '&#' . ord(DBSEPARATOR)),array('\\', "\r", "\n", DBSEPARATOR), $string);}return $string;}/*** Check fields name or table name (检查字段名,表名是否合法)*/function _check($string){if(is_array($string)) $string = implode('', $string);if(!is_string($string) or trim($string) == '') return false;return !strpos($string, "\n") and preg_match("/^[ \/\\_0-9a-zA-Z\x80-\xff]+$/", $string);}//-|END|[access private]---------------------------------------------------------------------}//END CLASSfunction getCache(&$varName,$dataFile,$cacheFile){if(@filemtime($cacheFile)<@filemtime($dataFile)){require($dataFile);$varName=$data;$fp=@fopen($cacheFile, 'w+');@fwrite($fp,DBHEADER.serialize($data));@fclose($fp);}else{$fp=@fopen($cacheFile,'r');$data=@fread($fp,filesize($cacheFile));@fclose($fp);$varName=unserialize(substr($data,strlen(DBHEADER)));}}function csubstr($str,$len){$strlen=strlen($str);$clen=0;for($i=0;$i<$strlen;$i++,$clen++){if($clen>=$len) break;if(ord(substr($str,$i,1))>0xa0){if($clen>=0) $tmpstr.=substr($str,$i,3);$i=$i+2;}else{if($clen>=0) $tmpstr.=substr($str,$i,1);}}return $tmpstr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -