/** * Download zipfile. * * @param array $files * @param string $filename * * @return string */ public function download($files, $filename) { // Get assets $criteria = craft()->elements->getCriteria(ElementType::Asset); $criteria->id = $files; $criteria->limit = null; $assets = $criteria->find(); // Set destination zip $destZip = craft()->path->getTempPath() . $filename . '_' . time() . '.zip'; // Create the zipfile IOHelper::createFile($destZip); // Loop through assets foreach ($assets as $asset) { // Get asset source $source = $asset->getSource(); // Get asset source type $sourceType = $source->getSourceType(); // Get asset file $file = $sourceType->getLocalCopy($asset); // Add to zip Zip::add($destZip, $file, dirname($file)); } // Return zip destination return $destZip; }
/** * Performs the tool's action. * * @param array $params * @return array */ public function performAction($params = array()) { $file = craft()->db->backup(); if (IOHelper::fileExists($file) && isset($params['downloadBackup']) && (bool) $params['downloadBackup']) { $destZip = craft()->path->getTempPath() . IOHelper::getFileName($file, false) . '.zip'; if (IOHelper::fileExists($destZip)) { IOHelper::deleteFile($destZip, true); } IOHelper::createFile($destZip); if (Zip::add($destZip, $file, craft()->path->getDbBackupPath())) { return array('backupFile' => IOHelper::getFileName($destZip, false)); } } }
/** * @param $source * @param $destZip * * @return bool 'true' if the zip was successfully created, 'false' if not. */ public static function compress($source, $destZip) { $source = IOHelper::normalizePathSeparators($source); $destZip = IOHelper::normalizePathSeparators($destZip); if (!IOHelper::folderExists($source) && !IOHelper::fileExists($destZip)) { Craft::log('Tried to zip the contents of ' . $source . ' to ' . $destZip . ', but the source path does not exist.', LogLevel::Error); return false; } if (IOHelper::fileExists($destZip)) { IOHelper::deleteFile($destZip); } IOHelper::createFile($destZip); craft()->config->maxPowerCaptain(); $zip = static::_getZipInstance($destZip); return $zip->zip(IOHelper::getRealPath($source), IOHelper::getRealPath($destZip)); }
/** * @inheritDoc ITool::performAction() * * @param array $params * * @return array */ public function performAction($params = array()) { // In addition to the default tables we want to ignore data in, we also don't care about data in the session // table in this tools' case. $file = craft()->db->backup(array('sessions')); if (IOHelper::fileExists($file) && isset($params['downloadBackup']) && (bool) $params['downloadBackup']) { $destZip = craft()->path->getTempPath() . IOHelper::getFileName($file, false) . '.zip'; if (IOHelper::fileExists($destZip)) { IOHelper::deleteFile($destZip, true); } IOHelper::createFile($destZip); if (Zip::add($destZip, $file, craft()->path->getDbBackupPath())) { return array('backupFile' => IOHelper::getFileName($destZip, false)); } } }
/** * Finish importing. * * @param array $settings * @param string $backup */ public function finish($settings, $backup) { craft()->import_history->end($settings['history'], ImportModel::StatusFinished); if ($settings['email']) { // Gather results $results = array('success' => $settings['rows'], 'errors' => array()); // Gather errors foreach ($this->log as $line => $result) { $results['errors'][$line] = $result; } // Recalculate successful results $results['success'] -= count($results['errors']); // Prepare the mail $email = new EmailModel(); $emailSettings = craft()->email->getSettings(); $email->toEmail = $emailSettings['emailAddress']; // Get current user $currentUser = craft()->userSession->getUser(); // Zip the backup if ($currentUser->can('backup') && $settings['backup'] && IOHelper::fileExists($backup)) { $destZip = craft()->path->getTempPath() . IOHelper::getFileName($backup, false) . '.zip'; if (IOHelper::fileExists($destZip)) { IOHelper::deleteFile($destZip, true); } IOHelper::createFile($destZip); if (Zip::add($destZip, $backup, craft()->path->getDbBackupPath())) { $backup = $destZip; } } // Set email content $email->subject = Craft::t('The import task is finished'); $email->htmlBody = TemplateHelper::getRaw(craft()->templates->render('import/_email', array('results' => $results, 'backup' => $backup))); // Send it craft()->email->sendEmail($email); } }
/** * Creates a new support ticket for the GetHelp widget. */ public function actionSendSupportRequest() { $this->requirePostRequest(); $this->requireAjaxRequest(); craft()->config->maxPowerCaptain(); $getHelpModel = new GetHelpModel(); $getHelpModel->fromEmail = craft()->request->getPost('fromEmail'); $getHelpModel->message = craft()->request->getPost('message'); $getHelpModel->attachDebugFiles = (bool) craft()->request->getPost('attachDebugFiles'); if ($getHelpModel->validate()) { $user = craft()->userSession->getUser(); $requestParamDefaults = array('sFirstName' => $user->getFriendlyName(), 'sLastName' => $user->lastName ? $user->lastName : 'Doe', 'sEmail' => $getHelpModel->fromEmail, 'tNote' => $getHelpModel->message); $requestParams = $requestParamDefaults; $hsParams = array('helpSpotApiURL' => 'https://support.pixelandtonic.com/api/index.php'); try { if ($getHelpModel->attachDebugFiles) { $tempZipFile = craft()->path->getTempPath() . StringHelper::UUID() . '.zip'; IOHelper::createFile($tempZipFile); if (IOHelper::folderExists(craft()->path->getLogPath())) { // Grab the latest log file. Zip::add($tempZipFile, craft()->path->getLogPath() . 'craft.log', craft()->path->getStoragePath()); // Grab the most recent rolled-over log file, if one exists. if (IOHelper::fileExists(craft()->path->getLogPath() . 'craft.log.1')) { Zip::add($tempZipFile, craft()->path->getLogPath() . 'craft.log.1', craft()->path->getStoragePath()); } // Grab the phperrors log file, if it exists. if (IOHelper::fileExists(craft()->path->getLogPath() . 'phperrors.log')) { Zip::add($tempZipFile, craft()->path->getLogPath() . 'phperrors.log', craft()->path->getStoragePath()); } } if (IOHelper::folderExists(craft()->path->getDbBackupPath())) { // Make a fresh database backup of the current schema/data. craft()->db->backup(); $contents = IOHelper::getFolderContents(craft()->path->getDbBackupPath()); rsort($contents); // Only grab the most recent 3 sorted by timestamp. for ($counter = 0; $counter <= 2; $counter++) { if (isset($contents[$counter])) { Zip::add($tempZipFile, $contents[$counter], craft()->path->getStoragePath()); } } } $requestParams['File1_sFilename'] = 'SupportAttachment.zip'; $requestParams['File1_sFileMimeType'] = 'application/zip'; $requestParams['File1_bFileBody'] = base64_encode(IOHelper::getFileContents($tempZipFile)); // Bump the default timeout because of the attachment. $hsParams['callTimeout'] = 120; } } catch (\Exception $e) { Craft::log('Tried to attach debug logs to a support request and something went horribly wrong: ' . $e->getMessage(), LogLevel::Warning); // There was a problem zipping, so reset the params and just send the email without the attachment. $requestParams = $requestParamDefaults; } require_once craft()->path->getLibPath() . 'HelpSpotAPI.php'; $hsapi = new \HelpSpotAPI($hsParams); $result = $hsapi->requestCreate($requestParams); if ($result) { if ($getHelpModel->attachDebugFiles) { if (IOHelper::fileExists($tempZipFile)) { IOHelper::deleteFile($tempZipFile); } } $this->returnJson(array('success' => true)); } else { $hsErrors = array_filter(preg_split("/(\r\n|\n|\r)/", $hsapi->errors)); $this->returnJson(array('errors' => array('Support' => $hsErrors))); } } else { $this->returnJson(array('errors' => $getHelpModel->getErrors())); } }
/** * @return string */ private function _createZip() { $zipFile = craft()->path->getTempPath() . StringHelper::UUID() . '.zip'; IOHelper::createFile($zipFile); return $zipFile; }
/** * Get a temporary file path. * * @param string $extension extension to use. "tmp" by default. * * @return mixed */ public static function getTempFilePath($extension = 'tmp') { $extension = strpos($extension, '.') !== false ? pathinfo($extension, PATHINFO_EXTENSION) : $extension; $fileName = uniqid('assets', true) . '.' . $extension; return IOHelper::createFile(craft()->path->getTempPath() . $fileName)->getRealPath(); }
/** * Get a temporary file path. * * @param string $extension extension to use. "tmp" by default. * @return mixed */ public static function getTempFilePath($extension = 'tmp') { $extension = preg_replace('/[^a-z]/i', '', $extension); $fileName = uniqid('assets', true) . '.' . $extension; return IOHelper::createFile(craft()->path->getTempPath() . $fileName)->getRealPath(); }
/** * @codeCoverageIgnore * * @param array $settings * @param string $backup * @param UserModel $currentUser * * @return string Backup filename */ protected function saveBackup($settings, $backup, $currentUser) { if ($currentUser->can('backup') && $settings['backup'] && IOHelper::fileExists($backup)) { $destZip = craft()->path->getTempPath() . IOHelper::getFileName($backup, false) . '.zip'; if (IOHelper::fileExists($destZip)) { IOHelper::deleteFile($destZip, true); } IOHelper::createFile($destZip); if (Zip::add($destZip, $backup, craft()->path->getDbBackupPath())) { $backup = $destZip; } } return $backup; }
/** * Export a submission to the export's zip file. * * @param AmForms_ExportModel $export * @param AmForms_SubmissionModel $submission */ private function _exportSubmissionToZip(AmForms_ExportModel $export, AmForms_SubmissionModel $submission) { // Get submission content $content = craft()->amForms_submissions->getSubmissionEmailBody($submission); // Create submission file $fileName = IOHelper::cleanFilename($submission->title); $fileExtension = '.html'; $folder = $this->_getExportPath(); $file = $folder . $fileName . $fileExtension; $counter = 1; while (!IOHelper::createFile($file)) { $file = $folder . $fileName . $counter . $fileExtension; $counter++; } // Add content to file file_put_contents($file, $content); // Add file to zip Zip::add($export->file, $file, $folder); // Find possible assets foreach ($this->_exportFields[$export->id] as $fieldHandle => $field) { if (is_array($field)) { $field = (object) $field; // Fix standard fields } if ($field->type == 'Assets') { foreach ($submission->{$fieldHandle}->find() as $asset) { $assetPath = craft()->amForms->getPathForAsset($asset); if (IOHelper::fileExists($assetPath . $asset->filename)) { Zip::add($export->file, $assetPath . $asset->filename, $assetPath); } } } } // Remove submission file now that's in the zip IOHelper::deleteFile($file); }