text.php 8.4 KB

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