CBlogPost :: GetSocNetPostPerms: Bitrix method

      
<?php 
//  CBlogPost :: GetSocNetPostPerms()
//  /dist/bitrix/modules/blog/general/blog_post.php:1497

    
public static function GetSocNetPostPerms($postId 0$bNeedFull false$userId false$postAuthor 0)
    {
        global 
$USER;

        
$cId md5(serialize(func_get_args()));

        if (
            
is_array($postId)
            && isset(
$postId["POST_ID"])
        )
        {
            
$arParams $postId;
            
$postId intval($arParams["POST_ID"]);
            
$bNeedFull = (isset($arParams["NEED_FULL"]) ? $arParams["NEED_FULL"] : false);
            
$userId = (isset($arParams["USER_ID"]) ? $arParams["USER_ID"] : false);
            
$postAuthor = (isset($arParams["POST_AUTHOR_ID"]) ? $arParams["POST_AUTHOR_ID"] : 0);
            
$bpublic = (isset($arParams["PUBLIC"]) ? $arParams["PUBLIC"] : false);
            
$logId = (isset($arParams["LOG_ID"]) ? intval($arParams["PUBLIC"]) : false);
            
$bIgnoreAdmin = (isset($arParams["IGNORE_ADMIN"]) ? $arParams["IGNORE_ADMIN"] : false);
        }
        else
        {
            
$bpublic $logId $bIgnoreAdmin false;
        }

        if(!
$userId)
        {
            
$userId IntVal($USER->GetID());
            
$bByUserId false;
        }
        else
        {
            
$userId IntVal($userId);
            
$bByUserId true;
        }
        
$postId IntVal($postId);
        if(
$postId <= 0)
        {
            return 
false;
        }

        if (!empty(static::
$arSocNetPostPermsCache[$cId]))
        {
            return static::
$arSocNetPostPermsCache[$cId];
        }

        if (!
CModule::IncludeModule("socialnetwork"))
        {
            return 
false;
        }

        
$perms BLOG_PERMS_DENY;
        
$arAvailPerms array_keys($GLOBALS["AR_BLOG_PERMS"]);

        if(!
$bByUserId)
        {
            if (
CSocNetUser::IsCurrentUserModuleAdmin())
            {
                
$perms $arAvailPerms[count($arAvailPerms) - 1]; // max
            
}
        }
        elseif(
            !
$bIgnoreAdmin
            
&& CSocNetUser::IsUserModuleAdmin($userId)
        )
        {
            
$perms $arAvailPerms[count($arAvailPerms) - 1]; // max
        
}

        if(
IntVal($postAuthor) <= 0)
        {
            
$dbPost CBlogPost::GetList(array(), array("ID" => $postId), falsefalse, array("ID""AUTHOR_ID"));
            
$arPost $dbPost->Fetch();
        }
        else
        {
            
$arPost["AUTHOR_ID"] = $postAuthor;
        }

        if(
$arPost["AUTHOR_ID"] == $userId)
        {
            
$perms BLOG_PERMS_FULL;
        }

        if(
$perms <= BLOG_PERMS_DENY)
        {
            
$arPerms CBlogPost::GetSocNetPerms($postId);

            if (
                
intval($userId) > 0
                
&& IsModuleInstalled('mail')
            ) 
// check for email authorization users
            
{
                
$rsUsers CUser::GetList(
                    (
$by="ID"),
                    (
$order="asc"),
                    array(
                        
"ID" => $userId
                    
),
                    array(
                        
"FIELDS" => array("ID""EXTERNAL_AUTH_ID"),
                        
"SELECT" => array("UF_DEPARTMENT")
                    )
                );

                if(
$arUser $rsUsers->Fetch())
                {
                    if (
$arUser["EXTERNAL_AUTH_ID"] == 'email')
                    {
                        return (
                            isset(
$arPerms["U"])
                            && isset(
$arPerms["U"][$userId])
                                ? 
BLOG_PERMS_WRITE
                                
BLOG_PERMS_DENY
                        
);
                    }
                    elseif (
                        
$bPublic
                        
&& (
                            !
is_array($arUser["UF_DEPARTMENT"])
                            || empty(
$arUser["UF_DEPARTMENT"])
                            || 
intval($arUser["UF_DEPARTMENT"][0]) <= 0
                        
)
                        && 
CModule::IncludeModule('extranet')
                        && (
$extranet_site_id CExtranet::GetExtranetSiteID()) // for extranet users in public section
                    
)
                    {
                        if (
$logId)
                        {
                            
$arPostSite = array();
                            
$rsLogSite CSocNetLog::GetSite($logId);
                            while (
$arLogSite $rsLogSite->Fetch())
                            {
                                
$arPostSite[] = $arLogSite["LID"];
                            }

                            if (!
in_array($extranet_site_id$arPostSite))
                            {
                                return 
BLOG_PERMS_DENY;
                            }
                        }
                        else
                        {
                            return 
BLOG_PERMS_DENY;
                        }
                    }
                }
                else
                {
                    return 
BLOG_PERMS_DENY;
                }
            }

            
$arEntities = Array();
            if (!empty(static::
$arUACCache[$userId]))
            {
                
$arEntities = static::$arUACCache[$userId];
            }
            else
            {
                
$arCodes CAccess::GetUserCodesArray($userId);
                foreach(
$arCodes as $code)
                {
                    if (
                        
preg_match('/^DR([0-9]+)/'$code$match)
                        || 
preg_match('/^D([0-9]+)/'$code$match)
                        || 
preg_match('/^IU([0-9]+)/'$code$match)
                    )
                    {
                        
$arEntities["DR"][$code] = $code;
                    }
                    elseif (
preg_match('/^SG([0-9]+)_([A-Z])/'$code$match))
                    {
                        
$arEntities["SG"][$match[1]][$match[2]] = $match[2];
                    }
                }
                static::
$arUACCache[$userId] = $arEntities;
            }

            foreach(
$arPerms as $t => $val)
            {
                foreach(
$val as $id => $p)
                {
                    if(!
is_array($p))
                    {
                        
$p = array();
                    }
                    if(
$userId && $t == "U" && $userId == $id)
                    {
                        
$perms BLOG_PERMS_WRITE;
                        if(
in_array("US".$userId$p)) // if author
                            
$perms BLOG_PERMS_FULL;
                        break;
                    }
                    if(
in_array("G2"$p))
                    {
                        
$perms BLOG_PERMS_WRITE;
                        break;
                    }
                    if(
$userId && in_array("AU"$p))
                    {
                        
$perms BLOG_PERMS_WRITE;
                        break;
                    }
                    if(
$t == "SG")
                    {
                        if(!empty(
$arEntities["SG"][$id]))
                        {
                            foreach(
$arEntities["SG"][$id] as $gr)
                            {
                                if(
in_array("SG".$id."_".$gr$p))
                                {
                                    
$perms BLOG_PERMS_READ;
                                    break;
                                }
                            }
                        }
                    }

                    if(
$t == "DR" && !empty($arEntities["DR"]))
                    {
                        if(
in_array("DR".$id$arEntities["DR"]))
                        {
                            
$perms BLOG_PERMS_WRITE;
                            break;
                        }
                    }
                }

                if(
$perms BLOG_PERMS_DENY)
                {
                    break;
                }
            }

            if(
                
$perms <= BLOG_PERMS_READ
                
&& !empty($arPerms['SG'])
            ) 
// check OSG
            
{
                
$bOpenedSGFound false;
                foreach(
$arPerms['SG'] as $arSGPerm)
                {
                    if (!empty(
$arSGPerm))
                    {
                        foreach(
$arSGPerm as $sgPerm)
                        {
                            if (
preg_match('/^OSG(\d+)_'.(!$userId SONET_ROLES_ALL SONET_ROLES_AUTHORIZED).'$/'$sgPerm$matches))
                            {
                                
$bOpenedSGFound true;
                                break;
                            }
                        }

                        if (
$bOpenedSGFound)
                        {
                            break;
                        }
                    }
                }

                if (
$bOpenedSGFound)
                {
                    
$perms BLOG_PERMS_READ;
                }
            }

            if(
                
$bNeedFull
                
&& $perms BLOG_PERMS_FULL
            
)
            {
                
$arGroupsId = Array();
                if(!empty(
$arPerms["SG"]))
                {
                    foreach(
$arPerms["SG"] as $gid => $val)
                    {
                        if(!empty(
$arEntities["SG"][$gid]))
                            
$arGroupsId[] = $gid;
                    }
                }

                
$operation = Array("full_post""moderate_post""write_post""premoderate_post");
                if(!empty(
$arGroupsId))
                {
                    foreach(
$operation as $v)
                    {
                        if(
$perms <= BLOG_PERMS_READ)
                        {
                            
$f CSocNetFeaturesPerms::GetOperationPerm(SONET_ENTITY_GROUP$arGroupsId"blog"$v);
                            if(
is_array($f))
                            {
                                foreach(
$f as $gid => $val)
                                {
                                    if(
in_array($val$arEntities["SG"][$gid]))
                                    {
                                        switch(
$v)
                                        {
                                            case 
"full_post":
                                                
$perms BLOG_PERMS_FULL;
                                                break;
                                            case 
"moderate_post":
                                                
$perms BLOG_PERMS_MODERATE;
                                                break;
                                            case 
"write_post":
                                                
$perms BLOG_PERMS_WRITE;
                                                break;
                                            case 
"premoderate_post":
                                                
$perms BLOG_PERMS_PREMODERATE;
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    
// check if landing
                    
if ($perms BLOG_PERMS_READ)
                    {
                        
$res = \Bitrix\Socialnetwork\WorkgroupTable::getList([
                            
'filter' => [
                                
'@ID' => $arGroupsId,
                                
'ACTIVE' => 'Y',
                                
'LANDING' => 'Y'
                            
],
                            
'select' => ['ID']
                        ]);
                        if (
$res->fetch())
                        {
                            
$perms BLOG_PERMS_READ;
                        }
                    }
                }
            }
        }

        static::
$arSocNetPostPermsCache[$cId] = $perms;

        return 
$perms;
    }