/home/coolpkct/www/websites/cake3.cool.rocks/admin/classes/filer.php
<?php
/**
 * Part of Showkase web site management package
 *
 * @package Showkase
 * @author Jack Hardie {@link http://www.jhardie.com}
 * @copyright Copyright (c) 2012, SimpleViewer Inc.
 */
defined('SK_ACCESS')||die('<h1>403: Forbidden</h1>');

/**
 * Wrapper for php file functions
 *
 * @package Showkase
 */
Class Filer
{
    /**
     * File put contents
     *
     * @param string file name
     * @param mixed data
     * @param integer flags see php.net/file_put_contents
     * @param resource context
     * 
     * @return object gallery object
     */
    public static function skPutContents($filename , $data , $flags=LOCK_EX, $context=NULL)
    {
        if (DEMO_MODE) return false;
        return @file_put_contents($filename , $data , $flags);
    }
    
    /**
     * File copy
     *
     * @param string source
     * @param string destination
     * @param resource context
     */
    public static function skCopy($source, $dest, $context=NULL)
    {
        if (DEMO_MODE) return false;
        return @copy($source, $dest);
    }

    /**
     * File delete
     *
     * @param string path
     * @param resource context
     */
    public static function skUnlink($filename, $context=NULL)
    {
        if (DEMO_MODE) return false;
        return @unlink($filename);
    }
    
    /**
     * File chmod
     *
     * @param string path
     * @param integer mode
     */
    public static function skChmod($filename, $mode)
    {
        if (DEMO_MODE) return false;
        return @chmod($filename, $mode);
    }

    /**
     * File rename
     *
     * @param string old name
     * @param string new name
     * @param resource context
     */
    public static function skRename($oldname, $newname, $context=NULL)
    {
        if (DEMO_MODE) return false;
        return @rename($oldname, $newname);
    }
     
    /**
     * Make directory
     * $mode = NULL gives mkdir default 0777 & umask
     * $mode = 0777 attempts to chmod to 0777
     * @param string path
     * @param integer octal mode
     * @param boolean recursive
     */
    public static function skMkdir($path, $mode=NULL, $recursive=false, $context=NULL)
    {
        if (DEMO_MODE) return false;
        if (
            !@mkdir($path, 0777, $recursive)
        ) {
            return false;
        }
        if (!is_null($mode)) {
            @chmod($path, $mode);
        }
        return true;
    }
        
    /**
     * Copy files from one directory to another one recursively. Function returns number of files copied.
     * Always creates destination directory even if source directory does not exist
     * Optionally attempts to set directory and file permissions
     *
     * @access public
     * @param string source directory path
     * @param string destination directory path
     * @param integer chmod for directories
     * @param integer chmod for files
     * @return integer number of files NOT copied
     */
    public static function rCopy($srcdir, $dstdir, $dirMode=NULL, $fileMode=NULL)
    {
        if (DEMO_MODE) return 0;
        $failures = 0;
        if (!is_dir($dstdir)) {
            if (!@mkdir($dstdir, 0777, true)) {
                throw new Exception('rCopy is unable to create directory '.$dstdir);
            }
            if (!is_null($dirMode)) {
                @chmod($dstdir, $dirMode);
            }
        }
        if (!is_dir($srcdir)) return $failures;
        if (!@($curdir = opendir($srcdir))) {
            throw new Exception('rCopy failed to open directory '.rtrim($srcdir, '\\/'));
        }
        while (false !== ($file = readdir($curdir))) {
            if ($file[0] == '.') continue;
            $srcfile = $srcdir . DIRECTORY_SEPARATOR . $file;
            $dstfile = $dstdir . DIRECTORY_SEPARATOR . $file;
            if (is_file($srcfile)) {
                if (!@copy($srcfile, $dstfile)) {
                  $failures++;
                } elseif (!is_null($fileMode)) {
                  @chmod($dstfile, $fileMode);
                }
            }
            elseif (is_dir($srcfile)) {
                $failures += self::rCopy($srcfile, $dstfile, $dirMode, $fileMode);
            }
        }
        closedir($curdir);
        return $failures;
    }
    
    /**
     * Recursive chmod
     *
     * @access private
     * @return boolean success
     * @param string directory name. Also works on files but there are easier ways to chmod a file
     * @param integer mode to set directories
     * @param integer mode to set files
     */
    public static function rChmod($start, $dirMode, $fileMode)
    {
        if (DEMO_MODE) return false;
        $start = rtrim($start, '\\/');
        if (!file_exists($start)) {
            return false;
        }
        if (!is_dir($start)) {
            return @chmod($start, $fileMode);
        } elseif (is_readable($start)) {
            $handle = opendir($start);
            while (false !== ($item = readdir($handle))) {
                if ($item[0] == '.') continue;
                $path = $start.DIRECTORY_SEPARATOR.$item;
                if (is_dir($path))  {
                    self::rChmod($path, $dirMode, $fileMode);
                } else {
                    @chmod($path, $fileMode);
                }
            }
            closedir($handle);  
        }
        clearstatcache();
        return @chmod($start, $dirMode);
    }
}