b2evolution

Multilingual multiuser multiblog engine

b2evolution Technical Documentation (0.9.x) [ class tree: evocore ] [ index: evocore ] [ all elements ]

Source for file _functions_users.php

Documentation is available at _functions_users.php

  1. <?php
  2. /**
  3.  * User stuff
  4.  * 
  5.  * b2evolution - {@link http://b2evolution.net/}
  6.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  7.  * @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
  8.  *
  9.  * @package evocore
  10.  * @author This file built upon code from original b2 - http://cafelog.com/
  11.  */
  12. if!defined('DB_USER') ) die'Please, do not access this page directly.' );
  13.  
  14. /**
  15.  * Includes:
  16.  */
  17. require_once dirname(__FILE__)'/_functions_groups.php';
  18. require_once dirname(__FILE__)'/_class_user.php';
  19.  
  20.  
  21. /*
  22.  * veriflog(-)
  23.  *
  24.  * Verify if user is logged in
  25.  * checking login & pass in the database
  26.  */
  27. function veriflog$login_required false )
  28. {
  29.     global $cookie_user$cookie_pass$cookie_expires$cookie_path$cookie_domain$error$core_dirout;
  30.     global $user_login$user_pass_md5$userdata$user_ID$user_nickname$user_email$user_url;
  31.     global $current_User;
  32.     global $DB$tableusers;
  33.  
  34.     // Reset all global variables in case some tricky stuff is trying to set them otherwise:
  35.     // Warning: unset() prevent from setting a new global value later in the func !!! :((
  36.     $user_login '';
  37.     $user_pass_md5 '';
  38.     $userdata '';
  39.     $user_ID '';
  40.     $user_nickname '';
  41.     $user_email '';
  42.     $user_url '';
  43.  
  44.     // Check if user is trying to login right now:
  45.     ifisset($_POST['log'&& isset($_POST['pwd'))
  46.     {    // Trying to log in with a POST
  47.         $log strtolower(trim(strip_tags(get_magic_quotes_gpc(stripslashes($_POST['log']$_POST['log'])));
  48.         $user_pass_md5 md5(trim(strip_tags(get_magic_quotes_gpc(stripslashes($_POST['pwd']$_POST['pwd'])));
  49.         unset($_POST['pwd'])// password is hashed from now on
  50.     }
  51.     elseifisset($_GET['log'&& isset($_GET['pwd'))
  52.     {    // Trying to log in with a GET
  53.         $log strtolower(trim(strip_tags(get_magic_quotes_gpc(stripslashes($_GET['log']$_GET['log'])));
  54.         $user_pass_md5 md5(trim(strip_tags(get_magic_quotes_gpc(stripslashes($_GET['pwd']$_GET['pwd'])));
  55.         unset($_GET['pwd'])// password is hashed from now on
  56.     }
  57.  
  58.     ifisset($log) )
  59.     {    /*
  60.          * ---------------------------------------------------------
  61.          * User is trying to login right now
  62.          * ---------------------------------------------------------
  63.          */
  64.         // echo 'Trying to log in right now...';
  65.  
  66.         header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  67.         header("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  68.         header("Cache-Control: no-cache, must-revalidate");
  69.         header("Pragma: no-cache");
  70.  
  71.         // Check login and password
  72.         $user_login $log;
  73.         if!$login_ok user_pass_ok$user_login$user_pass_md5true ) ) )
  74.         {
  75.             // echo 'login failed!!';
  76.             return '<strong>'T_('ERROR')':</strong> 'T_('wrong login/password.');
  77.         }
  78.  
  79.         // Login succeeded:
  80.         //echo $user_login, $pass_is_md5, $user_pass,  $cookie_domain;
  81.         if!setcookie$cookie_user$log$cookie_expires$cookie_path$cookie_domain ) )
  82.             printfT_('setcookie %s failed!')'<br />'$cookie_user );
  83.         if!setcookie$cookie_pass$user_pass_md5$cookie_expires$cookie_path$cookie_domain) )
  84.             printfT_('setcookie %s failed!')'<br />'$cookie_pass );
  85.     }
  86.     elseifisset($_COOKIE[$cookie_user]&& isset($_COOKIE[$cookie_pass]) )
  87.     {    /*
  88.          * ---------------------------------------------------------
  89.          * User was not trying to log in, but he already was logged in: check validity
  90.          * ---------------------------------------------------------
  91.          */
  92.         // echo 'Was already logged in...';
  93.  
  94.         $user_login trim(strip_tags(get_magic_quotes_gpc(stripslashes($_COOKIE[$cookie_user]$_COOKIE[$cookie_user]));
  95.         $user_pass_md5 trim(strip_tags(get_magic_quotes_gpc(stripslashes($_COOKIE[$cookie_pass]$_COOKIE[$cookie_pass]));
  96.         // echo 'pass=', $user_pass_md5;
  97.  
  98.         ifuser_pass_ok$user_login$user_pass_md5true ) )
  99.         {    // login is NOT OK:
  100.             if$login_required )
  101.             {
  102.                 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  103.                 header("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  104.                 header("Cache-Control: no-cache, must-revalidate");
  105.                 header("Pragma: no-cache");
  106.  
  107.                 return '<strong>'T_('ERROR')':</strong> 'T_('login/password no longer valid.');
  108.             }
  109.  
  110.             return 0;    // Wrong login but we don't care.
  111.         }
  112.     }
  113.     else
  114.     {    /*
  115.          * ---------------------------------------------------------
  116.          * User was not logged in at all
  117.          * ---------------------------------------------------------
  118.          */
  119.         // echo ' NOT logged in...';
  120.  
  121.         if$login_required )
  122.         {
  123.             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  124.             header("Last-Modified: " gmdate("D, d M Y H:i:s"" GMT");
  125.             header("Cache-Control: no-cache, must-revalidate");
  126.             header("Pragma: no-cache");
  127.  
  128.             return T_('You must log in!');
  129.             exit();
  130.         }
  131.  
  132.         return 0;    // Not logged in but we don't care
  133.     }
  134.  
  135.     /*
  136.      * Login info is OK, we set the global variables:
  137.      */
  138.     // echo 'LOGGED IN';
  139.     $userdata    get_userdatabylogin($user_login);
  140.     $current_User new User$userdata );    // COPY!
  141.     //echo $current_User->disp('login');
  142.     
  143.     $user_ID $userdata['ID'];
  144.     $user_nickname $userdata['user_nickname'];
  145.     $user_email    $userdata['user_email'];
  146.     $user_url    $userdata['user_url'];
  147.  
  148.     return 0;        // OK
  149. }
  150.  
  151.  
  152. /*
  153.  * logout
  154.  *
  155.  * Log the user out:
  156.  */
  157. function logout()
  158. {
  159.     global $cookie_user$cookie_pass$cookie_expired$cookie_path$cookie_domain;
  160.     global $user_login$user_pass_md5$userdata$user_ID$user_nickname$user_email$user_url;
  161.  
  162.     // Reset all global variables
  163.     // Note: unset is bugguy on globals
  164.     $user_login '';
  165.     $user_pass_md5 '';
  166.     $userdata '';
  167.     $user_ID '';
  168.     $user_nickname '';
  169.     $user_email '';
  170.     $user_url '';
  171.  
  172.     setcookie'cafeloguser' );        // OLD
  173.     setcookie'cafeloguser'''$cookie_expired$cookie_path$cookie_domain)// OLD
  174.     setcookie$cookie_user''$cookie_expired$cookie_path$cookie_domain);
  175.  
  176.     setcookie'cafelogpass' );        // OLD
  177.     setcookie'cafelogpass'''$cookie_expired$cookie_path$cookie_domain);    // OLD
  178.     setcookie$cookie_pass''$cookie_expired$cookie_path$cookie_domain);
  179.  
  180. }
  181.  
  182. /**
  183.  * is_logged_in(-)
  184.  */
  185. function is_logged_in()
  186. {
  187.     global $user_ID$generating_static;
  188.  
  189.     ifisset($generating_static&& $generating_static )
  190.     {    // When generating static page, we should always consider we are not logged in.
  191.         return false;
  192.     }
  193.  
  194.     return (!empty($user_ID));
  195. }
  196.  
  197.  
  198.  
  199. /**
  200.  * user_pass_ok(-)
  201.  * @return boolean 
  202.  */
  203. function user_pass_ok$user_login$user_pass$pass_is_md5 false )
  204. {
  205.     $userdata get_userdatabylogin($user_login);
  206.     ifempty($userdata) )
  207.     {
  208.         return false;
  209.     }
  210.     // echo 'got data for: ', $userdata['user_login'];
  211.  
  212.     if!$pass_is_md5 $user_pass md5$user_pass );
  213.     // echo 'pass: ', $user_pass, '/', $userdata['user_pass'];
  214.  
  215.     return ($user_pass == $userdata['user_pass']);
  216. }
  217.  
  218.  
  219. /**
  220.  * get_userdatabylogin(-)
  221.  * @return array DB row on success, false if login does not exist
  222.  */
  223. function get_userdatabylogin$user_login )
  224. {
  225.     global $DB$tableusers$cache_userdata$use_cache;
  226.     if( (empty($cache_userdata[$user_login])) OR (!$use_cache) )
  227.     {
  228.         $sql "SELECT * 
  229.                         FROM $tableusers 
  230.                         WHERE user_login = '".$DB->escape($user_login)."'";
  231.         $myrow $DB->get_row$sqlARRAY_A );
  232.         $cache_userdata[$user_login$myrow;
  233.     }
  234.     else
  235.     {
  236.         $myrow $cache_userdata[$user_login];
  237.     }
  238.     return($myrow);
  239. }
  240.  
  241. /*
  242.  * get_userdata(-)
  243.  */
  244. function get_userdata($userid)
  245. {
  246.     global $DB$tableusers$cache_userdata;
  247.     ifempty($cache_userdata[$userid) )
  248.     {    // We do a progressive cache load beacuse there can be many many users!
  249.         $sql "SELECT *
  250.                         FROM $tableusers
  251.                         WHERE ID = $userid";
  252.         if$myrow $DB->get_row$sqlARRAY_A ) )
  253.         {
  254.             $cache_userdata$myrow['ID'] ] $myrow;
  255.         }
  256.     }
  257.  
  258.     ifisset$cache_userdata[$userid) )
  259.     {
  260.         die('Requested user '.$userid.' does not exist!');
  261.     }
  262.  
  263.     return $cache_userdata[$userid];
  264. }
  265.  
  266.  
  267. /*
  268.  * get_usernumposts(-)
  269.  */
  270. function get_usernumposts$userid )
  271. {
  272.     global $DB$tableposts;
  273.     return $DB->get_var"SELECT count(*)
  274.                                                 FROM $tableposts
  275.                                                 WHERE post_author = $userid);
  276. }
  277.  
  278.  
  279. /*
  280.  * get_user_info(-)
  281.  */
  282. function get_user_info$show ''$this_userdata '' )
  283. {
  284.     global $userdata;
  285.  
  286.     ifempty$this_userdata ) )
  287.     {    // We want the current user
  288.         $this_userdata $userdata;
  289.     }
  290.  
  291.     switch$show )
  292.     {
  293.         case 'ID':
  294.             $output $this_userdata['ID'];
  295.             break;
  296.  
  297.         case 'num_posts':
  298.             $output get_usernumposts$this_userdata['ID');
  299.             break;
  300.  
  301.         case 'level':
  302.         case 'firstname':
  303.         case 'lastname':
  304.         case 'nickname':
  305.         case 'idmode':
  306.         case 'email':
  307.         case 'url':
  308.         case 'icq':
  309.         case 'aim':
  310.         case 'msn':
  311.         case 'yim':
  312.         case 'notify':
  313.         case 'locale':
  314.             $output $this_userdata['user_'$show];
  315.             break;
  316.  
  317.         case 'login':
  318.         default:
  319.             $output $this_userdata['user_login'];
  320.             break;
  321.     }
  322.     return trim($output);
  323. }
  324.  
  325.  
  326. /*
  327.  * user_info(-)
  328.  *
  329.  * Template tag
  330.  */
  331. function user_info$show ''$format 'raw'$display true )
  332. {
  333.     $content get_user_info$show );
  334.     $content format_to_output$content$format );
  335.     if$display )
  336.         echo $content;
  337.     else
  338.         return $content;
  339. }
  340.  
  341.  
  342.  
  343. /*
  344.  * user_login_link(-)
  345.  *
  346.  * Template tag; Provide a link to login
  347.  */
  348. function user_login_link$before ''$after ''$link_text ''$link_title '#' )
  349. {
  350.     global $htsrv_url$edited_Blog$generating_static;
  351.  
  352.     ifis_logged_in() ) return false;
  353.  
  354.     if$link_text == '' $link_text T_('Login...');
  355.     if$link_title == '#' $link_title T_('Login if you have an account...');
  356.         
  357.     if!isset($generating_static|| $generating_static == false )
  358.     {    // We are not generating a static page here:
  359.         $redirect '?redirect_to='.urlencoderegenerate_url() );
  360.     }
  361.     elseifisset($edited_Blog) )
  362.     {    // We are generating a static page
  363.         $redirect '?redirect_to='.$edited_Blog->get('dynurl');
  364.     }
  365.     else
  366.     {    // We are in a weird situation
  367.         $redirect '';
  368.     }
  369.         
  370.     echo $before;
  371.     echo '<a href="'$htsrv_url'/login.php'.$redirect.'" title="'$link_title'">';
  372.     echo $link_text;
  373.     echo '</a>';
  374.     echo $after;
  375. }
  376.  
  377. /*
  378.  * user_register_link(-)
  379.  *
  380.  * Template tag; Provide a link to new user registration
  381.  */
  382. function user_register_link$before ''$after ''$link_text ''$link_title '#' )
  383. {
  384.     global $htsrv_url$Settings$edited_Blog$generating_static;
  385.  
  386.     ifis_logged_in(|| !$Settings->get('newusers_canregister'))
  387.     {    // There's no need to provide this link if already logged in or if we won't let him register
  388.         return false;
  389.     }
  390.  
  391.     if$link_text == '' $link_text T_('Register...');
  392.     if$link_title == '#' $link_title T_('Register to open an account...');
  393.  
  394.     if!isset($generating_static|| $generating_static == false )
  395.     {    // We are not generating a static page here:
  396.         $redirect '?redirect_to='.urlencoderegenerate_url() );
  397.     }
  398.     elseifisset($edited_Blog) )
  399.     {    // We are generating a static page
  400.         $redirect '?redirect_to='.$edited_Blog->get('dynurl');
  401.     }
  402.     else
  403.     {    // We are in a weird situation
  404.         $redirect '';
  405.     }
  406.         
  407.     echo $before;
  408.     echo '<a href="'$htsrv_url'/register.php'.$redirect.'" title="'$link_title'">';
  409.     echo $link_text;
  410.     echo '</a>';
  411.     echo $after;
  412. }
  413.  
  414.  
  415. /*
  416.  * user_logout_link(-)
  417.  *
  418.  * Template tag; Provide a link to logout
  419.  */
  420. function user_logout_link$before ''$after ''$link_text ''$link_title '#' )
  421. {
  422.     global $htsrv_url$user_login$blog;
  423.  
  424.     ifis_logged_in() ) return false;
  425.  
  426.     if$link_text == '' $link_text T_('Logout (%s)');
  427.     if$link_title == '#' $link_title T_('Logout from your account');
  428.  
  429.     echo $before;
  430.     echo '<a href="'$htsrv_url'/login.php?action=logout&amp;redirect_to='.urlencoderegenerate_url() )'" title="'$link_title'">';
  431.     printf$link_text$user_login );
  432.     echo '</a>';
  433.     echo $after;
  434. }
  435.  
  436. /*
  437.  * user_admin_link(-)
  438.  *
  439.  * Template tag; Provide a link to the backoffice
  440.  */
  441. function user_admin_link$before ''$after ''$page 'b2edit.php'$link_text ''$link_title '#' )
  442. {
  443.     global $admin_url$blog$current_User;
  444.  
  445.     ifis_logged_in() ) return false;
  446.  
  447.     if$current_User->get('level'== )
  448.     // If user is NOT active:
  449.         return false;
  450.     }
  451.  
  452.     if$link_text == '' $link_text T_('Admin');
  453.     if$link_title == '#' $link_title T_('Go to the back-office');
  454.     // add the blog param to $page if it is not already in there
  455.     if!preg_match('/(&|&amp;|\?)blog=/'$page) ) $page url_add_param$page'blog='.$blog );
  456.  
  457.     echo $before;
  458.     echo '<a href="'$admin_url'/'$page'" title="'$link_title'">';
  459.     echo $link_text ;
  460.     echo '</a>';
  461.     echo $after;
  462. }
  463.  
  464.  
  465. /*
  466.  * user_profile_link(-)
  467.  *
  468.  * Template tag; Provide a link to user profile
  469.  */
  470. function user_profile_link$before ''$after ''$link_text ''$link_title '#' )
  471. {
  472.     global $user_login$pagenow$Blog;
  473.  
  474.     ifis_logged_in() ) return false;
  475.  
  476.     if$link_text == '' $link_text T_('Profile (%s)');
  477.     if$link_title == '#' $link_title T_('Edit your profile');
  478.  
  479.     echo $before;
  480.     echo '<a href="'.url_add_param$Blog->dget'blogurl''raw' )'disp=profile&amp;redirect_to='.urlencode(regenerate_url()) )
  481.             .'" title="'$link_title'">';
  482.     printf$link_text$user_login );
  483.     echo '</a>';
  484.     echo $after;
  485. }
  486.  
  487. /**
  488.  * Display "User profile" title if it has been requested
  489.  *
  490.  * {@internal profile_title(-) }}
  491.  *
  492.  * @param string Prefix to be displayed if something is going to be displayed
  493.  * @param mixed Output format, see {@link format_to_output()} or false to
  494.  *                                 return value instead of displaying it
  495.  */
  496. function profile_title$prefix ' '$display 'htmlbody' )
  497. {
  498.     global $disp;
  499.  
  500.     if$disp == 'profile' )
  501.     {
  502.         $info $prefix.T_('User profile');
  503.         if ($display)
  504.             echo format_to_output$info$display );
  505.         else
  506.             return $info;
  507.     }
  508. }
  509.  
  510.  
  511. ?>

Documentation generated on Tue, 20 May 2008 01:55:07 +0200 by phpDocumentor 1.4.2