/addRemoveGroups/addGroup.php |
@@ -0,0 +1,258 @@ |
<?php |
|
########################################################################### |
## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
########################################################################### |
## This is a script that creates a Corrade group from a JSON object. ## |
########################################################################### |
|
########################################################################### |
## GET POST VARIABLES ## |
########################################################################### |
|
if(!isset($_POST['group'])) return -1; |
|
########################################################################### |
## CONFIGURATION ## |
########################################################################### |
|
# The path to the configuration. |
include_once('config.php'); |
|
########################################################################### |
## INTERNALS ## |
########################################################################### |
|
# This function adds a Corrade group from a JSON object $JSONdata |
# The JSON object has the following structure with example data: |
# { |
# "ChatLog":"logs/groupLog.txt", |
# "ChatLogEnabled":false, |
# "DatabaseFile":"databases/groupDatabase.db", |
# "Workers":5, |
# "Notifications":[ |
# "alert", |
# "typing", |
# "invite" |
# ], |
# "Permissions":[ |
# "group", |
# "movement" |
# ], |
# "Name":"Nanny & Baby", |
# "UUID":"708cc093-22b9-43e9-9682-6b4f284c75dd", |
# "Password":"badmoonrising" |
# } |
function AddCorradeGroup( |
$domDocument, |
$JSONdata, |
$ALLOWED_PERMISSION = [ |
'movement', |
'economy', |
'land', |
'grooming', |
'inventory', |
'interact', |
'mute', |
'database', |
'notifications', |
'talk', |
'directory', |
'system', |
'friendship', |
'execute', |
'group', |
'filter' |
], |
$ALLOWED_NOTIFICATIONS = [ |
'alert', |
'region', |
'group', |
'balance', |
'local', |
'message', |
'notice', |
'dialog', |
'friendship', |
'inventory', |
'permission', |
'lure', |
'effect', |
'collision', |
'crossing', |
'terse', |
'typing', |
'invite', |
'economy', |
'membership', |
'url', |
'ownersay', |
'regionsayto', |
'objectim', |
'rlv', |
'debug', |
'avatars', |
'primitives', |
'control' |
]) { |
# Decode JSON |
$JSONdata = json_decode($JSONdata); |
|
# First, create a new group element. |
$newGroup = $domDocument->createElement('Group'); |
|
# Create the chat-log entry. |
if(isset($JSONdata->ChatLog)) { |
$chatLog = $domDocument->createElement('ChatLog'); |
$chatLog->appendChild( |
$domDocument->createTextNode($JSONdata->ChatLog) |
); |
$newGroup->appendChild($chatLog); |
} |
# Create the chat-log enabled entry. |
if(isset($JSONdata->ChatLogEnabled)) { |
$chatLogEnabled = $domDocument->createElement('ChatLogEnabled'); |
$chatLogEnabled->appendChild( |
$domDocument->createTextNode($JSONdata->ChatLogEnabled) |
); |
$newGroup->appendChild($chatLogEnabled); |
} |
# Create the database file entry. |
if(isset($JSONdata->DatabaseFile)) { |
$databaseFile = $domDocument->createElement('DatabaseFile'); |
$databaseFile->appendChild( |
$domDocument->createTextNode($JSONdata->DatabaseFile) |
); |
$newGroup->appendChild($databaseFile); |
} |
# Create the group name entry. |
if(isset($JSONdata->Name)) { |
$groupName = $domDocument->createElement('Name'); |
$groupName->appendChild( |
$domDocument->createTextNode($JSONdata->Name) |
); |
$newGroup->appendChild($groupName); |
} |
# Create the group UUID. |
if(isset($JSONdata->UUID)) { |
$groupGUID = $domDocument->createElement('Guid'); |
$groupGUID->appendChild( |
$domDocument->createTextNode($JSONdata->UUID) |
); |
$groupUUID = $domDocument->createElement('UUID'); |
$groupUUID->appendChild($groupGUID); |
$newGroup->appendChild($groupUUID); |
} |
# Create the group password entry. |
if(isset($JSONdata->Password)) { |
$groupPassword = $domDocument->createElement('Password'); |
$groupPassword->appendChild( |
$domDocument->createTextNode($JSONdata->Password) |
); |
$newGroup->appendChild($groupPassword); |
} |
# Create the group workers. |
if(isset($JSONdata->Workers)) { |
$groupWorkers = $domDocument->createElement('Workers'); |
$groupWorkers->appendChild( |
$domDocument->createTextNode($JSONdata->Workers) |
); |
$newGroup->appendChild($groupWorkers); |
} |
# Create the group notifications. |
if(count($JSONdata->Notifications)) { |
$groupNotifications = $domDocument->createElement('Notifications'); |
foreach($JSONdata->Notifications as $notification) { |
# if the notification is not allowed, skip it |
if(!in_array($notification, $ALLOWED_NOTIFICATIONS)) |
continue; |
$notificationElement = $domDocument-> |
createElement('Notifications'); |
$notificationElement->appendChild( |
$domDocument->createTextNode($notification) |
); |
$groupNotifications->appendChild($notificationElement); |
} |
$newGroup->appendChild($groupNotifications); |
} |
#Create the group permissions. |
if(count($JSONdata->Permissions)) { |
$groupPermissions = $domDocument->createElement('Permissions'); |
foreach($JSONdata->Permissions as $permission) { |
# if the permission is not allowed, skip it |
if(!in_array($permission, $ALLOWED_PERMISSION)) |
continue; |
$permissionElement = $domDocument-> |
createElement('Permissions'); |
$permissionElement->appendChild( |
$domDocument->createTextNode($permission) |
); |
$groupPermissions->appendChild($permissionElement); |
} |
$newGroup->appendChild($groupPermissions); |
} |
|
# Finally, add the new group to the groups. |
$groups = $domDocument->getElementsByTagName('Groups'); |
switch($groups->length != 0) { |
case true: # The "Groups" node exists, so just append. |
$groups[0]->appendChild($newGroup); |
break; |
default: # The "Groups" node does not exist, so create it. |
$groups = $domDocument->createElement('Groups'); |
$groups[0]->appendChild($newGroup); |
break; |
} |
|
return $domDocument; |
} |
|
# This function checks whether a Corrade group by name and UUID exists. |
# The JSON must contain "Name" set to the name of the group and "UUID" |
# set to the UUID of the group to be searched. |
function DoesCorradeGroupExist($domDocument, $JSONdata) { |
$JSONdata = json_decode($JSONdata); |
$groups = $domDocument->getElementsByTagName('Groups'); |
if($groups->length == 0) return false; |
foreach($groups[0]->getElementsByTagName('Group') as $group) { |
if( |
$group->getElementsByTagName('Name')[0]-> nodeValue == |
$JSONdata->Name && |
$group->getElementsByTagName('UUID')[0]-> |
getElementsByTagName('Guid')[0]->nodeValue == |
$JSONdata->UUID |
) return true; |
} |
return false; |
} |
|
########################################################################### |
## DEMO ## |
########################################################################### |
|
#### |
# I. Read the configuration file. |
$doc = new DomDocument('1.0'); |
# This is needed to preserve the indenting of Corrade.ini |
$doc->formatOutput = true; |
$doc->preserveWhiteSpace = false; |
$doc->load('Corrade.ini'); |
|
#### |
# II. Create a new group if it does not already exist. |
switch(DoesCorradeGroupExist($doc, $_POST['group'])) { |
case false: # only add the group if it does not exist |
$doc = AddCorradeGroup($doc, $_POST['group']); |
break; |
default: # the group already exists - panic! |
break; |
} |
|
#### |
# III. Save the configuration file. |
$doc->save('Corrade.ini'); |
|
return 0; |
|
|
?> |