Source for file functions_security.php

Documentation is available at functions_security.php

  1. <?php
  2.  
  3. function people_get_login_content() {
  4.  
  5. //outputs the code for the log-in page
  6.  
  7. $returnString;
  8.  
  9.  
  10. if( $_GET['object'] ) {
  11.  
  12. $returnString .= people_get_change_password_content();
  13.  
  14. } else {
  15.  
  16. $returnString .= '
  17.  
  18. <table width="290px" cellpadding="4" bgcolor="#FFFFFF">
  19.  
  20. <form id="loginForm" method="post">
  21.  
  22. <tr>
  23.  
  24. <td bgcolor="#eeeeee" colspan="2" style="text-align:right;">&nbsp;</td>
  25.  
  26. </tr>
  27.  
  28. <tr>
  29.  
  30. <td bgcolor="#eeeeee">username</td>
  31.  
  32. <td>'.form_input( 'username', NULL, array(), $GLOBALS['errorArray'] ).'</td>
  33.  
  34.  
  35.  
  36. </tr>
  37.  
  38. <tr>
  39.  
  40. <td bgcolor="#eeeeee">password</td>
  41.  
  42. <td>'.form_input( 'password', NULL, true ).'</td>
  43.  
  44. </tr>
  45.  
  46. <tr>
  47.  
  48. <td bgcolor="#eeeeee" colspan="2" style="text-align:right;"><input type="button" class="submitLink" value="log in" '.mouseover().' onclick="this.form.submit();"></td>
  49.  
  50. </tr>
  51.  
  52. </form>
  53.  
  54. </table>
  55.  
  56. ';
  57.  
  58. }
  59.  
  60. return $returnString;
  61.  
  62. }
  63.  
  64.  
  65.  
  66. function people_get_change_password_content() {
  67.  
  68. //outputs the code to change a user's password
  69.  
  70. $returnString;
  71.  
  72. if( $person = get_person( $_GET['object'] ) ) {
  73.  
  74. if( $person['password'] ) {
  75.  
  76. $returnString .= '
  77.  
  78. <form action="../system_beta_1/system_management.php?p=people&object='.$_GET['object'].'" id="loginForm" method="post">
  79.  
  80. <input type="hidden" name="action" value="change_password">
  81.  
  82. old password: <input type="password" name="old_password" /><br />
  83.  
  84. <p>new password: <input type="password" name="new_password" /><br /><br />
  85.  
  86. verify new password: <input type="password" name="new_password_confirm" onKeyPress="return submitenter(this,event)"><br /><br />
  87.  
  88. <input type="submit" name="userLogin" value="update" />
  89.  
  90. </form>
  91.  
  92. ';
  93.  
  94. }
  95.  
  96. } else {
  97.  
  98. $returnString .= people_get_login_content();
  99.  
  100. }
  101.  
  102. return $returnString;
  103.  
  104. }
  105.  
  106.  
  107.  
  108. function people_log_in( $username, $password ) {
  109.  
  110. //attempts to log the person in and returns the default start page if successful (nothing if not)
  111.  
  112. global $db;
  113.  
  114.  
  115.  
  116. if(strlen($username) > 0) {
  117.  
  118. if(strlen(trim($password) ) > 0) {
  119.  
  120. $safeUsername = addslashes( $username );
  121.  
  122. $safePassword = sha1( $password );
  123.  
  124.  
  125. $type =& $GLOBALS['core']->get_object_type( 'people' );
  126.  
  127.  
  128. if( !$users = $type->get_byValue( array( array( 'attribute'=>'username', 'operator'=>'=', 'value'=>$safeUsername ), array( 'attribute'=>'password', 'operator'=>'=', 'value'=>$safePassword ) ) ) ) {
  129.  
  130. $users = $type->get_byValue( array( array( 'attribute'=>'email', 'operator'=>'=', 'value'=>$safeUsername ), array( 'attribute'=>'password', 'operator'=>'=', 'value'=>$safePassword ) ) );
  131.  
  132. }
  133.  
  134.  
  135.  
  136. if( count($users) == 1 ) {
  137.  
  138. $user = reset($users);
  139.  
  140. if( ( $user->get_attribute('username') == $safeUsername || $user->get_attribute('email') == $safeUsername ) && $user->get_attribute('password') === $safePassword ) {
  141.  
  142. $GLOBALS['user'] = reset($users);
  143.  
  144. $user =& $GLOBALS['user'];
  145.  
  146.  
  147. if( isset( $GLOBALS['user'] ) ) {
  148.  
  149. people_log_out();
  150.  
  151. }
  152.  
  153.  
  154. session_start();
  155.  
  156. session_regenerate_id();
  157.  
  158.  
  159. //setting a session property to be a reference may be a bad idea... we'll have to see
  160.  
  161. $_SESSION["user"] = $user->id;
  162.  
  163. $_SESSION["IP"] = $_SERVER["REMOTE_ADDR"];
  164.  
  165. $_SESSION["timestamp"] = time();
  166.  
  167.  
  168. $user->set( array( 'sessionID'=>session_id(), 'sessionIP'=>$_SESSION['IP'], 'sessionTimestamp'=>$_SESSION['timestamp'] ) );
  169.  
  170.  
  171. } else {
  172.  
  173. return array( "incorrect username or password" );
  174.  
  175. }
  176.  
  177. } else {
  178.  
  179. return array( "incorrect username or password" );
  180.  
  181. }
  182.  
  183. } else {
  184.  
  185. return array( 'password' => "password was empty" );
  186.  
  187. }
  188.  
  189. } else {
  190.  
  191. return array( 'username' => "username was empty" );
  192.  
  193. }
  194.  
  195. }
  196.  
  197.  
  198.  
  199. function people_log_out() {
  200.  
  201. //logs out the current user
  202.  
  203. global $db;
  204.  
  205. $_SESSION = array();
  206.  
  207. session_destroy();
  208.  
  209. unset($_COOKIE[session_name()]);
  210.  
  211.  
  212. if( isset( $GLOBALS['user'] ) ) {
  213.  
  214. $GLOBALS['user']->set( array( 'sessionID'=>'', 'sessionIP'=>'', 'sessionTimestamp'=>'' ) );
  215.  
  216.  
  217. unset( $GLOBALS['user'] );
  218.  
  219. }
  220.  
  221. header( "Location: http://".$_SERVER['HTTP_HOST']."/pi/Modules/people/login.php" );
  222.  
  223. }
  224.  
  225.  
  226.  
  227. function allowed( $permissionName, $objectArray = NULL ) {
  228.  
  229. if( !isset( $GLOBALS['core'] ) ) { $GLOBALS['core'] = new core; }
  230.  
  231. if( !isset($GLOBALS['permissions']) ) { $GLOBALS['permissions'] = array(); }
  232.  
  233. if( !isset( $GLOBALS['user'] ) && $user =& get_object( $_SESSION['user'] ) ) {
  234.  
  235. $GLOBALS['user'] =& $user;
  236.  
  237. }
  238.  
  239.  
  240. if( array_key_exists( $permissionName, $GLOBALS['permissions'] ) ) { return $GLOBALS['permissions'][ $permissionName ]; }
  241.  
  242.  
  243.  
  244. $permissionType =& $GLOBALS['core']->get_object_type( 'permissions' );
  245.  
  246.  
  247.  
  248. if( $permissionName ) {
  249.  
  250. if( !$permission = $permissionType->get_byValue( array( array( 'attribute'=>'name','operator'=>'=','value'=>$permissionName ) ) ) ) {
  251.  
  252. //echo print_array($permission);
  253.  
  254. $permission = new permission;
  255.  
  256. $permission->set( array( 'name'=>$permissionName ) );
  257.  
  258. } else { $permission = reset($permission); }
  259.  
  260. } else { return true; }
  261.  
  262.  
  263.  
  264. //if the permission is open, return true
  265.  
  266. if( !count( $permission->constraintArray ) ) {
  267.  
  268. $GLOBALS['permissions'][ $permissionName ] = ($objectArray ? $objectArray : true);
  269.  
  270. return $GLOBALS['permissions'][ $permissionName ];
  271.  
  272. }
  273.  
  274. //if the permission has ben set but no-one is logged in return false
  275.  
  276. if( !isset( $GLOBALS['user'] ) ) { return false; }
  277.  
  278. else {
  279.  
  280. if( $userConstraints = $GLOBALS['user']->get_satisfied_constraints() ) {
  281.  
  282. foreach( $permission->constraintArray as $constraint ) {
  283.  
  284. foreach( $userConstraints as $u_constraint ) {
  285.  
  286. if( $u_constraint->id == $constraint->id ) {
  287.  
  288. $GLOBALS['permissions'][ $permissionName ] = true;
  289.  
  290. return $GLOBALS['permissions'][ $permissionName ];
  291.  
  292. }
  293.  
  294. }
  295.  
  296. }
  297.  
  298. }
  299.  
  300. }
  301.  
  302.  
  303. $GLOBALS['permissions'][ $permissionName ] = false;
  304.  
  305. return $GLOBALS['permissions'][ $permissionName ];
  306.  
  307. /*
  308.  
  309. if( $userConstraints = $GLOBALS['user']->get_satisfied_constraints() ) {
  310.  
  311. foreach($userConstraints as $userConstraint ) {
  312.  
  313. if( !$objectArray ) {
  314.  
  315. if( array_key_exists( $userConstraint->id, $permission->constraintArray ) ) {
  316.  
  317. $GLOBALS['permissions'][ $permissionName ] = true;
  318.  
  319. return $GLOBALS['permissions'][ $permissionName ];
  320.  
  321. }
  322.  
  323. } else {
  324.  
  325. $satisfiedPermissions = array();
  326.  
  327.  
  328. foreach( $permission['constraintArray'] as $constraint ) {
  329.  
  330. if( !is_array($constraint) ) {
  331.  
  332. echo '<strong>!permission error</strong>: view permission used in place of array permission<strong>!</strong>';
  333.  
  334. return $objectArray;
  335.  
  336. }
  337.  
  338. if( $constraint['owner_constraint'] == $userConstraint ) {
  339.  
  340. array_push( $satisfiedPermissions, $permission['constraintArray'] );
  341.  
  342. break;
  343.  
  344. }
  345.  
  346. }
  347.  
  348. }
  349.  
  350. }
  351.  
  352. }
  353.  
  354. }*/
  355.  
  356. /*
  357.  
  358. //ok, so this is a start, but it doesn't allow sets of constraints to be defined on a per-constraint basis
  359.  
  360. if( $objectArray ) {
  361.  
  362. $returnArray = array();
  363.  
  364. foreach( $satisfiedPermissions as $currentPermission ) {
  365.  
  366. foreach( $objectArray as $object ) {
  367.  
  368. $objectConstraints = get_satisfied_constraints( $object );
  369.  
  370. for($i=0;$i<count($objectConstraints) && !isset($break);$i++) {
  371.  
  372. if( in_array( $objectConstraints[$i], $currentPermission['array_constraints'] ) ) { array_push( $returnArray, $object ); break; }
  373.  
  374. }
  375.  
  376. }
  377.  
  378. }
  379.  
  380. $GLOBALS['permissions'][ $permissionName ] = $returnArray;
  381.  
  382. return $GLOBALS['permissions'][ $permissionName ];
  383.  
  384. } else {
  385.  
  386. $GLOBALS['permissions'][ $permissionName ] = false;
  387.  
  388. return $GLOBALS['permissions'][ $permissionName ];
  389.  
  390. }*/
  391.  
  392. }
  393.  
  394. ?>

Documentation generated on Tue, 24 May 2005 03:58:07 -0400 by phpDocumentor 1.3.0RC3