<?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_clearsearch_HE extends superfecta_base {
        public $description = "http://www.clearsearch.se - This listing include data from the Swedish Clearsearch directory.";
        public $version_requirement = "2.11";
        public $source_param = array (
                 'Search_Type' => array (
                   'description' => 'Enter 1=Personsok 2=Foretagssok 3=Bade Person och Foretag',
                   'type' => 'number'
		 )
        );

	function get_caller_id($thenumber, $run_param=array()) {
		$debug = $this->debug;
                $this->DebugPrint("Searching clearsearch.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.");
			if($run_param['Search_Type'] != 2)
                        {
                                $this->DebugPrint("Search for PERSON in clearsearch.se... ");

                                $url="http://www.clearsearch.se/personer/-/q_$thenumber/1/";
                                $value = $this->get_url_contents($url);

                                $notfound = strpos($value, "inte hitta"); //No hit.
                                $notfound = ($notfound < 1) ? strpos($value, "inte hitta") : $notfound;
                                if($notfound)
                                {
                                        $value = "";
                                        $this->DebugPrint("clearsearch PERSON reports number not found!<br>\n");
                                }
                                else
                                {
                                        $start = strpos($value, "class=\"result-bn  medium\" itemprop=\"name\">");
                                        $value = substr($value,$start+42);
                                        $end = strpos($value, "</span>");
                                        $value = substr($value,0,$end);
					
					$value = str_replace("\r\n", "", $value);
					$value = str_replace(" ", "", $value);
					$this->DebugPrint("Resultatet:".$value);

                                }
                                if (strlen($value) > 1)
                                {
					
                                        $caller_id = strip_tags($value);
                                        return($caller_id);
                                }
                                else
                                {
                                        $this->DebugPrint("clearsearch PERSON returns no data!<br>\n");
                                }
                        }
			if($run_param['Search_Type'] >= 2)
                        {

				$this->DebugPrint("Search for BUSINESS in clearsearch.se... ");

                                $url="http://www.clearsearch.se/foretag/-/q_$thenumber/1/";
                                $value = $this->get_url_contents($url);

                                $notfound = strpos($value, "inte hitta"); //No hit.
                                $notfound = ($notfound < 1) ? strpos($value, "inte hitta") : $notfound;
                                if($notfound)
                                {
                                        $value = "";
                                        $this->DebugPrint("clearsearch BUSINESS reports number not found!<br>\n");
                                }
                                else
                                {
                                        $start = strpos($value, "class=\"result-bn  medium\">");
                                        $value = substr($value,$start+30);
                                        $end = strpos($value, "</span>");
                                        $value = substr($value,0,$end);

                                        $value = str_replace("\r\n", "", $value);
                                        $value = str_replace(" ", "", $value);
                                        $this->DebugPrint("Resultatet:".$value);

                                }
                                if (strlen($value) > 1)
                                {

                                        $caller_id = strip_tags($value);
                                        return($caller_id);
                                }
                                else
                                {
                                        $this->DebugPrint("clearserach BUSINESS returns no data!<br>\n");
                                }


                        }
		}
		else 
		{
			$this->DebugPrint("Skipping Source - Non Swedish number: ".$thenumber."<br>\n");
		}
   	}
	function cisf_url_encode_array($arr){
	  $string = "";
	  foreach ($arr as $key => $value) {
		$string .= $key . "=" . urlencode($value) . "&";
	  }
	  trim($string,"&");
	  return $string;
	}
}
