|
<?php |
|
$show_api = "/api/ap/show"; |
|
$instance = "mi.okin-jp.net"; |
|
|
|
// クエリからuriを取得 |
|
$uri = isset($_GET['uri']) ? $_GET['uri'] : ""; |
|
// ログインユーザーのトークンを取得 |
|
$authi = isset($_COOKIE['token']) ? $_COOKIE['token'] : ""; |
|
// エラーハンドリング |
|
if ($authi == "" || $uri == "") { |
|
if ($authi == "") { |
|
echo "ログインしてください"; |
|
} elseif ($uri == "") { |
|
echo "uriが不足しています"; |
|
} |
|
return; |
|
} |
|
// /api/ap/show を使って連合に照会 |
|
$data = json_decode(curl_wrap("https://" . $instance . $show_uri, ["i" => $authi, "uri" => $uri])); |
|
// Mastodonではエラーにする部分(AP上で照会できないため) |
|
// 以下の処理ではURL先の情報をとり共有用の投稿画面に飛ばす(個人的な趣味) |
|
if (isset($data->error)) { |
|
// リンクのタイトルを取得 |
|
$url_data = json_decode(curl_wrap("https://{$instance}/url?url=" . urlencode($uri) . "&lang=ja-JP", "", "GET")); |
|
if (isset($url_data->title)) { |
|
$url_title = $url_data->title; |
|
} else { |
|
$url_title = ""; |
|
} |
|
// 共有用の投稿ページのリンクを作り転送 |
|
$return_url = "https://{$instance}/share?text=" . urlencode("{$url_title} {$uri}"); |
|
header("Location: {$return_url}"); |
|
return; |
|
} |
|
// Mastodonのauthorize_interaction相当の処理 |
|
if (json_last_error() === JSON_ERROR_NONE) { |
|
if (isset($data->type)) { |
|
$id = isset($data->object->id) ? $data->object->id : ""; |
|
if ($data->type == "User" && isset($data->object->username) && isset($data->object->host)) { |
|
// ユーザーを見つけたとき |
|
$username = $data->object->username; |
|
$host = $data->object->host; |
|
$return_url = "https://{$instance}/@{$username}@{$host}"; |
|
} else if ($data->type == "Note" && $id != "") { |
|
// ノート(投稿)を見つけたとき |
|
$return_url = "https://{$instance}/notes/{$id}"; |
|
} else { |
|
// JSONに期待するデータが無いとき |
|
header('HTTP/1.1 500 Internal Server Error'); |
|
echo "不正なデータを取得しました"; |
|
return; |
|
} |
|
header("Location: {$return_url}"); |
|
return; |
|
} |
|
} else { |
|
// header('HTTP/1.1 500 Internal Server Error'); |
|
// 連合で照会出来なかったとき (共有へ飛ばす処理を削除したとき有効) |
|
echo "お探しのは見つかりませんでした。"; |
|
return; |
|
} |
|
// Mastodonのauthorize_interaction相当の処理 おわり |
|
|
|
// curlに渡すパラメータを事前定義しているラッパー |
|
function curl_wrap($url, $postfields, $method = 'POST') |
|
{ |
|
$ch = curl_init(); |
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postfields)); |
|
$data = curl_exec($ch); |
|
curl_close($ch); |
|
return $data; |
|
} |