This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
cms_auth_functions.php
155 lines (151 loc) · 5.15 KB
/
cms_auth_functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*
* This functions are necessary to implement the bridge between Pydio
* and other CMS's.
*/
/**
* @package AjaXplorer_Plugins
* @subpackage Auth
*
* @param HttpClient $client
* @return array
*/
function extractResponseCookies($client)
{
//AJXP_Logger::debug(print_r($client, true));
$cooks = $client->getHeader("set-cookie");
if(empty($cooks)) return array();
if (is_string($cooks)) {
$cooks = array($cooks);
}
$cookies = array();
foreach ($cooks as $cookieString) {
list($name,$value) = explode("=", $cookieString);
$ar = explode(";", $value);
$value = array_shift($ar);
$cookies[$name] = $value;
}
return $cookies;
}
function wordpress_remote_auth($host, $uri, $login, $pass, $formId = "")
{
$client = new HttpClient($host);
$client->setHandleRedirects(false);
$client->setHeadersOnly(true);
$res = $client->post($uri."/wp-login.php", array(
"log" => $login,
"pwd" => $pass,
"wp-submit" => "Log In",
"testcookie" => 1)
);
$newCookies = extractResponseCookies($client);
if (isSet($newCookies["AjaXplorer"])) {
return $newCookies;
}
return "";
}
function joomla_remote_auth($host, $uri, $login, $pass, $formId = "")
{
$client = new HttpClient($host);
$client->setHandleRedirects(false);
$res = $client->get($uri);
$content = $client->getContent();
$postData = array(
"username" => $login,
"password" => $pass,
"Submit" => "Log in",
"remember" => "yes"
);
$xmlDoc = @DOMDocument::loadHTML($content);
if ($xmlDoc === false) {
$pos1 = strpos($content, "<form ");
$pos2 = strpos($content, "</form>", $pos1);
$content = substr($content, $pos1, $pos2 + "7" - $pos1);
$xmlDoc = @DOMDocument::loadHTML($content);
}
if ($xmlDoc !== false) {
$xPath = new DOMXPath($xmlDoc);
if($formId == "") $formId = "login-form";
$nodes = $xPath->query('//form[@id="'.$formId.'"]');
if (!$nodes->length) {
return "";
}
$form = $nodes->item(0);
$postUri = $form->getAttribute("action");
$hiddens = $xPath->query('.//input[@type="hidden"]', $form);
foreach ($hiddens as $hiddenNode) {
$postData[$hiddenNode->getAttribute("name")] = $hiddenNode->getAttribute("value");
}
} else {
// Grab all inputs and hardcode $postUri.
if (preg_match_all("<input type=\"hidden\" name=\"(.*)\" value=\"(.*)\">", $content, $matches)) {
foreach ($matches[0] as $key => $match) {
$postData[$matches[1][$key]] = $matches[2][$key];
}
$postUri = "/login-form";
}
}
//AJXP_Logger::debug("Carry on ". $hiddens->length);
$client->setHandleRedirects(false);
$client->setHeadersOnly(true);
$client->setCookies(extractResponseCookies($client));
$res2 = $client->post($postUri, $postData);
$newCookies = extractResponseCookies($client);
if (isSet($newCookies["AjaXplorer"])) {
return $newCookies;
}
return "";
}
function drupal_remote_auth($host, $uri, $login, $pass, $formId = "")
{
$client = new HttpClient($host);
$client->setHandleRedirects(false);
$res = $client->get($uri);
$content = $client->getContent();
$xmlDoc = DOMDocument::loadHTML($content);
$xPath = new DOMXPath($xmlDoc);
if($formId == "") $formId = "user-login-form";
$nodes = $xPath->query('//form[@id="'.$formId.'"]');
if (!$nodes->length) {
return "";
}
$form = $nodes->item(0);
$postUri = $form->getAttribute("action");
$hiddens = $xPath->query('.//input[@type="hidden"]', $form);
AJXP_Logger::debug("Carry on Drupal hiddens ". $hiddens->length);
$postData = array(
"name" => $login,
"pass" => $pass,
"Submit" => "Log in"
);
foreach ($hiddens as $hiddenNode) {
$postData[$hiddenNode->getAttribute("name")] = $hiddenNode->getAttribute("value");
}
$client->setHandleRedirects(false);
$client->setHeadersOnly(true);
$client->setCookies(extractResponseCookies($client));
$res2 = $client->post($postUri, $postData);
$newCookies = extractResponseCookies($client);
if (isSet($newCookies["AjaXplorer"])) {
return $newCookies;
}
return "";
}