alias mapping!

calls to "resource=http//some.alias.org/user" now return proper values accourding to webfinger spec
This commit is contained in:
Joshua P Panter 2018-01-27 21:30:56 -05:00
parent 92f5d6052d
commit fa74db1100

View file

@ -2,7 +2,7 @@
/* /*
*------------------------------------------------------------ *------------------------------------------------------------
* *
* PEWS (pew! pew!) - PHP Easy WebFinger Server 1.7.2 * PEWS (pew! pew!) - PHP Easy WebFinger Server 1.8.2
* *
* This script enables webfinger support on a server that * This script enables webfinger support on a server that
* handles one or more domains. * handles one or more domains.
@ -16,9 +16,8 @@
*/ */
// Set an alternate location for the data store. note: no trailing slash // Set an alternate location for the data store. note: no trailing slash
define( 'PEWS_DATA_STORE', 'store' ); define( 'PEWS_DATA_STORE', 'store' );
// allow a user to edit their own data? //------------------ DO NOT EDIT ANYTHING BELOW THIS LINE (Unless you REALLY mean it!) ------------------//
define( 'PEWS_USER_SELF_EDIT', true ); // Begin PEWS server
// Begin PEWS server //------------------ DO NOT EDIT ANYTHING BELOW THIS LINE (Unless you REALLY mean it!) ------------------//
$req = $_SERVER['REQUEST_METHOD']; $req = $_SERVER['REQUEST_METHOD'];
if ($req === 'GET') { if ($req === 'GET') {
// are we receiving a JSON object? // are we receiving a JSON object?
@ -42,15 +41,38 @@ if ($req === 'GET') {
} else { $json_object = false; } } else { $json_object = false; }
// JSON object or not, ready to process data // JSON object or not, ready to process data
if( isset($_GET['resource'])) { if( isset($_GET['resource'])) {
$subject = explode(':', $_GET['resource']); $querry = $_GET['resource'];
if($subject[0] === 'acct') { $subject = explode(':', $querry);
$resource = pews_parse_account_string ( $subject[1]); $case = $subject[0];
if($case !== 'acct') {
$acctAliasMap = PEWS_DATA_STORE . '/acctAliasMap.json';
if (file_exists($acctAliasMap)) {
$acctAliasArray = json_decode(file_get_contents($acctAliasMap), true);
if(array_key_exists($querry, $acctAliasArray)) {
$subjectOverride = $querry;
$aliasOverride = $acctAliasArray[$querry];
$case = 'acct';
}
}
}
if($case == 'acct' ) {
$resource = isset($aliasOverride) ? pews_parse_account_string($aliasOverride) : pews_parse_account_string($subject[1]);
$acct_file = PEWS_DATA_STORE."/".$resource['host']."/".$resource['user'].".json"; $acct_file = PEWS_DATA_STORE."/".$resource['host']."/".$resource['user'].".json";
// is there an account on file? // is there an account on file?
if (file_exists($acct_file)) { if (file_exists($acct_file)) {
// retrieve resource file and remove PEWS info // retrieve resource file and remove PEWS info
$data = json_decode(file_get_contents($acct_file), true); $data = json_decode(file_get_contents($acct_file), true);
if(isset($data['PEWS'])) unset($data['PEWS']); if(isset($data['PEWS'])) unset($data['PEWS']);
// is this an alias request?
if(isset($subjectOverride) && isset($aliasOverride)) {
$resAA = $data['aliases'];
if (($key = array_search($subjectOverride, $resAA)) !== false) {
unset($resAA[$key]);
}
$resAA[] = $aliasOverride;
$data['subject'] = $subjectOverride;
$data['aliases'] = $resAA;
}
// check for rel request // check for rel request
if($json_object == false) { if($json_object == false) {
if( isset($_GET['rel'])) { if( isset($_GET['rel'])) {
@ -75,23 +97,21 @@ if ($req === 'GET') {
} }
$data['links'] = ($result == null) ? $data['links'] : $result; $data['links'] = ($result == null) ? $data['links'] : $result;
$return = $data; $return = $data;
} else { } else {
$return = $data; $return = $data;
} }
// set return headers, response code, and return data // set return headers, response code, and return data
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
http_response_code(200); http_response_code(200);
} else { } else {
http_response_code(404); http_response_code(404);
$return['statusCode'] = 404; $return['statusCode'] = 404;
$return['message'] = 'Account ' . $resource['acct'] . ' not found.'; $return['message'] = 'Account ' . $resource['acct'] . ' not found.';
} }
} else { } else {
http_response_code(400); http_response_code(404);
$return['statusCode'] = 400; $return['statusCode'] = 404;
$return['message'] = 'Malformed query: ['.$subject[0].'] not recognized in ' .$subject. '.'; $return['message'] = 'Resource ' . $querry . ' not found.';
} }
} else { } else {
http_response_code(400); http_response_code(400);
@ -150,7 +170,7 @@ function pews_parse_account_string ( $acct ) {
$user = preg_replace('/^((\.*)(\/*))*/', '', $parts[0]); $user = preg_replace('/^((\.*)(\/*))*/', '', $parts[0]);
$host = preg_replace('/^((\.*)(\/*))*/', '', $parts[1]); $host = preg_replace('/^((\.*)(\/*))*/', '', $parts[1]);
} else { } else {
$user = preg_replace('/^((\.*)(\/*))*/', '', $str); $user = preg_replace('/^((\.*)(\/*))*/', '', $acct);
$host = $_SERVER['HTTP_HOST']; $host = $_SERVER['HTTP_HOST'];
$acct = $user . '@' . $host; $acct = $user . '@' . $host;
} }
@ -259,13 +279,38 @@ function pews_manager( $auth, $password ) {
// delete a host AND all resources // delete a host AND all resources
} elseif(isset($_POST['delHost'])) { } elseif(isset($_POST['delHost'])) {
if(isset($auth) && $auth == 'admin') { if(isset($auth) && $auth == 'admin') {
$host = preg_replace('/^((\.*)(\/*))*/', '', $_POST['addHost']); $host = preg_replace('/^((\.*)(\/*))*/', '', $_POST['delHost']);
$old = PEWS_DATA_STORE . '/' . $host; $old = PEWS_DATA_STORE . '/' . $host;
if (file_exists($old)) { if (file_exists($old)) {
$files = glob($old.'/*'); $files = glob($old.'/*');
foreach($files as $file) { $acctAliasMap = PEWS_DATA_STORE . '/acctAliasMap.json';
if(is_file($file)) if(file_exists($acctAliasMap)) {
unlink($file); $acctAliasArray = json_decode(file_get_contents($acctAliasMap), true);
$did_del = 0;
foreach($files as $file) {
if(is_file($file)) {
$fileData = json_decode(file_get_contents($file), true);
if(isset($fileData['aliases'])) {
$acctAliases = $fileData['aliases'];
foreach($acctAliases as $alias) {
if(array_key_exists($alias, $acctAliasArray)) {
unset($acctAliasArray[$alias]);
$did_del++;
}
}
}
unlink($file);
}
}
if($did_del > 0) {
file_put_contents( $acctAliasMap, json_encode($acctAliasArray, JSON_UNESCAPED_SLASHES) );
chmod( $acctAliasMap, 0755 );
}
} else {
foreach($files as $file) {
if(is_file($file))
unlink($file);
}
} }
$destroy = rmdir($old); $destroy = rmdir($old);
if(!$destroy) { if(!$destroy) {
@ -293,8 +338,8 @@ function pews_manager( $auth, $password ) {
$newHost = PEWS_DATA_STORE . '/' . $resource['host']; $newHost = PEWS_DATA_STORE . '/' . $resource['host'];
if (!file_exists($newHost)){ if (!file_exists($newHost)){
http_response_code(404); http_response_code(404);
$response['statusCode'] = '404'; $return['statusCode'] = '404';
$response['message'] = 'The host '. $resource['host'] .' is not present, and must be on this system before resource accounts are added to it.'; $return['message'] = 'The host '. $resource['host'] .' is not present, and must be on this system before resource accounts are added to it.';
} else { } else {
$newUser = $newHost .'/'. $resource['user'] .'.json'; $newUser = $newHost .'/'. $resource['user'] .'.json';
if (!file_exists($newUser)){ if (!file_exists($newUser)){
@ -305,8 +350,9 @@ function pews_manager( $auth, $password ) {
$data['PEWS'] = array( 'class' => $class, 'pass' => $pass ); $data['PEWS'] = array( 'class' => $class, 'pass' => $pass );
$data['subject'] = 'acct:'. $resource['acct']; $data['subject'] = 'acct:'. $resource['acct'];
if(isset($_POST['setAliases'])) { if(isset($_POST['setAliases'])) {
$aliases = $_POST['setAliases']; if(is_array($_POST['setAliases'])) $aliases = $_POST['setAliases'];
$data['aliases'] = is_array($aliases) ? $aliases : array($aliases); else $aliases[] = $_POST['setAliases'];
$data['aliases'] = $aliases;
} }
if(isset($_POST['setProps'])) { if(isset($_POST['setProps'])) {
if(is_array($_POST['setProps'])) { if(is_array($_POST['setProps'])) {
@ -335,7 +381,6 @@ function pews_manager( $auth, $password ) {
foreach($link as $k => $v) { foreach($link as $k => $v) {
if($v == null) unset($link[$k]); if($v == null) unset($link[$k]);
} }
$data['links'] = $link; $data['links'] = $link;
} }
} }
@ -346,7 +391,19 @@ function pews_manager( $auth, $password ) {
$return['statusCode'] = 500; $return['statusCode'] = 500;
$return['message'] = 'Resource not created'; $return['message'] = 'Resource not created';
} else { } else {
chmod( $newUser, 0755 ); if(isset($aliases)){
foreach($aliases as $alias) $acctAliasArray[$alias] = $resource['acct'];
$acctAliasMap = PEWS_DATA_STORE . '/acctAliasMap.json';
if(!file_exists($acctAliasMap)) {
$dataAlt = $acctAliasArray;
} else {
$oldMap = json_decode(file_get_contents($acctAliasMap), true);
$dataAlt = array_merge($oldMap , $acctAliasArray);
}
file_put_contents( $acctAliasMap, json_encode($dataAlt, JSON_UNESCAPED_SLASHES) );
chmod( $acctAliasMap, 0755 );
}
chmod( $newUser, 0755 );
http_response_code(201); http_response_code(201);
$return['statusCode'] = 201; $return['statusCode'] = 201;
$return['message'] = 'Resource: '.$resource['acct'].' successfully added'; $return['message'] = 'Resource: '.$resource['acct'].' successfully added';
@ -372,12 +429,29 @@ function pews_manager( $auth, $password ) {
$resource = pews_parse_account_string( $resource ); $resource = pews_parse_account_string( $resource );
$acct_file = PEWS_DATA_STORE ."/". $resource['host'] ."/". $resource['user'] .".json"; $acct_file = PEWS_DATA_STORE ."/". $resource['host'] ."/". $resource['user'] .".json";
if (file_exists($acct_file)) { if (file_exists($acct_file)) {
$acctArray = json_decode(file_get_contents($acct_file), true);
$acctAliases = $acctArray['aliases'];
$destroy = unlink($acct_file); $destroy = unlink($acct_file);
if(!$destroy) { if(!$destroy) {
http_response_code(500); http_response_code(500);
$return['statusCode'] = 500; $return['statusCode'] = 500;
$return['message'] = 'Server Error: resource not destroyed.'; $return['message'] = 'Server Error: resource not destroyed.';
} else { } else {
$acctAliasMap = PEWS_DATA_STORE . '/acctAliasMap.json';
if(file_exists($acctAliasMap)) {
$acctAliasArray = json_decode(file_get_contents($acctAliasMap), true);
$did_del = 0;
foreach($acctAliases as $alias) {
if(array_key_exists($alias, $acctAliasArray)) {
unset($acctAliasArray[$alias]);
$did_del++;
}
}
if($did_del > 0) {
file_put_contents( $acctAliasMap, json_encode($acctAliasArray, JSON_UNESCAPED_SLASHES) );
chmod( $acctAliasMap, 0755 );
}
}
http_response_code(200); http_response_code(200);
$return['statusCode'] = 200; $return['statusCode'] = 200;
$return['message'] = 'Acct: '. $resource['acct'] .' successfully removed'; $return['message'] = 'Acct: '. $resource['acct'] .' successfully removed';
@ -418,6 +492,18 @@ function pews_manager( $auth, $password ) {
$return['statusCode'] = 500; $return['statusCode'] = 500;
$return['message'] = 'Could not write to resource file'; $return['message'] = 'Could not write to resource file';
} else { } else {
if(isset($aliases)){
foreach($aliases as $alias) $acctAliasArray[$alias] = $resource['acct'];
$acctAliasMap = PEWS_DATA_STORE . '/acctAliasMap.json';
if(!file_exists($acctAliasMap)) {
$dataAlt = $acctAliasArray;
} else {
$oldMap = json_decode(file_get_contents($acctAliasMap), true);
$dataAlt = array_merge($oldMap , $acctAliasArray);
}
file_put_contents( $acctAliasMap, json_encode($dataAlt, JSON_UNESCAPED_SLASHES) );
chmod( $acctAliasMap, 0755 );
}
http_response_code(200); http_response_code(200);
$return['statusCode'] = 200; $return['statusCode'] = 200;
$return['message'] = 'Alias: '.$newAlias.' added to '.$resource['acct']; $return['message'] = 'Alias: '.$newAlias.' added to '.$resource['acct'];
@ -471,6 +557,15 @@ function pews_manager( $auth, $password ) {
$return['statusCode'] = 500; $return['statusCode'] = 500;
$return['info'] = 'Could not write to resource file'; $return['info'] = 'Could not write to resource file';
} else { } else {
$acctAliasMap = PEWS_DATA_STORE . '/acctAliasMap.json';
if(file_exists($acctAliasMap)) {
$acctAliasArray = json_decode(file_get_contents($acctAliasMap), true);
if(array_key_exists($oldAlias, $acctAliasArray)) {
unset($acctAliasArray[$oldAlias]);
file_put_contents( $acctAliasMap, json_encode($acctAliasArray, JSON_UNESCAPED_SLASHES) );
chmod( $acctAliasMap, 0755 );
}
}
http_response_code(200); http_response_code(200);
$return['statusCode'] = 200; $return['statusCode'] = 200;
$return['info'] = 'Alias: '.$oldAlias.' removed '.$resource['acct']; $return['info'] = 'Alias: '.$oldAlias.' removed '.$resource['acct'];