summaryrefslogblamecommitdiffstats
path: root/app/class/agent.class.php
blob: 6d0e20d6b0a6852dfb819943493b9d0ca157862e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                                         
                                   
                                   


                                         




                                                                           
                                











                                                                
                                              






                                                               
                                               








                                             

      








                                                                     

















                                                                   




























                                                                              


                                                               
                                              














                                    
                                            




                                             
                                          










                                                               
                                              











                                                        
                                            




                                             
                                          










                                                                 
                                                     











                                                      
                                            




                                                    
                                          










                                                                 
                                                         








                                   
                                            




                                                        
                                          










                                                             
                                                 














                                                      
                                            




                                             
                                          










                                                         
                                                 














                                                      
                                            




                                             
                                          










                                                         
                                                 











                                                         
                                            




                                             
                                          










                                                      
                                                        











                                                       
                                            




                                                    
                                          










                                                          
                                                            








                                   
                                            




                                                        
                                          





                                                       


  
<?php

/*
 * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * For more information, please refer to UNLICENSE
 */

require_once "class/obj.class.php";
require_once "class/pad.class.php";
require_once "class/settings.class.php";
require_once "class/phpmailer.class.php";
require_once "class/smtp.class.php";

/*
 * This is a supertype for users and groups, since these two object types
 * will often be handled polymorphically and will share some functionality.
 */
abstract class agent extends obj
{
    /*
     * Constructor
     */
    public function __construct(?string $guid = NULL)
    {
        parent::__construct($guid);
    }

    /*
     * Check whether this agent is the owner of the given object
     */
    public function isOwnerOf(obj $obj) : bool
    {
        return $obj->getOwner()->guid == $this->guid;
    }

    /*
     * Check whether this agent is a member of the given object
     */
    public function isMemberOf(obj $obj) : bool
    {
        foreach ($obj->getMembers() as $memb)
        {
            if ($memb->guid == $this->guid)
                return true;
        }

        return false;
    }

    /*
     * Send an email message to this agent using stored configuration
     * parameters.  If config is not established, delivery is not
     * attempted.  Return status.
     */
    public abstract function sendEmail(string $subj, string $mesg,
        ?string $attachPath = NULL, ?string $attachName = NULL,
        bool $ignoreEmailConf = false) : bool;

    /*
     * Get the display name for this agent.  For groups this is the
     * object name; for users, this is the object name, unless an
     * alias is set.
     */
    public function getDisplayName() : string
    {
        if ($this->objtype != "user")
            return $this->name;

        $user = new user($this->guid);

        if ($user->alias != "")
            return $user->alias;

        return $user->name;
    }

    /*
     * Get all pads this agent owns or is a member of.  This isn't
     * necessarily all pads this agent has access permission for.
     * Results are sorted by ownership, then by name.
     */
    public function getPads_ordByOwnByName() : array
    {
        $pads = array();

        /* owner */
        $query = "SELECT guid FROM objects WHERE objtype = 'pad' AND " .
            "owner = '" . database::esc($this->guid) . "' ORDER BY name";
        $res = database::query($query);

        foreach ($res as $p)
            $pads[] = new pad($p['guid']);

        /* members */
        $query = "SELECT o.guid FROM objects o JOIN members m ON " .
            "o.guid = m.guid WHERE o.objtype = 'pad' AND " .
            "m.member = '" . database::esc($this->guid) . "' ORDER BY o.name";
        $res = database::query($query);

        foreach ($res as $p)
            $pads[] = new pad($p['guid']);

        return $pads;
    }

    /*
     * Check whether this agent has access permission for given
     * object
     */
    public function canAccess(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj))
            return true;

        if ($obj->pubAcc)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canAccessSub($parent))
                return true;
        }
        else if ($this->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canAccessSub($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has modify permission for given
     * object
     */
    public function canModify(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj) && $obj->membModify)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canModifySub($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canModifySub($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has modify members permission for
     * given object
     */
    public function canModifyMembers(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj) && $obj->membMemb)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canModifySubMembers($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canModifySubMembers($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has modify permissions permission
     * for given object
     */
    public function canModifyPermissions(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canModifySubPermissions($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canModifySubPermissions($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has access-sub permission for
     * given object
     */
    public function canAccessSub(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj) && $obj->membAccs)
            return true;

        if ($obj->pubAccs)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canAccessSub($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canAccessSub($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has create-sub permission
     * for given object
     */
    public function canCreateSub(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj) && $obj->membCres)
            return true;

        if ($obj->pubCres)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canCreateSub($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canCreateSub($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has modify-sub permission
     * for given object
     */
    public function canModifySub(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj) && $obj->membModifys)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canModifySub($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canModifySub($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has modify-sub-members
     * permission for given object
     */
    public function canModifySubMembers(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($this->isMemberOf($obj) && $obj->membMembs)
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canModifySubMembers($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canModifySubMembers($owner))
                return true;
        }

        return false;
    }

    /*
     * Check whether this agent has modify-sub-permissions
     * permission for given object
     */
    public function canModifySubPermissions(obj $obj) : bool
    {
        if ($this->admin)
            return true;

        if ($this->isOwnerOf($obj))
            return true;

        if ($obj->parent)
        {
            $parent = new obj($obj->parent);
            if ($this->canModifySubPermissions($parent))
                return true;
        }
        else if ($obj->owner)
        {
            $owner = new obj($obj->owner);
            if ($this->canModifySubPermissions($owner))
                return true;
        }

        return false;
    }
}

?>