1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
<?php
/**
* TfDataObject class file.
*
* @copyright Simon Wilkinson 2013+ (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.1.2
* @package core
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Base class for data objects providing properties and methods that are standard in Tuskfish.
*
* I thought about making this a trait, since it will apply across otherwise unrelated families of
* content objects, but I'm still lukewarm on using traits for anything other than static lists.
*
* @copyright Simon Wilkinson 2013+ (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.1.2
* @package core
* @properties int $id Auto-increment, set by database.
* @properties int $online Toggle object on or offline.
* @properties int $submissionTime Timestamp representing submission time.
* @properties int $lastUpdated Timestamp representing last time this object was updated.
* @properties int $expiresOn Timestamp indicating the expiry date for this object.
* @properties int $counter Number of times this content was viewed or downloaded.
* @properties string $metaTitle Set a custom page title for this content.
* @properties string $metaDescription Set a custom page meta description for this content.
* @properties string $seo SEO-friendly string; it will be appended to the URL for this content.
* @properties string $handler Handler for this object (not persistent).
* @properties string $template The template that should be used to display this object (not persistent).
* @properties string $module The module that handles this content type (not persistent).
* @properties string $icon The vector icon that represents this object type (not persistent).
*/
class TfDataObject
{
/** Common properties. */
protected $id = '';
protected $submissionTime = '';
protected $lastUpdated = '';
protected $expiresOn = '';
protected $counter = '';
protected $online = '';
protected $metaTitle = '';
protected $metaDescription = '';
protected $seo = '';
protected $handler = '';
protected $template = '';
protected $module = '';
protected $icon = '';
/**
* Common utilities.
*/
/**
* Convert URLs back to TFISH_LINK and back for insertion or update, to aid portability.
*
* This is a helper method for loadPropertiesFromArray(). Only useful on HTML fields. Basically
* it converts the base URL of your site to the TFISH_LINK constant for storage or vice versa
* for display. If you change the base URL of your site (eg. domain) all your internal links
* will automatically update when they are displayed.
*
* @param string $html A HTML field that makes use of the TFISH_LINK constant.
* @param bool $liveUrls Flag to convert urls to constants (true) or constants to urls (false).
* @return string HTML field with converted URLs.
*/
protected function convertBaseUrlToConstant(string $html, bool $convertToConstant = false)
{
if ($convertToConstant === true) {
$html = str_replace(TFISH_LINK, 'TFISH_LINK', $html);
} else {
$html = str_replace('TFISH_LINK', TFISH_LINK, $html);
}
return $html;
}
/**
* Generates a URL to access this object in single view mode.
*
* URL can point relative to either the home page (index.php, or other custom content stream
* page defined by modifying TFISH_PERMALINK_URL in config.php) or to an arbitrary page in the
* web root. For example, you could rename index.php to 'blog.php' to free up the index page
* for a landing page (this requires you to append the name of the new page to the
* TFISH_PERMALINK_URL constant).
*
* @param string $customPage Use an arbitrary target page or the home page (index.php).
* @return string URL to view this object.
*/
public function getUrl(string $customPage = '')
{
$url = empty($customPage) ? TFISH_PERMALINK_URL : TFISH_URL;
if ($customPage) {
$url .= $this->validator->isAlnumUnderscore($customPage)
? $this->validator->trimString($customPage) . '.php' : '';
}
$url .= '?id=' . (int) $this->id;
if ($this->seo) {
$url .= '&title=' . $this->validator->encodeEscapeUrl($this->seo);
}
return $url;
}
/**
* Reset the last updated time for this content object (timestamp).
*/
public function updateLastUpdated()
{
$this->lastUpdated = time();
}
/**
* Common accessors and mutators.
*/
/**
* Set the ID for this object.
*
* @param int $id ID of this object.
*/
public function setId(int $id)
{
if ($this->validator->isInt($id, 0)) {
$this->id = $id;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the ID of the content object, XSS safe.
*
* @return int ID of content object.
*/
public function getId()
{
return (int) $this->id;
}
/**
* Set the submission time for this expert.
*
* @param int $submissionTime Timestamp.
*/
public function setSubmissionTime(int $submissionTime)
{
if ($this->validator->isInt($submissionTime, 0)) {
$this->submissionTime = $submissionTime;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Return formatted date that this expert was submitted.
*
* @return string Date/time of submission.
*/
public function getSubmissionTime()
{
$date = date('j F Y', $this->$submissionTime);
return $this->validator->escapeForXss($date);
}
/**
* Set the time this expert was last updated.
*
* @param int $lastUpdated Timestamp.
*/
public function setLastUpdated(int $lastUpdated)
{
if ($this->validator->isInt($lastUpdated, 0)) {
$this->lastUpdated = $lastUpdated;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Return formatted date/time this expert was last updated, escaped for display.
*
* @return string Date/time last updated.
*/
public function getLastUpdated()
{
$date = date('j F Y', $this->$lastUpdated);
return $this->validator->escapeForXss($date);
}
/**
* Set the time this expert profile expires.
*
* @param int $expiresOn Timestamp.
*/
public function setExpiresOn(int $expiresOn)
{
if ($this->validator->isInt($expiresOn, 0)) {
$this->expiresOn = $expiresOn;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Set this profile as online or offline.
*
* Offline profiles are not publicly accessible and are not returned in search results.
*
* @param int $online Online (1) or offline (0).
*/
public function setOnline(int $online)
{
if ($this->validator->isInt($online, 0, 1)) {
$this->online = $online;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the XSS-safe online status of this record.
*
* @return boolean True if online, false otherwise.
*/
public function getOnline()
{
if ($this->online === 1) {
return true;
}
return false;
}
/**
* Set the view counter for this profile.
*
* @param int $counter Number of times this profile has been viewed.
*/
public function setCounter(int $counter)
{
if ($this->validator->isInt($counter, 0)) {
$this->counter = $counter;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the number of times this expert was viewed, XSS safe.
*
* @return int View counter.
*/
public function getCounter()
{
return (int) $this->counter;
}
/**
* Set the meta title for this expert.
*
* @param string $metaTitle Meta title.
*/
public function setMetaTitle(string $metaTitle)
{
$this->metaTitle = $this->validator->trimString($metaTitle);
}
/**
* Returns the meta title for this expert XSS escaped for display.
*
* @return string Meta title.
*/
public function getMetaTitle()
{
return $this->validator->escapeForXss($this->metaTitle);
}
/**
* Set the meta description for this expert.
*
* @param string $metaDescription Meta description.
*/
public function setMetaDescription(string $metaDescription)
{
$this->metaDescription = $this->validator->trimString($metaDescription);
}
/**
* Return the meta description of this expert XSS escaped for display.
*
* @return string Meta description.
*/
public function getMetaDescription()
{
return $this->validator->escapeForXss($this->metaDescription);
}
/**
* Set the SEO-friendly search string for this content object.
*
* The SEO string will be appended to the URL for this object.
*
* @param string $seo Dash-separated-title-of-this-object.
*/
public function setSeo(string $seo)
{
$cleanSeo = (string) $this->validator->trimString($seo);
// Replace spaces with dashes.
if ($this->validator->isUtf8($cleanSeo)) {
$cleanSeo = str_replace(' ', '-', $cleanSeo);
} else {
trigger_error(TFISH_ERROR_NOT_UTF8, E_USER_ERROR);
}
$this->seo = $cleanSeo;
}
/**
* Return the SEO string for this expert XSS for display.
*
* @return string SEO-friendly URL string.
*/
public function getSeo()
{
return $this->validator->escapeForXss($this->seo);
}
/**
* Set the handler class for this sensor type.
*
* @param string $handler Handler name (alphabetical characters only).f
*/
public function setHandler(string $handler)
{
$cleanHandler = $this->validator->trimString($handler);
if ($this->validator->isAlpha($cleanHandler)) {
$this->handler = $cleanHandler;
} else {
trigger_error(TFISH_ERROR_NOT_ALPHA, E_USER_ERROR);
}
}
/**
* Set the module for this sensor.
*
* Usually handled by the sensor's constructor.
*
* @param string $module Module name (alphabetical characters only).
*/
public function setModule(string $module)
{
$cleanModule = $this->validator->trimString($module);
if ($this->validator->isAlpha($module)) {
$this->module = $cleanModule;
}
}
/**
* Set the template file for displaying this sensor.
*
* The equivalent HTML template file must be present in the active theme.
*
* @param string $template Template filename without extension, eg. 'camera'.
*/
public function setTemplate(string $template)
{
$cleanTemplate = $this->validator->trimString($template);
if ($this->validator->isAlnumUnderscore($cleanTemplate)) {
$this->template = $cleanTemplate;
} else {
trigger_error(TFISH_ERROR_NOT_ALNUMUNDER, E_USER_ERROR);
}
}
/**
* Set (override) the icon for this expert.
*
* @param string $icon Icon expressed as a FontAwesome tag, eg. '<i class="fas fa-file-alt"></i>'
*/
public function setIcon(string $icon)
{
$icon = $this->validator->trimString($icon);
$this->icon = $this->validator->filterHtml($icon);
}
/**
* Returns the Font Awesome icon for this expert, XSS safe (prevalidated with HTMLPurifier).
*
* @return string FontAwesome icon for this expert (HTML).
*/
public function getIcon()
{
return $this->icon;
}
}