/** * Process the lock * @param string $id * @return boolean */ public static function process($id) { $lock_data = lock::exists($id); if ($lock_data !== false) { $minutes = round(abs(strtotime(format::now()) - strtotime($lock_data)) / 60, 2); if ($minutes > 30) { lock::release($id); $lock_data = false; } } // we are ok to proceed if ($lock_data === false) { lock::create($id); return true; } else { return false; } }
/** * Auto release any open locks on shutdown. * This is required, because we may be using persistent DB connections. */ public function auto_release() { // Called from the shutdown handler. Must release all open locks. foreach ($this->openlocks as $key => $unused) { $lock = new lock($key, $this); $lock->release(); } }
function lock() { require_once $this->controller->rootdir . '/class/lock.class.php'; require_once $this->controller->rootdir . '/class/event.class.php'; $event = new event(); $lock_cmd = $this->response->html->request()->get('lock'); $resource_id = $this->response->html->request()->get('resource_id'); $section = $this->response->html->request()->get('section'); if (!strlen($lock_cmd) || !strlen($resource_id) || !strlen($section)) { $event->log("lock", $_SERVER['REQUEST_TIME'], 2, "openqrm.api.class.php", "Got empty paramater for lock, section or resource_id!", "", "", 0, 0, 0); return; } $lock = new lock(); switch ($lock_cmd) { case 'aquire': $description = $this->response->html->request()->get('description'); $token = $this->response->html->request()->get('token'); $lock_fields['lock_resource_id'] = $resource_id; $lock_fields['lock_section'] = $section; $lock_fields['lock_description'] = $description; $lock_fields['lock_token'] = $token; $lock_id = $lock->add($lock_fields); if (strlen($lock_id)) { echo $lock_id; $event->log("lock", $_SERVER['REQUEST_TIME'], 5, "openqrm.api.class.php", "Section " . $section . " is now locked by " . $resource_id . "!", "", "", 0, 0, 0); } else { $event->log("lock", $_SERVER['REQUEST_TIME'], 2, "openqrm.api.class.php", "Section " . $section . " is still locked!", "", "", 0, 0, 0); } break; case 'release': $lock->get_instance_by_section($section); if (!strlen($lock->id)) { $event->log("lock", $_SERVER['REQUEST_TIME'], 2, "openqrm.api.class.php", "Resource " . $resource_id . " trying to remove lock but no lock active for section " . $section, "", "", 0, 0, 0); return; } if ($resource_id == $lock->resource_id) { $event->log("lock", $_SERVER['REQUEST_TIME'], 5, "openqrm.api.class.php", "Resource " . $resource_id . " released lock for section " . $section, "", "", 0, 0, 0); echo $lock->id; $lock->remove_by_section($section); } else { $event->log("lock", $_SERVER['REQUEST_TIME'], 2, "openqrm.api.class.php", "Resource " . $resource_id . " trying to remove lock from " . $lock->resource_id . " for section " . $section, "", "", 0, 0, 0); } break; } }
/** * Run application * * @param array $options * string application_name * string application_path * string ini_folder * boolean __run_only_bootstrap * @throws Exception */ public static function run($options = []) { // fixing location paths $application_path = isset($options['application_path']) ? rtrim($options['application_path'], '/') . '/' : '../application/'; $application_name = isset($options['application_name']) ? $options['application_name'] : 'default'; $ini_folder = isset($options['ini_folder']) ? rtrim($options['ini_folder'], '/') . '/' : $application_path . 'config/'; // working directory is location of the application chdir($application_path); $application_path_full = getcwd(); // setting include_path $paths = []; $paths[] = $application_path_full; $paths[] = __DIR__; $paths[] = str_replace('/numbers/framework', '', __DIR__); set_include_path(implode(PATH_SEPARATOR, $paths)); // support functions require "functions.php"; // load ini settings self::$settings = system_config::load($ini_folder); // special handling of media files for development, so there's no need to redeploy application if (self::$settings['environment'] == 'development' && isset($_SERVER['REQUEST_URI'])) { system_media::serve_media_if_exists($_SERVER['REQUEST_URI'], $application_path); } // we need to solve chicken and egg problem so we load cache first and then run application //cache::create('php', array('type'=>'php', 'dir'=>'../application/cache')); // setting variables if (!isset(self::$settings['application']) || !is_array(self::$settings['application'])) { self::$settings['application'] = []; } self::$settings['application']['name'] = $application_name; self::$settings['application']['path'] = $application_path; self::$settings['application']['path_full'] = $application_path_full . '/'; self::$settings['application']['loaded_classes'] = []; // class paths self::$settings['layout'] = []; // layout settings // flags self::$settings['flag'] = isset(self::$settings['flag']) && is_array(self::$settings['flag']) ? self::$settings['flag'] : []; self::$settings['flag']['global']['__run_only_bootstrap'] = !empty($options['__run_only_bootstrap']); // magic variables processed here self::$settings['flag']['global']['__content_type'] = 'text/html'; self::process_magic_variables(); // processing php settings if (isset(self::$settings['php'])) { foreach (self::$settings['php'] as $k => $v) { if (is_array($v)) { foreach ($v as $k2 => $v2) { if (is_numeric($v2)) { $v2 = $v2 * 1; } ini_set($k . '.' . $k2, $v2); } } else { if (is_numeric($v)) { $v = $v * 1; } ini_set($k, $v); } } } // Destructor register_shutdown_function(array('bootstrap', 'destroy')); // error handler first error_base::init(); // debug after error handler debug::init(self::get('debug')); // Bootstrap Class $bootstrap = new bootstrap(); $bootstrap_methods = get_class_methods($bootstrap); foreach ($bootstrap_methods as $method) { if (strpos($method, 'init') === 0) { call_user_func(array($bootstrap, $method), $options); } } // if we are calling application from the command line if (!empty($options['__run_only_bootstrap'])) { // dispatch before, in case if we open database connections in there if (!empty(self::$settings['application']['dispatch']['before_controller'])) { call_user_func(self::$settings['application']['dispatch']['before_controller']); } return; } // processing mvc settings self::set_mvc(); // check if controller exists if (!file_exists(self::$settings['mvc']['controller_file'])) { throw new Exception('Resource not found!', -1); } // initialize the controller $controller_class = self::$settings['mvc']['controller_class']; $controller = new $controller_class(); self::$settings['controller'] = get_object_vars($controller); // dispatch before, we need some settings from the controller if (!empty(self::$settings['application']['dispatch']['before_controller'])) { call_user_func(self::$settings['application']['dispatch']['before_controller']); } // singleton start if (!empty(self::$settings['controller']['singleton_flag'])) { $message = !empty(self::$settings['controller']['singleton_message']) ? self::$settings['controller']['singleton_message'] : 'This script is being run by another user!'; $lock_id = "singleton_" . $controller_class; if (lock::process($lock_id) === false) { throw new Exception($message); } } // process parameters and provide output self::process(); // release singleton lock if (!empty(self::$settings['controller']['singleton_flag'])) { lock::release($lock_id); } // dispatch after controller if (!empty(self::$settings['application']['dispatch']['after_controller'])) { call_user_func(self::$settings['application']['dispatch']['after_controller']); } // headers if (!empty(self::$settings['header']) && !headers_sent()) { foreach (self::$settings['header'] as $k => $v) { header($v); } } }
public static function delData() { if (self::lock() !== FALSE) { unlink(self::$lock_data_file); self::log('Data deleted'); lock::unlock(false); } else { self::log('ERROR! Attempted data delete, but file was locked!'); } }
/** * Extend a lock that was previously obtained with @lock. * @param lock $lock - a lock obtained from this factory. * @param int $maxlifetime - the new lifetime for the lock (in seconds). * @return boolean - true if the lock was extended. */ public function extend_lock(lock $lock, $maxlifetime = 86400) { $now = time(); $expires = $now + $maxlifetime; $params = array('expires' => $expires, 'token' => $lock->get_key()); $sql = 'UPDATE {lock_db} SET expires = :expires, WHERE owner = :token'; $this->db->execute($sql, $params); $countparams = array('owner' => $lock->get_key()); $result = $this->count_records('lock_db', $countparams); return $result === 0; }
session_start(); include "../../includes/include_geral.inc.php"; include "../../includes/include_geral_II.inc.php"; include "../../includes/classes/lock.class.php"; //print "<script type='text/javascript' src='../../includes/fckeditor/fckeditor.js'></script>"; print "<html>"; include 'includes/header.php'; print "<body onLoad=\"ajaxFunction('divSelProblema', 'showSelProbs.php', 'idLoad', 'prob=idProblema', 'area_cod=idArea', 'area_habilitada=idAreaHabilitada'); ajaxFunction('divProblema', 'showProbs.php', 'idLoad', 'prob=idProblema', 'area_cod=idArea'); checarSchedule(''); ajaxFunction('divInformacaoProblema', 'showInformacaoProb.php', 'idLoad', 'prob=idProblema', 'area_cod=idArea'); \">"; $auth = new auth(); $auth->testa_user($_SESSION['s_usuario'], $_SESSION['s_nivel'], $_SESSION['s_nivel_desc'], 2); $hoje = date("Y-m-d H:i:s"); $hoje2 = date("d/m/Y"); $qry_config = "SELECT * FROM config "; $exec_config = mysql_query($qry_config) or die(TRANS('ERR_TABLE_CONFIG')); $row_config = mysql_fetch_array($exec_config); $LOCK = new lock(); if (isset($_GET['numero'])) { if (isset($_GET['FORCE_EDIT']) && $_GET['FORCE_EDIT'] == 1) { $FORCE_EDIT = 1; } else { $FORCE_EDIT = 0; } $LOCK->setLock($_GET['numero'], $_SESSION['s_uid'], $FORCE_EDIT); } if ($_SESSION['s_nivel'] == 1) { $admin = true; if ($_SESSION['s_allow_date_edit'] == 0) { $allowDateEdit = "readonly"; } else { $allowDateEdit = ""; }
/** * Release a lock that was previously obtained with @lock. * @param lock $lock - a lock obtained from this factory. * @return boolean - true if the lock is no longer held (including if it was never held). */ public function release_lock(lock $lock) { $params = array('locktype' => $this->dblockid, 'token' => $lock->get_key()); $result = $this->db->get_record_sql('SELECT pg_advisory_unlock(:locktype, :token) AS unlocked', $params); $result = $result->unlocked === 't'; if ($result) { unset($this->openlocks[$lock->get_key()]); } return $result; }
* * Use lock::getData() and lock::setData() to get and set persistent data for the lock pid. * Data is saved only on a successful lock-unlock process. If you want to save it in other cases use lock::saveData(); * If you want to delete all data associated with a lock pid, just set the pid, then call lock::delData(); * * @author Vlad Fratila * @version 1.0.0 */ error_reporting(E_ALL); ini_set('memory_limit', '32M'); ini_set('max_execution_time', '300'); //requiring cron lock class require dirname(__FILE__) . '/lock.class.php'; //set the identifier (lock id). //If you use this class in multiple jobs, you need to set this from the cron job file lock::$lid = 'alertecandidati'; //your temp dir, it will hold lock files and logs for all the cron jobs lock::$lock_dir = dirname(__FILE__) . '/tmp/'; /** * If a job fails, the cleanup step will be skipped and the lock will not be removed. * For this case, we write a timestamp inside the .lock file that allows us to implement * timeouts. * * A script times out when $timeout minutes have passed since its inception. * If the script detects a timed out lock, it will overwrite it with a new lock. * Therefore, our jobs will keep running even if one of them failed * * (You will see these events in the logs.) */ lock::$timeout = 4;
/** * Release a lock that was previously obtained with @lock. * @param lock $lock - A lock obtained from this factory. * @return boolean - true if the lock is no longer held (including if it was never held). */ public function release_lock(lock $lock) { $handle = $lock->get_key(); if (!$handle) { // We didn't have a lock. return false; } $result = flock($handle, LOCK_UN); fclose($handle); return $result; }