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');
61 $comms = getCommanders($K_id);
62 $isComm = isCommander($comms,$user_id);
63 $isSOwner = isSenatOwner($senat_id, $user_id);
65 if (is_array($_POST['node_chosen'])) {
66 $k=$_POST['node_chosen'];
68 else $k[]=$node['node_id'];
69 $db->query("set autocommit=0");
75 if ($k == 0) { continue; }
79 $isSenat = hasAncestor(getAncestors($id), $senat_id);
80 if ($isSenat && !($isComm || $isSOwner)){
81 $error.="Sorry, ale v senate mozu Kckovat len obcania.";
83 $userset=$db->query("select node_creator from nodes where node_id='$id'");
85 $acceptor_id=$userset->getString('node_creator');
87 $set=$db->query("select * from node_access where node_id='$id' and user_id='$user_id' and given_k='yes'");
88 if ($set->getNumRows()) {
89 $error.=$error_messages['K_GIVEN'];
92 $db->query("update users set k_wallet=k_wallet+1 where user_id='$acceptor_id'");
94 $db->query("update nodes set k=k+1 where node_id='$id'");
95 $db->query("insert into I set node_id='$id'");
96 $result=$db->update("update node_access set given_k='yes' where node_id='$id' and user_id='$user_id'");
98 $db->query("insert into node_access set given_k='yes',node_id='$id',user_id='$user_id',last_visit=NOW()");
105 $error.=$error_messages['K_SPENT'];
109 $db->query("update users set user_k='$user_k' where user_id='$user_id'");
110 $db->query("commit");
112 if (!$error) return true;