<file_sep><?php

require_once("common.php");
require_once("twitter.php");
$debug=1;


require_once('/www/govalert/streams/interpol/tasks.php');
interpolIzcheznali();
interpolProcessIzcheznali();

?>
<file_sep><?php

/*
links:
0: новини http://www.nek.bg/cgi?d=101
*/

function nekSaobshteniq() {
  setSession(6,0);

  echo "> Проверявам за съобщения на НЕК\n";
  
  $html = loadURL("http://www.nek.bg/cgi?d=101",0);
  if (!$html) return;
  $html = mb_convert_encoding($html, "utf8","cp1251");
  $items = nek_xpathDoc($html,"//div[@class='subpage']//li");

  $query=array();
	foreach ($items as $item) {
    if (count($query)>15)
      break;
    $hash = md5($item->textContent);

    $date = $item->childNodes->item(0)->textContent;
    $date = mb_substr($date,9,4)."-".mb_substr($date,5,2)."-".mb_substr($date,1,2);
    if (strtotime($date)<strtotime("-1 month"))
      continue;
    $url = $item->childNodes->item(2)->getAttribute("href");
    $title = $item->childNodes->item(2)->textContent;
    $title = mb_ereg_replace("П Р Е С С Ъ О Б Щ Е Н И Е","Прессъобщение",$title,"im");
    $title = mb_ereg_replace("О Б Я В Л Е Н И Е","Обявление",$title,"im");
    $title = "Съобщение: ".nek_cleanText($title);
    $query[]=array($title,null,$date,"http://www.nek.bg$url",$hash);
  }

  echo "Възможни ".count($query)." нови съобщения\n";

  $itemids = saveItems($query);
  queueTweets($itemids);
}

/*
-----------------------------------------------------------------
*/

function nek_xpathDoc($html,$q) {
  if (!$html)
    return array();
  $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  $doc = new DOMDocument("1.0", "UTF-8");
  $doc->preserveWhiteSpace=false;
  $doc->strictErrorChecking=false;
  $doc->encoding = 'UTF-8';
  $doc->loadHTML($html);
  $xpath = new DOMXpath($doc);  

  $items = $xpath->query($q);
  return is_null($items)?array():$items;
}


function nek_cleanText($text) {
  $text = str_replace(" "," ",$text);
	$text = mb_ereg_replace("[\n\r\t ]+"," ",$text);
  $text = mb_ereg_replace("(^\s+)|(\s+$)", "", $text);
	$text = html_entity_decode($text);
	return $text;
}

?>
<file_sep><?php

/*

0: новини http://www.prb.bg/main/bg/News/
1: документи http://www.prb.bg/main/bg/Documents/
2: конкурс http://www.prb.bg/main/bg/konkursi
3: галерия http://www.prb.bg/main/bg/gallery/

*/

function prok_Novini() {
  echo "> Проверявам за новини в Прокуратурата\n";
  setSession(13,0);

  $html = loadURL("http://www.prb.bg/main/bg/News/",0);
  if (!$html) return;
  $items = prok_xpathDoc($html,"//div[@class='list-inner']");

  $query=array();
	foreach ($items as $item) {
    $hasimage=$item->childNodes->item(1)->nodeName=="a";
      
    $date = trim($item->childNodes->item(3+($hasimage?2:0))->textContent);
    $date = mb_substr($date,6,4)."-".mb_substr($date,3,2)."-".mb_substr($date,0,2);
    if (strtotime($date)<strtotime("-2 weeks"))
      continue;
    $description = trim($item->childNodes->item(5+($hasimage?2:0))->textContent);
    $description = prok_cleanText($description);

    $title = trim($item->childNodes->item(1+($hasimage?2:0))->textContent);
    $title = prok_cleanTitle($title);
    $title = prok_cleanText($title);

    $url = "http://www.prb.bg".$item->childNodes->item(1)->firstChild->getAttribute("href");
    $hash = md5($url);
    $media = null;
    if ($hasimage) {
      $imageurl = $item->childNodes->item(1)->firstChild->getAttribute("src");
      $imageurl=mb_ereg_replace("logo","big",$imageurl,"im");
      $imageurl = "http://www.prb.bg$imageurl";
      $imagetitle = trim($item->childNodes->item(3)->textContent);
      $imagetitle = prok_cleanTitle($imagetitle);
      $imagetitle = prok_cleanText($imagetitle);
      $media = array("image" => array(loadItemImage($imageurl),$imagetitle));
    }

    $query[]=array($title,$description,$date,$url,$hash,$media);
  }

  echo "Възможни ".count($query)." нови новини\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function prok_Dokumenti() {
  echo "> Проверявам за документи в Прокуратурата\n";
  setSession(13,1);

  $html = loadURL("http://www.prb.bg/main/bg/Documents/",1);
  if (!$html) return;
  $items = prok_xpathDoc($html,"//div[@class='list-inner']");

  $query=array();
	foreach ($items as $item) {

    $date = trim($item->childNodes->item(3)->textContent);
    $date = mb_substr($date,6,4)."-".mb_substr($date,3,2)."-".mb_substr($date,0,2);
    if (strtotime($date)<strtotime("-2 months"))
      continue;
    $description = trim($item->childNodes->item(5)->textContent);
    $description = prok_cleanText($description);

    $title = trim($item->childNodes->item(1)->textContent);
    $title = prok_cleanTitle($title);
    $title = "Документ: ".prok_cleanText($title);

    $url = "http://www.prb.bg".$item->childNodes->item(1)->firstChild->getAttribute("href");
    $hash = md5($url);

    $query[]=array($title,$description,$date,$url,$hash);
  }

  echo "Възможни ".count($query)." нови документи\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function prok_Konkursi() {
  echo "> Проверявам за конкурси в Прокуратурата\n";
  setSession(13,2);

  $html = loadURL("http://www.prb.bg/main/bg/konkursi",2);
  if (!$html) return;
  $items = prok_xpathDoc($html,"//div[@class='list-inner']");

  $query=array();
	foreach ($items as $item) {

    $date = trim($item->childNodes->item(3)->textContent);
    $date = mb_substr($date,6,4)."-".mb_substr($date,3,2)."-".mb_substr($date,0,2);
    if (strtotime($date)<strtotime("-2 weeks"))
      continue;
    $description = trim($item->childNodes->item(5)->textContent);
    $description = prok_cleanText($description);

    $title = trim($item->childNodes->item(1)->textContent);
    $title = prok_cleanTitle($title);
    $title = "Конкурс: ".prok_cleanText($title);

    $url = "http://www.prb.bg".$item->childNodes->item(1+($hasimage?2:0))->firstChild->getAttribute("href");
    $hash = md5($url);

    $query[]=array($title,$description,$date,$url,$hash);
  }

  echo "Възможни ".count($query)." нови конкурса\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function prok_Snimki() {
  echo "> Проверявам за галерии в Прокуратурата\n";
  setSession(13,3);

  $html = loadURL("http://www.prb.bg/main/bg/gallery/",3);
  if (!$html) return;
  $items = prok_xpathDoc($html,"//div[@class='list-inner']");

  $query=array();
	foreach ($items as $item) {
    $date = trim($item->childNodes->item(5)->textContent);
    $date = mb_substr($date,6,4)."-".mb_substr($date,3,2)."-".mb_substr($date,0,2);
    if (strtotime($date)<strtotime("-2 weeks"))
      continue;

    $title = trim($item->childNodes->item(3)->textContent);
    $title = prok_cleanTitle($title);
    $title = "Снимки: ".$title;
    $title = prok_cleanText($title);

    $url = "http://www.prb.bg".$item->childNodes->item(1)->getAttribute("href");
    $hash = md5($url);
    $media = array("image" => array());
    $mhtml = loadURL($url);
    if (!$mhtml) 
      continue;

    $mitems = prok_xpathDoc($mhtml,"//a[@class='thumb']");
    foreach ($mitems as $mitem) {
      $imageurl = $mitem->getAttribute("href");
      $imageurl = "http://www.prb.bg$imageurl";
      $imageurl = str_replace(array("logo","pic"),"big",$imageurl);
      $imageurl = loadItemImage($imageurl);
      if ($imageurl)
        $media["image"][] = array($imageurl);
    }
  
    if (count($media["image"])==0)
      $media=null;

    $query[]=array($title,null,$date,$url,$hash,$media);
  }

  echo "Възможни ".count($query)." нови галерии\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

/*
-----------------------------------------------------------------
*/

function prok_xpathDoc($html,$q) {
  if (!$html)
    return array();
  $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  $doc = new DOMDocument("1.0", "UTF-8");
  $doc->preserveWhiteSpace=false;
  $doc->strictErrorChecking=false;
  $doc->encoding = 'UTF-8';
  $doc->loadHTML($html);
  $xpath = new DOMXpath($doc);  

  $items = $xpath->query($q);
  return is_null($items)?array():$items;
}


function prok_cleanTitle($title) {
  if (mb_substr($title,-1)==".")
    $title = mb_substr($title,0,mb_strlen($title)-1);
  $title=mb_ereg_replace("Република България","РБ",$title,"im");
  $title=mb_ereg_replace("Р България","РБ",$title,"im");
  $title=mb_ereg_replace("„|“","",$title,"im");
  $title=mb_ereg_replace("Народно(то)? събрание","НС",$title,"im");
  $title=mb_ereg_replace("Министерски(ят)? съвет","МС",$title,"im");
  $title=mb_ereg_replace("(ИЗБИРАТЕЛНИ КОМИСИИ)|(избирателна комисия)","ИК",$title,"im");
  $title=mb_ereg_replace("ОБЯВЛЕНИЕОТНОСНО:?|ОТНОСНО:?|С Ъ О Б Щ Е Н И Е|СЪОБЩЕНИЕ|г\.|ч\.|\\\\|„|\"|'","",$title,"im");
  return $title;
}

function prok_cleanText($text) {
  $text = text_cleanSpaces($text);
	$text = html_entity_decode($text);
	return $text;
}

?>
<file_sep><?php

/*

0: съобщения http://www.mh.government.bg/AllMessages.aspx
1: новини http://www.mh.government.bg/News.aspx?pageid=401
2: проекти за нормативни актове http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=393 
3: наредби http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=391
4: постановления http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=381
5: отчети http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=532&currentPage=1

*/

function mh_Saobshteniq() {
  echo "> Проверявам за съобщения в МЗ\n";
  setSession(21,0);

  $html = loadURL("http://www.mh.government.bg/AllMessages.aspx",0);
  if (!$html) return;
  $items = mh_xpathDoc($html,"//table[@id='ctl00_ContentPlaceClient_gvMessages']//a");

  $query=array();
	foreach ($items as $item) {
    $title = $item->textContent;
    $title = "Съобщение: ".mh_cleanText($title);
    $url = "http://www.mh.government.bg/".$item->getAttribute("href");
    $hash = md5($url);
    $query[]=array($title,null,'now',$url,$hash);
    if (count($query)>=20)
       break;
  }

  echo "Възможни ".count($query)." нови съобщения\n";

  $itemids = saveItems($query);
  queueTweets($itemids);
}

function mh_Novini() {
  echo "> Проверявам за новини в МЗ\n";
  setSession(21,1);

  $html = loadURL("http://www.mh.government.bg/News.aspx?pageid=401",1);
  if (!$html) return;
  $items = mh_xpathDoc($html,"//table[@id='ctl00_ContentPlaceClient_ucNewsList_gvwNews']//tr[not(@class)]/td");

  $query=array();
	foreach ($items as $item) {
    $date = $item->childNodes->item(3)->textContent;
    $date = mh_cleanText($date); 
    $date = explode(".",$date);
    $date = substr($date[2],0,4)."-".$date[1]."-".$date[0];

    if (strtotime($date)<strtotime("-1 month"))
      continue;

    $title = $item->childNodes->item(1)->textContent;
    $title = mh_cleanText($title); 

    $description = $item->childNodes->item(5)->textContent;
    $description = mh_cleanText($description); 
    
    if (mb_strlen($title)<25)
      $title.=" ".$description;

    $url = $item->childNodes->item(1)->getAttribute("href");
    $urlstart = strpos($url,'News.aspx');
    $url = substr($url,$urlstart,strpos($url,'"',$urlstart)-$urlstart);
    $url = "http://www.mh.government.bg/$url";
    $hash = md5($url);

    $media=null;
    if ($item->childNodes->item(5)->childNodes->item(1)->nodeName=="input") {
      $imageurl = $item->childNodes->item(5)->childNodes->item(1)->getAttribute("src");
      $imageurl = "http://www.mh.government.bg/$imageurl";
      $imageurl=mb_ereg_replace("small","large",$imageurl,"im");
      $media = array("image" => array(loadItemImage($imageurl),null));
    }

    $query[]=array($title,$description,$date,$url,$hash,$media);
  }

  echo "Възможни ".count($query)." нови новини\n";
  
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function mh_Normativni() {
  echo "> Проверявам за нормативни актове в МЗ\n";
  setSession(21,2);

  $html = loadURL("http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=393",2);
  if (!$html) return;
  $items = mh_xpathDoc($html,"//table[@id='ctl00_ContentPlaceClient_ucArticlesList_gvArticles']//tr[not(@class)]/td");

  $query=array();
	foreach ($items as $item) {
    $date = $item->childNodes->item(3)->textContent;
    $date = mh_cleanText($date); 
    $date = explode(".",$date);
    $date = substr($date[2],0,4)."-".$date[1]."-".substr($date[0],-2);

    if (strtotime($date)<strtotime("-1 month"))
      continue;

    $title = $item->childNodes->item(1)->textContent;
    $title = mh_cleanText($title); 
    
    $url = $item->childNodes->item(1)->getAttribute("href");
    $urlstart = strpos($url,'Articles.aspx');
    $url = substr($url,$urlstart,strpos($url,'"',$urlstart)-$urlstart);
    $url = "http://www.mh.government.bg/$url";
    $hash = md5($url);

    $query[]=array($title,null,$date,$url,$hash);
  }

  echo "Възможни ".count($query)." нови нормативни актове\n";

  $itemids = saveItems($query);
  queueTweets($itemids);
}

function mh_Naredbi() {
  echo "> Проверявам за наредби в МЗ\n";
  setSession(21,3);

  $html = loadURL("http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=391",3);
  if (!$html) return;
  $items = mh_xpathDoc($html,"//table[@id='ctl00_ContentPlaceClient_ucArticlesList_gvArticles']//tr[not(@class)]/td/a[@class='list_article_title']");

  $query=array();
	foreach ($items as $item) {
    $title = $item->textContent;
    $title = mh_cleanText($title); 

    $url = $item->getAttribute("href");
    $urlstart = strpos($url,'Articles.aspx');
    $url = substr($url,$urlstart,strpos($url,'"',$urlstart)-$urlstart);
    $url = "http://www.mh.government.bg/$url";
    $hash = md5($url);

    $query[]=array($title,null,"now",$url,$hash);
  }

  echo "Възможни ".count($query)." нови наредби\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function mh_Postanovleniq() {
  echo "> Проверявам за постановления в МЗ\n";
  setSession(21,4);

  $html = loadURL("http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=381",4);
  if (!$html) return;
  $items = mh_xpathDoc($html,"//table[@id='ctl00_ContentPlaceClient_ucArticlesList_gvArticles']//tr[not(@class)]/td/a[@class='list_article_title']");

  $query=array();
	foreach ($items as $item) {
    $title = $item->textContent;
    $title = mh_cleanText($title); 

    $url = $item->getAttribute("href");
    $urlstart = strpos($url,'Articles.aspx');
    $url = substr($url,$urlstart,strpos($url,'"',$urlstart)-$urlstart);
    $url = "http://www.mh.government.bg/$url";
    $hash = md5($url);

    $query[]=array($title,null,"now",$url,$hash);
  }

  echo "Възможни ".count($query)." нови постановления\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function mh_Otcheti() {
  echo "> Проверявам за отчети в МЗ\n";
  setSession(21,5);

  $html = loadURL("http://www.mh.government.bg/Articles.aspx?lang=bg-BG&pageid=532",5);
  if (!$html) return;
  $items = mh_xpathDoc($html,"//table[@id='ctl00_ContentPlaceClient_ucArticlesList_gvArticles']//tr[not(@class)]/td/a[@class='list_article_title']");

  $query=array();
	foreach ($items as $item) {
    $title = $item->textContent;
    $title = mh_cleanText($title); 

    $url = $item->getAttribute("href");
    $urlstart = strpos($url,'Articles.aspx');
    $url = substr($url,$urlstart,strpos($url,'"',$urlstart)-$urlstart);
    $url = "http://www.mh.government.bg/$url";
    $hash = md5($url);

    $query[]=array($title,null,"now",$url,$hash);
  }

  echo "Възможни ".count($query)." нови отчети\n";
  $itemids = saveItems($query);
  queueTweets($itemids);
}

/*
-----------------------------------------------------------------
*/

function mh_xpathDoc($html,$q) {
  if (!$html)
    return array();
  $html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
  $doc = new DOMDocument("1.0", "UTF-8");
  $doc->preserveWhiteSpace=false;
  $doc->strictErrorChecking=false;
  $doc->encoding = 'UTF-8';
  $doc->loadHTML($html);
  $xpath = new DOMXpath($doc);  

  $items = $xpath->query($q);
  return is_null($items)?array():$items;
}

function mh_cleanText($text) {
	$text = html_entity_decode($text);
  $text = text_cleanSpaces($text);
  $text = text_fixCase($text);
	return $text;
}

?>
<file_sep><?php

/*

0: дневен ред http://www.vss.justice.bg/bg/schedule/1.htm
1: протоколи http://www.vss.justice.bg/bg/decisions/2014/1.htm
2: новини http://www.vss.justice.bg/bg/press/2014/2014.htm

*/

function vssDnevenRed() {
  echo "> Проверявам за дневен ред на ВСС\n";
  setSession(9,0);

  $baseurl = vssGetLink("Дневен ред");
  $html = loadURL($baseurl,0);
  if (!$html) return;
  $items = vss_xpathDoc($html,"//td//a[@class='link']");
  $baseurl=substr($baseurl,0,strrpos($baseurl,"/")+1);

  $query=array();
	foreach ($items as $item) {
    $date = trim($item->textContent);
    $date = "20".mb_substr($date,-2)."-".mb_substr($date,-5,2)."-".mb_substr($date,-8,2);
    if (strtotime($date)<strtotime("-1 week"))
      continue;

    $url = $baseurl.$item->getAttribute("href");
    $hash = md5($url);

    $html1 = loadURL($url);
    if (!$html1) continue;
    $html1 = mb_convert_encoding($html1, 'UTF-8', 'cp1251');
    mb_ereg_search_init($html1);
    mb_ereg_search(">\s+(\d+)\. ");
    $points="";
    while ($match = mb_ereg_search_regs())
      if (count($match)==2)
        $points=intval($match[1]);
    if ($points!="")
       $points="от $points точки ";

    $title = $item->textContent;
    $title = vss_cleanText($title);
    $title = mb_ereg_replace("№ ","№",$title,"im");
    $title = mb_ereg_replace(" "," на ",$title,"im");
    $title = "Публикуван е дневният ред ".$points."за заседание ".$title;

    $query[]=array($title,null,'now',$url,$hash);
  }
  echo "Възможни ".count($query)." нов запис за дневен ред\n";

  $itemids = saveItems($query);
  queueTweets($itemids);
}

function vssProtokol() {
  echo "> Проверявам за протоколи на ВСС\n";
  setSession(9,1);

  $baseurl = vssGetLink("Протоколи");
  $html = loadURL($baseurl,1);
  if (!$html) return;
  $items = vss_xpathDoc($html,"//td//a[@class='link']");

  $query=array();
	foreach ($items as $item) {
    $date = trim($item->textContent);
    $date = "20".mb_substr($date,-2)."-".mb_substr($date,-5,2)."-".mb_substr($date,-8,2);
    if (strtotime($date)<strtotime("-1 month"))
      continue;

    $title = $item->textContent;
    $title = vss_cleanText($title);
    $title = mb_ereg_replace("№ ","№",$title,"im");
    $title = mb_ereg_replace(" "," на ",$title,"im");
    $title = "Публикуван е протокол от заседание ".$title;

    $url = $baseurl.$item->getAttribute("href");
    $hash = md5($url);

    $query[]=array($title,null,'now',$url,$hash);
  }
  echo "Възможни ".count($query)." нови протоколи\n";
  $query=array_reverse($query);
  $itemids = saveItems($query);
  queueTweets($itemids);
}

function vssNovini() {
  echo "> Проверявам за новини на ВСС\n";
  setSession(9,2);

  $baseurl = "http://www.vss.justice.bg/bg/press/".date("Y")."/";
  $mainurl = $baseurl.date("Y").".htm";
  $html = loadURL($mainurl,2);
  if (!$html) return;
  $xpath = vss_xpath($html);
  if (!$xpath) return;
  $items = $xpath->query("//td/div");

  $query=array();
	foreach ($items as $item) {
    $date = trim($item->childNodes->item(1)->textContent);
    $date = vss_cleanText($date);
    $date = text_bgMonth($date);
    $date = mb_substr($date,6,4)."-".mb_substr($date,3,2)."-".mb_substr($date,0,2);
    if (strtotime($date)<strtotime("-1 week"))
      continue;

    $item->removeChild($item->childNodes->item(1));
    $title = $item->textContent;
    $title = mb_ereg_replace("“|„|”","",$title);
    $title = vss_cleanText($title);

    if (mb_strpos($title,'юлетин за дейността')!==false) {
      $links = $xpath->query(".//a",$item);
      if ($links->length>0) {
        $url = $baseurl.$links->item(0)->getAttribute("href");
        $hash = md5($url);
        $query[]=array($title,null,$date,$url,$hash);
        continue;
      }
    }

    $url = $mainurl;
    $hash = md5($url.$title);
    if (!checkHash($hash))
      continue;

    $description = trim($item->C14N());
    $description = mb_ereg_replace(" </","</",mb_ereg_replace("> ",">",$description));
    $description = mb_ereg_replace("\s?(title|name|style|class|id|face|align|img)=[\"'].*?[\"']\s?","",$description);
    $description = mb_ereg_replace("<p>[  ]*</p>|<a>[  ]*</a>|<div>[  ]*</div>","",$description);
    $description = text_cleanSpaces($description);
    $description = html_entity_decode($description);

    $media=array("image" => array());
    $imgs = $xpath->query(".//img[not(src)]",$item);
    if ($imgs->length>0) {
      $name=$imgs->item(0)->getAttribute("name");
      $name=str_replace("show","images",$name);
      if (mb_strpos($html,"$name=new Array(")!==false) {
          $medialiststart = mb_strpos($html,"$name=new Array(")+mb_strlen("$name=new Array(");
          $medialist=mb_substr($html,$medialiststart,mb_strpos($html,");",$medialiststart)-$medialiststart);
          $medialist=explode(",",str_replace(array('"',"'"),"",$medialist));
          foreach ($medialist as $mediafile) {
            $imageurl = loadItemImage($baseurl.$mediafile);
            if ($imageurl)
              $media["image"][] = array($imageurl);
          }
       }
    }
    if (count($media["image"])==0)
      $media=null;

    $query[]=array($title,$description,$date,$url,$hash,$media);
  }
  echo "Възможни ".count($query)." нови новини\n";

  $itemids = saveItems($query);
  queueTweets($itemids);
}


/*
-----------------------------------------------------------------
*/

function vssGetLink($which) {
  $html = loadURL("http://www.vss.justice.bg/bg/sessions.htm");
  $html = mb_convert_encoding($html, 'UTF-8', 'cp1251');
  $pos = mb_strpos($html,$which."|")+mb_strlen($which)+1;
  return "http://www.vss.justice.bg/bg/".mb_substr($html,$pos,mb_strpos($html,"\"",$pos)-$pos);
}

function vss_xpath($html) {
  if (!$html)
    return null;
  $html = mb_convert_encoding($html, 'UTF-8', 'cp1251');
  $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
  $doc = new DOMDocument("1.0", "UTF-8");
  $doc->preserveWhiteSpace=false;
  $doc->strictErrorChecking=false;
  $doc->encoding = 'UTF-8';
  $doc->loadHTML($html);
  return new DOMXpath($doc);  
}

function vss_xpathDoc($html,$q) {
  $xpath =  vss_xpath($html);  
  if ($xpath==null)
    return array();
  $items = $xpath->query($q);
  return is_null($items)?array():$items;
}


function vss_cleanText($text) {
  $text = text_cleanSpaces($text);
	$text = html_entity_decode($text);
	return $text;
}

?>
