Пример #1
0
function WDUFUserFieldView(&$arParams, &$arResult)
{
    static $DROPPED = null;
    static $OLD_DROPPED = null;
    if (!(CModule::IncludeModule('iblock') && CModule::IncludeModule('webdav'))) {
        return false;
    }
    if ($DROPPED === null) {
        $DROPPED = CWebDavIblock::getDroppedMetaData();
        $DROPPED = $DROPPED['name'];
        $OLD_DROPPED = CWebDavIblock::getOldDroppedMetaData();
        $OLD_DROPPED = $OLD_DROPPED['name'];
    }
    global $APPLICATION, $USER_FIELD_MANAGER, $USER;
    static $arIBlock = array();
    $result = array();
    $arIBlockCacheID = array();
    $arValue = array();
    $isHistoryDocInComment = false;
    //from historical comment
    $versionHistoryDocInComment = $throughVersionComment = 0;
    $EVId = is_array($arParams["arUserField"]) && $arParams["arUserField"]["ENTITY_VALUE_ID"] > 0 ? intval($arParams["arUserField"]["ENTITY_VALUE_ID"]) : 0;
    $arResult['VALUE'] = is_array($arResult['VALUE']) ? $arResult['VALUE'] : array();
    if ($arParams['arUserField']['USER_TYPE_ID'] == 'webdav_element_history') {
        $isHistoryDocInComment = true;
        //not multiple UF
        if (!empty($arResult['VALUE'][0]['id'])) {
            $arValue[] = (int) $arResult['VALUE'][0]['id'];
            $versionHistoryDocInComment = (int) $arResult['VALUE'][0]['v'];
            $throughVersionComment = empty($arResult['VALUE'][1]['t_vers']) ? 0 : (int) $arResult['VALUE'][1]['t_vers'];
        }
        $cacheID = md5(serialize($arResult['VALUE']));
    } else {
        foreach ($arResult['VALUE'] as $val) {
            $val = intval($val);
            if ($val > 0) {
                $arValue[] = $val;
            }
        }
        $cacheID = $EVId > 0 ? $EVId : md5(serialize($arValue));
    }
    if (!empty($arParams["arUserField"]) && !empty($arParams["arUserField"]['ID'])) {
        $cacheID = (string) $cacheID;
        $cacheID = $arParams["arUserField"]['ID'] . '_' . $cacheID;
    }
    if (sizeof($arValue) > 0) {
        // cache
        $obCache = new CPHPCache();
        $cachePath = SITE_ID . "/webdav/inline";
        if ($obCache->InitCache(30 * 86400, $cacheID, $cachePath)) {
            $vars = $obCache->GetVars();
            $result = $vars["RESULT"];
        }
        if (empty($result) && $obCache->StartDataCache()) {
            $ElementID = $arValue;
            if ($EVId > 0) {
                $ElementID = $USER_FIELD_MANAGER->GetUserFieldValue($arParams["arUserField"]["ENTITY_ID"], $arParams["arUserField"]["FIELD_NAME"], $EVId);
                $ElementID = empty($ElementID) ? $arValue : $ElementID;
                if ($isHistoryDocInComment) {
                    $ElementID = $ElementID[0]['id'];
                }
            }
            // check file exists
            $ibe = new CIBlockElement();
            $dbWDFile = $ibe->GetList(array(), array('ID' => $ElementID), false, false, array('ID', 'NAME', 'IBLOCK_SECTION_ID', 'IBLOCK_ID', 'IBLOCK_CODE', 'PROPERTY_' . CWebDavIblock::PROPERTY_VERSION, 'PROPERTY_WEBDAV_SIZE', 'PROPERTY_FILE', 'CREATED_BY', 'CREATED_USER_NAME', 'CREATED_BY_FORMATTED'));
            if ($dbWDFile) {
                $dbWDFile->SetNameTemplate($arParams['NAME_TEMPLATE']);
                while ($arWDFile = $dbWDFile->Fetch()) {
                    $id = intval($arWDFile['ID']);
                    $arNavChain = array();
                    if (!isset($arIBlock[$arWDFile['IBLOCK_ID']])) {
                        $dbWDIBlock = CIBlock::GetList(array(), array('ID' => $arWDFile['IBLOCK_ID'], 'CHECK_PERMISSIONS' => 'N'));
                        if ($dbWDIBlock && ($arWDIBlock = $dbWDIBlock->Fetch())) {
                            $arIBlock[$arWDFile['IBLOCK_ID']] = $arWDIBlock;
                        }
                    }
                    if (isset($arIBlock[$arWDFile['IBLOCK_ID']])) {
                        $arWDIBlock = $arIBlock[$arWDFile['IBLOCK_ID']];
                        $arIBlockCacheID[] = $arWDFile['IBLOCK_ID'];
                        $arNavChain = WDUGetNavChainSections($arWDFile['IBLOCK_ID'], $arWDFile['IBLOCK_SECTION_ID']);
                        if ($arNavChain === false) {
                            continue;
                            // not show items from trash
                        }
                        // get path to document
                        $detailPath = CWebDavIblock::LibOptions('lib_paths', true, $arWDFile['IBLOCK_ID']);
                        $detailPath = !!$detailPath ? $detailPath : $arWDIBlock['DETAIL_PAGE_URL'];
                        $arPaths = WDUFGetPathOptions($detailPath, $arWDFile['IBLOCK_ID'], reset($arNavChain), $arWDFile);
                        $isSocnet = $arPaths["entity"] != "lib";
                        $arWDFile['VIEW'] = $arPaths["path"];
                        $arWDFile['HISTORY'] = CHTTP::urlAddParams($arPaths['view'], array('webdavForm' . $arWDFile['IBLOCK_ID'] . '_active_tab' => 'tab_history'));
                        $arWDFile['EDIT'] = $arPaths["edit"];
                        $arWDFile['DELETE_DROPPED'] = $arPaths["delete_dropped"];
                        $arWDFile['PATH'] = $arPaths["history_get"];
                        // 'breadcrumb'
                        $arSectionsChain = array();
                        //to link on element
                        $userIBlockID = CWebDavIblock::LibOptions('user_files', false, SITE_ID);
                        $groupIBlockID = CWebDavIblock::LibOptions('group_files', false, SITE_ID);
                        $arUrlSectionsChain = array();
                        $i = 0;
                        foreach ($arNavChain as $res) {
                            $name = $res["NAME"];
                            if ($i == 0 && !!$res["SOCNET_GROUP_ID"] && CModule::IncludeModule('socialnetwork') && strlen(GetMessage('SONET_GROUP_PREFIX')) > 0) {
                                if ($name == GetMessage('SONET_GROUP_PREFIX')) {
                                    $arGroup = CSocNetGroup::GetByID($res["SOCNET_GROUP_ID"]);
                                    $name = GetMessage("SONET_GROUP_PREFIX") . $arGroup['NAME'];
                                }
                            }
                            //drop prefix storage name (1st level in section tree) if user or groups file. If shared docs - don't
                            if ($i != 0 || !$isSocnet) {
                                $arUrlSectionsChain[] = $name;
                            }
                            if ($name != $DROPPED) {
                                $arSectionsChain[] = $name;
                                $i++;
                            }
                            if ($name == $DROPPED || $name == $OLD_DROPPED) {
                                //disable local edit if file id .Dropped
                                $arWDFile['IN_PERSONAL_LIB'] = false;
                            }
                        }
                        if ($arSectionsChain[$i] == $DROPPED) {
                            $arWDFile['NAVCHAIN'] = GetMessage('WDUF_ATTACHED_TO_MESSAGE');
                        } else {
                            if ($userIBlockID && $groupIBlockID) {
                                if ($arWDFile['IBLOCK_ID'] != $userIBlockID['id'] && $arWDFile['IBLOCK_ID'] != $groupIBlockID['id']) {
                                    $name = CIBlock::GetArrayByID($arWDFile['IBLOCK_ID'], 'NAME');
                                    array_unshift($arSectionsChain, $name);
                                }
                            }
                            $arWDFile['NAVCHAIN'] = implode("/", $arSectionsChain);
                        }
                        $arUrlSectionsChain[] = $arWDFile["NAME"];
                        $arWDFile['VIEW'] .= implode('/', $arUrlSectionsChain);
                        //non urnencoded
                        $arWDFile['RELATIVE_PATH'] = $arWDFile['VIEW'];
                        $arWDFile['VIEW'] = CHTTP::urnEncode($arWDFile['VIEW']);
                        // extension
                        $name = $arWDFile['NAME'];
                        $ext = '';
                        $dotpos = strrpos($name, ".");
                        if ($dotpos !== false && $dotpos + 1 < strlen($name)) {
                            $ext = substr($name, $dotpos + 1);
                        }
                        if (strlen($ext) < 3 || strlen($ext) > 5) {
                            $ext = '';
                        }
                        $arWDFile['EXTENSION'] = $ext;
                        // size
                        $arWDFile['SIZE'] = 0;
                        if ($arWDFile['PROPERTY_WEBDAV_SIZE_VALUE']) {
                            $arWDFile['SIZE'] = CFile::FormatSize(intval($arWDFile['PROPERTY_WEBDAV_SIZE_VALUE']), 0);
                        }
                        // file
                        $arWDFile['FILE'] = array();
                        if ($arWDFile['PROPERTY_FILE_VALUE']) {
                            $arWDFile['FILE'] = CFile::GetFileArray($arWDFile['PROPERTY_FILE_VALUE']);
                        }
                        if ($isHistoryDocInComment) {
                            $arWDFile['PATH'] = $arWDFile['PATH'] . "?toWDController=1&ncc=1&downloadHistory=1&id={$arWDFile['ID']}&v={$versionHistoryDocInComment}&f={$arWDFile['FILE']['ID']}";
                            $arWDFile['THROUGH_VERSION'] = empty($throughVersionComment) ? -1 : $throughVersionComment;
                        } else {
                            $arWDFile['THROUGH_VERSION'] = (int) $arWDFile['PROPERTY_' . CWebDavIblock::PROPERTY_VERSION . '_VALUE'];
                        }
                        if (strlen($arWDFile['PATH']) > 0) {
                            $arWDFile['PATH'] = CHTTP::urnEncode($arWDFile['PATH']);
                            $result[$id] = $arWDFile;
                        }
                    }
                }
            }
            global $CACHE_MANAGER;
            $CACHE_MANAGER->StartTagCache($cachePath);
            foreach ($arIBlockCacheID as $ibID) {
                $CACHE_MANAGER->RegisterTag("iblock_id_" . $ibID);
            }
            $CACHE_MANAGER->EndTagCache();
            $obCache->EndDataCache(array("RESULT" => $result));
        }
        // not cached
        // check file access rights
        static $op = 'element_read';
        foreach ($result as $id => $arWDFile) {
            if (!in_array($id, $arValue)) {
                unset($result[$id]);
                continue;
            } else {
                if (!isset($arIBlock[$arWDFile['IBLOCK_ID']])) {
                    continue;
                }
            }
            $arWDIBlock = $arIBlock[$arWDFile['IBLOCK_ID']];
            if ($arWDIBlock['RIGHTS_MODE'] == 'E') {
                $ibRights = CWebDavIblock::_get_ib_rights_object('ELEMENT', $id, $arWDIBlock['ID']);
                if (!$ibRights->UserHasRightTo($arWDIBlock['ID'], $id, $op)) {
                    unset($result[$id]);
                    continue;
                }
            } else {
                if (CIBlock::GetPermission($arWDIBlock['ID']) < 'R') {
                    unset($result[$id]);
                    continue;
                }
            }
        }
    }
    //output
    $arResult['FILES'] = $result;
}