source: branches/1.1dev/lib/RecordLock.inc.php

Last change on this file was 708, checked in by anonymous, 4 years ago

Update class constructor method names to construct

File size: 7.9 KB
Line 
1<?php
2/**
3 * The RecordLock:: class provides a system for locking abstract DB rows.
4 *
5 * @author  Quinn Comendant <quinn@strangecode.com>
6 * @version 1.1
7 */
8class RecordLock {
9
10    var $data = array(); // Store lock data from DB.
11    var $timeout = 600; // 10 minutes
12    var $auto_timeout = 1800; // 30 minutes
13    var $_auth; // AuthSQL object from which to access a current user_id.
14
15    /**
16     * Constructor. Optionally select a lock in one swift move.
17     *
18     * @param mixed  $record_table_or_lock_id  The table containing the record to lock,
19     *                                         or a numeric lock_id.
20     * @param string $record_key  The key column for the record to lock.
21     * @param string $record_val  The value of the key column for the record to lock.
22     * @param string $title       A title to apply to the lock, for display purposes.
23     */
24    function __construct($auth_object=null)
25    {
26        // Expire old locks.
27        $this->_auto_timeout();
28       
29        $this->_auth = $auth_object; // FIXME: why does passing by refernce not work here?
30    }
31
32    /**
33     * Select the lock to manipulate.
34     *
35     * @param mixed  $record_table_or_lock_id  The table containing the record to lock,
36     *                                         or a numeric lock_id.
37     * @param string $record_key  The key column for the record to lock.
38     * @param string $record_val  The value of the key column for the record to lock.
39     * @param string $title       A title to apply to the lock, for display purposes.
40     */
41    function select($record_table_or_lock_id, $record_key=null, $record_val=null)
42    {
43        if (is_numeric($record_table_or_lock_id) && !isset($record_key) && !isset($record_val)) {
44            // Get lock data by lock_id.
45            $qid = dbQuery("
46                SELECT * FROM lock_tbl
47                WHERE lock_id = '" . mysql_real_escape_string($record_table_or_lock_id) . "'
48            ");
49        } else {
50            // Get lock data by record specs
51            $qid = dbQuery("
52                SELECT * FROM lock_tbl
53                WHERE record_table = '" . mysql_real_escape_string($record_table_or_lock_id) . "'
54                AND record_key = '" . mysql_real_escape_string($record_key) . "'
55                AND record_val = '" . mysql_real_escape_string($record_val) . "'
56            ");
57        }
58        if ($this->data = mysql_fetch_assoc($qid)) {
59            // This could be integrated into the above query, but with the new auth system, this will be a $auth-> method call.
60//             $qid = dbQuery("SELECT username FROM admin_tbl WHERE admin_id = '" . mysql_real_escape_string($this->data['set_by_admin_id']) . "'");
61//             list($this->data['editor']) = mysql_fetch_row($qid);
62            $this->data['editor'] = $this->_auth->getUsername($this->data['set_by_admin_id']);
63            return true;
64        } else {
65            return false;
66        }
67    }
68
69    /**
70     * Returns true if the record we instantiated with is locked.
71     *
72     * @return bool            True if locked.
73     */
74    function isLocked()
75    {
76        return isset($this->data['lock_id']);
77    }
78
79    /**
80     * Returns the status of who set the lock. Use this to ignore locks set by
81     * the current user.
82     *
83     * @return bool            True if current user set the lock.
84     */
85    function isMine()
86    {
87        if (isset($this->data['lock_id'])) {
88            $qid = dbQuery("SELECT * FROM lock_tbl WHERE lock_id = '" . mysql_real_escape_string($this->data['lock_id']) . "'");
89            if ($lock = mysql_fetch_assoc($qid)) {
90                return ($lock['set_by_admin_id'] == $this->_auth->getVal('user_id'));
91            } else {
92                return false;
93            }
94        } else {
95            return false;
96        }
97    }
98
99    /**
100     * Create a new lock for the specified table/key/value.
101     *
102     * @param string $record_table  The table containing the record to lock.
103     * @param string $record_key  The key column for the record to lock.
104     * @param string $record_val  The value of the key column for the record to lock.
105     * @param string $title       A title to apply to the lock, for display purposes.
106     *
107     * @return int            The id for the lock (mysql last insert id).
108     */
109    function set($record_table, $record_key, $record_val, $title='')
110    {       
111        // Remove previous locks if exist. Is this better than using a REPLACE INTO?
112        dbQuery("
113            DELETE FROM lock_tbl
114            WHERE record_table = '" . mysql_real_escape_string($record_table) . "'
115            AND record_key = '" . mysql_real_escape_string($record_key) . "'
116            AND record_val = '" . mysql_real_escape_string($record_val) . "'
117        ");
118       
119        // Set new lock.
120        dbQuery("
121            INSERT INTO lock_tbl (
122                record_table,
123                record_key,
124                record_val,
125                title,
126                set_by_admin_id,
127                lock_datetime
128            ) VALUES (
129                '" . mysql_real_escape_string($record_table) . "',
130                '" . mysql_real_escape_string($record_key) . "',
131                '" . mysql_real_escape_string($record_val) . "',
132                '" . mysql_real_escape_string($title) . "',
133                '" . mysql_real_escape_string($this->_auth->getVal('user_id')) . "',
134                NOW()
135            )
136        ");
137        $lock_id = mysql_insert_id($GLOBALS['dbh']);
138       
139        // Must register this locked record as the current.
140        $this->select($lock_id);
141       
142        return $lock_id;
143    }
144
145    /**
146     * Unlock the currently selected record.
147     */
148    function remove()
149    {
150        // Delete a specific lock.
151        dbQuery("
152            DELETE FROM lock_tbl
153            WHERE lock_id = '" . mysql_real_escape_string($this->data['lock_id']) . "'
154        ");
155    }
156
157    /**
158     * Unlock all records, or all records for a specified user.
159     */
160    function removeAll($user_id=null)
161    {       
162        if (isset($user_id)) {
163            // Delete specific user's locks.
164            dbQuery("DELETE FROM lock_tbl WHERE set_by_admin_id = '" . mysql_real_escape_string($user_id) . "'");
165            logMsg(sprintf('Record locks owned by %s %s have been deleted', $this->_auth->getVal('auth_name'), $this->_auth->getUsername($user_id)), LOG_INFO, __FILE__, __LINE__);
166        } else {
167            // Delete ALL locks.
168            dbQuery("DELETE FROM lock_tbl");
169            logMsg(sprintf('All record locks deleted by %s %s', $this->_auth->getVal('auth_name'), $this->_auth->getVal('username')), LOG_INFO, __FILE__, __LINE__);
170        }
171    }
172
173    /**
174     * Delete's all locks that are older than auto_timeout.
175     */
176    function _auto_timeout()
177    {
178        // Delete all old locks.
179        dbQuery("
180            DELETE FROM lock_tbl
181            WHERE DATE_ADD(lock_datetime, INTERVAL '" . $this->auto_timeout . "' SECOND) < NOW()
182        ");
183    }
184
185    /**
186     * Redirect to record lock error page.
187     */
188    function dieErrorPage($boomerang_url=null)
189    {
190        global $CFG;
191       
192        dieURL($CFG->admin_url . '/record_lock.php?lock_id=' . $this->data['lock_id'] . '&boomerang=true');
193    }
194
195    /**
196     * Return lock_id of locked record.
197     */
198    function getID()
199    {
200        return $this->data['lock_id'];
201    }
202
203    /**
204     * Return title of locked record.
205     */
206    function getTitle()
207    {
208        return $this->data['title'];
209    }
210
211    /**
212     * Return administrator username for locked record.
213     */
214    function getEditor()
215    {
216        return $this->data['editor'];
217    }
218
219    /**
220     * Return total seconds since the record was locked.
221     */
222    function getSecondsElapsed()
223    {
224        if (isset($this->data['lock_datetime']) && $this->data['lock_datetime'] < time()) {
225            return time() - strtotime($this->data['lock_datetime']);
226        } else {
227            return 0;
228        }
229    }
230
231
232} // End of class.
233?>
Note: See TracBrowser for help on using the repository browser.