3 // All mysql code should go here
4 // Split into multiple files if needed
6 public static function processContent($node_content) { return processContent_hack($node_content); } //XXX TODO FIXME HACK
8 // Called for every node view. Updates node views, neurons,
9 public static 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);
18 $q="insert into neurons set synapse_creator='".
19 $user_id."',dst='".$node_id.
20 "',src='$referer_id',synapse=1";
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
31 $q="insert delayed into levenshtein set user_id='".
32 $user_id."',node_id='".$node_id."'";
38 public static function putNode($what,$where,$checkpermissions=true) {
39 global $db,$error,$error_messages;
40 $user_id = $_SESSION['user_id'];
44 if (!is_numeric($what) || !is_numeric($where)) {
45 $error=$error_messages['NUMERIC_NOT_NUMERIC'];
48 $nodeshell = nodes::getNodeById($where,$user_id);
49 $nodeshell_vector = $nodeshell['node_vector'];
51 if ($checkpermissions) {
52 $nodeshell_permissions = permissions::checkPerms($nodeshell);
53 if (!$nodeshell_permissions['w']) {
54 $error = $error_messages['WRITE_PERMISSION_ERROR'];
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);
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'");
67 public static function addNode($params) {
68 global $db,$node,$error,$error_messages;
69 $parent_id=$params['node_parent'];
71 if ($params['flag']=='registration') $params['node_creator']=UBIK_ID;
73 if (!is_numeric($parent_id)) {
74 $parent_id=$node['node_id'];
77 $parent_permissions=permissions::checkPerms($parent_id);
79 if (!$parent_permissions['w']) {
80 $error=$error_messages['WRITE_PERMISSION_ERROR'];
81 logger::log('add','error','WRITE_PERMISSION_ERROR');
86 $kset=$db->query("select user_k from users where user_id='$params[node_creator]'");
88 $user_k=$kset->getString('user_k');
90 if (!$user_k && $params['node_creator']!=UBIK_ID) {
91 $error=$error_messages['K_SPENT'];
96 $set=$db->query("select node_vector,node_children_count from nodes where node_id='$parent_id'");
98 $parent_vector=$set->getString('node_vector');
100 if ($set->getInt('node_children_count')>MAX_CHILDREN) {
101 $error=$error_messages['MAX_CHILDREN'];
105 //working with external links
106 $external_link=$params['external_link'];
108 $template_id=$params['template_id'];
109 if (!is_numeric($template_id)) $template_id=DEFAULT_TEMPLATE_ID;
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'];
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");
127 $id=$db->getLastInsertId();
129 //node_content MyIsam only for FULLTEXT !!!
130 // $db->query("insert into node_content set node_id='$id',node_content='".$params['node_content']."'");
132 $new_id=str_pad($id,VECTOR_CHARS,"0",STR_PAD_LEFT);
133 $new_vector=trim($parent_vector,"z").$new_id;
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");
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'");
140 $node_vector=trim(chunk_split($new_vector,VECTOR_CHARS,';'),';');
142 $ancestors=explode(";",$node_vector);
143 foreach($ancestors as $key => $ancestor_id) {
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'");
149 $db->query("update node_access set node_user_subchild_count=node_user_subchild_count+1 where node_id='$parent_id'");
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);
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);
168 public static function getUserByLogin($login) {
169 global $error, $error_messages;
170 $q2="select user_id from users where login='".$login."'";
171 $userset=$db->query($q2);
173 $id=$userset->getString('user_id');
174 if (is_numeric($id)) return $id;
176 $error = $error_messages['USER_NOT_FOUND'];
181 public static function getNodeIdByName($name, $external_link=false) {
184 $qh = sprintf('select node_id from nodes where node_name = "%s"', db_escape_string($name));
186 $qh .= sprintf(' and external_link="%s"', db_escape_string($external_link));
188 $set = $db->query($qh);
190 return $set->getString('node_id');
193 public static function getNodeById($node_handle,$user_id, $table_name="nodes") {
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'";
204 $result=$db->query($q);
205 if (!$result->next()) {
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"));
216 transport_process_node($node);
222 public static function redirByName($node_handle) {
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'");
227 $node_id=$set->getString('node_id');
228 if (!empty($node_id)) {
229 return nodes::getNodeById($node_id,$_SESSION['user_id']);
233 $set=$db->query("select node_id from nodes where node_name='$node_handle' ");
235 $node_id=$set->getString('node_id');
236 if (!empty($node_id)) {
237 return nodes::getNodeById($node_id,$_SESSION['user_id']);
242 public static function getNodesByName($node_handle) {
244 $q="select nodes.* from nodes where node_name='$node_handle%'";
246 $result=$db->query($q);
248 while ($result->next()){
249 $record[]=addBase36id($result->getRecord());
257 public static function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
258 global $db, $error, $node;
259 $node_handle=$node['node_id'];
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";
265 $result=$db->query($q);
267 while ($result->next()) {
268 $array[]=addBase36id($result->getRecord());
276 public static function GetUserSubmissionsChildren($user_id,$limit=23,$offset=0,$orderby='') {
279 if (!is_numeric($user_id)) {
282 if (!is_numeric($limit)) {
285 if (!is_numeric($offset)) {
289 // XXX orderby mysql escape
291 $q = "select child.*, users.login as login, parent.node_name as parent_name from nodes as child join
292 (select node_id,node_name,node_creator from nodes where node_creator='$user_id')
293 as parent on child.node_parent=parent.node_id and child.node_creator <> '$user_id'
294 join users as users on users.user_id=child.node_creator order by node_created desc
295 $orderby LIMIT $offset,$limit";
298 $result=$db->query($q);
300 while ($result->next()) {
301 $array[]=addBase36id($result->getRecord());
307 public static function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
308 global $db, $error, $node;
309 $node_handle=$node['node_id'];
311 $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";
313 $result=$db->query($q);
315 while ($result->next()) {
316 $children_array[]=addBase36id($result->getRecord());
319 return $children_array;
325 public static function getNodeAccessData() {
327 $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!=''";
328 $result=$db->query($q);
330 while ($result->next()) {
331 $access_data[]=$result->getRecord();
339 // Simple internal function to set node parrent
341 public static function setParent($node_id,$parent_id) {
342 global $db,$node,$error,$error_messages;
344 if (!is_numeric($parent_id)) {
348 $q="select node_vector from nodes where node_id='$parent_id'";
351 $parent_vector=$set->getString('node_vector');
352 $new_vector=$parent_vector.str_pad($node_id,VECTOR_CHARS,"0",STR_PAD_LEFT);
354 $q="update nodes set node_parent='$parent_id',node_vector='$new_vector' where node_id='$node_id'";
360 // Get last submissions of all users on kyberia.
361 // XXX ad permission checking
362 // XXX remove constants
364 public static function getLast($params) {
365 global $db,$node,$error,$error_messages;
367 if ($params['listing_amount']=='all') $listing_amount='-1';
368 else $listing_amount=addslashes($params['listing_amount']);
369 if (empty($params['offset'])) $offset=0;
370 else $offset=addslashes($params['offset']);
373 if ($node['node_id']==DEF_LAST_NODE) {
374 $interval=" nodes.node_created>NOW()-INTERVAL 65 HOUR and";
378 $vector=$node['node_vector'];
379 $interval=" nodes.node_created>NOW()-INTERVAL 42 DAY and";
382 $node_id=$node['node_id'];
383 $user_id=$_SESSION['user_id'];
384 $q="select parent.node_name as parent_name,users.*,nodes.* from nodes
385 left join nodes as parent on parent.node_id=nodes.node_parent
386 left join users on users.user_id=nodes.node_creator where $interval
387 nodes.node_vector like '$vector%' and
388 nodes.node_system_access!='private' order by nodes.node_id desc LIMIT $offset,$listing_amount ";
392 while ($set->next()) {
393 $last[]=$set->getRecord();
401 // Get nodes sorted by weight_k specific to user
403 public static function getKNeurons($user_id,$interval) {
404 global $db,$node,$error,$error_messages;
406 $q="call k_neurons('$user_id','$interval')";
409 while ($set->next()) {
410 $k_array[]=$set->getRecord();
418 // Set synapse weight.
419 // If synapse does not exists, create a new one.
421 // Returns true on sucess, otherwise false.
423 public static function setSynapse($params){
424 global $db,$node,$error,$error_messages;
427 if ((!is_numeric($params['src']))
428 or (!is_numeric($params['dst']))
429 or (!is_numeric($params['weight']))) {
436 $weight=$params['weight'];
438 // weight could be only positive
444 $q="select count(src) from neurons where dst ='$dst' and src = '$src'";
447 $isrc=$set->getString('count(src)');
450 $q="update neurons set synapse_weight='$weight'
451 where dst = '$dst' and src = '$src'";
453 // FIXME no vector set
454 $q="insert into neurons values('$weight','$dst','$src',0,'synapse',
455 CURRENT_TIMESTAMP(),now(),NULL,$src)";
464 // If synapse does not exists, weight is 1
466 // Returns synapse weight (from user to node)
468 public static function getSynapseWeight($user_id,$node_id){
471 if (!is_numeric($user_id))
473 if (!is_numeric($node_id))
476 $set=$db->query("select synapse_weight from neurons where src =".
477 $user_id." and dst = ".$node_id." and link='synapse'");
480 $synapse_weight=$set->getString('synapse_weight');
482 if (! ($synapse_weight) ) { $synapse_weight = 1;}
484 return $synapse_weight;
489 // Get list of users currently viewing specified node.
491 // Returns list of (login, user_id)
493 public static function getNodeUserlist($node_id) {
496 if (!is_numeric($node_id))
500 $set=$db->query("select login,user_id from users where user_action_id='$node_id'");
501 while ($set->next()) {
502 $userlist[]=$set->getRecord();
510 // Get logins of all node commanders (used in configure)
512 // Returns list of (node_permission, login)
514 public static function getNodeCommanders($node_id) {
517 if (!is_numeric($node_id))
520 $set=$db->query("select node_permission,users.login from node_access
521 left join users on node_access.user_id=users.user_id where
522 node_id='$node_id' and node_permission!='' order by node_permission");
524 while ($set->next()) {
525 $commanders[$set->getString('node_permission')].=$set->getString('login').";";
536 public static function logout() {
538 $q="update users set user_action_id=null where user_id='".$_SESSION['user_id']."'";
548 public static function getNodesByType($vector,$user_id,$type,$orderby,$offset,$listing_amount) {
551 if ((!is_numeric($user_id))
552 or (!is_numeric($offset))
553 or (!is_numeric($listing_amount))
554 or ($vector && !is_numeric($vector))
555 or (!is_numeric($type)))
558 $orderby=db_escape_string($orderby);
561 $q="select parent.node_name as parent_name,users.*,nodes.*,node_access.node_user_subchild_count from nodes
562 left join nodes as parent on parent.node_id=nodes.node_parent
563 left join node_access on node_access.node_id=nodes.node_id and node_access.user_id='$user_id'
564 left join users on users.user_id=nodes.node_creator where ";
565 if ($vector) $q.="nodes.node_vector like '$vector%' and";
566 $q.=" nodes.template_id='$type' and nodes.node_system_access!='private'";
567 if ($orderby) $q.=" order by $orderby ";
568 else $q.=" order by nodes.node_id desc ";
569 $q.= " LIMIT $offset,$listing_amount ";
572 while ($set->next()) $result[]=$set->getRecord();
583 public static function getLinkedNodes($node_id,$orderby,$offset,$listing_amount) {
586 if ((!is_numeric($node_id))
587 or (!is_numeric($offset))
588 or (!is_numeric($listing_amount)))
589 { return false; } // XXX check return value by caller?
590 $orderby=db_escape_string($orderby);
592 $q="select neurons.synapse_created,node_content,author.login,linker.login as linker,nodes.* from neurons
593 left join nodes on neurons.src=nodes.node_id
594 left join users as linker on neurons.synapse_creator=linker.user_id
595 left join users as author on nodes.node_creator=author.user_id
596 where dst='$node_id' and link in ('hard','bookmark')
597 order by $orderby desc limit $offset , $listing_amount";
599 $result=$db->query($q);
600 while ($result->next()) {
601 $array=$result->getRecord();
602 transport_process_node($array); // XXX
603 $array['node_status']="linked";
604 $array['node_created']=$array['synapse_created'];
605 $get_linked_nodes[]=$array;
607 return (isset($get_linked_nodes) ? $get_linked_nodes : false);
610 // getThreadedChildren
613 public static function getThreadedChildren($node_id,$node_vector,$offset,$limit,$orderby,$time,$synapse_time,$security,$link,$search,$search_param) {
620 if ($synapse_time) { $sql_synapse.=" and node_created >'".db_escape_string($synapse_time)."'"; }
622 if ($orderby=='' OR $orderby=='desc') {
623 $orderby="concat(node_vector,'z') desc,depth";
625 $orderby="node_vector asc";
629 $sql_time="node_created > '".db_escape_string($time)."' and";
633 if ($security) { $security=" and node_system_access!='private'"; }
634 else { $security = ""; }
637 if ($search=='content') {
638 $sql_type.=" and node_content like '%".db_escape_string($search_param)."%' ";
640 if ($search=='user') {
641 if (!is_numeric($search_param)) { return false; }
642 $sql_type=" and nodes.node_creator='$search_param'";
647 if ($link=='yes') $q.="(";
648 $q.="select nodes.node_id,node_name,node_external_access,external_link,node_parent,
649 node_system_access,node_children_count,node_creator,node_created,lastchild_created,
650 k,node_views,node_descendant_count,lastdescendant_created,template_id,node_updated,
651 length(node_vector) as depth,users.login,node_vector, node_content,'' as synapse_creator
653 left join users on users.user_id=nodes.node_creator
654 where $sql_time node_vector like '".$node_vector."%' $sql_type
655 and node_id != '".$node_id."' $security
656 order by $orderby LIMIT $offset,$limit";
659 $q.=" ) UNION (select nodes.node_id,node_name,node_external_access,external_link,
660 node_parent,node_system_access,node_children_count,node_creator,node_created,
661 lastchild_created,k,node_views,node_descendant_count,lastdescendant_created,
662 template_id,node_updated,length(dst_vector) as depth,
663 users.login,dst_vector as node_vector,node_content,synapse_creator
665 left join nodes on neurons.src=nodes.node_id
666 left join users on users.user_id=nodes.node_creator
667 where $sql_time dst_vector like '".$node_vector."%' $sql_synapse $sql_type
668 and node_id != '".$node_id."' order by $orderby LIMIT $offset,$limit)";
671 if ($link=='yes') $q.=" order by $orderby LIMIT $limit";
673 $result=$db->query($q);
675 while ($result->next()) {
676 $child = $result->getRecord();
677 transport_process_node($child);
678 if($child['synapse_creator']!='') $child['node_status']='linked';
680 $get_children_array[]=$child;
683 return $get_children_array;
688 public static function getPoll($user_id,$poll_id) {
691 $set=$db->query("select nodes.*,node_access.node_permission from nodes
692 left join node_access on (nodes.node_id=node_access.node_id and node_access.user_id='$user_id')
693 where node_parent='$poll_id' and template_id='1549834' order by node_id desc limit 1");
696 $array=$set->getRecord();
703 public static function resetPassword($login_id,$login,$vercode,$password) {
707 $login = db_escape_string($login);
708 if (!is_numeric($login_id)) {
709 $error="Not numeric id is not numeric. Here, take this stone.";
714 $error="Please enter name or id";
719 $set=$db->query("select * from users where login='$login'");
721 $set=$db->query("select * from users where user_id='$login'");
725 $user_name=$set->getString('login');
726 $user_id=$set->getString('user_id');
727 $hash=$set->getString('hash');
729 if ($hash != $vercode) {
730 $error="Bad verification code!";
734 $password = sha1($password);
735 $q="update users set password='$password',hash='' where user_id='$user_id'";
738 $error="OK, password was RE-set";