public function getDbTableExport($params)
 {
     if (!isset($params['table'])) {
         throw new WireException('No table specified');
     }
     $exportPath = $this->tmpDir . 'export/';
     wireMkdir($exportPath, true, '0755');
     $table = $params['table'];
     $sql = $this->local->getDatabaseTableSQL($table);
     file_put_contents($exportPath . $table . '.sql', $sql);
 }
 /**
  * Construct the FileLog
  *
  * @param string $path Path where the log will be stored (path should have trailing slash)
  * 	This may optionally include the filename if you intend to leave the second param blank.
  * @param string $identifier Basename for the log file, not including the extension. 
  * 
  */
 public function __construct($path, $identifier = '')
 {
     if ($identifier) {
         $path = rtrim($path, '/') . '/';
         $this->logFilename = "{$path}{$identifier}.{$this->fileExtension}";
     } else {
         $this->logFilename = $path;
         $path = dirname($path) . '/';
     }
     $this->path = $path;
     if (!is_dir($path)) {
         wireMkdir($path);
     }
 }
 public function getDatabaseTableSQL($table)
 {
     $exportPath = wireTempDir('ProcessDeploy');
     wireMkdir($exportPath, true, '0755');
     $backup = new WireDatabaseBackup($exportPath);
     $backup->setDatabase($this->database);
     $backup->setBackupOptions(array('allowDrop' => false));
     $file = $backup->backup(array('filename' => $table . '.sql', 'tables' => array($table)));
     if (!$file) {
         return null;
     }
     $sql = file_get_contents($file);
     @unlink($file);
     return $sql;
 }
 /**
  * Retrieve new instance of WireDatabaseBackups ready to use with this connection
  * 
  * See WireDatabaseBackup class for usage. 
  * 
  * @return WireDatabaseBackup
  * @throws WireException|Exception on fatal error
  * 
  */
 public function backups()
 {
     $path = $this->wire('config')->paths->assets . 'backups/database/';
     if (!is_dir($path)) {
         wireMkdir($path, true);
         if (!is_dir($path)) {
             throw new WireException("Unable to create path for backups: {$path}");
         }
     }
     $backups = new WireDatabaseBackup($path);
     $backups->setDatabase($this);
     $backups->setDatabaseConfig($this->wire('config'));
     $backups->setBackupOptions(array('user' => $this->wire('user')->name));
     return $backups;
 }
 /**
  * Create a directory with proper permissions, for internal use. 
  *
  * @param string $path Path to create
  * @return bool True on success, false if not
  *
  */
 protected function _createPath($path)
 {
     if (is_dir($path)) {
         return true;
     }
     return wireMkdir($path, true);
 }
Exemple #6
0
 /**
  * Saves $data to the cache
  * 
  * @param string $data
  * @return bool
  *
  */
 public function save($data)
 {
     $filename = $this->buildFilename();
     if (!is_file($filename)) {
         $dirname = dirname($filename);
         if (is_dir($dirname)) {
             $files = glob("{$dirname}/*.*");
             $numFiles = count($files);
             if ($numFiles >= self::maxCacheFiles) {
                 // if the cache file doesn't already exist, and there are too many files here
                 // then abort the cache save for security (we don't want to fill up the disk)
                 // also go through and remove any expired files while we are here, to avoid
                 // this limit interrupting more cache saves.
                 foreach ($files as $file) {
                     if (self::isCacheFile($file) && $this->isCacheFileExpired($file)) {
                         $this->removeFilename($file);
                     }
                 }
                 return false;
             }
         } else {
             wireMkdir("{$dirname}/", true);
         }
     }
     $result = file_put_contents($filename, $data);
     wireChmod($filename);
     return $result;
 }
Exemple #7
0
/**
 * Unzips the given ZIP file to the destination directory
 * 
 * @param string $file ZIP file to extract
 * @param string $dst Directory where files should be unzipped into. Directory is created if it doesn't exist.
 * @return array Returns an array of filenames (excluding $dst) that were unzipped.
 * @throws WireException All error conditions result in WireException being thrown.
 * 
 */
function wireUnzipFile($file, $dst)
{
    $dst = rtrim($dst, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
    if (!class_exists('ZipArchive')) {
        throw new WireException("PHP's ZipArchive class does not exist");
    }
    if (!is_file($file)) {
        throw new WireException("ZIP file does not exist");
    }
    if (!is_dir($dst)) {
        wireMkdir($dst, true);
    }
    $names = array();
    $chmodFile = wire('config')->chmodFile;
    $chmodDir = wire('config')->chmodDir;
    $zip = new ZipArchive();
    $res = $zip->open($file);
    if ($res !== true) {
        throw new WireException("Unable to open ZIP file, error code: {$res}");
    }
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        if ($zip->extractTo($dst, $name)) {
            $names[$i] = $name;
            $filename = $dst . ltrim($name, '/');
            if (is_dir($filename)) {
                if ($chmodDir) {
                    chmod($filename, octdec($chmodDir));
                }
            } else {
                if (is_file($filename)) {
                    if ($chmodFile) {
                        chmod($filename, octdec($chmodFile));
                    }
                }
            }
        }
    }
    $zip->close();
    return $names;
}
 /**
  * Returns a temporary directory (path) 
  *
  * @return string Returns path
  * @throws WireException If can't create temporary dir
  *
  */
 public function get()
 {
     if (!is_null($this->tempDir) && file_exists($this->tempDir)) {
         return $this->tempDir;
     }
     $n = 0;
     do {
         $n++;
         $tempDir = $this->tempDirRoot . "{$n}/";
         $exists = is_dir($tempDir);
         if ($exists) {
             $time = filemtime($tempDir);
             if ($time < time() - $this->tempDirMaxAge) {
                 // dir is old and can be removed
                 if (wireRmdir($tempDir, true)) {
                     $exists = false;
                 }
             }
         }
     } while ($exists);
     if (!wireMkdir($tempDir, true)) {
         throw new WireException($this->_('Unable to create temp dir') . " - {$tempDir}");
     }
     $this->tempDir = $tempDir;
     return $tempDir;
 }
 /**
  * Unzip the module file to tempDir and then copy to destination directory
  *
  * @param string $file File to unzip
  * @param string $destinationDir Directory to copy completed files into. Optionally omit to determine automatically.
  * @return bool|string Returns destinationDir on success, false on failure
  * @throws WireException
  *
  */
 public function unzipModule($file, $destinationDir = '')
 {
     $success = false;
     $tempDir = $this->getTempDir();
     $mkdirDestination = false;
     try {
         $files = wireUnzipFile($file, $tempDir);
         if (is_file($file)) {
             unlink($file);
         }
         foreach ($files as $f) {
             $this->message("Extracted: {$f}", Notice::debug);
         }
     } catch (Exception $e) {
         $this->error($e->getMessage());
         if (is_file($file)) {
             unlink($file);
         }
         return false;
     }
     if (!$destinationDir) {
         $destinationDir = $this->determineDestinationDir($files);
         if (!$destinationDir) {
             throw new WireException($this->_('Unable to find any module files'));
         }
     }
     $this->message("Destination directory: {$destinationDir}", Notice::debug);
     $files0 = trim($files[0], '/');
     $extractedDir = is_dir("{$tempDir}/{$files0}") && substr($files0, 0, 1) != '.' ? "{$files0}/" : "";
     // now create module directory and copy files over
     if (is_dir($destinationDir)) {
         // destination dir already there, perhaps an older version of same module?
         // create a backup of it
         $hasBackup = $this->backupDir($destinationDir);
         if ($hasBackup) {
             wireMkdir($destinationDir, true);
         }
     } else {
         if (wireMkdir($destinationDir, true)) {
             $mkdirDestination = true;
         }
         $hasBackup = false;
     }
     // label to identify destinationDir in messages and errors
     $dirLabel = str_replace($this->config->paths->root, '/', $destinationDir);
     if (is_dir($destinationDir)) {
         $from = $tempDir . $extractedDir;
         if (wireCopy($from, $destinationDir)) {
             $this->message($this->_('Successfully copied files to new directory:') . ' ' . $dirLabel);
             wireChmod($destinationDir, true);
             $success = true;
         } else {
             $this->error($this->_('Unable to copy files to new directory:') . ' ' . $dirLabel);
             if ($hasBackup) {
                 $this->restoreDir($destinationDir);
             }
         }
     } else {
         $this->error($this->_('Could not create directory:') . ' ' . $dirLabel);
     }
     if (!$success) {
         $this->error($this->_('Unable to copy module files:') . ' ' . $dirLabel);
         if ($mkdirDestination && !wireRmdir($destinationDir, true)) {
             $this->error($this->_('Could not delete failed module dir:') . ' ' . $destinationDir, Notice::log);
         }
     }
     return $success ? $destinationDir : false;
 }
 /**
  * Returns a temporary directory (path) 
  *
  * @return string Returns path
  * @throws WireException If can't create temporary dir
  *
  */
 public function get()
 {
     // first check if cached result from previous call
     if (!is_null($this->tempDir) && file_exists($this->tempDir)) {
         return $this->tempDir;
     }
     // find unique temp dir
     $n = 0;
     do {
         $n++;
         $tempDir = $this->tempDirRoot . "{$n}/";
         $exists = is_dir($tempDir);
         if ($exists) {
             // check if we can remove existing temp dir
             $time = filemtime($tempDir);
             if ($time < time() - $this->tempDirMaxAge) {
                 // dir is old and can be removed
                 if (wireRmdir($tempDir, true)) {
                     $exists = false;
                 }
             }
         }
     } while ($exists);
     // create temp dir
     if (!wireMkdir($tempDir, true)) {
         clearstatcache();
         if (!is_dir($tempDir) && !wireMkdir($tempDir, true)) {
             throw new WireException($this->_('Unable to create temp dir') . " - {$tempDir}");
         }
     }
     // cache result
     $this->tempDir = $tempDir;
     return $tempDir;
 }