PageRenderTime 46ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/portal/registro/includes/functions.php

https://bitbucket.org/asada03/camino
PHP | 266 lines | 130 code | 44 blank | 92 comment | 25 complexity | 686fdb653e46152af689552f1fbd7e69 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. This is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This software is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this software; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. or visit www.gnu.org
  15. Code by Lutsen Stellingwerff (www.biepenlu.nl)
  16. Based on code from evolt.org by jpmaster77
  17. http://evolt.org/php_login_script_with_remember_me_feature
  18. */
  19. if ('functions.php' == basename($_SERVER['SCRIPT_FILENAME']))
  20. exit("You cannot access this file directly");
  21. // Checks whether or not the given username is in the database,
  22. // if so it checks if the given password is the same password in the database for that user.
  23. // If the user doesn't exist or if the passwords don't match up, it returns an error code (1 or 2).
  24. // On success it returns 0.
  25. function confirmUser($username, $password){
  26. global $conn;
  27. /* Add slashes if necessary (for query) */
  28. if(!get_magic_quotes_gpc()) {
  29. $username = addslashes($username);
  30. }
  31. /* Verify that user is in database */
  32. $q = "select password from ".DB_PREFIX."users where username = '$username' limit 1";
  33. $result = mysql_query($q,$conn);
  34. if(!$result || (mysql_numrows($result) < 1)){
  35. return 1; // Indicates username failure
  36. }
  37. /* Retrieve password from result, strip slashes */
  38. $dbarray = mysql_fetch_array($result);
  39. // combine password in database with key
  40. $dbarray['password'] = hmac($_SESSION['key'], stripslashes($dbarray['password']));
  41. $password = stripslashes($password);
  42. /* Validate that password is correct */
  43. if($password == $dbarray['password']){
  44. return 0; // Success! Username and password confirmed
  45. }
  46. else{
  47. return 2; // Indicates password failure
  48. }
  49. }
  50. // prevent including php or html in a string
  51. function cleanString($string, $length) {
  52. $string = filter_var($string, FILTER_SANITIZE_STRING); // PHP 5
  53. $string = trim($string);
  54. $string = stripslashes($string);
  55. $string = strip_tags($string);
  56. $string = substr($string, 0, $length);
  57. return $string;
  58. }
  59. // Calculate HMAC according to RFC2104
  60. // http://www.ietf.org/rfc/rfc2104.txt
  61. // From php.net by mina86 at tlen dot pl 19-Sep-2005 10:41
  62. function hmac($key, $data, $hash = 'sha1', $blocksize = 64) {
  63. if (strlen($key)>$blocksize) {
  64. $key = pack('H*', $hash($key));
  65. }
  66. $key = str_pad($key, $blocksize, chr(0));
  67. $ipad = str_repeat(chr(0x36), $blocksize);
  68. $opad = str_repeat(chr(0x5c), $blocksize);
  69. return $hash(($key^$opad) . pack('H*', $hash(($key^$ipad) . $data)));
  70. }
  71. function displayAlert($alertArr) {
  72. if (count($alertArr) > 0) {
  73. $string = '';
  74. foreach ($alertArr as $val) {
  75. $string .= "*".$val."<br />\n";
  76. }
  77. return $string;
  78. } else {
  79. return false;
  80. }
  81. }
  82. // email validation
  83. // (original: Chris Williams, cwilliams@aerospace.state.al.us, www.php.net)
  84. /*function emailValid($email) {
  85. // Do the basic Reg Exp Matching for simple validation
  86. if (eregi("^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email)) {
  87. // split the Email Up for Server validation
  88. list($Username, $Domain) = split("@",$email);
  89. // If you get an mx record then this is a valid email domain
  90. if(@getmxrr($Domain, $MXHost)) {
  91. return TRUE;
  92. } else {
  93. // else use the domain given to try and connect on port 25
  94. // if you can connect then it's a valid domain and that's good enough
  95. if(@fsockopen($Domain, 25, $errno, $errstr, 30)) {
  96. return TRUE;
  97. } else {
  98. return FALSE;
  99. }
  100. }
  101. } else {
  102. return FALSE;
  103. }
  104. }*/
  105. // Update: use PHP 5 filter function instead
  106. function emailValid($email) {
  107. return filter_var($email, FILTER_VALIDATE_EMAIL);
  108. }
  109. /*
  110. Author: Peter Mugane Kionga-Kamau
  111. http://www.pmkmedia.com
  112. Description: string str_makerand(int $minlength, int $maxlength, bool $useupper, bool $usespecial, bool $usenumbers)
  113. returns a randomly generated string of length between $minlength and $maxlength inclusively.
  114. Notes:
  115. - If $useupper is true uppercase characters will be used; if false they will be excluded.
  116. - If $usespecial is true special characters will be used; if false they will be excluded.
  117. - If $usenumbers is true numerical characters will be used; if false they will be excluded.
  118. - If $minlength is equal to $maxlength a string of length $maxlength will be returned.
  119. - Not all special characters are included since they could cause parse errors with queries.
  120. Modify at will.
  121. (original function name: str_makerand)
  122. */
  123. function randString($minlength = 6, $maxlength = 30, $useupper = true, $usespecial = false, $usenumbers = true)
  124. {
  125. $charset = "abcdefghijklmnopqrstuvwxyz";
  126. if ($useupper) $charset .= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  127. if ($usenumbers) $charset .= "0123456789";
  128. if ($usespecial) $charset .= "~@#%^*()_+-=|]["; // Note: using all special characters this reads: "~!@#$%^&*()_+`-={}|\\]?[\":;'><,./";
  129. if ($minlength > $maxlength) $length = mt_rand ($maxlength, $minlength);
  130. else $length = mt_rand ($minlength, $maxlength);
  131. for ($i=0; $i<$length; $i++) $key .= $charset[(mt_rand(0,(strlen($charset)-1)))];
  132. return $key;
  133. }
  134. // Returns corresponding username if the email address exists, false otherwise.
  135. function emailExist($email){
  136. global $conn;
  137. if(!get_magic_quotes_gpc()){
  138. $email = addslashes($email);
  139. }
  140. $q = "select username from ".DB_PREFIX."users where email = '$email' limit 1";
  141. $result = mysql_query($q,$conn);
  142. if (mysql_numrows($result) > 0) {
  143. $dbarray = mysql_fetch_array($result);
  144. return $dbarray['username'];
  145. } else {
  146. return false;
  147. }
  148. }
  149. // Returns true if the username has been taken by another user, false otherwise.
  150. function usernameTaken($username){
  151. global $conn;
  152. if(!get_magic_quotes_gpc()){
  153. $username = addslashes($username);
  154. }
  155. $q = "select username from ".DB_PREFIX."users where username = '$username' limit 1";
  156. $result = mysql_query($q,$conn);
  157. return (mysql_numrows($result) > 0);
  158. }
  159. // put already submitted data back in form
  160. function printField($fieldname) {
  161. if ($_POST[$fieldname] && strlen($_POST[$fieldname]) > 0) {
  162. print $_POST[$fieldname];
  163. }
  164. }
  165. // create directory for a personalized prospecting webpage
  166. function createDirectory($username)
  167. {
  168. // Let's create the directory and files
  169. // Create directory
  170. $myFileName = "../../" . $username;
  171. if (!mkdir($myFileName,0755))
  172. return "No se pudo crear el directorio ".$username;
  173. // Create index.html
  174. $myFileName = "../../" . $username . "/index.html";
  175. $myFile = fopen($myFileName,"w");
  176. if (!$myFile) return "No se pudeo crear el archivo index.html";
  177. fwrite ($myFile,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
  178. fwrite ($myFile,"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
  179. fwrite ($myFile,"<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n");
  180. fwrite ($myFile,"<title>Untitled Document</title>\n");
  181. $myString = "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://www.caminoaserlibre.com/index.php?userunet=" . $username . "\">\n";
  182. fwrite ($myFile,$myString);
  183. fwrite ($myFile,"</head><body></body></html>\n");
  184. fclose($myFile);
  185. // Create todalainformacion.html
  186. $myFileName = "../../" . $username . "/todalainformacion.html";
  187. $myFile = fopen($myFileName,"w");
  188. if (!$myFile) return "No se pudeo crear el archivo todalainformacion.html";
  189. fwrite ($myFile,"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
  190. fwrite ($myFile,"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n");
  191. fwrite ($myFile,"<head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\" />\n");
  192. fwrite ($myFile,"<title>Untitled Document</title>\n");
  193. $myString = "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://www.caminoaserlibre.com/html/todalainformacion.php?userunet=" . $username . "\">\n";
  194. fwrite ($myFile,$myString);
  195. fwrite ($myFile,"</head><body></body></html>\n");
  196. fclose($myFile);
  197. return "success";
  198. }
  199. function dateDiff($startDate, $endDate)
  200. {
  201. // Parse dates for conversion
  202. $startArry = date_parse($startDate);
  203. $endArry = date_parse($endDate);
  204. // Convert dates to Julian Days
  205. $start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);
  206. $end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);
  207. // Return difference
  208. return round(($end_date - $start_date), 0);
  209. }
  210. // NOTHING after ?>