<?php

if (PH_SAPI !== 'cli')
	die();

//downloadClintonEmails();
//dwnloadWikileaksVault7();

function downloadClintonEmails()
{
for ($i = 1; $i <= 30322; $i++)
	file_put_contents("D:/intuview/apps/docs/wikileaks_clinton/$i.htm", fopen("https://www.wikileaks.org/clinton-emails/emailid/$i?embed=1", "r"));
}


//used 1 time
function extractCzechNewsJson()
{
	foreach (file("U:/Aziz/Customers/Czech/news.json") as $line)
	{
		file_put_contents("U:/Aziz/Customers/Czech/" . ++$ind . ".txt", "﻿" . json_decode($line)->text);
	}
}

function dwnloadWikileaksVault7()
{
	$content = file_get_contents("https://wikileaks.org/ciav7p1/cms/index.html");
	$xml = new DOMDocument();
	$xml->loadHTML($content);
	$xpath = new DOMXPath($xml);
	$nodes = $xpath->query("//a/@href");
	echo "numer of anchors: " . ($nodes? $nodes->length : "klum");
	$all = false;
	$fp = fopen("php://stdin","r");
	foreach ($nodes as $node)
	{
		$pageName = $node->value;
		if (!strpos($pageName, ".html") || strpos($pageName, "http") === 0 || strpos($pageName, ".") === 0)
			continue;
		
		$pageContent = file_get_contents("https://wikileaks.org/ciav7p1/cms/$pageName");
		file_put_contents("U:\\Aziz\\wikileaks\\vault7\\$pageName", $pageContent);
		$navStart = strpos($pageContent, "<p>Navigation:");
		$navEnd = strpos($pageContent, "</p>", $navStart);
		$navigation = "";
		for ($navPos = $navStart; $navPos < $navEnd; $navPos = strpos($pageContent, ">", $navStart) + 1)
		{
			$navStart = strpos($pageContent, "<", $navPos);
			$navigation .= substr($pageContent, $navPos, $navStart - $navPos);
		}
		echo "\r\nNavigation of $pageName: $navigation";
		if (strpos($navigation, "\n") === FALSE)
		{
			$navs = explode("»", $navigation);
			array_shift($navs); //remove "Navigation:"
			$navigation = "";
			foreach ($navs as $nav)
				$navigation .= trim($nav) . "/";
			echo "\r\nFolder: $navigation";
			mkdir("U:\\Aziz\\wikileaks\\vault7\\$navigation", 0777, true);
			file_put_contents("U:\\Aziz\\wikileaks\\vault7\\$navigation$pageName", $pageContent);
		}
		if (!$all)
		{
			echo "Press 'a' for downloading all documents, 'q' to quit, otherwise - next doc:";
			$answer = fgetc($fp);
			$all = $answer == 'a';
			if ($answer == 'q')
				die();
		}
	}
}

?>