PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/virtuoso-opensource-6.1.5/binsrc/tutorial/hosting/ho_s_30/WebCalendar/includes/user.php

#
PHP | 386 lines | 248 code | 46 blank | 92 comment | 60 complexity | 449e330838e0f675949b762c47fe2b9f MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. <?php
  2. #
  3. # This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
  4. # project.
  5. #
  6. # Copyright (C) 1998-2012 OpenLink Software
  7. #
  8. # This project is free software; you can redistribute it and/or modify it
  9. # under the terms of the GNU General Public License as published by the
  10. # Free Software Foundation; only version 2 of the License, dated June 1991.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. #
  21. #
  22. ?>
  23. <?php
  24. // This file contains all the functions for getting information
  25. // about users. So, if you want to use an authentication scheme
  26. // other than the webcal_user table, you can just create a new
  27. // version of each function found below.
  28. //
  29. // Note: this application assumes that usernames (logins) are unique.
  30. //
  31. // Note #2: If you are using HTTP-based authentication, then you still
  32. // need these functions and you will still need to add users to
  33. // webcal_user.
  34. // Set some global config variables about your system.
  35. $user_can_update_password = true;
  36. $admin_can_add_user = true;
  37. $admin_can_delete_user = true;
  38. // Check to see if a given login/password is valid. If invalid,
  39. // the error message will be placed in $login_error.
  40. // params:
  41. // $login - user login
  42. // $password - user password
  43. // returns: true or false
  44. function user_valid_login ( $login, $password ) {
  45. global $error;
  46. $ret = false;
  47. $login_error = "";
  48. $sql = "SELECT cal_login FROM webcal_user WHERE " .
  49. "cal_login = '" . $login . "' AND cal_passwd = '" . $password . "'";
  50. $res = dbi_query ( $sql );
  51. if ( $res ) {
  52. $row = dbi_fetch_row ( $res );
  53. if ( $row && $row[0] != "" ) {
  54. // MySQL seems to do case insensitive matching, so double-check
  55. // the login.
  56. if ( $row[0] == $login )
  57. $ret = true; // found login/password
  58. else
  59. $error = translate ("Invalid login");
  60. } else {
  61. $error = translate ("Invalid login");
  62. }
  63. dbi_free_result ( $res );
  64. } else {
  65. $error = translate("Database error") . ": " . dbi_error();
  66. }
  67. return $ret;
  68. }
  69. // Check to see if a given login/crypted password is valid. If invalid,
  70. // the error message will be placed in $login_error.
  71. // params:
  72. // $login - user login
  73. // $crypt_password - crypted user password
  74. // returns: true or false
  75. function user_valid_crypt ( $login, $crypt_password ) {
  76. global $error;
  77. $ret = false;
  78. $login_error = "";
  79. $salt = substr($crypt_password, 0, 2);
  80. $sql = "SELECT cal_login, cal_passwd FROM webcal_user WHERE " .
  81. "cal_login = '" . $login . "'";
  82. $res = dbi_query ( $sql );
  83. if ( $res ) {
  84. $row = dbi_fetch_row ( $res );
  85. if ( $row && $row[0] != "" ) {
  86. // MySQL seems to do case insensitive matching, so double-check
  87. // the login.
  88. // also check if password matches
  89. if ( ($row[0] == $login) && (crypt($row[1], $salt) == $crypt_password) )
  90. $ret = true; // found login/password
  91. else
  92. //$error = translate ("Invalid login");
  93. $error = "Invalid login";
  94. } else {
  95. //$error = translate ("Invalid login");
  96. $error = "Invalid login";
  97. }
  98. dbi_free_result ( $res );
  99. } else {
  100. //$error = translate("Database error") . ": " . dbi_error();
  101. $error = "Database error : " . dbi_error();
  102. }
  103. return $ret;
  104. }
  105. // Load info about a user (first name, last name, admin) and set
  106. // globally.
  107. // params:
  108. // $user - user login
  109. // $prefix - variable prefix to use
  110. function user_load_variables ( $login, $prefix ) {
  111. global $PUBLIC_ACCESS_FULLNAME;
  112. if ( $login == "__public__" ) {
  113. $GLOBALS[$prefix . "login"] = $login;
  114. $GLOBALS[$prefix . "firstname"] = "";
  115. $GLOBALS[$prefix . "lastname"] = "";
  116. $GLOBALS[$prefix . "is_admin"] = "N";
  117. $GLOBALS[$prefix . "email"] = "";
  118. $GLOBALS[$prefix . "fullname"] = $PUBLIC_ACCESS_FULLNAME;
  119. $GLOBALS[$prefix . "password"] = "";
  120. return true;
  121. }
  122. $sql =
  123. "SELECT cal_firstname, cal_lastname, cal_is_admin, cal_email, cal_passwd " .
  124. "FROM webcal_user WHERE cal_login = '" . $login . "'";
  125. $res = dbi_query ( $sql );
  126. if ( $res ) {
  127. if ( $row = dbi_fetch_row ( $res ) ) {
  128. $GLOBALS[$prefix . "login"] = $login;
  129. $GLOBALS[$prefix . "firstname"] = $row[0];
  130. $GLOBALS[$prefix . "lastname"] = $row[1];
  131. $GLOBALS[$prefix . "is_admin"] = $row[2];
  132. $GLOBALS[$prefix . "email"] = empty ( $row[3] ) ? "" : $row[3];
  133. if ( strlen ( $row[0] ) && strlen ( $row[1] ) )
  134. $GLOBALS[$prefix . "fullname"] = "$row[0] $row[1]";
  135. else
  136. $GLOBALS[$prefix . "fullname"] = $login;
  137. $GLOBALS[$prefix . "password"] = $row[4];
  138. }
  139. dbi_free_result ( $res );
  140. } else {
  141. $error = translate ("Database error") . ": " . dbi_error ();
  142. return false;
  143. }
  144. return true;
  145. }
  146. // Add a new user.
  147. // params:
  148. // $user - user login
  149. // $password - user password
  150. // $firstname - first name
  151. // $lastname - last name
  152. // $email - email address
  153. // $admin - is admin? ("Y" or "N")
  154. function user_add_user ( $user, $password, $firstname, $lastname, $email,
  155. $admin ) {
  156. global $error;
  157. if ( $user == "__public__" ) {
  158. $error = translate ("Invalid user login");
  159. return false;
  160. }
  161. if ( strlen ( $email ) )
  162. $uemail = "'" . $email . "'";
  163. else
  164. $uemail = "NULL";
  165. if ( strlen ( $firstname ) )
  166. $ufirstname = "'" . $firstname . "'";
  167. else
  168. $ufirstname = "NULL";
  169. if ( strlen ( $lastname ) )
  170. $ulastname = "'" . $lastname . "'";
  171. else
  172. $ulastname = "NULL";
  173. if ( strlen ( $password ) )
  174. $upassword = "'" . $password . "'";
  175. else
  176. $upassword = "NULL";
  177. if ( $admin != "Y" )
  178. $admin = "N";
  179. $sql = "INSERT INTO webcal_user " .
  180. "( cal_login, cal_lastname, cal_firstname, " .
  181. "cal_is_admin, cal_passwd, cal_email ) " .
  182. "VALUES ( '$user', $ulastname, $ufirstname, " .
  183. "'$admin', $upassword, $uemail )";
  184. if ( ! dbi_query ( $sql ) ) {
  185. $error = translate ("Database error") . ": " . dbi_error ();
  186. return false;
  187. }
  188. return true;
  189. }
  190. // Update a user
  191. // params:
  192. // $user - user login
  193. // $firstname - first name
  194. // $lastname - last name
  195. // $email - email address
  196. // $admin - is admin?
  197. function user_update_user ( $user, $firstname, $lastname, $email, $admin ) {
  198. global $error;
  199. if ( $user == "__public__" ) {
  200. $error = translate ("Invalid user login");
  201. return false;
  202. }
  203. if ( strlen ( $email ) )
  204. $uemail = "'" . $email . "'";
  205. else
  206. $uemail = "NULL";
  207. if ( strlen ( $firstname ) )
  208. $ufirstname = "'" . $firstname . "'";
  209. else
  210. $ufirstname = "NULL";
  211. if ( strlen ( $lastname ) )
  212. $ulastname = "'" . $lastname . "'";
  213. else
  214. $ulastname = "NULL";
  215. if ( $admin != "Y" )
  216. $admin = "N";
  217. $sql = "UPDATE webcal_user SET cal_lastname = $ulastname, " .
  218. "cal_firstname = $ufirstname, cal_email = $uemail," .
  219. "cal_is_admin = '$admin' WHERE cal_login = '$user'";
  220. if ( ! dbi_query ( $sql ) ) {
  221. $error = translate ("Database error") . ": " . dbi_error ();
  222. return false;
  223. }
  224. return true;
  225. }
  226. // Update user password
  227. // params:
  228. // $user - user login
  229. // $password - last name
  230. function user_update_user_password ( $user, $password ) {
  231. global $error;
  232. $sql = "UPDATE webcal_user SET cal_passwd = '$password' " .
  233. "WHERE cal_login = '$user'";
  234. if ( ! dbi_query ( $sql ) ) {
  235. $error = translate ("Database error") . ": " . dbi_error ();
  236. return false;
  237. }
  238. return true;
  239. }
  240. // Delete a user from the system.
  241. // We assume that we've already checked to make sure this user doesn't
  242. // have events still in the database.
  243. // params:
  244. // $user - user to delete
  245. function user_delete_user ( $user ) {
  246. // Get event ids for all events this user is a participant
  247. $events = array ();
  248. $res = dbi_query ( "SELECT webcal_entry.cal_id " .
  249. "FROM webcal_entry, webcal_entry_user " .
  250. "WHERE webcal_entry.cal_id = webcal_entry_user.cal_id " .
  251. "AND webcal_entry_user.cal_login = '$user'" );
  252. if ( $res ) {
  253. while ( $row = dbi_fetch_row ( $res ) ) {
  254. $events[] = $row[0];
  255. }
  256. }
  257. // Now count number of participants in each event...
  258. // If just 1, then save id to be deleted
  259. $delete_em = array ();
  260. for ( $i = 0; $i < count ( $events ); $i++ ) {
  261. $res = dbi_query ( "SELECT COUNT(*) FROM webcal_entry_user " .
  262. "WHERE cal_id = " . $events[$i] );
  263. if ( $res ) {
  264. if ( $row = dbi_fetch_row ( $res ) ) {
  265. if ( $row[0] == 1 )
  266. $delete_em[] = $events[$i];
  267. }
  268. dbi_free_result ( $res );
  269. }
  270. }
  271. // Now delete events that were just for this user
  272. for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
  273. dbi_query ( "DELETE FROM webcal_entry WHERE cal_id = " . $delete_em[$i] );
  274. }
  275. // Delete user participation from events
  276. dbi_query ( "DELETE FROM webcal_entry_user WHERE cal_login = '$user'" );
  277. // Delete preferences
  278. dbi_query ( "DELETE FROM webcal_user_pref WHERE cal_login = '$user'" );
  279. // Delete from groups
  280. dbi_query ( "DELETE FROM webcal_group_user WHERE cal_login = '$user'" );
  281. // Delete user's views
  282. $delete_em = array ();
  283. $res = dbi_query ( "SELECT cal_view_id FROM webcal_view " .
  284. "WHERE cal_owner = '$user'" );
  285. if ( $res ) {
  286. while ( $row = dbi_fetch_row ( $res ) ) {
  287. $delete_em[] = $row[0];
  288. }
  289. dbi_free_result ( $res );
  290. }
  291. for ( $i = 0; $i < count ( $delete_em ); $i++ ) {
  292. dbi_query ( "DELETE FROM webcal_view_user WHERE cal_view_id = " .
  293. $delete_em[$i] );
  294. }
  295. dbi_query ( "DELETE FROM webcal_view WHERE cal_owner = '$user'" );
  296. // Delete layers
  297. dbi_query ( "DELETE FROM webcal_user_layers WHERE cal_login = '$user'" );
  298. // Delete any layers other users may have that point to this user.
  299. dbi_query ( "DELETE FROM webcal_user_layers WHERE cal_layeruser = '$user'" );
  300. // Delete user
  301. dbi_query ( "DELETE FROM webcal_user WHERE cal_login = '$user'" );
  302. }
  303. // Get a list of users and return info in an array.
  304. function user_get_users () {
  305. global $public_access, $PUBLIC_ACCESS_FULLNAME;
  306. $count = 0;
  307. $ret = array ();
  308. if ( $public_access == "Y" )
  309. $ret[$count++] = array (
  310. "cal_login" => "__public__",
  311. "cal_lastname" => "",
  312. "cal_firstname" => "",
  313. "cal_is_admin" => "N",
  314. "cal_email" => "",
  315. "cal_password" => "",
  316. "cal_fullname" => $PUBLIC_ACCESS_FULLNAME );
  317. $res = dbi_query ( "SELECT cal_login, cal_lastname, cal_firstname, " .
  318. "cal_is_admin, cal_email, cal_passwd FROM webcal_user " .
  319. "ORDER BY cal_lastname, cal_firstname, cal_login" );
  320. if ( $res ) {
  321. while ( $row = dbi_fetch_row ( $res ) ) {
  322. if ( strlen ( $row[1] ) && strlen ( $row[2] ) )
  323. $fullname = "$row[2] $row[1]";
  324. else
  325. $fullname = $row[0];
  326. $ret[$count++] = array (
  327. "cal_login" => $row[0],
  328. "cal_lastname" => $row[1],
  329. "cal_firstname" => $row[2],
  330. "cal_is_admin" => $row[3],
  331. "cal_email" => empty ( $row[4] ) ? "" : $row[4],
  332. "cal_password" => $row[5],
  333. "cal_fullname" => $fullname
  334. );
  335. }
  336. dbi_free_result ( $res );
  337. }
  338. return $ret;
  339. }
  340. ?>