<?php
/* * ***********************************************************************
 * Akeeba Warning App for Watchful
 * Copyright (c) 2006-2013 Anything Digital & Inetis
 * ************************************************************************
 * Watchful.li is a web site monitoring and security service.
 * 
 * Although Watchful.li is a paid service, 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, or (at your option) any later version.
 *
 * This header must not be removed. Additional contributions/changes
 * may be added to this header as long as no information is deleted.
 * ************************************************************************
 * Get the latest version of this program at: https://watchful.li
 * *********************************************************************** */

defined('_JEXEC') or die;

jimport('joomla.filesystem.file');

$baseClassFile = JPATH_ADMINISTRATOR . '/components/com_watchfulli/classes/apps.php';
if (JFile::exists($baseClassFile)) {
	require_once $baseClassFile;
}
else {
	JError::raiseError('500', 'Internal Server Error');
}

class AkeebaWarningAppAlert extends AppAlert
{
	public $parameter1;
	public $parameter2;
	public $parameter3;
	
	public function AkeebaWarningAppAlert($level, $message, $parameter1 = null, $parameter2 = null, $parameter3 = null) {
		if (!is_null($level) && !is_null($message)) {
			$this->level      = $level;
			$this->message    = $message;
			$this->parameter1 = $parameter1;
			$this->parameter2 = $parameter2;
			$this->parameter3 = $parameter3;
		}
	}
}

class plgWatchfulliAppsAkeebaWarningApp extends watchfulliApps
{
	public function createAppAlert($level, $message, $parameter1 = null, $parameter2 = null, $parameter3 = null) {
		$alert = new AkeebaWarningAppAlert($level, $message, $parameter1, $parameter2, $parameter3);
		$this->addAlert($alert);
	}
	
	public function debug($what) {
		$this->createAppAlert(1, "[DEBUG][AKEEBAWARNINGAPP] " . print_r($what, 1));
	}
	
	public function appMainProgram($oldValuesSerialized = null) {
		JFactory::getLanguage()->load('plg_watchfulliApps_akeebaWarningApp.sys', JPATH_ADMINISTRATOR);
		// set the application details
		$this->setName("AkeebaWarningApp");
		$this->setDescription("Akeeba Warning App for Watchful - This App checks for recent backups.");
		// init
		$debug = $this->params->get('debug', 0);
		$hours = max(1, (int) $this->params->get('hours', 48));
		$db    = JFactory::getDbo();
		// check for akeeba
		$db->setQuery("SHOW TABLES LIKE '%_ak_stats'");
		try {
			if ($debug) {
				$this->debug(JText::sprintf("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_RUNNING_QUERY", (string) $db->getQuery(false)));
			}
			$table = $db->loadResult();
			if (empty($table)) {
				if ($debug) {
					$this->debug(JText::_("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_NOT_INSTALLED"));
				}
				goto done;
			}
		}
		catch (Exception $e) {
			if ($debug) {
				$this->debug(JText::sprintf("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_SQL_ERROR", $e->getMessage()));
			}
			goto done;
		}
		
		// get the time
		$db->setQuery($db->getQuery(true)
			->select('TIME_TO_SEC(TIMEDIFF(NOW(),MAX(' . $db->quoteName('backupstart') . ')))/3600 AS hours')
			->from('#__ak_stats')
			->where($db->quoteName('status') . ' = ' . $db->quote('complete'))
			->where($db->quoteName('type') . ' = ' . $db->quote('full'))
		);
		
		try {
			if ($debug) {
				$this->debug(JText::sprintf("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_RUNNING_QUERY", (string) $db->getQuery(false)));
			}
			$result = $db->loadResult();
			if ($debug) {
				$this->debug(JText::sprintf("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_LAST_BACKUP", $result));
			}
			if (empty($result)) {
				if ($debug) {
					$this->debug(JText::_("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_NO_BACKUPS_FOUND"));
				}
				goto done;
			}
		}
		catch (Exception $e) {
			if ($debug) {
				$this->debug(JText::sprintf("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_DEBUG_SQL_ERROR", $e->getMessage()));
			}
			goto done;
		}
		
		if ($hours < (float) $result) {
			$this->createAppAlert(1, JText::sprintf("PLG_WATCHFULLIAPPS_AKEEBAWARNINGAPP_ALERT_OLD_BACKUP", $result));
		}
		
		done: {
			return $this;
		}
	}
}
