lainchan-webring

Code for the unofficial lainchan webring.
git clone git://git.concealed.world/lainchan-webring
Log | Files | Refs | README | LICENSE

webring.php (3142B)


      1 <?php
      2 
      3 #    Copyright (C) <2021>  <nixx@firemail.cc> & contributors
      4 #
      5 #    This program is free software: you can redistribute it and/or modify
      6 #    it under the terms of the GNU General Public License as published by
      7 #    the Free Software Foundation, either version 3 of the License, or
      8 #    (at your option) any later version.
      9 #
     10 #    This program is distributed in the hope that it will be useful,
     11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 #    GNU General Public License for more details.
     14 #
     15 #    You should have received a copy of the GNU General Public License
     16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 $valid_action = array('next', 'previous', 'random'); // Ring actions
     19 $valid_network = array('clearnet', 'onion'); // Ring networks
     20 $file = 'webring.csv'; // Sites list
     21 $list = []; // Final array
     22 $redirkey = 0; // Site to go to, 0 should be ringmaster
     23 
     24 // If sites file doesn't exist, die
     25 if(!file_exists($file)) die('ERROR: Sites list \''.$file.'\' not found.');
     26 
     27 // Open file, store data, close file
     28 $handle = fopen($file, 'r');
     29 while ($row = fgetcsv($handle)){
     30 	$list[] = $row;
     31 }
     32 fclose($handle);
     33 
     34 // Get number of sites
     35 $length = count($list) - 1;
     36 
     37 // Check network
     38 $network = isset($_GET['network']) ? strtolower($_GET['network']) : 'clearnet';
     39 if(!in_array($network, $valid_network)) die('ERROR: Invalid network \''.$network.'\'.');
     40 
     41 // Remove irrelevant rows
     42 $count = 0;
     43 while($count <= $length){
     44 	if($list[$count][0] != $network){
     45 		array_splice($list, $count, 1);
     46 		$length = count($list) - 1;
     47 	}else{
     48 		$count++;
     49 	}
     50 }
     51 
     52 // Check calling site then store index
     53 $caller = isset($_GET['id']) ? strtolower($_GET['id']) : $list[0][1];
     54 if(!in_array($caller, array_column($list, 1))) die('ERROR: Invalid member ID \''.$caller.'\'.');
     55 $id = array_search($caller, array_column($list, 1));
     56 
     57 // Store and check action
     58 $action = isset($_GET['action']) ? strtolower($_GET['action']) : 'random';
     59 if(!in_array($action, $valid_action)) die('ERROR: Invalid action \''.$action.'\'.');
     60 
     61 // Store and check banner mode
     62 $banner = isset($_GET['banner']) ? $_GET['banner'] : 'false';
     63 if(!in_array($banner, array('true', 'false'))) die('ERROR: Invalid banner mode \''.$banner.'\'.');
     64 // No banners with random mode
     65 if($banner == 'true' && $action == "random") die('ERROR: Cannot use banners with random mode.');
     66 
     67 // Perform requested action
     68 if($action == 'next'){ // Get next site in list
     69 	$redirkey = $id + 1;
     70 	if($redirkey > $length){ // Fix overflow
     71 		$redirkey = 0;
     72 	}
     73 }elseif($action == 'previous'){ // Get previous site
     74 	$redirkey = $id - 1;
     75 	if($redirkey < 0){ // Fix underflow
     76 		$redirkey = $length;
     77 	}
     78 }
     79 
     80 // Show banner
     81 if($banner == 'true'){
     82 	header('Location: ../img/bnr/'.$list[$redirkey][1].'.'.$list[$redirkey][3]);
     83 	exit;
     84 }elseif($action == 'random'){ // Get random site
     85 	$redirkey = rand(0, $length);
     86 	if($length + 1 > 1){ // Don't waste CPU
     87 		while($redirkey == $id){ // Don't visit caller
     88 			$redirkey = rand(0, $length);
     89 		}
     90 	}
     91 }
     92 
     93 // Finally, do the redirect
     94 header('Location: '.$list[$redirkey][2]);
     95 
     96 ?>