<?php
/*
    EZgetPHP ver1.1.2
    作者:まおう
    http://ezweb.s35.xrea.com/
    詳しくはHPを読んでください。
*/

// _/_/_/_/_/_/  設定  _/_/_/_/_/_/

// データのあるディレクトリ
define('DF''./file/');

// EZweb以外からのアクセス制限(0:しない 1:DL領域のみ 2:すべて)
define('OE''0');

// XHTML OBJECTダウンロード領域(0:使わない 1:使う)
define('XD''0');

// ファイルリストモード(0:使わない 1:使う)
define('FL''0');

// カシオ/日立アイコン(png)相互変換機能(0:使わない 1:使う)
define('CI''1');

// CRCのキャッシュ機能(0:使わない 1:使う)
define('CS''1');

// _/_/_/_/_/_/  変更の必要がない設定  _/_/_/_/_/_/

/* <対応しているファイル>
L1 すべてCRC付加ダウンロード
L2 XHTML対応機種のみダウンロード可能
L3 XHTML対応機種の場合OBJECTダウンロードを使用
*/
define('EX',
  
'bmp,jpg,gif,png,wnn,pdl,sli,scl,ck4,ck3,ck2,clk|'.
  
'amc,3g2,3gp,kmf,swf,tar,aad,pbx,hvs,m3f,mqf,ys1,di1|'.
  
'mmf,pmd,qcp,kjx,vcf,vcs,k3d'
);

// EZweb機種確認
define('EZ'preg_match("/\.(ezweb|ido|brew)\.ne\.jp$/i"gethostbyaddr($_SERVER['REMOTE_ADDR'])));
//define('EZ', preg_match("/\.(ezweb|ido|brew)\.ne\.jp$/i", $_SERVER['REMOTE_HOST']));

// _/_/_/_/_/_/  以下から処理開始  _/_/_/_/_/_/

if(OE == && !EZTemp::err('EZweb以外は禁止');

// ファイル情報表示
if($_GET['f']){
  new 
Info();

// CRC付加ダウンロード領域
}elseif(!empty($_GET['name']) && isset($_GET['count']) && isset($_GET['offset'])){
  new 
Download();

//XHTML OBJECTダウンロード領域
}elseif(XD && !empty($_GET['name'])
  && 
preg_match("/^[A-z\d\/\-]+$/"$_GET['m'])
  && 
preg_match("/^\d+$/"$_GET['s'])){
  new 
Download(1);

// ファイルリスト
}elseif(FL){
  
Tool::FileList();

// エラー
}else{
  
Temp::err('ファイル名を指定してください');
}


// _/_/_/_/_/_/  クラス  _/_/_/_/_/_/

// テンプレート
class Temp {

  
// ヘッダー
  
function Head($title 'ダウンロード') {
    
header('Content-type: text/html;charset=Shift_JIS');
    echo 
'<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS" />
<title>'
.$title.'</title>
<meta http-equiv="Cache-Control" content="no-cache" />
<meta name="vnd.up.markable" content="false" />
<do type="accept"><noop /></do>
</head><body>'
;
  }

  
// フッター
  
function Foot() {
    die(
'</body></html>');
  }

  
// エラーメッセージ
  
function err($mes){
    
header('Content-type: text/plain;charset=Shift_JIS');
    die(
$mes);
  }

}

// 情報表示
class Info extends Temp {
  var 
$info = array();

  function 
Info() {

    if(
preg_match("/\.([A-z\d]+)$/"$_GET['f'], $ext)){
      
$ext strtolower($ext[1]);
    }else 
$this->err('拡張子がありません');
    if(!
preg_match("/".$ext."/"EX)) $this->err('対応したファイルではありません');
    
$this->info['path'] = DF.$_GET['f'];
    
$size = @filesize($this->info['path']) or $this->err('ファイルが見つかりません');
    
$title rawurldecode($_GET['t'] ? $_GET['t'] : basename($_GET['f']));

    list(,
$XH$XH2) = explode("|"EX);

    
// XHTML対応チェック
    
if(preg_match("/^KDDI\-/"$_SERVER['HTTP_USER_AGENT'])
      && 
$_SERVER['HTTP_X_UP_DEVCAP_MULTIMEDIA']{6}
      && 
preg_match("/".$ext."/"$XH.$XH2)){
      
$this->info['xhdl'] = 1;

    }else{
// 非対応
      
if(EZ && !preg_match("/".$ext."/"$XH)){

        
// CRC付加チェック用データ取得
        
$fp fopen($this->info['path'], 'rb');
        
$this->data fread($fp$size);
        
fclose($fp);

        
// カシオアイコン相互変換チェック
        
if(CI && $ext == 'png' && Download::pngcrc()) $size strlen($this->data);

        
// CRC付加チェック
        
if(Download::crc()) $size += 2;

        
// キャッシュ
        
if(CS){
          
setcookie('crc',
            
$_GET['f'].'/'.bin2hex($this->info['crc']),
            
time()+86400);
        }

      }else 
$this->info['xhdl'] = 0;
    }

    
// 解析用データ取得
    
if(!$this->data){
      
$fp fopen($this->info['path'], 'rb');
      
$this->data fread($fp512);
      if(
preg_match("/".$ext."/"'3g2,3gp,amc') && $moov strpos($this->data'moov')){
        
rewind($fp);
        
$this->data fread($fphexdec(bin2hex(substr($this->data$moov-44)))+$moov);
      }elseif(
$ext == 'swf'){
        
rewind($fp);
        
$this->data fread($fp307200);
      }
      
fclose($fp);
    }else 
$this->data substr($this->data0512);

    
// 情報取得
    
if(preg_match("/^\d/"$ext)) $ext 'x'.$ext;
    
$this->info['oth'] = '';
    
FileInfo::$ext();

    
// 解析用データ破棄
    
unset($this->data);

    
// 情報強制指定
    
if(preg_match("/^[A-z\d]+$/"$_GET['d'])) $this->info['dis'] = $_GET['d'];
    if(
$this->info['xhdl'] && preg_match("/^[A-z\d\/\-]+$/"$_GET['m'])) $this->info['mine'] = $_GET['m'];
    if(
$_GET['k']) $this->info['kind'] = htmlspecialchars(rawurldecode($_GET['k']));

    
// ヘッダー
    
$this->Head();

    
// 情報の表示
    
echo '
<div mode="nowrap">名前:'
.htmlspecialchars($title).'<br>
種類:'
.$this->info['kind'].'<br>'.$this->info['oth'].'
容量:'
.Tool::mosize($size).'<br>
更新:'
.date('y/m/d'filemtime($this->info['path'])).'<br>
</div>'
;

    if(
EZ){
      
// 半角スペース( )と半角プラス(+)対策?
      
$file rawurlencode(rawurldecode($_GET['f']));

      
// OBJECT DL
      
if($this->info['xhdl']){
        if(
XD$url '?name='.$file.'&m='.rawurlencode($this->info['mine']).'&s='.$size.'&';
        else 
$url DF.$file;

        if(
$_SERVER['HTTP_X_UP_DEVCAP_NONATTACHMENT'])
          echo 
'添付データを再生するに設定してください<br>';

        echo 
'
<object data="'
.$url.'" type="'.$this->info['mine'].'" copyright="no" standby="ダウンロード">
<param name="disposition" value="'
.$this->info['dis'].'" valuetype="data" />
<param name="size" value="'
.$size.'" valuetype="data" />
<param name="title" value="'
.rawurlencode($title).'" valuetype="data" />
<param name="ac" value="" valuetype="data" />
</object>'
;

      }elseif(
$this->info['xhdl'] === 0){
        echo 
'非対応ファイル';

      }else{ 
// EZGET
        // スクリプトまでのURL
        
$url 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
        
//$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
        //$url =  = '';    // 直接入力
        
echo '<wml:anchor label="DL"><wml:spawn href="device:data/dnld?url='.$url.'&name='.$file.'&size='.$size.'&disposition='.$this->info['dis'].'&title='.rawurlencode($title).'" />ダウンロード</wml:anchor>';

      }

    }elseif(!
OE){

      
// FLASHの表示
      
if($ext == 'swf'){
        
$wh getimagesize($this->info['path']);
        echo 
'
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
    codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"
    width="'
.$wh[0].'" height="'.$wh[1].'">
    <param name="movie" value="'
.$this->info['path'].'" />
    <param name="quality" value="high" />
    <embed src="'
.$this->info['path'].'" quality="high" width="'.$wh[0].'" height="'.$wh[1].'"
         name="'
.htmlspecialchars($title).'" type="application/x-shockwave-flash"
         pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object><hr>
'
;
      }
      echo 
'<a href="'.$this->info['path'].'">ダウンロード</a>';

    }else{
      echo 
'EZweb以外はダウンロードできません';

    }

    
// フッター
    
$this->Foot();
  }

}


// ツール
class Tool {

  
// ファイルリスト
  
function FileList() {
    
Temp::Head('ファイルリスト');
    if(
$dir = @dir(DF.$_GET['d'])){
      while((
$name $dir->read()) !== false){
        if(!
preg_match("/^[\.]+$/"$name)){
          if(
is_dir(DF.$name)) $d[] = $name.'/';
          elseif(
preg_match("/".substr($name, -3)."/i"EX))
            
$f[] = $name;
        }
      }
      
$dir->close();
      
$len count($d);
      
$i 0;
      while(
$i $len){
        echo 
'<a href="?d='.$_GET['d'].rawurlencode($d[$i]).'">./'.$d[$i].'</a><br>';
        
$i++;
      }
      if(
$len) echo '<hr>';
      
$len count($f);
      
$i 0;
      while(
$i $len){
        echo 
'<a href="?f='.$_GET['d'].rawurlencode($f[$i]).'">'.$f[$i].'</a><br>';
        
$i++;
      }
    }else echo 
DF.'が存在しません';
    
Temp::Foot();
  }

  
// 最適サイズ表示
  
function mosize($size) {
    
$tan = array('byte','KB','MB','GB');
    
$si floor($size/1024);
    
$i 0;
    while(
$si != 0){
      if(
$i++ > 2) break;
      
$si floor($si/1024);
    }
    return 
round($size/pow(1024$i), $i).$tan[$i];
  }

}

// 拡張子別情報取得
class FileInfo {

  
// BMP画像
  
function bmp() {
    
$this->info['kind'] = 'BMP画像';
    
$wh getimagesize($this->info['path']);
    
$this->info['oth'] = '横縦:'.$wh[0].'*'.$wh[1].'<br>';
    
$this->info['dis'] = 'devabm';
  }

  
// JPEG画像
  
function jpg() {
    
$this->info['kind'] = 'JPEG画像';
    
$wh getimagesize($this->info['path']);
    
$this->info['oth'] = '横縦:'.$wh[0].'*'.$wh[1].'<br>';
    
$this->info['dis'] = 'devjaww';
  }

  
// GIF画像
  
function gif() {
    if(
preg_match("/NETSCAPE/i"$this->data)){
      
$this->info['dis'] = "devga2z";
      
$this->info['kind'] = "GIFアニメ";
    }else{
      
$this->info['dis'] = "devgi0z";
      
$this->info['kind'] = 'GIF画像';
    }
    
$wh getimagesize($this->info['path']);
    
$this->info['oth'] = '横縦:'.$wh[0].'*'.$wh[1].'<br>';
  }

  
// PNG画像
  
function png() {
    
$wh getimagesize($this->info['path']);
    
$this->info['oth'] = '横縦:'.$wh[0].'*'.$wh[1].'<br>';
    if((
$wh[0] == 162 && $wh[1] == 248) || ($wh[0] == 519 && $wh[1] == 371)){
      
preg_match("/tEXtDescription\\x00\-ICN([A-z\d]+)\-a02/i"$this->data$ki);
      
$this->info['dis'] = 'devicna';
      if(
$ki[1] == 'W31H'$ki[1] = 'PENCK';
      elseif(!
$ki[1]) $ki[1] = 'カシオ/日立';
      
$this->info['kind'] = $ki[1].'アイコン';
    }elseif(
preg_match("/tEXtDescription\\x00KDFRAMAU/i"$this->data)){
      
$this->info['dis'] = 'devfr0i';
      
$this->info['kind'] = 'フレーム';
    }elseif(
preg_match("/tEXtDescription\\x00KDSTMPAU/i"$this->data)){
      
$this->info['dis'] = 'devst0j';
      
$this->info['kind'] = 'スタンプ';
    }else{
      
$this->info['dis'] = 'dev8aww';
      
$this->info['kind'] = 'PNG画像';
    }
  }

  
// 旧EZムービー,着うた
  
function amc() {
    
$this->info['dis'] = 'devdl1q';
    
$this->info['mine'] = 'application/x-mpeg';
    if(
strlen($this->data) === 512) return $this->info['kind'] = '?AMC';
    if(
$vide strpos($this->data'vide')){
      
$stsd strpos($this->data'stsd'$vide)+45;
      
$video '横縦:'.ord($this->data{$stsd}).'*'.ord($this->data{$stsd+2}).'<br>';
    }
    if(
$soun strpos($this->data'soun')){
      if(
strpos($this->data'Qcelp'$soun)) $sound 'QCP';
      else 
$sound 'MP3';
    }else{
      
$this->info['dis'] = 'devdk1n';
    }
    if(!
$video){
      if(
$sound == 'MP3'){
        
$this->info['kind'] = '着うた';
        
$this->info['dis'] = 'devdl1m';
      }else{
        
$this->info['kind'] = 'ボイス';
        if(
preg_match("/^KDDI\-SN23/i"$_SERVER['HTTP_USER_AGENT'])) $this->info['dis'] = 'devdl2bq';
        else 
$this->info['dis'] = 'devdl1bq';
      }
    }else{
      if(
$sound == 'MP3'$this->info['kind'] = '着ムービー';
      else 
$this->info['kind'] = 'ムービー';
    }
    if(
$sound$sound '音声:'.$sound.'<br>';
    
$this->info['oth'] = $video.$sound;
  }

  
// EZムービー,着うた
  
function x3g2($key 1) {
    
$this->info['dis'] = 'devmpzz';
    if(
$vide strpos($this->data'vide')){
      if(
$key$this->info['mine'] = 'video/3gpp2';
      else 
$this->info['mine'] = 'video/3gpp';
      
$stsd strpos($this->data'stsd'$vide)+45;
      
$video '横縦:'.ord($this->data{$stsd}).'*'.ord($this->data{$stsd+2}).'<br>';
    }else{
      if(
$key$this->info['mine'] = 'audio/3gpp2';
      else 
$this->info['mine'] = 'audio/3gpp';
    }
    if(
$soun strpos($this->data'soun')){
      if(
strpos($this->data'damrappl'$soun)) $sound 'AMR';
      elseif(
strpos($this->data'Qcelp'$soun)) $sound 'QCP';
      else 
$sound 'AAC';
    }else{
      
$this->info['dis'] = 'devdk1n';
    }
    if(!
$video){
      if(
$sound == 'AAC'$this->info['kind'] = '着うた';
      else 
$this->info['kind'] = 'ボイス';
    }else{
      if(
$sound == 'AAC'$this->info['kind'] = '着ムービー';
      else 
$this->info['kind'] = 'ムービー';
    }
    if(
strlen($this->data) === 512) return $this->info['kind'] = '?3GPP';
    if(
$sound$sound '音声:'.$sound.'<br>';
    
$this->info['oth'] = $video.$sound;
  }

  
// EZムービー,着うた
  
function x3gp() {
    
FileInfo::x3g2(0);// 3g2,3gp共通処理
  
}

  
// 着うたフル
  
function kmf() {
    
$this->info['dis']  = 'devcufz';
    
$this->info['mine'] = 'application/x-kddi-ezmusic';
    
$this->info['kind'] = '着うたフル';
  }
  
  
// EZアプリ(Java)
  
function kjx() {
    
$this->info['mine'] = 'application/x-kjx';
    
$this->info['kind'] = 'EZアプリ';
    if(
preg_match("/MicroEdition\-Profile:\sKDDIP\-(\d\.\d)/i"$this->data$api)){
      switch(
$api[1]){
        case 
'1.0':
          
$this->info['kind'] .= '(Phase1)';
          
$this->info['dis'] = 'devkdjx';
          break;
        case 
'2.0':
          
$this->info['kind'] .= '(Phase2)';
          
$this->info['dis'] = 'devkdj2';
          break;
        case 
'2.5':
          
$this->info['kind'] .= '(Phase2.5)';
          
$this->info['dis'] = 'devkdj2';
          break;
        case 
'3.0':
          
$this->info['kind'] .= '(Phase3)';
          
$this->info['dis'] = 'devkdj3';
          break;
        default:
          
$this->info['dis'] = 'devkdjx';
      }
    }else 
$this->info['dis'] = 'devkdjx';
  }

  
// ボイス
  
function qcp() {
    
$this->info['dis'] = 'dev13anq';
    
$this->info['mine'] = 'audio/vnd.qcelp';
    
$this->info['kind'] = 'ボイス(Qcelp)';
  }

  
// 着メロ(SMAF)
  
function mmf() {
    
$this->info['mine'] = 'application/x-smaf';
    
$this->info['kind'] = '着メロ(SMAF)';
    
$cnk 8;
    
$i 0;
    while(
$cnkn != 'MTR'){
      if(
$i++ > 4){ $i 0; break; }
      
$cnk += hexdec(bin2hex(substr($this->data$cnk+44))) + 8;
      
$cnkn substr($this->data$cnk3);
    }
    if(
$i){
      switch(
ord($this->data{$cnk+3})){
        case 
0:
          
$wo 4;
          
$this->info['dis'] = 'dev4anm';
          break;
        case 
1:
          
$wo 16;
          
$this->info['dis'] = 'devmfan';
          break;
        case 
5:
          
$wo 40;
          
$this->info['dis'] = 'devm39z';
          break;
        case 
6:
          
$wo 64;
          
$this->info['dis'] = 'devm53z';
          break;
        default:
          
$wo '?';
          
$this->info['dis'] = 'devmfan';
      }
      
$this->info['oth'] = '和音:'.$wo.'和音<br>';
    }else 
$this->info['dis'] = 'devmfan';
  }

  
// フォトミキサー
  
function m3f() {
    
FileInfo::mmf();
    
$this->info['oth'] = '';
    
$this->info['kind'] = "フォトミキサー";
  }

  
// フォトミキサー
  
function mqf() {
    
FileInfo::mmf();
    
$this->info['oth'] = '';
    
$this->info['kind'] = 'フォトミキサー';
  }

  
// 着メロ(C-MIDI)
  
function pmd() {
    
$this->info['mine'] = 'application/x-pmd';
    
$this->info['kind'] = '着メロ(C-MIDI)';
    if(
preg_match("/\\x76\\x65\\x72\\x73\\x00\\x040([1-2])00/"$this->data$ki)){
      switch(
$ki[1]){
        case 
1:
          
$this->info['dis'] = 'dev3anp';
          
$this->info['oth'] = '和音:3和音<br>';
          break;
        case 
2:
          
$this->info['dis'] = 'devcman';
          
$this->info['oth'] = '和音:16和音<br>';
          break;
      }
    }else 
$this->info['dis'] = 'devcman';
  }

  
// FLASH
  
function swf() {
    
$this->info['mine'] = 'application/x-shockwave-flash';
    
$wh getimagesize($this->info['path']);
    
$this->info['oth'] = '横縦:'.$wh[0].'*'.$wh[1].'<br>';
    if(
preg_match("/FSCommand:launch\\x00M\\x00/i"$this->data)
    && 
preg_match("/FSCommand:launch\\x00df\\x00/i"$this->data)){
      
// カシオ,日立アイコン
      
if(preg_match("/FSCommand:launch\\x00photo\\x00/i"$this->data)){
        
$this->info['dis'] = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 54)).'fl7k';
        if(
$wh[0] == 240 && $wh[1] == 400){
          if(!
preg_match("/FSCommand:launch\\x00enjoytool\\x00/i"$this->data))
            
$this->info['kind'] = 'W41H/W41CA';
          else 
$this->info['kind'] = 'W21CA/W31CA';
        }elseif(
$wh[0] == 240 && $wh[1] == 320){
          if(
preg_match("/FSCommand:launch\\x00fmradio\\x00/i"$this->data))
            
$this->info['kind'] = 'W32H';
          if(
preg_match("/FSCommand:launch\\x00eznavigation\\x00/i"$this->data))
            
$this->info['kind'] = 'A5512CA/G\'zOne';
          if(
preg_match("/FSCommand:launch\\x00memorycard\\x00/i"$this->data))
            
$this->info['kind'] = 'W22H/PENCK';
          else 
$this->info['kind'] = 'カシオ/日立';
        }else 
$this->info['kind'] = 'カシオ/日立';
        
$this->info['kind'] .= 'アイコン';
      
// 鳥取三洋アイコン
      
}elseif(preg_match("/FSCommand:launch\\x00camera\\x00/i"$this->data)
        && 
$wh[0] == 240 &&