| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | require_once(IA_ROOT_DIR."common/db/db.php"); |
|---|
| 4 | |
|---|
| 5 | // Various routines used for tight SMF integration. |
|---|
| 6 | // |
|---|
| 7 | // There is no decent way of linking native SMF APIs so we resort |
|---|
| 8 | // to duplication and hard-coding. |
|---|
| 9 | // These must be updated every time we switch SMF distributions. |
|---|
| 10 | |
|---|
| 11 | // Creates SMF user from a regular infoarena user. |
|---|
| 12 | // Returns newly created user id. |
|---|
| 13 | function smf_create_user($ia_user) { |
|---|
| 14 | $fields = array( |
|---|
| 15 | 'memberName' => $ia_user['username'], |
|---|
| 16 | 'dateRegistered' => time(), |
|---|
| 17 | //'posts' => 0, |
|---|
| 18 | //'lngfile' => '', |
|---|
| 19 | 'realName' => $ia_user['full_name'], |
|---|
| 20 | //'pm_ignore_list' => '', |
|---|
| 21 | |
|---|
| 22 | // NOTE: We copy the password verbatim since ia2 uses same password |
|---|
| 23 | // hasing scheme as SMF |
|---|
| 24 | 'passwd' => $ia_user['password'], |
|---|
| 25 | |
|---|
| 26 | 'emailAddress' => $ia_user['email'], |
|---|
| 27 | //'personalText' => '', |
|---|
| 28 | //'websiteTitle' => '', |
|---|
| 29 | //'websiteUrl' => '', |
|---|
| 30 | 'location' => getattr($ia_user, 'city'), |
|---|
| 31 | //'ICQ' => '', |
|---|
| 32 | //'AIM' => '', |
|---|
| 33 | //'YIM' => '', |
|---|
| 34 | //'MSN' => '', |
|---|
| 35 | //'timeFormat' => '', |
|---|
| 36 | //'signature' => '', |
|---|
| 37 | //'avatar' => '', |
|---|
| 38 | 'pm_email_notify' => 1, |
|---|
| 39 | //'usertitle' => '', |
|---|
| 40 | //'memberIP' => '', |
|---|
| 41 | //'secretQuestion' => '', |
|---|
| 42 | //'secretAnswer' => '', |
|---|
| 43 | 'is_activated' => "1", |
|---|
| 44 | //'validation_code' => '', |
|---|
| 45 | //'additionalGroups' => '', |
|---|
| 46 | //'smileySet' => '', |
|---|
| 47 | //'passwordSalt' => '\'' . substr(md5(rand()), 0, 4) . '\'', |
|---|
| 48 | //'messageLabels' => '', |
|---|
| 49 | //'buddy_list' => '', |
|---|
| 50 | //'memberIP2' => '', |
|---|
| 51 | |
|---|
| 52 | // ID_GROUP 1 is forum administrator |
|---|
| 53 | 'ID_GROUP' => ('admin' == $ia_user['security_level'] ? 1 : null), |
|---|
| 54 | ); |
|---|
| 55 | |
|---|
| 56 | $user_id = db_insert(IA_SMF_DB_PREFIX.'members', $fields); |
|---|
| 57 | smf_update_member_stats(); |
|---|
| 58 | return $user_id; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | // Adapted from updateStats function in smf/Sources/Subs.php |
|---|
| 62 | function smf_update_member_stats() { |
|---|
| 63 | $changes = array( |
|---|
| 64 | 'memberlist_updated' => time(), |
|---|
| 65 | ); |
|---|
| 66 | |
|---|
| 67 | // Get the latest member (highest ID_MEMBER) and count. |
|---|
| 68 | list($changes['totalMembers'], $changes['latestMember']) = array_values( |
|---|
| 69 | db_fetch(" |
|---|
| 70 | SELECT COUNT(*), MAX(ID_MEMBER) |
|---|
| 71 | FROM `" . IA_SMF_DB_PREFIX . "members`" |
|---|
| 72 | ) |
|---|
| 73 | ); |
|---|
| 74 | |
|---|
| 75 | // Get the latest member's display name. |
|---|
| 76 | list($changes['latestRealName']) = array_values( |
|---|
| 77 | db_fetch(" |
|---|
| 78 | SELECT realName |
|---|
| 79 | FROM `" . IA_SMF_DB_PREFIX . "members` |
|---|
| 80 | WHERE ID_MEMBER = " . (int)$changes['latestMember'] . " |
|---|
| 81 | LIMIT 1" |
|---|
| 82 | ) |
|---|
| 83 | ); |
|---|
| 84 | |
|---|
| 85 | // Update smf settings table. |
|---|
| 86 | // SMF also clears it's $modSettings cache at this point |
|---|
| 87 | // but, since infoarena and SMF don't use the same caching |
|---|
| 88 | // engine, I don't know how to handle this. The cache timeout |
|---|
| 89 | // is short though so it's ok to leave it like this. |
|---|
| 90 | foreach ($changes as $variable => $value) |
|---|
| 91 | { |
|---|
| 92 | db_query(" |
|---|
| 93 | UPDATE `" . IA_SMF_DB_PREFIX . "settings` |
|---|
| 94 | SET value = " . db_quote($value) . " |
|---|
| 95 | WHERE variable = " . db_quote($variable) . " |
|---|
| 96 | LIMIT 1" |
|---|
| 97 | ); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | // Updates SMF user information from a regular infoarena user. |
|---|
| 102 | function smf_update_user($ia_user) { |
|---|
| 103 | $fields = array( |
|---|
| 104 | 'memberName' => $ia_user['username'], |
|---|
| 105 | //'dateRegistered' => time(), |
|---|
| 106 | //'posts' => 0, |
|---|
| 107 | //'lngfile' => '', |
|---|
| 108 | 'realName' => $ia_user['full_name'], |
|---|
| 109 | //'pm_ignore_list' => '', |
|---|
| 110 | |
|---|
| 111 | // NOTE: We copy the password verbatim since ia2 uses same password |
|---|
| 112 | // hasing scheme as SMF |
|---|
| 113 | 'passwd' => $ia_user['password'], |
|---|
| 114 | |
|---|
| 115 | 'emailAddress' => $ia_user['email'], |
|---|
| 116 | //'personalText' => '', |
|---|
| 117 | //'websiteTitle' => '', |
|---|
| 118 | //'websiteUrl' => '', |
|---|
| 119 | 'location' => getattr($ia_user, 'city'), |
|---|
| 120 | //'ICQ' => '', |
|---|
| 121 | //'AIM' => '', |
|---|
| 122 | //'YIM' => '', |
|---|
| 123 | //'MSN' => '', |
|---|
| 124 | //'timeFormat' => '', |
|---|
| 125 | //'signature' => '', |
|---|
| 126 | //'avatar' => '', |
|---|
| 127 | //'pm_email_notify' => 1, |
|---|
| 128 | //'usertitle' => '', |
|---|
| 129 | //'memberIP' => '', |
|---|
| 130 | //'secretQuestion' => '', |
|---|
| 131 | //'secretAnswer' => '', |
|---|
| 132 | //'is_activated' => "1", |
|---|
| 133 | //'validation_code' => '', |
|---|
| 134 | //'additionalGroups' => '', |
|---|
| 135 | //'smileySet' => '', |
|---|
| 136 | //'passwordSalt' => '\'' . substr(md5(rand()), 0, 4) . '\'', |
|---|
| 137 | //'messageLabels' => '', |
|---|
| 138 | //'buddy_list' => '', |
|---|
| 139 | //'memberIP2' => '', |
|---|
| 140 | ); |
|---|
| 141 | |
|---|
| 142 | $smf_user = smf_get_member_by_name($ia_user['username']); |
|---|
| 143 | |
|---|
| 144 | $additional_groups = array(); |
|---|
| 145 | if (strlen($smf_user['additional_groups']) > 0) { |
|---|
| 146 | $additional_groups = explode(',', $smf_user['additional_groups']); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // Check if user is a smf admin |
|---|
| 150 | // SMF holds the ID of the first group in id_group |
|---|
| 151 | // and the others in additional_groups separated by , (comma) |
|---|
| 152 | $is_smf_admin = false; |
|---|
| 153 | if ($smf_user['id_group'] == 1) { |
|---|
| 154 | $is_smf_admin = true; |
|---|
| 155 | } |
|---|
| 156 | if (in_array(1, $additional_groups)) { |
|---|
| 157 | $is_smf_admin = true; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // If user is admin on IA but not on SMF he is promoted |
|---|
| 161 | if ($ia_user['security_level'] == 'admin' && !$is_smf_admin) { |
|---|
| 162 | $fields['ID_GROUP'] = 1; |
|---|
| 163 | |
|---|
| 164 | if ($smf_user['id_group'] != 0) { |
|---|
| 165 | $additional_groups[] = $smf_user['id_group']; |
|---|
| 166 | } |
|---|
| 167 | $fields['additionalGroups'] = implode(',', $additional_groups); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | // If user is admin on smf but not on IA he is demoted |
|---|
| 171 | if ($ia_user['security_level'] != 'admin' && $is_smf_admin) { |
|---|
| 172 | if ($smf_user['id_group'] == '1') { |
|---|
| 173 | $fields['ID_GROUP'] = 0; |
|---|
| 174 | } else { |
|---|
| 175 | $groups = ''; |
|---|
| 176 | foreach ($additional_groups as $group) { |
|---|
| 177 | // Strip group 1 from additionalGroups |
|---|
| 178 | if ($group == 1) { |
|---|
| 179 | continue; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | if (strlen($groups) > 0) { |
|---|
| 183 | $groups .= ','; |
|---|
| 184 | } |
|---|
| 185 | $groups .= $group; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | $fields['additionalGroups'] = $groups; |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | $where = sprintf("memberName='%s'", db_escape($ia_user['username'])); |
|---|
| 193 | $res = db_update(IA_SMF_DB_PREFIX.'members', $fields, $where); |
|---|
| 194 | |
|---|
| 195 | log_assert(1 >= $res, "smf_update_user() affected multiple rows in table " |
|---|
| 196 | ."ia_user! Needs serious attention!"); |
|---|
| 197 | return $res; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | // Returns SMF member id from infoarena username |
|---|
| 201 | function smf_get_member_id($username) { |
|---|
| 202 | $prefix = IA_SMF_DB_PREFIX; |
|---|
| 203 | $query = " |
|---|
| 204 | SELECT ID_MEMBER FROM {$prefix}members |
|---|
| 205 | WHERE memberName = '%s' |
|---|
| 206 | "; |
|---|
| 207 | return db_query_value(sprintf($query, db_escape($username))); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | function smf_get_member_by_name($username) { |
|---|
| 211 | $prefix = IA_SMF_DB_PREFIX; |
|---|
| 212 | $query = " |
|---|
| 213 | SELECT ID_MEMBER AS id, memberName AS user_name, |
|---|
| 214 | ID_GROUP AS id_group, additionalGroups AS additional_groups |
|---|
| 215 | FROM {$prefix}members |
|---|
| 216 | WHERE memberName = '".db_escape($username)."' |
|---|
| 217 | "; |
|---|
| 218 | |
|---|
| 219 | return db_fetch($query); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | // Counts the number of unread personal messages |
|---|
| 223 | function smf_get_pm_count($username) { |
|---|
| 224 | $from_cache = mem_cache_get("smf-new-pm-".$username); |
|---|
| 225 | |
|---|
| 226 | if ($from_cache !== false) { |
|---|
| 227 | return $from_cache; |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | $user_smf_id = smf_get_member_id($username); |
|---|
| 231 | $prefix = IA_SMF_DB_PREFIX; |
|---|
| 232 | $query = " |
|---|
| 233 | SELECT COUNT(*) FROM {$prefix}pm_recipients |
|---|
| 234 | WHERE ID_MEMBER = ".$user_smf_id." AND is_read = 0 |
|---|
| 235 | "; |
|---|
| 236 | |
|---|
| 237 | // Cache value |
|---|
| 238 | $new_pm_count = db_query_value($query); |
|---|
| 239 | mem_cache_set("smf-new-pm-".$username, $new_pm_count, 600); |
|---|
| 240 | return $new_pm_count; |
|---|
| 241 | } |
|---|
| 242 | ?> |
|---|