Global.php
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:6k
源码类别:
P2P编程
开发平台:
Visual C++
- <?php
- //define("__ERROR_LOGFILE", 'debug/internalError.txt');
- /*
- * 内部错误记录
- * 参数: $msg 记录信息内容
- */
- function internalError($msg = '') {
- if(defined("__DEBUG")) { // 只有在开启了调试模式的情况下记录错误信息
- if($msg) {
- $fp = fopen(__ERROR_LOGFILE, 'a');
- $data = date("Y-m-d H:i:s").
- "n-----------------------------------------n".
- $msg.
- "n-----------------------------------------nn";
- fputs($fp, $data);
- fclose($fp);
- }
- }
- exit();
- }
- function utime() {
- list($sec, $usec) = explode(' ', microtime());
- return $sec + $usec;
- }
- /*
- * 设定 Cookie
- * 参数: $name Cookie 名称
- $var Cookie 内容
- $expire 作用时长
- $secure 在安全域中使用时设置为1
- */
- function Cookie($name, $var, $expires = 0, $secure = 0) {
- GLOBAL $timestamp;
- $cookie_time = $expires ? ($timestamp + $expires) : 0;
- SetCookie($name, $var, $cookie_time, COOKIE_PATH, COOKIE_DOMAIN, $secure);
- }
- function checkAttribute($name, $value) {
- /*
- $allowedAttributes = Array("username" => '^[a-zA-Z]([0-9a-zA-Z_-])*$',
- "email" => '^[a-z0-9]+[a-z0-9_\.-]*@([a-z0-9]+([.-][a-z0-9]+)*)\.[a-z]{2,4}$',
- "common" => '^[A-Za-z 0-9-_]+$',
- "password" => '^([A-Za-z0-9_@!??\.-]){6,}$',
- "mobilnr" => '^[0-9-]{8,}$',
- "float" => '^[0-9]*.*[0-9]*$',
- "numbers" => '^[0-9]+$',
- "signednumbers" => '^[0-9-]+$',
- "url" => '^[A-Za-z0-9._~/:?&=+-]+$',
- "digits+letters" => '^[A-Za-z0-9]+$'
- );
- */
- $allowedAttributes = Array(
- 'username' => '^([0-9a-zA-Z_-])*$',
- 'common' => '^[0-9a-zA-Z_'.chr(0xa1).'-'.chr(0xff).'-]+$',
- 'email' => '^[a-z0-9]+[a-z0-9_\.-]*@([a-z0-9]+([.-][a-z0-9]+)*)\.[a-z]{2,4}$',
- 'password' => '^([A-Za-z0-9_@!??\.-]){3,}$',
- 'float' => '^[0-9]*.*[0-9]*$',
- 'mobilnr' => '^[0-9-]{8,}$',
- 'numbers' => '^[0-9]+$',
- 'url' => '^[A-Za-z0-9._~/:?&=+-]+$',
- 'alnum' => '^[a-zA-Z0-9]+$',
- 'ip' => '^[0-9.]+$',
- 'letter' => '^[A-Za-z]+$',
- 'number+' => '^[0-9-]+$'
- );
- $regexp = '~'.str_replace("~", "\~", $allowedAttributes[$name]).'~';
- if(preg_match($regexp, $value) != 0)
- return true;
- else
- return false;
- }
- /*
- * 获取页面传递的变量
- * 参数: $name 需要获取的变量名
- $format 过滤方法
- $method 获取方法,默认为 GET 方式
- */
- function getParameter($name, $format, $method = PP_GET) {
- switch($method) {
- case PP_GET:
- $var = $_GET[$name];
- break;
- case PP_POST:
- $var = $_POST[$name];
- break;
- case PP_GET_POST:
- $var = isset($_GET[$name]) ? $_GET[$name] : $_POST[$name];
- break;
- default:
- internalError("getPageParameter(): Invalid parameter method: $method");
- break;
- }
- if(isset($var)) {
- if($format) {
- if(!checkAttribute($format, $var))
- $var = false;
- }
- }
- if($var == "")
- unset($var);
- return $var;
- }
- /*
- function is_ip($ip) {
- return eregi("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$", $ip);
- }
- */
- function convert_ip_tonumber($ip) {
- if(!checkAttribute('ip', $ip)) return false;
- $ip_array = explode(".", $ip);
- $base = 256;
- $ip_number = $ip_array[0] * pow($base, 3);
- $ip_number += $ip_array[1] * pow($base, 2);
- $ip_number += $ip_array[2] * pow($base, 1);
- $ip_number += $ip_array[3] * pow($base, 0);
- return $ip_number;
- }
- /*
- * 网络切换函数,默认返回 telecom 电信的频道信息数组
- * 参数: $channel 网络名称
- */
- function change_channel($channel = 'telecom') {
- switch($channel) {
- case 'netcom':
- case 'cnc':
- return array('CabUrl' => 'http://222.240.213.60/update/Chaos.cab',
- 'ClientUrl' => 'http://222.240.213.60/update/XTVsetup.exe',
- 'Gate' => 'cnc'
- );
- break;
- case 'edu':
- return array('CabUrl' => 'http://166.111.118.5/update/Chaos.cab',
- 'ClientUrl' => 'http://166.111.118.5/update/XTVsetup.exe',
- 'Gate' => 'edu'
- );
- break;
- case 'telecom':
- case 'tel':
- return array('CabUrl' => 'http://58.20.33.3/update/Chaos.cab',
- 'ClientUrl' => 'http://58.20.33.3/update/XTVsetup.exe',
- 'Gate' => 'tel'
- );
- break;
- default:
- ipunknown($onlineip);
- return array('CabUrl' => 'http://58.20.33.3/update/Chaos.cab',
- 'ClientUrl' => 'http://58.20.33.3/update/XTVsetup.exe',
- 'Gate' => 'tel'
- );
- break;
- }
- }
- /*
- * 通过用户来访IP,切换网络,返回相关信息的数组
- */
- function ip_table() {
- global $onlineip;
- $ipnum = convert_ip_tonumber($onlineip);
- if(!$ipnum) internalError("ip_table(): Remote IP check failed:".$onlineip);
- $sql = "SELECT ip_comefrom FROM ip_table WHERE ip_start <= '$ipnum' AND ip_end >= '$ipnum'";
- list($net) = db_fetch_row(db_query($sql));
- return change_channel($net);
- }
- /*
- * 中文字符串截取
- * 参数: $string 传入的字符串
- $sublen 需要截取的长度
- */
- function cnSubStr($string, $sublen) {
- if($sublen >= strlen($string))
- return $string;
- $s = "";
- for($i = 0; $i < $sublen; $i ++) {
- if(ord($string{$i}) > 127) {
- $s .= $string{$i} . $string{++$i};
- continue;
- } else {
- $s .= $string{$i};
- continue;
- }
- }
- return $s;
- }
- function obj_check($filename, $expire = 300) {
- GLOBAL $timestamp;
- if(!file_exists($filename))
- return false;
- if($timestamp - filemtime($filename) > $expire)
- return false;
- else
- return true;
- }
- function obj_put($filename, $str) {
- @touch($filename.'.new');
- $fp = @fopen($filename.'.new', 'w');
- @fwrite($fp, $str);
- @fclose($fp);
- @unlink($filename);
- @rename($filename.'.new', $filename);
- @chmod($filename, 0777);
- }
- function obj_get($filename) {
- if(!file_exists($filename))
- return false;
- $fp = @fopen($filename, 'r');
- $contents = @fread($fp, filesize($filename));
- @fclose($fp);
- return $contents;
- }
- function checkdir($dirname) {
- if(is_dir($dirname)) {
- return true;
- } else {
- if(!@mkdir($dirname, 0777)) {
- @chmod($dirname,0777);
- }
- }
- return true;
- }
- function ipunknown($onlineip){
- $handle = fopen("Debug/ip_new.txt","a");
- if($handle){
- fwrite($handle, $onlineip."n");
- }
- fclose($handle);
- }
- function listFiles($dir, $suffix){
- $fileList = array();
- if ($handle = opendir($dir)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != ".." && stristr($file, $suffix)!=false ) {
- $fileList[]=$file;
- }
- }
- closedir($handle);
- }
- return $fileList;
- }
- ?>