functions.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. $IMG_SIZE_PREVIEW = 600;
  3. $IMG_SIZE_MINIATURE = 200;
  4. /*****************************************************
  5. * Finds out the protocol the user is connecting to *
  6. * the site with (http or https) *
  7. * @return: (string): "http://" or "https://". *
  8. ****************************************************/
  9. function getProtocol(){
  10. if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  11. $protocol = 'https://';
  12. }
  13. else {
  14. $protocol = 'http://';
  15. }
  16. return $protocol;
  17. }
  18. /*****************************************************
  19. * Connects to the database, in 'r' or 'rw' mode. *
  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 closes all the opened HTML tags in *
  60. * a given string. *
  61. * *
  62. * @params: *
  63. * html: (string): The string with HTML tags *
  64. *****************************************************/
  65. function closeTags($html) {
  66. preg_match_all('#<(?!meta|img|br|hr|input\b)\b([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
  67. $openedtags = $result[1];
  68. preg_match_all('#</([a-z]+)>#iU', $html, $result);
  69. $closedtags = $result[1];
  70. $len_opened = count($openedtags);
  71. if (count($closedtags) == $len_opened) {
  72. return $html;
  73. }
  74. $openedtags = array_reverse($openedtags);
  75. for ($i=0; $i < $len_opened; $i++) {
  76. if (!in_array($openedtags[$i], $closedtags)) {
  77. $html .= '</'.$openedtags[$i].'>';
  78. } else {
  79. unset($closedtags[array_search($openedtags[$i], $closedtags)]);
  80. }
  81. }
  82. return $html;
  83. }
  84. /*****************************************************
  85. * Text shortener. Given a string, it trims in the *
  86. * proximity of the desired streng, ut to the next *
  87. * white character. If indicated, it will append a *
  88. * link to the full text. *
  89. * *
  90. * @params: *
  91. * text: (string): The text to shorten. *
  92. * length: (int): The desired length. *
  93. * linktext: (string): Text fot the link. *
  94. * link: (string): URI of the full text. *
  95. *****************************************************/
  96. function cutText($text, $length, $linktext, $link){
  97. if (strlen($text) < $length){
  98. return $text;
  99. }
  100. $cut = substr($text, 0, strpos($text, " ", $length));
  101. $cut = closeTags($cut);
  102. if (strlen($cut) == 0){
  103. $cut = $text;
  104. }
  105. if (strlen($text) != strlen($cut)){
  106. $cut = $cut . "... <a href='$link'>$linktext</a>";
  107. }
  108. return $cut;
  109. }
  110. /*****************************************************
  111. * Generates a URL-valid string from a regular one. *
  112. * *
  113. * @params: *
  114. * text: (string): Original string. *
  115. * @return: (string): URL-valid string. *
  116. *****************************************************/
  117. function permalink($text){
  118. $unwanted_array = array('Š'=>'S', 'š'=>'s', 'Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
  119. 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  120. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c',
  121. 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
  122. 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y' );
  123. $str = strtr( $text, $unwanted_array );
  124. $str = preg_replace('/[^\da-zA-Z ]/i', '', $str);
  125. $str = str_replace(' ', '-', $str);
  126. return $str;
  127. }
  128. /*****************************************************
  129. * Validates a user/password combo. *
  130. * *
  131. * @params: *
  132. * con: (Mysql connectrion): DB connection. *
  133. * user: (string): Username. *
  134. * pass: (string): Password. *
  135. * @return: (int): User id, or -1 if the user and *
  136. * pass didn't match. *
  137. *****************************************************/
  138. function login($con, $user, $pass){
  139. session_start();
  140. $q = mysqli_query($con,"SELECT id, salt, username AS username, sha1(salt) AS s FROM user WHERE (lower(username) = lower('$user') OR lower(email) = lower('$user')) AND password = sha1(concat('$pass', sha1(salt)));");
  141. if (mysqli_num_rows($q) == 1){
  142. $r = mysqli_fetch_array($q);
  143. $_SESSION['id'] = $r['id'];
  144. $_SESSION['salt'] = $r['s'];
  145. $_SESSION['name'] = $r['username'];
  146. return $r['id'];
  147. }
  148. else{
  149. error_log("Invalid login. username = $user, id = $_SESSION[id]");
  150. return -1;
  151. }
  152. }
  153. /*****************************************************
  154. * Finds out the IP address of the client. *
  155. * *
  156. * @return: (String): Client IP address. *
  157. *****************************************************/
  158. function getUserIP(){
  159. $client = @$_SERVER["HTTP_CLIENT_IP"];
  160. $forward = @$_SERVER["HTTP_X_FORWARDED_FOR"];
  161. $remote = $_SERVER["REMOTE_ADDR"];
  162. if(filter_var($client, FILTER_VALIDATE_IP)){
  163. $ip = $client;
  164. }
  165. elseif(filter_var($forward, FILTER_VALIDATE_IP)){
  166. $ip = $forward;
  167. }
  168. else{
  169. $ip = $remote;
  170. }
  171. return $ip;
  172. }
  173. ?>