Merge commit 'origin/fusionax' into account
[platal.git] / include / user.func.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ function user_clear_all_subs()
23 /** kills the inscription of a user.
24 * we still keep his birthdate, adresses, and personnal stuff
25 * kills the entreprises, mentor, emails and lists subscription stuff
26 */
27 function user_clear_all_subs($user_id, $really_del=true)
28 {
29 // keep datas in : aliases, adresses, tels, profile_education, binets_ins, contacts, groupesx_ins, homonymes, identification_ax, photo
30 // delete in : competences_ins, emails, entreprises, langues_ins, mentor,
31 // mentor_pays, mentor_secteurs, newsletter_ins, perte_pass, requests, user_changes, virtual_redirect, watch_sub
32 // + delete maillists
33
34 global $globals;
35 $uid = intval($user_id);
36 $user = User::getSilent($uid);
37 list($alias) = explode('@', $user->forlifeEmail());
38
39 // TODO: clear profile.
40 $tables_to_clear = array('uid' => array('competences_ins', 'profile_job', 'langues_ins', 'profile_mentor_country',
41 'profile_mentor_sector', 'profile_mentor', 'perte_pass', 'watch_sub'),
42 'user_id' => array('requests', 'user_changes'));
43
44 if ($really_del) {
45 array_push($tables_to_clear['uid'], 'emails', 'groupex.membres', 'contacts', 'adresses', 'profile_phones',
46 'photo', 'perte_pass', 'langues_ins', 'forum_subs', 'forum_profiles');
47 array_push($tables_to_clear['user_id'], 'newsletter_ins', 'binets_ins');
48 $tables_to_clear['id'] = array('aliases');
49 $tables_to_clear['contact'] = array('contacts');
50 XDB::execute("UPDATE accounts
51 SET registration_date = 0, state = 'pending', password = NULL, weak_password = NULL, token = NULL, is_admin = 0
52 WHERE uid = {?}", $uid);
53 XDB::execute("DELETE virtual.* FROM virtual INNER JOIN virtual_redirect AS r USING(vid) WHERE redirect = {?}",
54 $alias.'@'.$globals->mail->domain);
55 XDB::execute("DELETE virtual.* FROM virtual INNER JOIN virtual_redirect AS r USING(vid) WHERE redirect = {?}",
56 $alias.'@'.$globals->mail->domain2);
57 } else {
58 XDB::execute("UPDATE accounts
59 SET password = NULL, weak_password = NULL, token = NULL
60 WHERE uid = {?}", $uid);
61 }
62
63 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain);
64 XDB::execute("DELETE FROM virtual_redirect WHERE redirect = {?}", $alias.'@'.$globals->mail->domain2);
65
66 /* TODO: handle both account and profile
67 foreach ($tables_to_clear as $key=>&$tables) {
68 foreach ($tables as $table) {
69 XDB::execute("DELETE FROM $table WHERE $key={?}", $uid);
70 }
71 }*/
72
73 $mmlist = new MMList($user);
74 $mmlist->kill($alias, $really_del);
75
76 // Deactivates, when available, the Google Apps account of the user.
77 if ($globals->mailstorage->googleapps_domain) {
78 require_once 'googleapps.inc.php';
79 if (GoogleAppsAccount::account_status($uid)) {
80 $account = new GoogleAppsAccount($user);
81 $account->suspend();
82 }
83 }
84 }
85
86 // }}}
87 // {{{ function _user_reindex
88
89 function _user_reindex($uid, $keys)
90 {
91 foreach ($keys as $i => $key) {
92 if ($key['name'] == '') {
93 continue;
94 }
95 $toks = preg_split('/[ \'\-]+/', $key['name']);
96 $token = "";
97 $first = 5;
98 while ($toks) {
99 $token = strtolower(replace_accent(array_pop($toks) . $token));
100 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
101 XDB::execute("REPLACE INTO search_name (token, uid, soundex, score, flags)
102 VALUES ({?}, {?}, {?}, {?}, {?})",
103 $token, $uid, soundex_fr($token), $score, $key['public']);
104 $first = 0;
105 }
106 }
107 }
108
109 // }}}
110 // {{{ function user_reindex
111
112 function user_reindex($uid) {
113 XDB::execute("DELETE FROM search_name
114 WHERE uid = {?}",
115 $uid);
116 $res = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
117 FIND_IN_SET('public', e.flags) AS public
118 FROM profile_name AS n
119 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
120 WHERE n.pid = {?}",
121 $uid);
122 _user_reindex($uid, $res);
123 }
124
125 // }}}
126 // {{{ function get_X_mat
127 function get_X_mat($ourmat)
128 {
129 if (!preg_match('/^[0-9]{8}$/', $ourmat)) {
130 // le matricule de notre base doit comporter 8 chiffres
131 return 0;
132 }
133
134 $year = intval(substr($ourmat, 0, 4));
135 $rang = intval(substr($ourmat, 5, 3));
136 if ($year < 1996) {
137 return;
138 } elseif ($year < 2000) {
139 $year = intval(substr(1900 - $year, 1, 3));
140 return sprintf('%02u0%03u', $year, $rang);
141 } else {
142 $year = intval(substr(1900 - $year, 1, 3));
143 return sprintf('%03u%03u', $year, $rang);
144 }
145 }
146
147 // }}}
148
149
150 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
151 ?>