Kyberia v2.0
[mirrors/Kyberia-bloodline.git] / inc / nodes.inc
1 <?php
2 /* This program is free software. It comes without any warranty, to
3 * the extent permitted by applicable law. You can redistribute it
4 * and/or modify it under the terms of the Do What The Fuck You Want
5 * To Public License, Version 2, as published by Sam Hocevar. See
6 * http://sam.zoy.org/wtfpl/COPYING for more details. */
7
8
9 function processRegexp($matches) {
10 global $db,$error;
11 $set=$db->query("select node_id from nodes where node_name='".$matches[1]."' order by node_views desc limit 1");
12 $set->next();
13 $id=$set->getString('node_id');
14 return "<a href='/id/$id'>".$matches[2]."</a>";
15 }
16
17 class nodes {
18
19 function processRegexp($matches) {
20 print_r($matches);
21 }
22
23 function addNode($params) {
24 global $db,$node,$error;
25 $parent_id=$params['node_parent'];
26 $parent_permissions=permissions::checkPermissions($parent_id);
27 if (!$parent_permissions['w']) {
28 $error=$error_messages['WRITE_PERMISSION_ERROR'];
29 log::log('add','error','WRITE_PERMISSION_ERROR');
30 return false;
31 }
32
33 $kset=$db->query("select user_k from users where user_id='$params[node_creator]'");
34 $kset->next();
35 $user_k=$kset->getString('user_k');
36
37 if (!$user_k) {
38 $error=$error_messages['K_SPENT'];
39 return false;
40 }
41
42
43
44 $set=$db->query("select node_vector from nodes where node_id='$parent_id'");
45 $set->next();
46 $parent_vector=$set->getString('node_vector');
47
48 //working with external links
49 $external_link=$params['external_link'];
50
51 $template_id=$params['template_id'];
52 if (!is_numeric($template_id)) $template_id=DEFAULT_TEMPLATE_ID;
53
54 $q="insert into nodes set
55 node_name='".addslashes($params['node_name'])."',
56 node_external_access='".$node['node_external_access']."',
57 node_system_access='".$node['node_system_access']."',
58 node_creator='".$params['node_creator']."',
59 template_id='".$template_id."',
60 external_link='".$external_link."',
61 node_parent='".$params['node_parent']."',
62 node_views=0,node_created=NOW(),
63 node_vector='".$params['node_vector']."'";
64 $db->query("start transaction");
65 $db->query($q);
66 $id=$db->getLastInsertId();
67 $db->query("insert into node_content set node_id='$id',node_content='".$params['node_content']."'");
68 $new_id=str_pad($id,VECTOR_CHARS,"0",STR_PAD_LEFT);
69 $new_vector=$parent_vector.$new_id;
70
71 $db->query("update nodes set node_vector='$new_vector' where node_id='$id'");
72 $db->query("update nodes set node_children_count=node_children_count+1 where node_id='$parent_id'");
73
74 $node_vector=trim(chunk_split($new_vector,VECTOR_CHARS,';'),';');
75 //if ($_SESSION['user_id']==548) echo "LALALA".$node_vector;
76 $ancestors=explode(";",$node_vector);
77 foreach($ancestors as $key => $ancestor_id) {
78 if ($key) {
79 $ancestor_id=ltrim($ancestor_id,'0');
80 $db->query("update nodes set node_descendant_count=node_descendant_count+1,lastdescendant_created=NOW() where node_id='$ancestor_id'");
81 }
82 }
83 $db->query("update node_access set node_user_subchild_count=node_user_subchild_count+1 where node_id='$parent_id'");
84 --$user_k;
85 $db->query("update users set user_k='$user_k' where user_id='$params[node_creator]'");
86 $db->query("commit");
87 log::log('add','ok',$id);
88
89 return $id;
90 }
91
92
93 function processContent($node_content) {
94 global $node;
95 include_once(SYSTEM_ROOT.'/inc/htmlparse.inc');
96
97 if ($node['template_id']==$node['node_id'] && $_POST['event']=='configure_content') {
98
99 }
100
101 elseif ($_POST['no_html']) {
102 $node_content=htmlspecialchars($node_content);
103 }
104
105 else {
106 global $db,$htmlparse,$error;
107
108 if (!htmlparse::htmlparse($node_content)) {
109 $error=$htmlparse;
110 return false;
111 }
112
113 $node_content = EregI_Replace("((( )|(\n)|(^))+)(http://|ftp://|https://)([[:alnum:]][^,[:space:]]*)","\\2<a target='_blank'href=\"\\6\\7\">\\6\\7</a>",$node_content);
114 $node_content=preg_replace_callback("/##(.*)##(.*)##/i","processRegexp",$node_content);
115 }
116
117 $node_content=addslashes($node_content);
118 return $node_content;
119 }
120
121
122 function getUserByLogin($login) {
123 global $error, $error_messages;
124 $q2="select user_id from users where login='".$login."'";
125 $userset=$db->query($q2);
126 $userset->next();
127 $id=$userset->getString('user_id');
128 if (is_numeric($id)) return $id;
129 else {
130 $error = $error_messages['USER_NOT_FOUND'];
131 return false;
132 }
133 }
134
135
136 function getNodeIdByName($name) {
137 global $db;
138 $q="select node_id from nodes where node_name='$name'";
139 $set=$db->query($q);
140 $set->next();
141 return $set->getString('node_id');
142 }
143
144 function getNodeById($node_handle,$user_id) {
145 global $db, $error;
146 $q="select length(concat(nodes.node_vector)) as vector_depth,node_content.node_content,nodes.*,nodes.node_creator as node_owner_id,creator.node_name as owner,node_access.*,nodes.node_id as node_id,node_parent.node_name as node_parent_name from nodes left join nodes as creator on creator.node_id=nodes.node_creator left join nodes as node_parent on nodes.node_parent=node_parent.node_id left join node_content on nodes.node_id=node_content.node_id left join node_access on (node_access.node_id='$node_handle' and node_access.user_id='$user_id') where nodes.node_id='$node_handle'";
147
148 $result=$db->query($q);
149 if (!$result->next()) {
150 $error="no such node_id $node_handle exists";
151 return false;
152 }
153 else {
154 $node=$result->getRecord();
155 }
156 return $node;
157
158 }
159
160
161 function redirByName($node_handle) {
162 global $db, $error;
163 $user_id=$_SESSION['user_id'];
164 $set=$db->query("select node_id from nodes where node_name='$node_handle' and node_creator='$user_id'");
165 if ($set->next()) {
166 $node_id=$set->getString('node_id');
167 if (!empty($node_id)) {
168 Header("Location: /id/$node_id");
169 }
170 return true;
171 }
172 $set=$db->query("select node_id from nodes where node_name='$node_handle' ");
173 $set->next();
174 $node_id=$set->getString('node_id');
175 if (!empty($node_id)) {
176 Header("Location: /id/$node_id");
177 }
178
179 }
180
181 function getNodesByName($node_handle) {
182 global $db, $error;
183 $q="select nodes.* from nodes where node_name='$node_handle%'";
184
185 $result=$db->query($q);
186
187 while ($result->next()){
188 $record[]=$result->getRecord();
189 }
190 return $record;
191
192 }
193
194
195
196 function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
197 global $db, $error, $node;
198 $node_handle=$node['node_id'];
199
200 $q="select users.*,nodes.*,node_content.* from nodes
201 left join node_content on (node_content.node_id=nodes.node_id)
202 left join users on users.user_id=nodes.node_creator where
203 node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit";
204 echo $q;
205 $result=$db->query($q);
206
207 while ($result->next()) {
208 $array[]=$result->getRecord();
209 }
210
211 return $array;
212
213 }
214
215
216 function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) {
217 global $db, $error, $node;
218 $node_handle=$node['node_id'];
219
220 $q="select length(node_vector) as depth,users.login,nodes.*,node_content.* from nodes left join node_content on (node_content.node_id=nodes.node_id) 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";
221
222 $result=$db->query($q);
223
224 while ($result->next()) {
225 $children_array[]=$result->getRecord();
226 }
227
228 return $children_array;
229
230 }
231
232
233
234 function getNodeAccessData() {
235 global $node,$db;
236 $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!=''";
237 $result=$db->query($q);
238
239 while ($result->next()) {
240 $access_data[]=$result->getRecord();
241 }
242
243 return $access_data;
244
245 }
246 }
247
248 ?>
This page took 0.43919 seconds and 4 git commands to generate.