Lock.php
[Pman.Core] / Lock.php
1 <?php
2
3
4 require_once 'Pman.php';
5
6 class Pman_Core_Lock extends Pman
7 {
8     
9     function getAuth()
10     {
11          $au = $this->getAuthUser();
12         if (!$au) {
13              $this->jerr("Not authenticated", array('authFailure' => true));
14         }
15         $this->authUser = $au;
16         // check that it's a supplier!!!! 
17         
18         return true; 
19     }
20     
21     function get($action)
22     {
23         
24         // default action is to attempt to lock..
25         $action = empty($action) ? 'lock' : 'unlock';
26         $this->$action($curlock);
27         
28        
29         
30         
31     }
32     
33     function unlock($curlock)
34     {
35     
36         if (empty($_REQUEST['id'])) {
37             $this->jerr("No lock id");
38         }
39         $curlock = DB_DataObject::factory('Core_locking');
40         if (!$curlock->get($_REQUEST['id'])) {
41             $this->jerr("No lock exists");
42         }
43         
44         if ($curlock->person_id != $this->authUser->id) {
45             $this->jerr("Lock id is invalid");
46         }
47         
48         $curlock->delete();
49         
50         $this->jok('unlocked');
51     }
52     function lock()
53     {
54         
55         if (empty($_REQUEST['on_id']) || empty($_REQUEST['on_table'])) {
56             $this->jerr("Missing table or id");
57         }
58        
59         $tab = str_replace('/', '',$_REQUEST['on_table']); // basic protection??
60         $x = DB_DataObject::factory($tab);
61         if (!$x->get($_REQUEST['on_id'])) {
62             $this->jerr("Item does not exist");
63         }
64         // is there a current lock on the item..
65         
66         $curlock = DB_DataObject::factory('Core_locking');
67         $curlock->setFrom(array(
68             'on_id' => $_REQUEST['on_id'],
69             'on_table' => $_REQUEST['on_table']
70         ));
71         if ($curlock->count()) {
72             $err  = $this->canUnlock();
73             if ($err !== true) {
74                 $this->jerr($err);
75             }
76         }
77         // make a lock..
78         
79         $curlock = DB_DataObject::factory('Core_locking');
80         $curlock->setFrom(array(
81             'on_id' => $_REQUEST['on_id'],
82             'on_table' => $_REQUEST['on_table'],
83             'created' => date('Y-m-d H:i:s'),
84             'person_id' => $this->authUser->id,
85         ));
86         $id = $curlock->insert();
87         $this->jok($id);
88         
89     }
90     
91     function canUnlock()
92     {
93         // the only scenario where we can automatically unlock is:::
94         
95         // this user owns the lock.
96         
97         $curlock = DB_DataObject::factory('Core_locking');
98         $curlock->setFrom(array(
99             'on_id' => $_REQUEST['on_id'],
100             'on_table' => $_REQUEST['on_table']
101         ));
102         $cc = clone($curlock);
103         // the user who owns the lock is not logged in.. ?? - their last 
104         $curlock->find();
105         $u = false;
106         while ($curlock->fetch()) {
107             $u = DB_DataObject::factory('Person');
108             $u->get($curlock->person_id);
109             if (!$u->isCurrentlyLoggedIn()) {
110                 $cc = clone($curlock);
111                 $cc->delete();
112                 $u = false;
113                 continue;
114             }
115             break;
116             
117         }
118         if ($u) {
119             $this->jerr("Item is Locked by " . $u->name . ' (' . $u->email . "),  Try asking them to log out");
120             
121         }
122         return true;
123         
124         
125         
126         
127         
128     }
129     
130         
131     
132 }