001 <?php 002 $_VARIABLE{'BaseURL'} = "http://www.(userdomain)/"; 003 $_VARIABLE{'BasePath'} = "/home/(username)/"; 004 005 $_VARIABLE{'SiteMapPath'} = "/home/(username)/blog/sitemaps.xml"; 006 007 $_VARIABLE{'DummyImageURL'} = "http://www.(userdomain)/blog/image/transparent.gif"; 008 $_VARIABLE{'MinWidth'} = 200; 009 $_VARIABLE{'MinHeight'} = 200; 010 011 function ExtractSiteLinks($FileName) { 012 global $_VARIABLE; 013 014 if(!$FileHandle = fopen($FileName, "r")) { 015 die("fopen(ERROR): $FileName"); 016 } 017 018 while($Line = fgets($FileHandle)) { 019 if(preg_match('/<loc>(.+)<\/loc>/i', $Line, $Match)) { 020 $Match[1] = preg_replace('/\/$/', '/index.php', $Match[1]); 021 $SiteLinks[] = $Match[1]; 022 } 023 } 024 025 fclose($FileHandle); 026 027 return $SiteLinks; 028 } 029 030 function ChangeImageTags($Subject) { 031 global $_VARIABLE; 032 033 preg_match_all("/(\S+)=\"([^\"]*)\"/", $Subject[1], $Matches, PREG_SET_ORDER); 034 035 foreach($Matches as $Match) { 036 switch($Match[1]) { 037 case "src": 038 $Src = $Match[2]; 039 break; 040 case "width": 041 $Width = $Match[2]; 042 break; 043 case "height": 044 $Height = $Match[2]; 045 break; 046 } 047 } 048 049 if(($Width > $_VARIABLE{'MinWidth'}) && ($Height > $_VARIABLE{'MinHeight'})) { 050 $String = sprintf("<img class=\"lazy\" src=\"%s\" data-original=\"%s\" width=\"%d\" height=\"%d\" alt=\"%s\" />", $_VARIABLE{'DummyImageURL'}, $Src, $Width, $Height, basename($Src)); 051 } else { 052 $String = $Subject[0]; 053 } 054 return $String; 055 } 056 057 // Main 058 $SiteLinks = ExtractSiteLinks($_VARIABLE{'SiteMapPath'}); 059 printf("<div>Found %d Links.</div>\n", count($SiteLinks)); 060 061 $Cnt = 0; 062 foreach ($SiteLinks as $Sitelink) { 063 $FileName = preg_replace("/" . preg_quote($_VARIABLE{'BaseURL'}, '/') . "/", $_VARIABLE{'BasePath'}, $Sitelink); 064 065 if(file_exists($FileName)) { 066 $Ext = pathinfo($FileName, PATHINFO_EXTENSION); 067 $BackFileName = preg_replace("/$Ext$/", "bak", $FileName); 068 069 if(!file_exists($BackFileName) || (filemtime($BackFileName) < filemtime($FileName))) { 070 if(!rename($FileName, $BackFileName)) { 071 printf("<div>rename(ERROR): [%s] to [%s]</div>\n", $FileName, $BackFileName); 072 continue; 073 } 074 075 if(!$BackFileHandle = fopen($BackFileName, "r")) { 076 printf("<div>fopen(ERROR): [%s]</div>\n", $BackFileName); 077 continue; 078 } 079 080 $BackFileSize = filesize($BackFileName); 081 $Content = fread($BackFileHandle, $BackFileSize); 082 083 fclose($BackFileHandle); 084 085 $Content = preg_replace_callback("/<img ([^>]*)>/", "ChangeImageTags", $Content); 086 087 $Content = mb_ereg_replace("/\*\x20.*?\x20\*/", "", $Content, "m"); 088 $Content = mb_ereg_replace("//\x20.*?\n", "", $Content); 089 $Content = mb_ereg_replace("#\x20.*?\n", "", $Content); 090 091 $Content = mb_ereg_replace("[\t\n]+", "\x20", $Content); 092 $Content = mb_ereg_replace("\x20+", "\x20", $Content); 093 $Content = mb_ereg_replace(">\x20<", "><", $Content); 094 095 if(!$FileHandle = fopen($FileName, "w")) { 096 printf("<div>fopen(ERROR): [%s]</div>\n", $FileName); 097 continue; 098 } 099 100 $FileSize = fwrite($FileHandle, $Content); 101 102 fclose($FileHandle); 103 104 touch($BackFileName, filemtime($FileName)); 105 106 printf("<div>Compress [%s] %d to %d (%.1f%%)</div>\n", $FileName, $BackFileSize, $FileSize, ($FileSize / $BackFileSize) * 100.0); 107 108 $Cnt++; 109 } 110 } 111 } 112 printf("<div>Compressed %d Links.</div>\n", $Cnt); 113 ?>