<?php
	/*
	test_board_index.php v1.0 by kristoffe brodeur. ©2021 All Rights Reserved.
	
	01-11-2021
		simple program to scrape the board indexes
		of the main area of a phpbb with [Lucid Lime] theme
	*/
	
	$to_root="./";
	$base="http://www.supercala.net/phpBB3/";//allows us to use the scraped data to make links to the forum page areas
	$url=$base."index.php";
	$html=file_get_contents($url);
	//echo "scraping away v1.0 by kristoffe<hr />";
	
	$phpbbMain_doc=new DomDocument();
	
	libxml_use_internal_errors(TRUE);//disable libxml errors
	$bbStr="";
	$topMenuStr="";
	
	//
	if(!empty($html))
		{
		//echo "cool we got the website as data<hr />";
		
		$phpbbMain_doc->loadHTML($html);
		libxml_clear_errors();//remove any of the weird html errors etc we don't need
		
		$phpbbMain_xpath=new DOMXPath($phpbbMain_doc);		
		$phpbbMain_topics=$phpbbMain_xpath->query('//div[1]/div/ul[2]/li/dl/dt/div/a');
		$lenT=0;
		$lenSS=0;
			
		//
		if($phpbbMain_topics->length >0)
			{
			//echo "[let's look into an object node for it's parts now]<br/>";
			$lenT=count($phpbbMain_topics);
			$pagePos=-1;
			
			//
			foreach($phpbbMain_topics as $row)
				{
				$pagePos++;
				$a_textCol=$row->nodeValue;
				$a_hrefCol=$row->getAttribute("href");
				
				//get rid of the one character (.) period in the return ./viewforum.php?f= etc
				$aStr=substr($a_hrefCol,1);
				
				/*
				use the $base not the $url so it starts from the phpbb forum folder root as the ^ link goes to on the page(s)
				pretend we're at the scraped url and going to the link as if we were locally there (./)
				*/
				$a_hrefLink=$base.$aStr;
				
				//now let's get the variables (queries) from both ? and & in the href string phpbb outputs to each area
				$queryArr=parse_url($a_hrefCol);
				//
				foreach($queryArr as $key=>$val)
					{
					$subPageStr="";
					
					//echo "[$key][$val]<br />";
					//
					if($key=="query")
						{
						$tmpQListArr=array();
						parse_str($val,$tmpQListArr);
						//echo count($tmpQListArr)."<br />";
						$forumNode=-1;
						//
						foreach($tmpQListArr as $key2=>$val2)
							{
							//
							switch($key2)
								{
								case("f"):
									$forumNode=$val2;
									//echo "[forum id]$forumNode<br />";	
									break;
								case("sid"):
									break;
								default:
									break;
								}
							}
						//
						if($forumNode!=-1)
							{
							/*
							
							test with $pagePos==0 to save time and debug if one loop of doSubArea(#) will work
							
							test with $pagePos<$lenT for all of them. 
							it might be better to call this with javascript and populate areas with a 1 second wait
							instead of a really long pause. maybe
								html->
									jquery php that does this with a result->
									add to area on screen per main area with sub-areas via DOM js->
									wait 1s->
									loop->
							*/
							
							//
							if($pagePos<$lenT)
								{
								$subPageStr=doSubAreaPage($forumNode);
								//echo "!!!!! [$subPageStr] !!!!! <br />";
								}
							}
						}
					/*
					now the forum areas have titles and links to all the their listed nodes too
					time to loop but with a different target per loop on the cpath found with [scraper] and chrome (I'm learning)
					*/
					
					}
				$topMenuStr.="
					<div class='bb_areaButton'><a href='#area".$pagePos."'>".$a_textCol."</a></div>
				";
				
				$bbStr.="
					<div class='bb_page' id='area".$pagePos."'>
						<div class='dataBox_m'>
							$a_textCol
						</div>
						<div class='dataBox_l'>
							<a href='$a_hrefLink'>$a_hrefCol</a>
						</div>
						<div class='clearBoth'></div>
							$subPageStr
					</div>
					";
				}
			}
			
		}
	//
	function doSubAreaPage($sentArea)
		{
		global $base;
		global $lenSS;
		
		$sub_url=$base."viewforum.php?f=".$sentArea;
		//echo "$sub_url<br />";
		$sub_html=file_get_contents($sub_url);
		$phpbbSub_doc=new DomDocument();
		
		//maybe only declare this once, not each time per php page (done in page root)
		//libxml_use_internal_errors(TRUE);
		
		$sub_bbStr="";
		//
		if(!empty($sub_html))
			{
			$phpbbSub_doc->loadHTML($sub_html);
			libxml_clear_errors();
			$phpbbSub_xpath=new DOMXPath($phpbbSub_doc);
			/*
			new query, the template puts the areas in a different level of the DOM structure
			found right scraper and just right clicking the title of the sub area entry and [scraper similar...] 
			lazy, but it's a lot to learn at once
			*/
			$phpbbSub_topics=$phpbbSub_xpath->query('//dt/div/a');
			//echo "subTopic areas found with xpath [".count($phpbbSub_topics)."]<br />";
			
			$lenST=0;
				
			//
			if($phpbbSub_topics->length >0)
				{
				//echo "sub topic page[$sentArea] so far so good!<br />";
				$pagePos=-1;
				
				//
				foreach($phpbbSub_topics as $row)
					{
					$pagePos++;
					$a_textCol=$row->nodeValue;
					$a_hrefCol=$row->getAttribute("href");
					$aStr=substr($a_hrefCol,1);
					$a_hrefLink=$GLOBALS['base'].$aStr;
					
					$lenSS++;
					
					//
					$sub_bbStr.="
						<div class='bb_page_sub'>
							<div class='dataBox_m'>
								$a_textCol
							</div>
							<div class='dataBox_l'>
								<a href='$a_hrefLink'>$a_hrefCol</a>
							</div>
							<div class='clearBoth'></div>
						</div>
						";
					}
				}					
			}
		//
		else
			{
		$sub_bbStr="page load error<br />";
			}

		return $sub_bbStr;
		}
		
	//
	function showRows()
		{
		$lenT=count($phpbbMain_topics);
		//echo "wow. I found [$lenT] rows of topics! thanks to php xpath and chrome with the 'scraper' plugin<hr />";
		//
		foreach($phpbbMain_topics as $row)
			{
			//echo "<div class=''>".$row->nodeValue."</div>";
			}
		}
	//echo "<hr />finished<hr />";
?>

<html>
	<head>
		<link type="text/css" rel="stylesheet" href="<?php echo $to_root;?>css/page.css" />
	</head>
	<body>
		<div class='dataBox_m'>Forum Main Sections</div>
		<div class='dataBox_l'><?php echo $lenT;?></div>
		<div class="clearBoth"></div>
		
		<div class='dataBox_m'>Total Postings In All Main Areas</div>
		<div class='dataBox_l'><?php echo $lenSS;?></div>
		<div class="clearBoth"></div>
				
		<div class="topMenuButtons">
			<?php echo $topMenuStr;?>
		</div>
		<div class="clearBoth"></div>
		
		<?php echo $bbStr;?>
	</body>
</html>