functions.common.auth.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /* $Id: functions.common.auth.php 3053 2007-05-25 21:01:48Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * perform Authentication
  17. *
  18. * @param $username
  19. * @param $password
  20. * @param $md5password
  21. * @return int with :
  22. * 1 : user authenticated
  23. * 0 : user not authenticated
  24. */
  25. function performAuthentication($username = '', $password = '', $md5password = '') {
  26. global $cfg, $db;
  27. // check username
  28. if (!isset($username))
  29. return 0;
  30. if ($username == '')
  31. return 0;
  32. // sql-state
  33. $sql = "SELECT uid, hits, hide_offline, theme, language_file FROM tf_users WHERE state = 1 AND user_id=".$db->qstr($username)." AND password=";
  34. if ((isset($md5password)) && (strlen($md5password) == 32)) /* md5-password */
  35. $sql .= $db->qstr($md5password);
  36. elseif (isset($password)) /* plaintext-password */
  37. $sql .= $db->qstr(md5($password));
  38. else /* no password */
  39. return 0;
  40. // exec query
  41. $result = $db->Execute($sql);
  42. if ($db->ErrorNo() != 0) dbError($sql);
  43. list($uid, $hits, $cfg["hide_offline"], $cfg["theme"], $cfg["language_file"]) = $result->FetchRow();
  44. if ($result->RecordCount() == 1) { // suc. auth.
  45. // Add a hit to the user
  46. $hits++;
  47. $sql = "select * from tf_users where uid = ".$db->qstr($uid);
  48. $rs = $db->Execute($sql);
  49. if ($db->ErrorNo() != 0) dbError($sql);
  50. $rec = array(
  51. 'hits' => $hits,
  52. 'last_visit' => $db->DBDate(time()),
  53. 'theme' => $cfg['theme'],
  54. 'language_file' => $cfg['language_file']
  55. );
  56. $sql = $db->GetUpdateSQL($rs, $rec);
  57. $result = $db->Execute($sql);
  58. if ($db->ErrorNo() != 0) dbError($sql);
  59. $_SESSION['user'] = $username;
  60. $_SESSION['uid'] = $uid;
  61. $cfg["user"] = $_SESSION['user'];
  62. $cfg['uid'] = $uid;
  63. @session_write_close();
  64. return 1;
  65. } else { // wrong credentials
  66. // log
  67. AuditAction($cfg["constants"]["access_denied"], "FAILED AUTH: ".$username);
  68. // unset
  69. unset($_SESSION['user']);
  70. unset($_SESSION['uid']);
  71. unset($cfg["user"]);
  72. // flush users cookie
  73. @setcookie("autologin", "", time() - 3600);
  74. // return
  75. return 0;
  76. }
  77. // return
  78. return 0;
  79. }
  80. /**
  81. * get image-code
  82. *
  83. * @param $rstr
  84. * @param $rnd
  85. * @return string
  86. */
  87. function loginImageCode($rstr, $rnd) {
  88. return substr((hexdec(md5($_SERVER['HTTP_USER_AGENT'].$rstr.$rnd.date("F j")))), 3, 6);
  89. }
  90. /**
  91. * first Login
  92. *
  93. * @param $username
  94. * @param $password
  95. */
  96. function firstLogin($username = '', $password = '') {
  97. global $cfg, $db;
  98. if (!isset($username))
  99. return 0;
  100. if (!isset($password))
  101. return 0;
  102. if ($username == '')
  103. return 0;
  104. if ($password == '')
  105. return 0;
  106. $create_time = time();
  107. // This user is first in DB. Make them super admin.
  108. // this is The Super USER, add them to the user table
  109. $record = array(
  110. 'user_id'=>strtolower($username),
  111. 'password'=>md5($password),
  112. 'hits'=>1,
  113. 'last_visit'=>$create_time,
  114. 'time_created'=>$create_time,
  115. 'user_level'=>2,
  116. 'hide_offline'=>0,
  117. 'theme'=>$cfg["default_theme"],
  118. 'language_file'=>$cfg["default_language"],
  119. 'state'=>1
  120. );
  121. $sTable = 'tf_users';
  122. $sql = $db->GetInsertSql($sTable, $record);
  123. $result = $db->Execute($sql);
  124. if ($db->ErrorNo() != 0) dbError($sql);
  125. // Test and setup some paths for the TF settings
  126. // path
  127. $tfPath = $cfg["path"];
  128. if (!is_dir($cfg["path"]))
  129. $tfPath = getcwd() . "/downloads/";
  130. // settings
  131. $settings = array(
  132. "path" => $tfPath,
  133. "pythonCmd" => $cfg["pythonCmd"],
  134. "perlCmd" => $cfg["perlCmd"],
  135. "bin_php" => $cfg["bin_php"],
  136. "bin_grep" => $cfg["bin_grep"],
  137. "bin_awk" => $cfg["bin_awk"],
  138. "bin_du" => $cfg["bin_du"],
  139. "bin_wget" => $cfg["bin_wget"],
  140. "bin_unrar" => $cfg["bin_unrar"],
  141. "bin_unzip" => $cfg["bin_unzip"],
  142. "bin_cksfv" => $cfg["bin_cksfv"],
  143. "bin_vlc" => $cfg["bin_vlc"],
  144. "bin_uudeview" => $cfg["bin_uudeview"],
  145. "btclient_transmission_bin" => $cfg["btclient_transmission_bin"],
  146. "bin_netstat" => $cfg["bin_netstat"],
  147. "bin_sockstat" => $cfg["bin_sockstat"]
  148. );
  149. // binaries to test
  150. $binaries = array(
  151. "pythonCmd" => $cfg["pythonCmd"],
  152. "perlCmd" => $cfg["perlCmd"],
  153. "bin_php" => $cfg["bin_php"],
  154. "bin_grep" => $cfg["bin_grep"],
  155. "bin_awk" => $cfg["bin_awk"],
  156. "bin_du" => $cfg["bin_du"],
  157. "bin_wget" => $cfg["bin_wget"],
  158. "bin_unrar" => $cfg["bin_unrar"],
  159. "bin_unzip" => $cfg["bin_unzip"],
  160. "bin_cksfv" => $cfg["bin_cksfv"],
  161. "bin_vlc" => $cfg["bin_vlc"],
  162. "bin_uudeview" => $cfg["bin_uudeview"],
  163. "btclient_transmission_bin" => $cfg["btclient_transmission_bin"],
  164. "bin_netstat" => $cfg["bin_netstat"],
  165. "bin_sockstat" => $cfg["bin_sockstat"]
  166. );
  167. // bins for which
  168. $bins = array(
  169. "pythonCmd" => "python",
  170. "perlCmd" => "perl",
  171. "bin_php" => "php",
  172. "bin_grep" => "grep",
  173. "bin_awk" => "awk",
  174. "bin_du" => "du",
  175. "bin_wget" => "wget",
  176. "bin_unrar" => "unrar",
  177. "bin_unzip" => "unzip",
  178. "bin_cksfv" => "cksfv",
  179. "bin_vlc" => "vlc",
  180. "bin_uudeview" => "uudeview",
  181. "btclient_transmission_bin" => "transmissioncli",
  182. "bin_netstat" => "netstat",
  183. "bin_sockstat" => "sockstat"
  184. );
  185. // check
  186. foreach ($binaries as $key => $value) {
  187. if (!is_file($value)) {
  188. $bin = "";
  189. $bin = @trim(shell_exec("which ".$bins[$key]));
  190. if ($bin != "")
  191. $settings[$key] = $bin;
  192. }
  193. }
  194. // save
  195. saveSettings('tf_settings', $settings);
  196. AuditAction($cfg["constants"]["update"], "Initial Settings Updated for first login.");
  197. }
  198. ?>