Back to Installer class

Method copyFiles

public bool
copyFiles
(mixed $files, mixed $overwrite = null)
Copyfiles
Parameters
  • array $files Array with filenames
  • bool $overwrite True if existing files can be replaced
Returns
  • bool True on success
Since
  • 3.1
Class: Installer
Project: Joomla

Method copyFiles - Source code

/**
 * Copyfiles
 *
 * Copy files from source directory to the target directory
 *
 * @param   array    $files      Array with filenames
 * @param   boolean  $overwrite  True if existing files can be replaced
 *
 * @return  boolean  True on success
 *
 * @since   3.1
 */
public function copyFiles($files, $overwrite = null)
{
    /*
     * To allow for manual override on the overwriting flag, we check to see if
     * the $overwrite flag was set and is a boolean value.  If not, use the object
     * allowOverwrite flag.
     */
    if ($overwrite === null || !\is_bool($overwrite)) {
        $overwrite = $this->overwrite;
    }
    /*
     * $files must be an array of filenames.  Verify that it is an array with
     * at least one file to copy.
     */
    if (\is_array($files) && \count($files) > 0) {
        foreach ($files as $file) {
            // Get the source and destination paths
            $filesource = Path::clean($file['src']);
            $filedest = Path::clean($file['dest']);
            $filetype = \array_key_exists('type', $file) ? $file['type'] : 'file';
            if (!file_exists($filesource)) {
                /*
                 * The source file does not exist.  Nothing to copy so set an error
                 * and return false.
                 */
                Log::add(Text::sprintf('JLIB_INSTALLER_ERROR_NO_FILE', $filesource), Log::WARNING, 'jerror');
                return false;
            } elseif (($exists = file_exists($filedest)) && !$overwrite) {
                // It's okay if the manifest already exists
                if ($this->getPath('manifest') === $filesource) {
                    continue;
                }
                // The destination file already exists and the overwrite flag is false.
                // Set an error and return false.
                Log::add(Text::sprintf('JLIB_INSTALLER_ERROR_FILE_EXISTS', $filedest), Log::WARNING, 'jerror');
                return false;
            } else {
                // Copy the folder or file to the new location.
                if ($filetype === 'folder') {
                    if (!Folder::copy($filesource, $filedest, null, $overwrite)) {
                        Log::add(Text::sprintf('JLIB_INSTALLER_ERROR_FAIL_COPY_FOLDER', $filesource, $filedest), Log::WARNING, 'jerror');
                        return false;
                    }
                    $step = array('type' => 'folder', 'path' => $filedest);
                } else {
                    if (!File::copy($filesource, $filedest, null)) {
                        Log::add(Text::sprintf('JLIB_INSTALLER_ERROR_FAIL_COPY_FILE', $filesource, $filedest), Log::WARNING, 'jerror');
                        // In 3.2, TinyMCE language handling changed.  Display a special notice in case an older language pack is installed.
                        if (strpos($filedest, 'media/editors/tinymce/jscripts/tiny_mce/langs')) {
                            Log::add(Text::_('JLIB_INSTALLER_NOT_ERROR'), Log::WARNING, 'jerror');
                        }
                        return false;
                    }
                    $step = array('type' => 'file', 'path' => $filedest);
                }
                /*
                 * Since we copied a file/folder, we want to add it to the installation step stack so that
                 * in case we have to roll back the installation we can remove the files copied.
                 */
                if (!$exists) {
                    $this->stepStack[] = $step;
                }
            }
        }
    } else {
        // The $files variable was either not an array or an empty array
        return false;
    }
    return \count($files);
}