51ff3226 |
1 | <?php |
2 | if (!function_exists('str_split')){ |
3 | function str_split($string, $split_length=1){ |
4 | |
5 | if ($split_length < 1){ |
6 | return false; |
7 | } |
8 | |
9 | for ($pos=0, $chunks = array(); $pos < strlen($string); $pos+=$split_length){ |
10 | $chunks[] = substr($string, $pos, $split_length); |
11 | } |
12 | return $chunks; |
13 | } |
14 | } |
15 | |
16 | class nodes { |
17 | |
18 | |
19 | |
20 | function addNode($params) { |
21 | global $db,$node,$error,$error_messages; |
22 | $parent_id=$params['node_parent']; |
23 | |
24 | if (!is_numeric($parent_id)) { |
25 | $parent_id=$node['node_id']; |
26 | } |
27 | |
28 | $parent_permissions=permissions::checkPermissions($parent_id); |
29 | if (!$parent_permissions['w']) { |
30 | $error=$error_messages['WRITE_PERMISSION_ERROR']; |
fd15ea3a |
31 | logger::log('add','error','WRITE_PERMISSION_ERROR'); |
51ff3226 |
32 | return false; |
33 | } |
34 | |
35 | |
36 | $kset=$db->query("select user_k from users where user_id='$params[node_creator]'"); |
37 | $kset->next(); |
38 | $user_k=$kset->getString('user_k'); |
39 | |
40 | if ($params['flag']=='registration') $params['node_creator']=UBIK_ID; |
41 | |
42 | if (!$user_k && $params['node_creator']!=UBIK_ID) { |
43 | $error=$error_messages['K_SPENT']; |
44 | return false; |
45 | } |
46 | |
47 | |
48 | |
49 | $set=$db->query("select node_vector,node_children_count from nodes where node_id='$parent_id'"); |
50 | $set->next(); |
51 | $parent_vector=$set->getString('node_vector'); |
52 | |
53 | if ($set->getInt('node_children_count')>MAX_CHILDREN) { |
54 | $error=$error_messages['MAX_CHILDREN']; |
55 | return false; |
56 | } |
57 | |
58 | //working with external links |
59 | $external_link=$params['external_link']; |
60 | |
61 | $template_id=$params['template_id']; |
62 | if (!is_numeric($template_id)) $template_id=DEFAULT_TEMPLATE_ID; |
63 | |
64 | $q="insert into nodes set |
65 | node_name='".$params['node_name']."', |
66 | node_external_access='".$node['node_external_access']."', |
67 | node_system_access='".$node['node_system_access']."', |
68 | node_creator='".$params['node_creator']."', |
69 | template_id='".$template_id."', |
70 | external_link='".$external_link."', |
71 | node_parent='".$parent_id."', |
72 | node_views=0,node_created=NOW(), |
73 | node_content='".$params['node_content']."', |
74 | node_vector='".$params['node_vector']."'"; |
75 | $db->query("start transaction"); |
76 | $db->query($q); |
77 | $id=$db->getLastInsertId(); |
78 | |
79 | //node_content MyIsam only for FULLTEXT !!! |
80 | // $db->query("insert into node_content set node_id='$id',node_content='".$params['node_content']."'"); |
81 | |
82 | $new_id=str_pad($id,VECTOR_CHARS,"0",STR_PAD_LEFT); |
83 | $new_vector=trim($parent_vector,"z").$new_id; |
84 | |
85 | if ($params['flag']=='registration') $db->query("update nodes set node_system_access='public',node_external_access='yes',node_creator=$id where node_id=$id"); |
86 | |
87 | $db->query("update nodes set node_vector='$new_vector' where node_id='$id'"); |
88 | $db->query("update nodes set node_children_count=node_children_count+1 where node_id='$parent_id'"); |
89 | |
90 | $node_vector=trim(chunk_split($new_vector,VECTOR_CHARS,';'),';'); |
91 | |
92 | $ancestors=explode(";",$node_vector); |
93 | foreach($ancestors as $key => $ancestor_id) { |
94 | if ($key) { |
95 | $ancestor_id=ltrim($ancestor_id,'0'); |
96 | $db->query("update nodes set node_descendant_count=node_descendant_count+1,lastdescendant_created=NOW() where node_id='$ancestor_id'"); |
97 | } |
98 | } |
99 | $db->query("update node_access set node_user_subchild_count=node_user_subchild_count+1 where node_id='$parent_id'"); |
100 | --$user_k; |
101 | $db->query("update users set user_k='$user_k' where user_id='$params[node_creator]'"); |
102 | $db->query("commit"); |
fd15ea3a |
103 | logger::log('add','ok',$id); |
51ff3226 |
104 | |
105 | if ($_POST['code']) { |
106 | $params['node_creator']=UBIK_ID; |
107 | $params['node_parent']=WARNING_ZONE; |
108 | $params['node_name']="node $id added with code_parameter"; |
109 | $params['node_content']="node <a href='/id/$id'>$id</a> added with code_parameter"; |
110 | unset($_POST['code']); |
111 | nodes::addNode($params); |
112 | } |
113 | |
114 | return $id; |
115 | } |
116 | |
117 | |
118 | function processContent($node_content) { |
119 | global $node; |
e909f81b |
120 | include_once(INCLUDE_DIR.'htmlparse.inc'); |
51ff3226 |
121 | |
122 | if ($node['template_id']==$node['node_id'] && $_POST['event']=='configure_content') { |
123 | |
124 | } |
125 | |
126 | elseif ($_POST['no_html']) { |
127 | $node_content=htmlspecialchars($node_content); |
128 | } |
129 | |
130 | elseif ($_POST['wiki']) { |
131 | // load the class file |
132 | require_once 'Text/Wiki.php'; |
133 | |
134 | // instantiate a Text_Wiki object with the default rule set |
63c0e85f |
135 | $wiki = new Text_Wiki(); |
51ff3226 |
136 | |
137 | $node_content = $wiki->transform($node_content, 'Xhtml'); |
138 | } |
139 | |
140 | elseif ($_POST['code']) {} |
141 | |
142 | else { |
143 | global $db,$htmlparse,$error; |
144 | |
54bc3775 |
145 | if (!htmlparser::htmlparse($node_content)) { |
51ff3226 |
146 | $error=$htmlparse; |
147 | return false; |
148 | } |
149 | |
150 | $node_content = eregi_Replace("((( )|(\n)|(^))+)(http://|ftp://|https://)([[:alnum:]][^,[:space:]]*)","\\2<a target='_blank' href=\"\\6\\7\">\\6\\7</a>",$node_content); |
151 | $node_content = mysql_real_escape_string($node_content); |
152 | } |
153 | |
154 | return $node_content; |
155 | } |
156 | |
157 | |
158 | function getUserByLogin($login) { |
159 | global $error, $error_messages; |
160 | $q2="select user_id from users where login='".$login."'"; |
161 | $userset=$db->query($q2); |
162 | $userset->next(); |
163 | $id=$userset->getString('user_id'); |
164 | if (is_numeric($id)) return $id; |
165 | else { |
166 | $error = $error_messages['USER_NOT_FOUND']; |
167 | return false; |
168 | } |
169 | } |
170 | |
171 | |
172 | function getNodeIdByName($name,$external_link=false) { |
173 | global $db; |
174 | $q="select node_id from nodes where node_name='$name'"; |
175 | if ($external_link) $q.=" and external_link='$external_link'"; |
176 | $set=$db->query($q); |
177 | $set->next(); |
178 | return $set->getString('node_id'); |
179 | } |
180 | |
181 | function getNodeById($node_handle,$user_id, $table_name="nodes") { |
182 | global $db, $error; |
183 | $q="select length(concat($table_name.node_vector)) as |
184 | vector_depth,$table_name.*,$table_name.node_creator as |
185 | node_owner_id,creator.node_name as owner,node_access.*,$table_name.node_id as |
186 | node_id,node_parent.node_name as node_parent_name |
187 | from $table_name left join $table_name as creator on creator.node_id=$table_name.node_creator |
188 | left join $table_name as node_parent on $table_name.node_parent=node_parent.node_id |
189 | left join node_access on (node_access.node_id='$node_handle' and node_access.user_id='$user_id') |
190 | where $table_name.node_id='$node_handle'"; |
191 | |
192 | $result=$db->query($q); |
193 | if (!$result->next()) { |
194 | return false; |
195 | } |
196 | else { |
197 | $node=$result->getRecord(); |
9371f8b6 |
198 | $node['node_kid']=base_convert($node['node_id'],10,36); |
51ff3226 |
199 | $node['node_vector']=trim($node['node_vector'],"z"); |
200 | $ancestors=str_split($node['node_vector'],VECTOR_CHARS); |
201 | foreach ($ancestors as $ancestor) { |
202 | $node['ancestors'][]=array("name"=>"","link"=>ltrim($ancestor,"0")); |
203 | } |
204 | } |
205 | return $node; |
206 | |
207 | } |
208 | |
209 | |
210 | function redirByName($node_handle) { |
211 | global $db, $error; |
212 | $user_id=$_SESSION['user_id']; |
213 | $set=$db->query("select node_id from nodes where node_name='$node_handle' and node_creator='$user_id'"); |
214 | if ($set->next()) { |
215 | $node_id=$set->getString('node_id'); |
216 | if (!empty($node_id)) { |
217 | return nodes::getNodeById($node_id,$_SESSION['user_id']); |
218 | } |
219 | |
220 | } |
221 | $set=$db->query("select node_id from nodes where node_name='$node_handle' "); |
222 | $set->next(); |
223 | $node_id=$set->getString('node_id'); |
224 | if (!empty($node_id)) { |
225 | return nodes::getNodeById($node_id,$_SESSION['user_id']); |
226 | } |
227 | |
228 | } |
229 | |
230 | function getNodesByName($node_handle) { |
231 | global $db, $error; |
232 | $q="select nodes.* from nodes where node_name='$node_handle%'"; |
233 | |
234 | $result=$db->query($q); |
235 | |
236 | while ($result->next()){ |
237 | $record[]=$result->getRecord(); |
238 | } |
239 | return $record; |
240 | |
241 | } |
242 | |
243 | |
244 | |
245 | function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) { |
246 | global $db, $error, $node; |
247 | $node_handle=$node['node_id']; |
248 | |
249 | $q="select users.*,nodes.* from nodes |
250 | left join users on users.user_id=nodes.node_creator where |
251 | node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit"; |
252 | echo $q; |
253 | $result=$db->query($q); |
254 | |
255 | while ($result->next()) { |
256 | $array[]=$result->getRecord(); |
257 | } |
258 | |
259 | return $array; |
260 | |
261 | } |
262 | |
263 | |
264 | function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) { |
265 | global $db, $error, $node; |
266 | $node_handle=$node['node_id']; |
267 | |
268 | $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"; |
269 | |
270 | $result=$db->query($q); |
271 | |
272 | while ($result->next()) { |
273 | $children_array[]=$result->getRecord(); |
274 | } |
275 | |
276 | return $children_array; |
277 | |
278 | } |
279 | |
280 | |
281 | |
282 | function getNodeAccessData() { |
283 | global $node,$db; |
284 | $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!=''"; |
285 | $result=$db->query($q); |
286 | |
287 | while ($result->next()) { |
288 | $access_data[]=$result->getRecord(); |
289 | } |
290 | |
291 | return $access_data; |
292 | |
293 | } |
294 | } |
295 | |
296 | ?> |