forked from civicrm/civicrm-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
189 lines (159 loc) · 5.18 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
/**
* Utilties for Drupal 7 compatibility
*/
function _civicrm_get_user_table_name() {
if (function_exists('db_select')) {
//docs say 'user', but not the schema in alpha 3.
$user_tab = 'users';
}
else {
$user_tab = 'users';
}
}
/**
* Create a Drupal user and return Drupal ID
*
* @param email email address of new user
*
* @return res Drupal ID for new user or FALSE if error
*/
function civicrm_drupal_create_user($email, $rid = NULL) {
$email = trim($email);
if (empty($email)) {
return FALSE;
}
$user_tab = _civicrm_get_user_table_name();
// If user already exists, return Drupal id
$uid = db_result(db_query("SELECT uid FROM {$user_tab} WHERE mail = '%s'", $email));
if ($uid) {
return $uid;
}
// escape email to prevent sql injection
$dao = new CRM_Core_DAO();
$email = $dao->escape($email);
// Default values for new user
$params = array();
//WARNING -- this is likely *wrong* since it will crash Drupal 6.
//calling conventions for Drupal 7 are different, as well.
//$params['uid'] = db_next_id('{users}_uid');
$params['name'] = $email;
$params['pass'] = md5(uniqid(rand(), TRUE));
$params['mail'] = $email;
$params['mode'] = 0;
$params['access'] = 0;
// don't allow user to login until verified
$params['status'] = 0;
$params['init'] = $email;
$params['created'] = time();
$db_fields = '(';
$db_values = '(';
foreach ($params as $key => $value) {
$db_fields .= "$key,";
$db_values .= "'$value',";
}
$db_fields = rtrim($db_fields, ",");
$db_values = rtrim($db_values, ",");
$db_fields .= ')';
$db_values .= ')';
$q = "INSERT INTO {$user_tab} $db_fields VALUES $db_values";
db_query($q);
if ($rid) {
// Delete any previous roles entry before adding the role id
//NOTE: weirdly, D7 schema from alpha 3 allows the following:
db_query('DELETE FROM {users_roles} WHERE uid = %d', $params['uid']);
db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $params['uid'], $rid);
}
return $params['uid'];
}
/**
* Get the role id for a given name
*
* @param string $name name of the role
*
* @return int the role id
* @static
*/
function civicrm_drupal_role_id($name) {
$roleIDs = user_roles();
$roleNames = array_flip($roleIDs);
return array_key_exists($name, $roleNames) ? $roleNames[$name] : NULL;
}
/**
* Check status of Drupal user
*
* @param id Drupal ID of user
*
* @return status Status of user
*/
function civicrm_drupal_is_user_verified($id) {
if (!$id) {
return FALSE;
}
$params = array();
$params['uid'] = $id;
$user = user_load($params);
if (!$user->uid) {
return FALSE;
}
return $user->status;
}
/**
* Verify user and update user's status
*
* @param params User fields, includes email
*/
function civicrm_drupal_user_update_and_redirect($params) {
global $user;
if (!($params['email'] && $params['drupalID'] && $params['password'])) {
return FALSE;
}
$user_fields['uid'] = $params['drupalID'];
$user_fields['mail'] = $params['email'];
$user = user_load($user_fields);
if (!$user->uid) {
return FALSE;
}
$update = array();
$update['status'] = 1;
$update['pass'] = $params['password'];
$user = user_save($user, $update);
// Login the user
$edit = array();
user_module_invoke('login', $edit, $user);
// redirect user to locker
drupal_goto('locker');
}
//end func civicrm_drupal_user_update_and_redirect