Hi Bawolff,
Thanks for your help - I was able to make some progress: I rewrote the php script to write a cookie file and then login using action=clientlogin which thankfully returned the following response:
{ "clientlogin": { "status": "PASS", "username": "Admin" }}
However, I think I am still missing something: once this login was successful I was expecting in the same browser to be able to open a mediawiki page without logging in, however the main page is still showing "not logged in".
I also noticed that after the script successfully logged in, there was nothing listed in the browsers "Storage/Cookies" list under the domain.
Is this a case of me not understanding how cookies work?
Any help appreciated.
Dennis
EDIT: or will I need to programmatically open the wiki page from php using something like Headers() after using setcookies() to set the cookies?
EDIT#2:
OK so a little further progress:
I logged in as normal directly through the wiki main page to determine cookie behavior and saw that 3 cookies are created:
1) session cookie containing a token
2) username cookie
3) user ID cookie
I then added code to the php script which replicates these exact cookies after the login is completed using the newly acquired token to create the session cookie.
What I notice is that I can run my php script and see the 3 cookies get created and when the script echos the page, it shows as logged in.
However the moment I click on a link to go to a different wiki page, the user ID and session cookies disappear and only the user ID cookie remains "in the browser" and the page logs out.
So it seems I am close, but the elusive part is how to get those cookies to persist so that the session stays logged in?
php:
<?php
$cookie_jar = tempnam('/volume1/web/cookies','cookie');
//retrieve token
$c = curl_init('http://mywikisite/api.php?action=query&meta=tokens&type=login&format=json');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
$page = curl_exec($c);
$logintoken_array = json_decode($page);
$logintoken = $logintoken_array->query->tokens->logintoken;
echo $logintoken;
curl_close($c);
//log in
$c = curl_init('http://mywikisite/api.php');
$post = [
'action' => 'clientlogin',
'password' => 'xxxxxxxxx',
'username' => 'admin',
'logintoken' => $logintoken,
'loginreturnurl' => 'http://1mywikisite/index.php'
];
$c = curl_init('http://mywikisite/tng/wiki/api.php');
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, $post);
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
$page = curl_exec($c);
curl_close($c);
//create 3 cookies
$cookie_name = "tng_upgrade_12_3_wiki__session";
$cookie_value = $logintoken;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
$cookie_name = "tng_upgrade_12_3_wiki_UserID";
$cookie_value = "1";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
$cookie_name = "tng_upgrade_12_3_wiki_UserName";
$cookie_value = "Admin";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
//open wiki
$c = curl_init('http://mywikisite/index.php?title=Main_Page');
curl_setopt($c, CURLOPT_HEADER, 0);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($c, CURLOPT_COOKIEJAR, $cookie_jar);
$page = curl_exec($c);
echo $page;
curl_close($c);
?>