#!/usr/bin/perl

# This is a wrapper that wraps around the PHP mail() function. 
# It logs some useful extra variables to /var/log/maillog.
# These give us a good indication about which PHP script is
# sending emails:

$HTTP_HOST = $ENV{_HTTP_HOST};
$REMOTE_ADDR = $ENV{_REMOTE_ADDR};
$PWD = $ENV{PWD};
$SCRIPT_NAME = $ENV{_SCRIPT_NAME};
$UID = $ENV{SUID};
$OWNER = $ENV{_SOWNER};

# If the AV-SPAM and Milter-GeoIP are installed, then we include the somewhat
# more sophisticated libraries of it to check if PHP scripts send more email
# than allowed for Vsites. If so, the sending of Email for scripts of that
# Vsite will be aborted.
if (-f "/home/solarspeed/milter-geoip/bin/libs/phpsendmail_include.pl") {
    require "/home/solarspeed/milter-geoip/bin/libs/phpsendmail_include.pl";
    $status = &mysql_logger();
    if ($status eq "FAIL") {
        exit(0);
    }
}

# Get the Bash $* equivalent:
$options = join(" " , @ARGV);

# Log the activity into /var/log/maillog with all the info we have gathered:
system("/usr/bin/logger -p mail.info sendmail-wrapper-php: site=$HTTP_HOST, client=$REMOTE_ADDR, script=$PWD$SCRIPT_NAME, uid=$UID, user=$OWNER");

# Actually send the email by passing it to Sendmail:
system("/usr/sbin/sendmail -t -i $options");

# 
# Copyright (c) 2015-2022 Michael Stauber, SOLARSPEED.NET
# Copyright (c) 2015-2022 Team BlueOnyx, BLUEONYX.IT
# All Rights Reserved.
# 
# 1. Redistributions of source code must retain the above copyright 
#     notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright 
#     notice, this list of conditions and the following disclaimer in 
#     the documentation and/or other materials provided with the 
#     distribution.
# 
# 3. Neither the name of the copyright holder nor the names of its 
#     contributors may be used to endorse or promote products derived 
#     from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.
# 
# You acknowledge that this software is not designed or intended for 
# use in the design, construction, operation or maintenance of any 
# nuclear facility.
# 