Created
February 19, 2021 17:14
-
-
Save pe7er/f241bead9fae8d7b441b7ac2324928c4 to your computer and use it in GitHub Desktop.
PHP website script to send an email notification about the changed .php files in the last 24 hours
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package CheckChangedFiles | |
* | |
* @copyright Copyright (C) 2021 db8.nl. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE.txt | |
*/ | |
/** | |
* A script to check new files and mail them to an e-mail address | |
* When the server does not support the mail functionality on SSH CLI command line | |
* Put the script at some folder and give it an unique not-easy-to-guess name checkfiles-324586yasfdasf234.php | |
* and trigger it via the URL via a crontab | |
*/ | |
use Joomla\CMS\Factory; | |
define('_JEXEC', 1); | |
define('JPATH_BASE', dirname(__FILE__)); | |
require_once JPATH_BASE . '/includes/defines.php'; | |
require_once JPATH_BASE . '/includes/framework.php'; | |
$mainframe = JFactory::getApplication('site'); | |
$mainframe->initialise(); | |
// Settings | |
$folder = '/var/www/public_html/'; | |
$subject = '[example.com] Files changed on server'; | |
$recipients = [ | |
'person1@example.com', | |
'person2@example.com' | |
]; | |
$body = "The following files have been changed in the last 24 hours:\n\n"; | |
$files = scanSite($folder, "/^.*\.(php)$/"); | |
foreach ($files as $key => $file) | |
{ | |
$body .= $key + 1 . '. ' . $file . "\n"; | |
} | |
mailChanges($subject, $recipients, $body); | |
/** | |
* @param string $folder Folder to scan | |
* @param string $pattern File pattern to scan | |
* | |
* @return array | |
* @since 1.0.0 | |
*/ | |
function scanSite($folder, $pattern) | |
{ | |
$dir = new RecursiveDirectoryIterator($folder); | |
$ite = new RecursiveIteratorIterator($dir); | |
$files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); | |
$fileList = array(); | |
foreach ($files as $file) | |
{ | |
if (filemtime($file[0]) > (time() - (24 * 60 * 60))) | |
{ | |
$fileList[] = $file[0]; | |
} | |
} | |
return $fileList; | |
} | |
/** | |
* @param string $subject Email subject | |
* @param array $recipients Send email to these recepients | |
* @param string $body Email body | |
* | |
* @return void | |
* @since 1.0.0 | |
*/ | |
function mailChanges($subject, $recipients, $body) | |
{ | |
$mailer = Factory::getMailer(); | |
$config = Factory::getConfig(); | |
$sender = [ | |
$config->get('mailfrom'), | |
$config->get('fromname') | |
]; | |
$mailer->setSender($sender); | |
$mailer->addRecipient($recipients); | |
$mailer->setSubject($subject); | |
$mailer->setBody($body); | |
$send = $mailer->Send(); | |
if ($send !== true) | |
{ | |
echo 'Error sending email: '; | |
} | |
else | |
{ | |
echo 'Mail sent'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment