b954bc4ed502052958b35eb883ac3892b7210587
[mirrors/Kyberia-bloodline.git] / wwwroot / inc / eventz / K.inc
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 // prevent sqli
74 $k = intval($k);
75 if ($k == 0) { continue; }
76
77
78 if ($user_k) {
79 $isSenat = hasAncestor(getAncestors($id), $senat_id);
80 if ($isSenat && !($isComm || $isSOwner)){
81 $error.="Sorry, ale v senate mozu Kckovat len obcania.";
82 }else{
83 $userset=$db->query("select node_creator from nodes where node_id='$id'");
84 $userset->next();
85 $acceptor_id=$userset->getString('node_creator');
86
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'];
90 }else{
91 --$user_k;
92 $db->query("update users set k_wallet=k_wallet+1 where user_id='$acceptor_id'");
93
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'");
97 if (!$result) {
98 $db->query("insert into node_access set given_k='yes',node_id='$id',user_id='$user_id',last_visit=NOW()");
99 }
100 }
101 }
102 }
103
104 else {
105 $error.=$error_messages['K_SPENT'];
106 }
107 }
108
109 $db->query("update users set user_k='$user_k' where user_id='$user_id'");
110 $db->query("commit");
111
112 if (!$error) return true;
113 else return false;
114 }
115
116 ?>
117
118
This page took 1.720405 seconds and 4 git commands to generate.