| 1 |
<?php |
| 2 |
/* |
| 3 |
iSlideManager.php |
| 4 |
|
| 5 |
iSlideMaker |
| 6 |
http://sourceforge.jp/projects/islidemaker/simple/ |
| 7 |
|
| 8 |
Copyright(c) 2010, Isao Hara, isao-hara@users.sourceforge.jp |
| 9 |
|
| 10 |
All rights reserved. This program is made available under the terms of the |
| 11 |
Eclipse Public License v1.0 which accompanies this distribution, and is |
| 12 |
available at http://www.eclipse.org/legal/epl-v10.html |
| 13 |
|
| 14 |
Contributors: Isao Hara. |
| 15 |
|
| 16 |
*/ |
| 17 |
$name = $_POST['name']; |
| 18 |
$cmd = $_POST['cmd']; |
| 19 |
$filetype = $_POST['filetype']; |
| 20 |
$content = $_POST['datalob']; |
| 21 |
|
| 22 |
$path = dirname($_SERVER['DOCUMENT_ROOT'].$_SERVER['PHP_SELF']); |
| 23 |
|
| 24 |
if(!$name){ |
| 25 |
echo "Filename required"; |
| 26 |
exit; |
| 27 |
} |
| 28 |
if($filetype == "svg"){ |
| 29 |
$dirname = $path."/SVG/"; |
| 30 |
$fname = $name.".svg"; |
| 31 |
}else{ |
| 32 |
$dirname = $path."/Slide/"; |
| 33 |
$fname = $name.".txt"; |
| 34 |
} |
| 35 |
|
| 36 |
if( $cmd=='get'){ |
| 37 |
$res = file_get_contents($dirname.$fname); |
| 38 |
echo $res; |
| 39 |
}else if($cmd=='list'){ |
| 40 |
$res = getFileList($dirname); |
| 41 |
echo implode(',',$res); |
| 42 |
}else if($cmd=='upload'){ |
| 43 |
$res = save_content($dirname.$fname, $content); |
| 44 |
if($res){ |
| 45 |
echo "Finish to upload."; |
| 46 |
}else{ |
| 47 |
echo "Fail to upload."; |
| 48 |
} |
| 49 |
}else if($cmd=='download'){ |
| 50 |
download_file($dirname.$fname, $fname, $filetype); |
| 51 |
exit(); |
| 52 |
}else { |
| 53 |
echo "No such command: $cmd.<br>"; |
| 54 |
echo "name: $name.<br>"; |
| 55 |
echo "filetype: $filetype.<br>"; |
| 56 |
print_r($_POST); |
| 57 |
} |
| 58 |
|
| 59 |
function save_content($filename, $content){ |
| 60 |
$res = false; |
| 61 |
$fp = fopen($filename, "w+"); |
| 62 |
if(! $fp){ return $res; } |
| 63 |
if(flock($fp, LOCK_EX)){ |
| 64 |
$res = fwrite($fp, $content); |
| 65 |
flock($fp,LOCK_UN); |
| 66 |
} |
| 67 |
fclose($fp); |
| 68 |
return $res; |
| 69 |
} |
| 70 |
|
| 71 |
function getFileList($dir){ |
| 72 |
$handle = opendir($dir); |
| 73 |
|
| 74 |
$filenames=array(); |
| 75 |
while(false !== ($file = readdir($handle))){ |
| 76 |
if( $file != "." && $file != ".." && !is_dir($dir."/".$file)){ |
| 77 |
array_push($filenames, $file); |
| 78 |
} |
| 79 |
} |
| 80 |
closedir($handle); |
| 81 |
return $filenames; |
| 82 |
} |
| 83 |
|
| 84 |
/////////////////////////////////////// |
| 85 |
// download file |
| 86 |
function download_file($file, $fname, $ftype){ |
| 87 |
if(!file_exists($file)) return 0; |
| 88 |
if(!($fp = fopen($file,"r"))) return 0; |
| 89 |
fclose($fp); |
| 90 |
|
| 91 |
if(($content_len = filesize($file)) == 0) return 0; |
| 92 |
|
| 93 |
|
| 94 |
header("Pragma: private"); |
| 95 |
header("Cache-control: private, must-revalidate"); |
| 96 |
|
| 97 |
$k_code="UTF-8"; |
| 98 |
|
| 99 |
if(chkBrowser() == "windows"){ |
| 100 |
$k_code="SJIS"; |
| 101 |
}else{ |
| 102 |
$k_code="UTF-8"; |
| 103 |
} |
| 104 |
|
| 105 |
if(chkBrowser() == "safari"){ |
| 106 |
header("Content-Disposition: attachment; filename=\"\""); |
| 107 |
}else if(chkBrowser() == "osx"){ |
| 108 |
header("Content-Disposition: attachment; filename=\"".mb_convert_encoding($fname,$k_code,"UTF-8,EUC-JP,SJIS")."\""); |
| 109 |
}else{ |
| 110 |
header("Content-Disposition: attachment; filename=\"".mb_convert_encoding($fname,$k_code,"SJIS,UTF-8,EUC-JP")."\""); |
| 111 |
} |
| 112 |
header("Content-Type: applicaion/octet-stream"); |
| 113 |
|
| 114 |
if($ftype == "svg"){ |
| 115 |
$content = file_get_contents($file); |
| 116 |
|
| 117 |
$contents =<<< _HTML |
| 118 |
<?xml version="1.0" ?> |
| 119 |
<svg:svg width="100%" height="100%" |
| 120 |
xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 121 |
$content |
| 122 |
</svg:svg> |
| 123 |
_HTML; |
| 124 |
|
| 125 |
$content_len = strlen($contents); |
| 126 |
header("Content-Length: ".$content_len); |
| 127 |
|
| 128 |
print $contents; |
| 129 |
|
| 130 |
}else if($ftype = "slide_html"){ |
| 131 |
|
| 132 |
header("Content-Length: ".$content_len); |
| 133 |
if(! readfile($file)) return 0; |
| 134 |
}else{ |
| 135 |
header("Content-Length: ".$content_len); |
| 136 |
if(! readfile($file)) return 0; |
| 137 |
} |
| 138 |
|
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
function chkBrowser(){ |
| 143 |
if (preg_match("/^DoCoMo/",$_SERVER['HTTP_USER_AGENT'])){ |
| 144 |
return "imode"; |
| 145 |
}else if(preg_match("/NetFront/",$_SERVER['HTTP_USER_AGENT'])){ |
| 146 |
return "netfront"; |
| 147 |
}else if(preg_match("/Windows/",$_SERVER['HTTP_USER_AGENT'])){ |
| 148 |
return "windows"; |
| 149 |
}else if(preg_match("/Mac OS X/",$_SERVER['HTTP_USER_AGENT'])){ |
| 150 |
return "osx"; |
| 151 |
}else if(preg_match("/Safari/",$_SERVER['HTTP_USER_AGENT'])){ |
| 152 |
return "safari"; |
| 153 |
}else return "unix"; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
?> |