Advertisement
imoda

XML creation utilities

Aug 31st, 2011
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.68 KB | None | 0 0
  1. <?php
  2.  
  3.     function full_copy($source, $target) {
  4.        
  5.         $source = (substr($source, -1) == '/' ? substr($source, 0, -1) : $source);
  6.         $target = (substr($target, -1) == '/' ? substr($target, 0, -1) : $target);
  7.        
  8.         if (is_dir($source)) {
  9.            
  10.             @mkdir($target, true);
  11.            
  12.             $d = dir($source);
  13.            
  14.             while (FALSE !== ($entry = $d->read())) {
  15.                
  16.                 if ($entry == '.' || $entry == '..') {
  17.                    
  18.                     continue;
  19.                 }
  20.                
  21.                 $Entry = $source . '/' . $entry;
  22.                
  23.                 if (is_dir($Entry)) {
  24.                    
  25.                     full_copy($Entry, $target . '/' . $entry);
  26.                     continue;
  27.                 }
  28.                
  29.                 copy($Entry, $target . '/' . $entry);
  30.            
  31.                 echo "Copied {$Entry} to " . $target . '/' . $entry . "\n";
  32.                
  33.                 modPath($target . '/' . $entry);
  34.             }
  35.            
  36.             $d->close();
  37.         }
  38.         else {
  39.            
  40.             copy($source, $target);
  41.            
  42.             echo "Copied {$source} to {$target}\n";
  43.            
  44.             modPath($target);
  45.         }
  46.     }
  47.    
  48.     function createDir($path) {
  49.        
  50.         if (!is_dir($path)) { // if the dir doesn't exist
  51.            
  52.             echo "Making directory at: {$path}\n";
  53.            
  54.             mkdir($path, 755, true); // create the dir (recursively)
  55.         }
  56.         else {
  57.            
  58.             echo "Directory exists at: {$path}\n";
  59.         }
  60.                
  61.         modPath($path);
  62.     }
  63.    
  64.     function modPath($path) {
  65.        
  66.         if (is_dir($path)) {
  67.            
  68.             $mod = 0755;
  69.         }
  70.         else {
  71.            
  72.             $mod = 0644;
  73.         }
  74.                
  75.         chmod($path, $mod);
  76.        
  77.         $aPath = explode("/", $path);
  78.        
  79.         chown($path, $aPath[2]);
  80.         chgrp($path, $aPath[2]);
  81.        
  82.         echo "Ownership of {$path} given to user: " .  $aPath[2] . "\n";
  83.     }
  84.  
  85.     class xmldoc {
  86.        
  87.         function build($name = 'index') {
  88.            
  89.             $dom = new DOMDocument("1.0", "utf-8");
  90.             $dom->formatOutput = true;
  91.             $root = $dom->createElement($name);
  92.             $dom->appendChild($root);
  93.            
  94.             return array($root, $dom);
  95.         }
  96.        
  97.         function add($xmlObject, $parent, $field, $value = false, $textNode = false) {
  98.            
  99.             $dom = $xmlObject[1];
  100.            
  101.             $field = $dom->createElement($field);
  102.             $parent->appendChild($field);
  103.            
  104.             if ($value !== false) {
  105.                
  106.                
  107.                 $text = ($textNode !== false ? $dom->createTextNode($value) : $dom->createCDATASection($value));
  108.                 $field->appendChild($text);
  109.             }
  110.            
  111.             return $field;
  112.         }
  113.        
  114.         function attr($xmlObject, $field, $name, $text) {
  115.            
  116.             $dom = $xmlObject[1];
  117.            
  118.             $name = $dom->createAttribute($name);
  119.             $field->appendChild($name);
  120.            
  121.             $value = $dom->createTextNode($text);
  122.             $name->appendChild($value);
  123.         }
  124.        
  125.         function save($xmlObject, $path, $save = true) {
  126.            
  127.             $dom = $xmlObject[1];
  128.            
  129.             echo $dom->saveXML();
  130.            
  131.             if ($save === true) {
  132.                
  133.                 echo "Wrote to {$path}, " . $dom->save($path) . " bytes.\n" . date('r', time()) . "\n";
  134.                
  135.                 modPath($path);
  136.             }
  137.         }
  138.     }
  139.  
  140. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement