source: contrib/Dms/inc/inc.ClassUser.php @ 4362

Revision 4362, 15.8 KB checked in by afernandes, 13 years ago (diff)

Ticket #1416 - Disponibilizado módulo de recursos para a comunidade

  • Property svn:executable set to *
Line 
1<?php
2
3/**********************************************************************\
4|                  statische, User-bezogene Funktionen                 |
5\**********************************************************************/
6//added by dawnlinux to port mydms user management to egroupware
7function phpgw_name2id($login)
8{
9        $id = $GLOBALS['phpgw']->accounts->name2id($login);
10        return $id;
11}
12
13function phpgw_id2name($id)
14{
15        $login = $GLOBALS['phpgw']->accounts->id2name($id);
16        return $login;
17
18}
19
20function phpgw_get_accname($id)
21{
22        $GLOBALS['phpgw']->accounts->get_account_name($id,$lid,$fname,$lname);
23        $fullname = $fname.' '.$lname;
24        return $fullname;
25}
26
27function phpgw_get_accemail($id)
28{
29        $email = $GLOBALS['phpgw']->accounts->id2name($id, 'account_email');
30        return $email;
31}
32
33function phpgw_is_admin($id)
34{
35        if($id == 0)
36        {
37                $id = $GLOBALS['phpgw_info']['user']['account_id'];
38        }
39
40        $acl = CreateObject('phpgwapi.acl',$id);
41        if ($acl->check('run',1,'admin'))
42        {
43                return true;
44        }
45        else
46        {
47                return false;
48        }
49}
50
51
52
53
54               
55        function read_repository2($id)
56                {
57                       
58                        $login=$GLOBALS['phpgw']->accounts->id2name($id);
59                                $ds = $GLOBALS['phpgw']->common->ldapConnect();
60                                $user_context  = $GLOBALS['phpgw_info']['server']['ldap_context'];
61                                $group_context  = $GLOBALS['phpgw_info']['server']['ldap_context'];
62                                $justthese = array("gidnumber");
63                                $sri = @ldap_search($ds, $group_context, ("(&(memberUid=" . $login.")(phpgwaccounttype=g))"), $justthese);
64                       
65                                if(!$sri)
66                                return null;
67                                $allValues = ldap_get_entries($ds, $sri);
68
69                               
70                                while (list($null,$allVals) = @each($allValues))
71                                {
72                                        settype($allVals,'array');
73                                       
74                                        if($data!=""){
75                                                $data=$data.",";
76                                        }
77                                        $data=$data. $allVals['gidnumber'][0];
78                               
79                                       
80                                       
81                                }
82                               
83                                //echo "<br>".$data;
84                                return $data;
85                }
86               
87
88function get_account_name($account_id,$valor)
89                {
90                //      $acct_type = $this->get_type($account_id);
91$acct_type ='u';
92                        /* jakjr: using justthese with ldap_search */
93                        //$justthese = array("cn","uid","givenname","sn","gecos");
94
95                        /* search the dn for the given uid */
96$ds = $GLOBALS['phpgw']->common->ldapConnect();
97$user_context  = $GLOBALS['phpgw_info']['server']['ldap_context'];
98                       
99$justthese = array("uidnumber",  "cn",  "mail","gecos","dn");
100                        //      $sri = @ldap_search($this->ds, "ou=usuarios,ou=cnti,dc=gob,dc=ve", ("(&(sn=*)(mail=*@*)(structuralObjectClass=inetOrgPerson)(deliveryMode=virtual))"), $justthese);
101
102                        //if(($acct_type == 'g') && $this->group_context)
103                //      {
104                //              $sri = @ldap_search($this->ds, $this->group_context, '(gidnumber=' . (int)$account_id . ')', $justthese);
105                //      }
106                //      else
107                //      {
108                                $sri = @ldap_search($ds, $user_context, '(uidnumber=' . (int)$account_id . ')', $justthese);
109                        //}
110
111//echo $this->ds.",". $this->user_context.",". '(uidnumber=' . (int)$account_id . ')'.",". $justthese;
112                        if(!$sri)
113                                return False;
114                        $allValues = ldap_get_entries($ds, $sri);
115$retorna="";
116                        if($acct_type =='g')
117                        {
118                                if($valor=='gecos') {
119                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['gecos'][0],'utf-8');
120                                }
121                                if($valor=='uidnumber') {
122                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['uidnumber'][0],'utf-8');
123                                }
124                                if($valor=='cn') {
125                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['cn'][0],'utf-8');
126                                }
127
128                                if($valor=='mail') {
129                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['mail'][0],'utf-8');
130                                }
131                                if($valor=='dn') {
132                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['dn'][0],'utf-8');
133                                }
134                               
135                        }
136                        else
137                        {
138                                if($valor=='gecos') {
139                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['gecos'][0],'utf-8');
140                                }
141                                if($valor=='uidnumber') {
142                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['uidnumber'][0],'utf-8');
143                                }
144                                if($valor=='cn') {
145                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['cn'][0],'utf-8');
146                                }
147
148                                if($valor=='mail') {
149                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['mail'][0],'utf-8');
150                                }
151                                if($valor=='dn') {
152                                        $retorna=$GLOBALS['phpgw']->translation->convert($allValues[0]['dn'][0],'utf-8');
153                                }
154                               
155
156
157
158                        }
159                        return $retorna;
160                }
161
162function getUser($id)
163{
164//echo "llego";
165        $resArr["id"] = $id;
166        $resArr["login"] =  phpgw_id2name($id);//get_account_name($id,'dn');
167        $resArr["fullName"] = phpgw_get_accname($id);
168        $resArr["email"] = phpgw_get_accemail($id);
169        $resArr["isAdmin"] = phpgw_is_admin($id);
170
171        $resArr["pwd"] = '';
172        $resArr["comment"] = '';
173//      echo "hhh".$resArr["login"]."  ".$resArr["fullName"]."  ".$resArr["isAdmin"];
174        return new User($resArr["id"], $resArr["login"], $resArr["pwd"], $resArr["fullName"], $resArr["email"], $resArr["comment"], $resArr["isAdmin"]);
175}
176
177function getUserByLogin($login)
178{
179        $id = $this->phpgw_name2id($login);
180        $resArr["id"] = $id;
181        $resArr["login"] = $login;
182        $resArr["fullName"] = $this->phpgw_get_accname($id);
183        $resArr["email"] = $this->phpgw_get_accemail($id);
184        $resArr["isAdmin"] = $this->phpgw_is_admin($id);
185
186        $resArr["pwd"] = '';
187        $resArr["comment"] = '';
188               
189        return new User($resArr["id"], $resArr["login"], $resArr["pwd"], $resArr["fullName"], $resArr["email"], $resArr["comment"], $resArr["isAdmin"]);
190}
191
192
193function getAllUsers()
194{
195
196        $accounts = $GLOBALS['phpgw']->accounts->get_list('accounts');
197       
198        for ($i = 0; $i < count($accounts); $i++)
199        {
200                $id = $accounts[$i]["account_id"];
201                $login = $accounts[$i]["account_lid"];
202                $fullName = $accounts[$i]['account_firstname'].' '.$accounts[$i]['account_lastname'];
203                $email = $accounts[$i]["account_email"];
204                $isAdmin = phpgw_is_admin($id);
205                $pwd = '';
206                $comment = '';
207                $users[$i] = new User($id, $login, $pwd, $fullName, $email, $comment, $isAdmin);
208        }
209       
210        return $users;
211}
212
213
214function addUser($login, $pwd, $fullName, $email, $comment)
215{
216        echo "add user is disabled \n";
217        /*GLOBAl $db;
218       
219        $queryStr = "INSERT INTO phpgw_mydms_Users (login, pwd, fullName, email, comment, isAdmin) VALUES ('".$login."', '".$pwd."', '".$fullName."', '".$email."', '".$comment."', false)";
220        $res = $db->getResult($queryStr);
221        if (!$res)
222                return false;
223       
224        return getUser($db->getInsertID('phpgw_mydms_Users','id'));*/
225}
226
227
228/**********************************************************************\
229|                            User-Klasse                               |
230\**********************************************************************/
231
232class User
233{
234        var $_id;
235        var $_login;
236        var $_pwd;
237        var $_fullName;
238        var $_email;
239        var $_comment;
240        var $_isAdmin;
241
242        function User($id, $login, $pwd, $fullName, $email, $comment, $isAdmin)
243        {
244                $this->_id = $id;
245                $this->_login = $login;
246                $this->_pwd = $pwd;
247                $this->_fullName = $fullName;
248                $this->_email = $email;
249                $this->_comment = $comment;
250                $this->_isAdmin = $isAdmin;
251        }
252       
253       
254        function add($id,$login,$fullName,$email,$isAdmin,$pwd,$comment)
255    {
256        $this->User = new User($id, $login, $pwd, $fullName, $email, $comment, $isAdmin);
257    }
258       
259         function sortDataSet($s)
260    {
261        //Sort by the given parameter
262        switch($s)
263        {
264            case "fullName":
265                //Note use of array to reference member method of this object in callback
266                uasort($this->User,array($this,"cmpName"));
267                break;
268           
269            case "login":
270                uasort($this->User,array($this,"cmpX"));
271                break;
272               
273            case "id":
274                uasort($this->User,array($this,"cmpY"));
275                break;               
276           
277            case "added":
278            default:
279                //Re-sort array by original keys
280                ksort($this->User);       
281        }
282    }
283
284    //Callback function for sorting by name
285    //$a and $b are dataItem objects
286    function cmpName($a,$b)
287    {
288        //Use sort() for simple alphabetical comparison
289        //Convert to lowercase to ensure consistent behaviour
290        $sortable = array(strtolower($a->_fullName),strtolower($b->_fullName));
291        $sorted = $sortable;
292        sort($sorted);   
293       
294        //If the names have switched position, return -1. Otherwise, return 1.
295        return ($sorted[0] == $sortable[0]) ? -1 : 1;
296    }
297   
298    //Callback function for sorting by x
299    //$a and $b are dataItem objects
300    function cmpX($a,$b)
301    {
302        //Use sort() for simple alphabetical comparison
303        //Convert to lowercase to ensure consistent behaviour
304        $sortable = array(strtolower($a->x),strtolower($b->x));
305        $sorted = $sortable;
306        sort($sorted);   
307       
308        //If the names have switched position, return -1. Otherwise, return 1.
309        return ($sorted[0] == $sortable[0]) ? -1 : 1;
310    }
311   
312    //Callback function for sorting by y
313    //$a and $b are dataItem objects
314    function cmpY($a,$b)
315    {       
316        //If $a's y attribute >= $b's y attribute, return 1. Otherwise, return -1.
317        return ($a->y >= $b->y) ? 1 : -1;
318    }   
319
320        function getID() { return $this->_id; }
321
322        function getLogin() { return $this->_login; }
323
324        function setLogin($newLogin)
325        {
326                GLOBAL $db;
327               
328                $queryStr = "UPDATE phpgw_mydms_Users SET login ='" . $newLogin . "' WHERE id = " . $this->_id;
329                $res = $db->getResult($queryStr);
330                if (!$res)
331                        return false;
332               
333                $this->_login = $newLogin;
334                return true;
335        }
336
337        function getFullName() { return $this->_fullName; }
338
339        function setFullName($newFullName)
340        {
341                GLOBAL $db;
342               
343                $queryStr = "UPDATE phpgw_mydms_Users SET fullname = '" . $newFullName . "' WHERE id = " . $this->_id;
344                $res = $db->getResult($queryStr);
345                if (!$res)
346                        return false;
347               
348                $this->_fullName = $newFullName;
349                return true;
350        }
351
352        function getPwd() { return $this->_pwd; }
353
354        function setPwd($newPwd)
355        {
356                GLOBAL $db;
357               
358                $queryStr = "UPDATE phpgw_mydms_Users SET pwd ='" . $newPwd . "' WHERE id = " . $this->_id;
359                $res = $db->getResult($queryStr);
360                if (!$res)
361                        return false;
362               
363                $this->_pwd = $newPwd;
364                return true;
365        }
366
367        function getEmail() { return $this->_email; }
368
369        function setEmail($newEmail)
370        {
371                GLOBAL $db;
372               
373                $queryStr = "UPDATE phpgw_mydms_Users SET email ='" . $newEmail . "' WHERE id = " . $this->_id;
374                $res = $db->getResult($queryStr);
375                if (!$res)
376                        return false;
377               
378                $this->_email = $newEmail;
379                return true;
380        }
381
382        function getComment() { return $this->_comment; }
383
384        function setComment($newComment)
385        {
386                GLOBAL $db;
387               
388                $queryStr = "UPDATE phpgw_mydms_Users SET comment ='" . $newComment . "' WHERE id = " . $this->_id;
389                $res = $db->getResult($queryStr);
390                if (!$res)
391                        return false;
392               
393                $this->_comment = $newComment;
394                return true;
395        }
396
397        function isAdmin() { return $this->_isAdmin; }
398
399        function setAdmin($isAdmin)
400        {
401                GLOBAL $db;
402               
403                $isAdmin = $isAdmin ? "1" : "0";
404                $queryStr = "UPDATE phpgw_mydms_Users SET isAdmin = " . $GLOBALS['phpgw']->db->quote($isAdmin) . " WHERE id = " . $this->_id;
405                if (!$db->getResult($queryStr))
406                        return false;
407               
408                $this->_isAdmin = $isAdmin;
409                return true;
410        }
411
412        /**
413         * Entfernt den Benutzer aus dem System.
414         * Dies ist jedoch nicht mit einem Lï¿œschen des entsprechenden Eintrags aus phpgw_mydms_Users geschehen - vielmehr
415         * muss dafï¿œr gesorgt werden, dass der Benutzer nirgendwo mehr auftaucht. D.h. auch die Tabellen phpgw_mydms_ACLs,
416         * phpgw_mydms_Notify, phpgw_mydms_GroupMembers, phpgw_mydms_Folders, phpgw_mydms_Documents und phpgw_mydms_DocumentContent mï¿œssen berï¿œcksichtigt werden.
417         */
418        function remove()
419        {
420                GLOBAL $db, $settings;
421               
422                //Private Stichwortlisten lï¿œschen
423                $queryStr = "SELECT phpgw_mydms_Keywords.id FROM phpgw_mydms_Keywords, phpgw_mydms_KeywordCategories WHERE phpgw_mydms_Keywords.category = phpgw_mydms_KeywordCategories.id AND phpgw_mydms_KeywordCategories.owner = " . $this->_id;
424                $resultArr = $db->getResultArray($queryStr);
425                if (count($resultArr) > 0) {
426                        $queryStr = "DELETE FROM phpgw_mydms_Keywords WHERE ";
427                        for ($i = 0; $i < count($resultArr); $i++) {
428                                $queryStr .= "id = " . $resultArr[$i]["id"];
429                                if ($i + 1 < count($resultArr))
430                                        $queryStr .= " OR ";
431                        }
432                        if (!$db->getResult($queryStr))
433                                return false;
434                }
435                $queryStr = "DELETE FROM phpgw_mydms_KeywordCategories WHERE owner = " . $this->_id;
436                if (!$db->getResult($queryStr))
437                        return false;
438               
439                //Benachrichtigungen entfernen
440                $queryStr = "DELETE FROM phpgw_mydms_Notify WHERE userID = " . $this->_id;
441                if (!$db->getResult($queryStr))
442                        return false;
443                //Der Besitz von Dokumenten oder Ordnern, deren bisheriger Besitzer der zu lï¿œschende war, geht an den Admin ï¿œber
444                $queryStr = "UPDATE phpgw_mydms_Folders SET owner = " . $settings->_adminID . " WHERE owner = " . $this->_id;
445                if (!$db->getResult($queryStr))
446                        return false;
447                $queryStr = "UPDATE phpgw_mydms_Documents SET owner = " . $settings->_adminID . " WHERE owner = " . $this->_id;
448                if (!$db->getResult($queryStr))
449                        return false;
450                $queryStr = "UPDATE phpgw_mydms_DocumentContent SET createdBy = " . $settings->_adminID . " WHERE createdBy = " . $this->_id;
451                if (!$db->getResult($queryStr))
452                        return false;
453                //Verweise auf Dokumente: Private lï¿œschen...
454                $queryStr = "DELETE FROM phpgw_mydms_DocumentLinks WHERE userID = " . $this->_id . " AND public = 0";
455                if (!$db->getResult($queryStr))
456                        return false;
457                //... und ï¿œffentliche an Admin ï¿œbergeben
458                $queryStr = "UPDATE phpgw_mydms_DocumentLinks SET userID = " . $settings->_adminID . " WHERE userID = " . $this->_id;
459                if (!$db->getResult($queryStr))
460                        return false;
461                //Evtl. von diesem Benutzer gelockte Dokumente werden freigegeben
462                $queryStr = "UPDATE phpgw_mydms_Documents SET locked = -1 WHERE locked = " . $this->_id;
463                if (!$db->getResult($queryStr))
464                        return false;
465                //User aus allen Gruppen lï¿œschen
466                $queryStr = "DELETE FROM phpgw_mydms_GroupMembers WHERE userID = " . $this->_id;
467                if (!$db->getResult($queryStr))
468                        return false;
469                //User aus allen ACLs streichen
470                $queryStr = "DELETE FROM phpgw_mydms_ACLs WHERE userID = " . $this->_id;
471                if (!$db->getResult($queryStr))
472                        return false;
473                //Eintrag aus phpgw_mydms_Users lï¿œschen
474                $queryStr = "DELETE FROM phpgw_mydms_Users WHERE id = " . $this->_id;
475                if (!$db->getResult($queryStr))
476                        return false;
477               
478               
479//              unset($this);
480                return true;
481        }
482
483        function joinGroup($group)
484        {
485                if ($group->isMember($this))
486                        return false;
487               
488                if (!$group->addUser($this))
489                        return false;
490               
491                unset($this->_groups);
492                return true;
493        }
494
495        function leaveGroup($group)
496        {
497                if (!$group->isMember($this))
498                        return false;
499               
500                if (!$group->removeUser($this))
501                        return false;
502               
503                unset($this->_groups);
504                return true;
505        }
506
507        function getGroups()
508        {
509                $usergroups = $GLOBALS['phpgw']->accounts->membership($this->_id);
510                $this->_groups = array();
511                foreach($usergroups as $phpgw_group)
512                {
513                        $group = getGroup($phpgw_group['account_id']);
514                        array_push($this->_groups, $group);
515                }               
516                return $this->_groups;
517        }
518
519        function isMemberOfGroup($group)
520        {
521                //echo "es miembro de".$this."<br>";
522                return $group->isMember($this);
523        }
524
525        function hasImage()
526        {
527                if (!isset($this->_hasImage))
528                {
529                        GLOBAL $db;
530                       
531                        $queryStr = "SELECT COUNT(*) AS num FROM phpgw_mydms_UserImages WHERE userID = " . $this->_id;
532                        $resArr = $db->getResultArray($queryStr);
533                        if (is_bool($resArr) && $resArr == false)
534                                return false;
535                       
536                        if ($resArr[0]["num"] == 0)     $this->_hasImage = false;
537                        else $this->_hasImage = true;
538                }
539               
540                return $this->_hasImage;
541        }
542
543        function getImageURL()
544        {
545                GLOBAL $settings;
546               
547//              if (!$this->hasImage())
548//                      return false;
549                return $settings->_httpRoot . "out/out.UserImage.php?userid=" . $this->_id;
550        }
551
552        function setImage($tmpfile, $mimeType)
553        {
554                GLOBAL $db;
555               
556                $fp = fopen($tmpfile, "rb");
557                if (!$fp) return false;
558                $content = fread($fp, filesize($tmpfile));
559                fclose($fp);
560               
561                if ($this->hasImage())
562                        $queryStr = "UPDATE phpgw_mydms_UserImages SET image = '".base64_encode($content)."', mimeType = '". $mimeType."' WHERE userID = " . $this->_id;
563                else
564                        $queryStr = "INSERT INTO phpgw_mydms_UserImages (userID, image, mimeType) VALUES (" . $this->_id . ", '".base64_encode($content)."', '".$mimeType."')";
565                if (!$db->getResult($queryStr))
566                        return false;
567               
568                $this->_hasImage = true;
569                return true;
570        }
571}
572
573
574?>
Note: See TracBrowser for help on using the repository browser.