<?php

try {
	$file_name = end($argv);

	if (realpath($file_name) == __FILE__) {
		throw new Exception("Empty file argument");
	}

	if (!file_exists($file_name)) {
		throw new Exception("File $file_name not found");
	}

	if (is_dir($file_name)) {
		throw new Exception("$file_name is not a file");
	}

	$file_size = filesize($file_path);

	if (empty($file_size)) {
		throw new Exception("$file_name is empty");	
	}

	$file = @fopen($file_name, "r");

	if ($file === false) {
		throw new Exception("Can not open file $file_name");
	}

	while (($str = fgets($file, 4096)) !== false) {
        $str = trim($str);

        if (strlen($str) > 0) {
        	$buffer[$str]++;
        }
    }

    fclose($file);
    arsort($buffer);

    foreach ($buffer as $str => $f) {
    	if ($f < 2) {
    		break;
    	}

    	print_r("$str $f\n");
    	$has_strs = true;
    }

    if (!$has_strs) {
    	print_r("Strings not found\n");
    }
} catch (Exception $e) {
	$error = "Error: ".$e->getMessage()."\n";
	print_r($error);
}
?>