| Server IP : 37.9.174.138 / Your IP : 216.73.216.117 Web Server : Apache System : Linux vps6.backend.sk 6.12.100+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.100-1 (2026-07-30) x86_64 User : ftpuser ( 1001) PHP Version : 8.4.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/backend-accounting.sk/app/modules/locks/code/ |
Upload File : |
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace modules\locks\code;
/**
* Description of Lock
*
* @author pegas
*/
class Lock extends \app\classes\MNRelation {
const LOCK_INTERVAL = 20; // in lock.js too
private $time;
public function __construct($element_id, $element_table, $id = 0) {
$this->time = time();
parent::__construct($id, "locks", array("element_id" => $element_id, "element_table" => $element_table), "id", false, "SELECT * FROM locks WHERE element_table=:element_table AND element_id=:element_id");
}
/**
* @return Lock
*/
public static function getLock($element_id, $element_table) {
return parent::getMNRelationPrototype($element_id, $element_table);
}
protected static function addRelation($element_id, $element_table) {
$db = \app\classes\PDODatabase::getInstance();
$db->addRecord("locks", array(
"element_id" => $element_id,
"element_table" => $element_table,
"last_lock" => "2000-01-01 00:00:00"
));
}
public function lock($by = false, $throwEx = true) {
if ($by === false) {
try {
$user = \app\classes\BasicUser::getLoggedUser();
$by = $user->_get("email");
} catch (\Exception $ex) {
if ($throwEx) {
throw new \Exception($ex->getMessage(), $ex->getCode(), $ex->getPrevious());
}
return false;
}
}
$db = \app\classes\PDODatabase::getInstance();
$db->sqlCommand("UPDATE locks SET last_lock=:last_lock, last_lock_by=:last_lock_by WHERE element_id=:element_id AND element_table=:element_table "
. "AND (last_lock_by=:last_lock_by OR last_lock < :last_lock_cond)", array(
"last_lock" => date("Y-m-d H:i:s", $this->time),
"last_lock_by" => $by,
"element_id" => $this->_get("element_id"),
"element_table" => $this->_get("element_table"),
"last_lock_cond" => date("Y-m-d H:i:s", time() - 2.5 * self::LOCK_INTERVAL)
));
$success = $db->getValue("SELECT id FROM locks WHERE element_id=:element_id AND element_table=:element_table AND last_lock_by=:last_lock_by AND last_lock=:last_lock", array(
"last_lock" => date("Y-m-d H:i:s", $this->time),
"last_lock_by" => $by,
"element_id" => $this->_get("element_id"),
"element_table" => $this->_get("element_table"),
)) === NULL ? false : true;
if (!$success && $throwEx) {
$lock = Lock::getLock($this->_get("element_id"), $this->_get("element_table"));
throw new \Exception($lock->_get("last_lock_by"), \app\constants\ResponseCodes::LOCKED);
}
return $success;
}
public function unlock($by = false) {
if ($by === false) {
try {
$user = \app\classes\BasicUser::getLoggedUser();
$by = $user->_get("email");
} catch (\Exception $ex) {
return false;
}
}
$db = \app\classes\PDODatabase::getInstance();
$db->sqlCommand("UPDATE locks SET last_lock=:last_lock, last_lock_by=:last_lock_by WHERE element_id=:element_id AND element_table=:element_table "
. "AND (last_lock_by=:last_lock_by OR last_lock < :last_lock_cond)", array(
"last_lock" => date("Y-m-d H:i:s", $this->time - 5 * self::LOCK_INTERVAL),
"last_lock_by" => $by,
"element_id" => $this->_get("element_id"),
"element_table" => $this->_get("element_table"),
"last_lock_cond" => date("Y-m-d H:i:s", time() - 2.5 * self::LOCK_INTERVAL)
));
}
}