<?php
######################################################################
# ZW3B.Site v7.1.2 : The Web Sites Management System
# --------------------------------------------------------------------
#
# Copyright (c) 2022 by LAB3W : O.Romain Jaillet-ramey - (orj+php+api@lab3w.fr)
#
# Date Create : 2022/02/25
# Date Modify : 2022/11/11
#
# Web Domain : lab3w.fr
# Web Domain : lab3w.com
#
# Web Domain : zw3b.fr
# Web Domain : zw3b.tv
# Web Domain : zw3b.site
# Web Domain : zw3b.net
# Web Domain : zw3b.blog
#
# Web Domain : ipv01.net
# Web Domain : ipv10.net
#
# This module is to manage ---------------------------- :: Descripton
#
# This program is free software. You can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License.
######################################################################

//----------------------------------------------------------------
// INI Config

ini_set('track_errors','on');
ini_set('display_errors','on');
ini_set('default_charset''UTF-8');

// INI Config
//----------------------------------------------------------------

//----------------------------------------------------------------
// Caching system

# On cache
#header("Cache-Control: max-age=300");
#header("Pragma: cache, max-age=300, max-stale=600");

# No cache
header("Cache-control: private, no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");

// Caching system
//----------------------------------------------------------------

//----------------------------------------------------------------
// Header Security

header('X-Frame-Options: deny'); // allows not to load this page from another site via an iframe

// Header Security
//----------------------------------------------------------------

//----------------------------------------------------------------
// Library zw3bApiClient

include '/var/pro/web_apps/zw3bApiClient/7.1.2/zw3bApiClient.class.php';

// Library zw3bApiClient
//----------------------------------------------------------------


//-------------------------------------------------------------------------
// CONFIGURATION

$config = array();
//$config = null;
/*
// propriétaire des ressources (utilisateur final)
$config['api_name'] = 'ZW3B';
$config['api_key'] = null;


// Avec ce client (par exemple : une machine, un site Web)
// -> on demande au propri�taire d'une/des ressources "sur le serveur d'autorisation" s'il est autoris� � acceder � une ressource.
$config['client_id'] = null; //'1000-ZW3B-zw3b_api.json'; // dans ce fichier - les informations du client - origins - redirection...
//$config['client_secret'] = 'dJKJqlfqlkf3563q256kf89qfk33Aklj';
$config['client_secret'] = null;

$config['client_project']['name'] = 'ZW3B-BLOG';
*/
//-------------------------------------------------------------------------
// BETA-TESTEURS (Wilcard)

$config['api_name'] = 'BETA-TESTERS';
$config['client_project']['name'] = 'ZW3B-API-BETA-TESTERS';

//-------------------------------------------------------------------------
// sur la configuration du serveur d'autorisation
// origins javascript autorisées
//$config['client_project']['origins'] = array('https://wwdsdssds.zw3b.site');
// redirection autorisées
//$config['client_project']['redirection_url'] = array('https://api-dev.zw3b.fr', 'https://wwd.zw3b.site');

// CONFIGURATION
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
// APPEL DE L'API CLIENTe

$zw3bApiClient = new zw3bApiClient();

$my_client $zw3bApiClient->ClientModUsr($config);
//$my_client = $my_client->authentification();

// APPEL DE L'API CLIENTe
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
// APPEL DES SCOPES - FONCTIONS

/*
$client = $zw3bApiClient->ClientAuthorization(
 'https://www.zw3b.fr/api/reports/dmarc/analysis',
 //'https://www.zw3b.fr/api/server/domain/list',
 'https://www.zw3b.fr/api/contents/docs/list'
 //'https://www.zw3b.fr/api/contents/videos/list'
 );
*/

// APPEL DES SCOPES - FONCTIONS
//-------------------------------------------------------------------------


//echo 'CLIENT REPORTS : <pre>'.print_r(($my_client),1).'</pre>';
//echo 'CONTENTS<pre>'.print_r(json_decode($docs),1).'</pre>';
//echo 'DMARC<pre>'.print_r(json_decode($dmarc),1).'</pre>';

//-------------------------------------------------------------------------

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";
echo 
'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
echo 
'<head>'."\n";
echo 
'<title>ZW3B :-: API Client * Reports * DMARC * Analysis</title>'."\n";
echo 
'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'."\n";
echo 
'<meta http-equiv="cache-control" content="no-cache"/>'."\n";
echo 
'<meta http-equiv="Pragma" content="no-cache"/>'."\n\n";

echo 
'<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'."\n";
//echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" integrity="sha512-uto9mlQzrs59VwILcLiRYeLKPPbS/bT71da/OEBYEwcdNUk8jYIy+D176RYoop1Da+f9mvkYrmj5MCLZWEtQuA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'."\n";
echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js" integrity="sha512-qzgd5cYSZcosqpzpn7zF2ZId8f/8CHmFKZ8j7mU4OUXTNRd5g+ZHBPsgKEwoqxCtdQvExE5LprwwPAgoicguNg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'."\n\n";

echo 
'<script type="text/javascript" src="https://www.zw3b.site/sys/modules/reports/dmarc/skins/zw3b-site/js/dmarc-analysis-sortable.js"></script>'."\n";
echo 
'<link rel="stylesheet" type="text/css" href="https://www.zw3b.site/sys/modules/reports/dmarc/skins/zw3b-site/css/dmarc-analysis.css" />'."\n\n";

echo 
'<link rel="shortcut icon" href="https://www.zw3b.site/skins/zw3b-site/css/img/zw3b-logo-20x20.png" />'."\n\n";

echo 
'</head>'."\n";

echo 
'<body>'."\n";

echo 
'<h1><a href="https://www.zw3b.fr">ZW3B</a> :-: <a href="https://api.zw3b.fr">API Client</a> * Reports * <a href="https://www.zw3b.site/dmarc/">DMARC</a> * <a href="https://www.zw3b.site/dmarc/analysis">Analysis</a> by LAB3W.<acronym title="O.Romain.Jaillet-ramey" lang="FR">ORJ</acronym></h1>'."\n";

//-------------------------------------------------------------------------
// Infos Client

$my_client = ($my_client);
if(isset(
$my_client) && isset($my_client->authentification->client) && is_object($my_client->authentification->client))
{
    if(isset(
$my_client->authentification->client->name))
        echo 
'<p>Name : '.$my_client->authentification->client->name.'</p>'."\n";
        
        if(isset(
$my_client->authentification->project) && is_object($my_client->authentification->project) && isset($my_client->authentification->project->name))
            echo 
'<p>Project name : '.$my_client->authentification->project->name.'</p>'."\n";
}


if(isset(
$my_client->authentification->message)) 
{
    echo 
'<p>';
    echo 
''.$my_client->authentification->message.' - ';
    if(isset(
$my_client->authentification->reason) && is_object($my_client->authentification->reason) && isset($my_client->authentification->reason->status))
        echo 
'<span>'.$my_client->authentification->reason->status.' - '.$my_client->authentification->reason->text.'</span>';
    echo 
'</p>'."\n";
}

// Infos Client
//-------------------------------------------------------------------------

//-------------------------------------------------------------------------
// Reports / DMARC / Analysis

$reports = new zw3bApiClientModReports($my_client);

$analysis $reports->dmarc()->analysis();

$read_analysis json_decode($analysis);

//echo 'DMARC<pre>'.print_r($read_analysis,1).'</pre>';

if(isset($read_analysis->dmarc->servers_error) && is_array($read_analysis->dmarc->servers_error)):

# Hide SPF (valid) rows in HTML table
define('HIDE_MY_SPF_LINE_IN_TABLE'true);

echo 
'<h2>DMARC ANALYSIS</h2>'."\n";

//-----------------------------------------
echo '<h3>Mail server : <a href="https://mxtoolbox.com/SuperTool.aspx?action=mx:'.($read_analysis->dmarc->infos->mailserver).'&run=toolpage" title="Check my mail server" target="_blank">'.$read_analysis->dmarc->infos->mailserver.'</a></h3>'."\n";
echo 
'<blockquote>';

echo 
'Mail server <acronym title="Sender Policy Framework" lang="EN">SPF</acronym> authorized : '."\n";

foreach(
$read_analysis->dmarc->infos->spf as $k => $my_spf):
echo 
'<code>'.$my_spf.'</code> '.(count($read_analysis->dmarc->infos->spf)-$k ' | ' '')."\n";
endforeach;

echo 
'</blockquote>'."\n";

echo 
'<p>Period : </p>'."\n";
echo 
'<ul>'."\n";
echo 
'<li>Date since : '.$read_analysis->dmarc->infos->date->since->year.'/'.$read_analysis->dmarc->infos->date->since->month.'</li>'."\n";
echo 
'<li>Date until : '.$read_analysis->dmarc->infos->date->until->year.'/'.$read_analysis->dmarc->infos->date->until->month.'</li>'."\n";
echo 
'</ul>'."\n";

echo 
'<h3>List of spoofing mail servers ('.$read_analysis->dmarc->count->servers_error.')</h3>'."\n";

echo 
'<table id="global_stats_spoofers" class="rapport sortable">'."\n";
echo 
'<thead>'."\n";
echo 
'<tr class="label">'."\n";
echo 
'<th>Conformance</th>'."\n";
echo 
'<th>Address IP</th>'."\n";
echo 
'<th>Emails Volume</th>'."\n";
echo 
'<th>Mail Server Spoofing</th>'."\n";
echo 
'<th>ASN</th>'."\n";
echo 
'<th>Last Date</th>'."\n";
echo 
'</tr>'."\n";
echo 
'</thead>'."\n";
echo 
'<tbody>'."\n";

$count_my_spf 0;
$count_my_error_mail 0;

foreach(
$read_analysis->dmarc->servers_error as $item):

$req_if_hide_my_spf constant('HIDE_MY_SPF_LINE_IN_TABLE') === true ? !in_array($item->address_ip$read_analysis->dmarc->infos->spf) : true;

$get_class = !in_array($item->address_ip$read_analysis->dmarc->infos->spf) ? 'error ip' 'error';

$count_my_spf += constant('HIDE_MY_SPF_LINE_IN_TABLE') === true && in_array($item->address_ip$read_analysis->dmarc->infos->spf) ? 0;
$count_my_error_mail += in_array($item->address_ip$read_analysis->dmarc->infos->spf) ? (constant('HIDE_MY_SPF_LINE_IN_TABLE') === true $item->count_mails_error 0) : 0;

if(
$req_if_hide_my_spf)
{

    echo 
'<tr class="data '.$get_class.'">'."\n";
    echo 
'<td class="auth">'.$item->auth.'</td>'."\n";
    echo 
'<td class="addr_ip"><a href="https://dnslytics.com/'.(preg_match('/(:)/'$item->address_ip$m) ? 'ipv6' 'ip').'/'.$item->address_ip.'" onclick="window.open(this.href); return false;">'.$item->address_ip.'</a></td>'."\n";
    echo 
'<td class="count">'.number_format($item->count_mails_error).'</td>'."\n";
    echo 
'<td class="nameserver"><a href="https://mxtoolbox.com/SuperTool.aspx?action=mx:'.($item->fqdn).'&run=toolpage" title="Check this mail server" target="_blank">'.$item->fqdn.'</a></td>'."\n";
    echo 
'<td class="asn">';
    
$code3 geoip_country_code3_by_name($item->address_ip);
    
$asn geoip_asnum_by_name($item->address_ip);
    
$city geoip_record_by_name($item->address_ip)['city'];
    
$country geoip_record_by_name($item->address_ip)['country_name'];
    echo 
$asn !== false ''.$asn.'' '';
    echo 
$code3 !== false ' ('.$code3.')' '';
    echo 
$city !== false ' '.$city.'' '';
    echo 
$country !== false ' '.$country.'' '';
    echo 
'</td>'."\n";
    echo 
'<td class="date">'.$item->date->year.'/'.$item->date->month.'</td>'."\n";
    echo 
'</tr>'."\n";
}

endforeach;
echo 
'</tbody>'."\n";
echo 
'</table>'."\n";

echo 
'<p>Count : </p>'."\n";
echo 
'<ul>'."\n";
//echo '<li>Emails sent : '.number_format($read_analysis->dmarc->count->emails->sent).'</li>'."\n";
//echo '<li>Emails past : '.number_format($read_analysis->dmarc->count->emails->passed).'</li>'."\n";
echo '<li>Emails sent unauthorized : '.number_format($read_analysis->dmarc->count->emails->errors $count_my_error_mail).'</li>'."\n";
echo 
'<li>Number of email sender servers in error : '.number_format($read_analysis->dmarc->count->servers_error $count_my_spf).'</li>'."\n";
echo 
'</ul>'."\n";

echo 
'<hr /><br /><br />'."\n";
//-----------------------------------------

endif;

// Reports / DMARC / Analysis
//-------------------------------------------------------------------------

echo '</body>'."\n";
echo 
'</html>'."\n";

?>