<commit_before><?php
/*
Copyright 2016 <NAME>
(https://daniil.it)

This file is part of the PWRTelegram API.
the PWRTelegram API is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 
The PWRTelegram API is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details. 
You should have received a copy of the GNU General Public License along with the PWRTelegram API. 
If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * Returns the size of a file without downloading it, or -1 if the file
 * size could not be determined.
 *
 * @param $url - The location of the remote file to download. Cannot
 * be null or empty.
 *
 * @return The size of the file referenced by $url, or -1 if the size
 * could not be determined.
 */
 
function curl_get_file_size( $url ) {
  // Assume failure.
  $result = -1;

  $curl = curl_init( $url );

  // Issue a HEAD request and follow any redirects.
  curl_setopt( $curl, CURLOPT_NOBODY, true );
  curl_setopt( $curl, CURLOPT_HEADER, true );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );

  $data = curl_exec( $curl );
  curl_close( $curl );

  if( $data ) {
    $content_length = "unknown";
    $status = "unknown";

    if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
      $status = (int)$matches[1];
    }

    if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
      $content_length = (int)$matches[1];
    }

    // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if( $status == 200 || ($status > 300 && $status <= 308) ) {
      $result = $content_length;
    }
  }

  return $result;
}


function checkbotuser($me) {
	include 'telegram_connect.php';
	global $pwrtelegram_api, $token, $url, $pwrtelegram_storage;
	$me = curl($url . "/getMe")["result"]["username"]; // get my username

	$usernames = array();
	foreach ($telegram->getDialogList() as $username){ $usernames[] = $username->username; };
	if(!in_array($me, $usernames)) { // If never contacted bot send start command
		if(!$telegram->msg("@".$me, "/start")) return false;
	}
	return true;
}

function checkdir($dir) {
	if (!file_exists($dir)) {
		if(!mkdir($dir, 0777, true)) return false;
	}
	return true;
}

function unlink_link($path) {
	$rpath = readlink($path);
	unlink($path);
	unlink($rpath);
}

function download($file_id) {
	include '../db_connect.php';
	global $pwrtelegram_api, $token, $url, $pwrtelegram_storage, $homedir, $methods;
	$me = curl($url . "/getMe")["result"]["username"]; // get my username
	$selectstmt = $pdo->prepare("SELECT * FROM dl WHERE file_id=? AND bot=? LIMIT 1;");
	$selectstmt->execute(array($file_id, $me));
	$select = $selectstmt->fetch(PDO::FETCH_ASSOC);

	if($selectstmt->rowCount() == "1" && checkurl($pwrtelegram_storage . $select["file_path"])) {
		$newresponse["ok"] = true;
		$newresponse["result"]["file_id"] = $select["file_id"];
		$newresponse["result"]["file_path"] = $select["file_path"];
		$newresponse["result"]["file_size"] = $select["file_size"];
		return $newresponse;
	}

	include 'telegram_connect.php';
	$gmethods = array_keys($methods);

	if(!checkbotuser($me)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't initiate chat.");

	$count = 0;
	while($result["ok"] != true && $count < count($gmethods)) {
		$result = curl($url . "/send" . $gmethods["$count"] . "?chat_id=" . $botusername . "&" . $gmethods["$count"] . "=" . $file_id);
		$count++;
	};
	$count--;

	if($result["ok"] == false) return array("ok" => false, "error_code" => 400, "description" => "Couldn't forward file to download user.");
	$result = curl($url . "/sendMessage?reply_to_message_id=" . $result["result"]["message_id"] . "&chat_id=" . $botusername . "&text=" . $file_id);
	if($result["ok"] == false) return array("ok" => false, "error_code" => 400, "description" => "Couldn't send file id.");
	$result = $telegram->getFile($me, $file_id, $methods[$gmethods[$count]]);
	$path = $result->{"result"};
	if($path == "") return array("ok" => false, "error_code" => 400, "description" => "Couldn't download file.");
	try {
		$mediaInfo = new Mhor\MediaInfo\MediaInfo();
		$mediaInfoContainer = $mediaInfo->getInfo($path);
		$general = $mediaInfoContainer->getGeneral();
		if($general->get("file_extension") == null) {
			$newpath = $path . "." . preg_replace("/.*\s/", "", $general->get("format_extensions_usually_used"));
			if(!rename($path, $newpath)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't append file extension.");
			$path = $newpath;
		}
	} catch(Exception $e) { ; };
	if(!checkdir($homedir . "/storage/" . $me)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't create storage directory.");
	$file_path = $me . preg_replace('/.*\.telegram-cli\/downloads/', '', $path);
	unlink($homedir . "/storage/" . $file_path);
	if(!symlink($path, $homedir . "/storage/" . $file_path)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't symlink file to storage.");
	if(!chmod($homedir . "/storage/" . $file_path, 0755)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't chmod file.");
	$file_size = filesize($homedir . "/storage/" . $file_path);
	$newresponse["ok"] = true;
	$newresponse["result"]["file_id"] = $file_id;
	$newresponse["result"]["file_path"] = $file_path;
	$newresponse["result"]["file_size"] = $file_size;
	$delete_stmt = $pdo->prepare("DELETE FROM dl WHERE file_id=? AND bot=?;");
	$delete = $delete_stmt->execute(array($file_id, $me));
	$insert_stmt = $pdo->prepare("INSERT INTO dl (file_id, file_path, file_size, bot) VALUES (?, ?, ?, ?);");
	$insert = $insert_stmt->execute(array($file_id, $file_path, $file_size, $me));
	return $newresponse;
}

function get_finfo($file_id){
	global $pwrtelegram_api, $token, $url, $pwrtelegram_storage;
	include 'telegram_connect.php';
	global $methods;
	$methods = array_keys($methods);
	$me = curl($url . "/getMe")["result"]["username"]; // get my username

	if(!checkbotuser($me)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't initiate chat.");

	$count = 0;
	while($result["ok"] != true && $count < count($methods)) {
		$result = curl($url . "/send" . $methods["$count"] . "?chat_id=" . $botusername . "&" . $methods["$count"] . "=" . $file_id);
		$count++;
	};
	$count--;
	$info["ok"] = $result["ok"];
	if($info["ok"] == true) {
		$info["type"] = $methods[$count];
		if($info["type"] == "photo") $info["file_size"] = $result["result"][$methods[$count]][0]["file_size"]; else $info["file_size"] = $result["result"][$methods[$count]]["file_size"];
	}
	return $info;
}

function upload($file, $name = "", $type = "", $detect = false, $forcename = false) {
	if($file == "") return array("ok" => false, "error_code" => 400, "description" => "No file specified.");
	include '../db_connect.php';
	global $pwrtelegram_api, $token, $url, $pwrtelegram_storage;
	include 'beta.php';
	include 'telegram_connect.php';
	global $methods, $homedir;
	$me = curl($url . "/getMe")["result"]["username"]; // get my username<commit_msg>Commenting code...<commit_after><?php
/*
Copyright 2016 <NAME>
(https://daniil.it)

This file is part of the PWRTelegram API.
the PWRTelegram API is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 
The PWRTelegram API is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details. 
You should have received a copy of the GNU General Public License along with the PWRTelegram API. 
If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * Returns the size of a file without downloading it, or -1 if the file
 * size could not be determined.
 *
 * @param $url - The location of the remote file to download. Cannot
 * be null or empty.
 *
 * @return The size of the file referenced by $url, or -1 if the size
 * could not be determined.
 */
 
function curl_get_file_size( $url ) {
  // Assume failure.
  $result = -1;

  $curl = curl_init(str_replace(' ', '%20', $url));

  // Issue a HEAD request and follow any redirects.
  curl_setopt( $curl, CURLOPT_NOBODY, true );
  curl_setopt( $curl, CURLOPT_HEADER, true );
  curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );

  $data = curl_exec( $curl );
  curl_close( $curl );

  if( $data ) {
    $content_length = "unknown";
    $status = "unknown";

    if( preg_match( "/^HTTP\/1\.[01] (\d\d\d)/", $data, $matches ) ) {
      $status = (int)$matches[1];
    }

    if( preg_match( "/Content-Length: (\d+)/", $data, $matches ) ) {
      $content_length = (int)$matches[1];
    }

    // http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    if( $status == 200 || ($status > 300 && $status <= 308) ) {
      $result = $content_length;
    }
  }

  return $result;
}

/**
 * Check if tg-cli ever contacted contacted username, if not send a /start command
 *
 * @param $me - The username to check
 *
 * @return true if user is in dialoglist or if it was contacted successfully, false if it couldn't be contacted.
 */
function checkbotuser($me) {
	include 'telegram_connect.php';
	global $url;
	// $me is the bot's username
	$me = curl($url . "/getMe")["result"]["username"]; // get my username
	// Get all of the usernames
	$usernames = array();
	ini_set("log_errors", 0);
	foreach ($telegram->getDialogList() as $username){ $usernames[] = $username->username; };
	ini_set("log_errors", 1);
	// If never contacted bot send start command
	if(!in_array($me, $usernames)) {
		if(!$telegram->msg("@".$me, "/start")) return false;
	}
	return true;
}


/**
 * Check dir exists
 *
 * @param $dir - The dir to check
 *
 * @return true if dir exists or if it was created successfully, false if it couldn't be created.
 */
function checkdir($dir) {
	if (!file_exists($dir)) {
		if(!mkdir($dir, 0777, true)) return false;
	}
	return true;
}

/**
 * Remove symlink and destination path
 *
 * @param $symlink - The symlink to delete
 *
 * @return void
 */
function unlink_link($symlink) {
	$rpath = readlink($symlink);
	unlink($symlink);
	unlink($rpath);
}

/**
 * Download given file id and return json with error or downloaded path
 *
 * @param $file_id - The file id of the file to download
 *
 * @return json with error or file path
 */
function download($file_id) {
	include '../db_connect.php';
	global $url, $pwrtelegram_storage, $homedir, $methods;
	$me = curl($url . "/getMe")["result"]["username"]; // get my username
	$selectstmt = $pdo->prepare("SELECT * FROM dl WHERE file_id=? AND bot=? LIMIT 1;");
	$selectstmt->execute(array($file_id, $me));
	$select = $selectstmt->fetch(PDO::FETCH_ASSOC);

	if($selectstmt->rowCount() == "1" && checkurl($pwrtelegram_storage . $select["file_path"])) {
		$newresponse["ok"] = true;
		$newresponse["result"]["file_id"] = $select["file_id"];
		$newresponse["result"]["file_path"] = $select["file_path"];
		$newresponse["result"]["file_size"] = $select["file_size"];
		return $newresponse;
	}

	include 'telegram_connect.php';
	$gmethods = array_keys($methods);

	if(!checkbotuser($me)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't initiate chat.");

	$count = 0;
	while($result["ok"] != true && $count < count($gmethods)) {
		$result = curl($url . "/send" . $gmethods["$count"] . "?chat_id=" . $botusername . "&" . $gmethods["$count"] . "=" . $file_id);
		$count++;
	};
	$count--;
	if($result["ok"] == false) return array("ok" => false, "error_code" => 400, "description" => "Couldn't forward file to download user.");
	if($result["result"]["message_id"] == false) return array("ok" => false, "error_code" => 400, "description" => "Reply message id is empty.");

	$result = curl($url . "/sendMessage?reply_to_message_id=" . $result["result"]["message_id"] . "&chat_id=" . $botusername . "&text=" . $file_id);
	if($result["ok"] == false) return array("ok" => false, "error_code" => 400, "description" => "Couldn't send file id.");

	$result = $telegram->getFile($me, $file_id, $methods[$gmethods[$count]]);
	$path = $result->{"result"};
	if($path == "") return array("ok" => false, "error_code" => 400, "description" => "Couldn't download file.");
	if(!file_exists($path)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't download file (file does not exist).");
	if(!checkdir($homedir . "/storage/" . $me)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't create storage directory.");
	try {
		$mediaInfo = new Mhor\MediaInfo\MediaInfo();
		$mediaInfoContainer = $mediaInfo->getInfo($path);
		$general = $mediaInfoContainer->getGeneral();
		$ext = "";
		try {
			$ext = $general->get("file_extension");
		} catch(Exception $e) { ; };
		try {
			$format = preg_replace("/.*\s/", "", $general->get("format_extensions_usually_used"));
		} catch(Exception $e) { ; };
		try {
			$codec = preg_replace("/.*\s/", "", $general->get("codec_extensions_usually_used"));
		} catch(Exception $e) { ; };
		if($format == "") $format = $codec;
	} catch(Exception $e) { ; };

	$file_path = $me . preg_replace('/.*\.telegram-cli\/downloads/', '', $path);
	if($ext == "" && $format != "") {
		$file_path = $file_path . "." . $format;
	}

	unlink($homedir . "/storage/" . $file_path);
	if(!symlink($path, $homedir . "/storage/" . $file_path)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't symlink file to storage.");
	if(!chmod($homedir . "/storage/" . $file_path, 0755)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't chmod file.");

	$file_size = filesize($homedir . "/storage/" . $file_path);

	$newresponse["ok"] = true;
	$newresponse["result"]["file_id"] = $file_id;
	$newresponse["result"]["file_path"] = $file_path;
	$newresponse["result"]["file_size"] = $file_size;

	$delete_stmt = $pdo->prepare("DELETE FROM dl WHERE file_id=? AND bot=?;");
	$delete = $delete_stmt->execute(array($file_id, $me));
	$insert_stmt = $pdo->prepare("INSERT INTO dl (file_id, file_path, file_size, bot) VALUES (?, ?, ?, ?);");
	$insert = $insert_stmt->execute(array($file_id, $file_path, $file_size, $me));

	return $newresponse;
}

// Returninfo from file id
function get_finfo($file_id){
	global $methods, $url;
	$methods = array_keys($methods);
	$me = curl($url . "/getMe")["result"]["username"]; // get my username

	if(!checkbotuser($me)) return array("ok" => false, "error_code" => 400, "description" => "Couldn't initiate chat.");

	$count = 0;
	while($result["ok"] != true && $count < count($methods)) {
		$result = curl($url . "/send" . $methods["$count"] . "?chat_id=" . $botusername . "&" . $methods["$count"] . "=" . $file_id);
		$count++;
	};
	$count--;
	$info["ok"] = $result["ok"];
	if($info["ok"] == true) {
		$info["type"] = $methods[$count];
		if($info["type"] == "photo") $info["file_size"] = $result["result"][$methods[$count]][0]["file_size"]; else $info["file_size"] = $result["result"][$methods[$count]]["file_size"];
	}
	return $info;
}


function upload($file, $name = "", $type = "", $detect = false, $forcename = false) {
	if($file == "") return array("ok" => false, "error_code" => 400, "description" => "No file specified.");
	include '../db_connect.php';
	global $pwrtelegram_api, $token, $url, $pwrtelegram_storage;
	include 'beta.php';
	include 'telegram_connect.php';
	global $methods, $homedir;
	$me = curl($url . "/getMe")["result"]["username"]; // get my username
{'fraction_non_alphanumeric': 0.19843847595252967, 'fraction_numerical': 0.008557151780137414, 'mean_word_length': 4.567107093184979, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 107, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2521715', 'n_tokens_mistral': 5692, 'n_tokens_neox': 5450, 'n_words': 1423}