<?php

/*
	PHP sample code for Siteliner Premium API
	
	Compatible with PHP 5.2.x or later, requires curl compiled in
	
	You may install, use, reproduce, modify and redistribute this code, with or without
	modifications, subject to the general Terms and Conditions on the Siteliner website.
	
	For any technical assistance please contact us via our website.
	
	5-Aug-2020: First version
	
	Siteliner (c) Indigo Stream Technologies 2020 - https://www.siteliner.com/


	Instructions for use:
	
	1. Set the constants SITELINER_USERNAME and SITELINER_API_KEY below to your details.
	2. Call the appropriate API function, following the examples below.
	3. Use print_r to discover the structure of the output.
	4. To run the example provided, please uncomment and edit the lines below.
	   NOTE: Each scanned page and API request for scan results costs 1c
*/

	//$runexample=true;
	//$rooturl='http://www.example.com/';
	//$maxpages=100;
	
/*
	Error handling:
	
	* If a call failed completely (e.g. curl failed to connect), functions return false.
	* If the API returned an error, the response array will contain an 'error' element.
*/

/*
	A. Constants you need to change
*/

	define('SITELINER_USERNAME', 'your-siteliner-username');
	define('SITELINER_API_KEY', 'your-siteliner-api-key');
    
	define('SITELINER_API_URL', 'https://www.siteliner.com/api/');    
	
/*
	B. Functions for you to use
*/
	
	function siteliner_get_account_summary($count=100, $start=1)
	{
		return siteliner_api_call('account',array('count'=>$count,'start'=>$start));
	}
	
	function siteliner_start_scan($rooturl, $maxpages, $parameters=null)
	{
		// any optional API parameters should be provided to the function in the format specified by the API documentation
		//
		// Example:
		//
		// siteliner_start_scan("http://www.example.com", 100,
		//						array("scanmode" => "excludedirs",
		//							  "excludedirs" => "sports/\nbusiness/banking/"
		//							 ));
		
		$params=array(
					'rooturl'  => $rooturl,
					'maxpages' => $maxpages,
					 );
		
		if(is_array($parameters))
		{
			$params=array_merge($params,$parameters);
		}

		return siteliner_api_call('start',
								  $params,
								  true);
	}
	
	function siteliner_pause_scan($scan)
	{
		return siteliner_api_call('pause',array('scan'=> $scan),true);
	}
	
	function siteliner_resume_scan($scan)
	{
		return siteliner_api_call('resume',array('scan'=> $scan),true);
	}
	
	function siteliner_cancel_scan($scan)
	{
		return siteliner_api_call('cancel',array('scan'=> $scan),true);
	}
	
	function siteliner_get_scan_status($scan)
	{
		return siteliner_api_call('status',array('scan'=> $scan));
	}
	
	function siteliner_get_scan_summary($scan)
	{
		return siteliner_api_call('sitesummary',array('scan'=> $scan));
	}

	function siteliner_get_analyzed_pages($scan, $count=100, $start=1)
	{
		return siteliner_api_call('siteanalyzed',array('scan'=> $scan,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_skipped_pages($scan, $count=100, $start=1)
	{
		return siteliner_api_call('siteskipped',array('scan'=> $scan,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_duplicate_pages($scan, $count=100, $start=1)
	{
		return siteliner_api_call('siteduplicate',array('scan'=> $scan,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_broken_link_pages($scan, $count=100, $start=1)
	{
		return siteliner_api_call('sitebroken',array('scan'=> $scan,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_related_domains($scan, $count=100, $start=1)
	{
		return siteliner_api_call('siterelateddomain',array('scan'=> $scan,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_page_duplicates($scan, $page, $count=100, $start=1)
	{
		return siteliner_api_call('pageduplicate',array('scan'=> $scan,'page'=> $page,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_page_int_links_in($scan, $page, $count=100, $start=1)
	{
		return siteliner_api_call('pagelinkin',array('scan'=> $scan,'page'=> $page,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_page_int_links_out($scan, $page, $count=100, $start=1)
	{
		return siteliner_api_call('pagelinkout',array('scan'=> $scan,'page'=> $page,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_page_ext_links_out($scan, $page, $count=100, $start=1)
	{
		return siteliner_api_call('pageexternal',array('scan'=> $scan,'page'=> $page,'count'=>$count,'start'=>$start));
	}
	
	function siteliner_get_related_links_in($scan, $url, $count=100, $start=1)
	{
		return siteliner_api_call('relatedlinkin',array('scan'=> $scan,'url'=> $url,'count'=>$count,'start'=>$start));
	}

/*
	C. Example
*/
	
	if($runexample)
	{
		siteliner_run_example($rooturl, $maxpages);
	}
	
	function siteliner_run_example($rooturl, $maxpages)
	{
		$account_summary=siteliner_get_account_summary(1);
		handle_error($account_summary);

		if($account_summary->resultcount>0)
			echo sprintf("\nLast scan %s for %s performed on %s\n",
					$account_summary->results[0]->scan, $account_summary->results[0]->rooturl,gmdate("M j, Y",$account_summary->results[0]->starttime));

		echo "\nStarting scan for ".$rooturl."\n";

		$scan_info=siteliner_start_scan($rooturl,$maxpages);
		handle_error($scan_info);

		$scan=$scan_info->scan;
		echo sprintf("Scan successfully started, scan ID: %s\n",$scan);

		while($scan_info->status != 'completed')
		{
			sleep(10);
			$scan_info=siteliner_get_scan_status($scan);
			handle_error($scan_info);
			echo sprintf("%s found so far, %s retrieved\n",format_plural_string($scan_info->found,"page"),format_plural_string($scan_info->retrieved,"page"));
		}

		$summary=siteliner_get_scan_summary($scan);
		handle_error($summary);

		echo sprintf("\nScan completed successfully, summary:\n");
		print_r($summary);

		$analyzed=siteliner_get_analyzed_pages($scan,5);
		handle_error($analyzed);
		echo sprintf("\n%s:\n",format_plural_string(count($analyzed->results), 'most prominent page'));
		print_r($analyzed);

		$home_links_in= siteliner_get_page_int_links_in($scan,"/",5);
		handle_error($home_links_in);
		if($home_links_in->resultcount > 0)
		{
			echo sprintf("\n%s with most links to home page:\n\n",format_plural_string(count($home_links_in->results), 'page'));
			print_r($home_links_in);
		}
		else
		{
			echo sprintf("\nNo pages found with links to home page\n\n");
		}
	}
	
/*
	D. Functions used internally
*/

	function siteliner_api_call($operation, $params=array(), $post=false)
	{
		$paramstring='user='.urlencode(SITELINER_USERNAME).
					 '&key='.urlencode(SITELINER_API_KEY);
		if($post)
			$paramstring.='&command='.urlencode($operation);
		else
			$paramstring.='&report='.urlencode($operation);
		
		foreach ($params as $name => $value)
			$paramstring.='&'.urlencode($name).'='.urlencode($value);
		
		$url=SITELINER_API_URL;
		if(!$post)
			$url.='?'.$paramstring;
		
		$curl=curl_init();
		
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_TIMEOUT, 30);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_POST, $post);
		
		if ($post)
		{
			curl_setopt($curl, CURLOPT_HTTPHEADER,array('application/x-www-form-urlencoded'));
			curl_setopt($curl, CURLOPT_POSTFIELDS, $paramstring);
		}
		
		$response=curl_exec($curl);
		curl_close($curl);
		
		if (strlen($response))
			return json_decode($response);
		else
			return false;
	}
	
	function handle_error($response)
	{
		if($response === false)
		{
			echo "API call failed\n";
			die();
		}
		if(isset($response->error))
		{
			echo "API returned error: ".$response->error."\n";
			die();
		}
	}

	function format_plural_string($count,$text)
	{
		$result='No '.$text.'s';
		
		if($count)
		{
			if($count==1)
			{
				$result='1 '.$text;
			}
			else
			{
				$result=number_format($count).' '.$text.'s';
			}
		}
		
		return $result;
	}
