SkayoDB
  • Class

Classes

  • SkayoDB
  1 <?php
  2 /**
  3  * <b>SkayoDB</b>
  4  *
  5  * SkayoDB is a Flat-File-Database System made by Skayo.
  6  * It uses XML for storing data and .htaccess to deny access from anyone. So it's save too! 
  7  * SkayoDB is free and easy to use and for everyone who don't want to spend money on a MySQL-Server like me.
  8  *
  9  * <b>Installation:
 10  * <code>
 11  * <?php
 12  * ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)');
 13  * file_put_contents('SkayoDB.php', file_get_contents('http://skayo.lima-city.de/tests/SkayoDB/SkayoDBxml.txt'));
 14  * require_once('SkayoDB.php');
 15  * $db = new SkayoDB;
 16  * echo $db->setup();
 17  * ?>
 18  * </code>
 19  *
 20  * @author Skayo
 21  * @version 1.0
 22  * @license http://opensource.org/licenses/bsd-license.php BSD License
 23  */
 24 class SkayoDB{
 25 
 26     /**
 27      *  Configuration-Array
 28      *  
 29      *  It contains two values:<br>
 30      *  <h3>"debug":</h3> (default: True)<br>
 31      *  <p>Setting debug to true will return all errors.</p>
 32      *  <br>
 33      *  <h3>"DB_dir":</h3> (default: "DB")<br>
 34      *  <p>If the path is not DB you can set this to where the DB-Directory is. <br>
 35      *  So for example if you want the DB-Directory to be data/DB/, you set the path to data/DB. </p>
 36      *  
 37      *  @var array
 38      */
 39     public $config;
 40 
 41     function __construct($config = array()){
 42         if(count($config) == 0){
 43             $this->config = array(
 44                 'debug' => True,
 45                 'DB_dir' => 'DB',
 46                 );
 47         }
 48         if($this->config["debug"]){error_reporting(E_ALL);};
 49     }
 50 //-----------------------------------------------------------------
 51 
 52     /**
 53      *  Call once to create the DB-Folder and a htaccess-File
 54      *  
 55      *  @return void
 56      */
 57     function setup(){
 58         if(!file_exists($this->config["DB_dir"])){
 59             mkdir($this->config["DB_dir"]);
 60         }
 61             if(!file_exists($this->config["DB_dir"]."/.htaccess")){
 62             $handle = fopen($this->config["DB_dir"]."/.htaccess", "w");
 63             fwrite($handle, '<Files ~ "\.xml$">
 64 Order deny,allow
 65 Deny from all
 66 </Files>');
 67             fclose($handle);
 68         }
 69     }
 70 
 71     /**
 72      *  Creates a new databank
 73      *  
 74      *  @param string $name The name of the databank
 75      *  @return string If debug is true, it returns a message
 76      */
 77     function createDB($name){
 78         if(is_writable($this->config["DB_dir"]) && !file_exists($this->config["DB_dir"]."/".$name.".xml")){
 79             $handle = fopen($this->config["DB_dir"]."/".$name.".xml", "w");
 80             fwrite($handle, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 81 <databank>
 82     <'.$name.'>
 83     </'.$name.'>
 84 </databank>');
 85             fclose($handle);
 86             if(file_exists($this->config["DB_dir"]."/".$name.".xml")){
 87                 if($this->config["debug"]){
 88                 return "Done";
 89                 }
 90             } else {
 91                 if($this->config["debug"]){
 92                 return "Databank couldn't be created! Please check.";
 93                 }
 94             }
 95         } else {
 96             if($this->config["debug"]){
 97             return "Databank-Dir is not writeable or file already exists! Please check.";
 98             }
 99         }
100     }
101 
102     /**
103      *  Used to create a new XML-Node
104      *  @param   string  $DBname         The name of the database you want to use
105      *  @param   string  $content        The content you want to store in that node
106      *  @param   string  $elementname    The name of the node you want to create
107      *  @param   string  $parent         The name of the parent wich you want to create a child from
108      *  @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
109      *  @param   string  $attributename  The name of the attribute, if you want to have an attribute for the node
110      *  @param   string  $attributevalue The value of the attribute, if you want to have an attribute for the node
111      *  @return string                   If debug is true, it returns a message
112      */
113     function newXMLelement($DBname, $content, $elementname, $parent, $parentnum = 0, $attributename = NULL, $attributevalue = NULL){
114         if(file_exists($this->config["DB_dir"]."/".$DBname.".xml")){
115             $doc = new DOMDocument();
116             $xpath = new DOMXPath($doc);
117             $doc->load($this->config["DB_dir"]."/".$DBname.".xml");
118             $parents = $doc->getElementsByTagName($parent)->item($parentnum);
119             if($parents == NULL){
120                 if($this->config["debug"]){
121                     return "Element not found! Please check";
122                 }
123             } else {
124                 $element = $doc->createElement($elementname, $content);
125                 if($attributename !== NULL && $attributevalue !== NULL){
126                     $attribute = $doc->createAttribute($attributename);
127                     $attribute->value = $attributevalue;
128                     $element->appendChild($attribute);
129                 }
130                 $parents->appendChild($element);
131                 $doc->save($doc->documentURI);
132                 if($this->config["debug"]){
133                     return "Done";
134                 }
135             }
136         } else {
137             if($this->config["debug"]){
138                 return "Databank does not exits! Please check.";
139             }
140         }
141     }
142 
143     /**
144      *  Used to update the value of an existing node.
145      *  @param   string  $DBname      The name of the database you want to use
146      *  @param   string  $newValue    The new value you want to store
147      *  @param   string  $elementname The name of the node you want to update
148      *  @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
149      *  @return string                If debug is true, it returns a message
150      */
151     function updateXMLvalue($DBname, $newValue, $elementname, $elementnum = 0){
152         $doc = new DOMDocument();
153         $xpath = new DOMXPath($doc);
154         $doc->load($this->config["DB_dir"]."/".$DBname.".xml");
155         $newElement = $doc->getElementsByTagName($elementname)->item($elementnum);
156         if($newElement == NULL){
157             if($this->config["debug"]){
158                 return "Element not found! Please check";
159             }
160         } else {
161             $newElement->nodeValue = $newValue;
162             $doc->save($doc->documentURI);
163             if($this->config["debug"]){
164                 return "Done";
165             }
166         }
167     }
168 
169     /**
170      *  Used to get the value of an existing node
171      *  @param   string  $DBname         The name of the database you want to use
172      *  @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)
173      *  @param   string  $elementname    The name of the node you want to get the information from
174      *  @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
175      *  @param   string  $attributename  The name of the attribute you want to get the value from
176      *  @param   string  $attributevalue The value of the attribute you want to get the value from
177      *  @return string                   If debug is true, it returns a message
178      */
179     function getXML($DBname, $mode, $elementname, $elementnum = 0, $attributename = NULL, $attributevalue = NULL){
180         $doc = new DOMDocument();
181         $xpath = new DOMXPath($doc);
182         $doc->load($this->config["DB_dir"]."/".$DBname.".xml");
183         if($mode == "value"){
184             if($attributename !== NULL && $attributevalue !== NULL){
185                 $search = $doc->getElementsByTagName($elementname);
186                 if($search == NULL){
187                     if($this->config["debug"]){
188                         return "Element not found! Please check";
189                     }
190                 } else {
191                     foreach ($search as $node) {
192                         if($node->getAttribute($attributename) == $attributevalue){
193                             $found = $node->nodeValue;
194                             if($found == NULL or $found == ""){
195                                 if($this->config["debug"]){
196                                     return "Element has no value! Please check.";
197                                 }
198                             } else {
199                                 return $found;
200                             }
201                         }
202                     }
203                 }
204             } else {
205                 $search = $doc->getElementsByTagName($elementname)->item($elementnum);
206                 if($search == NULL){
207                     if($this->config["debug"]){
208                         return "Element not found! Please check";
209                     }
210                 } else {
211                     $found = $search->nodeValue;
212                     if($found == NULL or $found == ""){
213                         if($this->config["debug"]){
214                             return "Element has no value! Please check.";
215                         }
216                     } else {
217                         return $found;
218                     }   
219                 }
220             }
221         } elseif ($mode == "number" && $attributename !== NULL && $attributevalue !== NULL){
222             $search = $doc->getElementsByTagName($elementname);
223             if($search == NULL){
224                     if($this->config["debug"]){
225                         return "Element not found! Please check";
226                     }
227             } else {
228                 $count = 0;
229                 foreach ($search as $node) {
230                     $count++;
231                     if($node->getAttribute($attributename) == $attributevalue){
232                         return $count;
233                     }
234                 }
235             }
236         } elseif ($mode == "count") {
237             if($attributename !== NULL && $attributevalue !== NULL){
238                 $search = $doc->getElementsByTagName($elementname);
239                 if($search == NULL){
240                     if($this->config["debug"]){
241                         return "Element not found! Please check";
242                     }
243                 } else {
244                     $count = 0;
245                     foreach ($search as $node) {
246                         if($node->getAttribute($attributename) == $attributevalue){
247                             $count++;
248                         }
249                     }
250                     return $count;
251                 }
252             } else {
253                 $search = $doc->getElementsByTagName($elementname);
254                 $count = 0;
255                 foreach ($search as $node) {
256                     $count++;
257                 }
258                 return $count;
259             }
260         }
261     }
262 
263     /**
264      *  Used to remove an existing node
265      *  @param   string  $DBname      The name of the database you want to use
266      *  @param   string  $parentname  The name of the parent you want to delete a child from
267      *  @param   integer $parentnum   The number of the parent you want to delete a child from
268      *  @param   string  $elementname The name of the node you want to delete
269      *  @param   integer $elementnum  The number of the node you want to delete
270      *  @return string                If debug is true, it returns a message
271      */
272     function removeXMLnode($DBname, $parentname, $parentnum = 0, $elementname, $elementnum = 0){
273         $doc = new DOMDocument();
274         $xpath = new DOMXPath($doc);
275         $doc->load($this->config["DB_dir"]."/".$DBname.".xml");
276         $parent = $doc->getElementsByTagName($parentname)->item($parentnum);
277         $del = $parent->getElementsByTagName($elementname)->item($elementnum);
278         if($parent == NULL || $del == NULL){
279             if($this->config["debug"]){
280                 return "Could not find Node with the given parentname or elementname! Please check.";
281             }
282         } else {
283             $parent->removeChild($del);
284             $doc->save($doc->documentURI);
285             if($this->config["debug"]){
286                 return "Done";
287             }
288         }
289     }
290 }
291 ?>
SkayoDB API documentation generated by ApiGen