Beispiel #1
0
 /**
  * Copy over source file to working dir
  * @param $sourceFile
  * @param $processingBookType
  * @param $sourceFileCount
  * @throws Exception
  */
 protected function copySourceToWorkingDir($sourceFile)
 {
     static $importPageNumber;
     // Check if this is an image
     if ($this->isValidSourceFile($sourceFile)) {
         $importPageNumber = (int) $importPageNumber + 1;
         // Get source file
         $source = Folder::addPathElement(Configure::read('Chaucer.sourceDir'), $sourceFile['filename']);
         // Set target file
         // In case of images increment filename with number padding
         $assetNumPad = str_pad($importPageNumber, 4, '0', STR_PAD_LEFT);
         // Assemble asset name
         $assetFilename = 'image' . $assetNumPad . '.';
         $assetFilename .= MediaManager::isConvertableImage($source) ? 'png' : $sourceFile['ext'];
         // Set target
         $target = Folder::addPathElement($this->workingDir, $assetFilename);
         // Log the message
         CakeLog::debug('[ImageProcessor::getBookSourceFiles] ' . 'Preparing to copy remote file on instance ' . Configure::read('Chaucer.instanceName') . ': ' . $source . ' to ' . $target);
         // Get the source file from remote
         $this->FileManager->copy($source, $target);
         if (file_exists($target)) {
             $this->sourceFiles[] = $target;
         } else {
             throw new Exception('[ImageProcessor::getBookSourceFiles] ' . 'Unable to copy book source file.');
         }
         return true;
     }
     return false;
 }
 /**
  * testBasic method
  *
  * @access public
  * @return void
  */
 function testBasic()
 {
     $path = dirname(__FILE__);
     $Folder =& new Folder($path);
     $result = $Folder->pwd();
     $this->assertEqual($result, $path);
     $result = Folder::addPathElement($path, 'test');
     $expected = $path . DS . 'test';
     $this->assertEqual($result, $expected);
     $result = $Folder->cd(ROOT);
     $expected = ROOT;
     $this->assertEqual($result, $expected);
     $result = $Folder->cd(ROOT . DS . 'non-existent');
     $this->assertFalse($result);
 }
Beispiel #3
0
 /**
  * Initialize the working directory and ensure it exists
  * @return  string  Path to working directory
  */
 public function initWorkingDir()
 {
     // Check if working directory is already initialized
     if (is_dir($this->workingDir)) {
         return $this->workingDir;
     }
     // Get temp target directory
     $tmpDir = Configure::read('Chaucer.instanceName') . '/' . $this->bookId;
     $targetDir = Folder::addPathElement($this->FileManager->getTmpPath(), $tmpDir);
     // Create and return dir
     if (!$this->FileManager->createDir($targetDir)) {
         throw new Exception('Unable to create working directory: ' . $targetDir);
     }
     CakeLog::debug('[CommonProcessor::initWorkingDir] working dir: ' . $targetDir);
     return $targetDir;
 }
Beispiel #4
0
 /**
  * Get the real path (taking ".." and such into account)
  *
  * @param string $path Path to resolve
  * @return string The resolved path
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::realpath
  */
 public function realpath($path)
 {
     $path = str_replace('/', DS, trim($path));
     if (strpos($path, '..') === false) {
         if (!Folder::isAbsolute($path)) {
             $path = Folder::addPathElement($this->path, $path);
         }
         return $path;
     }
     $parts = explode(DS, $path);
     $newparts = array();
     $newpath = '';
     if ($path[0] === DS) {
         $newpath = DS;
     }
     while (($part = array_shift($parts)) !== NULL) {
         if ($part === '.' || $part === '') {
             continue;
         }
         if ($part === '..') {
             if (!empty($newparts)) {
                 array_pop($newparts);
                 continue;
             } else {
                 return false;
             }
         }
         $newparts[] = $part;
     }
     $newpath .= implode(DS, $newparts);
     return Folder::slashTerm($newpath);
 }
Beispiel #5
0
 /**
  * test Adding path elements to a path
  *
  * @return void
  */
 public function testAddPathElement()
 {
     $expected = DS . 'some' . DS . 'dir' . DS . 'another_path';
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
     $this->assertEquals($expected, $result);
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
     $this->assertEquals($expected, $result);
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir', array('another_path'));
     $this->assertEquals($expected, $result);
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, array('another_path'));
     $this->assertEquals($expected, $result);
     $expected = DS . 'some' . DS . 'dir' . DS . 'another_path' . DS . 'and' . DS . 'another';
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir', array('another_path', 'and', 'another'));
     $this->assertEquals($expected, $result);
 }
 /**
  * test Adding path elements to a path
  *
  * @return void
  */
 public function testAddPathElement()
 {
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
     $this->assertEquals(DS . 'some' . DS . 'dir' . DS . 'another_path', $result);
     $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
     $this->assertEquals(DS . 'some' . DS . 'dir' . DS . 'another_path', $result);
 }
Beispiel #7
0
 /**
  * Recursive directory copy.
  *
  * @param string $fromDir
  * @param string $toDir
  * @param octal $chmod
  * @param boolean	 $verbose
  * @return Success.
  */
 function __copydirr($fromDir, $toDir, $chmod = 0755, $verbose = false)
 {
     $errors = array();
     $messages = array();
     uses('folder');
     $folder = new Folder($toDir, true, 0755);
     if (!is_writable($toDir)) {
         $errors[] = 'target ' . $toDir . ' is not writable';
     }
     if (!is_dir($fromDir)) {
         $errors[] = 'source ' . $fromDir . ' is not a directory';
     }
     if (!empty($errors)) {
         if ($verbose) {
             foreach ($errors as $err) {
                 $this->stdout('Error: ' . $err);
             }
         }
         return false;
     }
     $exceptions = array('.', '..', '.svn');
     $handle = opendir($fromDir);
     while (false !== ($item = readdir($handle))) {
         if (!in_array($item, $exceptions)) {
             $from = $folder->addPathElement($fromDir, $item);
             $to = $folder->addPathElement($toDir, $item);
             if (is_file($from)) {
                 if (@copy($from, $to)) {
                     chmod($to, $chmod);
                     touch($to, filemtime($from));
                     $messages[] = 'File copied from ' . $from . ' to ' . $to;
                 } else {
                     $errors[] = 'cannot copy file from ' . $from . ' to ' . $to;
                 }
             }
             if (is_dir($from)) {
                 if (@mkdir($to)) {
                     chmod($to, $chmod);
                     $messages[] = 'Directory created: ' . $to;
                 } else {
                     $errors[] = 'cannot create directory ' . $to;
                 }
                 $this->__copydirr($from, $to, $chmod, $verbose);
             }
         }
     }
     closedir($handle);
     if ($verbose) {
         foreach ($errors as $err) {
             $this->stdout('Error: ' . $err);
         }
         foreach ($messages as $msg) {
             $this->stdout($msg);
         }
     }
     return true;
 }
Beispiel #8
0
 /**
  *	Copy
  *	@param array $options
  *	@return BOOL
  */
 public function copy($options = [])
 {
     if (!$this->path) {
         return false;
     }
     $to = null;
     if (is_string($options)) {
         $to = $options;
         $options = array();
     }
     $options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->folderChmod, 'skip' => array()), $options);
     $fromDir = $options['from'];
     $toDir = $options['to'];
     $mode = $options['mode'];
     if (!$this->cd($fromDir)) {
         $this->_errors[] = sprintf('%s not found', $fromDir);
         return false;
     }
     if (!is_dir($toDir)) {
         $this->makeDir($toDir);
     }
     if (!is_writable($toDir)) {
         $this->_errors[] = sprintf('%s not writable', $toDir);
         return false;
     }
     $exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
     if ($handle = @opendir($fromDir)) {
         while (false !== ($item = readdir($handle))) {
             if (!in_array($item, $exceptions)) {
                 $from = Folder::addPathElement($fromDir, $item);
                 $to = Folder::addPathElement($toDir, $item);
                 if (is_file($from)) {
                     if (copy($from, $to)) {
                         chmod($to, intval($mode, 8));
                         touch($to, filemtime($from));
                         $this->_messages[] = sprintf('%s copied to %s', $from, $to);
                     } else {
                         $this->_errors[] = sprintf('%s NOT copied to %s', $from, $to);
                     }
                 }
                 if (is_dir($from) && !file_exists($to)) {
                     $old = umask(0);
                     if (mkdir($to, $mode)) {
                         umask($old);
                         $old = umask(0);
                         chmod($to, $mode);
                         umask($old);
                         $this->_messages[] = sprintf('%s created', $to);
                         $options = array_merge($options, array('to' => $to, 'from' => $from));
                         $this->copy($options);
                     } else {
                         $this->_errors[] = sprintf('%s not created', $to);
                     }
                 }
             }
         }
         closedir($handle);
     } else {
         return false;
     }
     if (!empty($this->_errors)) {
         return false;
     }
     return true;
 }
 /**
  * Recursive directory copy.
  *
  * ### Options
  *
  * - `to` The directory to copy to.
  * - `from` The directory to copy from, this will cause a cd() to occur, changing the results of pwd().
  * - `mode` The mode to copy the files/directories with.
  * - `skip` Files/directories to skip.
  * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
  *
  * @param array|string $options Either an array of options (see above) or a string of the destination directory.
  *
  * @return boolean Success
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::copy
  */
 public function copy($options)
 {
     if (!$this->pwd()) {
         return false;
     }
     $to = null;
     if (is_string($options)) {
         $to = $options;
         $options = array();
     }
     $options = array_merge(array('to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => array(), 'scheme' => Folder::MERGE), $options);
     $fromDir = $options['from'];
     $toDir = $options['to'];
     $mode = $options['mode'];
     if (!$this->cd($fromDir)) {
         $this->_errors[] = __d('cake_dev', '%s not found', $fromDir);
         return false;
     }
     if (!is_dir($toDir)) {
         $this->create($toDir, $mode);
     }
     if (!is_writable($toDir)) {
         $this->_errors[] = __d('cake_dev', '%s not writable', $toDir);
         return false;
     }
     $exceptions = array_merge(array('.', '..', '.svn'), $options['skip']);
     //@codingStandardsIgnoreStart
     if ($handle = @opendir($fromDir)) {
         //@codingStandardsIgnoreEnd
         while (($item = readdir($handle)) !== false) {
             $to = Folder::addPathElement($toDir, $item);
             if (($options['scheme'] != Folder::SKIP || !is_dir($to)) && !in_array($item, $exceptions)) {
                 $from = Folder::addPathElement($fromDir, $item);
                 if (is_file($from)) {
                     if (copy($from, $to)) {
                         chmod($to, intval($mode, 8));
                         touch($to, filemtime($from));
                         $this->_messages[] = __d('cake_dev', '%s copied to %s', $from, $to);
                     } else {
                         $this->_errors[] = __d('cake_dev', '%s NOT copied to %s', $from, $to);
                     }
                 }
                 if (is_dir($from) && file_exists($to) && $options['scheme'] == Folder::OVERWRITE) {
                     $this->delete($to);
                 }
                 if (is_dir($from) && !file_exists($to)) {
                     $old = umask(0);
                     if (mkdir($to, $mode)) {
                         umask($old);
                         $old = umask(0);
                         chmod($to, $mode);
                         umask($old);
                         $this->_messages[] = __d('cake_dev', '%s created', $to);
                         $options = array_merge($options, array('to' => $to, 'from' => $from));
                         $this->copy($options);
                     } else {
                         $this->_errors[] = __d('cake_dev', '%s not created', $to);
                     }
                 } elseif (is_dir($from) && $options['scheme'] == Folder::MERGE) {
                     $options = array_merge($options, array('to' => $to, 'from' => $from));
                     $this->copy($options);
                 }
             }
         }
         closedir($handle);
     } else {
         return false;
     }
     if (!empty($this->_errors)) {
         return false;
     }
     return true;
 }
 protected function getFullFilePath()
 {
     $path = $this->FileManager->getRemoteRootDir();
     $path = Folder::addPathElement($path, 'Source');
     return $path;
 }
Beispiel #11
0
 /**
  * Adds a page to the beginning of the book to display the cover page. uses the book_cover field from the books
  * table to determine the image file
  *
  * @return string path to cover image. Relative in S3, absolute locally
  */
 public function setCoverImage()
 {
     $book = $this->bookData->getBookItem();
     $coverSource = trim($book['ChChaucerBook']['book_cover']);
     $path = '';
     if (strlen($coverSource) > 0) {
         CakeLog::debug('[ImportProcessor::addCoverPageImage] Book ' . $book['ChChaucerBook']['book_id'] . ' has cover image: ' . $coverSource);
         $path = $this->epub->addCoverImage(Folder::addPathElement(Configure::read('Chaucer.sourceDir'), $coverSource));
         $book['ChChaucerBook']['book_cover'] = $coverSource;
         $this->bookData->saveBook($book);
         return $this->epub->epubRoot() . $path;
     }
     return '';
 }
Beispiel #12
0
 /**
  * Create image assets.
  */
 private function createImageAssets()
 {
     // Get image directory
     $imageDir = Folder::addPathElement($this->workingDir, 'images');
     if (is_dir($imageDir)) {
         // Get all image files
         $imageFiles = $this->FileManager->getAllFiles($imageDir);
         // Check if there are any images
         if (count($imageFiles) > 0) {
             // Adjust progress
             $this->progress->adjustMaxSteps(count($imageFiles), TRUE);
             // Loop through image files
             foreach ($imageFiles as $imagePath) {
                 // Get file image name
                 $imagePathInfo = pathinfo($imagePath);
                 $imageFilename = $imagePathInfo['basename'];
                 // Check if image exists
                 if (file_exists($imagePath)) {
                     // Path to unlink
                     $imagePathUnlink = $imagePath;
                     // Import image files
                     $this->addImageAssetToBook($imagePath, 'images/');
                     // Remove image files
                     unlink($imagePathUnlink);
                 } else {
                     CakeLog::warning("[WordProcessor::importImages] File {$imageFilename} not found");
                 }
                 // Update step
                 $this->progress->incrementStep();
             }
         }
     }
 }
Beispiel #13
0
 /**
  * Get Book CSS by reading css/template.css and stripping the font-face rules
  * @return  string  CSS content
  */
 protected function getBookCSS()
 {
     // Get template CSS
     $file = Folder::addPathElement($this->workingDir, "Styles/template.css");
     if (file_exists($file)) {
         // Strip @font-face
         $css = file_get_contents($file);
         while (strpos($css, '@font-face') !== false) {
             $start = strpos($css, '@font-face');
             $end = strpos($css, "}", $start);
             $pre = substr($css, 0, $start - 1);
             $post = substr($css, $end + 1);
             $css = $pre . $post;
         }
         // Return CSS
         return $css;
     }
     // Return empty string if there is no file
     return "";
 }