/**
  * 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()));
     }
 }
 /**
  * Creates a new support ticket for the GetHelp widget.
  *
  * @return null
  */
 public function actionSendSupportRequest()
 {
     $this->requirePostRequest();
     craft()->config->maxPowerCaptain();
     $success = false;
     $errors = array();
     $zipFile = null;
     $tempFolder = null;
     $widgetId = craft()->request->getPost('widgetId');
     $namespace = craft()->request->getPost('namespace');
     $namespace = $namespace ? $namespace . '.' : '';
     $getHelpModel = new GetHelpModel();
     $getHelpModel->fromEmail = craft()->request->getPost($namespace . 'fromEmail');
     $getHelpModel->message = trim(craft()->request->getPost($namespace . 'message'));
     $getHelpModel->attachLogs = (bool) craft()->request->getPost($namespace . 'attachLogs');
     $getHelpModel->attachDbBackup = (bool) craft()->request->getPost($namespace . 'attachDbBackup');
     $getHelpModel->attachTemplates = (bool) craft()->request->getPost($namespace . 'attachTemplates');
     $getHelpModel->attachment = UploadedFile::getInstanceByName($namespace . 'attachAdditionalFile');
     if ($getHelpModel->validate()) {
         $user = craft()->userSession->getUser();
         // Add some extra info about this install
         $message = $getHelpModel->message . "\n\n" . "------------------------------\n\n" . 'Craft ' . craft()->getEditionName() . ' ' . craft()->getVersion() . '.' . craft()->getBuild();
         $plugins = craft()->plugins->getPlugins();
         if ($plugins) {
             $pluginNames = array();
             foreach ($plugins as $plugin) {
                 $pluginNames[] = $plugin->getName() . ' ' . $plugin->getVersion() . ' (' . $plugin->getDeveloper() . ')';
             }
             $message .= "\nPlugins: " . implode(', ', $pluginNames);
         }
         $requestParamDefaults = array('sFirstName' => $user->getFriendlyName(), 'sLastName' => $user->lastName ? $user->lastName : 'Doe', 'sEmail' => $getHelpModel->fromEmail, 'tNote' => $message);
         $requestParams = $requestParamDefaults;
         $hsParams = array('helpSpotApiURL' => 'https://support.pixelandtonic.com/api/index.php');
         try {
             if ($getHelpModel->attachLogs || $getHelpModel->attachDbBackup) {
                 if (!$zipFile) {
                     $zipFile = $this->_createZip();
                 }
                 if ($getHelpModel->attachLogs && IOHelper::folderExists(craft()->path->getLogPath())) {
                     // Grab it all.
                     $logFolderContents = IOHelper::getFolderContents(craft()->path->getLogPath());
                     foreach ($logFolderContents as $file) {
                         // Make sure it's a file.
                         if (IOHelper::fileExists($file)) {
                             Zip::add($zipFile, $file, craft()->path->getStoragePath());
                         }
                     }
                 }
                 if ($getHelpModel->attachDbBackup && IOHelper::folderExists(craft()->path->getDbBackupPath())) {
                     // Make a fresh database backup of the current schema/data. We want all data from all tables
                     // for debugging.
                     craft()->db->backup();
                     $backups = IOHelper::getLastModifiedFiles(craft()->path->getDbBackupPath(), 3);
                     foreach ($backups as $backup) {
                         if (IOHelper::getExtension($backup) == 'sql') {
                             Zip::add($zipFile, $backup, craft()->path->getStoragePath());
                         }
                     }
                 }
             }
             if ($getHelpModel->attachment) {
                 // If we don't have a zip file yet, create one now.
                 if (!$zipFile) {
                     $zipFile = $this->_createZip();
                 }
                 $tempFolder = craft()->path->getTempPath() . StringHelper::UUID() . '/';
                 if (!IOHelper::folderExists($tempFolder)) {
                     IOHelper::createFolder($tempFolder);
                 }
                 $tempFile = $tempFolder . $getHelpModel->attachment->getName();
                 $getHelpModel->attachment->saveAs($tempFile);
                 // Make sure it actually saved.
                 if (IOHelper::fileExists($tempFile)) {
                     Zip::add($zipFile, $tempFile, $tempFolder);
                 }
             }
             if ($getHelpModel->attachTemplates) {
                 // If we don't have a zip file yet, create one now.
                 if (!$zipFile) {
                     $zipFile = $this->_createZip();
                 }
                 if (IOHelper::folderExists(craft()->path->getLogPath())) {
                     // Grab it all.
                     $templateFolderContents = IOHelper::getFolderContents(craft()->path->getSiteTemplatesPath());
                     foreach ($templateFolderContents as $file) {
                         // Make sure it's a file.
                         if (IOHelper::fileExists($file)) {
                             $templateFolderName = IOHelper::getFolderName(craft()->path->getSiteTemplatesPath(), false);
                             $siteTemplatePath = craft()->path->getSiteTemplatesPath();
                             $tempPath = substr($siteTemplatePath, 0, strlen($siteTemplatePath) - strlen($templateFolderName) - 1);
                             Zip::add($zipFile, $file, $tempPath);
                         }
                     }
                 }
             }
             if ($zipFile) {
                 $requestParams['File1_sFilename'] = 'SupportAttachment-' . IOHelper::cleanFilename(craft()->getSiteName()) . '.zip';
                 $requestParams['File1_sFileMimeType'] = 'application/zip';
                 $requestParams['File1_bFileBody'] = base64_encode(IOHelper::getFileContents($zipFile));
                 // Bump the default timeout because of the attachment.
                 $hsParams['callTimeout'] = 120;
             }
             // Grab the license.key file.
             if (IOHelper::fileExists(craft()->path->getLicenseKeyPath())) {
                 $requestParams['File2_sFilename'] = 'license.key';
                 $requestParams['File2_sFileMimeType'] = 'text/plain';
                 $requestParams['File2_bFileBody'] = base64_encode(IOHelper::getFileContents(craft()->path->getLicenseKeyPath()));
             }
         } 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 ($zipFile) {
                 if (IOHelper::fileExists($zipFile)) {
                     IOHelper::deleteFile($zipFile);
                 }
             }
             if ($tempFolder) {
                 IOHelper::clearFolder($tempFolder);
                 IOHelper::deleteFolder($tempFolder);
             }
             $success = true;
         } else {
             $hsErrors = array_filter(preg_split("/(\r\n|\n|\r)/", $hsapi->errors));
             $errors = array('Support' => $hsErrors);
         }
     } else {
         $errors = $getHelpModel->getErrors();
     }
     $this->renderTemplate('_components/widgets/GetHelp/response', array('success' => $success, 'errors' => JsonHelper::encode($errors), 'widgetId' => $widgetId));
 }