functions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. $IMG_SIZE_PREVIEW = 600;
  3. $IMG_SIZE_MINIATURE = 200;
  4. /****************************************************
  5. * This function is called from almost everywhere at *
  6. * the beggining of the page. It initializes the *
  7. * session variables, connect to the db, enabling *
  8. * the variable $con for futher use everywhere in *
  9. * the php code, and populates the arrays $user *
  10. * and $permission, with info about the user. *
  11. * @params: *
  12. * mode: (string) Indicates required permissions *
  13. * on database. 'ro' gives read *
  14. * permissions, and 'rw' read and write *
  15. * permissions. Other values will result in *
  16. * errors. *
  17. * @return: (db connection): The connection handler. *
  18. ****************************************************/
  19. function startdb($mode = 'ro'){
  20. //Include the db configuration file. It's somehow like this
  21. /*
  22. <?php
  23. $host = 'XXXX';
  24. $db_name = 'XXXX';
  25. $username_ro = 'XXXX';
  26. $username_rw = 'XXXX';
  27. $pass_ro = 'XXXX';
  28. $pass_rw = 'XXXX';
  29. ?>
  30. */
  31. include('.htpasswd');
  32. //Connect to to database
  33. if ($mode == 'ro')
  34. $con = mysqli_connect($host, $username_ro, $pass_ro, $db_name);
  35. else if ($mode == 'rw')
  36. $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
  37. // Check connection
  38. if (mysqli_connect_errno()){
  39. error_log("Failed to connect to database: " . mysqli_connect_error());
  40. return -1;
  41. }
  42. //Set encoding options
  43. mysqli_set_charset($con, 'utf-8');
  44. header('Content-Type: text/html; charset=utf8');
  45. mysqli_query($con, 'SET NAMES utf8;');
  46. //Return the db connection
  47. return $con;
  48. }
  49. /****************************************************
  50. * This function turns a date string into a human *
  51. * readable string, deppending o the specified *
  52. * language. *
  53. * @params: *
  54. * strdate: (string): Date string. *
  55. * lang: (string): Language code (es, en, eu). *
  56. * http_accept_language: (string) Raw header with *
  57. * info about client *
  58. * language preferences. *
  59. * time: (boolean): Append the time at the end. *
  60. * @return: (string array) List of the languages *
  61. * offered, sorted by prefference. *
  62. ****************************************************/
  63. function formatDate($strdate, $lang, $time = true){
  64. $date = strtotime($strdate);
  65. $year = date('o', $date);
  66. $month = date('n', $date);
  67. $month --;
  68. $day = date('j', $date);
  69. $wday = date('N', $date);
  70. $wday --;
  71. $hour = date('H', $date);
  72. $minute = date('i', $date);
  73. switch ($lang){
  74. case 'en':
  75. $week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
  76. $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  77. if ($time){
  78. $str = "$week[$wday], $months[$month] $day, $year at $hour:$minute";
  79. }
  80. else{
  81. $str = "$week[$wday], $months[$month] $day, $year";
  82. }
  83. break;
  84. case 'eu':
  85. $week = ['astelehena', 'asteartea', 'asteazkena', 'osteguna', 'ostirala', 'larumbata', 'igandea'];
  86. $months = ['urtarrilaren', 'otsailaren', 'martxoaren', 'apirilaren', 'maiatzaren', 'ekainaren', 'uztailaren', 'abuztuaren', 'irailaren', 'urriaren', 'azaroaren', 'abenduaren'];
  87. if ($time){
  88. $str = $year . "ko $months[$month] $day" . "an, $week[$wday], $hour:$minute";
  89. }
  90. else{
  91. $str = $year . "ko $months[$month] $day" . "an, $week[$wday]";
  92. }
  93. break;
  94. default:
  95. $week = ['Lunes', 'Martes', 'Mi&eacute;rcoles', 'Jueves', 'Viernes', 'S&aacute;bado', 'Domingo'];
  96. $months = ['enero', ' febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'];
  97. if ($time){
  98. $str = "$week[$wday] $day de $months[$month] de $year a las $hour:$minute";
  99. }
  100. else{
  101. $str = "$week[$wday] $day de $months[$month] de $year";
  102. }
  103. }
  104. return $str;
  105. }
  106. /****************************************************
  107. * This function turns a date string into a human *
  108. * readable string, deppending o the specified *
  109. * language. It is designed to be used for festival *
  110. * days only, since it doesnt return the weekday. *
  111. * @params: *
  112. * strdate: (string): Date string. *
  113. * lang: (string): Language code (es, en, eu). *
  114. * http_accept_language: (string) Raw header with *
  115. * info about client *
  116. * language preferences. *
  117. * @return: (string array) List of the languages *
  118. * offered, sorted by prefference. *
  119. ****************************************************/
  120. function formatFestivalDate($strdate, $lang){
  121. $date = strtotime($strdate);
  122. $month = date('n', $date);
  123. $month --;
  124. $day = date('j', $date);
  125. switch ($lang){
  126. case 'en':
  127. $months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
  128. $str = "$months[$month] $day";
  129. break;
  130. case 'eu':
  131. $months = ['urtarrilaren', 'otsailaren', 'martxoaren', 'apirilaren', 'maiatzaren', 'ekainaren', 'uztailaren', 'abuztuaren', 'irailaren', 'urriaren', 'azaroaren', 'abenduaren'];
  132. $str = "$months[$month] $day";
  133. break;
  134. default:
  135. $months = ['enero', ' febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'];
  136. $str = "$day de $months[$month]";
  137. }
  138. return $str;
  139. }
  140. /****************************************************
  141. * This function closes all the opened HTML tags in *
  142. * a given string. *
  143. * *
  144. * @params: *
  145. * html: (string): The string with HTML tags *
  146. ****************************************************/
  147. function closeTags($html) {
  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 *
  168. * proximity of the desired streng, ut to the next *
  169. * white character. If indicated, it will append a *
  170. * link to the full text. *
  171. * *
  172. * @params: *
  173. * text: (string): The text to shorten. *
  174. * length: (int): The desired length. *
  175. * linktext: (string): Text fot the link. *
  176. * link: (string): URI of the full text. *
  177. ****************************************************/
  178. function cutText($text, $length, $linktext, $link){
  179. if (strlen($text) < $length){
  180. return $text;
  181. }
  182. $cut = substr($text, 0, strpos($text, " ", $length));
  183. $cut = closeTags($cut);
  184. if (strlen($cut) == 0){
  185. $cut = $text;
  186. }
  187. if (strlen($text) != strlen($cut)){
  188. $cut = $cut . "... <a href='$link'>$linktext</a>";
  189. }
  190. return $cut;
  191. }
  192. /****************************************************
  193. * Generates a URL-valid string from a regular one. *
  194. * *
  195. * @params: *
  196. * text: (string): Original string. *
  197. * @return: (string): URL-valid string. *
  198. ****************************************************/
  199. function permalink($text){
  200. $unwanted_array = array('Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  201. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  202. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  203. 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
  204. 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
  205. $str = strtr( $text, $unwanted_array );
  206. $str = preg_replace('/[^\da-zA-Z ]/i', '', $str);
  207. $str = str_replace(' ', '-', $str);
  208. return $str;
  209. }
  210. function login($con, $user, $pass){
  211. session_start();
  212. $q = mysqli_query($con,"SELECT id, md5(salt) AS s, username FROM user WHERE (lower(username) = lower('$user') OR lower(email) = lower('$user')) AND password = md5('$pass');");
  213. if (mysqli_num_rows($q) == 1){
  214. $r = mysqli_fetch_array($q);
  215. $_SESSION['id'] = $r['id'];
  216. $_SESSION['salt'] = $r['s'];
  217. $_SESSION['name'] = $r['username'];
  218. return true;
  219. }
  220. else{
  221. error_log("Invalid login. username = $_SESSION[name], id = $_SESSION[id]");
  222. return false;
  223. }
  224. }
  225. function checkSession($con){
  226. session_start(['cookie_lifetime' => 1800,]);
  227. $qr = mysqli_query($con, "SELECT id FROM user WHERE id = '$_SESSION[id]' AND md5(salt) = '$_SESSION[salt]';");
  228. //error_log("SELECT id FROM user WHERE id = '$_SESSION[id]' AND md5(salt) = '$_SESSION[salt]';");
  229. if (mysqli_num_rows($qr) == 1)
  230. return true;
  231. else{
  232. return false;
  233. error_log("Invalid session. username = $_SESSION[name], id = $_SESSION[id]");
  234. }
  235. }
  236. /****************************************************
  237. * Increases version value in table settings by one. *
  238. * Helpfull for the app to know when to perform a *
  239. * full sync. Must be called after every INSERT, *
  240. * UPDATE or DELETE query to the database. *
  241. ****************************************************/
  242. function version(){
  243. $con = startdb('rw');
  244. mysqli_query($con, 'UPDATE settings SET value = value + 1 WHERE name = "version";');
  245. }
  246. ?>