text.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Selects the language the page will be displayed on.
  4. *
  5. * First, it tries to read the language cookie in the client. If none is
  6. * set, get the browser language preference list. If none is provided or
  7. * they are not supported, it will use the default language.
  8. *
  9. * @param MySQL_connection $db Connection to the database.
  10. * @return string Lowercase, two-letter language code.
  11. */
  12. function select_language($db){
  13. // Get available languages from db
  14. $available_languages = array();
  15. $q_lang = mysqli_query($db, "SELECT code FROM lang WHERE active = 1;");
  16. while ($r_lang = mysqli_fetch_array($q_lang)){
  17. array_push($available_languages, $r_lang["code"]);
  18. }
  19. // Is there a language cookie installed on the client?.
  20. header("Cache-control: private");
  21. if (isSet($_COOKIE["lang"])){
  22. $lang = $_COOKIE["lang"];
  23. if (in_array($lang, $available_languages)){
  24. header("Location: " . get_protocol() . $_SERVER["HTTP_HOST"] . "/" . $lang . $_SERVER["REQUEST_URI"]);
  25. return $lang;
  26. }
  27. }
  28. // If no cookie, select from client browser preferences.
  29. $lang = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
  30. if (in_array($lang, $available_languages)){
  31. header("Location: " . get_protocol() . $_SERVER["HTTP_HOST"] . "/" . $lang . $_SERVER["REQUEST_URI"]);
  32. return $lang;
  33. }
  34. // If no method was succesfull, default language
  35. $lang = $available_languages[0];
  36. header("Location: " . get_protocol() . $_SERVER["HTTP_HOST"] . "/" . $lang . $_SERVER["REQUEST_URI"]);
  37. return $lang;
  38. }
  39. /**
  40. * Parses the language prefrences of the client broser, comparing them to
  41. * the languages offered by the site and selects the prefered one among
  42. * the ones supported.
  43. *
  44. * @param string[] available_languages List of lowercase, two-letter
  45. * language codes allowed by the site.
  46. * @param string http_accept_language Raw header with info about client
  47. * language preferences.
  48. * @return string Lowercase, two-letter code of the prefered available
  49. * language.
  50. */
  51. function prefered_language(array $available_languages, $http_accept_language) {
  52. $available_languages = array_flip($available_languages);
  53. $langs;
  54. preg_match_all("~([\w-]+)(?:[^,\d]+([\d.]+))?~", strtolower($http_accept_language), $matches, PREG_SET_ORDER);
  55. foreach($matches as $match) {
  56. list($a, $b) = explode("-", $match[1]) + array("", "");
  57. $value = isset($match[2]) ? (float) $match[2] : 1.0;
  58. if(isset($available_languages[$match[1]])) {
  59. $langs[$match[1]] = $value;
  60. continue;
  61. }
  62. if(isset($available_languages[$a])) {
  63. $langs[$a] = $value - 0.1;
  64. }
  65. }
  66. arsort($langs);
  67. return $langs[0];
  68. }
  69. /**
  70. * Tturns a date string into a human readable string on the specified
  71. * language. Only works for spanish, basque and english.
  72. *
  73. * @param string $str_date Date string ('yyyy-mm-dd' or 'yyyy-mm-dd HH:MM:SS')
  74. * @param string $lang Language code (es, en, eu).
  75. * @param string $time Append the time at the end.
  76. * @return string Formatted date string.
  77. */
  78. function format_date($str_date, $lang, $time = true){
  79. $date = strtotime($str_date);
  80. $year = date("o", $date);
  81. $month = date("n", $date);
  82. $month --;
  83. $day = date("j", $date);
  84. $wday = date("N", $date);
  85. $wday --;
  86. $hour = date("H", $date);
  87. $minute = date("i", $date);
  88. switch ($lang){
  89. case "en":
  90. $week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
  91. $months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  92. if ($time){
  93. $str = "$week[$wday], $months[$month] $day, $year at $hour:$minute";
  94. }
  95. else{
  96. $str = "$week[$wday], $months[$month] $day, $year";
  97. }
  98. break;
  99. case "eu":
  100. $week = ["astelehena", "asteartea", "asteazkena", "osteguna", "ostirala", "larumbata", "igandea"];
  101. $months = ["urtarrilaren", "otsailaren", "martxoaren", "apirilaren", "maiatzaren", "ekainaren", "uztailaren", "abuztuaren", "irailaren", "urriaren", "azaroaren", "abenduaren"];
  102. if ($time){
  103. $str = $year . "ko $months[$month] $day" . "an, $week[$wday], $hour:$minute";
  104. }
  105. else{
  106. $str = $year . "ko $months[$month] $day" . "an, $week[$wday]";
  107. }
  108. break;
  109. default:
  110. $week = ["Lunes", "Martes", "Mi&eacute;rcoles", "Jueves", "Viernes", "S&aacute;bado", "Domingo"];
  111. $months = ["enero", " febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"];
  112. if ($time){
  113. $str = "$week[$wday] $day de $months[$month] de $year a las $hour:$minute";
  114. }
  115. else{
  116. $str = "$week[$wday] $day de $months[$month] de $year";
  117. }
  118. }
  119. return $str;
  120. }
  121. /**
  122. * Retrieves a text fragment from the database.
  123. *
  124. * @param Page|Entity $model An initialized data model.
  125. * @param string $id Text identifier.
  126. * @returns string The text.
  127. */
  128. function text($model, $id){
  129. global $path;
  130. error_log("SELECT text, file FROM text WHERE id = '$id' AND lang = '" . $model->lang . "';");
  131. $q = mysqli_query($model->db, "SELECT text, file FROM text WHERE id = '$id' AND lang = '" . $model->lang . "';");
  132. $r = mysqli_fetch_array($q);
  133. if (strlen($r["file"]) > 0){
  134. return file_get_contents($path["string"] . $r["file"]);
  135. }
  136. else{
  137. return $r["text"];
  138. }
  139. }
  140. /**
  141. * This function closes all the opened HTML tags in a given string.
  142. *
  143. * @param string html The string with HTML tags.
  144. * @return string HTML with closed tags.
  145. */
  146. function close_tags($html) {
  147. $result = [];
  148. preg_match_all("#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU", $html, $result);
  149. $openedtags = $result[1];
  150. preg_match_all("#</([a-z]+)>#iU", $html, $result);
  151. $closedtags = $result[1];
  152. $len_opened = count($openedtags);
  153. if (count($closedtags) == $len_opened) {
  154. return $html;
  155. }
  156. $openedtags = array_reverse($openedtags);
  157. for ($i=0; $i < $len_opened; $i++) {
  158. if (!in_array($openedtags[$i], $closedtags)) {
  159. $html .= "</".$openedtags[$i].">";
  160. } else {
  161. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  162. }
  163. }
  164. return $html;
  165. }
  166. /**
  167. * Text shortener. Given a string, it trims in the proximity of the
  168. * desired string, up to the next white character. If indicated, it will
  169. * append a link to the full text.
  170. *
  171. * @param string $text The text to shorten.
  172. * @param int $length The desired length.
  173. * @param string $link_text Text for the link. Optional.
  174. * @param string $link URI of the link.
  175. * @return string Shortened text.
  176. */
  177. function cut_text($text, $length, $link_text = "", $link = ""){
  178. if (strlen($text) < $length){
  179. return $text;
  180. }
  181. $cut = substr($text, 0, strpos($text, " ", $length));
  182. $cut = close_tags($cut);
  183. if (strlen($cut) == 0){
  184. $cut = $text;
  185. }
  186. if (strlen($text) != strlen($cut) && strlen($link) > 0 && strlen($link_text) > 0){
  187. $cut = $cut . "... <a href='$link'>$link_text</a>";
  188. }
  189. return $cut;
  190. }
  191. /**
  192. * Creates the srcset attribute for content images.
  193. * Creates 9 different resolutions, from 100px to 900px.
  194. *
  195. * @param string $file Path to the file, relative to $path["img"]["content"]).
  196. * @return string srcset atttribute content.
  197. */
  198. function srcset($file){
  199. global $path;
  200. $srcset = "";
  201. $dir = dirname($file);
  202. $name = basename($file);
  203. foreach (range(1, 9) as $d) {
  204. $srcset = $srcset . $path["img"]["content"] . $dir . "/x" . $d . "00/" . $name . " " . $d . "00px, ";
  205. }
  206. $srcset = rtrim($srcset, ", ");
  207. return $srcset;
  208. }
  209. ?>