Генерирует карту сайта!

                        
<?php/**   * Class to generate the sitemap ( outputa dir structure)   * Thsi class outputs the result in XMl or displays the result on the browser   * @author Rochak Chauhan   */  class Sitemap  {    private $_dirArray = array() ;    private $_fileArray = array() ;    private $_dirName = '/';    private $_nameArray = array();    private static $_flag = true;    private $_nodeFlag = true;    private $_dom = '';    private $_root = '';    private $_fileNode = '';    private $_dirNode = '';    private $_childDirNode = '';    /**     * Contruction fucntion     * @param string $dirName     * @return void     */    public function __construct($dirName)    {      if (trim($dirName) != '') $this->_dirName = trim($dirName);    }    /**     * Function to convert time in secs intomins     * @param $timeInSec int     * $return string     */    public function convertSecToMins($timeInSec)    {      if( $timeInSec < 60 )      {        if( $timeInSec > 1)              $secString = "secs";        else              $secString = "sec";        $timeTaken = "$timeInSec $secString";      }      else      {        $seconds = ($timeInSec % 60);        $minutes = ($timeInSec/60);        $minutes = sprintf("%01.0f", $minutes);        if( $minutes > 1)              $minString = "mins";        else              $minString = "min";        if( $seconds > 1)              $secString = "secs";        else              $secString = "sec";        $timeTaken = "$minutes $minString $seconds $secString";      }      return trim($timeTaken);    }    /**    * Function to read a directory recursively and finds a string in all the files.    * it also returns the number of times a sting was found in a file    * @param $path string    * @return mixed    */    public function readDirectory($path = '')    {      if (trim($path) == '') $path = $this->_dirName;      $returnArray = array();      $handle = @opendir($path);      while ($file = @readdir($handle) )      {        if (is_dir($path.$file) && $file != "." && $file != "..")       { $sub_dir = $path . $file . "/" ;         array_push($this->_dirArray, $sub_dir);         $this->readDirectory($sub_dir);       }        elseif (is_file($path.$file) && $file != "." && $file != "..")       { $sub_dir = $path . $file ;         array_push($this->_fileArray, $sub_dir);       }      }      $returnArray = array($this->_dirArray, $this->_fileArray );      return $returnArray;    }    /**     * This function generates the sitemap     * @return mixed     */    public function generateXmlSiteMap($writeToFile = false)    { $mainArray = $this->readDirectory('');      $fileArray = $mainArray[1];      $this->startDomXml('sitemap');      for ($i=0; $i < count($fileArray); $i++)      { $fileName = trim(substr(strrchr($fileArray[$i], '/'), 1));        $dirName = trim(str_replace($fileName, '', $fileArray[$i]));        $this->populateArray($dirName);      }      $foldersInRootDir = $this->getFoldersInRootDir();      foreach ($this->_nameArray as $name=>$key)      { $this->generateNodes($name, $key , $foldersInRootDir);      }      if ($writeToFile)      { $fp = fopen('xmlOutput.xml', 'w+') or die("<B>ERROR: </B> Failed to open file for writing.");        fwrite($fp, $this->_dom->saveXML()) or die("<B>ERROR: </B> Failed to write contents to the file.");        fclose($fp);        echo "Content written to file: <a href='xmlOutput.xml'> Click to open.</a>";      }      else     { return $this->_dom->saveXML();     }    }    /**     * This function generates the nodes     * @param string $name     * @param array $foldersInRootDir     * @param array $key     * @return void     */    private function generateNodes($name, $key, $foldersInRootDir)    {      $childDirArray = $this->getChildDir($name);      /*echo "<pre>";      echo $name."<br>";      print_r($foldersInRootDir);      print_r($this->_dirArray);      print_r($childDirArray);      echo "<HR>";*/      if (in_array($name, $foldersInRootDir))      {        $this->createDirNode('dirname', $name);        for ($i=0; $i < count($key); $i++)        {          $this->createFileNode('filenode', $key[$i]);        }        if (count($childDirArray))        {          for ($i=0; $i < count($childDirArray); $i++)          {            $this->createDirNode('dirname', $childDirArray[$i], true);            $this->generateNodes($childDirArray[$i], $key, $this->_dirArray);          }        }      }    }    /**     * This fucntion creates the instance of DOM Xml object     *     * @return void     */    private function startDomXml($rootNodeName)    {      $this->_dom = new DOMDocument('1.0');      $this->_dom->formatOutput = true;      $this->_root = $this->_dom->createElement($rootNodeName);      $this->_root = $this->_dom->appendChild($this->_root);    }    /**     * This function creates a dir node in the xml file     * @param string $dirNode     * @return void     */    private function createDirNode($dirNode, $value, $isChildNode = false, $appendToChildNode = false)    {      if (!is_object($this->_dom))      {        die ("<B>ERROR:</B> Cannot call fucntion: ".__FUNCTION__." without creating dom xml object");      }      else      {        if ($appendToChildNode)          $node = $this->_childDirNode;        else          $node = $this->_dirNode;        if ($isChildNode)        {          $this->_childDirNode = $this->_dom->createElement($dirNode, $value);          $this->_childDirNode = $node->appendChild($this->_childDirNode);        }        else        {          $this->_dirNode = $this->_dom->createElement($dirNode, $value);          $this->_dirNode = $this->_root->appendChild($this->_dirNode);        }      }    }    /**     * This function creates a file node in the xml file     * @param string $fileNode     * @return void     */    private function createFileNode($fileNode, $value, $appendToChildNode = false)    {      if (!is_object($this->_dom))      {        die ("<B>ERROR:</B> Cannot call fucntion: ".__FUNCTION__." without creating dom xml object");      }      else      {        if ($appendToChildNode)        {          $this->_fileNode = $this->_dom->createElement($fileNode, $value);          $this->_fileNode = $this->_childDirNode->appendChild($this->_fileNode);        }        else        {          $this->_fileNode = $this->_dom->createElement($fileNode, $value);          $this->_fileNode = $this->_dirNode->appendChild($this->_fileNode);        }      }    }    /**     * This function returns the names of the folder inside the parent dir     * @param string $parentDir     * @access private     * @return array     */    public function getChildDir($parentDir)    {      $returnArray = array();      foreach ($this->_dirArray as $tmp)      {        if (substr_count($tmp, $parentDir))        {          $dirToBeAdded = substr($tmp, (strpos($tmp, $parentDir)+strlen($parentDir)) ) ;          if ( trim($dirToBeAdded) != '' && substr_count($dirToBeAdded, '/') == 1)          {            $returnArray[] = $parentDir.$dirToBeAdded;          }        }      }      return $returnArray;    }    /**     * This function returns the name(s) of all folders in the root/parent dir     * @param string $rootdir     * @return array     */    private function getFoldersInRootDir()    {      $returnArray = array();      foreach ($this->_dirArray as $dir)      {        $tmpArray = explode('/', $dir);        if (isset($tmpArray[1]))           { if (!in_array($this->_dirName.$tmpArray[1]."/", $returnArray))              array_push($returnArray, $this->_dirName.$tmpArray[1]."/");       }      }      return $returnArray;    }    /**     * This function populated the array fordir/file     * @param array $dirName     * @return void     */    private function populateArray($dirName)    {      $fileArray = $this->_fileArray;      for ($i=0; $i < count($fileArray); $i++)      {        $dirPath = substr($fileArray[$i], 0, strrpos($fileArray[$i], '/')).'/';        if (trim($dirName) == trim($dirPath))         $tempArray[] = trim(str_replace($dirPath, '', $fileArray[$i]));      }      $this->_nameArray[$dirName] = $tempArray;    }  }?>?>
0 12 0
0

Нет фото
Chainsaw * [мошенник]
• 27 дек 2014, 15:10


ну и код *