| 1 | <?php |
| 2 | class nodes { |
| 3 | // All mysql code should go here |
| 4 | // Split into multiple files if needed |
| 5 | |
| 6 | function processContent($node_content) { return processContent_hack($node_content); } //XXX TODO FIXME HACK |
| 7 | |
| 8 | // Called for every node view. Updates node views, neurons, |
| 9 | function update_nodes($user_id,$node_id,$referer_id) { |
| 10 | global $node,$db,$error; |
| 11 | $db->update("update nodes set node_views=node_views+1 where". |
| 12 | "node_id='".$node_id."'"); |
| 13 | if (is_numeric($referer_id) && ($referer_id)) { |
| 14 | $q="update neurons set synapse=synapse+1 where ". |
| 15 | "dst='".$node_id."' and src='$referer_id'"; |
| 16 | $result=$db->update($q); |
| 17 | if (!$result) { |
| 18 | $q="insert into neurons set synapse_creator='". |
| 19 | $user_id."',dst='".$node_id. |
| 20 | "',src='$referer_id',synapse=1"; |
| 21 | $db->query($q); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // LEVENSHTEIN |
| 26 | |
| 27 | // these 4 lines are not the source of kyberia lagging problems. |
| 28 | // leave them. started on the 10.4. |
| 29 | // data gained will be used for scientific purposes |
| 30 | if ($user_id) { |
| 31 | $q="insert delayed into levenshtein set user_id='". |
| 32 | $user_id."',node_id='".$node_id."'"; |
| 33 | $db->update($q); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | function putNode($what,$where,$checkpermissions=true) { |
| 39 | global $db,$error,$error_messages; |
| 40 | $user_id = $_SESSION['user_id']; |
| 41 | if (!$user_id) { |
| 42 | $user_id=UBIK_ID; |
| 43 | } |
| 44 | if (!is_numeric($what) || !is_numeric($where)) { |
| 45 | $error=$error_messages['NUMERIC_NOT_NUMERIC']; |
| 46 | } |
| 47 | |
| 48 | $nodeshell = nodes::getNodeById($where,$user_id); |
| 49 | $nodeshell_vector = $nodeshell['node_vector']; |
| 50 | |
| 51 | if ($checkpermissions) { |
| 52 | $nodeshell_permissions = permissions::checkPermissions($nodeshell); |
| 53 | if (!$nodeshell_permissions['w']) { |
| 54 | $error = $error_messages['WRITE_PERMISSION_ERROR']; |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | $dst_vector = $nodeshell_vector.str_pad($chosen,VECTOR_CHARS,0,STR_PAD_LEFT); |
| 60 | $q = "update neurons set synapse_created=NOW(),link='hard',synapse=synapse+1, dst_vector='$dst_vector' where src='$what' and dst='$where'"; |
| 61 | $result=$db->update($q); |
| 62 | |
| 63 | if (!$result) $db->query("insert into neurons set synapse_creator='$user_id',synapse_created=NOW(), src='$what',dst='$where',dst_vector='$dst_vector',link='hard',synapse=1"); |
| 64 | $db->query("update nodes set lastdescendant_created=NOW(),node_children_count=node_children_count+1 where node_id='$where'"); |
| 65 | } |
| 66 | |
| 67 | function addNode($params) { |
| 68 | global $db,$node,$error,$error_messages; |
| 69 | $parent_id=$params['node_parent']; |
| 70 | |
| 71 | if (!is_numeric($parent_id)) { |
| 72 | $parent_id=$node['node_id']; |
| 73 | } |
| 74 | |
| 75 | $parent_permissions=permissions::checkPermissions($parent_id); |
| 76 | if (!$parent_permissions['w']) { |
| 77 | $error=$error_messages['WRITE_PERMISSION_ERROR']; |
| 78 | logger::log('add','error','WRITE_PERMISSION_ERROR'); |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | $kset=$db->query("select user_k from users where user_id='$params[node_creator]'"); |
| 84 | $kset->next(); |
| 85 | $user_k=$kset->getString('user_k'); |
| 86 | |
| 87 | if ($params['flag']=='registration') $params['node_creator']=UBIK_ID; |
| 88 | |
| 89 | if (!$user_k && $params['node_creator']!=UBIK_ID) { |
| 90 | $error=$error_messages['K_SPENT']; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | $set=$db->query("select node_vector,node_children_count from nodes where node_id='$parent_id'"); |
| 97 | $set->next(); |
| 98 | $parent_vector=$set->getString('node_vector'); |
| 99 | |
| 100 | if ($set->getInt('node_children_count')>MAX_CHILDREN) { |
| 101 | $error=$error_messages['MAX_CHILDREN']; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | //working with external links |
| 106 | $external_link=$params['external_link']; |
| 107 | |
| 108 | $template_id=$params['template_id']; |
| 109 | if (!is_numeric($template_id)) $template_id=DEFAULT_TEMPLATE_ID; |
| 110 | |
| 111 | if(!isset($params['node_system_access'])) $params['node_system_access'] = $node['node_system_access']; |
| 112 | if(!isset($params['node_external_access'])) $params['node_external_access'] = $node['node_external_access']; |
| 113 | |
| 114 | $q="insert into nodes set |
| 115 | node_name='".$params['node_name']."', |
| 116 | node_external_access='".$params['node_external_access']."', |
| 117 | node_system_access='".$params['node_system_access']."', |
| 118 | node_creator='".$params['node_creator']."', |
| 119 | template_id='".$template_id."', |
| 120 | external_link='".$external_link."', |
| 121 | node_parent='".$parent_id."', |
| 122 | node_views=0,node_created=NOW(), |
| 123 | node_content='".$params['node_content']."', |
| 124 | node_vector='".$params['node_vector']."'"; |
| 125 | $db->query("start transaction"); |
| 126 | $db->query($q); |
| 127 | $id=$db->getLastInsertId(); |
| 128 | |
| 129 | //node_content MyIsam only for FULLTEXT !!! |
| 130 | // $db->query("insert into node_content set node_id='$id',node_content='".$params['node_content']."'"); |
| 131 | |
| 132 | $new_id=str_pad($id,VECTOR_CHARS,"0",STR_PAD_LEFT); |
| 133 | $new_vector=trim($parent_vector,"z").$new_id; |
| 134 | |
| 135 | if ($params['flag']=='registration') $db->query("update nodes set node_system_access='public',node_external_access='yes',node_creator=$id where node_id=$id"); |
| 136 | |
| 137 | $db->query("update nodes set node_vector='$new_vector' where node_id='$id'"); |
| 138 | $db->query("update nodes set node_children_count=node_children_count+1 where node_id='$parent_id'"); |
| 139 | |
| 140 | $node_vector=trim(chunk_split($new_vector,VECTOR_CHARS,';'),';'); |
| 141 | |
| 142 | $ancestors=explode(";",$node_vector); |
| 143 | foreach($ancestors as $key => $ancestor_id) { |
| 144 | if ($key) { |
| 145 | $ancestor_id=ltrim($ancestor_id,'0'); |
| 146 | $db->query("update nodes set node_descendant_count=node_descendant_count+1,lastdescendant_created=NOW() where node_id='$ancestor_id'"); |
| 147 | } |
| 148 | } |
| 149 | $db->query("update node_access set node_user_subchild_count=node_user_subchild_count+1 where node_id='$parent_id'"); |
| 150 | --$user_k; |
| 151 | $db->query("update users set user_k='$user_k' where user_id='$params[node_creator]'"); |
| 152 | $db->query("commit"); |
| 153 | logger::log('add','ok',$id); |
| 154 | |
| 155 | if ($_POST['code']) { |
| 156 | $params['node_creator']=UBIK_ID; |
| 157 | $params['node_parent']=WARNING_ZONE; |
| 158 | $params['node_name']="node $id added with code_parameter"; |
| 159 | $params['node_content']="node <a href='/id/$id'>$id</a> added with code_parameter"; |
| 160 | unset($_POST['code']); |
| 161 | nodes::addNode($params); |
| 162 | } |
| 163 | |
| 164 | return $id; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | function getUserByLogin($login) { |
| 169 | global $error, $error_messages; |
| 170 | $q2="select user_id from users where login='".$login."'"; |
| 171 | $userset=$db->query($q2); |
| 172 | $userset->next(); |
| 173 | $id=$userset->getString('user_id'); |
| 174 | if (is_numeric($id)) return $id; |
| 175 | else { |
| 176 | $error = $error_messages['USER_NOT_FOUND']; |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static function getNodeIdByName($name, $external_link=false) { |
| 182 | global $db; |
| 183 | |
| 184 | $qh = sprintf('select node_id from nodes where node_name = "%s"', mysql_real_escape_string($name)); |
| 185 | if ($external_link) |
| 186 | $qh .= sprintf(' and external_link="%s"', mysql_real_escape_string($external_link)); |
| 187 | |
| 188 | $set = $db->query($qh); |
| 189 | $set->next(); |
| 190 | return $set->getString('node_id'); |
| 191 | } |
| 192 | |
| 193 | function getNodeById($node_handle,$user_id, $table_name="nodes") { |
| 194 | global $db, $error; |
| 195 | $q="select length(concat($table_name.node_vector)) as |
| 196 | vector_depth,$table_name.*,$table_name.node_creator as |
| 197 | node_owner_id,creator.node_name as owner,node_access.*,$table_name.node_id as |
| 198 | node_id,node_parent.node_name as node_parent_name |
| 199 | from $table_name left join $table_name as creator on creator.node_id=$table_name.node_creator |
| 200 | left join $table_name as node_parent on $table_name.node_parent=node_parent.node_id |
| 201 | left join node_access on (node_access.node_id='$node_handle' and node_access.user_id='$user_id') |
| 202 | where $table_name.node_id='$node_handle'"; |
| 203 | |
| 204 | $result=$db->query($q); |
| 205 | if (!$result->next()) { |
| 206 | return false; |
| 207 | } |
| 208 | else { |
| 209 | $node=addBase36id($result->getRecord()); |
| 210 | $node['node_vector']=trim($node['node_vector'],"z"); |
| 211 | $ancestors=str_split($node['node_vector'],VECTOR_CHARS); |
| 212 | foreach ($ancestors as $ancestor) { |
| 213 | $node['ancestors'][]=array("name"=>"","link"=>ltrim($ancestor,"0")); |
| 214 | } |
| 215 | } |
| 216 | transport_process_node($node); |
| 217 | return $node; |
| 218 | |
| 219 | } |
| 220 | |
| 221 | |
| 222 | function redirByName($node_handle) { |
| 223 | global $db, $error; |
| 224 | $user_id=$_SESSION['user_id']; |
| 225 | $set=$db->query("select node_id from nodes where node_name='$node_handle' and node_creator='$user_id'"); |
| 226 | if ($set->next()) { |
| 227 | $node_id=$set->getString('node_id'); |
| 228 | if (!empty($node_id)) { |
| 229 | return nodes::getNodeById($node_id,$_SESSION['user_id']); |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | $set=$db->query("select node_id from nodes where node_name='$node_handle' "); |
| 234 | $set->next(); |
| 235 | $node_id=$set->getString('node_id'); |
| 236 | if (!empty($node_id)) { |
| 237 | return nodes::getNodeById($node_id,$_SESSION['user_id']); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | function getNodesByName($node_handle) { |
| 243 | global $db, $error; |
| 244 | $q="select nodes.* from nodes where node_name='$node_handle%'"; |
| 245 | |
| 246 | $result=$db->query($q); |
| 247 | |
| 248 | while ($result->next()){ |
| 249 | $record[]=addBase36id($result->getRecord()); |
| 250 | } |
| 251 | return $record; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | |
| 256 | |
| 257 | function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) { |
| 258 | global $db, $error, $node; |
| 259 | $node_handle=$node['node_id']; |
| 260 | |
| 261 | $q="select users.*,nodes.* from nodes |
| 262 | left join users on users.user_id=nodes.node_creator where |
| 263 | node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit"; |
| 264 | echo $q; |
| 265 | $result=$db->query($q); |
| 266 | |
| 267 | while ($result->next()) { |
| 268 | $array[]=addBase36id($result->getRecord()); |
| 269 | } |
| 270 | |
| 271 | return $array; |
| 272 | |
| 273 | } |
| 274 | |
| 275 | |
| 276 | function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) { |
| 277 | global $db, $error, $node; |
| 278 | $node_handle=$node['node_id']; |
| 279 | |
| 280 | $q="select length(node_vector) as depth,users.login,nodes.* from nodes left join users on users.user_id=nodes.node_creator where node_vector like '".$node['node_vector'].";".$node['node_id']."%' and node_type=3 order by concat(node_vector,';',nodes.node_id,';z') desc,depth LIMIT $offset,$limit"; |
| 281 | |
| 282 | $result=$db->query($q); |
| 283 | |
| 284 | while ($result->next()) { |
| 285 | $children_array[]=addBase36id($result->getRecord()); |
| 286 | } |
| 287 | |
| 288 | return $children_array; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | function getNodeAccessData() { |
| 295 | global $node,$db; |
| 296 | $q="select users.login,node_access.* from node_access left join users on users.user_id=node_access.user_id where node_id='".$node['node_id']."' and node_permission!=''"; |
| 297 | $result=$db->query($q); |
| 298 | |
| 299 | while ($result->next()) { |
| 300 | $access_data[]=$result->getRecord(); |
| 301 | } |
| 302 | |
| 303 | return $access_data; |
| 304 | |
| 305 | } |
| 306 | |
| 307 | |
| 308 | // Simple internal function to set node parrent |
| 309 | |
| 310 | function setParent($node_id,$parent_id) { |
| 311 | global $db,$node,$error,$error_messages; |
| 312 | |
| 313 | if (!is_numeric($parent_id)) { |
| 314 | return false; |
| 315 | } |
| 316 | |
| 317 | $q="select node_vector from nodes where node_id='$parent_id'"; |
| 318 | $set=$db->query($q); |
| 319 | $set->next(); |
| 320 | $parent_vector=$set->getString('node_vector'); |
| 321 | $new_vector=$parent_vector.str_pad($node_id,VECTOR_CHARS,"0",STR_PAD_LEFT); |
| 322 | |
| 323 | $q="update nodes set node_parent='$parent_id',node_vector='$new_vector' where node_id='$node_id'"; |
| 324 | $db->query($q); |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | // Get nodes sorted by weight_k specific to user |
| 330 | |
| 331 | function getKNeurons($user_id,$interval) { |
| 332 | global $db,$node,$error,$error_messages; |
| 333 | |
| 334 | $q="call k_neurons('$user_id','$interval')"; |
| 335 | $set=$db->query($q); |
| 336 | |
| 337 | while ($set->next()) { |
| 338 | $k_array[]=$set->getRecord(); |
| 339 | } |
| 340 | |
| 341 | return $k_array; |
| 342 | } |
| 343 | |
| 344 | } |