Somewhat reducing the fascist tendencies :)
[mirrors/Kyberia-bloodline.git] / wwwroot / nodes.php
index f4924cc555c5dacf8abc343bef55c3eddb998a42..246a99e10d1e2fb3689fb042b184fe7ed31c8812 100644 (file)
@@ -1,93 +1,29 @@
 <?php
-require_once('config/config.inc'); //requiring main config file with path/database etc. constants
-if(isset($realm) && isset($users)) require_once(INCLUDE_DIR.'http_auth.php'); //Ask for auth if enabled...
-//echo($_SERVER['PATH_INFO']."\n<pre>"); var_dump(preg_split('/\//', $_SERVER['PATH_INFO'])); die(); //PATH_INFO Debug (usefull when messing with mod_rewrite)
-// output buffering forcing (mx)
-if (!empty($_POST['FORCE_OB']) && $_POST['FORCE_OB'] == 'true') ob_start();
+//requiring main config file with path/database etc. constants
+require_once('config/config.inc'); 
 
-//header("Location: http://web.archive.org/web/20020925021139/http://kyberia.sk");
-//echo "je to uz uplne v pici. vsetky data su stratene, prajem pekny den :)";
-//exit;
+//Ask for auth if enabled...
+//if(isset($realm) && isset($users)) require_once(INCLUDE_DIR.'http_auth.php'); 
 
 //starting timer for benchmarking purposes
 $timer_start=Time()+SubStr(MicroTime(),0,8);
 //setting PHPSESSID cookie and starting user session
-session_start();
-
-error_reporting(1);
-//$_SESSION['debugging']=1;
-//unset($_SESSION['debugging']); //Well... we should make some event or JavaScript page to turning this on/off...
-//exit;
-
 
-if ($_SESSION['debugging']) {
-
-    error_reporting(E_ALL);
-    echo 'GET VARIABLES::<br/>';
-    print_r($_GET);
-    echo 'POST VARIABLES::<br/>';
-    print_r($_POST);
-    echo '<b>SESSION VARIABLES::</b><br/>';
-    print_r($_SESSION);
-}
+//error reporting has to be before session_start
+error_reporting(-1);
+ini_set('display_errors','On');
+session_start();
 
 @ini_set('magic_quotes_gpc' , 'off');
-if(get_magic_quotes_gpc()) die("Error: magic_quotes_gpc needs to be disabled! F00K!\n");
+if(get_magic_quotes_gpc()) {
+       die("Error: magic_quotes_gpc needs to be disabled! F00K!\n");
+}
 
 //Smarty from DB
 $smarty_resource = 'kyberia';
-//$smarty_resource = ''; //same as 'file' (fallback)
-/* I have moved old templates to DB using following lame script:
- * for i in *.tpl; do j=$(echo "$i" | cut -d . -f 1); echo UPDATE nodes SET node_content = "'$(php -r "echo mysql_escape_string(file_get_contents('$i'));")'" WHERE node_id = "'$j'" COLLATE utf8_bin LIMIT '1;'; done | mysql --user=kyberia --password=PASSSSSSS kyberia
- * In future we should have some mechanism for distributing templates because they are very important part of kyberia source...
- */
-
-//Path info (Experimental - this replaced most of mod_rewrites...)
-@$PATH_INFO=trim($_SERVER[PATH_INFO]);
-if($PATH_INFO != '') {
-       $PATH_CHUNKS = preg_split('/\//', $PATH_INFO);
-       if(isset($PATH_CHUNKS[1])) switch($PATH_CHUNKS[1]) {
-               case 'k':
-                       if(isset($PATH_CHUNKS[2]) && $PATH_CHUNKS[2] != '') $_GET['node_kid'] = $PATH_CHUNKS[2];
-                       if(isset($PATH_CHUNKS[3]) && $PATH_CHUNKS[3] != '') $_GET['template_kid'] = $PATH_CHUNKS[3];
-                       break;
-               case 'id':
-                       if(isset($PATH_CHUNKS[2]) && $PATH_CHUNKS[2] != '') $_GET['node_id'] = $PATH_CHUNKS[2];
-                       if(isset($PATH_CHUNKS[3]) && $PATH_CHUNKS[3] != '') $_GET['template_id'] = $PATH_CHUNKS[3];
-
-                       //Base36 fascism redirect
-                       if($_GET['template_id'] == 'download') break; //Fix ugly download hack...
-                       if(!count($_POST)) {
-                               header('Location: /k/'.base_convert($_GET['node_id'], 10, 36).
-                                       (isset($_GET['template_id'])?'/'.base_convert($_GET['template_id'], 10, 36):'')
-                               );
-                               die("Base36 fascism...\n"); //If you want to be a fascist you have to die imediatelly...
-                       }
-
-                       break;
-               default:
-                       if($PATH_CHUNKS[1] != '') $_GET['node_name'] = $PATH_CHUNKS[1];
-                       if(isset($PATH_CHUNKS[2]) && $PATH_CHUNKS[2] != '') $_GET['template_kid'] = $PATH_CHUNKS[2];
-                       break;
-       }
-}
-if(
-       (!isset($_GET['node_kid']) || trim($_GET['node_kid']) == '') &&
-       (!isset($_GET['node_id']) || trim($_GET['node_id']) == '')
-) $_GET['node_kid'] = 1;
-
-//Base36 http://en.wikipedia.org/wiki/Base_36 (Initial support only :-)
-if(isset($_GET['node_kid'])) $_GET['node_id'] = base_convert($_GET['node_kid'], 36, 10);
-if(isset($_GET['template_kid'])) $_GET['template_id'] = base_convert($_GET['template_kid'], 36, 10);
-
-require_once(INCLUDE_DIR.'senate.inc');
-
-if (isset($_SERVER['HTTP_REFERER'])) {
-       preg_match('/(k|id)\/([0-9]*)\//',$_SERVER['HTTP_REFERER'],$ref_match);
-       $referer_id=$ref_match[1];
-}
 
 //connecting to database and creating universal $db object
+//require_once(INCLUDE_DIR.'senate.inc'); // in config already
 require_once(INCLUDE_DIR.'log.inc');
 require_once(INCLUDE_DIR.'ubik.inc');
 require_once(INCLUDE_DIR.'nodes.inc');
@@ -97,20 +33,74 @@ require_once(INCLUDE_DIR.'transports.inc');
 
 $db = new CLASS_DATABASE();
 
+switch(true) {
+       case preg_match('/id\/([0-9]+)(?:\/([a-zA-Z0-9]+)\/?)?/',$_SERVER['PATH_INFO'],$match):
+               //      print_r($match);
+               $_GET['node_id']=$match[1];
+               if (!empty($match[2])) {
+                       $_GET['template_id']=$match[2];
+               }
+               //Base36
+/*
+               if(!count($_POST) && !(isset($_GET['template_id']) && $_GET['template_id'] == 'download')) { //Fix ugly download hack...
+                       header('Location: /k/'.base_convert($_GET['node_id'], 10, 36).
+                               (isset($_GET['template_id'])?'/'.base_convert($_GET['template_id'], 10, 36):'')
+                       );
+                       die("Die!!! All Fascists Are Bastards...\n")
+               }
+*/
+               break;
+
+       case preg_match('/k\/([a-z0-9]{1,7})(?:\/([a-z0-9]+))?/',$_SERVER['PATH_INFO'],$match):
+               echo "picic";
+               $_GET['node_id']=base_convert($match[1], 36, 10);
+               if (!empty($match[2])) {
+                       $_GET['template_id']=$match[2];
+               }
+               break;
+       case preg_match('/name\/(.*?)\/?$/',$_SERVER['PATH_INFO'],$match):
+               $_GET['node_id']  = nodes::getNodeIdByName($match[1]);
+               break;
+       case preg_match('/search\/(.*?)\/?$/',$_SERVER['PATH_INFO'],$match):
+               //$query  = nodes::getNodeIdByName($match[1]);
+               //XXX TODO: Predat searchi az bude fungovat
+               break;
+       case preg_match('/\/(.+)\/?$/',$_SERVER['PATH_INFO'],$match):
+               $_GET['node_id']  = nodes::getNodeIdByName($match[1]);
+               break;
+       default:
+               $_GET['node_id']=1; //WELCOME_NODE
+               break;
+}
+
 if (!empty($_GET['template_id'])) {
        $template_id=$_GET['template_id'];
 } else {
        $template_id=false;
 }
 
-//initializing node methods
-if (!empty($_GET['node_name'])) {
-       $node  = nodes::redirByName($_GET['node_name']);
-} elseif (!empty($_GET['node_id'])) {
-       $node = nodes::getNodeById($_GET['node_id'],
-               (isset($_SESSION['user_id']))?$_SESSION['user_id']:'');
+error_reporting(E_ALL);
+//use wwwroot/debugswitch.php (from Your browser) to switch debugging on/off 
+if(isset($_SESSION['debugging']) && $_SESSION['debugging']) {
+    ini_set('display_errors','On');
+    echo 'GET VARIABLES::<br/>';
+    print_r($_GET);
+    echo 'POST VARIABLES::<br/>';
+    print_r($_POST);
+    echo 'FILES VARIABLES::<br/>';
+    print_r($_FILES);    
+    echo '<b>SESSION VARIABLES::</b><br/>';
+    print_r($_SESSION);
+} else {
+       $_SESSION['debugging']=false;
+       set_error_handler('logger::error_handler');
 }
 
+require_once(INCLUDE_DIR.'logout_idle.inc'); //Logout when idle
+
+//initializing node
+$node = nodes::getNodeById($_GET['node_id'],(isset($_SESSION['user_id']))?$_SESSION['user_id']:'');
+
 //XXX Paths are wrong (!)
 //loading smarty template engine and setting main parameters
 require(SMARTY_DIR.'Smarty.class.php');
@@ -126,7 +116,7 @@ $smarty->compile_dir = SYSTEM_DATA.'templates_c/';
 $smarty->config_dir = SMARTY_DIR.'configs/'; //XXX neexistuje
 $smarty->cache_dir = SMARTY_DIR.'cache/';
 $smarty->plugins_dir = SMARTY_PLUGIN_DIR ;
-if ($_SESSION['debugging']) $smarty->debugging=true;
+if (isset($_SESSION['debugging']) && $_SESSION['debugging']) $smarty->debugging=true;
 
 // initializing variables
 // preg_replace prevents LFI
@@ -134,7 +124,7 @@ if (empty($_POST['event'])) $event='display';
 else $event= preg_replace( "![^a-zA-Z0-9_]+!", "", $_POST['event']);
 
 
-if ($_SESSION['debugging']) {
+if (isset($_SESSION['debugging']) && $_SESSION['debugging']) {
        echo "<pre><b>NODE::";
        print_r($node);
        echo "</pre>";
@@ -153,186 +143,39 @@ if (isset($_SESSION['cube_vector']) && ($_SESSION['cube_vector'])) {
        }
 }
 
-//if not existent node show our own 404
-if (empty($node)) {
-       $nodes= nodes::getNodesByName($_GET['node_name']);
-       if ($nodes) {
-               $smarty->assign('nodes',$nodes);
-               $content=$smarty->display('404.tpl');
-               die();
-       }
-       elseif ($_SESSION['user_id']) {
-               $smarty->assign('node_name',$_GET['node_name']);
-               $content=$smarty->display("modules/addnode.tpl");
-       }
-}
-
-//modifying node glass pearl //XXX WTF
-//if (is_array($children_types[$node['node_type']])) {
-//     $smarty->assign('children_types',$children_types[$node['node_type']]);
-//}
-
-//smarty->assign('types',$types);
-
-
-//$node['node_type']=$types[$node['node_type']];
-/* This should NOT BE HANDLED HERE! This breaks things...
-$node['node_content']= StripSlashes($node['node_content']);
-$node['node_name']= StripSlashes($node['node_name']);
-*/
+#@include_once(INCLUDE_DIR.'mail_rss.inc'); //haluz...
 
 //checking permissions
-function _checkPermissions()
-{
-       global $permissions, $node;
-
-       require(INCLUDE_DIR.'permissions.inc');
-       $permissions=permissions::checkPermissions($node);
-       $permissions['h']=permissions::isHierarch($node);
+include_once(BACKEND_DIR.'/'.DB_TYPE.'/permissions.inc');
+$permissions=permissions::checkPerms($node);
+if (!empty($_SESSION['debugging']) && $_SESSION['debugging']) {
+       print_r($permissions);
 }
 
-// mail rss
-if ($template_id=='rss') //XXX WHAT?
-{
-       $_feedType = "RSS0.91";
-       if (!is_numeric($_SESSION['user_id']))
-       {
-               if (!isset($_SERVER['PHP_AUTH_USER'])) {
-                       header('WWW-Authenticate: Basic realm="Kyberia"');
-                       header('HTTP/1.0 401 Unauthorized');
-                       echo 'Cancel button';
-                       exit;
-               }
-               else
-               {
-                       require_once(EVENT_DIR.'/login.inc');
-                       $_POST['login'] = $_SERVER['PHP_AUTH_USER'];
-                       $_POST['password'] = $_SERVER['PHP_AUTH_PW'];
-                       $_POST['login_type'] = "name";
-                       if (!login())
-                       {
-                               echo "Zle meno/heslo.";
-                               exit();
-                       }
-               }
-       }
-
-       _checkPermissions();
-
-       // Mail
-       if ($_GET['node_id']==='24' && $permissions['r'])
-       {
-          require_once(INCLUDE_DIR.'/feedcreator.class.php');
-
-          $rss = new UniversalFeedCreator();
-          $rss->title = "Kyberia mail";
-          $rss->description = "";
-          $rss->link = "https://". SYSTEM_URL . "/id/24";
-
-               //XXX into function
-          $query = "select date_format(mail.mail_timestamp,\"%e.%c. %k:%i:%s\") as cas,
-   userfrom.user_action as locationfrom_action,
-   userfrom.user_action_id as locationfrom_action_id,
-   userto.user_action as locationto_action,
-   userto.user_action_id as locationto_action_id,
-   userto.login as mail_to_name, userfrom.login as mail_from_name,
-   mail.* from mail left join users as userfrom on
-   mail_from=userfrom.user_id left join users as userto on mail_to=userto.user_id
-   where mail_user='$_SESSION[user_id]' and mail_to='$_SESSION[user_id]' order by mail_id desc limit 0,10";
 
-          $set = $db->query($query);
 
-          while($set->next()) {
-                  $m = $set->getRecord();
-                  if ($m['mail_to'] != $_SESSION['user_id'])
-                          continue;
-                  $item = new FeedItem();
-                  $item->title = $m['mail_from_name'];
-                  $item->link = "https://".SYSTEM_URL."/id/24";
-                  $item->description = $m['mail_text'];
-                  $rss->addItem($item);
-          }
-       }
-       // bookmarks
-       elseif ($_GET['node_id']=='19' && $permissions['r'])
-       {
-               require_once(INCLUDE_DIR.'/feedcreator.class.php');
-
-               $rss = new UniversalFeedCreator();
-               $rss->title = "Kyberia bookmarks";
-               $rss->link = "http://".SYSTEM_URL."/id/19"; //XXX https ?
-
-               require_once(SMARTY_PLUGIN_DIR.'/function.get_bookmarks.php');
-               smarty_function_get_bookmarks(array(), $smarty);
-               $_items = $smarty->get_template_vars('get_bookmarks');
-               foreach ($_items as $_item)
-               {
-                       if (is_array($_item['children']))
-                               foreach ($_item['children'] as $_b)
-                               {
-                                       $item = new FeedItem();
-                                       $item->title = $_b['node_name'];
-                                       $item->link = "http://".SYSTEM_URL."/id/".$_b['node_id']."/rss";
-                                       $rss->addItem($item);
-                               }
-               }
-               $_feedType = 'OPML';
-       }
-       elseif ($permissions['r'])
-       {
-               require_once(INCLUDE_DIR.'/feedcreator.class.php');
-
-               $rss = new UniversalFeedCreator();
-               $rss->title = $node['node_name'];
-               $rss->description = "";
-               $rss->link = "http://".SYSTEM_URL."/id/".$node['node_id'];
-
-               // K list
-               if ($_GET['node_id']=='15')
-               {
-                       require_once(SMARTY_PLUGIN_DIR.'/function.get_k.php');
-                       smarty_function_get_k(array(), $smarty);
-                       $_items = $smarty->get_template_vars('get_k');
-               }
-               else
-               {
-                       require_once(SMARTY_PLUGIN_DIR.'/function.get_children.php');
-                       smarty_function_get_children(
-                               array('orderby' => 'desc', 'orderby_type' => 'time'), $smarty);
-                       $_items = $smarty->get_template_vars('get_children');
-               }
-
-               foreach ($_items as $_item)
-               {
-                       $item = new FeedItem();
-                       $item->title = $_item['node_name'];
-                       $item->link = "http://".SYSTEM_URL."/id/".$_item['node_id'];
-                       $item->description = $_item['node_content'];
-                       $rss->addItem($item);
+// DO NOT MESS WITH THIS !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+//creating neural network
+$db->update("update nodes set node_views=node_views+1 where node_id='".$node['node_id']."'");
+if (isset($referer_id) && is_numeric($referer_id)) {
+       $q="update neurons set synapse=synapse+1 where dst='".$node['node_id']."' and src='$referer_id'";
+       $result=$db->update($q);
+       if (!$result) {
+               $q="insert into neurons set synapse_creator='".$_SESSION['user_id']."',dst='".$node['node_id']."',src='$referer_id',synapse=1";
+               $db->query($q);
                }
-       }
-
-       if ($permissions['r']) $rss->showFeed($_feedType);
-       exit();
+} else {
+       logger::log('enter',$node['node_id'],'failed');
 }
 
 
-_checkPermissions();
-
-//entering the node
 
+//entering the node (executing the eventz)
 if (($permissions['r']) || ($event != 'register')) {
-
        //performing node_events (based on update/insert/delete db queries)
        if ($event) {
                require(INCLUDE_DIR.'eventz.inc');
        }
-
-       //end of performing node events
 }
 
-
-// output buffering forcing (mx)
-if (!empty($_POST['FORCE_OB']) && $_POST['FORCE_OB'] == 'true') ob_end_flush();
-
 ?>
This page took 0.175663 seconds and 4 git commands to generate.