| 1 | <?php |
| 2 | |
| 3 | class permissions { |
| 4 | |
| 5 | function isHierarch($node) { |
| 6 | |
| 7 | global $db; |
| 8 | if (IsSet($_SESSION['user_id'])) { |
| 9 | $user_id=$_SESSION['user_id']; |
| 10 | } else { |
| 11 | $user_id=0; |
| 12 | } |
| 13 | if (!$user_id) return false; |
| 14 | |
| 15 | $node_vector=chunk_split($node['node_vector'],VECTOR_CHARS,';'); |
| 16 | $hierarchy=explode(';',$node_vector); |
| 17 | foreach ($hierarchy as $hierarch) { |
| 18 | $hierarch=ltrim($hierarch,0); |
| 19 | $q="select nodes.node_creator,node_access.node_permission from nodes left join node_access on nodes.node_id=node_access.node_id and node_access.user_id='".$user_id."' where nodes.node_id='$hierarch'"; |
| 20 | $result=$db->query($q); |
| 21 | $result->next(); |
| 22 | if ($result->getString('node_creator')==$user_id) |
| 23 | return true; |
| 24 | if ($result->getString('node_permission')=='master') |
| 25 | return true; |
| 26 | if ($result->getString('node_creator')=='operator') |
| 27 | return true; |
| 28 | // if ($user_id == 2045) // OMG |
| 29 | // return true; |
| 30 | } |
| 31 | return false; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | function checkPermissions($node) { |
| 36 | global $db; |
| 37 | if (IsSet($_SESSION['user_id'])) { |
| 38 | $user_id=$_SESSION['user_id']; |
| 39 | } else { |
| 40 | $user_id=0; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | thousand lights // OMGto Hierarchy! |
| 45 | (check&set procedure for giving permissions for non-public subnodes according |
| 46 | to bottom-top Hierarchy |
| 47 | */ |
| 48 | if (($node['node_system_access']!='public' and $node['node_system_access']!='crypto') and empty($node['node_permission'])) { |
| 49 | $node_vector=trim(chunk_split($node['node_vector'],VECTOR_CHARS,';'),';'); |
| 50 | $hierarchy=array_reverse(explode(';',$node_vector)); |
| 51 | foreach ($hierarchy as $hierarch) { |
| 52 | $hierarch=ltrim($hierarch,0); |
| 53 | $q="select nodes.node_creator,nodes.node_system_access,node_access.node_permission from nodes left join node_access on nodes.node_id=node_access.node_id and node_access.user_id='".$user_id."' where nodes.node_id='$hierarch'"; |
| 54 | $result=$db->query($q); |
| 55 | $result->next(); |
| 56 | $hierarchy_bounce[]=$hierarch; |
| 57 | |
| 58 | //if hierarch permission rights are different than that of the node, quit the process changing nothing |
| 59 | if ($result->getString('node_system_access')!=$node['node_system_access']) { |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | //if hierarch node_user relation exist, set it for node also |
| 64 | elseif ($result->getString('node_permission')!='') { |
| 65 | array_pop($hierarchy_bounce); |
| 66 | $node['node_permission']=$result->getString('node_permission'); |
| 67 | $q="update node_access set node_permission='".$result->getString('node_permission')."' where node_id='".$node['node_id']."' and user_id='".$user_id."'"; |
| 68 | $updated=$db->update($q); |
| 69 | if (!$updated && IsSet($_SESSION['user_id'])) { |
| 70 | $q="insert into node_access set node_permission='".$result->getString('node_permission')."', node_id='".$node['node_id']."',user_id='".$user_id."'"; |
| 71 | $db->query($q); |
| 72 | } |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | //similiar.if user is creator of hierarch, give him access |
| 77 | elseif ($result->getString('node_creator')==$user_id) { |
| 78 | array_pop($hierarchy_bounce); |
| 79 | $node['node_permission']='access'; |
| 80 | $q="update node_access set node_permission='access' where node_id='".$node['node_id']."' and user_id='".$user_id."'"; |
| 81 | $updated=$db->update($q); |
| 82 | if (!$updated && IsSet($_SESSION['user_id'])) { |
| 83 | $q="insert into node_access set node_permission='access', node_id='".$node['node_id']."',user_id='".$user_id."'"; |
| 84 | $db->query($q); |
| 85 | } |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | } |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | //setting permissions for not logged in users |
| 97 | if ($user_id==$node['node_creator']) { |
| 98 | $permissions['r']=true; |
| 99 | $permissions['w']=true; |
| 100 | } |
| 101 | |
| 102 | elseif (!$user_id) { |
| 103 | |
| 104 | if ($node['node_external_access']=='yes' AND ($node['node_system_access']=='public' OR $node['node_system_access']=='moderated' OR $node['node_system_access']=='cube')) { |
| 105 | $permissions['r']=true; |
| 106 | $permissions['w']=false; |
| 107 | |
| 108 | } |
| 109 | |
| 110 | else { |
| 111 | |
| 112 | $permissions['r']=false; |
| 113 | $permissions['w']=false; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | //setting permissions for cube node |
| 118 | |
| 119 | elseif ($node['node_system_access']=='cube') { |
| 120 | |
| 121 | if (strpos($node['node_vector'],$_SESSION['cube_vector'])==true ) { |
| 122 | $permissions['r']=true; |
| 123 | $permissions['w']=true; |
| 124 | } |
| 125 | elseif (($node['node_permission']=='access')||($node['node_permission']=='op')||($node['node_permission']=='master')) { |
| 126 | $permissions['r']=true; |
| 127 | $permissions['w']=true; |
| 128 | } |
| 129 | |
| 130 | |
| 131 | else { |
| 132 | $permissions['r']=false; |
| 133 | $permissions['w']=false; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | //setting permissions for private node |
| 139 | elseif ($node['node_system_access']=='private') { |
| 140 | if (empty($node['node_permission'])) { |
| 141 | $permissions['r']=false; |
| 142 | $permissions['w']=false; |
| 143 | } |
| 144 | elseif ($node['node_permission']=='ban') { |
| 145 | $permissions['r']=false; |
| 146 | $permissions['w']=false; |
| 147 | } |
| 148 | |
| 149 | elseif($node['node_permission']=='silence') { |
| 150 | $permissions['r']=true; |
| 151 | $permissions['w']=false; |
| 152 | } |
| 153 | else { |
| 154 | |
| 155 | $permissions['r']=true; |
| 156 | $permissions['w']=true; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | //setting permissions for moderated node |
| 161 | elseif ($node['node_system_access']=='moderated') { |
| 162 | if (($node['node_permission']=='access')||($node['node_permission']=='op')||($node['node_permission']=='master')) { |
| 163 | $permissions['r']=true; |
| 164 | $permissions['w']=true; |
| 165 | } |
| 166 | elseif ($node['node_permission']=='ban') { |
| 167 | $permissions['r']=false; |
| 168 | $permissions['w']=false; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | else { |
| 173 | $permissions['r']=true; |
| 174 | $permissions['w']=false; |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | //setting rights for public node |
| 180 | elseif ($node['node_system_access']=='public') { |
| 181 | if ($node['node_permission']=='silence') { |
| 182 | $permissions['r']=true; |
| 183 | $permissions['w']=false; |
| 184 | } |
| 185 | |
| 186 | elseif ($node['node_permission']=='ban') { |
| 187 | $permissions['r']=false; |
| 188 | $permissions['w']=false; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | else { |
| 193 | $permissions['r']=true; |
| 194 | $permissions['w']=true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | else { |
| 200 | $permissions['r']=true; |
| 201 | $permissions['w']=true; |
| 202 | } |
| 203 | return $permissions; |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | |
| 208 | ?> |