2 function hasAncestor($ancest, $n_id){
3 foreach($ancest as $one){
4 if($one['link'] == $n_id){ return true;}
8 function getAncestors($node_id){
10 $q="select node_vector from nodes where node_id=".$node_id;
11 $result=$db->query($q);
12 if ($result->next()) {
13 $node=$result->getRecord();
14 $node['node_vector']=trim($node['node_vector'],"z");
15 $ancestors=explode(' ',chunk_split($node['node_vector'],VECTOR_CHARS,' '));
16 foreach ($ancestors as $ancestor) {
17 $anc[]=array("name"=>"","link"=>ltrim($ancestor,"0"));
22 function getCommanders($node_id) {
24 $set=$db->query("select node_permission,users.login,users.user_id from node_access left join users on node_access.user_id=users.user_id where node_id='$node_id' and node_permission!='' order by node_permission");
25 while ($set->next()) {
26 $commanders[$set->getString('node_permission')][]=$set->getString('user_id');
30 function isCommander($comms,$user_id){
31 foreach($comms['master'] as $one){
32 if($one == $user_id){return true;}
34 foreach($comms['op'] as $one){
35 if($one == $user_id){return true;}
37 foreach($comms['execute'] as $one){
38 if($one == $user_id){return true;}
42 function isSenatOwner($senat_id, $user_id){
44 $set=$db->query("select node_creator from nodes where node_id='$senat_id'");
46 if(($set->getString('user_id')) == $user_id){return true;}
52 global $db,$node,$error,$error_messages;
53 $user_id=$_SESSION['user_id'];
54 $user_name=$_SESSION['user_name'];
55 $kset=$db->query("select user_k from users where user_id='$user_id'");
57 $user_k=$kset->getString('user_k');
62 $comms = getCommanders($K_id);
63 $isComm = isCommander($comms,$user_id);
64 $isSOwner = isSenatOwner($senat_id, $user_id);
66 if (is_array($_POST['node_chosen'])) {
67 $k=$_POST['node_chosen'];
69 else $k[]=$node['node_id'];
70 $db->query("set autocommit=0");
76 if ($k == 0) { continue; }
80 $isSenat = hasAncestor(getAncestors($id), $senat_id);
81 if ($isSenat && !($isComm || $isSOwner)){
82 $error.="Sorry, ale v senate mozu Kckovat len obcania.";
84 $userset=$db->query("select node_creator from nodes where node_id='$id'");
86 $acceptor_id=$userset->getString('node_creator');
88 $set=$db->query("select * from node_access where node_id='$id' and user_id='$user_id' and given_k='yes'");
89 if ($set->getNumRows()) {
90 $error.=$error_messages['K_GIVEN'];
93 $db->query("update users set k_wallet=k_wallet+1 where user_id='$acceptor_id'");
95 $db->query("update nodes set k=k+1 where node_id='$id'");
96 $db->query("insert into I set node_id='$id'");
97 $result=$db->update("update node_access set given_k='yes' where node_id='$id' and user_id='$user_id'");
99 $db->query("insert into node_access set given_k='yes',node_id='$id',user_id='$user_id',last_visit=NOW()");
106 $error.=$error_messages['K_SPENT'];
110 $db->query("update users set user_k='$user_k' where user_id='$user_id'");
111 $db->query("commit");
113 if (!$error) return true;