Files
grzanieplus.pl/plugins/stLockPlugin/lib/stLock.class.php
2025-03-12 17:06:23 +01:00

132 lines
3.3 KiB
PHP

<?php
require_once SF_ROOT_DIR . '/lib/stSimpleShopDataCache.class.php';
class stLock
{
const LOCK_BOOTSTRAP_PATH = '/web/errors/unavailable/bootstrap.php';
const MAINTENANCE_LOCK = 'maintenance';
const CACHE_LOCK = 'cache';
public static function hasLock($app = SF_APP, $type = null, $maxLockTime = null)
{
$file = self::getLockFilePath($app, $type);
return file_exists($file) && (null === $maxLockTime || null !== $maxLockTime && time() - filemtime($file) <= $maxLockTime);
}
public static function check($app = SF_APP, $environment = SF_ENVIRONMENT, $checkLicense = true, $type = null)
{
$phpVersion = floatval(phpversion());
if ($phpVersion < 5.6 || $phpVersion > 7.5)
{
return false;
}
if ($phpVersion >= 7.2 && !self::isPHP72Ready())
{
return false;
}
if ($checkLicense)
{
if (!class_exists('stLicenseAbuse') && file_exists($file = SF_ROOT_DIR . '/lib/license/stLicenseAbuse.class.php'))
{
include_once($file);
}
if (class_exists('stLicenseAbuse') && stLicenseAbuse::isOff())
{
return false;
}
}
if ($environment == 'dev')
{
return true;
}
if (isset($_REQUEST['open-key']) && $_REQUEST['open-key'] == md5_file(SF_ROOT_DIR . '/config/databases.yml'))
{
return true;
}
if (self::hasLock($app, $type))
{
return false;
}
return true;
}
public static function sentry($app = SF_APP, $environment = SF_ENVIRONMENT, $checkLicense = true)
{
$sc = stSimpleShopDataCache::getInstance();
if (SF_APP == 'frontend' && !in_array($environment, array('dev', 'theme', 'edit')) && $sc->get('shop_locked') || !self::check($app, $environment, $checkLicense) || self::hasLock($app, self::MAINTENANCE_LOCK) || self::hasLock($app, self::CACHE_LOCK, 30))
{
include_once SF_ROOT_DIR . self::LOCK_BOOTSTRAP_PATH;
exit;
}
}
public static function isPHP72Ready($create = false)
{
$file = SF_ROOT_DIR . '/data/.php72ready';
if ($create)
{
touch($file);
}
return is_file($file);
}
public static function getLockFilePath($app, $type = null)
{
return SF_ROOT_DIR . '/web/uploads/stLock' . self::getNamespace($app, $type) . '.lck';
}
public static function lock($app = SF_APP, $type = null)
{
$file = self::getLockFilePath($app, $type);
if (!is_file($file))
{
file_put_contents($file, time());
}
}
public static function wait($app = SF_APP, $wait = 3)
{
$time = time();
$lock_file = self::getLockFilePath($app);
while (is_file($lock_file) && time() - $time <= $wait)
{
usleep(300000);
}
self::unlock($app);
}
public static function unlock($app = SF_APP, $type = null)
{
$lockFile = self::getLockFilePath($app, $type);
if (file_exists($lockFile))
{
unlink($lockFile);
}
}
protected static function getNamespace($app, $type)
{
if (!class_exists('sfInflector'))
{
require_once SF_ROOT_DIR . '/lib/symfony/util/sfInflector.class.php';
}
return ucfirst(sfInflector::camelize($type ? $type . '_' . $app : $app));
}
}