functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /****************************************************
  3. * This function is called from almost everywhere at *
  4. * the beggining of the page. It initializes the *
  5. * session variables, connect to the db, enabling *
  6. * the variable $con for futher use everywhere in *
  7. * the php code, and populates the arrays $user *
  8. * and $permission, with info about the user. *
  9. * @params: *
  10. * mode: (string) Indicates required permissions *
  11. * on database. 'ro' gives read *
  12. * permissions, and 'rw' read and write *
  13. * permissions. Other values will result in *
  14. * errors. *
  15. * @return: (db connection): The connection handler. *
  16. ****************************************************/
  17. function startdb($mode = 'ro'){
  18. //Include the db configuration file. It's somehow like this
  19. /*
  20. <?php
  21. $host = 'XXXX';
  22. $db_name = 'XXXX';
  23. $username_ro = 'XXXX';
  24. $username_rw = 'XXXX';
  25. $pass_ro = 'XXXX';
  26. $pass_rw = 'XXXX';
  27. ?>
  28. */
  29. include('.htpasswd');
  30. //Connect to to database
  31. if ($mode == 'ro')
  32. $con = mysqli_connect($host, $username_ro, $pass_ro, $db_name);
  33. else if ($mode == 'rw')
  34. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  35. // Check connection
  36. if (mysqli_connect_errno()){
  37. error_log("Failed to connect to database: " . mysqli_connect_error());
  38. return -1;
  39. }
  40. //Set encoding options
  41. mysqli_set_charset($con, 'utf-8');
  42. header('Content-Type: text/html; charset=utf8');
  43. mysqli_query($con, 'SET NAMES utf8;');
  44. //Return the db connection
  45. return $con;
  46. }
  47. /****************************************************
  48. * This function selects the language the page will *
  49. * be displayed inf the page. Several methods are *
  50. * used: cookie detection, and browser language *
  51. * preferences. *
  52. * @return: (string): Language code. *
  53. ****************************************************/
  54. function selectLanguage(){
  55. //Try to read cookie.
  56. header('Cache-control: private');
  57. if (isSet($_COOKIE['lang'])){
  58. $lang = $_COOKIE['lang'];
  59. if ($lang == 'es' || $lang == 'en' || $lang == 'eu'){
  60. return $lang;
  61. }
  62. else{
  63. return 'es';
  64. }
  65. }
  66. //If no cookie, select from client browser preferences.
  67. else{
  68. $available_languages = array("en", "eu", "es");
  69. $langs = prefered_language($available_languages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
  70. $lang = $langs[0];
  71. if ($lang != 'es' && $lang != 'en' && $lang != 'eu'){
  72. return 'es';
  73. }
  74. else{
  75. return $langs[0]; //TODO test this
  76. }
  77. }
  78. }
  79. /****************************************************
  80. * This function parses the language prefrences of *
  81. * the client broser, comparing them to th languages *
  82. * offered by the site. *
  83. * the variable $con for futher use everywhere in *
  84. * the php code, and populates the arrays $user *
  85. * and $permission, with info about the user. *
  86. * @params: *
  87. * available_languages: (string array) Contains *
  88. * a list of strins offered *
  89. * by the site. *
  90. * http_accept_language: (string) Raw header with *
  91. * info about client *
  92. * language preferences. *
  93. * @return: (string array) List of the languages *
  94. * offered, sorted by prefference. *
  95. ****************************************************/
  96. function prefered_language(array $available_languages, $http_accept_language) {
  97. $available_languages = array_flip($available_languages);
  98. $langs;
  99. preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER);
  100. foreach($matches as $match) {
  101. list($a, $b) = explode('-', $match[1]) + array('', '');
  102. $value = isset($match[2]) ? (float) $match[2] : 1.0;
  103. if(isset($available_languages[$match[1]])) {
  104. $langs[$match[1]] = $value;
  105. continue;
  106. }
  107. if(isset($available_languages[$a])) {
  108. $langs[$a] = $value - 0.1;
  109. }
  110. }
  111. arsort($langs);
  112. return $langs;
  113. }
  114. /****************************************************
  115. * This function turns a date string into a human *
  116. * readable string, deppending o the specified *
  117. * language. *
  118. * @params: *
  119. * strdate: (string): Date string. *
  120. * lang: (string): Language code (es, en, eu). *
  121. * http_accept_language: (string) Raw header with *
  122. * info about client *
  123. * language preferences. *
  124. * time: (boolean): Append the time at the end. *
  125. * @return: (string array) List of the languages *
  126. * offered, sorted by prefference. *
  127. ****************************************************/
  128. function formatDate($strdate, $lang, $time = true){
  129. $date = strtotime($strdate);
  130. $year = date('o', $date);
  131. $month = date('n', $date);
  132. $month --;
  133. $day = date('j', $date);
  134. $wday = date('N', $date);
  135. $wday --;
  136. $hour = date('H', $date);
  137. $minute = date('i', $date);
  138. switch ($lang){
  139. case 'en':
  140. $week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
  141. $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  142. if ($time){
  143. $str = "$week[$wday], $months[$month] $day, $year at $hour:$minute";
  144. }
  145. else{
  146. $str = "$week[$wday], $months[$month] $day, $year";
  147. }
  148. break;
  149. case 'eu':
  150. $week = ['astelehena', 'asteartea', 'asteazkena', 'osteguna', 'ostirala', 'larumbata', 'igandea'];
  151. $months = ['urtarrilaren', 'otsailaren', 'martxoaren', 'apirilaren', 'maiatzaren', 'ekainaren', 'uztailaren', 'abuztuaren', 'irailaren', 'urriaren', 'azaroaren', 'abenduaren'];
  152. if ($time){
  153. $str = $year . "ko $months[$month] $day" . "an, $week[$wday], $hour:$minute";
  154. }
  155. else{
  156. $str = $year . "ko $months[$month] $day" . "an, $week[$wday]";
  157. }
  158. break;
  159. default:
  160. $week = ['Lunes', 'Martes', 'Mi&eacute;rcoles', 'Jueves', 'Viernes', 'S&aacute;bado', 'Domingo'];
  161. $months = ['enero', ' febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'];
  162. if ($time){
  163. $str = "$week[$wday] $day de $months[$month] de $year a las $hour:$minute";
  164. }
  165. else{
  166. $str = "$week[$wday] $day de $months[$month] de $year";
  167. }
  168. }
  169. return $str;
  170. }
  171. /****************************************************
  172. * This function turns a date string into a human *
  173. * readable string, deppending o the specified *
  174. * language. It is designed to be used for festival *
  175. * days only, since it doesnt return the weekday. *
  176. * @params: *
  177. * strdate: (string): Date string. *
  178. * lang: (string): Language code (es, en, eu). *
  179. * http_accept_language: (string) Raw header with *
  180. * info about client *
  181. * language preferences. *
  182. * @return: (string array) List of the languages *
  183. * offered, sorted by prefference. *
  184. ****************************************************/
  185. function formatFestivalDate($strdate, $lang){
  186. $date = strtotime($strdate);
  187. $month = date('n', $date);
  188. $month --;
  189. $day = date('j', $date);
  190. switch ($lang){
  191. case 'en':
  192. $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  193. $str = "$months[$month] $day";
  194. break;
  195. case 'eu':
  196. $months = ['urtarrilaren', 'otsailaren', 'martxoaren', 'apirilaren', 'maiatzaren', 'ekainaren', 'uztailaren', 'abuztuaren', 'irailaren', 'urriaren', 'azaroaren', 'abenduaren'];
  197. $str = "$months[$month] $day";
  198. break;
  199. default:
  200. $months = ['enero', ' febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'];
  201. $str = "$day de $months[$month]";
  202. }
  203. return $str;
  204. }
  205. /****************************************************
  206. * This function closes all the opened HTML tags in *
  207. * a given string. *
  208. * *
  209. * @params: *
  210. * html: (string): The string with HTML tags *
  211. ****************************************************/
  212. function closeTags($html) {
  213. preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  214. $openedtags = $result[1];
  215. preg_match_all('#</([a-z]+)>#iU', $html, $result);
  216. $closedtags = $result[1];
  217. $len_opened = count($openedtags);
  218. if (count($closedtags) == $len_opened) {
  219. return $html;
  220. }
  221. $openedtags = array_reverse($openedtags);
  222. for ($i=0; $i < $len_opened; $i++) {
  223. if (!in_array($openedtags[$i], $closedtags)) {
  224. $html .= '</'.$openedtags[$i].'>';
  225. } else {
  226. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  227. }
  228. }
  229. return $html;
  230. }
  231. /****************************************************
  232. * Text shortener. Given a string, it trims in the *
  233. * proximity of the desired streng, ut to the next *
  234. * white character. If indicated, it will append a *
  235. * link to the full text. *
  236. * *
  237. * @params: *
  238. * text: (string): The text to shorten. *
  239. * length: (int): The desired length. *
  240. * linktext: (string): Text fot the link. *
  241. * link: (string): URI of the full text. *
  242. ****************************************************/
  243. function cutText($text, $length, $linktext, $link){
  244. if (strlen($text) < $length){
  245. return $text;
  246. }
  247. $cut = substr($text, 0, strpos($text, " ", $length));
  248. $cut = closeTags($cut);
  249. if (strlen($cut) == 0){
  250. $cut = $text;
  251. }
  252. if (strlen($text) != strlen($cut)){
  253. $cut = $cut . "... <a href='$link'>$linktext</a>";
  254. }
  255. return $cut;
  256. }
  257. ?>