corrade-http-templates – Rev 2

Subversion Repositories:
Rev:
<?php

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      ##
###########################################################################
## This is a script that removes a Corrade group.                        ##
###########################################################################

###########################################################################
##                          GET POST VARIABLES                           ##
###########################################################################

if(!isset($_POST['group'])) return -1;

###########################################################################
##                            CONFIGURATION                              ##
###########################################################################

# The path to the configuration.
include_once('config.php');

###########################################################################
##                               INTERNALS                               ##
###########################################################################

# This function searches for a Corrade group and deletes it.
# The JSON must contain "Name" set to the name of the group and "UUID"
# set to the UUID of the group to be deleted.
function RemoveCorradeGroup($domDocument, $JSONdata) {
        $JSONdata = json_decode($JSONdata);
        $groups = $domDocument->getElementsByTagName('Groups');
        if($groups->length == 0) return $domDocument;
        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
                ) continue;
                $groups[0]->removeChild($group);
                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. Delete a group if it exists.
switch(DoesCorradeGroupExist($doc, $_POST['group'])) {
        case true: # only remove the group if it exists
                $doc = RemoveCorradeGroup($doc, $_POST['group']);
                break;
        default: # the group does not exist - panic!
                break;
}

####
# III. Save the configuration file.
$doc->save('Corrade.ini');


?>