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