source: extensions/external_connection/main.inc.php @ 30302

Last change on this file since 30302 was 4726, checked in by vdigital, 15 years ago

Just as a sample (done for a school)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1<?php
2/*
3Plugin Name: External Connection
4Version: 2.0.7.a
5Description: High school connection - Don't DEACTIVATE !!!
6*/
7/*
8PREREQs: (Maybe an admin interface would become a must).
9
101 - Mandatory API parameter to define in your private LOCAL configuration file
11 (ie. ./include/config_local.inc.php   and NEVER config_global)
12Like this:
13$conf['external_connection_api'] = 'http://website.api/api.php?user=%s&pass=%s&type=echodata';
14where user=%s will provide the user parameter to the API
15and pass=%s will provide the encoded password
16type=blahblahblah is any additionnal parameter
17
182 - Optional - Encoded password
19Default is MD5.
20If you need to encode it create a global static function (eg. main_convert_from_external)
21which will return the encoded password. Parameter is original clear password.
22$conf['pass_convert'] = create_function('$s', 'global $row; return main_convert_from_external($s);');
23
24LOGIC: (A bit complex due to the ID provided logic).
25
261 - Try to connect to Piwigo as usual
272 - On failure the handler would take over.
28
293 - On logon failure now, call the API following its ruleset
30in the present case, the school API returns Pseudo and ID which could be different for other APIs
314 - If not found by the API, do nothing (see else final logic)
32
335 - Yes recognised, now.
34Because we have the ID forced (could be different with other APIs), we should try to register in Piwigo.
35Try to register (normaly only new users are there but consider that the Pseudo could be changed and it was the case once a year max).
36(See (*) below)
37
38
396 - Register is ok. Logon and redirect.
40
41Else (4 and 6) we are in a failure exception process, so just return to the normal failure process.
42
43(*): This API provides an ID so...
44It can provide ID 1 and 2 (which are by default respectively the webmaster-id and the guest-id for anonymous access).
45So the first step is to get from the API provider in such case...
46You id (to become the webmaster-id of Piwigo) and another id (to become the guest-id).
47Then connect you with each of them, just to create their account in Piwigo.
48See in admin Identification > Users their new lines.
49Move over their profile icons just to get their IDs (comming from the API).
50Supposed to be 1234 and 5678 for following statements.
51SET your user status as Admin.
52
53define in your private LOCAL configuration file
54 (ie. ./include/config_local.inc.php   and NEVER config_global)
55Like this:
56$conf['guest_id'] = 5678;
57$conf['default_user_id'] = $conf['guest_id'];
58$conf['webmaster_id'] = 1234;
59
60Close to be finished:
61Connect you with you user (1234).
62See in admin Identification > Users
63The old webmaster is an Admin
64The old guest is a normal registered user.
65Delete them (or at least change the status of the old webmaster as user).
66
67That's it.
68*/
69
70/* Here already in step 1 */
71if (!defined('PHPWG_ROOT_PATH')) die ("Hacking attempt!");
72
73global $conf, $row;
74  $conf['allow_user_registration'] = false;
75  if (!isset($conf['external_connection_api'])) 
76    die("The API parameter is NOT defined \$conf['external_connection_api']. Please see ./plugins/external_connection/main.inc.php comments.");
77
78/* step 2 (creation) */
79add_event_handler('login_failure', 'try_external_identification');
80
81function try_external_identification($username)
82{
83  global $conf, $redirect_to, $remember_me;
84
85/* step 3 (call the external API) */
86  $external_url = sprintf($conf['external_connection_api'], addslashes($username), md5(addslashes($_POST['password'])));
87  $fp = fopen($external_url,'r');
88  $d = fgets($fp);
89  $g = split("//",$d);
90  $userid = (int)($g[0]); //$userid=32; #for local testing
91  fclose($fp);
92
93  if ($userid > 0)
94  { /* step 5 (register) */
95    $error = register_external_user($userid, $username, $_POST['password'], '');
96    if (empty($error))
97    { /* step 6 (Logon) */
98      log_user($userid, $remember_me);
99      redirect(empty($redirect_to) ? make_index_url() : $redirect_to);
100    }
101  }
102/* step 4 (do nothing) */
103}
104
105/* The orginal registration function has been reviewed to force an external provided id
106   and to be able to change the pseudo (no duplicate pseudo in this particular case) */
107function register_external_user($next_id, $login, $password, $mail_address,
108  $with_notification = true, $errors = array())
109{
110  global $conf;
111  if ($login == '') array_push($errors, l10n('reg_err_login1'));
112  if (preg_match('/^.* $/', $login)) array_push($errors, l10n('reg_err_login2'));
113  if (preg_match('/^ .*$/', $login)) array_push($errors, l10n('reg_err_login3'));
114  if (get_userid($login)) array_push($errors, l10n('reg_err_login5'));
115  $mail_error = validate_mail_address(null, $mail_address);
116  if ('' != $mail_error) array_push($errors, $mail_error);
117  $errors = trigger_event('register_user_check',
118              $errors, array(
119                'username'=>$login,
120                'password'=>$password,
121                'email'=>$mail_address,
122              ));
123
124  // if no error until here, registration of the user
125  if (count($errors) == 0) {
126    $query = 'REPLACE INTO piwigo_users
127  (id,username,password,mail_address)
128  VALUES(' . $next_id . ',\'' . mysql_real_escape_string($login) . '\',\'' . $conf['pass_convert']($password) . '\',NULL);';
129    $result = pwg_query($query);
130
131    // Assign by default groups
132      $query = 'SELECT id
133  FROM '.GROUPS_TABLE.'
134  WHERE is_default = \''.boolean_to_string(true).'\'
135  ORDER BY id ASC;';
136      $result = pwg_query($query);
137      $inserts = array();
138      while ($row = mysql_fetch_array($result))
139      {
140        array_push($inserts,
141          array(
142            'user_id' => $next_id,
143            'group_id' => $row['id']
144          ));
145      }
146
147    if (count($inserts) != 0) {
148      include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
149      mass_inserts(USER_GROUP_TABLE, array('user_id', 'group_id'), $inserts);
150    }
151
152    $num_infos = mysql_num_rows(pwg_query('SELECT user_id
153  FROM '.USER_INFOS_TABLE.' WHERE user_id = \''.$next_id.'\''));
154    if ($num_infos == 0) create_user_infos($next_id);
155
156    if ($with_notification and $num_infos == 0 and $conf['email_admin_on_new_user']) {
157      include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
158      $admin_url = get_absolute_root_url()
159                   .'admin.php?page=user_list&username='.$login;
160      $keyargs_content = array(
161        get_l10n_args('User: %s', $login),
162        get_l10n_args('Email: %s', $_POST['mail_address']),
163        get_l10n_args('', ''),
164        get_l10n_args('Admin: %s', $admin_url)
165      );
166      pwg_mail_notification_admins(
167        get_l10n_args('Registration of %s', $login),
168        $keyargs_content
169      );
170    }
171    trigger_action('register_user', array(
172        'id'=>$next_id,
173        'username'=>$login,
174        'email'=>$mail_address,
175       ));
176  }
177  return $errors;
178}
179
180?>
Note: See TracBrowser for help on using the repository browser.