home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / includes / class-wp-filesystem-direct.php < prev    next >
Encoding:
PHP Script  |  2017-07-26  |  11.0 KB  |  490 lines

  1. <?php
  2. /**
  3.  * WordPress Direct Filesystem.
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Filesystem
  7.  */
  8.  
  9. /**
  10.  * WordPress Filesystem Class for direct PHP file and folder manipulation.
  11.  *
  12.  * @since 2.5.0
  13.  *
  14.  * @see WP_Filesystem_Base
  15.  */
  16. class WP_Filesystem_Direct extends WP_Filesystem_Base {
  17.  
  18.     /**
  19.      * constructor
  20.      *
  21.      *
  22.      * @param mixed $arg ignored argument
  23.      */
  24.     public function __construct($arg) {
  25.         $this->method = 'direct';
  26.         $this->errors = new WP_Error();
  27.     }
  28.  
  29.     /**
  30.      * Reads entire file into a string
  31.      *
  32.      *
  33.      * @param string $file Name of the file to read.
  34.      * @return string|bool The function returns the read data or false on failure.
  35.      */
  36.     public function get_contents($file) {
  37.         return @file_get_contents($file);
  38.     }
  39.  
  40.     /**
  41.      * Reads entire file into an array
  42.      *
  43.      *
  44.      * @param string $file Path to the file.
  45.      * @return array|bool the file contents in an array or false on failure.
  46.      */
  47.     public function get_contents_array($file) {
  48.         return @file($file);
  49.     }
  50.  
  51.     /**
  52.      * Write a string to a file
  53.      *
  54.      *
  55.      * @param string $file     Remote path to the file where to write the data.
  56.      * @param string $contents The data to write.
  57.      * @param int    $mode     Optional. The file permissions as octal number, usually 0644.
  58.      *                         Default false.
  59.      * @return bool False upon failure, true otherwise.
  60.      */
  61.     public function put_contents( $file, $contents, $mode = false ) {
  62.         $fp = @fopen( $file, 'wb' );
  63.         if ( ! $fp )
  64.             return false;
  65.  
  66.         mbstring_binary_safe_encoding();
  67.  
  68.         $data_length = strlen( $contents );
  69.  
  70.         $bytes_written = fwrite( $fp, $contents );
  71.  
  72.         reset_mbstring_encoding();
  73.  
  74.         fclose( $fp );
  75.  
  76.         if ( $data_length !== $bytes_written )
  77.             return false;
  78.  
  79.         $this->chmod( $file, $mode );
  80.  
  81.         return true;
  82.     }
  83.  
  84.     /**
  85.      * Gets the current working directory
  86.      *
  87.      *
  88.      * @return string|bool the current working directory on success, or false on failure.
  89.      */
  90.     public function cwd() {
  91.         return @getcwd();
  92.     }
  93.  
  94.     /**
  95.      * Change directory
  96.      *
  97.      *
  98.      * @param string $dir The new current directory.
  99.      * @return bool Returns true on success or false on failure.
  100.      */
  101.     public function chdir($dir) {
  102.         return @chdir($dir);
  103.     }
  104.  
  105.     /**
  106.      * Changes file group
  107.      *
  108.      *
  109.      * @param string $file      Path to the file.
  110.      * @param mixed  $group     A group name or number.
  111.      * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
  112.      * @return bool Returns true on success or false on failure.
  113.      */
  114.     public function chgrp($file, $group, $recursive = false) {
  115.         if ( ! $this->exists($file) )
  116.             return false;
  117.         if ( ! $recursive )
  118.             return @chgrp($file, $group);
  119.         if ( ! $this->is_dir($file) )
  120.             return @chgrp($file, $group);
  121.         // Is a directory, and we want recursive
  122.         $file = trailingslashit($file);
  123.         $filelist = $this->dirlist($file);
  124.         foreach ($filelist as $filename)
  125.             $this->chgrp($file . $filename, $group, $recursive);
  126.  
  127.         return true;
  128.     }
  129.  
  130.     /**
  131.      * Changes filesystem permissions
  132.      *
  133.      *
  134.      * @param string $file      Path to the file.
  135.      * @param int    $mode      Optional. The permissions as octal number, usually 0644 for files,
  136.      *                          0755 for dirs. Default false.
  137.      * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
  138.      * @return bool Returns true on success or false on failure.
  139.      */
  140.     public function chmod($file, $mode = false, $recursive = false) {
  141.         if ( ! $mode ) {
  142.             if ( $this->is_file($file) )
  143.                 $mode = FS_CHMOD_FILE;
  144.             elseif ( $this->is_dir($file) )
  145.                 $mode = FS_CHMOD_DIR;
  146.             else
  147.                 return false;
  148.         }
  149.  
  150.         if ( ! $recursive || ! $this->is_dir($file) )
  151.             return @chmod($file, $mode);
  152.         // Is a directory, and we want recursive
  153.         $file = trailingslashit($file);
  154.         $filelist = $this->dirlist($file);
  155.         foreach ( (array)$filelist as $filename => $filemeta)
  156.             $this->chmod($file . $filename, $mode, $recursive);
  157.  
  158.         return true;
  159.     }
  160.  
  161.     /**
  162.      * Changes file owner
  163.      *
  164.      *
  165.      * @param string $file      Path to the file.
  166.      * @param mixed  $owner     A user name or number.
  167.      * @param bool   $recursive Optional. If set True changes file owner recursively.
  168.      *                          Default false.
  169.      * @return bool Returns true on success or false on failure.
  170.      */
  171.     public function chown($file, $owner, $recursive = false) {
  172.         if ( ! $this->exists($file) )
  173.             return false;
  174.         if ( ! $recursive )
  175.             return @chown($file, $owner);
  176.         if ( ! $this->is_dir($file) )
  177.             return @chown($file, $owner);
  178.         // Is a directory, and we want recursive
  179.         $filelist = $this->dirlist($file);
  180.         foreach ($filelist as $filename) {
  181.             $this->chown($file . '/' . $filename, $owner, $recursive);
  182.         }
  183.         return true;
  184.     }
  185.  
  186.     /**
  187.      * Gets file owner
  188.      *
  189.      *
  190.      * @param string $file Path to the file.
  191.      * @return string|bool Username of the user or false on error.
  192.      */
  193.     public function owner($file) {
  194.         $owneruid = @fileowner($file);
  195.         if ( ! $owneruid )
  196.             return false;
  197.         if ( ! function_exists('posix_getpwuid') )
  198.             return $owneruid;
  199.         $ownerarray = posix_getpwuid($owneruid);
  200.         return $ownerarray['name'];
  201.     }
  202.  
  203.     /**
  204.      * Gets file permissions
  205.      *
  206.      * FIXME does not handle errors in fileperms()
  207.      *
  208.      *
  209.      * @param string $file Path to the file.
  210.      * @return string Mode of the file (last 3 digits).
  211.      */
  212.     public function getchmod($file) {
  213.         return substr( decoct( @fileperms( $file ) ), -3 );
  214.     }
  215.  
  216.     /**
  217.      *
  218.      * @param string $file
  219.      * @return string|false
  220.      */
  221.     public function group($file) {
  222.         $gid = @filegroup($file);
  223.         if ( ! $gid )
  224.             return false;
  225.         if ( ! function_exists('posix_getgrgid') )
  226.             return $gid;
  227.         $grouparray = posix_getgrgid($gid);
  228.         return $grouparray['name'];
  229.     }
  230.  
  231.     /**
  232.      *
  233.      * @param string $source
  234.      * @param string $destination
  235.      * @param bool   $overwrite
  236.      * @param int    $mode
  237.      * @return bool
  238.      */
  239.     public function copy($source, $destination, $overwrite = false, $mode = false) {
  240.         if ( ! $overwrite && $this->exists($destination) )
  241.             return false;
  242.  
  243.         $rtval = copy($source, $destination);
  244.         if ( $mode )
  245.             $this->chmod($destination, $mode);
  246.         return $rtval;
  247.     }
  248.  
  249.     /**
  250.      *
  251.      * @param string $source
  252.      * @param string $destination
  253.      * @param bool $overwrite
  254.      * @return bool
  255.      */
  256.     public function move($source, $destination, $overwrite = false) {
  257.         if ( ! $overwrite && $this->exists($destination) )
  258.             return false;
  259.  
  260.         // Try using rename first. if that fails (for example, source is read only) try copy.
  261.         if ( @rename($source, $destination) )
  262.             return true;
  263.  
  264.         if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
  265.             $this->delete($source);
  266.             return true;
  267.         } else {
  268.             return false;
  269.         }
  270.     }
  271.  
  272.     /**
  273.      *
  274.      * @param string $file
  275.      * @param bool $recursive
  276.      * @param string $type
  277.      * @return bool
  278.      */
  279.     public function delete($file, $recursive = false, $type = false) {
  280.         if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
  281.             return false;
  282.         $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
  283.  
  284.         if ( 'f' == $type || $this->is_file($file) )
  285.             return @unlink($file);
  286.         if ( ! $recursive && $this->is_dir($file) )
  287.             return @rmdir($file);
  288.  
  289.         // At this point it's a folder, and we're in recursive mode
  290.         $file = trailingslashit($file);
  291.         $filelist = $this->dirlist($file, true);
  292.  
  293.         $retval = true;
  294.         if ( is_array( $filelist ) ) {
  295.             foreach ( $filelist as $filename => $fileinfo ) {
  296.                 if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
  297.                     $retval = false;
  298.             }
  299.         }
  300.  
  301.         if ( file_exists($file) && ! @rmdir($file) )
  302.             $retval = false;
  303.  
  304.         return $retval;
  305.     }
  306.     /**
  307.      *
  308.      * @param string $file
  309.      * @return bool
  310.      */
  311.     public function exists($file) {
  312.         return @file_exists($file);
  313.     }
  314.     /**
  315.      *
  316.      * @param string $file
  317.      * @return bool
  318.      */
  319.     public function is_file($file) {
  320.         return @is_file($file);
  321.     }
  322.     /**
  323.      *
  324.      * @param string $path
  325.      * @return bool
  326.      */
  327.     public function is_dir($path) {
  328.         return @is_dir($path);
  329.     }
  330.  
  331.     /**
  332.      *
  333.      * @param string $file
  334.      * @return bool
  335.      */
  336.     public function is_readable($file) {
  337.         return @is_readable($file);
  338.     }
  339.  
  340.     /**
  341.      *
  342.      * @param string $file
  343.      * @return bool
  344.      */
  345.     public function is_writable($file) {
  346.         return @is_writable($file);
  347.     }
  348.  
  349.     /**
  350.      *
  351.      * @param string $file
  352.      * @return int
  353.      */
  354.     public function atime($file) {
  355.         return @fileatime($file);
  356.     }
  357.  
  358.     /**
  359.      *
  360.      * @param string $file
  361.      * @return int
  362.      */
  363.     public function mtime($file) {
  364.         return @filemtime($file);
  365.     }
  366.  
  367.     /**
  368.      *
  369.      * @param string $file
  370.      * @return int
  371.      */
  372.     public function size($file) {
  373.         return @filesize($file);
  374.     }
  375.  
  376.     /**
  377.      *
  378.      * @param string $file
  379.      * @param int $time
  380.      * @param int $atime
  381.      * @return bool
  382.      */
  383.     public function touch($file, $time = 0, $atime = 0) {
  384.         if ($time == 0)
  385.             $time = time();
  386.         if ($atime == 0)
  387.             $atime = time();
  388.         return @touch($file, $time, $atime);
  389.     }
  390.  
  391.     /**
  392.      *
  393.      * @param string $path
  394.      * @param mixed  $chmod
  395.      * @param mixed  $chown
  396.      * @param mixed  $chgrp
  397.      * @return bool
  398.      */
  399.     public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
  400.         // Safe mode fails with a trailing slash under certain PHP versions.
  401.         $path = untrailingslashit($path);
  402.         if ( empty($path) )
  403.             return false;
  404.  
  405.         if ( ! $chmod )
  406.             $chmod = FS_CHMOD_DIR;
  407.  
  408.         if ( ! @mkdir($path) )
  409.             return false;
  410.         $this->chmod($path, $chmod);
  411.         if ( $chown )
  412.             $this->chown($path, $chown);
  413.         if ( $chgrp )
  414.             $this->chgrp($path, $chgrp);
  415.         return true;
  416.     }
  417.  
  418.     /**
  419.      *
  420.      * @param string $path
  421.      * @param bool $recursive
  422.      * @return bool
  423.      */
  424.     public function rmdir($path, $recursive = false) {
  425.         return $this->delete($path, $recursive);
  426.     }
  427.  
  428.     /**
  429.      *
  430.      * @param string $path
  431.      * @param bool $include_hidden
  432.      * @param bool $recursive
  433.      * @return bool|array
  434.      */
  435.     public function dirlist($path, $include_hidden = true, $recursive = false) {
  436.         if ( $this->is_file($path) ) {
  437.             $limit_file = basename($path);
  438.             $path = dirname($path);
  439.         } else {
  440.             $limit_file = false;
  441.         }
  442.  
  443.         if ( ! $this->is_dir($path) )
  444.             return false;
  445.  
  446.         $dir = @dir($path);
  447.         if ( ! $dir )
  448.             return false;
  449.  
  450.         $ret = array();
  451.  
  452.         while (false !== ($entry = $dir->read()) ) {
  453.             $struc = array();
  454.             $struc['name'] = $entry;
  455.  
  456.             if ( '.' == $struc['name'] || '..' == $struc['name'] )
  457.                 continue;
  458.  
  459.             if ( ! $include_hidden && '.' == $struc['name'][0] )
  460.                 continue;
  461.  
  462.             if ( $limit_file && $struc['name'] != $limit_file)
  463.                 continue;
  464.  
  465.             $struc['perms']     = $this->gethchmod($path.'/'.$entry);
  466.             $struc['permsn']    = $this->getnumchmodfromh($struc['perms']);
  467.             $struc['number']     = false;
  468.             $struc['owner']        = $this->owner($path.'/'.$entry);
  469.             $struc['group']        = $this->group($path.'/'.$entry);
  470.             $struc['size']        = $this->size($path.'/'.$entry);
  471.             $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
  472.             $struc['lastmod']   = date('M j',$struc['lastmodunix']);
  473.             $struc['time']        = date('h:i:s',$struc['lastmodunix']);
  474.             $struc['type']        = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
  475.  
  476.             if ( 'd' == $struc['type'] ) {
  477.                 if ( $recursive )
  478.                     $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
  479.                 else
  480.                     $struc['files'] = array();
  481.             }
  482.  
  483.             $ret[ $struc['name'] ] = $struc;
  484.         }
  485.         $dir->close();
  486.         unset($dir);
  487.         return $ret;
  488.     }
  489. }
  490.