$26 GRAYBYTE WORDPRESS FILE MANAGER $38

SERVER : vnpttt-amd7f72-h1.vietnix.vn #1 SMP Fri May 24 12:42:50 UTC 2024
SERVER IP : 103.200.23.149 | ADMIN IP 216.73.216.22
OPTIONS : CRL = ON | WGT = ON | SDO = OFF | PKEX = OFF
DEACTIVATED : NONE

/opt/cloudlinux/venv/lib/python3.11/site-packages/clwpos/user/

HOME
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

Current_dir [ NOT WRITEABLE ] Document_root [ WRITEABLE ]


[ Back ]
NAME
SIZE
LAST TOUCH
USER
CAN-I?
FUNCTIONS
..
--
16 Dec 2025 9.31 PM
root / root
0755
__pycache__
--
16 Dec 2025 9.38 PM
root / root
0755
website_check
--
16 Dec 2025 9.31 PM
root / root
0755
__init__.py
0 KB
29 Sep 2025 8.34 PM
root / root
0644
cache.py
2.574 KB
29 Sep 2025 8.34 PM
root / root
0644
config.py
13.313 KB
29 Sep 2025 8.34 PM
root / root
0644
progress_check.py
6.608 KB
29 Sep 2025 8.34 PM
root / root
0644
redis_lib.py
4.37 KB
29 Sep 2025 8.34 PM
root / root
0644
wpos_user.py
49.783 KB
29 Sep 2025 8.34 PM
root / root
0644

GRAYBYTE WORDPRESS FILE MANAGER @ 2026 CONTACT ME
Static GIF