if(!defined('ABSPATH')) exit; // ================= 配置 ================= define('WPIP_CACHE_DIR', WP_CONTENT_DIR.'/wpip-image-cache'); define('WPIP_CACHE_TTL', 604800); define('WPIP_MAP_FILE', WPIP_CACHE_DIR.'/map.json'); // ================= 初始化缓存目录 ================= register_activation_hook(__FILE__, function(){ if(!is_dir(WPIP_CACHE_DIR)) mkdir(WPIP_CACHE_DIR,0755,true); flush_rewrite_rules(); }); register_deactivation_hook(__FILE__, function(){ flush_rewrite_rules(); }); // ================= 工具函数 ================= function wpip_load_map(){ if(!file_exists(WPIP_MAP_FILE)) return []; return json_decode(file_get_contents(WPIP_MAP_FILE), true) ?: []; } function wpip_save_map($map){ file_put_contents(WPIP_MAP_FILE, json_encode($map, JSON_UNESCAPED_SLASHES)); } function wpip_get_ext($url){ $path = parse_url($url, PHP_URL_PATH); $ext = pathinfo($path, PATHINFO_EXTENSION); return strtolower($ext ?: 'jpg'); } function wpip_proxy_url($url){ if(strpos($url,'//')===0) $url='https:'.$url; if(!preg_match('#^https?://#i',$url)) return $url; $ext = wpip_get_ext($url); $hash = substr(md5($url),0,16); $map = wpip_load_map(); if(!isset($map[$hash])){ $map[$hash]=$url; if(count($map)>50000) $map=array_slice($map,-30000,null,true); wpip_save_map($map); } return home_url('/wpip-img/'.$hash.'.'.$ext); } function wpip_fetch_remote($url){ $opts = [ "http"=>[ "timeout"=>15, "header"=>"User-Agent: Mozilla/5.0\r\nReferer: $url\r\n" ] ]; $context = stream_context_create($opts); return @file_get_contents($url,false,$context) ?: false; } function wpip_get_mime($ext){ $map = [ 'jpg'=>'image/jpeg','jpeg'=>'image/jpeg', 'png'=>'image/png','gif'=>'image/gif', 'webp'=>'image/webp','svg'=>'image/svg+xml', 'bmp'=>'image/bmp','ico'=>'image/x-icon' ]; return $map[$ext] ?? 'application/octet-stream'; } // ================= rewrite & query_vars ================= add_action('init', function(){ add_rewrite_rule('^wpip-img/([a-f0-9]{16})\.(jpg|jpeg|png|gif|webp|svg)$', 'index.php?wpip_hash=$matches[1]&wpip_ext=$matches[2]', 'top'); }); add_filter('query_vars', function($vars){ $vars[]='wpip_hash'; $vars[]='wpip_ext'; return $vars; }); // ================= 图片请求处理 ================= add_action('template_redirect', function(){ $hash = get_query_var('wpip_hash'); $ext = get_query_var('wpip_ext'); if(!$hash || !$ext) return; $map = wpip_load_map(); if(!isset($map[$hash])){ status_header(404); exit('Image not found'); } $remote_url = $map[$hash]; $cache_file = WPIP_CACHE_DIR.'/'.$hash.'.'.$ext; if(!is_dir(WPIP_CACHE_DIR)) mkdir(WPIP_CACHE_DIR,0755,true); $lock_file = $cache_file.'.lock'; $lock = fopen($lock_file,'w+'); if($lock){ flock($lock,LOCK_EX); if(!file_exists($cache_file)){ $data = wpip_fetch_remote($remote_url); if($data) file_put_contents($cache_file,$data); } flock($lock,LOCK_UN); fclose($lock); @unlink($lock_file); } if(!file_exists($cache_file)){ status_header(404); exit('Not found'); } header('Content-Type: '.wpip_get_mime($ext)); header('Cache-Control: public, max-age=31536000'); readfile($cache_file); exit; }); // ================= HTML缓冲处理 ================= add_action('template_redirect', function(){ if(is_admin()) return; ob_start(function($html){ $html = preg_replace('//i','',$html); // img 标签替换 $html = preg_replace_callback('/]*>/i', function($match){ $tag = $match[0]; $tag = preg_replace_callback( '/(src|data-src|data-image|data-original|data-lazy)=["\']((https?:)?\/\/[^"\']+)["\']/i', function($m){ return 'src="'.wpip_proxy_url($m[2]).'"'; }, $tag ); $tag = preg_replace('/\sdata-[a-zA-Z0-9_-]+=["\'][^"\']*["\']/','',$tag); $tag = preg_replace('/\ssrcset=["\'][^"\']*["\']/','',$tag); return $tag; }, $html); // CSS 背景图 $html = preg_replace_callback( '/url\(["\']?(https?:)?\/\/([^"\')]+)["\']?\)/i', function($m){ return 'url("'.wpip_proxy_url('https://'.$m[2]).'")'; }, $html ); return $html; }); }); ?> Donation - PSBPN - Pracharya Shrimantrao Bhosale Pratishthan

Make A Donation

SHOW YOUR SUPPORT

Scan the QR code given below to donate money via G-Pay/Phone Pe/Paytm or UPI ID or use NGO’s UPI ID – pracharya60371167448@mahb

Scan to Donate
scanner photo

You can deposit directly into our Bank Account.
Please find below our Bank Details :

Account Name

Pracharya Shrimantrao Bhosale Pratishthan, Nira

Bank Name

Bank of Maharashtra, Nira Branch, District Pune

Current Account Number

60 37 11 67 448

IFSC Code

MAHB0000096

VALUABLE DONORS

The following ex-students and respected teachers have helped our foundation.

FEEDBACK