SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/Archive.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use Kohana_Exception;
  19.  
  20. use Sprout\Helpers\Drivers\ArchiveDriver;
  21.  
  22.  
  23. /**
  24.  * Class for creating archives in various formats - zip, tar.bz2, etc
  25.  */
  26. class Archive
  27. {
  28.  
  29. // Files and directories
  30. protected $paths;
  31.  
  32. // Driver instance
  33. protected $driver;
  34.  
  35. /**
  36.   * Loads the archive driver.
  37.   *
  38.   * @throws Kohana_Exception
  39.   * @param string type of archive to create
  40.   * @return void
  41.   */
  42. public function __construct($type = NULL)
  43. {
  44. $type = empty($type) ? 'zip' : $type;
  45.  
  46. // Set driver name
  47. $driver = 'Sprout\\Helpers\\Drivers\\Archive\\' . ucfirst($type);
  48.  
  49. // Load the driver
  50. if (!class_exists($driver)) throw new \Exception('Unknown archive type: ' . $type);
  51.  
  52. // Initialize the driver
  53. $this->driver = new $driver();
  54.  
  55. // Validate the driver
  56. if ( ! ($this->driver instanceof ArchiveDriver))
  57. throw new Kohana_Exception('core.driver_implements', $type, get_class($this), 'ArchiveDriver');
  58. }
  59.  
  60. /**
  61.   * Adds files or directories, recursively, to an archive.
  62.   *
  63.   * @param string file or directory to add
  64.   * @param string name to use for the given file or directory
  65.   * @param bool add files recursively, used with directories
  66.   * @return object
  67.   */
  68. public function add($path, $name = NULL, $recursive = NULL)
  69. {
  70. // Normalize to forward slashes
  71. $path = str_replace('\\', '/', $path);
  72.  
  73. // Set the name
  74. empty($name) and $name = $path;
  75.  
  76. if (is_dir($path))
  77. {
  78. // Force directories to end with a slash
  79. $path = rtrim($path, '/').'/';
  80. $name = rtrim($name, '/').'/';
  81.  
  82. // Add the directory to the paths
  83. $this->paths[] = array($path, $name);
  84.  
  85. if ($recursive === TRUE)
  86. {
  87. $dir = opendir($path);
  88. while (($file = readdir($dir)) !== FALSE)
  89. {
  90. // Do not add hidden files or directories
  91. if ($file[0] === '.')
  92. continue;
  93.  
  94. // Add directory contents
  95. $this->add($path.$file, $name.$file, TRUE);
  96. }
  97. closedir($dir);
  98. }
  99. }
  100. else
  101. {
  102. $this->paths[] = array($path, $name);
  103. }
  104.  
  105. return $this;
  106. }
  107.  
  108. /**
  109.   * Creates an archive and saves it into a file.
  110.   *
  111.   * @throws Kohana_Exception
  112.   * @param string archive filename
  113.   * @return boolean
  114.   */
  115. public function save($filename)
  116. {
  117. // Get the directory name
  118. $directory = pathinfo($filename, PATHINFO_DIRNAME);
  119.  
  120. if ( ! is_writable($directory))
  121. throw new Kohana_Exception('archive.directory_unwritable', $directory);
  122.  
  123. if (is_file($filename))
  124. {
  125. // Unable to write to the file
  126. if ( ! is_writable($filename))
  127. throw new Kohana_Exception('archive.filename_conflict', $filename);
  128.  
  129. // Remove the file
  130. unlink($filename);
  131. }
  132.  
  133. return $this->driver->create($this->paths, $filename);
  134. }
  135.  
  136. /**
  137.   * Creates a raw archive file and returns it.
  138.   *
  139.   * @return string
  140.   */
  141. public function create()
  142. {
  143. return $this->driver->create($this->paths);
  144. }
  145.  
  146. } // End Archive
  147.