source: trunk/include/functions_user.inc.php @ 18629

Last change on this file since 18629 was 18629, checked in by rvelices, 12 years ago
  • remove unsed code; shorten existing code and improve readability ...
  • Property svn:eol-style set to LF
File size: 35.6 KB
RevLine 
[2]1<?php
[362]2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
[2297]6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
[2]23
[2115]24// validate_mail_address:
25//   o verifies whether the given mail address has the
26//     right format. ie someone@domain.com "someone" can contain ".", "-" or
27//     even "_". Exactly as "domain". The extension doesn't have to be
28//     "com". The mail address can also be empty.
29//   o check if address could be empty
30//   o check if address is not used by a other user
[9]31// If the mail address doesn't correspond, an error message is returned.
[2127]32//
[2124]33function validate_mail_address($user_id, $mail_address)
[2]34{
[2115]35  global $conf;
[2]36
[2032]37  if (empty($mail_address) and
[2127]38      !($conf['obligatory_user_mail_address'] and
[2124]39      in_array(script_basename(), array('register', 'profile'))))
[2]40  {
[9]41    return '';
[2]42  }
[2115]43
[18164]44  if ( !email_check_format($mail_address) )
[9]45  {
[5021]46    return l10n('mail address must be like xxx@yyy.eee (example : jack@altern.org)');
[9]47  }
[2127]48
[2115]49  if (defined("PHPWG_INSTALLED") and !empty($mail_address))
50  {
51    $query = '
[18164]52SELECT count(*)
53FROM '.USERS_TABLE.'
54WHERE upper('.$conf['user_fields']['email'].') = upper(\''.$mail_address.'\')
55'.(is_numeric($user_id) ? 'AND '.$conf['user_fields']['id'].' != \''.$user_id.'\'' : '').'
[2115]56;';
[4325]57    list($count) = pwg_db_fetch_row(pwg_query($query));
[2115]58    if ($count != 0)
59    {
[8635]60      return l10n('this email address is already in use');
[2115]61    }
62  }
[2]63}
64
[4429]65// validate_login_case:
66//   o check if login is not used by a other user
67// If the login doesn't correspond, an error message is returned.
68//
69function validate_login_case($login)
70{
71  global $conf;
[13074]72
[4429]73  if (defined("PHPWG_INSTALLED"))
74  {
75    $query = "
76SELECT ".$conf['user_fields']['username']."
77FROM ".USERS_TABLE."
78WHERE LOWER(".stripslashes($conf['user_fields']['username']).") = '".strtolower($login)."'
79;";
80
81    $count = pwg_db_num_rows(pwg_query($query));
82
83    if ($count > 0)
84    {
[5021]85      return l10n('this login is already used');
[4429]86    }
87  }
88}
[10860]89/**
90 * For test on username case sensitivity
91 *
92 * @param : $username typed in by user for identification
93 *
94 * @return : $username found in database
95 *
96 */
97function search_case_username($username)
98{
99  global $conf;
[4429]100
[10860]101  $username_lo = strtolower($username);
102
103  $SCU_users = array();
[13074]104
[10860]105  $q = pwg_query("
106    SELECT ".$conf['user_fields']['username']." AS username
107    FROM `".USERS_TABLE."`;
108  ");
109  while ($r = pwg_db_fetch_assoc($q))
110   $SCU_users[$r['username']] = strtolower($r['username']);
111   // $SCU_users is now an associative table where the key is the account as
112   // registered in the DB, and the value is this same account, in lower case
[13074]113
[10860]114  $users_found = array_keys($SCU_users, $username_lo);
115  // $users_found is now a table of which the values are all the accounts
116  // which can be written in lowercase the same way as $username
117  if (count($users_found) != 1) // If ambiguous, don't allow lowercase writing
118   return $username; // but normal writing will work
119  else
120   return $users_found[0];
121}
[2178]122function register_user($login, $password, $mail_address,
123  $with_notification = true, $errors = array())
[2]124{
[2115]125  global $conf;
[2]126
[661]127  if ($login == '')
128  {
[13074]129    $errors[] = l10n('Please, enter a login');
[661]130  }
[3747]131  if (preg_match('/^.* $/', $login))
[661]132  {
[13074]133    $errors[] = l10n('login mustn\'t end with a space character');
[661]134  }
[3747]135  if (preg_match('/^ .*$/', $login))
[661]136  {
[13074]137    $errors[] = l10n('login mustn\'t start with a space character');
[661]138  }
[808]139  if (get_userid($login))
[804]140  {
[13074]141    $errors[] = l10n('this login is already used');
[2]142  }
[9923]143  if ($login != strip_tags($login))
144  {
[13074]145    $errors[] = l10n('html tags are not allowed in login');
[9923]146  }
[2124]147  $mail_error = validate_mail_address(null, $mail_address);
[808]148  if ('' != $mail_error)
[661]149  {
[13074]150    $errors[] = $mail_error;
[661]151  }
[2]152
[5060]153  if ($conf['insensitive_case_logon'] == true)
[4429]154  {
155    $login_error = validate_login_case($login);
156    if ($login_error != '')
157    {
[13074]158      $errors[] = $login_error;
[4429]159    }
160  }
161
[2237]162  $errors = trigger_event('register_user_check',
163              $errors,
164              array(
[2268]165                'username'=>$login,
[2237]166                'password'=>$password,
167                'email'=>$mail_address,
168              )
169            );
170
[9]171  // if no error until here, registration of the user
[661]172  if (count($errors) == 0)
[2]173  {
[906]174    // what will be the inserted id ?
175    $query = '
176SELECT MAX('.$conf['user_fields']['id'].') + 1
177  FROM '.USERS_TABLE.'
178;';
[4325]179    list($next_id) = pwg_db_fetch_row(pwg_query($query));
[1068]180
[808]181    $insert =
182      array(
[906]183        $conf['user_fields']['id'] => $next_id,
[4325]184        $conf['user_fields']['username'] => pwg_db_real_escape_string($login),
[808]185        $conf['user_fields']['password'] => $conf['pass_convert']($password),
186        $conf['user_fields']['email'] => $mail_address
187        );
[661]188
[808]189    mass_inserts(USERS_TABLE, array_keys($insert), array($insert));
[1068]190
[2178]191    // Assign by default groups
192    {
193      $query = '
[1583]194SELECT id
195  FROM '.GROUPS_TABLE.'
196  WHERE is_default = \''.boolean_to_string(true).'\'
197  ORDER BY id ASC
198;';
[2178]199      $result = pwg_query($query);
[1581]200
[2178]201      $inserts = array();
[4325]202      while ($row = pwg_db_fetch_assoc($result))
[2178]203      {
[13074]204          $inserts[] = array(
[2178]205            'user_id' => $next_id,
206            'group_id' => $row['id']
[13074]207          );
[2178]208      }
[1583]209    }
[1581]210
[1583]211    if (count($inserts) != 0)
212    {
213      mass_inserts(USER_GROUP_TABLE, array('user_id', 'group_id'), $inserts);
[1581]214    }
215
[2425]216    $override = null;
217    if ($with_notification and $conf['browser_language'])
218    {
219      if ( !get_browser_language($override['language']) )
220        $override=null;
221    }
222    create_user_infos($next_id, $override);
[1605]223
[2178]224    if ($with_notification and $conf['email_admin_on_new_user'])
225    {
226      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
227      $admin_url = get_absolute_root_url()
[2268]228                   .'admin.php?page=user_list&username='.$login;
[2178]229
230      $keyargs_content = array
231      (
[4304]232        get_l10n_args('User: %s', stripslashes($login)),
[2178]233        get_l10n_args('Email: %s', $_POST['mail_address']),
234        get_l10n_args('', ''),
235        get_l10n_args('Admin: %s', $admin_url)
236      );
237
238      pwg_mail_notification_admins
239      (
[4304]240        get_l10n_args('Registration of %s', stripslashes($login)),
[2178]241        $keyargs_content
242      );
243    }
244
[1605]245    trigger_action('register_user',
246      array(
247        'id'=>$next_id,
248        'username'=>$login,
249        'email'=>$mail_address,
250       )
251      );
[2]252  }
[906]253
[661]254  return $errors;
[2]255}
256
[1677]257function build_user( $user_id, $use_cache )
[1568]258{
259  global $conf;
[2038]260
[1568]261  $user['id'] = $user_id;
[1677]262  $user = array_merge( $user, getuserdata($user_id, $use_cache) );
[1926]263
[2055]264  if ($user['id'] == $conf['guest_id'] and $user['status'] <> 'guest')
265  {
266    $user['status'] = 'guest';
267    $user['internal_status']['guest_must_be_guest'] = true;
268  }
269
[5123]270  // Check user theme
[5264]271  if (!isset($user['theme_name']))
[5123]272  {
[5264]273    $user['theme'] = get_default_theme();
[5123]274  }
[2039]275
[1568]276  return $user;
277}
278
[808]279/**
280 * find informations related to the user identifier
281 *
282 * @param int user identifier
283 * @param boolean use_cache
284 * @param array
285 */
[1677]286function getuserdata($user_id, $use_cache)
[393]287{
[808]288  global $conf;
289
[6315]290  // retrieve basic user data
[808]291  $query = '
292SELECT ';
293  $is_first = true;
294  foreach ($conf['user_fields'] as $pwgfield => $dbfield)
295  {
296    if ($is_first)
297    {
298      $is_first = false;
299    }
300    else
301    {
302      $query.= '
303     , ';
304    }
305    $query.= $dbfield.' AS '.$pwgfield;
306  }
307  $query.= '
308  FROM '.USERS_TABLE.'
[3622]309  WHERE '.$conf['user_fields']['id'].' = \''.$user_id.'\'';
[1068]310
[4325]311  $row = pwg_db_fetch_assoc(pwg_query($query));
[808]312
[6510]313  // retrieve additional user data ?
314  if ($conf['external_authentification'])
315  {
316    $query = '
317SELECT
[11356]318    COUNT(1) AS counter
[6510]319  FROM '.USER_INFOS_TABLE.' AS ui
320    LEFT JOIN '.USER_CACHE_TABLE.' AS uc ON ui.user_id = uc.user_id
321    LEFT JOIN '.THEMES_TABLE.' AS t ON t.id = ui.theme
322  WHERE ui.user_id = '.$user_id.'
323  GROUP BY ui.user_id
324;';
[11356]325    list($counter) = pwg_db_fetch_row(pwg_query($query));
326    if ($counter != 1)
[6510]327    {
328      create_user_infos($user_id);
329    }
[808]330  }
[1068]331
[11356]332  // retrieve user info
333  $query = '
334SELECT
335    ui.*,
336    uc.*,
337    t.name AS theme_name
338  FROM '.USER_INFOS_TABLE.' AS ui
339    LEFT JOIN '.USER_CACHE_TABLE.' AS uc ON ui.user_id = uc.user_id
340    LEFT JOIN '.THEMES_TABLE.' AS t ON t.id = ui.theme
341  WHERE ui.user_id = '.$user_id.'
342;';
343
344  $result = pwg_query($query);
345  $user_infos_row = pwg_db_fetch_assoc($result);
346
[6315]347  // then merge basic + additional user data
[18629]348  $userdata = array_merge($row, $user_infos_row);
[1068]349
[18629]350  foreach ($userdata as &$value)
[808]351  {
[18629]352      // If the field is true or false, the variable is transformed into a boolean value.
353      if ($value == 'true')
[808]354      {
[18629]355        $value = true;
[808]356      }
[18629]357      elseif ($value == 'false')
[808]358      {
[18629]359        $value = false;
[808]360      }
361  }
[18629]362  unset($value);
[808]363
364  if ($use_cache)
365  {
366    if (!isset($userdata['need_update'])
367        or !is_bool($userdata['need_update'])
[1677]368        or $userdata['need_update'] == true)
[808]369    {
[2448]370      $userdata['cache_update_time'] = time();
371
372      // Set need update are done
373      $userdata['need_update'] = false;
374
[808]375      $userdata['forbidden_categories'] =
376        calculate_permissions($userdata['id'], $userdata['status']);
377
[2084]378      /* now we build the list of forbidden images (this list does not contain
379      images that are not in at least an authorized category)*/
380      $query = '
381SELECT DISTINCT(id)
382  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id
383  WHERE category_id NOT IN ('.$userdata['forbidden_categories'].')
384    AND level>'.$userdata['level'];
385      $forbidden_ids = array_from_query($query, 'id');
386
387      if ( empty($forbidden_ids) )
388      {
[13074]389        $forbidden_ids[] = 0;
[2084]390      }
391      $userdata['image_access_type'] = 'NOT IN'; //TODO maybe later
392      $userdata['image_access_list'] = implode(',',$forbidden_ids);
393
[1624]394
[1081]395      $query = '
396SELECT COUNT(DISTINCT(image_id)) as total
397  FROM '.IMAGE_CATEGORY_TABLE.'
398  WHERE category_id NOT IN ('.$userdata['forbidden_categories'].')
[3622]399    AND image_id '.$userdata['image_access_type'].' ('.$userdata['image_access_list'].')';
[4325]400      list($userdata['nb_total_images']) = pwg_db_fetch_row(pwg_query($query));
[1081]401
[3622]402
403      // now we update user cache categories
404      $user_cache_cats = get_computed_categories($userdata, null);
405      if ( !is_admin($userdata['status']) )
406      { // for non admins we forbid categories with no image (feature 1053)
407        $forbidden_ids = array();
[3640]408        foreach ($user_cache_cats as $cat)
[3622]409        {
410          if ($cat['count_images']==0)
411          {
[13074]412            $forbidden_ids[] = $cat['cat_id'];
[3640]413            unset( $user_cache_cats[$cat['cat_id']] );
[3622]414          }
415        }
416        if ( !empty($forbidden_ids) )
417        {
418          if ( empty($userdata['forbidden_categories']) )
419          {
420            $userdata['forbidden_categories'] = implode(',', $forbidden_ids);
421          }
422          else
423          {
424            $userdata['forbidden_categories'] .= ','.implode(',', $forbidden_ids);
425          }
426        }
427      }
428
429      // delete user cache
430      $query = '
431DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.'
432  WHERE user_id = '.$userdata['id'];
433      pwg_query($query);
434
[12748]435      // Due to concurrency issues, we ask MySQL to ignore errors on
436      // insert. This may happen when cache needs refresh and that Piwigo is
437      // called "very simultaneously".
[3622]438      mass_inserts
439      (
440        USER_CACHE_CATEGORIES_TABLE,
441        array
442        (
443          'user_id', 'cat_id',
444          'date_last', 'max_date_last', 'nb_images', 'count_images', 'count_categories'
445        ),
[12748]446        $user_cache_cats,
447        array('ignore' => true)
[3622]448      );
449
450
[808]451      // update user cache
452      $query = '
453DELETE FROM '.USER_CACHE_TABLE.'
[3622]454  WHERE user_id = '.$userdata['id'];
[808]455      pwg_query($query);
[1068]456
[12748]457      // for the same reason as user_cache_categories, we ignore error on
458      // this insert
[808]459      $query = '
[12748]460INSERT IGNORE INTO '.USER_CACHE_TABLE.'
[2448]461  (user_id, need_update, cache_update_time, forbidden_categories, nb_total_images,
[2084]462    image_access_type, image_access_list)
[808]463  VALUES
[2448]464  ('.$userdata['id'].',\''.boolean_to_string($userdata['need_update']).'\','
465  .$userdata['cache_update_time'].',\''
[4367]466  .$userdata['forbidden_categories'].'\','.$userdata['nb_total_images'].',\''
467  .$userdata['image_access_type'].'\',\''.$userdata['image_access_list'].'\')';
[808]468      pwg_query($query);
469    }
470  }
471
472  return $userdata;
[393]473}
[647]474
475/*
476 * deletes favorites of the current user if he's not allowed to see them
477 *
478 * @return void
479 */
480function check_user_favorites()
481{
482  global $user;
483
484  if ($user['forbidden_categories'] == '')
485  {
486    return;
487  }
[832]488
[1677]489  // $filter['visible_categories'] and $filter['visible_images']
490  // must be not used because filter <> restriction
[832]491  // retrieving images allowed : belonging to at least one authorized
492  // category
[647]493  $query = '
[832]494SELECT DISTINCT f.image_id
[647]495  FROM '.FAVORITES_TABLE.' AS f INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic
496    ON f.image_id = ic.image_id
497  WHERE f.user_id = '.$user['id'].'
[1677]498'.get_sql_condition_FandF
499  (
500    array
501      (
502        'forbidden_categories' => 'ic.category_id',
503      ),
504    'AND'
505  ).'
[647]506;';
[13074]507  $authorizeds = array_from_query($query, 'image_id');
[647]508
[832]509  $query = '
510SELECT image_id
511  FROM '.FAVORITES_TABLE.'
512  WHERE user_id = '.$user['id'].'
513;';
[13074]514  $favorites = array_from_query($query, 'image_id');
[832]515
516  $to_deletes = array_diff($favorites, $authorizeds);
517  if (count($to_deletes) > 0)
518  {
[647]519    $query = '
520DELETE FROM '.FAVORITES_TABLE.'
[832]521  WHERE image_id IN ('.implode(',', $to_deletes).')
[647]522    AND user_id = '.$user['id'].'
523;';
524    pwg_query($query);
525  }
526}
[648]527
528/**
[808]529 * calculates the list of forbidden categories for a given user
[648]530 *
[808]531 * Calculation is based on private categories minus categories authorized to
532 * the groups the user belongs to minus the categories directly authorized
533 * to the user. The list contains at least -1 to be compliant with queries
534 * such as "WHERE category_id NOT IN ($forbidden_categories)"
[648]535 *
536 * @param int user_id
[680]537 * @param string user_status
[648]538 * @return string forbidden_categories
539 */
[680]540function calculate_permissions($user_id, $user_status)
[648]541{
542  $query = '
543SELECT id
544  FROM '.CATEGORIES_TABLE.'
545  WHERE status = \'private\'
546;';
[13074]547  $private_array = array_from_query($query, 'id');
[680]548
[648]549  // retrieve category ids directly authorized to the user
550  $query = '
551SELECT cat_id
552  FROM '.USER_ACCESS_TABLE.'
553  WHERE user_id = '.$user_id.'
554;';
[808]555  $authorized_array = array_from_query($query, 'cat_id');
[648]556
557  // retrieve category ids authorized to the groups the user belongs to
558  $query = '
559SELECT cat_id
560  FROM '.USER_GROUP_TABLE.' AS ug INNER JOIN '.GROUP_ACCESS_TABLE.' AS ga
561    ON ug.group_id = ga.group_id
562  WHERE ug.user_id = '.$user_id.'
563;';
[808]564  $authorized_array =
565    array_merge(
566      $authorized_array,
567      array_from_query($query, 'cat_id')
568      );
[648]569
570  // uniquify ids : some private categories might be authorized for the
571  // groups and for the user
572  $authorized_array = array_unique($authorized_array);
573
574  // only unauthorized private categories are forbidden
575  $forbidden_array = array_diff($private_array, $authorized_array);
576
[1117]577  // if user is not an admin, locked categories are forbidden
[1851]578  if (!is_admin($user_status))
[1117]579  {
580    $query = '
581SELECT id
582  FROM '.CATEGORIES_TABLE.'
583  WHERE visible = \'false\'
584;';
585    $result = pwg_query($query);
[4325]586    while ($row = pwg_db_fetch_assoc($result))
[1117]587    {
[13074]588      $forbidden_array[] = $row['id'];
[1117]589    }
590    $forbidden_array = array_unique($forbidden_array);
591  }
[1068]592
[1117]593  if ( empty($forbidden_array) )
[1331]594  {// at least, the list contains 0 value. This category does not exists so
595   // where clauses such as "WHERE category_id NOT IN(0)" will always be
[1117]596   // true.
[13074]597    $forbidden_array[] = 0;
[1117]598  }
599
[808]600  return implode(',', $forbidden_array);
[648]601}
[708]602
603/**
[1677]604 * compute data of categories branches (one branch only)
[1624]605 */
[1640]606function compute_branch_cat_data(&$cats, &$list_cat_id, &$level, &$ref_level)
[1624]607{
[1640]608  $date = '';
609  $count_images = 0;
610  $count_categories = 0;
611  do
[1624]612  {
[1640]613    $cat_id = array_pop($list_cat_id);
614    if (!is_null($cat_id))
[1624]615    {
[1640]616      // Count images and categories
617      $cats[$cat_id]['count_images'] += $count_images;
618      $cats[$cat_id]['count_categories'] += $count_categories;
619      $count_images = $cats[$cat_id]['count_images'];
620      $count_categories = $cats[$cat_id]['count_categories'] + 1;
621
622      if ((empty($cats[$cat_id]['max_date_last'])) or ($cats[$cat_id]['max_date_last'] < $date))
[1624]623      {
[1640]624        $cats[$cat_id]['max_date_last'] = $date;
[1624]625      }
626      else
627      {
[1640]628        $date = $cats[$cat_id]['max_date_last'];
[1624]629      }
[1640]630      $ref_level = substr_count($cats[$cat_id]['global_rank'], '.') + 1;
631    }
632    else
[1624]633    {
[1640]634      $ref_level = 0;
[1624]635    }
[1640]636  } while ($level <= $ref_level);
637
638  // Last cat updating must be added to list for next branch
639  if ($ref_level <> 0)
640  {
[18629]641    $list_cat_id[] = $cat_id;
[1624]642  }
[1640]643}
[1624]644
[1640]645/**
[1677]646 * compute data of categories branches
[1640]647 */
[1677]648function compute_categories_data(&$cats)
[1640]649{
[1677]650  $ref_level = 0;
651  $level = 0;
652  $list_cat_id = array();
[1624]653
[1677]654  foreach ($cats as $id => $category)
[1624]655  {
[1677]656    // Compute
657    $level = substr_count($category['global_rank'], '.') + 1;
658    if ($level > $ref_level)
659    {
[18629]660      $list_cat_id[] = $id;
[1677]661    }
662    else
663    {
664      compute_branch_cat_data($cats, $list_cat_id, $level, $ref_level);
[18629]665      $list_cat_id[] = $id;
[1677]666    }
667    $ref_level = $level;
[1624]668  }
[1651]669
[1677]670  $level = 1;
671  compute_branch_cat_data($cats, $list_cat_id, $level, $ref_level);
672}
[1651]673
[1677]674/**
675 * get computed array of categories
676 *
[1860]677 * @param array userdata
678 * @param int filter_days number of recent days to filter on or null
[1677]679 * @return array
680 */
[1860]681function get_computed_categories($userdata, $filter_days=null)
[1677]682{
[6652]683  $query = 'SELECT c.id AS cat_id, global_rank';
[2084]684  // Count by date_available to avoid count null
685  $query .= ',
[6652]686  MAX(date_available) AS date_last, COUNT(date_available) AS nb_images
[2084]687FROM '.CATEGORIES_TABLE.' as c
688  LEFT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.category_id = c.id
689  LEFT JOIN '.IMAGES_TABLE.' AS i
690    ON ic.image_id = i.id
691      AND i.level<='.$userdata['level'];
692
693  if ( isset($filter_days) )
[1651]694  {
[4367]695    $query .= ' AND i.date_available > '.pwg_db_get_recent_period_expression($filter_days);
[1651]696  }
697
[1860]698  if ( !empty($userdata['forbidden_categories']) )
[1651]699  {
700    $query.= '
[1860]701  WHERE c.id NOT IN ('.$userdata['forbidden_categories'].')';
[1651]702  }
703
[2324]704  $query.= '
[4385]705  GROUP BY c.id, c.global_rank';
[1624]706
707  $result = pwg_query($query);
708
709  $cats = array();
[4325]710  while ($row = pwg_db_fetch_assoc($result))
[1624]711  {
[1860]712    $row['user_id'] = $userdata['id'];
[1641]713    $row['count_categories'] = 0;
[2324]714    $row['count_images'] = (int)$row['nb_images'];
715    $row['max_date_last'] = $row['date_last'];
[1860]716
[1624]717    $cats += array($row['cat_id'] => $row);
718  }
[3642]719  uasort($cats, 'global_rank_compare');
[1624]720
[1677]721  compute_categories_data($cats);
[1624]722
[1860]723  if ( isset($filter_days) )
[1624]724  {
[1651]725    $cat_tmp = $cats;
726    $cats = array();
[1860]727
[1677]728    foreach ($cat_tmp as $category)
[1651]729    {
[1677]730      if (!empty($category['max_date_last']))
[1651]731      {
[1677]732        // Re-init counters
733        $category['count_categories'] = 0;
[2324]734        $category['count_images'] = (int)$category['nb_images'];
[1677]735        // Keep category
736        $cats[$category['cat_id']] = $category;
[1651]737      }
738    }
[1677]739    // Compute a second time
740    compute_categories_data($cats);
[1651]741  }
[1677]742  return $cats;
743}
744
745/**
[808]746 * returns user identifier thanks to his name, false if not found
747 *
748 * @param string username
749 * @param int user identifier
750 */
751function get_userid($username)
752{
753  global $conf;
754
[4325]755  $username = pwg_db_real_escape_string($username);
[808]756
757  $query = '
758SELECT '.$conf['user_fields']['id'].'
759  FROM '.USERS_TABLE.'
760  WHERE '.$conf['user_fields']['username'].' = \''.$username.'\'
761;';
762  $result = pwg_query($query);
763
[4325]764  if (pwg_db_num_rows($result) == 0)
[808]765  {
766    return false;
767  }
768  else
769  {
[4325]770    list($user_id) = pwg_db_fetch_row($result);
[808]771    return $user_id;
772  }
773}
774
[11992]775function get_userid_by_email($email)
776{
777  global $conf;
778
779  $email = pwg_db_real_escape_string($email);
[13074]780
[11992]781  $query = '
782SELECT
783    '.$conf['user_fields']['id'].'
784  FROM '.USERS_TABLE.'
785  WHERE UPPER('.$conf['user_fields']['email'].') = UPPER(\''.$email.'\')
786;';
787  $result = pwg_query($query);
788
789  if (pwg_db_num_rows($result) == 0)
790  {
791    return false;
792  }
793  else
794  {
795    list($user_id) = pwg_db_fetch_row($result);
796    return $user_id;
797  }
798}
799
[1926]800/*
801 * Returns a array with default user value
[808]802 *
[1926]803 * @param convert_str allows to convert string value if necessary
[808]804 */
[1926]805function get_default_user_info($convert_str = true)
[808]806{
[3126]807  global $cache, $conf;
[2084]808
[3126]809  if (!isset($cache['default_user']))
[1926]810  {
[3126]811    $query = 'SELECT * FROM '.USER_INFOS_TABLE.
812            ' WHERE user_id = '.$conf['default_user_id'].';';
[1068]813
[1926]814    $result = pwg_query($query);
[4325]815    $cache['default_user'] = pwg_db_fetch_assoc($result);
[2084]816
[3126]817    if ($cache['default_user'] !== false)
[1930]818    {
[3126]819      unset($cache['default_user']['user_id']);
820      unset($cache['default_user']['status']);
821      unset($cache['default_user']['registration_date']);
[1930]822    }
[1926]823  }
[808]824
[3126]825  if (is_array($cache['default_user']) and $convert_str)
[1284]826  {
[18629]827    $default_user = $cache['default_user'];
828    foreach ($default_user as &$value)
[1926]829    {
[18629]830      // If the field is true or false, the variable is transformed into a boolean value.
831      if ($value == 'true')
[1926]832      {
[18629]833        $value = true;
[1926]834      }
[18629]835      elseif ($value == 'false')
[1926]836      {
[18629]837        $value = false;
[1926]838      }
839    }
840    return $default_user;
[1284]841  }
[1926]842  else
[1284]843  {
[3126]844    return $cache['default_user'];
[1284]845  }
[1926]846}
847
848/*
849 * Returns a default user value
850 *
851 * @param value_name: name of value
852 * @param sos_value: value used if don't exist value
853 */
854function get_default_user_value($value_name, $sos_value)
855{
856  $default_user = get_default_user_info(true);
[5271]857  if ($default_user === false or empty($default_user[$value_name]))
[1926]858  {
859    return $sos_value;
860  }
[1284]861  else
862  {
[1926]863   return $default_user[$value_name];
[1284]864  }
[1926]865}
[1567]866
[1926]867/*
868 * Returns the default template value
869 *
870 */
[5123]871function get_default_theme()
[1926]872{
[5982]873  $theme = get_default_user_value('theme', PHPWG_DEFAULT_TEMPLATE);
874  if (check_theme_installed($theme))
875  {
876    return $theme;
877  }
[13074]878
[5982]879  // let's find the first available theme
880  $active_themes = get_pwg_themes();
881  foreach (array_keys(get_pwg_themes()) as $theme_id)
882  {
[12670]883    return $theme_id;
[5982]884  }
[1926]885}
[808]886
[1926]887/*
888 * Returns the default language value
889 *
890 */
891function get_default_language()
892{
[2425]893  return get_default_user_value('language', PHPWG_DEFAULT_LANGUAGE);
[808]894}
[817]895
896/**
[2425]897  * Returns true if the browser language value is set into param $lang
[2411]898  *
899  */
[2425]900function get_browser_language(&$lang)
[2411]901{
[2572]902  $browser_language = substr(@$_SERVER["HTTP_ACCEPT_LANGUAGE"], 0, 2);
[2411]903  foreach (get_languages() as $language_code => $language_name)
904  {
905    if (substr($language_code, 0, 2) == $browser_language)
906    {
[2425]907      $lang = $language_code;
908      return true;
[2411]909    }
910  }
[2425]911  return false;
[2411]912}
913
914/**
[1926]915 * add user informations based on default values
916 *
917 * @param int user_id / array of user_if
[1930]918 * @param array of values used to override default user values
[1926]919 */
[1930]920function create_user_infos($arg_id, $override_values = null)
[1926]921{
922  global $conf;
923
924  if (is_array($arg_id))
925  {
926    $user_ids = $arg_id;
927  }
928  else
929  {
930    $user_ids = array();
[2229]931    if (is_numeric($arg_id))
[1926]932    {
933      $user_ids[] = $arg_id;
934    }
935  }
936
937  if (!empty($user_ids))
938  {
939    $inserts = array();
[4325]940    list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
[1926]941
942    $default_user = get_default_user_info(false);
943    if ($default_user === false)
944    {
945      // Default on structure are used
946      $default_user = array();
947    }
948
[1930]949    if (!is_null($override_values))
950    {
951      $default_user = array_merge($default_user, $override_values);
952    }
953
[1926]954    foreach ($user_ids as $user_id)
955    {
[2084]956      $level= isset($default_user['level']) ? $default_user['level'] : 0;
[1926]957      if ($user_id == $conf['webmaster_id'])
958      {
959        $status = 'webmaster';
[2084]960        $level = max( $conf['available_permission_levels'] );
[1926]961      }
[2084]962      else if (($user_id == $conf['guest_id']) or
[1926]963               ($user_id == $conf['default_user_id']))
964      {
965        $status = 'guest';
966      }
967      else
968      {
969        $status = 'normal';
970      }
971
[1930]972      $insert = array_merge(
973        $default_user,
[1926]974        array(
975          'user_id' => $user_id,
976          'status' => $status,
[2084]977          'registration_date' => $dbnow,
978          'level' => $level
[1930]979          ));
[1926]980
[18629]981      $inserts[] = $insert;
[2084]982    }
[1926]983
984    mass_inserts(USER_INFOS_TABLE, array_keys($inserts[0]), $inserts);
985  }
986}
987
988/**
[1622]989 * returns the auto login key or false on error
990 * @param int user_id
[2411]991 * @param time_t time
[1744]992 * @param string [out] username
[1622]993*/
[2409]994function calculate_auto_login_key($user_id, $time, &$username)
[1622]995{
996  global $conf;
997  $query = '
998SELECT '.$conf['user_fields']['username'].' AS username
999  , '.$conf['user_fields']['password'].' AS password
1000FROM '.USERS_TABLE.'
1001WHERE '.$conf['user_fields']['id'].' = '.$user_id;
1002  $result = pwg_query($query);
[4325]1003  if (pwg_db_num_rows($result) > 0)
[1622]1004  {
[4325]1005    $row = pwg_db_fetch_assoc($result);
[4304]1006    $username = stripslashes($row['username']);
[11826]1007    $data = $time.$user_id.$username;
1008    $key = base64_encode( hash_hmac('sha1', $data, $conf['secret_key'].$row['password'],true) );
[1622]1009    return $key;
1010  }
1011  return false;
1012}
1013
[1068]1014/*
1015 * Performs all required actions for user login
1016 * @param int user_id
1017 * @param bool remember_me
1018 * @return void
1019*/
1020function log_user($user_id, $remember_me)
1021{
[1511]1022  global $conf, $user;
[1493]1023
[1641]1024  if ($remember_me and $conf['authorize_remembering'])
[1068]1025  {
[2409]1026    $now = time();
1027    $key = calculate_auto_login_key($user_id, $now, $username);
[1622]1028    if ($key!==false)
[1493]1029    {
[2409]1030      $cookie = $user_id.'-'.$now.'-'.$key;
[2757]1031      if (version_compare(PHP_VERSION, '5.2', '>=') )
1032      {
1033        setcookie($conf['remember_me_name'],
[2409]1034            $cookie,
[2029]1035            time()+$conf['remember_me_length'],
[2757]1036            cookie_path(),ini_get('session.cookie_domain'),ini_get('session.cookie_secure'),
1037            ini_get('session.cookie_httponly')
[2029]1038          );
[2757]1039      }
1040      else
1041      {
1042        setcookie($conf['remember_me_name'],
1043            $cookie,
1044            time()+$conf['remember_me_length'],
1045            cookie_path(),ini_get('session.cookie_domain'),ini_get('session.cookie_secure')
1046          );
1047      }
[2029]1048    }
[1068]1049  }
[1568]1050  else
1051  { // make sure we clean any remember me ...
[2757]1052    setcookie($conf['remember_me_name'], '', 0, cookie_path(),ini_get('session.cookie_domain'));
[1568]1053  }
1054  if ( session_id()!="" )
[1622]1055  { // we regenerate the session for security reasons
1056    // see http://www.acros.si/papers/session_fixation.pdf
[6660]1057    session_regenerate_id(true);
[1568]1058  }
1059  else
1060  {
1061    session_start();
1062  }
[1622]1063  $_SESSION['pwg_uid'] = (int)$user_id;
[1511]1064
1065  $user['id'] = $_SESSION['pwg_uid'];
[1068]1066}
1067
[1070]1068/*
[1511]1069 * Performs auto-connexion when cookie remember_me exists
[1568]1070 * @return true/false
[1511]1071*/
[1567]1072function auto_login() {
[1511]1073  global $conf;
1074
[1568]1075  if ( isset( $_COOKIE[$conf['remember_me_name']] ) )
1076  {
[2409]1077    $cookie = explode('-', stripslashes($_COOKIE[$conf['remember_me_name']]));
[2411]1078    if ( count($cookie)===3
[2409]1079        and is_numeric(@$cookie[0]) /*user id*/
1080        and is_numeric(@$cookie[1]) /*time*/
1081        and time()-$conf['remember_me_length']<=@$cookie[1]
1082        and time()>=@$cookie[1] /*cookie generated in the past*/ )
[1568]1083    {
[2409]1084      $key = calculate_auto_login_key( $cookie[0], $cookie[1], $username );
1085      if ($key!==false and $key===$cookie[2])
[1622]1086      {
[2409]1087        log_user($cookie[0], true);
[4304]1088        trigger_action('login_success', stripslashes($username));
[1622]1089        return true;
1090      }
[1568]1091    }
[2757]1092    setcookie($conf['remember_me_name'], '', 0, cookie_path(),ini_get('session.cookie_domain'));
[1511]1093  }
[1568]1094  return false;
[1511]1095}
1096
[1744]1097/**
1098 * Tries to login a user given username and password (must be MySql escaped)
1099 * return true on success
1100 */
1101function try_log_user($username, $password, $remember_me)
1102{
[11737]1103  // we force the session table to be clean
1104  pwg_session_gc();
[13074]1105
[1744]1106  global $conf;
1107  // retrieving the encrypted password of the login submitted
1108  $query = '
1109SELECT '.$conf['user_fields']['id'].' AS id,
1110       '.$conf['user_fields']['password'].' AS password
1111  FROM '.USERS_TABLE.'
[4367]1112  WHERE '.$conf['user_fields']['username'].' = \''.pwg_db_real_escape_string($username).'\'
[1744]1113;';
[4325]1114  $row = pwg_db_fetch_assoc(pwg_query($query));
[1744]1115  if ($row['password'] == $conf['pass_convert']($password))
1116  {
1117    log_user($row['id'], $remember_me);
[4304]1118    trigger_action('login_success', stripslashes($username));
[1744]1119    return true;
1120  }
[4304]1121  trigger_action('login_failure', stripslashes($username));
[1744]1122  return false;
1123}
1124
[2757]1125/** Performs all the cleanup on user logout */
1126function logout_user()
1127{
1128  global $conf;
1129  $_SESSION = array();
1130  session_unset();
1131  session_destroy();
1132  setcookie(session_name(),'',0,
1133      ini_get('session.cookie_path'),
1134      ini_get('session.cookie_domain')
1135    );
1136  setcookie($conf['remember_me_name'], '', 0, cookie_path(),ini_get('session.cookie_domain'));
1137}
1138
[1511]1139/*
[2029]1140 * Return user status used in this library
1141 * @return string
[1070]1142*/
[2029]1143function get_user_status($user_status)
[1070]1144{
[2029]1145  global $user;
[1072]1146
[1854]1147  if (empty($user_status))
[1075]1148  {
[1854]1149    if (isset($user['status']))
1150    {
1151      $user_status = $user['status'];
1152    }
1153    else
1154    {
1155      // swicth to default value
1156      $user_status = '';
1157    }
[1075]1158  }
[2029]1159  return $user_status;
1160}
[1075]1161
[2029]1162/*
[2325]1163 * Return access_type definition of user
[2029]1164 * Test does with user status
1165 * @return bool
1166*/
1167function get_access_type_status($user_status='')
1168{
1169  global $conf;
1170
1171  switch (get_user_status($user_status))
[1072]1172  {
[1075]1173    case 'guest':
[1851]1174    {
[1854]1175      $access_type_status =
[2325]1176        ($conf['guest_access'] ? ACCESS_GUEST : ACCESS_FREE);
[1851]1177      break;
1178    }
[1075]1179    case 'generic':
[1072]1180    {
[1075]1181      $access_type_status = ACCESS_GUEST;
1182      break;
[1072]1183    }
[1075]1184    case 'normal':
1185    {
1186      $access_type_status = ACCESS_CLASSIC;
1187      break;
1188    }
1189    case 'admin':
1190    {
1191      $access_type_status = ACCESS_ADMINISTRATOR;
1192      break;
1193    }
1194    case 'webmaster':
1195    {
1196      $access_type_status = ACCESS_WEBMASTER;
1197      break;
1198    }
[2221]1199    default:
[1854]1200    {
[2325]1201      $access_type_status = ACCESS_FREE;
[2221]1202      break;
[1854]1203    }
[1072]1204  }
1205
[1085]1206  return $access_type_status;
[1070]1207}
1208
[1072]1209/*
[1085]1210 * Return if user have access to access_type definition
1211 * Test does with user status
1212 * @return bool
1213*/
[1851]1214function is_autorize_status($access_type, $user_status = '')
[1085]1215{
[1851]1216  return (get_access_type_status($user_status) >= $access_type);
[1085]1217}
1218
1219/*
1220 * Check if user have access to access_type definition
[1072]1221 * Stop action if there are not access
1222 * Test does with user status
1223 * @return none
1224*/
[1851]1225function check_status($access_type, $user_status = '')
[1072]1226{
[1851]1227  if (!is_autorize_status($access_type, $user_status))
[1072]1228  {
[1113]1229    access_denied();
[1072]1230  }
1231}
1232
1233/*
[2163]1234 * Return if user is generic
[1072]1235 * @return bool
1236*/
[2163]1237 function is_generic($user_status = '')
[2029]1238{
[2163]1239  return get_user_status($user_status) == 'generic';
[2029]1240}
1241
1242/*
[2163]1243 * Return if user is only a guest
[2161]1244 * @return bool
1245*/
[2163]1246 function is_a_guest($user_status = '')
[2161]1247{
[2163]1248  return get_user_status($user_status) == 'guest';
[2161]1249}
[2163]1250
[2161]1251/*
[2029]1252 * Return if user is, at least, a classic user
1253 * @return bool
1254*/
1255 function is_classic_user($user_status = '')
1256{
1257  return is_autorize_status(ACCESS_CLASSIC, $user_status);
1258}
1259
1260/*
1261 * Return if user is, at least, an administrator
1262 * @return bool
1263*/
[1851]1264 function is_admin($user_status = '')
[1072]1265{
[1851]1266  return is_autorize_status(ACCESS_ADMINISTRATOR, $user_status);
[1072]1267}
1268
[1085]1269/*
[5272]1270 * Return if user is, at least, a webmaster
1271 * @return bool
1272*/
1273 function is_webmaster($user_status = '')
1274{
1275  return is_autorize_status(ACCESS_WEBMASTER, $user_status);
1276}
1277
1278/*
[8126]1279 * Adviser status is depreciated from piwigo 2.2
1280 * @return false
[1085]1281*/
1282function is_adviser()
1283{
[12339]1284  // TODO for Piwigo 2.4 : trigger a warning. We don't do it on Piwigo 2.3
1285  // to avoid changes for plugin contributors
1286  // trigger_error('call to obsolete function is_adviser', E_USER_WARNING);
[8126]1287  return false;
[1085]1288}
[1458]1289
1290/*
[5195]1291 * Return if current user can edit/delete/validate a comment
1292 * @param action edit/delete/validate
[3445]1293 * @return bool
1294 */
[3622]1295function can_manage_comment($action, $comment_author_id)
[3445]1296{
[5195]1297  global $user, $conf;
[13074]1298
[5195]1299  if (is_a_guest())
1300  {
[3445]1301    return false;
1302  }
[13074]1303
[5195]1304  if (!in_array($action, array('delete','edit', 'validate')))
1305  {
1306    return false;
1307  }
1308
1309  if (is_admin())
1310  {
1311    return true;
1312  }
1313
1314  if ('edit' == $action and $conf['user_can_edit_comment'])
1315  {
1316    if ($comment_author_id == $user['id']) {
1317      return true;
1318    }
1319  }
1320
1321  if ('delete' == $action and $conf['user_can_delete_comment'])
1322  {
1323    if ($comment_author_id == $user['id']) {
1324      return true;
1325    }
1326  }
1327
1328  return false;
[3445]1329}
1330
1331/*
[1458]1332 * Return mail address as display text
1333 * @return string
1334*/
1335function get_email_address_as_display_text($email_address)
1336{
1337  global $conf;
1338
[1462]1339  if (!isset($email_address) or (trim($email_address) == ''))
[1458]1340  {
[1462]1341    return '';
[1458]1342  }
1343  else
1344  {
[8126]1345    return $email_address;
[1458]1346  }
1347}
1348
[1677]1349/*
[1817]1350 * Compute sql where condition with restrict and filter data. "FandF" means
1351 * Forbidden and Filters.
[1677]1352 *
[1817]1353 * @param array condition_fields: read function body
1354 * @param string prefix_condition: prefixes sql if condition is not empty
1355 * @param boolean force_one_condition: use at least "1 = 1"
[1677]1356 *
1357 * @return string sql where/conditions
1358 */
[1817]1359function get_sql_condition_FandF(
1360  $condition_fields,
1361  $prefix_condition = null,
1362  $force_one_condition = false
1363  )
[1677]1364{
1365  global $user, $filter;
1366
1367  $sql_list = array();
1368
1369  foreach ($condition_fields as $condition => $field_name)
1370  {
1371    switch($condition)
1372    {
1373      case 'forbidden_categories':
[1817]1374      {
[1677]1375        if (!empty($user['forbidden_categories']))
1376        {
[1817]1377          $sql_list[] =
1378            $field_name.' NOT IN ('.$user['forbidden_categories'].')';
[1677]1379        }
1380        break;
[1817]1381      }
[1677]1382      case 'visible_categories':
[1817]1383      {
[1677]1384        if (!empty($filter['visible_categories']))
1385        {
[1817]1386          $sql_list[] =
1387            $field_name.' IN ('.$filter['visible_categories'].')';
[1677]1388        }
1389        break;
[1817]1390      }
[1677]1391      case 'visible_images':
1392        if (!empty($filter['visible_images']))
1393        {
[1817]1394          $sql_list[] =
1395            $field_name.' IN ('.$filter['visible_images'].')';
[1677]1396        }
[2084]1397        // note there is no break - visible include forbidden
1398      case 'forbidden_images':
1399        if (
1400            !empty($user['image_access_list'])
1401            or $user['image_access_type']!='NOT IN'
1402            )
1403        {
1404          $table_prefix=null;
1405          if ($field_name=='id')
1406          {
1407            $table_prefix = '';
1408          }
1409          elseif ($field_name=='i.id')
1410          {
1411            $table_prefix = 'i.';
1412          }
1413          if ( isset($table_prefix) )
1414          {
1415            $sql_list[]=$table_prefix.'level<='.$user['level'];
1416          }
1417          else
1418          {
1419            $sql_list[]=$field_name.' '.$user['image_access_type']
1420                .' ('.$user['image_access_list'].')';
1421          }
1422        }
[1677]1423        break;
1424      default:
[1817]1425      {
[1677]1426        die('Unknow condition');
1427        break;
[1817]1428      }
[1677]1429    }
1430  }
1431
1432  if (count($sql_list) > 0)
1433  {
1434    $sql = '('.implode(' AND ', $sql_list).')';
1435  }
1436  else
1437  {
[2824]1438    $sql = $force_one_condition ? '1 = 1' : '';
[1677]1439  }
1440
1441  if (isset($prefix_condition) and !empty($sql))
1442  {
1443    $sql = $prefix_condition.' '.$sql;
1444  }
1445
1446  return $sql;
1447}
1448
[11992]1449/**
1450 * search an available activation_key
1451 *
1452 * @return string
1453 */
1454function get_user_activation_key()
1455{
1456  while (true)
1457  {
1458    $key = generate_key(20);
1459    $query = '
1460SELECT COUNT(*)
1461  FROM '.USER_INFOS_TABLE.'
1462  WHERE activation_key = \''.$key.'\'
1463;';
1464    list($count) = pwg_db_fetch_row(pwg_query($query));
1465    if (0 == $count)
1466    {
1467      return $key;
1468    }
1469  }
1470}
1471
[12339]1472?>
Note: See TracBrowser for help on using the repository browser.