text.php 8.4 KB

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