HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.activity.php
<?php
/**
 * This file contains the iwp_activity base class and supporting global functions / definitions
 *
 * @version $Id$
 * @author Gwilym Evans <gwilym.evans@interspire.com>
 *
 * @package IWP
 * @subpackage IWP_API
 */

define('IWP_ACTIVITY_TRIMLENGTH', 5000); // @var integer The activity log will be trimmed of all but the most recent IWP_ACTIVITY_TRIMLENGTH items

/**
 * IWP Activity Class
 * This class oversees access to the iwp activity log.
 *
 * @package IWP
 * @subpackage IWP_API
 */
class iwp_activity extends iwp_engine {
	protected $primaryKey = 'activityid';
	protected $baseTableName = 'activity';

	public $_columns = array(
		'activityid'	=> 0,
		'source'		=> '',
		'scope'			=> '',
		'subjecttext'	=> '',
		'subjectid'		=> '',
		'usertext'		=> '',
		'userid'		=> '',
		'userip'		=> '',
		'activitydate'	=> '',
		'action'		=> '',
		'colour'		=> '',
	);

	/**
	 * @var iwp_activity
	 */
	public static $Instance;

	/**
	 * @return iwp_activity
	 */
	public static function getInstance () {
		if (!isset(self::$Instance)) {
			self::$Instance = new self();
		}
		return self::$Instance;
	}

	/**
	 * Picks a colour id based on the source and scope by checking for an existing colour in the database or assigning a new, unused id. Will scan for unsued colour ids instead of using max() + 1
	 *
	 * @param string $source
	 * @param string $scope
	 * $return integer The decided colour id
	 */
	public static function PickColourId ($source, $scope) {
		$activity = self::getInstance();

		$sql = "SELECT colour FROM ". $activity->GetTableName() ." WHERE source = '". $activity->db->Quote($source) ."' AND scope = '". $activity->db->Quote($scope) ."' AND colour IS NOT NULL LIMIT 1";
		$colour = $activity->db->FetchOne($sql);

		if ($colour === '') {
			$sql = "SELECT DISTINCT colour FROM ". $activity->GetTableName() ." ORDER BY colour ASC";
			$result = $activity->db->Query($sql);
			$colour = 0;
			while ($row = $activity->db->Fetch($result)) {
				if ((int)$row['colour'] != $colour) {
					break;
				}
				$colour++;
			}
		} else {
			$colour = (int)$colour;
		}

		return $colour;
	}

	/**
	 * @param string $source The component of the system this activity has been generated from. Must match the ENUM in the activity table ('core', 'content' or 'module') - activities which are part of the module framework such as activate / deactivate should be classified as source 'core' and scope 'modules'
	 * @param string $scope The part of the above component which generated this activity (the codename of the module, the name of the content type, or a code for other core components such as 'users', 'contenttypes', 'categories', 'groups', etc. - generally the same as the class name)
	 * @param string $action A verb describing the action which has been taken (create, delete, activate, logout, etc.)
	 * @param string $subjectId An ID which references the item being actioned. Usually an integer but we store it as text for module compatability reasons as the id could be the codename of the module.
	 * @param string $subjectText Text representation of the subject, stored incase an item is deleted
	 * @param string $userId An ID which references the user performing this activity. Can be a number which references an internal user id or some other module-specific data (like the email address of a comment poster).
	 * @param string $userText Text representation of the user (user name, comment poster's name, etc.)
	 * @param string $userIP IP address of the user / process which triggered this activity (in IPv4 #.#.#.# format). Optional. If not provided, will use the client/browser IP.
	 */
	public static function AddEntry ($source, $scope, $action, $subjectId, $subjectText, $userId = null, $userText = null, $userIP = null) {
		$activity = new iwp_activity();
		$activity->ResetColumns();

		$colour = self::PickColourId($source, $scope);

		if (is_null($userIP)) {
			$userIP = GetIP();
		}

		if (is_null($userId)) {
			if (is_null($userText)) {
				$userId = $activity->auth->GetUserInfo('userid');
				$userText = $activity->auth->GetUserInfo('firstname') .' '. $activity->auth->GetUserInfo('lastname');
			} else {
				$userId = '';
			}
		}

		$activity->Set(array(
			'source'		=> $source,
			'scope'			=> $scope,
			'action'		=> $action,
			'subjectid'		=> self::Truncate($subjectId),
			'subjecttext'	=> self::Truncate($subjectText),
			'userid'		=> self::Truncate($userId),
			'usertext'		=> self::Truncate($userText),
			'userip'		=> self::Truncate($userIP),
			'colour'		=> $colour,
			'activitydate'	=> GetMysqlDateTime(),
		));

		$activity->Save();

		self::TrimTable();
	}

	public static function TrimTable () {
		$activity = self::getInstance();

		$sql = "SELECT COUNT(*) FROM ". $activity->GetTableName();
		$count = $activity->db->FetchOne($sql);

		if ($count > IWP_ACTIVITY_TRIMLENGTH) {
			$sql = "DELETE FROM ". $activity->GetTableName() ." ORDER BY activitydate ASC, activityid ASC LIMIT ". ($count - IWP_ACTIVITY_TRIMLENGTH);
			$activity->db->Query($sql);
			$activity->db->Query("OPTIMIZE TABLE ". $activity->GetTableName());
		}
	}

	/**
	 * Renames all items in the activity table from one scope name to another. Used when a content type name is changed.
	 *
	 * @param string $source The source to manage
	 * @param string $scopeOld The scope to rename
	 * @param string $scopeNew The new scope name
	 */
	public static function RenameScope ($source, $scopeOld, $scopeNew) {
		$activity = self::getInstance();
		$sql = "UPDATE ". $activity->GetTableName() ." SET scope = '". $activity->db->Quote($scopeNew) ."' WHERE source = '". $activity->db->Quote($source) ."' AND scope = '". $activity->db->Quote($scopeOld) ."'";
		return $activity->db->Query($sql);
	}

	/**
	 * Truncates a string for the activity log
	 *
	 * @param string $string The string to truncate.
	 * @param integer $length The length to truncate to. Optional. Default is 255.
	 * @param string $more The string to use to indicate the string was truncated. Optional. Default is '...'.
	 * @return string
	 */
	public static function Truncate ($string, $length = 255, $more = '...') {
		if (strlen($string) <= $length) {
			//	return immediately if the string to be truncated is smaller than our cutoff limit
			return $string;
		}

		if (is_null($more) || !$more) {
			$more = '';
		}

		$moreLength = strlen($more);
		$cutLength = $length - $moreLength;
		return substr($string, 0, $cutLength) . $more;
	}

	/**
	 * Returns the most recent $n entries in the activity log as an array of associative arrays.
	 *
	 * @param boolean $formatting If true will perform language and url lookups on the results in preparation for displaying in dashboard HTML. Optional. Default false.
	 * @param integer $n The number of entries to return. Optional. Default 10.
	 * @param string $source If provided, will filter on the given source. Optional.
	 * @param string $scope If provided, will filter on the given scope. Optional.
	 * @param string $action If provided, will filter on the given action. Optional.
	 * @return array
	 */
	public static function GetRecent ($formatting = false, $n = 10, $source = null, $scope = null, $action = null) {
		$n = (int)$n;
		if ($n < 1) {
			$n = 10;
		}

		$activity = self::getInstance();

		$where = array();

		if (!is_null($source)) {
			$where[] = "source = '". $activity->db->Quote($source) ."'";
		}

		if (!is_null($scope)) {
			$where[] = "scope = '". $activity->db->Quote($scope) ."'";
		}

		if (!is_null($action)) {
			$where[] = "action = '". $activity->db->Quote($action) ."'";
		}

		if (count($where)) {
			$where = "WHERE (". implode(") AND (", $where) .")";
		} else {
			$where = '';
		}

		$sql = "SELECT * FROM ". $activity->GetTableName() . " ". $where ." ORDER BY activitydate DESC, activityid DESC LIMIT ". (int)$n;
		$rows = $activity->db->FetchQueryAll($sql);

		if ($formatting) {
			foreach ($rows as &$row) {
				$subject_url = '';
				$user_url = '';

				switch ($row['source']) {
					case 'content':
						$scope_lang = $row['scope'];
						$action_lang = GetLangMulti(array('Activity_'. $row['source'] .'_'. $row['action'], 'Activity_core__'. $row['action']));

						if (@$row['subjectid']) {
							$subject_url = 'index.php?section=content&action=edit&id='. $row['subjectid'];
						}

						if (@$row['userid']) {
							$user_url = 'index.php?section=user&action=edit&userid='. $row['userid'];
						}
					break;

					case 'module':
						$moduleClass = 'iwp_module_'. $row['scope'];
						if (class_exists($moduleClass)) {
							$module = new $moduleClass;

							//	use module language files for verb language
							$scope_lang = $module->lang->Get('Activity_'. $row['source'] .'_'. $row['scope']);
							$action_lang = $module->lang->GetMulti(array('Activity_'. $row['source'] .'_'. $row['scope'] .'_'. $row['action'], 'Activity_core__'. $row['action']));

							//	ask the module for urls to this activity item
							$subject_url = $module->GetActivityLogSubjectURL(@$row['subjectid'], @$row['subjecttext']);
							$user_url = $module->GetActivityLogUserURL(@$row['userid'], @$row['usertext']);
						} else {
							$scope_lang = $row['scope'];
							$action_lang = $row['action'];
						}
					break;

					default:
						$scope_lang = GetLang('Activity_'. $row['source'] .'_'. $row['scope']);
						$action_lang = GetLangMulti(array('Activity_'. $row['source'] .'_'. $row['scope'] .'_'. $row['action'], 'Activity_core__'. $row['action']));
						$scopeClass = 'iwp_admin_'. $row['scope'];
						if (class_exists($scopeClass)) {
							$scope = new $scopeClass;
							$subject_url = $scope->GetActivityLogSubjectURL(@$row['subjectid'], @$row['subjecttext'], $row['source'], $row['scope'], $row['action']);
							$user_url = $scope->GetActivityLogUserURL(@$row['userid'], @$row['usertext'], $row['source'], $row['scope'], $row['action']);
						}
					break;
				}

				$row['scope_lang'] = $scope_lang;
				$row['action_lang'] = $action_lang;
				$row['subject_url'] = $subject_url;
				$row['user_url'] = $user_url;

				$row['activitydate_relative'] = '';
				if ($row['activitydate']) {
					$row['activitydate_relative'] = GetRelativeTime(strtotime($row['activitydate']));
				}
			}
		}

		return $rows;
	}
}