File: class.user.php

package info (click to toggle)
collabtive 2.0%2Bdfsg-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 17,708 kB
  • ctags: 27,425
  • sloc: php: 100,872; sh: 72; makefile: 50
file content (551 lines) | stat: -rw-r--r-- 15,918 bytes parent folder | download | duplicates (2)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?php
/**
 * Provides methods to interact with users
 *
 * @author Open Dynamics <info@o-dyn.de>
 * @name user
 * @version 0.7
 * @package Collabtive
 * @link http://www.o-dyn.de
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License v3 or laterg
 */
class user {
    public $mylog;

    /**
     * Constructor
     * Initializes event log
     */
    function __construct()
    {
        $this->mylog = new mylog;
    }

    /**
     * Creates a user
     *
     * @param string $name Name of the member
     * @param string $email E-mail address of the member
     * @param string $company Company of the member
     * @param string $pass Password
     * @param string $locale Localisation
     * @param float $rate Hourly rate
     * @return int $insid ID of the newly created member
     */
    function add($name, $email, $company, $pass, $locale = "", $tags = "", $rate = 0.0)
    {
        global $conn;
        $pass = sha1($pass);

        $ins1Stmt = $conn->prepare("INSERT INTO user (name,email,company,pass,locale,tags,rate) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $ins1 = $ins1Stmt->execute(array($name, $email, $company, $pass, $locale, $tags, $rate));

        if ($ins1) {
            $insid = $conn->lastInsertId();
            $this->mylog->add($name, 'user', 1, 0);
            return $insid;
        } else {
            return false;
        }
    }

    /**
     * Edits a member
     *
     * @param int $id Member ID
     * @param string $name Member name
     * @param string $realname realname
     * @param string $role role
     * @param string $email Email
     * @param string $company Company of the member
     * @param string $zip ZIP-Code
     * @param string $gender Gender
     * @param string $url URL
     * @param string $address1 Adressline1
     * @param string $address2 Addressline2
     * @param string $state State
     * @param string $country Country
     * @param string $locale Localisation
     * @param string $avatar Avatar
     * @return bool
     */
    function edit($id, $name, $realname, $email, $tel1, $tel2, $company, $zip, $gender, $url, $address1, $address2, $state, $country, $tags, $locale, $avatar = "", $rate = 0.0)
    {
        global $conn;

        $rate = (float) $rate;
        $id = (int) $id;

        if ($avatar != "") {
            $updStmt = $conn->prepare("UPDATE user SET name=?, email=?, tel1=?, tel2=?, company=?, zip=?, gender=?, url=?, adress=?, adress2=?, state=?, country=?, tags=?, locale=?, avatar=?, rate=? WHERE ID = ?");
            $upd = $updStmt->execute(array($name, $email, $tel1, $tel2, $company, $zip, $gender, $url, $address1, $address2, $state, $country, $tags, $locale, $avatar, $rate, $id));
        } else {
            $updStmt = $conn->prepare("UPDATE user SET name=?, email=?, tel1=?, tel2=?, company=?, zip=?, gender=?, url=?, adress=?, adress2=?, state=?, country=?, tags=?, locale=?, rate=? WHERE ID = ?");
            $upd = $updStmt->execute(array($name, $email, $tel1, $tel2, $company, $zip, $gender, $url, $address1, $address2, $state, $country, $tags, $locale, $rate, $id));
        }

        if ($upd) {
            $this->mylog->add($name, 'user', 2, 0);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Generate a new password and send it to the user's e-mail address
     *
     * @param string $email E-mail address entered by the user
     * @return string
     */
    function resetPassword($email)
    {
        global $conn;

        $qry = $conn->query("SELECT ID, email, locale FROM user WHERE email={$conn->quote($email)} LIMIT 1");
	if ($qry) {
	    $user = $qry->fetch();
	}

        if ($user["email"] == $email) {
            $id = $user["ID"];
            $locale = $user['locale'];
        }

        if (isset($id)) {
            $dummy = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'), range('0', '9'));
            shuffle($dummy);
            mt_srand((double)microtime() * 1000000);
            $newpass = "";
            for ($i = 1; $i <= 10; $i++) {
                $swap = mt_rand(0, count($dummy)-1);
                $tmp = $dummy[$swap];
                $newpass .= $tmp;
            }

            $sha1pass = sha1($newpass);

            $upd = $conn->query("UPDATE user SET `pass` = '$sha1pass' WHERE ID = $id");
            if ($upd) {
                return array('newpass'=>$newpass, 'locale'=>$locale);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Change password
     *
     * @param int $id Member ID
     * @param string $oldpass Old password
     * @param string $newpass New password
     * @param string $repeatpass Repetition of the new password
     * @return bool
     */
    function editpass($id, $oldpass, $newpass, $repeatpass)
    {
        global $conn;
        $id = (int) $id;

        if ($newpass != $repeatpass) {
            return false;
        }
        $newpass = sha1($newpass);

        $oldpass = sha1($oldpass);
        $qry = $conn->query("SELECT ID, name FROM user WHERE ID = $id AND pass = {$conn->quote($oldpass)}");
	if ($qry) {
	    $chk = $qry->fetch();
	    $chk = $chk[0];
	    $name = $chk[1];
	}
        if (!$chk) {
            return false;
        }

        $upd = $conn->query("UPDATE user SET pass={$conn->quote($newpass)} WHERE ID = $id");
        if ($upd) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Change password as admin
     *
     * @param int $id User ID
     * @param string $newpass New password
     * @param string $repeatpass Repetition of the new password
     * @return bool
     */
    function admin_editpass($id, $newpass, $repeatpass)
    {
        global $conn;
        $id = (int) $id;

        if ($newpass != $repeatpass) {
            return false;
        }
        $newpass = sha1($newpass);

        $upd = $conn->query("UPDATE user SET pass={$conn->quote($newpass)} WHERE ID = $id");
        if ($upd) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Delete a user
     *
     * @param int $id User ID
     * @return bool
     */
    function del($id)
    {
        global $conn;
        $id = (int) $id;

        $qry = $conn->query("SELECT name FROM user WHERE ID = $id");
	if ($qry) {
	    $chk = $qry->fetch();
	    $name = $chk[0];
	}

        $del = $conn->query("DELETE FROM user WHERE ID = $id");
        $del2 = $conn->query("DELETE FROM projekte_assigned WHERE user = $id");
        $del3 = $conn->query("DELETE FROM milestones_assigned WHERE user = $id");
        $del4 = $conn->query("DELETE FROM tasks_assigned WHERE user = $id");
        $del5 = $conn->query("DELETE FROM log WHERE user = $id");
        $del6 = $conn->query("DELETE FROM timetracker WHERE user = $id");
        $del7 = $conn->query("DELETE FROM roles_assigned WHERE user = $id");
        if ($del) {
            $this->mylog->add($name, 'user', 3, 0);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Get a user profile
     *
     * @param int $id User ID
     * @return array $profile Profile
     */
    function getProfile($id)
    {
        global $conn;
        $id = (int) $id;

        $sel = $conn->query("SELECT * FROM user WHERE ID = $id");
        if ($sel) {
	    $profile = $sel->fetch();
	}
        if (!empty($profile)) {
            $profile["name"] = stripslashes($profile["name"]);
            if (isset($profile["company"])) {
                $profile["company"] = stripslashes($profile["company"]);
            }
            if (isset($profile["adress"])) {
                $profile["adress"] = stripslashes($profile["adress"]);
            }
            if (isset($profile["adress2"])) {
                $profile["adress2"] = stripslashes($profile["adress2"]);
            }
            if (isset($profile["state"])) {
                $profile["state"] = stripslashes($profile["state"]);
            }
            if (isset($profile["country"])) {
                $profile["country"] = stripslashes($profile["country"]);
            }
            $tagsobj = new tags();
            $profile["tagsarr"] = $tagsobj->splitTagStr($profile["tags"]);

            $rolesobj = (object) new roles();
            $profile["role"] = $rolesobj->getUserRole($profile["ID"]);

            return $profile;
        } else {
            return false;
        }
    }

    /**
     * Get the avatar of a user
     *
     * @param int $id User ID
     * @return array $profile Avatar
     */
    function getAvatar($id)
    {
        $id = (int) $id;
        global $conn;
        $sel = $conn->query("SELECT avatar FROM user WHERE ID = $id");
	if ($sel) {
	    $profile = $sel->fetch();
	    $profile = $profile[0];
	}

        if (!empty($profile)) {
            return $profile;
        } else {
            return false;
        }
    }

    /**
     * Log a user in
     *
     * @param string $user User name
     * @param string $pass Password
     * @return bool
     */
    function login($user, $pass)
    {
        global $conn;

        if (!$user) {
            return false;
        }
        $user = $conn->quote($user);
        $pass = sha1($pass);

        $sel1 = $conn->query("SELECT ID,name,locale,lastlogin,gender FROM user WHERE (name = $user OR email = $user) AND pass = '$pass'");
	if ($sel1) {
  	    $chk = $sel1->fetch();
	}
        if ($chk["ID"] != "") {
            $rolesobj = new roles();
            $now = time();
            $_SESSION['userid'] = $chk['ID'];
            $_SESSION['username'] = stripslashes($chk['name']);
            $_SESSION['lastlogin'] = $now;
            $_SESSION['userlocale'] = $chk['locale'];
            $_SESSION['usergender'] = $chk['gender'];
            $_SESSION["userpermissions"] = $rolesobj->getUserRole($chk["ID"]);

            $userid = $_SESSION['userid'];
            $seid = session_id();
            $staylogged = getArrayVal($_POST, 'staylogged');

            if ($staylogged == 1) {
                setcookie("PHPSESSID", "$seid", time() + 14 * 24 * 3600);
            }
            $upd1 = $conn->query("UPDATE user SET lastlogin = '$now' WHERE ID = $userid");
            return true;
        } else {
            return false;
        }
    }

    /**
     * Log a user in
     *
     * @param string $user User name
     * @param string $pass Password
     * @return bool
     */
    function openIdLogin($url)
    {
        /* here the openid auth should take place */

        try {
            $openid = new LightOpenID($_SERVER['HTTP_HOST']);
            if (!$openid->mode) {
                $openid->identity = $url;
                header('Location: ' . $openid->authUrl());
            } elseif ($openid->mode == 'cancel') {
                return false;
            } else {
                $identity = $openid->data['openid_identity'];

                $sel1 = $conn->query("SELECT ID from openids WHERE identity='$identity'");
                if ($sel1 and $row = $sel1->fetch()) {
                    $id = $row['ID'];
                } else return false;
                // die("SELECT ID,name,locale,lastlogin,gender FROM user WHERE ID=$id");
                $sel1 = $conn->query("SELECT ID,name,locale,lastlogin,gender FROM user WHERE ID=$id");
		if ($sel1) {
		    $chk = $sel1->fetch();
		}
                if ($chk["ID"] != "") {
                    $rolesobj = new roles();
                    $now = time();
                    $_SESSION['userid'] = $chk['ID'];
                    $_SESSION['username'] = stripslashes($chk['name']);
                    $_SESSION['lastlogin'] = $now;
                    $_SESSION['userlocale'] = $chk['locale'];
                    $_SESSION['usergender'] = $chk['gender'];
                    $_SESSION["userpermissions"] = $rolesobj->getUserRole($chk["ID"]);

                    $userid = $_SESSION['userid'];
                    $seid = session_id();
                    $staylogged = getArrayVal($_POST, 'staylogged');

                    if ($staylogged == 1) {
                        setcookie("PHPSESSID", "$seid", time() + 14 * 24 * 3600);
                    }
                    $upd1 = $conn->prepare("UPDATE user SET lastlogin = ? WHERE ID = ?");
                    $upd1Stmt = $upd1->execute(array($now,$userid));
                    return true;
                } else {
                    return false;
                }
            }
        }
        catch(ErrorException $e) {
            return false;
        }
    }
    /**
     * Logout
     *
     * @return bool
     */
    function logout()
    {
        session_start();
        session_destroy();
        session_unset();
        setcookie("PHPSESSID", "");
        return true;
    }

    /**
     * Returns all users
     *
     * @param int $lim Limit
     * @return array $users Registrierte Mitglieder
     */
    function getAllUsers($lim = 10)
    {
        global $conn;

        $lim = (int) $lim;

        $qry = $conn->query("SELECT COUNT(*) FROM `user`");
	if ($qry) {
	    $num = $qry->fetch();
	    $num = $num[0];
	}
        SmartyPaginate::connect();
        // set items per page
        SmartyPaginate::setLimit($lim);
        SmartyPaginate::setTotal($num);

        $start = SmartyPaginate::getCurrentIndex();
        $lim = SmartyPaginate::getLimit();

        $sel2 = $conn->query("SELECT ID FROM `user` ORDER BY ID DESC LIMIT $start,$lim");

        $users = array();
        while ($sel2 and $user = $sel2->fetch()) {
            array_push($users, $this->getProfile($user["ID"]));
        }

        if (!empty($users)) {
            return $users;
        } else {
            return false;
        }
    }

    /**
     * Get all users who are logged in
     *
     * @param int $offset Allowed time from last login
     * @return array $users
     */
    function getOnlinelist($offset = 200)
    {
        global $conn;

        $offset = (int) $offset;
        $time = time();
        $now = $time - $offset;

        $sel = $conn->query("SELECT * FROM user WHERE lastlogin >= $now");

        $users = array();

        while ($sel and $user = $sel->fetch()) {
            $user["name"] = stripslashes($user["name"]);
            $user["company"] = stripslashes($user["company"]);
            $user["adress"] = stripslashes($user["adress"]);
            $user["adress2"] = stripslashes($user["adress2"]);
            $user["state"] = stripslashes($user["state"]);
            $user["country"] = stripslashes($user["country"]);
            array_push($users, $user);
        }

        if (!empty($users)) {
            return $users;
        } else {
            return false;
        }
    }

    /**
     * Is the given user logged in?
     *
     * @param int $user Member ID
     * @param int $offset Allowed time from last login
     * @return bool
     */
    function isOnline($user, $offset = 30)
    {
        global $conn;

        $user = (int) $user;
        $offset = (int) $offset;

        $time = time();
        $now = $time - $offset;

        $sel = $conn->query("SELECT ID FROM user WHERE lastlogin >= $now AND ID = $user");
        if ($sel) {
	    $user = $sel->fetch();
	}

        if (!empty($user)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Get a user's ID
     *
     * @param string $user Username
     * @return int $theid
     */
    function getId($user)
    {
        global $conn;

        $sel = $conn->query("SELECT ID FROM user WHERE name = {$conn->quote($user)}");
	if ($sel) {
  	    $id = $sel->fetch();
	    $id = $id[0];
	}

        $theid = array();

        $theid["ID"] = $id;

        if ($id > 0) {
            return $theid;
        } else {
            return array();
        }
    }
}

?>