mirror of https://github.com/owen0o0/getFavicon
				
				
				
			20241218
							parent
							
								
									03ee62443e
								
							
						
					
					
						commit
						dc16836ec4
					
				
							
								
								
									
										27
									
								
								Favicon.php
								
								
								
								
							
							
						
						
									
										27
									
								
								Favicon.php
								
								
								
								
							| 
						 | 
					@ -187,7 +187,8 @@ class Favicon
 | 
				
			||||||
        return array(
 | 
					        return array(
 | 
				
			||||||
            'X-Robots-Tag: noindex, nofollow',
 | 
					            'X-Robots-Tag: noindex, nofollow',
 | 
				
			||||||
            'Content-type: image/x-icon',
 | 
					            'Content-type: image/x-icon',
 | 
				
			||||||
          	'Cache-Control: public, max-age=604800'
 | 
					            'Cache-Control: public, max-age=86400',
 | 
				
			||||||
 | 
					            'Expires: ' . gmdate('D, d M Y H:i:s', time() + 86400) . ' GMT'
 | 
				
			||||||
        );
 | 
					        );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -347,16 +348,15 @@ class Favicon
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        $parsed_url = parse_url($url);
 | 
					        $parsed_url = parse_url($url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (!isset($parsed_url['host']) || !$parsed_url['host']) {
 | 
					        if ($parsed_url === false || !isset($parsed_url['host']) || !$parsed_url['host']) {
 | 
				
			||||||
            //在URL的前面加上http://
 | 
					            //在URL的前面加上http://
 | 
				
			||||||
            // add the prefix
 | 
					 | 
				
			||||||
            if (!preg_match('/^https?:\/\/.*/', $url))
 | 
					            if (!preg_match('/^https?:\/\/.*/', $url))
 | 
				
			||||||
                $url = 'http://' . $url;
 | 
					                $url = 'http://' . $url;
 | 
				
			||||||
            //解析URL并将结果保存到 $this->url
 | 
					            //解析URL并将结果保存到 $this->url
 | 
				
			||||||
            $parsed_url = parse_url($url);
 | 
					            $parsed_url = parse_url($url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if ($parsed_url == FALSE) {
 | 
					            if ($parsed_url === false) {
 | 
				
			||||||
                return FALSE;
 | 
					                return false;
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                /**
 | 
					                /**
 | 
				
			||||||
                 * 能成功解析的话就可以设置原始URL为这个添加过http://前缀的URL
 | 
					                 * 能成功解析的话就可以设置原始URL为这个添加过http://前缀的URL
 | 
				
			||||||
| 
						 | 
					@ -472,6 +472,12 @@ class Favicon
 | 
				
			||||||
    private function getFile($url, $isimg = false, $timeout = 2)
 | 
					    private function getFile($url, $isimg = false, $timeout = 2)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $ch = curl_init($url);
 | 
					        $ch = curl_init($url);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        //添加以下设置:
 | 
				
			||||||
 | 
					        curl_setopt($ch, CURLOPT_TIMEOUT, 5);//设置总体超时5秒
 | 
				
			||||||
 | 
					        curl_setopt($ch, CURLOPT_NOSIGNAL, 1);//在多线程下使用超时选项
 | 
				
			||||||
 | 
					        curL_setopt($ch, CURLOPT_TCP_NODELAY, 1);//不延迟传输
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 | 
					        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
 | 
				
			||||||
        /*
 | 
					        /*
 | 
				
			||||||
         * 2019-06-20
 | 
					         * 2019-06-20
 | 
				
			||||||
| 
						 | 
					@ -493,6 +499,11 @@ class Favicon
 | 
				
			||||||
        $ret = $this->curlExecFollow($ch, 2);
 | 
					        $ret = $this->curlExecFollow($ch, 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ($isimg) {
 | 
					        if ($isimg) {
 | 
				
			||||||
 | 
					            $img_info = @getimagesize($url);
 | 
				
			||||||
 | 
					            if (empty($img_info)) {
 | 
				
			||||||
 | 
					                $ret = '';
 | 
				
			||||||
 | 
					                $this->_log_message("不是图片:{$url}");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            $mime      = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
 | 
					            $mime      = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
 | 
				
			||||||
            $mimeArray = explode('/', $mime);
 | 
					            $mimeArray = explode('/', $mime);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
| 
						 | 
					@ -506,7 +517,8 @@ class Favicon
 | 
				
			||||||
                $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 | 
					                $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $arr = array(
 | 
					                $arr = array(
 | 
				
			||||||
                    'status'   => ($status >= 200 && $status <= 299) ? 'OK' : 'FAIL',
 | 
					                    'code'     => $status,
 | 
				
			||||||
 | 
					                    'status'   => ($status >= 200 && $status <= 399) ? "OK" : "FAIL",
 | 
				
			||||||
                    'data'     => $ret,
 | 
					                    'data'     => $ret,
 | 
				
			||||||
                    'real_url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)
 | 
					                    'real_url' => curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)
 | 
				
			||||||
                );
 | 
					                );
 | 
				
			||||||
| 
						 | 
					@ -531,7 +543,8 @@ class Favicon
 | 
				
			||||||
     * @param int      $maxredirect 最大允许的重定向次数
 | 
					     * @param int      $maxredirect 最大允许的重定向次数
 | 
				
			||||||
     * @return string
 | 
					     * @return string
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    private function curlExecFollow( &$ch, $maxredirect = null) { 
 | 
					    private function curlExecFollow(&$ch, $maxredirect = null)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
        $mr = $maxredirect === null ? 5 : intval($maxredirect);
 | 
					        $mr = $maxredirect === null ? 5 : intval($maxredirect);
 | 
				
			||||||
        if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
 | 
					        if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
 | 
				
			||||||
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
 | 
					            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					<?php
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					define( 'CACHE_DIR', 'cache' );          //缓存目录
 | 
				
			||||||
 | 
					define( 'HASH_KEY', 'iowen' );           //加密密钥, 请修改并勿泄露
 | 
				
			||||||
 | 
					define( 'DEFAULT_ICO', 'favicon.png' );  //默认图标路径
 | 
				
			||||||
 | 
					define( 'EXPIRE', 2592000 );             //缓存有效期30天, 单位为:秒,为0时不缓存
 | 
				
			||||||
							
								
								
									
										108
									
								
								get.php
								
								
								
								
							
							
						
						
									
										108
									
								
								get.php
								
								
								
								
							| 
						 | 
					@ -2,28 +2,30 @@
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * getFavicon
 | 
					 * getFavicon
 | 
				
			||||||
 * @author    一为
 | 
					 * @author    一为
 | 
				
			||||||
 * @date      2019-11-27
 | 
					 * @date      2024-12-18
 | 
				
			||||||
 * @link      https://www.iowen.cn
 | 
					 * @link      https://www.iowen.cn
 | 
				
			||||||
 * @version   1.2.0
 | 
					 * @version   1.2.1
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if( !isset($_GET['url'])){
 | 
					if( !isset($_GET['url'])){
 | 
				
			||||||
    return http_response_code(404);
 | 
					    return http_response_code(404);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require "./config.php"; // 配置文件
 | 
				
			||||||
require "./Favicon.php";
 | 
					require "./Favicon.php";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$favicon = new \Jerrybendy\Favicon\Favicon;
 | 
					$favicon = new \Jerrybendy\Favicon\Favicon;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$cache_dir  = CACHE_DIR;
 | 
				
			||||||
 | 
					$hash_key   = HASH_KEY;
 | 
				
			||||||
 | 
					$defaultIco = DEFAULT_ICO;
 | 
				
			||||||
 | 
					$expire     = EXPIRE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* ------ 参数设置 ------ */
 | 
					// 如果 HASH_KEY == iowen 则生成一个随机字符串,并更新config.php文件
 | 
				
			||||||
 | 
					if (HASH_KEY == 'iowen') {
 | 
				
			||||||
$defaultIco='favicon.png';   //默认图标路径
 | 
					    $hash_key = substr(hash('sha256', uniqid()), 0, 16);
 | 
				
			||||||
$expire = 2592000;           //缓存有效期30天, 单位为:秒,为0时不缓存
 | 
					    file_put_contents('./config.php', str_replace('iowen', $hash_key, file_get_contents('./config.php')));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
/* ------ 参数设置 ------ */
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 设置默认图标
 | 
					 * 设置默认图标
 | 
				
			||||||
| 
						 | 
					@ -45,17 +47,19 @@ if($formatUrl){
 | 
				
			||||||
        exit;
 | 
					        exit;
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        $defaultMD5 = md5(file_get_contents($defaultIco));
 | 
					        $defaultMD5 = md5(file_get_contents($defaultIco));
 | 
				
			||||||
 | 
					        $cache      = new Cache($hash_key, $cache_dir);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 2023-02-20
 | 
					         * 2023-02-20
 | 
				
			||||||
         * 增加刷新缓存参数:refresh=true 如:https://域名?url=www.iowen.cn&refresh=true
 | 
					         * 增加刷新缓存参数:refresh=true 如:https://域名?url=www.iowen.cn&refresh=true
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        if (!isset($_GET['refresh']) || (isset($_GET['refresh']) && $_GET['refresh'] != 'true')) {
 | 
					        if (!isset($_GET['refresh']) || (isset($_GET['refresh']) && $_GET['refresh'] != 'true')) {
 | 
				
			||||||
            $data = Cache::get($formatUrl,$defaultMD5,$expire);
 | 
					            $data = $cache->get($formatUrl, $defaultMD5, $expire);
 | 
				
			||||||
            if ($data !== NULL) {
 | 
					            if ($data !== NULL) {
 | 
				
			||||||
                foreach ($favicon->getHeader() as $header) {
 | 
					                foreach ($favicon->getHeader() as $header) {
 | 
				
			||||||
                    @header($header);
 | 
					                    @header($header);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					                header('X-Cache-Type: IO');
 | 
				
			||||||
                echo $data;
 | 
					                echo $data;
 | 
				
			||||||
                exit;
 | 
					                exit;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					@ -64,13 +68,9 @@ if($formatUrl){
 | 
				
			||||||
        /**
 | 
					        /**
 | 
				
			||||||
         * 缓存中没有指定的内容时, 重新获取内容并缓存起来
 | 
					         * 缓存中没有指定的内容时, 重新获取内容并缓存起来
 | 
				
			||||||
         */
 | 
					         */
 | 
				
			||||||
        $content = $favicon->getFavicon($formatUrl, TRUE);
 | 
					        $content = $favicon->getFavicon($formatUrl, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if( md5($content) == $defaultMD5 ){
 | 
					        $cache->set($formatUrl, $content);
 | 
				
			||||||
            $expire = 43200; //如果返回默认图标,设置过期时间为12小时。Cache::get 方法中需同时修改
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Cache::set($formatUrl, $content, $expire);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach ($favicon->getHeader() as $header) {
 | 
					        foreach ($favicon->getHeader() as $header) {
 | 
				
			||||||
            @header($header);
 | 
					            @header($header);
 | 
				
			||||||
| 
						 | 
					@ -88,64 +88,70 @@ if($formatUrl){
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
class Cache
 | 
					class Cache
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    public $dir = 'cache'; //图标缓存目录
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public $hash_key = 'iowen'; // 哈希密钥
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function __construct($hash_key, $dir = 'cache')
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->hash_key = $hash_key;
 | 
				
			||||||
 | 
					        $this->dir      = $dir;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 获取缓存的值, 不存在时返回 null
 | 
					     * 获取缓存的值, 不存在时返回 null
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param $key
 | 
					     * @param string $key      缓存键(URL)
 | 
				
			||||||
     * @param $default  默认图片
 | 
					     * @param string $default  默认图片
 | 
				
			||||||
     * @param $expire   过期时间
 | 
					     * @param int    $expire   过期时间
 | 
				
			||||||
     * @return string
 | 
					     * @return mixed
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static function get($key, $default, $expire)
 | 
					    public function get($key, $default, $expire)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $dir = 'cache'; //图标缓存目录
 | 
					        $host = strtolower(parse_url($key)['host']);
 | 
				
			||||||
 | 
					        $hash = substr(hash_hmac('sha256', $host, $this->hash_key), 8, 16);
 | 
				
			||||||
 | 
					        $f    = $host . '_' . $hash . '.txt';
 | 
				
			||||||
 | 
					        $path = $this->dir . '/' . $f;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //$f = md5( strtolower( $key ) );
 | 
					        if (is_file($path)) {
 | 
				
			||||||
        $f = parse_url($key)['host'];
 | 
					            $data = file_get_contents($path);
 | 
				
			||||||
 | 
					 | 
				
			||||||
        $a = $dir . '/' . $f . '.txt';
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if(is_file($a)){
 | 
					 | 
				
			||||||
            $data = file_get_contents($a);
 | 
					 | 
				
			||||||
            if (md5($data) == $default) {
 | 
					            if (md5($data) == $default) {
 | 
				
			||||||
                $expire = 43200; //如果返回默认图标,过期时间为12小时。
 | 
					                $expire = 43200; //如果返回默认图标,过期时间为12小时。
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            if( (time() - filemtime($a)) > $expire ){
 | 
					            if ((time() - filemtime($path)) > $expire) {
 | 
				
			||||||
                return null;
 | 
					                return null;
 | 
				
			||||||
            }
 | 
					            } else {
 | 
				
			||||||
            else{
 | 
					 | 
				
			||||||
                return $data;
 | 
					                return $data;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
		}
 | 
					        } else {
 | 
				
			||||||
        else{
 | 
					 | 
				
			||||||
            return null;
 | 
					            return null;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * 设置缓存
 | 
					     * 设置缓存
 | 
				
			||||||
 | 
					     * 保存图标到缓存目录
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
     * @param $key
 | 
					     * @param string $key      缓存键(URL)
 | 
				
			||||||
     * @param $value
 | 
					     * @param string $value    缓存值(图标)
 | 
				
			||||||
     * @param $expire   过期时间
 | 
					 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    public static function set($key, $value, $expire)
 | 
					    public function set($key, $value)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $dir = 'cache'; //图标缓存目录
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        //$f = md5( strtolower( $key ) );
 | 
					 | 
				
			||||||
        $f = parse_url($key)['host'];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        $a = $dir . '/' . $f . '.txt';
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        //如果缓存目录不存在则创建
 | 
					        //如果缓存目录不存在则创建
 | 
				
			||||||
        if (!is_dir($dir)) mkdir($dir,0777,true) or die('创建缓存目录失败!');
 | 
					        if (!is_dir($this->dir)) {
 | 
				
			||||||
 | 
					            mkdir($this->dir, 0755, true) or die('创建缓存目录失败!');
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if ( !is_file($a) || (time() - filemtime($a)) > $expire ) {
 | 
					        $host = strtolower(parse_url($key)['host']);
 | 
				
			||||||
            $imgdata = fopen($a, "w") or die("Unable to open file!");  //w  重写  a追加
 | 
					        $hash = substr(hash_hmac('sha256', $host, $this->hash_key), 8, 16);
 | 
				
			||||||
 | 
					        $f    = $host . '_' . $hash . '.txt';
 | 
				
			||||||
 | 
					        $path = $this->dir . '/' . $f;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $imgdata = fopen($path, "w") or die("Unable to open file!");
 | 
				
			||||||
 | 
					        if (flock($imgdata, LOCK_EX)) {  // 获取排他锁
 | 
				
			||||||
            fwrite($imgdata, $value);
 | 
					            fwrite($imgdata, $value);
 | 
				
			||||||
 | 
					            flock($imgdata, LOCK_UN);  // 释放锁
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        fclose($imgdata);
 | 
					        fclose($imgdata);
 | 
				
			||||||
            clearstatcache();
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue