source: contrib/Dms/inc/inc.ClassFolder.php @ 3526

Revision 3526, 16.7 KB checked in by afernandes, 13 years ago (diff)

Ticket #1416 - Disponibilizado modulos Timesheet e DMS para a comunidade.

  • Property svn:executable set to *
Line 
1<?php
2function getFolder($id)
3{
4        if (!is_numeric($id))
5                die ("invalid folderid");
6
7        $queryStr = "SELECT * FROM phpgw_mydms_Folders WHERE id = " . $id;
8        $resArr = $GLOBALS['mydms']->db->getResultArray($queryStr);
9               
10        if (is_bool($resArr) && $resArr == false)
11                return false;
12        else if (count($resArr) != 1)
13                return false;
14               
15        $resArr = $resArr[0];
16        if($id == 1) {
17                $resArr["defaultAccess"] = M_READ;
18        }
19        $newFolder =  new Folder($resArr["id"], $resArr["name"], $resArr["parent"], $resArr["comment"], $resArr["owner"], $resArr["inheritAccess"], $resArr["defaultAccess"], $resArr["sequence"]);
20       
21        #print $resArr["name"]."<br>";
22        #print $newFolder->getAccessMode(getUser($GLOBALS['phpgw_info']['user']['account_id']))."<br>";
23        if($newFolder->getAccessMode(getUser($GLOBALS['phpgw_info']['user']['account_id'])) > 1)
24                return $newFolder;
25        else
26                return false;
27}
28
29
30/**********************************************************************\
31|                            Folder-Klasse                             |
32\**********************************************************************/
33
34class Folder
35{
36        var $_id;
37        var $_name;
38        var $_parentID;
39        var $_comment;
40        var $_ownerID;
41        var $_inheritAccess;
42        var $_defaultAccess;
43        var $_sequence;
44
45        function Folder($id, $name, $parentID, $comment, $ownerID, $inheritAccess, $defaultAccess, $sequence)
46        {
47                $this->_id = $id;
48                $this->_name = $name;
49                $this->_parentID = $parentID;
50                $this->_comment = $comment;
51                $this->_ownerID = $ownerID;
52                $this->_inheritAccess = $inheritAccess;
53                $this->_defaultAccess = $defaultAccess;
54                $this->_sequence = $sequence;
55               
56                $this->db = clone($GLOBALS['phpgw']->db);
57                $this->db->set_app('mydms');
58        }
59
60        function getID() { return $this->_id; }
61
62        function getName() { return $this->_name; }
63
64        function setName($newName)
65        {
66                $data   = array('name' => $newName);
67                $where  = array('id' => $this->_id);
68               
69                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
70                        return false;
71               
72                $this->_name = $newName;
73               
74                return true;
75        }
76
77        function getComment() { return $this->_comment; }
78
79        function setComment($newComment)
80        {
81                $data   = array('comment' => $newComment);
82                $where  = array('id' => $this->_id);
83               
84                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
85                        return false;
86               
87                $this->_comment = $newComment;
88               
89                return true;
90        }
91
92        function getParent()
93        {
94                if (!isset($this->_parentID) || ($this->_parentID == "") || ($this->_parentID == 0))
95                        return false;
96               
97                if (!isset($this->_parent))
98                        $this->_parent = getFolder($this->_parentID);
99                return $this->_parent;
100        }
101
102        function setParent($newParent)
103        {
104                $data   = array('parent' => $newParent->getID());
105                $where  = array('id' => $this->_id);
106               
107                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
108                        return false;
109               
110                $this->_parentID = $newParent->getID();
111                $this->_parent = $newParent;
112               
113                return true;
114        }
115
116        function getOwner()
117        {
118                if (!isset($this->_owner))
119                        $this->_owner = getUser($this->_ownerID);
120                return $this->_owner;
121        }
122
123        function setOwner($user)
124        {
125                $data   = array('owner' => $user->getID());
126                $where  = array('id' => $this->_id);
127               
128                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
129                        return false;
130               
131                $this->_ownerID = $user->getID();
132                $this->_owner = $user;
133                return true;
134        }
135
136        function getDefaultAccess()
137        {
138                if ($this->inheritsAccess())
139                {
140                        $res = $this->getParent();
141                        if (!$res) return false;
142                        return $this->_parent->getDefaultAccess();
143                }
144               
145                return $this->_defaultAccess;
146        }
147
148        function setDefaultAccess($mode)
149        {
150                $data   = array('defaultAccess' => $mode);
151                $where  = array('id' => $this->_id);
152               
153                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
154                        return false;
155               
156                $this->_defaulAccess = $mode;
157                return true;
158        }
159
160        function inheritsAccess() { return $this->_inheritAccess; }
161
162        function setInheritAccess($inheritAccess)
163        {
164                $inheritAccess = $inheritAccess ? "1" : "0";
165
166                $data   = array('inheritAccess' => $inheritAccess);
167                $where  = array('id' => $this->_id);
168               
169                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
170                        return false;
171               
172                $this->_inheritAccess = $inheritAccess;
173                return true;
174        }
175
176        function getSequence() { return $this->_sequence; }
177
178        function setSequence($seq)
179        {
180                $data   = array('sequence' => $seq);
181                $where  = array('id' => $this->_id);
182               
183                if(!$this->db->update('phpgw_mydms_Folders', $data, $where, __LINE__, __FILE__))
184                        return false;
185
186                $this->_sequence = $seq;
187                return true;
188        }
189
190        function getSubFolders()
191        {
192                if (!isset($this->_subFolders))
193                {
194                        $queryStr = "SELECT * FROM phpgw_mydms_Folders WHERE parent = " . $this->_id . " ORDER BY sequence";
195
196
197                        $resArr = $GLOBALS['mydms']->db->getResultArray($queryStr);
198                        if (is_bool($resArr) && $resArr == false)
199                                return false;
200                       
201                        $this->_subFolders = array();
202                        for ($i = 0; $i < count($resArr); $i++)
203                        {
204                                $newSubFolder = new Folder($resArr[$i]["id"], $resArr[$i]["name"], $resArr[$i]["parent"], $resArr[$i]["comment"], $resArr[$i]["owner"], $resArr[$i]["inheritaccess"], $resArr[$i]["defaultaccess"], $resArr[$i]["sequence"]);
205
206                                if($newSubFolder->getAccessMode(getUser($GLOBALS['phpgw_info']['user']['account_id'])) > 1)
207                                        $this->_subFolders[$i] = $newSubFolder;
208                        }                       
209                }
210               
211                return $this->_subFolders;
212        }
213
214        function addSubFolder($name, $comment, $owner, $sequence)
215        {
216                $ownerid = $GLOBALS['phpgw_info']['user']['account_id'];
217                //inheritAccess = true, defaultAccess = M_READ
218
219                $insertData = array(
220                        'name'          => $name,
221                        'parent'        => $this->_id,
222                        'comment'       => $comment,
223                        'owner'         => $ownerid,
224                        'inheritAccess' => true,
225                        'defaultAccess' => M_READ,
226                        'sequence'      => $sequence,
227                );
228                $res = $this->db->insert('phpgw_mydms_Folders', $insertData, '', __LINE__, __FILE__, 'mydms');
229
230                if (!$res)
231                        return false;
232               
233                unset($this->_subFolders);
234               
235                return getFolder($this->db->get_last_insert_id('phpgw_mydms_Folders','id'));
236        }
237
238        /**
239         * Gibt ein Array mit allen Eltern, "Groï¿œelter" usw bis zum RootFolder zurï¿œck
240         * Der Ordner selbst ist das letzte Element dieses Arrays
241         */
242        function getPath()
243        {
244                if (!isset($this->_parentID) || ($this->_parentID == "") || ($this->_parentID == 0))
245                        return array($this);
246                else
247                {
248                        $res = $this->getParent();
249                        if (!$res) return false;
250                       
251                        $path = $this->_parent->getPath();
252                        if (!$path) return false;
253                       
254                        array_push($path, $this);
255                        return $path;
256                }
257        }
258
259        /**
260         * Gibt ein Array mit allen Eltern, "Groï¿œelter" usw bis zum RootFolder zurï¿œck
261         * Der Ordner selbst ist das letzte Element dieses Arrays
262         */
263        function getPathNew()
264        {
265                if (!isset($this->_parentID) || ($this->_parentID == "") || ($this->_parentID == 0))
266                        return array($this->_id => $this);
267                else
268                {
269                        $res = $this->getParent();
270                        if (!$res) return false;
271                       
272                        #print "search parent ".$this->_id."<br>";
273                        $path = $this->_parent->getPathNew();
274                        #print "_parent->getPathNew(".$this->_id."):<br>";
275                        #print "my parent ".$this->_id."<br>";
276                        #_debug_array($path);
277                        if (!$path) return false;
278                       
279                        #$path = array_merge($path, array($this->_id => $this));
280                        #$path[] = array($this);
281                        unset($this->_parent);
282                        #print "me ".$this->_id."<br>";
283                        #_debug_array($this);
284                        $path[$this->_id] = $this;
285                        return $path;
286                }
287        }
288
289        /**
290         * ï¿œberprï¿œft, ob dieser Ordner ein Unterordner von $folder ist
291         */
292        function isDescendant($folder)
293        {
294                if ($this->_parentID == $folder->getID())
295                        return true;
296                else if (isset($this->_parentID))
297                {
298                        $res = $this->getParent();
299                        if (!$res) return false;
300                       
301                        return $this->_parent->isDescendant($folder);
302                }
303                else
304                        return false;
305        }
306
307        function getDocuments()
308        {
309                if (!isset($this->_documents))
310                {
311                        $queryStr = "SELECT * FROM phpgw_mydms_Documents WHERE folder = " . $this->_id . " ORDER BY sequence";
312                        $resArr = $GLOBALS['mydms']->db->getResultArray($queryStr);
313                        if (is_bool($resArr) && !$resArr)
314                                return false;
315                       
316                        $this->_documents = array();
317                        foreach ($resArr as $row)
318                                array_push($this->_documents, new Document($row["id"], $row["name"], $row["comment"], $row["date"], $row["expires"], $row["owner"], $row["folder"], $row["inheritaccess"], $row["defaultaccess"], $row["locked"], $row["keywords"], $row["sequence"]));
319                }
320                return $this->_documents;
321        }
322
323        function addDocument($name, $comment, $expires, $owner, $keywords, $tmpFile, $orgFileName, $fileType, $mimeType, $sequence)
324        {
325                $ownerid = $GLOBALS['phpgw_info']['user']['account_id'];               
326
327                $expires = (!$expires) ? 0 : $expires;
328
329
330                $insertData = array(
331                        'name'          => $name,
332                        'comment'       => $comment,
333                        'date'          => mktime(),
334                        'expires'       => $expires,
335                        'owner'         => $ownerid,
336                        'folder'        => $this->_id,
337                        'inheritAccess' => true,
338                        'defaultAccess' => M_READ,
339                        'locked'        => -1,
340                        'keywords'      => $keywords,
341                        'sequence'      => $sequence,
342                );
343                $res = $this->db->insert('phpgw_mydms_Documents', $insertData, '', __LINE__, __FILE__, 'mydms');
344
345                if (!$res)
346                        return false;
347
348                #unset($this->_subFolders);
349               
350                #return getFolder($this->db->get_last_insert_id('phpgw_mydms_Folders','id'));
351
352                #$queryStr = "INSERT INTO phpgw_mydms_Documents (name, comment, date, expires, owner, folder, inheritAccess, defaultAccess, locked, keywords, sequence) VALUES ".
353                #                       "('".$name."', '".$comment."', " . mktime().", ".$expires.", ".$ownerid.", ".$this->_id.", true, ".M_READ.", -1, '".$keywords."', " . $sequence . ")";
354                #if (!$GLOBALS['mydms']->db->getResult($queryStr))
355                #       return false;
356               
357                $document = getDocument($this->db->get_last_insert_id('phpgw_mydms_Documents','id'));
358               
359                $res = $document->addContent($comment, $owner, $tmpFile, $orgFileName, $fileType, $mimeType);
360                if (is_bool($res) && !$res)
361                {
362                        $queryStr = "DELETE FROM phpgw_mydms_Documents WHERE id = " . $document->getID();
363                        $GLOBALS['mydms']->db->getResult($queryStr);
364                        return false;
365                }
366               
367                return $document;
368        }
369
370        function remove()
371        {
372                //Entfernen der Unterordner und Dateien
373                $res = $this->getSubFolders();
374                if (is_bool($res) && !$res) return false;
375                $res = $this->getDocuments();
376                if (is_bool($res) && !$res) return false;
377               
378                foreach ($this->_subFolders as $subFolder)
379                {
380                        $res = $subFolder->remove();
381                        if (!$res) return false;
382                }
383               
384                foreach ($this->_documents as $document)
385                {
386                        $res = $document->remove();
387                        if (!$res) return false;
388                }
389               
390                //Entfernen der Datenbankeintrï¿œge
391                $queryStr = "DELETE FROM phpgw_mydms_Folders WHERE id =  " . $this->_id;
392                if (!$GLOBALS['mydms']->db->getResult($queryStr))
393                        return false;
394                $queryStr = "DELETE FROM phpgw_mydms_ACLs WHERE target = ". $this->_id. " AND targetType = " . T_FOLDER;
395                if (!$GLOBALS['mydms']->db->getResult($queryStr))
396                        return false;
397                $queryStr = "DELETE FROM phpgw_mydms_Notify WHERE target = ". $this->_id. " AND targetType = " . T_FOLDER;
398                if (!$GLOBALS['mydms']->db->getResult($queryStr))
399                        return false;
400               
401                return true;
402        }
403
404
405        function getAccessList()
406        {
407
408                if ($this->inheritsAccess())
409                {
410                        $res = $this->getParent();
411                        if (!$res) return false;
412                        return $this->_parent->getAccessList();
413                }
414               
415                if (!isset($this->_accessList))
416                {
417                        $queryStr = "SELECT * FROM phpgw_mydms_ACLs WHERE targetType = ".T_FOLDER." AND target = " . $this->_id . " ORDER BY targetType";
418
419                        $resArr = $GLOBALS['mydms']->db->getResultArray($queryStr);
420
421
422                        if (is_bool($resArr) && !$resArr)
423                                return false;
424                       
425                        $this->_accessList = array("groups" => array(), "users" => array());
426                        foreach ($resArr as $row)
427                        {
428                                if ($row["userid"] != -1)
429                                        array_push($this->_accessList["users"], new UserAccess($row["userid"], $row["mode"]));
430                                else //if ($row["groupID"] != -1)
431                                        array_push($this->_accessList["groups"], new GroupAccess($row["groupid"], $row["mode"]));
432                        }
433                }
434
435                return $this->_accessList;
436        }
437
438        function clearAccessList()
439        {
440                $queryStr = "DELETE FROM phpgw_mydms_ACLs WHERE targetType = " . T_FOLDER . " AND target = " . $this->_id;
441                if (!$GLOBALS['mydms']->db->getResult($queryStr))
442                        return false;
443               
444                unset($this->_accessList);
445                return true;
446        }
447
448        function addAccess($mode, $userOrGroupID, $isUser)
449        {
450                $userOrGroup = ($isUser) ? "userid" : "groupid";
451               
452                $queryStr = "INSERT INTO phpgw_mydms_ACLs (target, targetType, ".$userOrGroup.", mode) VALUES
453                                        (".$this->_id.", ".T_FOLDER.", " . $userOrGroupID . ", " .$mode. ")";
454                if (!$GLOBALS['mydms']->db->getResult($queryStr))
455                        return false;
456               
457                unset($this->_accessList);
458                return true;
459        }
460
461        function changeAccess($newMode, $userOrGroupID, $isUser)
462        {
463                $userOrGroup = ($isUser) ? "userID" : "groupID";
464               
465                $queryStr = "UPDATE phpgw_mydms_ACLs SET mode = " . $newMode . " WHERE targetType = ".T_FOLDER." AND target = " . $this->_id . " AND " . $userOrGroup . " = " . $userOrGroupID;
466                if (!$GLOBALS['mydms']->db->getResult($queryStr))
467                        return false;
468               
469                unset($this->_accessList);
470                return true;
471        }
472
473        function removeAccess($userOrGroupID, $isUser)
474        {
475                $userOrGroup = ($isUser) ? "userID" : "groupID";
476               
477                $queryStr = "DELETE FROM phpgw_mydms_ACLs WHERE targetType = ".T_FOLDER." AND target = ".$this->_id." AND ".$userOrGroup." = " . $userOrGroupID;
478                if (!$GLOBALS['mydms']->db->getResult($queryStr))
479                        return false;
480               
481                unset($this->_accessList);
482                return true;
483        }
484
485        /*
486         * Liefert die Art der Zugriffsberechtigung fï¿œr den User $user; Mï¿œgliche Rechte: n (keine), r (lesen), w (schreiben+lesen), a (alles)
487         * Zunï¿œchst wird Geprï¿œft, ob die Berechtigung geerbt werden soll; in diesem Fall wird die Anfrage an den Eltern-Ordner weitergeleitet.
488         * Ansonsten werden die ACLs durchgegangen: Die hï¿œchstwertige Berechtigung gilt.
489         * Wird bei den ACLs nicht gefunden, wird die Standard-Berechtigung zurï¿œckgegeben.
490         * Ach ja: handelt es sich bei $user um den Besitzer ist die Berechtigung automatisch "a".
491         */
492        function getAccessMode($user)
493        {
494                GLOBAL $settings;
495
496
497                //Admin??
498                if ($user->isAdmin())
499                        return M_ALL;
500               
501                //Besitzer ??
502                if ($user->getID() == $this->_ownerID)
503                        return M_ALL;
504               
505                //Gast-Benutzer??
506                if (($user->getID() == $settings->_guestID) && ($settings->_enableGuestLogin))
507                {
508                        $mode = $this->getDefaultAccess();
509                        if ($mode >= M_READ)
510                                return M_READ;
511                        else
512                                return M_NONE;
513                }
514
515                //Berechtigung erben??
516                // wird ï¿œber GetAccessList() bereits realisiert.
517                // durch das Verwenden der folgenden Zeilen wï¿œren auch Owner-Rechte vererbt worden.
518                /*
519                if ($this->inheritsAccess())
520                {
521                        if (isset($this->_parentID))
522                        {
523                                if (!$this->getParent())
524                                        return false;
525                                return $this->_parent->getAccessMode($user);
526                        }
527                }
528                */
529               
530                $highestPrivileged = M_NONE;
531               
532                //ACLs durchforsten
533                $foundInACL = false;
534                $accessList = $this->getAccessList();
535                if (!$accessList) {
536                        return false;
537                }
538
539                foreach ($accessList["users"] as $userAccess)
540                {
541
542
543                        if ($userAccess->getUserID() == $user->getID())
544                        {
545                                $foundInACL = true;
546                                if ($userAccess->getMode() > $highestPrivileged)
547                                        $highestPrivileged = $userAccess->getMode();
548                                if ($highestPrivileged == M_ALL) //hï¿œher geht's nicht -> wir kï¿œnnen uns die arbeit schenken
549                                        return $highestPrivileged;
550                        }
551                }
552                foreach ($accessList["groups"] as $groupAccess)
553                {
554                        if ($user->isMemberOfGroup($groupAccess->getGroup()))
555                        {
556                                $foundInACL = true;
557                                if ($groupAccess->getMode() > $highestPrivileged)
558                                        $highestPrivileged = $groupAccess->getMode();
559                                if ($highestPrivileged == M_ALL) //hï¿œher geht's nicht -> wir kï¿œnnen uns die arbeit schenken
560                                        return $highestPrivileged;
561                        }
562                }
563                if ($foundInACL)
564                        return $highestPrivileged;
565               
566                //Standard-Berechtigung verwenden
567                return $this->getDefaultAccess();
568        }
569
570        function getNotifyList()
571        {
572                if (!isset($this->_notifyList))
573                {
574                        $queryStr ="SELECT * FROM phpgw_mydms_Notify WHERE targetType = " . T_FOLDER . " AND target = " . $this->_id;
575                        $resArr = $GLOBALS['mydms']->db->getResultArray($queryStr);
576                        if (is_bool($resArr) && $resArr == false)
577                                return false;
578                       
579                        $this->_notifyList = array("groups" => array(), "users" => array());
580                        foreach ($resArr as $row)
581                        {
582                                if ($row["userid"] != -1)
583                                        array_push($this->_notifyList["users"], getUser($row["userid"]) );
584                                else //if ($row["groupID"] != -1)
585                                        array_push($this->_notifyList["groups"], getGroup($row["groupid"]) );
586                        }
587                }
588                return $this->_notifyList;
589        }
590
591        function addNotify($userOrGroupID, $isUser)
592        {
593                $userOrGroup = ($isUser) ? "userID" : "groupID";
594               
595                $queryStr = "INSERT INTO phpgw_mydms_Notify (target, targetType, " . $userOrGroup . ") VALUES (" . $this->_id . ", " . T_FOLDER . ", " . $userOrGroupID . ")";
596                if (!$GLOBALS['mydms']->db->getResult($queryStr))
597                        return false;
598               
599                unset($this->_notifyList);
600                return true;
601        }
602
603        function removeNotify($userOrGroupID, $isUser)
604        {
605                $userOrGroup = ($isUser) ? "userID" : "groupID";
606               
607                $queryStr = "DELETE FROM phpgw_mydms_Notify WHERE target = " . $this->_id . " AND targetType = " . T_FOLDER . " AND " . $userOrGroup . " = " . $userOrGroupID;
608                if (!$GLOBALS['mydms']->db->getResult($queryStr))
609                        return false;
610               
611                unset($this->_notifyList);
612                return true;
613        }
614}
615
616?>
Note: See TracBrowser for help on using the repository browser.