| Current Path : /home/smartconb/www/armencom33/administrator/components/com_eyesite/models/ |
| Current File : /home/smartconb/www/armencom33/administrator/components/com_eyesite/models/history.php |
<?php
/********************************************************************
Product : Eyesite
Date : 11 January 2024
Copyright : Les Arbres Design 2009-2024
Contact : https://www.lesarbresdesign.info
Licence : GNU General Public License
*********************************************************************/
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Pagination\Pagination;
class EyesiteModelHistory extends LAE_model
{
var $data;
//-------------------------------------------------------------------------------
// Get one row
//
function getOne($id)
{
$query = "SELECT * FROM `#__eye_site_history` WHERE `id` = '$id'";
$this->data = $this->ladb_loadObject($query);
return $this->data;
}
//-------------------------------------------------------------------------------
// Return a pointer to our pagination object
// This should normally be called after getList()
//
function getPagination()
{
if ($this->_pagination == Null)
$this->_pagination = new Pagination(0,0,0);
return $this->_pagination;
}
//-------------------------------------------------------------------------------
// Get the list of history records
//
function getList()
{
$limit = $this->app->get('list_limit');
$limitstart = $this->app->getUserStateFromRequest('com_eyesite.history_limitstart', 'limitstart', 0, 'int');
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0); // In case limit has been changed
$search = $this->app->getUserStateFromRequest('com_eyesite.history_search','search','','string');
$history_filter = $this->app->getUserStateFromRequest('com_eyesite.history_filter','history_filter',0,'int');
// build the query
$query_count = "Select count(*) ";
$query_cols = "Select * ";
$query_from = "From `#__eye_site_history` ";
$query_where = "Where 1 ";
$query_order = " Order by `id` DESC";
if ($search != '')
$query_where .= $this->make_search($search);
switch ($history_filter)
{
case 1: $query_where .= " And `state` = ".LAE_HISTORY_SCAN_STARTED; break;
case 2: $query_where .= " And `state` = ".LAE_HISTORY_SCAN_CHANGES; break;
case 3: $query_where .= " And `state` = ".LAE_HISTORY_SCAN_NO_CHANGES; break;
case 4: $query_where .= " And `state` IN (".LAE_HISTORY_SCAN_ERROR.','.LAE_HISTORY_EMAIL_FAILED.','.LAE_HISTORY_SCAN_EXCEPTION.')'; break;
case 5: $query_where .= " And `state` = ".LAE_HISTORY_CONFIG_CHANGE; break;
case 6: $query_where .= " And `state` IN (".LAE_HISTORY_UPDATE_JOOMLA.','.LAE_HISTORY_UPDATE_EXTENSION.','.LAE_HISTORY_NEW_EXTENSION.')'; break;
}
// get the total row count
$count_query = $query_count.$query_from.$query_where;
$total = $this->ladb_loadResult($count_query);
if ($total === false)
{
$this->app->enqueueMessage($this->ladb_error_text, 'error');
return array();
}
// setup the pagination object
$this->_pagination = new Pagination($total, $limitstart, $limit);
// get the data
$main_query = $query_cols.$query_from.$query_where.$query_order;
LAE_trace::trace($main_query.' ['.$this->_pagination->limitstart.', '.$this->_pagination->limit.']');
$rows = $this->ladb_loadObjectList($main_query, $this->_pagination->limitstart, $this->_pagination->limit);
if ($rows === false)
{
$this->app->enqueueMessage($this->ladb_error_text, 'error');
return array();
}
return $rows;
}
//-------------------------------------------------------------------------------
// Make the where clause for the search string
//
function make_search($search)
{
// YYYY-MM-DD, YYYY-MM or YYYY - search by date
if (self::validDate($search)) // YYYY-MM-DD
return " AND DATE(`datetime`) = ".$this->_db->Quote($search).' ';
if (self::validDate($search.'-01')) // YYYY-MM
{
$search_year = substr($search,0,4);
$search_month = substr($search,5,2);
return " AND YEAR(`datetime`) = $search_year AND MONTH(`datetime`) = $search_month ";
}
if ((strlen($search) == 4) && LAE_helper::is_posint($search)) // YYYY
return " AND YEAR(`datetime`) = $search ";
// any other string searches summary and details
$search_like = $this->_db->Quote('%'.$search.'%');
return " AND (LOWER(`details`) LIKE LOWER($search_like) OR LOWER(`summary`) LIKE LOWER($search_like))";
}
//-------------------------------------------------------------------------------
// delete one or more history entries
//
function delete()
{
$jinput = Factory::getApplication()->input;
$cids = $jinput->get('cid', array(0), 'ARRAY');
$message = '';
foreach ($cids as $cid)
{
$query = "SELECT `state` FROM `#__eye_site_history` WHERE `id` = ".$this->_db->Quote($cid);
$state = $this->ladb_loadResult($query);
if (($state == LAE_HISTORY_SCAN_CHANGES) || ($state == LAE_HISTORY_CONFIG_CHANGE))
{
$message = Text::_('COM_EYESITE_NO_DELETE_CHANGES');
continue;
}
$query = "delete from `#__eye_site_history` where `id` = ".$this->_db->Quote($cid);
$result = $this->ladb_execute($query);
if ($result === false)
$message = $this->ladb_error_text;
}
if ($message != '')
{
$this->app->enqueueMessage($message, 'error');
return false;
}
return true;
}
//---------------------------------------------------------------
// Save a new history record
// Returns TRUE on success or FALSE if there is an error
//
function store($state, $summary, $details)
{
if (strlen($summary) > 128)
{
$details = $summary.'<br>'.$details;
$summary = substr($summary, 0, 128).' ...';
}
$query = "INSERT INTO `#__eye_site_history` (`datetime`, `state`, `summary`, `details`) VALUES
(NOW(), ".$this->_db->Quote($state).','.$this->_db->Quote($summary).','.$this->_db->Quote($details).')';
$result = $this->ladb_execute($query);
if ($result === false)
{
LAE_trace::trace($this->ladb_error_text);
return false;
}
return true;
}
//---------------------------------------------------------------
// Update the summary on a history record
// Returns TRUE on success or FALSE if there is an error
//
function update_summary($id, $summary)
{
$summary = substr($summary, 0, LAE_MAX_VARCHAR_LENGTH);
$query = "UPDATE `#__eye_site_history` SET `summary` = ".$this->_db->Quote($summary)." WHERE `id` = '$id'";
$result = $this->ladb_execute($query);
if ($result === false)
{
$this->app->enqueueMessage($this->ladb_error_text, 'error');
return false;
}
return true;
}
//-------------------------------------------------------------------------------
// Determine if a scanner instance is running
// Returns false if one is not running, true if one is
//
function scanning()
{
// get the last scan start record that is less than 10 minutes old
// (if there isn't one, we assume that no scanner is running)
$query = "SELECT MAX(`datetime`) FROM `#__eye_site_history`
WHERE `state` = ".LAE_HISTORY_SCAN_STARTED." AND TIMESTAMPDIFF(MINUTE, `datetime`, NOW()) < 10";
$datetime = $this->ladb_loadResult($query);
if (empty($datetime))
return false;
// we got a start record less than 10 minutes old
// is there a stop record newer than it?
$query = "SELECT count(*) FROM `#__eye_site_history`
WHERE `state` IN (".LAE_HISTORY_SCAN_NO_CHANGES.", ".LAE_HISTORY_SCAN_CHANGES.", ".LAE_HISTORY_SCAN_ERROR.")
AND `datetime` >= '$datetime'";
$result = $this->ladb_loadResult($query);
if ($result == 0)
return true; // we couldn't find a newer stop record
else
return false; // we found a newer stop record
}
//-------------------------------------------------------------------------------
// Return true if the scanner started in the last 20 seconds
// (20 seconds in case it takes a while before we call this)
//
function started()
{
$query = "SELECT MAX(`datetime`) FROM `#__eye_site_history`
WHERE `state` = ".LAE_HISTORY_SCAN_STARTED." AND TIMESTAMPDIFF(SECOND, `datetime`, NOW()) < 20";
$datetime = $this->ladb_loadResult($query);
if (empty($datetime))
return false;
else
return true;
}
//-------------------------------------------------------------------------------
// Check that a date is valid YYYY-MM-DD
// Returns true if valid, false if not
//
static function validDate($date, $allow_blank = false)
{
if ($allow_blank && (empty($date) || ($date == '0000-00-00')))
return true;
if (strlen($date) != 10)
return false;
if (($date[4] != '-') || ($date[7] != '-'))
return false;
if (!is_numeric(substr($date,0,4).substr($date,5,2).substr($date,8,2)))
return false;
return checkdate(substr($date,5,2), substr($date,8,2), substr($date,0,4)); // month, day, year
}
}