<?php

/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
 * Developer Notes:
 *        Discussion about TOS for this site here: 
 *        https://github.com/POSSA/Caller-ID-Superfecta/issues/182#issuecomment-38616185
 * 
 *
 * Version History
 *        2014-03-25   Initial release by Håkan
 *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/
 
 class SE_118100_HE extends superfecta_base {
        public $description = "http://www.118100.se - This listing include data from the Swedish 118100.se directory.";
        public $version_requirement = "2.11";

	function get_caller_id($thenumber, $run_param=array()) {
		$debug = $this->debug;
                $this->DebugPrint("Searching www.118100.se ... ");
                $number_error = false;

		//check for the correct digits for Sweden in international format.
		// international dialing prefix + country code + number

		if (strlen($thenumber) > 8) {
			if (substr($thenumber,0,2) == '46')
			{
				$thenumber = '0'.substr($thenumber, 2);
			}
			else
			{
				if (substr($thenumber,0,4) == '0046')
				{
					$thenumber = '0'.substr($thenumber, 4);
				}
				else
				{
					if (substr($thenumber,0,5) == '01146')
					{
						$thenumber = '0'.substr($thenumber,5);
					}
					else
					{
						$number_error = true;
					}
				}
			}
		}

		// number
		if(strlen($thenumber) < 11)
		{
			if (substr($thenumber,0,1) == '0')
			{
				$number_error = false;
			}
			else
			{
				$number_error = true;
			}
		}

		if(!$number_error)
		{
			$this->DebugPrint("Numret ar svenskt, slar nu upp numret.");
                        $this->DebugPrint("Search in 118100.se... ");

                        $url="http://www.118100.se/sok/$thenumber";
                        $value = $this->get_url_contents($url);

                        $notfound = strpos($value, "Hoppsan!"); //No hit.
                        $notfound = ($notfound < 1) ? strpos($value, "Hoppsan!") : $notfound;
                        if($notfound)
                        {
                                $value = "";
                                $this->DebugPrint("www.118100.se reports number not found!<br>\n");
                        }
                        else
                        {
                                $start = strpos($value, "nameBlock\">");
                                $value = substr($value,$start+12);
                                $end = strpos($value, "</span>");
                                $value = substr($value,0,$end);
                        }
                        if (strlen($value) > 1)
                        {
                                $caller_id = strip_tags($value);
                                return($caller_id);
                        }
                        else
                        {
                                $this->DebugPrint("www.118100.se returns no data!<br>\n");
                        }
		}
		else 
		{
			$this->DebugPrint("Skipping Source - Non Swedish number: ".$thenumber."<br>\n");
		}
   	}
}
