<?php

/*  Alternate WebKit
(C) 2013 by Alternate Tools
-----------------------------------------------------

File:         class_systemfile.php
Description:  Class cSystemFile definition,
              to load and save text into files

*/


if (!defined("CLASS_SYSTEMFILE"))
{
    define ("CLASS_SYSTEMFILE", "DEFINED");

    class cSystemFile
    {

        // Class member variables

        function cSystemFile()  // PHP 4
        {
            $this->InitSystemFile();
        }

        function __construct()  // PHP 5
        {
            $this->InitSystemFile();
        }

        // Constructor
        function InitSystemFile()
        {
        }

        function GetFolderString($folder)
        {
            return str_replace("\\", "/", $folder);
        }

        function GetPrevFolder($folder)
        {
            $correctFolder = $this->GetFolderString($folder);
            for ($i = (strlen($correctFolder) - 2); $i > 0; $i --)
            {
                if ($correctFolder[$i] == '/')
                {
                    $correctFolder = substr($correctFolder, 0, $i + 1);
                    break;
                }
            }
            return $correctFolder;
        }

        function GetFolderContent($folder, $fileExt = "", $nothisExt = "")
        {
            $outArray = array();
            $oDir = dir($folder);

            while (false !== ($strFile = $oDir->read()))
            {
                if ($strFile != '.' && $strFile != '..' && ! is_link($folder . $strFile) && is_file($folder . $strFile))
                {
                    if ($nothisExt != "" && $this->GetFileExt($strFile) == $nothisExt)
                        continue;
                    if ($fileExt == "" || $this->GetFileExt($strFile) == $fileExt)
                    {
                        $outArray[] = $strFile;
                    }
                }
            }

            $oDir->close();

            return $outArray;
        }

        function GetFolderContentTable($folder, $fileExt = "", $nothisExt = "")
        {
            $files = $this->GetFolderContent($folder, $fileExt, $nothisExt);
            $filesTable = array();
            for ($i = 0; $i < count($files); $i ++)
            {
                $filesTable[] = array($files[$i], filesize($folder . $files[$i]), filectime($folder . $files[$i]));
            }
            return $filesTable;
        }

        function GetFolderDirs($folder)
        {
            $outArray = array();
            $oDir = dir($folder);

            while (false !== ($strFile = $oDir->read()))
            {
                if ($strFile != '.' && $strFile != '..' && is_dir($folder . $strFile))
                {
                    $outArray[] = $strFile;
                }
            }

            return $outArray;
        }

        function GetFolderDirsTable($folder)
        {
            $dirs = $this->GetFolderDirs($folder);
            $dirsTable = array();
            for ($i = 0; $i < count($dirs); $i ++)
            {
                $dirsTable[] = array($dirs[$i], $dirs[$i], filectime($folder . $dirs[$i]));
            }
            return $dirsTable;
        }

        function CleanFolderContent($folder, $fileExt = "", $nothisExt = "")
        {
            $folderFiles = $this->GetFolderContent($folder, $fileExt, $nothisExt);
            $bAllDeleted = true;

            for ($i = 0; $i < count($folderFiles); $i ++)
            {
                if (unlink($folder . $folderFiles[$i]) == false)
                    $bAllDeleted = false;
            }

            return $bAllDeleted;
        }
        
        function DeleteFilesByPattern($fileNamePattern)
        {
            foreach(glob($fileNamePattern) as $fileName)
            {
                unlink($fileName);
            }
        }

        function DeleteFolder($folder)
        {
            if (is_dir($folder))
            $folder_handle = opendir($folder);
            if (!$folder_handle)
                return false;
            while($file = readdir($folder_handle))
            {
                if ($file != '.' && $file != '..')
                {
                    if (!is_dir($folder . '/' . $file))
                        unlink($folder . '/' . $file);
                    else
                        $this->DeleteFolder($folder . '/' . $file);
                }
            }
            closedir($folder_handle);
            rmdir($folder);
            return true;
       }
       
       function CopyFolder($src, $dst)
       {
           $dir = opendir($src);
           @mkdir($dst);
           while(false !== ( $file = readdir($dir)) )
           {
               if (( $file != '.' ) && ( $file != '..' ))
               {
                   if ( is_dir($src . '/' . $file) )
                   {
                       CopyFolder($src . '/' . $file,$dst . '/' . $file);
                   }
                   else
                   {
                       copy($src . '/' . $file,$dst . '/' . $file);
                   }
               }
           }
           closedir($dir);
       }

       function CopyFile ($src, $dst)
       {
           $contentx =@file_get_contents($src);
           $openedfile = fopen($dst, "w");
           fwrite($openedfile, $contentx);
           fclose($openedfile);
           if ($contentx === FALSE)
           {
               $status=false;
           }
           else
               $status=true;

           return $status;
       }


       function CreateFolder($folder)
       {
           if (file_exists($folder) == true)
               return false;

           return mkdir($folder);
       }

       function DeleteFile($fileName)
       {
           return unlink($fileName);
       }

       function RenameFile($fileName, $newName)
       {
           return rename($fileName, $newName);
       }

       function GetFileExt($fileName)
       {
           for ($i = strlen($fileName) - 1; $i >= 0; $i --)
           {
               if ($fileName[$i] == '.')
                   return substr($fileName, $i);
           }

           return $fileName;  // No extension found :-(
       }

    }  // End class
}  // End define