mediawikiを自分のウェブサイトに統合しようとしていますが、問題があります。私はmediawikiAPIから成功を得ているので、問題はCookieに関係していると思います。
これが私のコードです:
function mw_session_manager($Action = "")
{
$Root = $_SERVER['SERVER_ADDR'];
$API_Location = "${Root}/w/api.php";
$expire = 60*60*24*14 + time();
$CookieFilePath = tempnam("/tmp/thedirectory", "CURLCOOKIE");
$CookiePrefix = 'theprefix';
$Domain = 'thedomain';
if($Action == 'login')
{
// Retrieves email address and password from sign-in form
$Email = $_POST['LogInEmail'];
$LgPassword = $_POST['LogInPassword'];
// Query to retrieve username from database based on email. It is implied that authentication has already succeeded.
$Query = "SELECT Username FROM Accounts WHERE Email = '$Email'";
$ResultSet = mysql_query($Query);
$ResultArray = mysql_fetch_array($ResultSet);
$LgName = $ResultArray[0]; // Username
// set variables to use in curl_setopts
$PostFields = "action=login&lgname=$LgName&lgpassword=$LgPassword&format=php";
// first http post to sign in to MediaWiki
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_POSTFIELDS, "$PostFields");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ResultSerialized = curl_exec($ch);
curl_close($ch); // curl closed
$ResultUnserialized = unserialize($ResultSerialized);
$Token = $ResultUnserialized[login][token];
// cookie must be set using session id from first response
$WikiSessionID = $ResultUnserialized[login][sessionid];
setcookie("${CookiePrefix}_session", $WikiSessionID, $expire, '/', $Domain);
// second http post to finish sign in
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_POSTFIELDS, "action=login&lgname=${LgName}&lgpassword=${LgPassword}&lgtoken=${Token}&format=php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ResultSerialized = curl_exec($ch);
curl_close($ch); // curl closed
$ResultUnserialized = unserialize($ResultSerialized);
// set persistent cookies
$LgToken = $ResultUnserialized["login"]["lgtoken"];
$LgUserID = $ResultUnserialized["login"]["lguserid"];
$LgUserName = $ResultUnserialized["login"]["lgusername"];
setcookie("${CookiePrefix}UserName", $LgUserName, $expire, '/', $Domain);
setcookie("${CookiePrefix}UserID", $LgUserID, $expire, '/', $Domain);
setcookie("${CookiePrefix}Token", $LgToken, $expire, '/', $Domain);
// Delete cURL cookie
unlink($CookieFilePath);
return $somedebuggingvariable;
}
if($Action = "logout")
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$API_Location");
curl_setopt($ch, CURLOPT_POSTFIELDS, "action=logout");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFilePath);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFilePath);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ResultSerialized = curl_exec($ch);
$LogoutReturn = unserialize($ResultSerialized);
$_SESSION['APIReturn'] = $LogoutReturn;
curl_close($ch); // curl closed
// destroys persistent cookies and ends session
$expire = time() - 60*60*24*90;
setcookie('Session', '', $expire, '/', $Domain);
setcookie("${CookiePrefix}_session", '', $expire, '/', $Domain);
setcookie("${CookiePrefix}UserName", '', $expire, '/', $Domain);
setcookie("${CookiePrefix}UserID", '', $expire, '/', $Domain);
setcookie("${CookiePrefix}Token", '', $expire, '/', $Domain);
// delete cURL cookie
unlink($CookieFilePath);
}
}
また、不正なトークンを指定してもAPIは成功を返すため、それを否定することもできません。
編集:私はそれが完全に機能するようになり、コードを現在の機能するコードに更新しました。