protected function RepositorySetup()
 {
     NarroProject::RegisterPreference('SVN commit path', false, 0, 'text', 'The url to commit this project to SVN.', '');
     NarroProject::RegisterPreference('Username for SVN', false, 0, 'text', '', '');
     $this->objProject->SetPreferenceValueByName('SVN commit path', $this->txtRepositoryUrl->Text);
     $this->objProject->SetPreferenceValueByName('Username for SVN', $this->txtUsername->Text);
     $this->objProject->Save();
     $this->strSSHKey = sprintf('%s/svn_%d_%s_%d', __TMP_PATH__, QApplication::$User->UserId, QApplication::$TargetLanguage->LanguageCode, $this->objProject->ProjectId);
     $strProcLogFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . QApplication::$TargetLanguage->LanguageCode . '-svn.log';
     copy($this->fileSSHPrivateKey->File, $this->strSSHKey);
     chmod($this->strSSHKey, 0600);
     $mixProcess = NarroUtils::Exec(sprintf('svn checkout %s %s_svn', escapeshellarg($this->txtRepositoryUrl->Text), $this->strSSHKey), $arrOutput, $arrError, $intRetVal, false, array('SVN_SSH' => sprintf('ssh -o StrictHostKeyChecking=no -l %s -p 22 -i %s', escapeshellarg($this->txtUsername->Text), escapeshellarg($this->strSSHKey)), 'HOME' => __TMP_PATH__), __TMP_PATH__, true);
     if ($intRetVal == 0) {
         NarroUtils::RecursiveCopy($this->objProject->DefaultTranslationPath, $this->strSSHKey . '_svn');
         $mixProcess = NarroUtils::Exec('svn add -q ./*', $arrOutput, $arrError, $intRetVal, false, array('SVN_SSH' => sprintf('ssh -o StrictHostKeyChecking=no -l %s -p 22 -i %s', escapeshellarg($this->txtUsername->Text), escapeshellarg($this->strSSHKey)), 'HOME' => __TMP_PATH__), $this->strSSHKey . '_svn', true);
         return $intRetVal;
     } else {
         return $intRetVal;
     }
 }
 public function __get($strName)
 {
     switch ($strName) {
         case "Directory":
             if ($this->chkCopyFilesToDefaultDirectory->Checked && __IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode != $this->txtDirectory->Text) {
                 if (file_exists(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode)) {
                     NarroUtils::RecursiveDelete(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode . '/*');
                 } else {
                     mkdir(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode, 0777);
                 }
                 NarroUtils::RecursiveCopy($this->txtDirectory->Text, __IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode);
                 NarroUtils::RecursiveChmod(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode);
             }
             return $this->txtDirectory->Text;
         default:
             try {
                 return parent::__get($strName);
                 break;
             } catch (QCallerException $objExc) {
                 $objExc->IncrementOffset();
                 throw $objExc;
             }
     }
 }
 protected function GetWorkingDirectory()
 {
     if (!file_exists($this->fileSource->File)) {
         throw new Exception('You have to upload a file');
     }
     $this->strWorkingDirectory = sprintf('%s/upload-u_%d-l_%s-p_%d', __TMP_PATH__, QApplication::GetUserId(), $this->objLanguage->LanguageCode, $this->objProject->ProjectId);
     $this->CleanWorkingDirectory();
     mkdir($this->strWorkingDirectory);
     chmod($this->strWorkingDirectory, 0777);
     switch (strtolower(pathinfo($this->fileSource->File, PATHINFO_EXTENSION))) {
         case 'zip':
         case 'xpi':
             NarroLogger::LogInfo(sprintf('Trying to uncompress %s', $this->fileSource->FileName));
             $objZipFile = new ZipArchive();
             $intErrCode = $objZipFile->open($this->fileSource->File);
             if ($intErrCode === TRUE) {
                 $objZipFile->extractTo($this->strWorkingDirectory);
                 $objZipFile->close();
                 NarroLogger::LogInfo(sprintf('Sucessfully uncompressed %s.', $this->fileSource->FileName));
             } else {
                 switch ($intErrCode) {
                     case ZIPARCHIVE::ER_NOZIP:
                         $strError = 'Not a zip archive';
                         break;
                     default:
                         $strError = 'Error code: ' . $intErrCode;
                 }
                 $this->fileSource->File = '';
                 throw new Exception(sprintf('Failed to uncompress %s: %s', $this->fileSource->File, $strError));
             }
             break;
         case 'dtd':
         case 'properties':
         case 'ini':
         case 'inc':
         case 'po':
         case 'sdf':
         case 'svg':
         case 'dpo':
         case 'srt':
         case 'php':
             copy($this->fileSource->File, $this->strWorkingDirectory . '/' . $this->fileSource->FileName);
             chmod($this->strWorkingDirectory . '/' . $this->fileSource->FileName, 0666);
             NarroLogger::LogInfo(sprintf('Single file uploaded, copied %s to %s', $this->fileSource->FileName, $this->strWorkingDirectory));
             break;
         default:
             throw new Exception(sprintf('Unsupported file type uploaded'));
     }
     if (file_exists($this->fileSource->File)) {
         unlink($this->fileSource->File);
     }
     $this->fileSource->btnDelete_Click();
     $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode);
     if ($arrSearchResult == false) {
         $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '-' . $this->objLanguage->CountryCode);
     }
     if ($arrSearchResult == false) {
         $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '_' . $this->objLanguage->CountryCode);
     }
     NarroUtils::RecursiveChmod($this->strWorkingDirectory);
     if (is_array($arrSearchResult) && count($arrSearchResult) == 1) {
         NarroLogger::LogWarn(sprintf('Path changed from "%s" to "%s" because a directory named "%s" was found deeper in the given path.', $this->strWorkingDirectory, $arrSearchResult[0], $this->objLanguage->LanguageCode));
         $this->strWorkingDirectory = $arrSearchResult[0];
     }
     if ($this->chkCopyFilesToDefaultDirectory->Checked) {
         if (file_exists(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode)) {
             NarroUtils::RecursiveDelete(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode . '/*');
         } else {
             mkdir(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode, 0777);
         }
         NarroUtils::RecursiveCopy($this->strWorkingDirectory, __IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode);
         NarroUtils::RecursiveChmod(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode);
     }
     return $this->strWorkingDirectory;
 }
 protected function GetWorkingDirectory($strCheckoutCommand = null)
 {
     $this->strWorkingDirectory = sprintf('%s/upload-u_%d-l_%s-p_%d', __TMP_PATH__, QApplication::GetUserId(), $this->objLanguage->LanguageCode, $this->objProject->ProjectId);
     $this->CleanWorkingDirectory();
     if (!$this instanceof NarroMercurialSourcePanel) {
         mkdir($this->strWorkingDirectory);
     }
     $strCommand = sprintf($strCheckoutCommand, escapeshellarg($this->txtRepository->Text), escapeshellarg($this->strWorkingDirectory));
     NarroLogger::LogInfo(sprintf('Running "%s"', $strCommand));
     $strProcLogFile = __TMP_PATH__ . '/' . $this->objProject->ProjectId . '-' . $this->objLanguage->LanguageCode . '-vcs.log';
     if (file_exists($strProcLogFile) && is_writable($strProcLogFile)) {
         unlink($strProcLogFile);
     }
     chdir(__TMP_PATH__);
     $mixProcess = proc_open("{$strCommand}", array(1 => array("file", $strProcLogFile, 'a'), 2 => array("file", $strProcLogFile, 'a')), $foo);
     $status = proc_get_status($mixProcess);
     while ($status['running']) {
         $status = proc_get_status($mixProcess);
     }
     proc_close($mixProcess);
     if (!file_exists($this->strWorkingDirectory)) {
         throw new Exception(sprintf('The working directory "%s" does not exist, probably the checkout command failed', $this->strWorkingDirectory));
     }
     chmod($this->strWorkingDirectory, 0777);
     if (file_exists($strProcLogFile)) {
         NarroLogger::LogInfo(file_get_contents($strProcLogFile));
     }
     NarroUtils::RecursiveDelete($this->strWorkingDirectory . '/.hg');
     foreach (NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, '.svn') as $strSvnDir) {
         NarroUtils::RecursiveDelete($strSvnDir);
     }
     $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode);
     if ($arrSearchResult == false) {
         $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '-' . $this->objLanguage->CountryCode);
     }
     if ($arrSearchResult == false) {
         $arrSearchResult = NarroUtils::SearchDirectoryByName($this->strWorkingDirectory, $this->objLanguage->LanguageCode . '_' . $this->objLanguage->CountryCode);
     }
     NarroUtils::RecursiveChmod($this->strWorkingDirectory);
     if (is_array($arrSearchResult) && count($arrSearchResult) == 1) {
         NarroLogger::LogWarn(sprintf('Path changed from "%s" to "%s" because a directory named "%s" was found deeper in the given path.', $this->strWorkingDirectory, $arrSearchResult[0], $this->objLanguage->LanguageCode));
         $this->strWorkingDirectory = $arrSearchResult[0];
     }
     if ($this->chkCopyFilesToDefaultDirectory->Checked) {
         if (file_exists(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode)) {
             NarroUtils::RecursiveDelete(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode . '/*');
         } else {
             mkdir(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode, 0777, true);
         }
         NarroUtils::RecursiveCopy($this->strWorkingDirectory, __IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode);
         NarroUtils::RecursiveChmod(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/' . $this->objLanguage->LanguageCode);
     }
     return $this->strWorkingDirectory;
 }
 public function ImportProject()
 {
     $this->startTimer();
     if (function_exists('popen') && function_exists('escapeshellarg') && function_exists('escapeshellcmd') && file_exists(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/import.sh')) {
         NarroLogger::LogInfo('Found a before import script, trying to run it.');
         $fp = popen(sprintf('/bin/sh %s %s %d %s %d %d 2>&1', escapeshellarg(__IMPORT_PATH__ . '/' . $this->objProject->ProjectId . '/import.sh'), escapeshellarg($this->objTargetLanguage->LanguageCode), $this->objTargetLanguage->LanguageId, escapeshellarg($this->objProject->ProjectName), $this->objProject->ProjectId, QApplication::GetUserId()), 'r');
         $strOutput = '';
         while (!feof($fp)) {
             $strOutput .= fread($fp, 1024);
         }
         if (pclose($fp)) {
             NarroLogger::LogError("Before import script failed:\n" . $strOutput);
         } else {
             NarroLogger::LogInfo("Before import script finished successfully:\n" . $strOutput);
         }
     }
     /**
      * Make an exception for the naro project. The template path will be changed to the locale directory.
      */
     if ($this->objProject->ProjectName == 'Narro') {
         $this->strTemplatePath = __DOCROOT__ . __SUBDIRECTORY__ . '/locale/en-US';
         if (!file_exists($this->strTemplatePath)) {
             mkdir($this->strTemplatePath, 0777);
         }
         $this->strTranslationPath = __DOCROOT__ . __SUBDIRECTORY__ . '/locale/' . $this->objTargetLanguage->LanguageCode;
         if (!file_exists($this->strTranslationPath)) {
             mkdir($this->strTranslationPath, 0777);
         }
         if (SERVER_INSTANCE == 'dev') {
             $this->CreateNarroTemplate($this->objProject->ProjectId);
         }
     }
     if (!file_exists($this->strTemplatePath)) {
         throw new Exception(sprintf('Template path %s does not exist.', $this->strTemplatePath));
     }
     NarroLogger::LogInfo(sprintf('Starting import for the project %s', $this->objProject->ProjectName));
     NarroLogger::LogInfo(sprintf('Template path is %s', $this->strTemplatePath));
     NarroLogger::LogInfo(sprintf('Translation path is %s', $this->strTranslationPath));
     NarroLogger::LogInfo(sprintf('Active plugins: %s', join(',', QApplication::$PluginHandler->ActivePlugins)));
     NarroLogger::LogInfo(sprintf('Running import under user %s', $this->objUser->Username));
     if (is_dir($this->strTemplatePath)) {
         /**
          * If we have the big en-US.sdf file, we need to split it first in smaller pieces
          */
         if ($this->objProject->ProjectType == NarroProjectType::OpenOffice) {
             $strBigGsiFile = $this->strTemplatePath . '/' . $this->objSourceLanguage->LanguageCode . '.sdf';
             if (file_exists($strBigGsiFile)) {
                 NarroLogger::LogInfo(sprintf('Found a big GSI file, splitting it in smaller files before import'));
                 NarroOpenOfficeSdfFileImporter::SplitFile($strBigGsiFile, $this->strTemplatePath, array($this->objSourceLanguage->LanguageCode));
                 unlink($strBigGsiFile);
             }
             $strBigGsiFile = $this->strTranslationPath . '/' . $this->objTargetLanguage->LanguageCode . '.sdf';
             if (file_exists($strBigGsiFile)) {
                 NarroLogger::LogInfo(sprintf('Found a big GSI file, splitting it in smaller files before import'));
                 NarroOpenOfficeSdfFileImporter::SplitFile($strBigGsiFile, $this->strTranslationPath, array($this->objTargetLanguage->LanguageCode));
                 unlink($strBigGsiFile);
             }
         }
         if ($this->blnOnlySuggestions == false) {
             QApplication::$PluginHandler->BeforeImportProject($this->objProject);
         }
         /**
          * Go ahead and import from the directory.
          */
         if ($this->ImportFromDirectory()) {
             /**
              * After the import is finished, copy the files for future use to the project directory
              */
             if ($this->strTemplatePath != $this->objProject->DefaultTemplatePath) {
                 NarroUtils::RecursiveCopy($this->strTemplatePath, $this->objProject->DefaultTemplatePath);
             }
             /**
              * The same for translation files
              */
             if ($this->strTranslationPath != $this->objProject->DefaultTranslationPath) {
                 if (file_exists($this->strTranslationPath)) {
                     NarroUtils::RecursiveCopy($this->strTranslationPath, $this->objProject->DefaultTranslationPath);
                 } else {
                     NarroLogger::LogWarn(sprintf('"%s" is not a directory, not copying to default translation path: "%s"', $this->strTranslationPath, $this->objProject->DefaultTranslationPath));
                 }
             }
             $this->stopTimer();
             NarroLogger::LogInfo(sprintf('Import finished successfully in %d seconds.', NarroImportStatistics::$arrStatistics['End time'] - NarroImportStatistics::$arrStatistics['Start time']));
         } else {
             NarroLogger::LogError('Import failed. See any messages above for details.');
         }
     } else {
         throw new Exception(sprintf('Template path "%s" is not a directory.', $this->strTemplatePath));
     }
     QApplication::$PluginHandler->AfterImportProject($this->objProject);
     $strUploadPath = sprintf('%s/upload-u_%d-l_%s-p_%d', __TMP_PATH__, QApplication::GetUserId(), $this->objSourceLanguage->LanguageCode, $this->objProject->ProjectId);
     if (file_exists($strUploadPath)) {
         NarroUtils::RecursiveDelete($strUploadPath);
     }
     $strUploadPath = sprintf('%s/upload-u_%d-l_%s-p_%d', __TMP_PATH__, QApplication::GetUserId(), $this->objTargetLanguage->LanguageCode, $this->objProject->ProjectId);
     if (file_exists($strUploadPath)) {
         NarroUtils::RecursiveDelete($strUploadPath);
     }
     if ($this->blnOnlySuggestions == false) {
         $this->MarkUnusedFilesAsInactive();
     }
     NarroProgress::ClearProgressFileName($this->objProject->ProjectId, 'import');
     $this->objProject->CountAllTextsByLanguage();
     $this->objProject->CountApprovedTextsByLanguage();
     $this->objProject->CountTranslatedTextsByLanguage();
     foreach (NarroImportStatistics::$arrStatistics as $strName => $strValue) {
         if ($strValue != 0) {
             NarroLogger::LogInfo(stripslashes($strName) . ': ' . $strValue);
         }
     }
     if (SERVER_INSTANCE == 'dev') {
         $strQueryData = '';
         $intTimeSpentOnQueries = 0;
         if (isset($GLOBALS['arrQueries']) && is_array($GLOBALS['arrQueries'])) {
             foreach ($GLOBALS['arrQueries'] as $arrQueryData) {
                 $strQueryData .= sprintf('"%s";"%d"', str_replace("\n", " ", $arrQueryData[0]), $arrQueryData[1]) . "\n";
                 $intTimeSpentOnQueries += $arrQueryData[1];
             }
         }
         file_put_contents(__TMP_PATH__ . '/queries.csv', $strQueryData);
         @chmod(__TMP_PATH__ . '/queries.csv', 0666);
         $strCacheData = '';
         if (isset($GLOBALS['arrCacheQueries']) && is_array($GLOBALS['arrCacheQueries'])) {
             foreach ($GLOBALS['arrCacheQueries'] as $arrCacheData) {
                 $strCacheData .= sprintf('"%s";"%d"', str_replace("\n", " ", $arrCacheData[0]), $arrCacheData[1]) . "\n";
             }
         }
         file_put_contents(__TMP_PATH__ . '/cache.csv', $strCacheData);
         @chmod(__TMP_PATH__ . '/cache.csv', 0666);
         NarroLogger::LogInfo(sprintf('%d queries in total, %d seconds spent on them', count(@$GLOBALS['arrQueries']), $intTimeSpentOnQueries));
         NarroLogger::LogInfo(sprintf('%d cache queries in total, %s seconds spent on them', count(@$GLOBALS['arrCacheQueries']), intval(@$GLOBALS['arrCacheQueriesSpent'])));
     }
 }