Skip to content

Instantly share code, notes, and snippets.

@rhenium
Created September 25, 2011 12:39
Show Gist options
  • Save rhenium/1240570 to your computer and use it in GitHub Desktop.
Save rhenium/1240570 to your computer and use it in GitHub Desktop.
<?php
//--rev 6
//--request
//----GET ?count=$count(long int)
//----POST ?name=$name(string)&point=$point(long int)
//--response
//----body エラー401、200番台であれば結果が返ってきます。402は読み込みエラーなので
//---- 結果はタブ区切り、UTF-8です。1行目はランキングじゃないのでー
//UTF-8使用しています。URLエンコードしてからリクエストすること。
header("Content-type: text/plain");
//入力チェック
if(isset($_POST["name"]) && isset($_POST["point"]) && is_numeric($_POST["point"])){
echo "200\n";
//XSS対策
$name = htmlspecialchars($_POST["name"], ENT_QUOTES);
$point = htmlspecialchars($_POST["point"], ENT_QUOTES);
$fl = fopen("data.csv", "a+") or die("401\tファイルに書き込めません");
flock($fl, LOCK_SH);
fputcsv($fl, array($name, $point)) or die("401\tファイルに書き込めません");
flock($fl, LOCK_UN);
fclose($fl);
}else{
echo "201\n";
}
$fw = fopen("data.csv", "r") or die("402\tファイルを読み込めません");
while(($line = fgetcsv($fw)) !== FALSE){
$lines[] = array($line[0], $line[1]);
}
fclose($fw);
foreach ($lines as $item) {
$sizes[] = $item[1];
}
$sorted = $lines;
array_multisort($sizes, SORT_DESC, SORT_REGULAR,
$sorted, SORT_DESC, SORT_REGULAR);
$count = min(10, count($lines));
if(is_numeric($_GET["count"])){
$count = min($_GET["count"], count($lines));
}elseif($_GET["count"] === "all"){
$count = count($lines);
}
for($i = 0; $i < $count; $i++){
echo join($sorted[$i], "\t") . "\n";
}
//わざと閉じてない
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment