<?
CModule::IncludeModule("iblock");
// Добавление водяного знака
AddEventHandler("iblock", "OnBeforeIBlockElementAdd", array("SrWatermark", "OnBeforeIBlockElementAdd"));
AddEventHandler("iblock", "OnBeforeIBlockElementUpdate", array("SrWatermark", "OnBeforeIBlockElementUpdate"));
class SrWatermark
{
function OnBeforeIBlockElementAdd (&$arFields)
{
if($arFields["IBLOCK_ID"] == 10){
if (!empty($arFields["PROPERTY_VALUES"][20]))
{
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
$wm = imagecreatefrompng($_SERVER['DOCUMENT_ROOT']."/watermark2.png");
foreach ($arFields["PROPERTY_VALUES"][20] as $key => $value) {
if ($key != 'nnew' && !empty($value["VALUE"]["tmp_name"])) {
$img = imagecreatefromjpeg($value["VALUE"]["tmp_name"]);
imagecopy($img, $wm, imagesx($img) - imagesx($wm), imagesy($img) - imagesy($wm), 0, 0, imagesx($wm), imagesy($wm));
$result = tempnam($tmp_dir, "wmk").".jpg";
$rez = imagejpeg($img, $result, 100);
imagedestroy($img);
if ($rez) $arFields["PROPERTY_VALUES"][20][$key]["VALUE"]["tmp_name"] = $result;
}
}
imagedestroy($wm);
}
}
}
function OnBeforeIBlockElementUpdate (&$arFields)
{
if($arFields["IBLOCK_ID"] == 10){
if (!empty($arFields["PROPERTY_VALUES"][20]))
{
$tmp_dir = ini_get('upload_tmp_dir') ? ini_get('upload_tmp_dir') : sys_get_temp_dir();
$wm = imagecreatefrompng($_SERVER['DOCUMENT_ROOT']."/watermark2.png");
foreach ($arFields["PROPERTY_VALUES"][20] as $key => $value) {
if ($key != 'nnew' && !empty($value["tmp_name"])) {
$img = imagecreatefromjpeg($value["tmp_name"]);
imagecopy($img, $wm, imagesx($img) - imagesx($wm), imagesy($img) - imagesy($wm), 0, 0, imagesx($wm), imagesy($wm));
$result = tempnam($tmp_dir, "wmk").".jpg";
$rez = imagejpeg($img, $result, 100);
imagedestroy($img);
if ($rez) $arFields["PROPERTY_VALUES"][20][$key]["tmp_name"] = $result;
$arFields["PROPERTY_VALUES"][20][$key]["tmp_name"] = $result;
}
}
imagedestroy($wm);
}
}
}
}
class CSubsections
{
function Init()
{
global $APPLICATION;
$sef_folder = (isset($_SERVER["REAL_FILE_PATH"]) && $_SERVER["REAL_FILE_PATH"] != ""
? str_replace("index.php", "", $_SERVER["REAL_FILE_PATH"])
: $APPLICATION->GetCurDir(false)
);
$uri = $GLOBALS["BACK_REQUEST_URI"] = $APPLICATION->GetCurDir(false);
$uri = str_replace($sef_folder, "", $uri);
$uri = trim($uri, "/");
$tmp_uri = "";
if($uri && CModule::IncludeModule("iblock"))
{
$rs = CIBlockElement::GetList(
array(),
array("=CODE" => $uri),
false,
array("nTopCount" => 1),
array("ID", "IBLOCK_ID", "IBLOCK_SECTION_ID", "NAME")
);
if($ar = $rs->Fetch())
{
$tmp_uri = $sef_folder.intval($ar["IBLOCK_SECTION_ID"])."/".$ar["ID"]."/";
}
else
{
$rs = CIBlockSection::GetList(
array(),
array("=CODE" => $uri),
false,
array("ID")
);
if($ar = $rs->Fetch())
{
$tmp_uri = $sef_folder.$ar["ID"]."/";
}
}
}
if($tmp_uri)
{
$_SERVER["REQUEST_URI"] = $REQUEST_URI = $tmp_uri;
$APPLICATION->sDocPath2 = $tmp_uri."index.php";
CSubsections::InitParser();
}
elseif($sef_folder == $APPLICATION->GetCurDir(false))
{
CSubsections::InitParser();
}
}
function Back($arResult)
{
global $APPLICATION;
$arResult["URL_TEMPLATES"]["section"] = "#SECTION_CODE#/";
$arResult["URL_TEMPLATES"]["element"] = "#ELEMENT_CODE#/";
$_SERVER["REQUEST_URI"] = $REQUEST_URI = $GLOBALS["BACK_REQUEST_URI"];
$APPLICATION->sDocPath2 = $GLOBALS["BACK_REQUEST_URI"]."index.php";
unset($GLOBALS["BACK_REQUEST_URI"]);
}
function Parse($html)
{
$html = str_ireplace("%"."2f", "/", $html);
}
function InitParser()
{
if(!defined("CSUBSECTIONS_INIT_HANDLER"))
{
define("CSUBSECTIONS_INIT_HANDLER", true);
AddEventHandler("main", "OnEndBufferContent", array("CSubsections", "Parse"), 10000);
}
}
function GetCode($iblock_id, $section_id, $element_name="")
{
$arPath = array();
if(CModule::IncludeModule("iblock"))
{
if($element_name)
{
$element_name = CUtil::translit($element_name, LANGUAGE_ID);
$element_name = $element_name? "/".$element_name: "";
}
if($rs = CIBlockSection::GetNavChain($iblock_id, $section_id))
{
while($ar = $rs->Fetch())
{
$arPath[] = CUtil::translit($ar["NAME"], LANGUAGE_ID);
}
}
}
return trim(implode("/", $arPath).$element_name, "/");
}
function ElementAddHandler($arFields)
{
//if($GLOBALS["IBLOCK_ID"] == $IBLOCK_ID)
//{
$oElement = new CIBlockElement();
$oElement->Update($arFields["ID"], array("CODE" => "recalculate"));
//}
}
function ElementUpdateHandler($arFields)
{
//if($GLOBALS["IBLOCK_ID"] == $IBLOCK_ID)
//{
if($rs = CIBlockElement::GetByID($arFields["ID"]))
{
if($ar = $rs->Fetch())
{
$arFields["CODE"] = CSubsections::GetCode(
$ar["IBLOCK_ID"],
$ar["IBLOCK_SECTION_ID"],
$ar["NAME"]
);
}
}
//}
}
function SectionAddHandler($arFields)
{
//if($GLOBALS["IBLOCK_ID"] == $IBLOCK_ID)
//{
$oSection = new CIBlockSection();
$oSection->Update($arFields["ID"], array("CODE" => "recalculate"));
//}
}
function SectionUpdateHandler($arFields)
{
//if($GLOBALS["IBLOCK_ID"] == $IBLOCK_ID)
//{
$arFields["CODE"] = CSubsections::GetCode(
$arFields["IBLOCK_ID"],
$arFields["ID"]
);
//}
}
function Recalculate($iblock_id)
{
if(CModule::IncludeModule("iblock"))
{
$oSection = new CIBlockSection();
if($rs = CIBlockSection::GetList(array(), array("IBLOCK_ID" => $iblock_id), false, array("ID")))
{
while($ar = $rs->Fetch())
{
$oSection->Update($ar['ID'], array("CODE" => "recalculate"));
}
}
$oElement = new CIBlockElement();
if($rs = CIBlockElement::GetList(array(), array("IBLOCK_ID" => $iblock_id), false, false, array("ID")))
{
while($ar = $rs->Fetch())
{
$oElement->Update($ar['ID'], array("CODE" => "recalculate"));
}
}
}
}
}
// 404 страница для случая, когда элемент не найден
//AddEventHandler('main', 'OnEpilog', '_Check404Error', 1);
function _Check404Error()
{
if (defined('ERROR_404') && ERROR_404=='Y' && !defined('ADMIN_SECTION'))
{
GLOBAL $APPLICATION;
$APPLICATION->RestartBuffer();
require $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.SITE_TEMPLATE_ID.'/header.php';
require $_SERVER['DOCUMENT_ROOT'].'/404_.php';
require $_SERVER['DOCUMENT_ROOT'].'/bitrix/templates/'.SITE_TEMPLATE_ID.'/footer.php';
}
}
AddEventHandler("main", "OnEndBufferContent", array("CMelcosoft", "OnEndBufferContentHandler"));
class CMelcosoft
{
function OnEndBufferContentHandler(&$content)
{
$content = preg_replace("/(<a.+href?\s*=\s*\")(https|http)(:\/\/.+)(\".*>.*<\/a>)/siU", "$1/go/?$2$3$4", $content);
}
}
//AddEventHandler("iblock", "OnAfterIBlockElementAdd", array("CSubsections", "ElementAddHandler"));
//AddEventHandler("iblock", "OnBeforeIBlockElementUpdate", array("CSubsections", "ElementUpdateHandler"));
//AddEventHandler("iblock", "OnAfterIBlockSectionAdd", array("CSubsections", "SectionAddHandler"));
//AddEventHandler("iblock", "OnBeforeIBlockSectionUpdate", array("CSubsections", "SectionUpdateHandler"));
//CSubsections::InitParser();
?> |