403Webshell
Server IP : 192.158.238.246  /  Your IP : 3.15.7.43
Web Server : LiteSpeed
System : Linux uniform.iwebfusion.net 4.18.0-553.27.1.lve.1.el8.x86_64 #1 SMP Wed Nov 20 15:58:00 UTC 2024 x86_64
User : jenniferflocom ( 1321)
PHP Version : 8.1.32
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/user/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/user/cache.py
import hashlib
import logging
import os
import pwd
from contextlib import contextmanager
from dataclasses import dataclass
from typing import ContextManager, Optional

from clwpos import constants, scoped_cache


def _get_cache_directory():
    user = pwd.getpwuid(os.geteuid())
    cache_dir = os.path.join(user.pw_dir, constants.USER_WPOS_DIR, '.cache')
    os.makedirs(cache_dir, exist_ok=True)

    return cache_dir


def _get_wp_config_modification_ts(wp_path):
    try:
        return os.path.getmtime(os.path.join(wp_path, 'wp-config.php'))
    except FileNotFoundError:
        return os.path.getmtime(os.path.join(wp_path, '../wp-config.php'))


@dataclass
class CacheRecord:
    # any data that we would like to save
    # important! do not use pickle or any other serializable
    # data format that might be loaded in root environment
    data: Optional[str]
    # marker which becomes true whenever we change our data
    is_dirty: bool = False
    
    def __setattr__(self, key, value):
        super().__setattr__('is_dirty', True)
        super().__setattr__(key, value)


@contextmanager
def wp_config_cache(key, path) -> ContextManager[CacheRecord]:
    # there is no need to cache these values
    # in not actively requested places
    if not scoped_cache.CACHING_ENABLED:
        yield CacheRecord(data=None)
        return

    ts = _get_wp_config_modification_ts(wp_path=path)

    record = CacheRecord(
        data=get(key, path, valid_after_ts=ts))
    try:
        yield record
    finally:
        if record.is_dirty:
            set(key, path, record.data)

def get(key: str, path: str, valid_after_ts: float) -> Optional[str]:
    cache_file = os.path.join(
        _get_cache_directory(), f'{key}.{hashlib.md5(path.encode()).hexdigest()}.cache')

    try:
        # if file was modified earlier than timestamp,
        # we assume that cache is expired
        if os.path.getmtime(cache_file) < valid_after_ts:
            logging.info('Cache "%s" assumed to be outdated', key)
            return None

        with open(cache_file, 'r') as f:
            cache_info_raw = f.read()
    except (IOError, OSError):
        # assume that file does not exist
        # or we don't have access
        logging.info('Cache "%s" is not existing or malformed', key)
        return None

    return cache_info_raw

def set(key: str, path:str, value: str) -> None:
    cache_file = os.path.join(
        _get_cache_directory(), f'{key}.{hashlib.md5(path.encode()).hexdigest()}.cache')

    try:
        with open(cache_file, 'w') as f:
            f.write(value)
    except (IOError, OSError):
        return None

Youez - 2016 - github.com/yon3zu
LinuXploit