2018-01-23 16:39:07 +01:00
|
|
|
<?php
|
|
|
|
/*
|
2018-01-26 20:16:01 +01:00
|
|
|
*------------------------------------------------------------
|
|
|
|
*
|
2018-01-27 17:15:33 +01:00
|
|
|
* PEWS (pew! pew!) - PHP Easy WebFinger Server 1.7.2
|
2018-01-26 20:16:01 +01:00
|
|
|
*
|
|
|
|
* This script enables webfinger support on a server that
|
|
|
|
* handles one or more domains.
|
|
|
|
*
|
|
|
|
* by Josh Panter <joshu at unfettered dot net>
|
|
|
|
*
|
|
|
|
*------------------------------------------------------------
|
2018-01-23 16:39:07 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
CONFIG
|
|
|
|
*/
|
|
|
|
// Set an alternate location for the data store. note: no trailing slash
|
|
|
|
define( 'PEWS_DATA_STORE', 'store' );
|
|
|
|
// allow a user to edit their own data?
|
|
|
|
define( 'PEWS_USER_SELF_EDIT', true );
|
|
|
|
// Begin PEWS server //------------------ DO NOT EDIT ANYTHING BELOW THIS LINE (Unless you REALLY mean it!) ------------------//
|
|
|
|
$req = $_SERVER['REQUEST_METHOD'];
|
|
|
|
if ($req === 'GET') {
|
|
|
|
// are we receiving a JSON object?
|
|
|
|
function isValidJSON($str) {
|
|
|
|
json_decode($str);
|
|
|
|
return json_last_error() == JSON_ERROR_NONE;
|
|
|
|
}
|
|
|
|
$json_params = file_get_contents("php://input");
|
|
|
|
if (strlen($json_params) > 0 && isValidJSON($json_params)) {
|
|
|
|
$rels = false;
|
|
|
|
$json_object = true;
|
|
|
|
$json_params = str_replace("{", "", $json_params);
|
|
|
|
$json_params = str_replace("}", "", $json_params);
|
|
|
|
$json_params = explode(',', $json_params);
|
|
|
|
foreach($json_params as $jp) {
|
|
|
|
$jp = str_replace('"', '', $jp);
|
|
|
|
$jp = explode(':', $jp, 2);
|
|
|
|
if($jp[0]=='resource') $_GET['resource']=$jp[1];
|
|
|
|
if($jp[0]=='rel') $rels[]=$jp[1];
|
|
|
|
}
|
|
|
|
} else { $json_object = false; }
|
|
|
|
// JSON object or not, ready to process data
|
|
|
|
if( isset($_GET['resource'])) {
|
|
|
|
$subject = explode(':', $_GET['resource']);
|
|
|
|
if($subject[0] === 'acct') {
|
2018-01-26 20:16:01 +01:00
|
|
|
$resource = pews_parse_account_string ( $subject[1]);
|
|
|
|
$acct_file = PEWS_DATA_STORE."/".$resource['host']."/".$resource['user'].".json";
|
2018-01-23 16:39:07 +01:00
|
|
|
// is there an account on file?
|
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
// retrieve resource file and remove PEWS info
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
if(isset($data['PEWS'])) unset($data['PEWS']);
|
|
|
|
// check for rel request
|
|
|
|
if($json_object == false) {
|
|
|
|
if( isset($_GET['rel'])) {
|
|
|
|
// disect string for multiple 'rel' values
|
|
|
|
$query = explode('&', $_SERVER['QUERY_STRING']);
|
|
|
|
$array = array();
|
|
|
|
foreach( $query as $param ) {
|
|
|
|
list($key, $value) = explode('=', $param, 2);
|
|
|
|
if($key == 'rel') {
|
|
|
|
$array[urldecode($key)][] = urldecode($value);
|
|
|
|
$rels = $array['rel'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isset($rels) && $rels !== false) {
|
|
|
|
// check resource data against rel request
|
|
|
|
$links = $data['links'];
|
|
|
|
$result = null;
|
|
|
|
foreach($rels as $rel) {
|
|
|
|
foreach($links as $link) if($link['rel'] == $rel) $result[] = $link;
|
|
|
|
}
|
|
|
|
$data['links'] = ($result == null) ? $data['links'] : $result;
|
|
|
|
$return = $data;
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$return = $data;
|
|
|
|
}
|
|
|
|
// set return headers, response code, and return data
|
|
|
|
header('Access-Control-Allow-Origin: *');
|
|
|
|
http_response_code(200);
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['message'] = 'Account ' . $resource['acct'] . ' not found.';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['message'] = 'Malformed query: ['.$subject[0].'] not recognized in ' .$subject. '.';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "Missing 'resource' parameter, please check your query.";
|
|
|
|
}
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
print json_encode($return, JSON_UNESCAPED_SLASHES);
|
|
|
|
die();
|
2018-01-26 20:16:01 +01:00
|
|
|
// ----------- Begin PEWS manager -----------//
|
2018-01-23 16:39:07 +01:00
|
|
|
} elseif ($req === 'POST') {
|
|
|
|
// are we receiving a JSON object?
|
|
|
|
function isValidJSON($str) {
|
|
|
|
json_decode($str);
|
|
|
|
return json_last_error() == JSON_ERROR_NONE;
|
|
|
|
}
|
|
|
|
$json_params = file_get_contents("php://input");
|
|
|
|
if (strlen($json_params) > 0 && isValidJSON($json_params))
|
|
|
|
$_POST = json_decode($json_params, true);
|
|
|
|
// JSON object or not, ready to process data
|
|
|
|
if(isset($_POST['pass'])) {
|
|
|
|
$pass = $_POST['pass'];
|
|
|
|
if (isset($_POST['auth'])) {
|
|
|
|
$user = $_POST['auth'];
|
2018-01-26 20:16:01 +01:00
|
|
|
$auth = pews_auth($user, $pass);
|
|
|
|
$class = $auth['class'];
|
|
|
|
if(!$class) {
|
2018-01-23 16:39:07 +01:00
|
|
|
http_response_code(401);
|
|
|
|
$return['info'] = $auth['info'];
|
2018-01-26 20:16:01 +01:00
|
|
|
} elseif($class == 'user') {
|
|
|
|
http_response_code(403);
|
|
|
|
$return['info'] = 'forbidden: contact admin for access';
|
|
|
|
} else $return = pews_manager($class, false);
|
2018-01-23 16:39:07 +01:00
|
|
|
} else $return = pews_manager(false, $pass);
|
|
|
|
} else {
|
|
|
|
http_response_code(403);
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['info'] = 'forbidden: credentials required';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
print json_encode($return, JSON_UNESCAPED_SLASHES);
|
|
|
|
die();
|
2018-01-26 20:16:01 +01:00
|
|
|
// ----------- Begin PEWS Fail -----------//
|
2018-01-23 16:39:07 +01:00
|
|
|
} else {
|
|
|
|
header("Content-Type: application/json");
|
|
|
|
http_response_code(405);
|
|
|
|
print json_encode(array(
|
|
|
|
'statusCode' => 405,
|
|
|
|
'info' => 'method not allowed'
|
|
|
|
), JSON_UNESCAPED_SLASHES);
|
|
|
|
die();
|
|
|
|
}
|
2018-01-26 20:16:01 +01:00
|
|
|
// ----------- Begin PEWS functions -----------//
|
|
|
|
function pews_parse_account_string ( $acct ) {
|
|
|
|
if(strpos($acct, '@')) {
|
|
|
|
$parts = explode('@', $acct );
|
|
|
|
$user = preg_replace('/^((\.*)(\/*))*/', '', $parts[0]);
|
|
|
|
$host = preg_replace('/^((\.*)(\/*))*/', '', $parts[1]);
|
|
|
|
} else {
|
|
|
|
$user = preg_replace('/^((\.*)(\/*))*/', '', $str);
|
|
|
|
$host = $_SERVER['HTTP_HOST'];
|
|
|
|
$acct = $user . '@' . $host;
|
|
|
|
}
|
|
|
|
$return['user'] = $user;
|
|
|
|
$return['host'] = $host;
|
|
|
|
$return['acct'] = $acct;
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
function pews_auth( $resource, $key ) {
|
2018-01-26 15:41:45 +01:00
|
|
|
$resource = pews_parse_account_string( $resource );
|
2018-01-26 20:16:01 +01:00
|
|
|
$acct_file = PEWS_DATA_STORE ."/". $resource['host'] . "/" . $resource['user'] .".json";
|
2018-01-23 16:39:07 +01:00
|
|
|
// is there an account on file?
|
|
|
|
if(file_exists($acct_file)) {
|
2018-01-26 20:16:01 +01:00
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$userData = $data['PEWS'];
|
|
|
|
$class = $userData['class'];
|
|
|
|
$lock = $userData['pass'];
|
2018-01-23 16:39:07 +01:00
|
|
|
if(strpos($lock, 'pews-hashed') === false ) {
|
|
|
|
$hashit = pews_hash_pass($acct_file);
|
2018-01-26 20:16:01 +01:00
|
|
|
if($hashit['is'] !== true ) die($hashit['info']);
|
2018-01-23 16:39:07 +01:00
|
|
|
if($lock == $key ) {
|
|
|
|
$return['info'] = $hashit['info'];
|
|
|
|
$return['class'] = $class;
|
|
|
|
} else {
|
|
|
|
$return['info'] = 'bad password';
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['class'] = false;
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$hashLock = explode(':', $lock);
|
|
|
|
$hashLock = $hashLock[1];
|
|
|
|
if(password_verify($key, $hashLock)) {
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['info'] = 'success';
|
2018-01-23 16:39:07 +01:00
|
|
|
$return['class'] = $class;
|
|
|
|
} else {
|
|
|
|
$return['info'] = 'bad password';
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['class'] = false;
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$return['info'] = 'bad user name';
|
2018-01-26 20:16:01 +01:00
|
|
|
$return['class'] = false;
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
function pews_hash_pass($acct_file) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
if($data == false) {
|
|
|
|
$return['is'] = false;
|
|
|
|
$return['info'] = 'Could not read auth file';
|
|
|
|
} else {
|
|
|
|
$userData = $data['PEWS'];
|
|
|
|
$class = $userData['class'];
|
|
|
|
$lock = $userData['pass'];
|
|
|
|
$to_hash = 0;
|
|
|
|
|
|
|
|
if(strpos($lock, 'pews-hashed') === false) {
|
|
|
|
$to_hash++;
|
|
|
|
$hash = password_hash( $lock, PASSWORD_DEFAULT);
|
|
|
|
$data['PEWS'] = array('class' => $class, 'pass' => 'pews-hashed:'.$hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($to_hash == 0) {
|
|
|
|
$return['is'] = true;
|
|
|
|
$return['info'] = 'Nothing to hash';
|
|
|
|
} else {
|
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
|
|
|
$return['is'] = false;
|
|
|
|
$return['info'] = 'Could not write to auth file';
|
|
|
|
} else {
|
|
|
|
$return['is'] = true;
|
|
|
|
$return['info'] = 'password hashed';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
function pews_manager( $auth, $password ) {
|
|
|
|
// add a new host to the server TODO url validations, etc
|
|
|
|
if(isset($_POST['addHost'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
if(isset($auth) && $auth == 'admin') {
|
2018-01-26 16:08:58 +01:00
|
|
|
$host = preg_replace('/^((\.*)(\/*))*/', '', $_POST['addHost']);
|
|
|
|
$new = PEWS_DATA_STORE . '/' . $host;
|
2018-01-23 16:39:07 +01:00
|
|
|
if (!file_exists($new)){
|
|
|
|
$make = mkdir($new);
|
|
|
|
if(!$make) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'host not created';
|
|
|
|
} else {
|
|
|
|
chmod( $new, 0755 );
|
|
|
|
http_response_code(201);
|
|
|
|
$return['statusCode'] = 201;
|
2018-01-26 16:08:58 +01:00
|
|
|
$return['message'] = 'host: '. $host .' successfully added';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'host already present';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(403);
|
|
|
|
$return['info'] = 'forbidden';
|
|
|
|
}
|
|
|
|
// delete a host AND all resources
|
|
|
|
} elseif(isset($_POST['delHost'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
if(isset($auth) && $auth == 'admin') {
|
2018-01-26 16:08:58 +01:00
|
|
|
$host = preg_replace('/^((\.*)(\/*))*/', '', $_POST['addHost']);
|
|
|
|
$old = PEWS_DATA_STORE . '/' . $host;
|
2018-01-23 16:39:07 +01:00
|
|
|
if (file_exists($old)) {
|
|
|
|
$files = glob($old.'/*');
|
|
|
|
foreach($files as $file) {
|
|
|
|
if(is_file($file))
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
$destroy = rmdir($old);
|
|
|
|
if(!$destroy) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'host not destroyed, but the accounts probably were.';
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
2018-01-26 16:08:58 +01:00
|
|
|
$return['message'] = 'host: '.$host.' successfully removed';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'host already absent';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(403);
|
|
|
|
$return['info'] = 'forbidden';
|
|
|
|
}
|
|
|
|
// Add a new resource account!
|
|
|
|
} elseif(isset($_POST['addResource'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
if(isset($auth) && $auth == 'admin') {
|
2018-01-26 15:41:45 +01:00
|
|
|
$resource = pews_parse_account_string( $_POST['addResource'] );
|
|
|
|
$newHost = PEWS_DATA_STORE . '/' . $resource['host'];
|
2018-01-23 16:39:07 +01:00
|
|
|
if (!file_exists($newHost)){
|
|
|
|
http_response_code(404);
|
2018-01-26 15:41:45 +01:00
|
|
|
$response['statusCode'] = '404';
|
|
|
|
$response['message'] = 'The host '. $resource['host'] .' is not present, and must be on this system before resource accounts are added to it.';
|
2018-01-23 16:39:07 +01:00
|
|
|
} else {
|
2018-01-26 15:41:45 +01:00
|
|
|
$newUser = $newHost .'/'. $resource['user'] .'.json';
|
2018-01-23 16:39:07 +01:00
|
|
|
if (!file_exists($newUser)){
|
|
|
|
$class = isset($_POST['setClass']) && ($_POST['setClass'] === 'admin' || $_POST['setClass'] === 'user') ?
|
|
|
|
$_POST['setClass'] :
|
|
|
|
'user';
|
|
|
|
$pass= isset($_POST['setPass']) ? 'pews-hashed:'.password_hash($_POST['setPass'], PASSWORD_DEFAULT) : 'pewpewpassword';
|
|
|
|
$data['PEWS'] = array( 'class' => $class, 'pass' => $pass );
|
2018-01-26 15:41:45 +01:00
|
|
|
$data['subject'] = 'acct:'. $resource['acct'];
|
2018-01-23 16:39:07 +01:00
|
|
|
if(isset($_POST['setAliases'])) {
|
|
|
|
$aliases = $_POST['setAliases'];
|
|
|
|
$data['aliases'] = is_array($aliases) ? $aliases : array($aliases);
|
|
|
|
}
|
|
|
|
if(isset($_POST['setProps'])) {
|
|
|
|
if(is_array($_POST['setProps'])) {
|
|
|
|
$data['properties'] = $_POST['setProps'];
|
|
|
|
} elseif(isset($_POST['setPropKey']) && isset($_POST['setPropVal'])) {
|
|
|
|
$data['properties'] = array($_POST['setPropKey'] => $_POST['setPropVal']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isset($_POST['setLinks'])) {
|
|
|
|
if(is_array($_POST['setLinks'])) {
|
|
|
|
$data['links'] = $_POST['setLinks'];
|
|
|
|
} elseif(isset($_POST['setLinkRel']) && isset($_POST['setLinkHref'])) {
|
|
|
|
$link['rel'] = $_POST['setLinkRel'];
|
|
|
|
$link['href'] = $_POST['setLinkHref'];
|
|
|
|
$link['type'] = isset($_POST['setLinkType']) ? $_POST['setLinkType'] : null;
|
|
|
|
$link['titles'] = isset($_POST['setLinkTitles']) && is_array($_POST['setLinkTitles']) ?
|
|
|
|
$_POST['setLinkTitles'] :
|
|
|
|
isset($_POST['setLinkTitleLang']) && isset($_POST['setLinkTitle']) ?
|
|
|
|
array($_POST['setLinkTitleLang'] => $_POST['setLinkTitle']) :
|
|
|
|
null;
|
|
|
|
$link['properties'] = isset($_POST['setLinkProps']) && is_array($_POST['setLinkProps']) ?
|
|
|
|
$_POST['setLinkProps'] :
|
|
|
|
isset($_POST['setLinkPropKey']) && isset($_POST['setLinkPropVal']) ?
|
|
|
|
array($_POST['setLinkPropKey'] => $_POST['setLinkPropVal']) :
|
|
|
|
null;
|
|
|
|
foreach($link as $k => $v) {
|
|
|
|
if($v == null) unset($link[$k]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['links'] = $link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create the resource!!!
|
|
|
|
$success = file_put_contents( $newUser, json_encode($data, JSON_UNESCAPED_SLASHES) );
|
|
|
|
if(!$success) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'Resource not created';
|
|
|
|
} else {
|
|
|
|
chmod( $newUser, 0755 );
|
|
|
|
http_response_code(201);
|
|
|
|
$return['statusCode'] = 201;
|
2018-01-26 15:41:45 +01:00
|
|
|
$return['message'] = 'Resource: '.$resource['acct'].' successfully added';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Resource already present';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(403);
|
|
|
|
$return['info'] = 'forbidden';
|
|
|
|
}
|
|
|
|
// Remove a resource/account from the server
|
|
|
|
} elseif(isset($_POST['delResource'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
$resource = $_POST['delResource'];
|
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
|
|
|
case true:
|
|
|
|
$resource = pews_parse_account_string( $resource );
|
|
|
|
$acct_file = PEWS_DATA_STORE ."/". $resource['host'] ."/". $resource['user'] .".json";
|
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$destroy = unlink($acct_file);
|
|
|
|
if(!$destroy) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'Server Error: resource not destroyed.';
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Acct: '. $resource['acct'] .' successfully removed';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Acct already absent';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
|
|
|
$return['message'] = "You can delete your account if you know your credentials";
|
|
|
|
$return['info'] = $reauth['info'];
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
// adding an alias to a resource
|
|
|
|
} elseif(isset($_POST['addAlias'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
$resource = $_POST['addAlias'];
|
2018-01-23 16:39:07 +01:00
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
2018-01-26 20:16:01 +01:00
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
2018-01-23 16:39:07 +01:00
|
|
|
case true:
|
|
|
|
if(isset($_POST['newAlias'])) {
|
|
|
|
$newAlias = $_POST['newAlias'];
|
2018-01-27 09:05:45 +01:00
|
|
|
$resource = pews_parse_account_string( $resource );
|
2018-01-26 15:41:45 +01:00
|
|
|
$acct_file = PEWS_DATA_STORE . '/' . $resource['host'] .'/'. $resource['user'] . '.json';
|
2018-01-23 16:39:07 +01:00
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$aliases = isset($data['aliases']) ? $data['aliases'] : array();
|
|
|
|
$aliases[] = $newAlias;
|
|
|
|
$data['aliases'] = $aliases;
|
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
2018-01-27 09:05:45 +01:00
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'Could not write to resource file';
|
2018-01-23 16:39:07 +01:00
|
|
|
} else {
|
2018-01-27 09:05:45 +01:00
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Alias: '.$newAlias.' added to '.$resource['acct'];
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
2018-01-26 15:41:45 +01:00
|
|
|
$return['message'] = 'Account: '.$resource['acct'].' not found.';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "Missing newAlias, please check your query,";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
|
|
|
$return['message'] = "You can add an alias if you know your credentials";
|
|
|
|
$return['info'] = $reauth['info'];
|
|
|
|
}
|
|
|
|
// remove an alias from a resource
|
|
|
|
} elseif(isset($_POST['delAlias'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
$resource = $_POST['delAlias'];
|
2018-01-23 16:39:07 +01:00
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
2018-01-26 20:16:01 +01:00
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
2018-01-23 16:39:07 +01:00
|
|
|
case true:
|
|
|
|
if(isset($_POST['oldAlias'])) {
|
|
|
|
$oldAlias = $_POST['oldAlias'];
|
2018-01-27 09:05:45 +01:00
|
|
|
$resource = pews_parse_account_string( $resource );
|
2018-01-26 15:41:45 +01:00
|
|
|
$acct_file = PEWS_DATA_STORE . '/' . $resource['host'] .'/'. $resource['user'] . '.json';
|
2018-01-23 16:39:07 +01:00
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$aliases = isset($data['aliases']) ? $data['aliases'] : null;
|
2018-01-27 17:15:33 +01:00
|
|
|
|
|
|
|
if($aliases !== null && in_array( $oldAlias, $aliases ) ) {
|
|
|
|
unset($aliases[$oldAlias]);
|
2018-01-23 16:39:07 +01:00
|
|
|
if(empty($newAliasesArray)) {
|
|
|
|
unset ($data['aliases']);
|
|
|
|
} else {
|
|
|
|
$data['aliases'] = $newAliasesArray;
|
|
|
|
}
|
2018-01-27 17:15:33 +01:00
|
|
|
|
|
|
|
|
2018-01-23 16:39:07 +01:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
|
|
|
http_response_code(500);
|
2018-01-27 09:05:45 +01:00
|
|
|
$return['statusCode'] = 500;
|
2018-01-23 16:39:07 +01:00
|
|
|
$return['info'] = 'Could not write to resource file';
|
|
|
|
} else {
|
2018-01-27 09:05:45 +01:00
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
2018-01-26 15:41:45 +01:00
|
|
|
$return['info'] = 'Alias: '.$oldAlias.' removed '.$resource['acct'];
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(100);
|
|
|
|
$return['is'] = false;
|
|
|
|
$return['info'] = 'Nothing to do: Alias '.$oldAlias.' not found.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
2018-01-26 15:41:45 +01:00
|
|
|
$return['message'] = 'Account: '.$resource['acct'].' not found.';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "Missing oldAlias, please check your query,";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
|
|
|
$return['message'] = "You can remove an alias if you know your credentials";
|
|
|
|
$return['info'] = $reauth['info'];
|
|
|
|
}
|
|
|
|
} elseif(isset($_POST['addProp'])) {
|
2018-01-27 09:05:45 +01:00
|
|
|
$resource = $_POST['addProp'];
|
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
|
|
|
case true:
|
|
|
|
if(isset($_POST['propKey']) && isset($_POST['propVal'])) {
|
|
|
|
$propKey = $_POST['propKey'];
|
|
|
|
$propVal = $_POST['propVal'];
|
|
|
|
$resource = pews_parse_account_string( $resource );
|
|
|
|
$acct_file = PEWS_DATA_STORE .'/'. $resource['host'] .'/'. $resource['user'] .'.json';
|
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$oldProps = isset($data['properties']) ? $data['properties'] : array();
|
|
|
|
if(array_key_exists($propKey, $oldProps)) {
|
|
|
|
http_response_code(409);
|
|
|
|
$return['statusCode'] = 409;
|
|
|
|
$return['message'] = $propKey . ' exists as '. $oldProps[$propKey] .' . Use editProp to overwrite.';
|
|
|
|
} else {
|
|
|
|
$newProps = array($propKey => $propVal);
|
|
|
|
$props = array_replace($oldProps, $newProps);
|
|
|
|
$data['properties'] = $props;
|
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'Could not write to resource file';
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Property element added to '.$resource['acct'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
|
|
|
$return['message'] = 'Account '. $resource['acct'] .' not found.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "This function requires both propKey and propVal, please check your query,";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
|
|
|
$return['message'] = "You can only add new resource properties with correct credentials";
|
|
|
|
$return['info'] = $reauth['info'];
|
|
|
|
}
|
2018-01-23 16:39:07 +01:00
|
|
|
} elseif(isset($_POST['editProp'])) {
|
2018-01-27 09:05:45 +01:00
|
|
|
$resource = $_POST['editProp'];
|
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
|
|
|
case true:
|
|
|
|
if(isset($_POST['propKey']) && isset($_POST['propVal'])) {
|
|
|
|
$propKey = $_POST['propKey'];
|
|
|
|
$propVal = $_POST['propVal'];
|
|
|
|
$resource = pews_parse_account_string( $resource );
|
|
|
|
$acct_file = PEWS_DATA_STORE .'/'. $resource['host'] .'/'. $resource['user'] .'.json';
|
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$oldProps = isset($data['properties']) ? $data['properties'] : array();
|
|
|
|
$newProps = array($propKey => $propVal);
|
|
|
|
$props = array_replace($oldProps, $newProps);
|
|
|
|
$data['properties'] = $props;
|
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'Could not write to resource file';
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Property for'. $resource['acct'] .' updated.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
|
|
|
$return['message'] = 'Account '. $resource['acct'] .' not found.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "This function requires both propKey and propVal, please check your query,";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
|
|
|
$return['message'] = "You can only edit resource properties with correct credentials";
|
|
|
|
$return['info'] = $reauth['info'];
|
|
|
|
}
|
2018-01-23 16:39:07 +01:00
|
|
|
} elseif(isset($_POST['delProp'])) {
|
2018-01-27 09:05:45 +01:00
|
|
|
$resource = $_POST['delProp'];
|
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
|
|
|
case true:
|
|
|
|
if(isset($_POST['propKey'])) {
|
|
|
|
$propKey = $_POST['propKey'];
|
|
|
|
$resource = pews_parse_account_string( $resource );
|
|
|
|
$acct_file = PEWS_DATA_STORE .'/'. $resource['host'] .'/'. $resource['user'] .'.json';
|
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$props = isset($data['properties']) ? $data['properties'] : array();
|
|
|
|
if(array_key_exists($propKey, $props)){
|
|
|
|
unset($props[$propKey]);
|
2018-01-27 17:15:33 +01:00
|
|
|
if(empty($props)) {
|
|
|
|
unset ($data['properties']);
|
|
|
|
} else {
|
|
|
|
$data['properties'] = $props;
|
|
|
|
}
|
2018-01-27 09:05:45 +01:00
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
|
|
|
http_response_code(500);
|
|
|
|
$return['statusCode'] = 500;
|
|
|
|
$return['message'] = 'Could not write to resource file';
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Property for '. $resource['acct'] .' deleted.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(200);
|
|
|
|
$return['statusCode'] = 200;
|
|
|
|
$return['message'] = 'Nothing to delete, property already absent from server.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
|
|
|
$return['message'] = 'Account ['. $resource['acct'] .'] not found.';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "Missing parameter: propKey, please check your query,";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
|
|
|
$return['message'] = "You can only delete resource properties with correct credentials";
|
|
|
|
$return['info'] = $reauth['info'];
|
|
|
|
}
|
2018-01-23 16:39:07 +01:00
|
|
|
} elseif(isset($_POST['addLink'])) {
|
|
|
|
// Do Something
|
|
|
|
} elseif(isset($_POST['editLink'])) {
|
|
|
|
// Do Something
|
|
|
|
} elseif(isset($_POST['delLink'])) {
|
|
|
|
// Update a Password
|
|
|
|
} elseif(isset($_POST['updatePass'])) {
|
2018-01-26 20:16:01 +01:00
|
|
|
$resource = $_POST['updatePass'];
|
2018-01-23 16:39:07 +01:00
|
|
|
switch ($auth) {
|
|
|
|
case false:
|
2018-01-26 20:16:01 +01:00
|
|
|
$reauth = pews_auth( $resource, $password );
|
|
|
|
$auth = $reauth['class'];
|
2018-01-23 16:39:07 +01:00
|
|
|
case true:
|
2018-01-26 20:16:01 +01:00
|
|
|
$resource = pews_parse_account_string( $resource );
|
2018-01-23 16:39:07 +01:00
|
|
|
if(isset($_POST['newPass'])) {
|
|
|
|
$newPass = $_POST['newPass'];
|
2018-01-26 15:41:45 +01:00
|
|
|
$acct_file = PEWS_DATA_STORE .'/'. $resource['host'] .'/'. $resource['user'] .'.json';
|
2018-01-23 16:39:07 +01:00
|
|
|
if (file_exists($acct_file)) {
|
|
|
|
$data = json_decode(file_get_contents($acct_file), true);
|
|
|
|
$userData = $data['PEWS'];
|
|
|
|
$class = $userData['class'];
|
|
|
|
$hash = password_hash( $newPass, PASSWORD_DEFAULT);
|
|
|
|
$data['PEWS'] = array('class' => $class, 'pass' => 'pews-hashed:'.$hash);
|
|
|
|
$data = json_encode($data, JSON_UNESCAPED_SLASHES);
|
|
|
|
$success = file_put_contents( $acct_file, $data );
|
|
|
|
if($success === false) {
|
|
|
|
$return['is'] = false;
|
|
|
|
$return['info'] = 'Could not write to auth file';
|
|
|
|
} else {
|
|
|
|
$return['is'] = true;
|
|
|
|
$return['info'] = 'password updated';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(404);
|
|
|
|
$return['statusCode'] = 404;
|
2018-01-26 15:41:45 +01:00
|
|
|
$return['message'] = 'Account ['. $resource['acct'] .'] not found.';
|
2018-01-23 16:39:07 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "Missing newPass, please check your query,";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
http_response_code(401);
|
|
|
|
$return['statusCode'] = 401;
|
2018-01-27 09:05:45 +01:00
|
|
|
$return['message'] = "You can add only change your own password with correct credentials";
|
2018-01-23 16:39:07 +01:00
|
|
|
$return['info'] = $reauth['info'];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
http_response_code(400);
|
|
|
|
$return['statusCode'] = 400;
|
|
|
|
$return['message'] = "Missing parameter, please check your query,";
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
?>
|