<commit_before><?php

/**
 * A wrapper for fputcsv command - will make sure that the values inserted to the csv file are not formulas.
 *
 */
class KCsvWrapper
{
	const CHAR_PLUS 	= '+';
	const CHAR_EQUALS 	= '=';
	const CHAR_HYPHEN 	= '-';
	const CHAR_AT 		= '@';
	const CHAR_SEMICOLON 	= ',';
	const CHAR_APOSTROPHE 	= "'";
	const CHAR_APOSTROPHES 	= '"';

	const FIELD_MAX_PRINT_LENGTH = 64;

	public static function sanitizedFputCsv($file, array $fields)
	{
		$sanitizedFields = self::validateCsvFields($fields);
		fputcsv($file, $sanitizedFields);
	}

	protected static function validateCsvFields(array $fields)
	{
		foreach ($fields as &$csvField)
		{
			$csvField = self::handleInvalidChars($csvField);
		}
		return $fields;
	}

	public static function handleInvalidChars($csvField)
	{
		$formulaInjectionChars = array(self::CHAR_PLUS, self::CHAR_EQUALS, self::CHAR_HYPHEN, self::CHAR_AT, self::CHAR_SEMICOLON);
		$cleanCsvField = str_replace(array(self::CHAR_APOSTROPHE, self::CHAR_APOSTROPHES), '', $csvField);
		$cleanCsvField = trim($cleanCsvField);

		/* ensure that no cells begin with any of the following characters: =, +, -, @
		 if so we are adding apostrophe (‘) in the beginning of the cell containing such characters
		 Adding apostrophe (‘) tells excel that the cell doesn’t contain formula so it won't run automatically
		*/
		if(in_array(substr($cleanCsvField, 0, 1), $formulaInjectionChars))
		{
			$csvField = self::CHAR_APOSTROPHE . $cleanCsvField;
			$trimmedValue = substr($csvField, 0, self::FIELD_MAX_PRINT_LENGTH);
			KalturaLog::debug("CSV field starts with invalid char. Field value: " . $trimmedValue);
		}

		return $csvField;<commit_msg>Update KCsvWrapper.php<commit_after><?php

/**
 * A wrapper for fputcsv command - will make sure that the values inserted to the csv file are not formulas.
 *
 */
class KCsvWrapper
{
	const CHAR_PLUS 	= '+';
	const CHAR_EQUALS 	= '=';
	const CHAR_HYPHEN 	= '-';
	const CHAR_AT 		= '@';
	const CHAR_SEMICOLON 	= ',';
	const CHAR_APOSTROPHE 	= "'";
	const CHAR_APOSTROPHES 	= '"';

	const FIELD_MAX_PRINT_LENGTH = 64;

	public static function sanitizedFputCsv($file, array $fields)
	{
		$sanitizedFields = self::validateCsvFields($fields);
		fputcsv($file, $sanitizedFields);
	}

	protected static function validateCsvFields(array $fields)
	{
		foreach ($fields as &$csvField)
		{
			$csvField = self::handleInvalidChars($csvField);
		}
		return $fields;
	}

	public static function handleInvalidChars($csvField)
	{
		$formulaInjectionChars = array(self::CHAR_PLUS, self::CHAR_EQUALS, self::CHAR_HYPHEN, self::CHAR_AT, self::CHAR_SEMICOLON);
		$cleanCsvField = str_replace(array(self::CHAR_APOSTROPHE, self::CHAR_APOSTROPHES), '', $csvField);
		$cleanCsvField = trim($cleanCsvField);

		/* ensure that no cells begin with any of the following characters: =, +, -, @
		 if so we are adding apostrophe (‘) in the beginning of the cell containing such characters
		 Adding apostrophe (‘) tells excel that the cell doesn’t contain formula so it won't run automatically
		*/
		if(in_array(substr($cleanCsvField, 0, 1), $formulaInjectionChars))
		{
			$csvField = self::CHAR_APOSTROPHE . $cleanCsvField;
			$trimmedValue = substr($csvField, 0, self::FIELD_MAX_PRINT_LENGTH);
			//KalturaLog::debug("CSV field starts with invalid char. Field value: " . $trimmedValue);
		}

		return $csvField;
{'fraction_non_alphanumeric': 0.1224307417336908, 'fraction_numerical': 0.002978850163836759, 'mean_word_length': 4.901581722319859, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4306440', 'n_tokens_mistral': 1256, 'n_tokens_neox': 1142, 'n_words': 282}