locale.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php namespace HashOver;
  2. // Copyright (C) 2010-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class Locale
  27. {
  28. public $setup;
  29. public $mode;
  30. public $text;
  31. public function __construct (Setup $setup)
  32. {
  33. $this->setup = $setup;
  34. $this->mode = $setup->usage['mode'];
  35. // Get appropriate locale file
  36. $locale_file_path = $this->getLocaleFile ();
  37. // Include the locale file
  38. $this->includeLocaleFile ($locale_file_path);
  39. // Prepare locale
  40. $this->prepareLocale ();
  41. }
  42. // Check for PHP locale file
  43. protected function getLocaleFile ()
  44. {
  45. // Locales checklist
  46. $locales = array ();
  47. // Lowercase language code
  48. $language = mb_strtolower ($this->setup->language);
  49. // Check if we are automatically selecting the locale
  50. if ($language === 'auto') {
  51. // If so, get system locale
  52. $system_locale = mb_strtolower (setlocale (LC_CTYPE, 0));
  53. // Split the locale into specific parts
  54. $locale_parts = explode ('.', $system_locale);
  55. $language_parts = explode ('_', $locale_parts[0]);
  56. // Add locale in 'en-us' format to checklist
  57. $full_locale = str_replace ('_', '-', $locale_parts[0]);
  58. $locales[] = $full_locale;
  59. // Add front part of locale ('en') to checklist
  60. $locales[] = $language_parts[0];
  61. // Add end part of locale ('us') to checklist
  62. if (!empty ($language_parts[1])) {
  63. $locales[] = $language_parts[1];
  64. }
  65. } else {
  66. // If not, add configured locale to checklist
  67. $locales[] = $language;
  68. }
  69. foreach ($locales as $locale) {
  70. // Locale file path
  71. $locale_file = __DIR__ . '/locales/' . $locale . '.php';
  72. // Check if a locale file exists for current locale
  73. if (file_exists ($locale_file)) {
  74. // If so, return PHP locale file path
  75. return $locale_file;
  76. }
  77. }
  78. // Otherwise, default to English
  79. return __DIR__ . '/locales/en.php';
  80. }
  81. protected function includeLocaleFile ($file)
  82. {
  83. // Check if the locale file can be included
  84. if (@include ($file)) {
  85. // If so, set locale to array stored in the file
  86. $this->text = $locale;
  87. } else {
  88. // If not, throw exception
  89. $language = strtoupper ($language);
  90. $exception = $language . ' locale file could not be included!';
  91. throw new \Exception ($exception);
  92. }
  93. }
  94. // Prepares locale by modifing them in various ways
  95. public function prepareLocale ()
  96. {
  97. foreach ($this->text as $key => $value) {
  98. switch ($key) {
  99. // Add optionality to form field title locales
  100. case 'name-tip':
  101. case 'password-tip':
  102. case 'email-tip':
  103. case 'website-tip': {
  104. $field = str_replace ('-tip', '', $key);
  105. $option = $this->setup->fieldOptions[$field];
  106. $option = ($option === 'required') ? 'required' : 'optional';
  107. $option_locale = mb_strtolower ($this->text[$option]);
  108. $field_tip = sprintf ($this->text[$key], $option_locale);
  109. $this->set ($key, $field_tip);
  110. break;
  111. }
  112. // Inject date and time formats into date-time locale
  113. case 'date-time': {
  114. $date_format = $this->setup->dateFormat;
  115. $time_format = $this->setup->timeFormat;
  116. $date_time = sprintf ($value, $date_format, $time_format);
  117. $this->set ($key, $date_time);
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. // Sets a locale string
  124. public function set ($name, $value)
  125. {
  126. $this->text[$name] = $value;
  127. }
  128. // Returns a locale string, optionally adding C-style escaping
  129. public function get ($name, $add_slashes = true, $charlist = "\\'")
  130. {
  131. // Don't escape given string in PHP mode or if told not to
  132. if ($this->mode === 'php' or $add_slashes === false) {
  133. return $this->text[$name];
  134. }
  135. // Check if locale is an array
  136. if (is_array ($this->text[$name])) {
  137. $escaped_array = array ();
  138. // If so, escape each item in the array
  139. foreach ($this->text[$name] as $key => $value) {
  140. $escaped_array[$key] = addcslashes ($value, $charlist);
  141. }
  142. // And return the new array
  143. return $escaped_array;
  144. }
  145. // Otherwise escape the single string
  146. return addcslashes ($this->text[$name], $charlist);
  147. }
  148. }