| 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 | require_once(INCLUDE_DIR.'base36.inc'); |
| 17 | |
| 18 | class nodes { |
| 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']; |
| 31 | logger::log('add','error','WRITE_PERMISSION_ERROR'); |
| 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 | if(!isset($params['node_system_access'])) $params['node_system_access'] = $node['node_system_access']; |
| 65 | if(!isset($params['node_external_access'])) $params['node_external_access'] = $node['node_external_access']; |
| 66 | |
| 67 | $q="insert into nodes set |
| 68 | node_name='".$params['node_name']."', |
| 69 | node_external_access='".$params['node_external_access']."', |
| 70 | node_system_access='".$params['node_system_access']."', |
| 71 | node_creator='".$params['node_creator']."', |
| 72 | template_id='".$template_id."', |
| 73 | external_link='".$external_link."', |
| 74 | node_parent='".$parent_id."', |
| 75 | node_views=0,node_created=NOW(), |
| 76 | node_content='".$params['node_content']."', |
| 77 | node_vector='".$params['node_vector']."'"; |
| 78 | $db->query("start transaction"); |
| 79 | $db->query($q); |
| 80 | $id=$db->getLastInsertId(); |
| 81 | |
| 82 | //node_content MyIsam only for FULLTEXT !!! |
| 83 | // $db->query("insert into node_content set node_id='$id',node_content='".$params['node_content']."'"); |
| 84 | |
| 85 | $new_id=str_pad($id,VECTOR_CHARS,"0",STR_PAD_LEFT); |
| 86 | $new_vector=trim($parent_vector,"z").$new_id; |
| 87 | |
| 88 | if ($params['flag']=='registration') $db->query("update nodes set node_system_access='public',node_external_access='yes',node_creator=$id where node_id=$id"); |
| 89 | |
| 90 | $db->query("update nodes set node_vector='$new_vector' where node_id='$id'"); |
| 91 | $db->query("update nodes set node_children_count=node_children_count+1 where node_id='$parent_id'"); |
| 92 | |
| 93 | $node_vector=trim(chunk_split($new_vector,VECTOR_CHARS,';'),';'); |
| 94 | |
| 95 | $ancestors=explode(";",$node_vector); |
| 96 | foreach($ancestors as $key => $ancestor_id) { |
| 97 | if ($key) { |
| 98 | $ancestor_id=ltrim($ancestor_id,'0'); |
| 99 | $db->query("update nodes set node_descendant_count=node_descendant_count+1,lastdescendant_created=NOW() where node_id='$ancestor_id'"); |
| 100 | } |
| 101 | } |
| 102 | $db->query("update node_access set node_user_subchild_count=node_user_subchild_count+1 where node_id='$parent_id'"); |
| 103 | --$user_k; |
| 104 | $db->query("update users set user_k='$user_k' where user_id='$params[node_creator]'"); |
| 105 | $db->query("commit"); |
| 106 | logger::log('add','ok',$id); |
| 107 | |
| 108 | if ($_POST['code']) { |
| 109 | $params['node_creator']=UBIK_ID; |
| 110 | $params['node_parent']=WARNING_ZONE; |
| 111 | $params['node_name']="node $id added with code_parameter"; |
| 112 | $params['node_content']="node <a href='/id/$id'>$id</a> added with code_parameter"; |
| 113 | unset($_POST['code']); |
| 114 | nodes::addNode($params); |
| 115 | } |
| 116 | |
| 117 | return $id; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | function processContent($node_content) { |
| 122 | global $node; |
| 123 | include_once(INCLUDE_DIR.'htmlparse.inc'); |
| 124 | |
| 125 | if ($node['template_id']==$node['node_id'] && $_POST['event']=='configure_content') { |
| 126 | |
| 127 | } |
| 128 | |
| 129 | elseif ($_POST['no_html']) { |
| 130 | $node_content=htmlspecialchars($node_content); |
| 131 | } |
| 132 | |
| 133 | elseif ($_POST['wiki']) { |
| 134 | // load the class file |
| 135 | require_once 'Text/Wiki.php'; |
| 136 | |
| 137 | // instantiate a Text_Wiki object with the default rule set |
| 138 | $wiki = new Text_Wiki(); |
| 139 | |
| 140 | $node_content = $wiki->transform($node_content, 'Xhtml'); |
| 141 | } |
| 142 | |
| 143 | elseif ($_POST['code']) {} |
| 144 | |
| 145 | else { |
| 146 | global $db,$htmlparse,$error; |
| 147 | |
| 148 | if (!htmlparser::htmlparse($node_content)) { |
| 149 | $error=$htmlparse; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | $node_content = eregi_Replace("((( )|(\n)|(^))+)(http://|ftp://|https://)([[:alnum:]][^,[:space:]]*)","\\2<a target='_blank' href=\"\\6\\7\">\\6\\7</a>",$node_content); |
| 154 | //$node_content = mysql_real_escape_string($node_content); once is enough |
| 155 | } |
| 156 | |
| 157 | return $node_content; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | function getUserByLogin($login) { |
| 162 | global $error, $error_messages; |
| 163 | $q2="select user_id from users where login='".$login."'"; |
| 164 | $userset=$db->query($q2); |
| 165 | $userset->next(); |
| 166 | $id=$userset->getString('user_id'); |
| 167 | if (is_numeric($id)) return $id; |
| 168 | else { |
| 169 | $error = $error_messages['USER_NOT_FOUND']; |
| 170 | return false; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | |
| 175 | function getNodeIdByName($name,$external_link=false) { |
| 176 | global $db; |
| 177 | $q="select node_id from nodes where node_name='$name'"; |
| 178 | if ($external_link) $q.=" and external_link='$external_link'"; |
| 179 | $set=$db->query($q); |
| 180 | $set->next(); |
| 181 | return $set->getString('node_id'); |
| 182 | } |
| 183 | |
| 184 | function getNodeById($node_handle,$user_id, $table_name="nodes") { |
| 185 | global $db, $error; |
| 186 | $q="select length(concat($table_name.node_vector)) as |
| 187 | vector_depth,$table_name.*,$table_name.node_creator as |
| 188 | node_owner_id,creator.node_name as owner,node_access.*,$table_name.node_id as |
| 189 | node_id,node_parent.node_name as node_parent_name |
| 190 | from $table_name left join $table_name as creator on creator.node_id=$table_name.node_creator |
| 191 | left join $table_name as node_parent on $table_name.node_parent=node_parent.node_id |
| 192 | left join node_access on (node_access.node_id='$node_handle' and node_access.user_id='$user_id') |
| 193 | where $table_name.node_id='$node_handle'"; |
| 194 | |
| 195 | $result=$db->query($q); |
| 196 | if (!$result->next()) { |
| 197 | return false; |
| 198 | } |
| 199 | else { |
| 200 | $node=addBase36id($result->getRecord()); |
| 201 | $node['node_vector']=trim($node['node_vector'],"z"); |
| 202 | $ancestors=str_split($node['node_vector'],VECTOR_CHARS); |
| 203 | foreach ($ancestors as $ancestor) { |
| 204 | $node['ancestors'][]=array("name"=>"","link"=>ltrim($ancestor,"0")); |
| 205 | } |
| 206 | } |
| 207 | return $node; |
| 208 | |
| 209 | } |
| 210 | |
| 211 | |
| 212 | function redirByName($node_handle) { |
| 213 | global $db, $error; |
| 214 | $user_id=$_SESSION['user_id']; |
| 215 | $set=$db->query("select node_id from nodes where node_name='$node_handle' and node_creator='$user_id'"); |
| 216 | if ($set->next()) { |
| 217 | $node_id=$set->getString('node_id'); |
| 218 | if (!empty($node_id)) { |
| 219 | return nodes::getNodeById($node_id,$_SESSION['user_id']); |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | $set=$db->query("select node_id from nodes where node_name='$node_handle' "); |
| 224 | $set->next(); |
| 225 | $node_id=$set->getString('node_id'); |
| 226 | if (!empty($node_id)) { |
| 227 | return nodes::getNodeById($node_id,$_SESSION['user_id']); |
| 228 | } |
| 229 | |
| 230 | } |
| 231 | |
| 232 | function getNodesByName($node_handle) { |
| 233 | global $db, $error; |
| 234 | $q="select nodes.* from nodes where node_name='$node_handle%'"; |
| 235 | |
| 236 | $result=$db->query($q); |
| 237 | |
| 238 | while ($result->next()){ |
| 239 | $record[]=addBase36id($result->getRecord()); |
| 240 | } |
| 241 | return $record; |
| 242 | |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | function getChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) { |
| 248 | global $db, $error, $node; |
| 249 | $node_handle=$node['node_id']; |
| 250 | |
| 251 | $q="select users.*,nodes.* from nodes |
| 252 | left join users on users.user_id=nodes.node_creator where |
| 253 | node_parent='$node_handle' order by node_created $orderby LIMIT $offset,$limit"; |
| 254 | echo $q; |
| 255 | $result=$db->query($q); |
| 256 | |
| 257 | while ($result->next()) { |
| 258 | $array[]=addBase36id($result->getRecord()); |
| 259 | } |
| 260 | |
| 261 | return $array; |
| 262 | |
| 263 | } |
| 264 | |
| 265 | |
| 266 | function getThreadedChildrenNodes($orderby="desc",$offset=0,$limit=DEFAULT_LISTING_AMOUNT) { |
| 267 | global $db, $error, $node; |
| 268 | $node_handle=$node['node_id']; |
| 269 | |
| 270 | $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"; |
| 271 | |
| 272 | $result=$db->query($q); |
| 273 | |
| 274 | while ($result->next()) { |
| 275 | $children_array[]=addBase36id($result->getRecord()); |
| 276 | } |
| 277 | |
| 278 | return $children_array; |
| 279 | |
| 280 | } |
| 281 | |
| 282 | |
| 283 | |
| 284 | function getNodeAccessData() { |
| 285 | global $node,$db; |
| 286 | $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!=''"; |
| 287 | $result=$db->query($q); |
| 288 | |
| 289 | while ($result->next()) { |
| 290 | $access_data[]=$result->getRecord(); |
| 291 | } |
| 292 | |
| 293 | return $access_data; |
| 294 | |
| 295 | } |
| 296 | |
| 297 | |
| 298 | // Simple internal function to set node parrent |
| 299 | |
| 300 | function setParent($params) { |
| 301 | global $db,$node,$error,$error_messages; |
| 302 | $parent_id=$params['node_parent']; |
| 303 | $node_id=$params['node_id']; |
| 304 | |
| 305 | if (!is_numeric($parent_id)) { |
| 306 | return false; |
| 307 | } |
| 308 | $q="select node_vector from nodes where node_id='$parent_id'"; |
| 309 | $parent_vector=$db->query($q); |
| 310 | |
| 311 | $new_vector=$parent_vector.str_pad($node_id,VECTOR_CHARS,"0",STR_PAD_LEFT); |
| 312 | $q="update nodes set node_parent='$parent_id',node_vector='".$new_vector." |
| 313 | ' where node_id='$node_id'"; |
| 314 | $db->query($q); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | ?> |