<?php
/**
 * File permission detector.
 *
 * @package LocalGuardScanner
 */

namespace LocalGuardScanner\Detection\File;

use LocalGuardScanner\Domain\Detection\Detector;
use LocalGuardScanner\Domain\Detection\Evidence;
use LocalGuardScanner\Domain\Detection\ScannableObject;

/**
 * Reports world-writable files as a low-risk hardening signal.
 */
final class PermissionDetector implements Detector {
	/** @return string */
	public function id() {
		return 'file.permissions';
	}

	/** @inheritDoc */
	public function inspect( ScannableObject $scanned_object ) {
		$attributes = $scanned_object->attributes();
		$mode       = isset( $attributes['mode'] ) ? (int) $attributes['mode'] : 0;
		if ( 'file' === $scanned_object->type() && 0 !== ( $mode & 0002 ) ) {
			return array( new Evidence( 'file.world_writable', 20, 95, 'world_writable_file', decoct( $mode & 0777 ) ) );
		}
		return array();
	}
}
