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

Last change on this file since 28587 was 28587, checked in by mistic100, 10 years ago

feature 3010 : replace trigger_action/event by trigger_notify/change

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