00001 <?php
00019 define('LANGUAGE_PACK_DESCRIPTOR_URL', 'http://pkp.sfu.ca/omp/xml/%s/locales.xml');
00020 define('LANGUAGE_PACK_TAR_URL', 'http://pkp.sfu.ca/omp/xml/%s/%s.tar.gz');
00021
00022 class LanguageAction {
00026 function LanguageAction() {
00027 }
00028
00029
00030
00031
00032
00039 function isDownloadAvailable() {
00040
00041 if (!function_exists('proc_open')) return false;
00042
00043
00044
00045 if (!version_compare(phpversion(), '4.3.0')) return false;
00046
00047
00048 $tarExecutable = Config::getVar('cli', 'tar');
00049 if (!$tarExecutable) return false;
00050
00051 $fds = array(
00052 0 => array('pipe', 'r'),
00053 1 => array('pipe', 'w'),
00054 2 => array('pipe', 'w')
00055 );
00056
00057 $pipes = $process = null;
00058 @$process = proc_open($tarExecutable . ' --version', $fds, $pipes);
00059 if (!is_resource($process)) return false;
00060 fclose($pipes[0]);
00061 stream_get_contents($pipes[1]);
00062 stream_get_contents($pipes[2]);
00063 fclose($pipes[1]);
00064 fclose($pipes[2]);
00065 if (proc_close($process) !== 0) return false;
00066
00067
00068 if (!is_file(LOCALE_REGISTRY_FILE) || !is_writable(LOCALE_REGISTRY_FILE)) return false;
00069
00070 return true;
00071 }
00072
00077 function getDownloadableLocales() {
00078 $versionDao =& DAORegistry::getDAO('VersionDAO');
00079 $version =& $versionDao->getCurrentVersion();
00080 $versionString = $version->getVersionString();
00081
00082 $descriptorFilename = sprintf(LANGUAGE_PACK_DESCRIPTOR_URL, $versionString);
00083 return AppLocale::loadLocaleList($descriptorFilename);
00084 }
00085
00092 function downloadLocale($locale, &$errors) {
00093 $downloadableLocales =& $this->getDownloadableLocales();
00094 if (!is_array($downloadableLocales) || !isset($downloadableLocales[$locale])) {
00095 $errors[] = __('admin.languages.download.cannotOpen');
00096 return false;
00097 }
00098
00099 $versionDao =& DAORegistry::getDAO('VersionDAO');
00100 $version =& $versionDao->getCurrentVersion();
00101 $versionString = $version->getVersionString();
00102
00103
00104 $fds = array(
00105 0 => array('pipe', 'r'),
00106 1 => array('pipe', 'w'),
00107 2 => array('pipe', 'w')
00108 );
00109 $pipes = null;
00110
00111 $tarExecutable = Config::getVar('cli', 'tar');
00112 $process = proc_open($tarExecutable . ' -x -z --wildcards \\*' . $locale . '\\*.xml \\*' . $locale . '\\*.png', $fds, $pipes);
00113 if (!is_resource($process)) return false;
00114
00115
00116 $languagePackUrl = sprintf(LANGUAGE_PACK_TAR_URL, $versionString, $locale);
00117 $wrapper =& FileWrapper::wrapper($languagePackUrl);
00118 if (!$wrapper->open()) {
00119 $errors[] = __('admin.languages.download.cannotOpen');
00120 }
00121
00122 stream_set_blocking($pipes[0], 0);
00123 stream_set_blocking($pipes[1], 0);
00124 stream_set_blocking($pipes[2], 0);
00125
00126 $pipeStdout = $pipeStderr = '';
00127 while ($data = $wrapper->read()) {
00128 fwrite($pipes[0], $data);
00129 $pipeStdout .= fgets($pipes[1]);
00130 $pipeStderr .= fgets($pipes[2]);
00131 }
00132 fclose($pipes[0]);
00133 fclose($pipes[1]);
00134 fclose($pipes[2]);
00135 unset($wrapper);
00136
00137 if (!empty($pipeStderr)) {
00138 $errors[] = $pipeStderr;
00139 return false;
00140 }
00141
00142
00143 if (proc_close($process) !== 0) return false;
00144
00145
00146
00147 $locales = AppLocale::getAllLocales();
00148 if (!isset($locales[$locale])) {
00149
00150 $wrapper =& FileWrapper::wrapper(LOCALE_REGISTRY_FILE);
00151 $contents = $wrapper->contents();
00152 $pos = strpos($contents, '</locales>');
00153 if ($pos === false) {
00154
00155 $errors[] = __('admin.languages.download.cannotModifyRegistry');
00156 return false;
00157 }
00158 $contents = substr_replace($contents, "\t<locale key=\"$locale\" name=\"" . $downloadableLocales[$locale]['name'] . "\" />\n", $pos, 0);
00159 $fp = fopen(LOCALE_REGISTRY_FILE, 'w');
00160 if (!$fp) {
00161
00162 $errors[] = __('admin.languages.download.cannotModifyRegistry');
00163 return false;
00164 }
00165 fwrite($fp, $contents);
00166 fclose($fwrite);
00167 }
00168
00169 return true;
00170 }
00171 }
00172
00173 ?>