functions.php 12 KB

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