| 1 | <?php |
| 2 | function hasAncestor($ancest, $n_id){ |
| 3 | foreach($ancest as $one){ |
| 4 | if($one['link'] == $n_id){ return true;} |
| 5 | } |
| 6 | return false; |
| 7 | } |
| 8 | function getAncestors($node_id){ |
| 9 | global $db; |
| 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")); |
| 18 | } |
| 19 | return $anc; |
| 20 | }else{ return false;} |
| 21 | } |
| 22 | function getCommanders($node_id) { |
| 23 | global $db; |
| 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'); |
| 27 | } |
| 28 | return $commanders; |
| 29 | } |
| 30 | function isCommander($comms,$user_id){ |
| 31 | foreach($comms['master'] as $one){ |
| 32 | if($one == $user_id){return true;} |
| 33 | } |
| 34 | foreach($comms['op'] as $one){ |
| 35 | if($one == $user_id){return true;} |
| 36 | } |
| 37 | foreach($comms['execute'] as $one){ |
| 38 | if($one == $user_id){return true;} |
| 39 | } |
| 40 | return false; |
| 41 | } |
| 42 | function isSenatOwner($senat_id, $user_id){ |
| 43 | global $db; |
| 44 | $set=$db->query("select node_creator from nodes where node_id='$senat_id'"); |
| 45 | if ($set->next()) { |
| 46 | if(($set->getString('user_id')) == $user_id){return true;} |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | function K() { |
| 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'"); |
| 56 | $kset->next(); |
| 57 | $user_k=$kset->getString('user_k'); |
| 58 | |
| 59 | $senat_id = 876611; |
| 60 | $K_id = 1961061; |
| 61 | $comms = getCommanders($K_id); |
| 62 | $isComm = isCommander($comms,$user_id); |
| 63 | $isSOwner = isSenatOwner($senat_id, $user_id); |
| 64 | |
| 65 | if (is_array($_POST['node_chosen'])) { |
| 66 | $k=$_POST['node_chosen']; |
| 67 | } |
| 68 | else $k[]=$node['node_id']; |
| 69 | $db->query("set autocommit=0"); |
| 70 | |
| 71 | foreach ($k as $id) { |
| 72 | |
| 73 | if ($user_k) { |
| 74 | $isSenat = hasAncestor(getAncestors($id), $senat_id); |
| 75 | if ($isSenat && !($isComm || $isSOwner)){ |
| 76 | $error.="Sorry, ale v senate mozu Kckovat len obcania."; |
| 77 | }else{ |
| 78 | $userset=$db->query("select node_creator from nodes where node_id='$id'"); |
| 79 | $userset->next(); |
| 80 | $acceptor_id=$userset->getString('node_creator'); |
| 81 | |
| 82 | $set=$db->query("select * from node_access where node_id='$id' and user_id='$user_id' and given_k='yes'"); |
| 83 | if ($set->getNumRows()) { |
| 84 | $error.=$error_messages['K_GIVEN']; |
| 85 | }else{ |
| 86 | --$user_k; |
| 87 | $db->query("update users set k_wallet=k_wallet+1 where user_id='$acceptor_id'"); |
| 88 | |
| 89 | $db->query("update nodes set k=k+1 where node_id='$id'"); |
| 90 | $db->query("insert into I set node_id='$id'"); |
| 91 | $result=$db->update("update node_access set given_k='yes' where node_id='$id' and user_id='$user_id'"); |
| 92 | if (!$result) { |
| 93 | $db->query("insert into node_access set given_k='yes',node_id='$id',user_id='$user_id',last_visit=NOW()"); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | else { |
| 100 | $error.=$error_messages['K_SPENT']; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | $db->query("update users set user_k='$user_k' where user_id='$user_id'"); |
| 105 | $db->query("commit"); |
| 106 | |
| 107 | if (!$error) return true; |
| 108 | else return false; |
| 109 | } |
| 110 | |
| 111 | ?> |
| 112 | |
| 113 | |