config = array( 'debug' => True, 'DB_dir' => 'DB', ); if($this->config["debug"]){error_reporting(E_ALL);}; } //----------------------------------------------------------------- /** * Call once to create the DB-Folder and a htaccess-File * @returns null */ function setup(){ if(!file_exists($this->config["DB_dir"])){ mkdir($this->config["DB_dir"]); } if(!file_exists($this->config["DB_dir"]."/.htaccess")){ $handle = fopen($this->config["DB_dir"]."/.htaccess", "w"); fwrite($handle, ' Order deny,allow Deny from all '); fclose($handle); } } /** * Creates a new databank * @param string $name The name of the databank * @returns string If debug is true, it returns a message */ function createDB($name){ if(is_writable($this->config["DB_dir"]) && !file_exists($this->config["DB_dir"]."/".$name.".xml")){ $handle = fopen($this->config["DB_dir"]."/".$name.".xml", "w"); fwrite($handle, ' <'.$name.'> '); fclose($handle); if(file_exists($this->config["DB_dir"]."/".$name.".xml")){ if($this->config["debug"]){ return "Done"; } } else { if($this->config["debug"]){ return "Databank couldn't be created! Please check."; } } } else { if($this->config["debug"]){ return "Databank-Dir is not writeable or file already exists! Please check."; } } } /** * Used to create a new XML-Node * @param string $DBname The name of the database you want to use * @param string $content The content you want to store in that node * @param string $elementname The name of the node you want to create * @param string $parent The name of the parent wich you want to create a child from * @param integer $parentnum If you have more than one parent of a name, you can choose the number of the parent you want to use * @param string $attributename The name of the attribute, if you want to have an attribute for the node * @param string $attributevalue The value of the attribute, if you want to have an attribute for the node * @returns string If debug is true, it returns a message */ function newXMLelement($DBname, $content, $elementname, $parent, $parentnum = 0, $attributename = NULL, $attributevalue = NULL){ if(file_exists($this->config["DB_dir"]."/".$DBname.".xml")){ $doc = new DOMDocument(); $xpath = new DOMXPath($doc); $doc->load($this->config["DB_dir"]."/".$DBname.".xml"); $parents = $doc->getElementsByTagName($parent)->item($parentnum); if($parents == NULL){ if($this->config["debug"]){ return "Element not found! Please check"; } } else { $element = $doc->createElement($elementname, $content); if($attributename !== NULL && $attributevalue !== NULL){ $attribute = $doc->createAttribute($attributename); $attribute->value = $attributevalue; $element->appendChild($attribute); } $parents->appendChild($element); $doc->save($doc->documentURI); if($this->config["debug"]){ return "Done"; } } } else { if($this->config["debug"]){ return "Databank does not exits! Please check."; } } } /** * Used to update the value of an existing node. * @param string $DBname The name of the database you want to use * @param string $newValue The new value you want to store * @param string $elementname The name of the node you want to update * @param integer $elementnum If you have more than one nodes of a name, you can choose the number of the node you want to update * @returns string If debug is true, it returns a message */ function updateXMLvalue($DBname, $newValue, $elementname, $elementnum = 0){ $doc = new DOMDocument(); $xpath = new DOMXPath($doc); $doc->load($this->config["DB_dir"]."/".$DBname.".xml"); $newElement = $doc->getElementsByTagName($elementname)->item($elementnum); if($newElement == NULL){ if($this->config["debug"]){ return "Element not found! Please check"; } } else { $newElement->nodeValue = $newValue; $doc->save($doc->documentURI); if($this->config["debug"]){ return "Done"; } } } /** * Used to get the value of an existing node * @param string $DBname The name of the database you want to use * @param string $mode The mode you want to use. You can choose between value (to get the value), number (to get the number of an node) and count (to get the number of existing nodes) * @param string $elementname The name of the node you want to get the information from * @param integer $elementnum If you have more than one nodes of a name, you can choose the number of the node you want to get the information from * @param string $attributename The name of the attribute you want to get the value from * @param string $attributevalue The value of the attribute you want to get the value from * @returns string If debug is true, it returns a message */ function getXML($DBname, $mode, $elementname, $elementnum = 0, $attributename = NULL, $attributevalue = NULL){ $doc = new DOMDocument(); $xpath = new DOMXPath($doc); $doc->load($this->config["DB_dir"]."/".$DBname.".xml"); if($mode == "value"){ if($attributename !== NULL && $attributevalue !== NULL){ $search = $doc->getElementsByTagName($elementname); if($search == NULL){ if($this->config["debug"]){ return "Element not found! Please check"; } } else { foreach ($search as $node) { if($node->getAttribute($attributename) == $attributevalue){ $found = $node->nodeValue; if($found == NULL or $found == ""){ if($this->config["debug"]){ return "Element has no value! Please check."; } } else { return $found; } } } } } else { $search = $doc->getElementsByTagName($elementname)->item($elementnum); if($search == NULL){ if($this->config["debug"]){ return "Element not found! Please check"; } } else { $found = $search->nodeValue; if($found == NULL or $found == ""){ if($this->config["debug"]){ return "Element has no value! Please check."; } } else { return $found; } } } } elseif ($mode == "number" && $attributename !== NULL && $attributevalue !== NULL){ $search = $doc->getElementsByTagName($elementname); if($search == NULL){ if($this->config["debug"]){ return "Element not found! Please check"; } } else { $count = 0; foreach ($search as $node) { $count++; if($node->getAttribute($attributename) == $attributevalue){ return $count; } } } } elseif ($mode == "count") { if($attributename !== NULL && $attributevalue !== NULL){ $search = $doc->getElementsByTagName($elementname); if($search == NULL){ if($this->config["debug"]){ return "Element not found! Please check"; } } else { $count = 0; foreach ($search as $node) { if($node->getAttribute($attributename) == $attributevalue){ $count++; } } return $count; } } else { $search = $doc->getElementsByTagName($elementname); $count = 0; foreach ($search as $node) { $count++; } return $count; } } } /** * Used to remove an existing node * @param string $DBname The name of the database you want to use * @param string $parentname The name of the parent you want to delete a child from * @param integer $parentnum The number of the parent you want to delete a child from * @param string $elementname The name of the node you want to delete * @param integer $elementnum The number of the node you want to delete * @returns string If debug is true, it returns a message */ function removeXMLnode($DBname, $parentname, $parentnum = 0, $elementname, $elementnum = 0){ $doc = new DOMDocument(); $xpath = new DOMXPath($doc); $doc->load($this->config["DB_dir"]."/".$DBname.".xml"); $parent = $doc->getElementsByTagName($parentname)->item($parentnum); $del = $parent->getElementsByTagName($elementname)->item($elementnum); if($parent == NULL || $del == NULL){ if($this->config["debug"]){ return "Could not find Node with the given parentname or elementname! Please check."; } } else { $parent->removeChild($del); $doc->save($doc->documentURI); if($this->config["debug"]){ return "Done"; } } } } ?>