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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
<?php
/**
* TfPreference 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.0
* @package core
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Holds Tuskfish site configuration (preference) data.
*
* A preference object is automatically instantiated on every page via tfHeader.php.
*
* @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.0
* @package core
* @uses trait TfMagicMethods Prevents direct setting of properties / unlisted properties.
* @uses trait TfLanguage to obtain a list of available translations.
* @property TfValidator $validator Instance of the Tuskfish data validator class.
* @property string $siteName Name of website.
* @property string $siteDescription Meta description of website.
* @property string $siteAuthor Author of website.
* @property string $siteEmail Administrative contact email for website.
* @property string $siteCopyright Copyright notice.
* @property int $closeSite Toggle to close this site.
* @property string $serverTimezone Timezone of server location.
* @property string $siteTimezone Timezone for main audience location.
* @property int $minSearchLength Minimum length of search terms.
* @property int searchPagination Number of search results to show on a page.
* @property int userPagination Number of content objects to show on public index page.
* @property int adminPagination Number of content objects to show on admin index page.
* @property int galleryPagination Number of images to show in admin gallery.
* @property int paginationElements Number of slots to include on pagination controls.
* @property string sessionName Name of session.
* @property int sessionLife Expiry timer for inactive sessions (minutes).
* @property string defaultLanguage Default language of site.
* @property string dateFormat Format to display dates, as per PHP date() function.
* @property int enableCache Enable site cache.
* @property int cacheLife Expiry timer for site cache (seconds).
*/
class TfPreference
{
use TfMagicMethods;
use TfLanguage;
protected $validator;
protected $siteName;
protected $siteDescription;
protected $siteAuthor;
protected $siteEmail;
protected $siteCopyright;
protected $closeSite;
protected $serverTimezone;
protected $siteTimezone;
protected $minSearchLength;
protected $searchPagination;
protected $userPagination;
protected $adminPagination;
protected $galleryPagination;
protected $rssPosts;
protected $paginationElements;
protected $sessionName;
protected $sessionLife;
protected $defaultLanguage;
protected $dateFormat;
protected $enableCache;
protected $cacheLife;
/**
* Constructor.
*
* @param TfValidator $validator An instance of the Tuskfish data validator class.
* @param array $preferences An associative array holding Tuskfish preference settings, may
* be read from the database or passed in via the preference form.
*/
function __construct(TfValidator $validator, array $preferences)
{
if (is_a($validator, 'TfValidator')) {
$this->validator = $validator;
} else {
trigger_error(TFISH_ERROR_NOT_VALIDATOR, E_USER_ERROR);
}
$this->loadPropertiesFromArray($preferences);
}
/**
* Escape a property for on-screen display to prevent XSS.
*
* Applies htmlspecialchars() to a property destined for display to mitigate XSS attacks.
* Note that preference values should not be directly assigned to meta tags; they should be
* assigned to $tfMetadata instead, which will handle any escaping necessary.
*
* @param string $property Name of property.
* @return string Value of property escaped for display.
*/
public function escapeForXss(string $property)
{
$cleanProperty = $this->validator->trimString($property);
if (isset($this->$cleanProperty)) {
return htmlspecialchars($this->$cleanProperty, ENT_QUOTES, 'UTF-8', false);
} else {
return null;
}
}
/**
* Converts the preference object to an array suitable for insert/update calls to the database.
*
* Note that the output is not XSS escaped and should not be sent to display.
*
* @return array Array of object property/values.
*/
public function getPreferencesAsArray()
{
$keyValues = array();
foreach ($this as $key => $value) {
$keyValues[$key] = $value;
}
return $keyValues;
}
/**
* Update the preference object from an external data source (eg. form submission).
*
* The preference object will conduct its own internal data type validation and range checks.
*
* @param array $dirtyInput Usually $_REQUEST data.
*/
public function loadPropertiesFromArray(array $dirtyInput)
{
if (!$this->validator->isArray($dirtyInput)) {
trigger_error(TFISH_ERROR_NOT_ARRAY, E_USER_ERROR);
}
// Validate object properties as they are assigned.
if (isset($dirtyInput['siteName'])) $this->setSiteName($dirtyInput['siteName']);
if (isset($dirtyInput['siteDescription'])) $this->setSiteDescription($dirtyInput['siteDescription']);
if (isset($dirtyInput['siteAuthor'])) $this->setSiteAuthor($dirtyInput['siteAuthor']);
if (isset($dirtyInput['siteEmail'])) $this->setSiteEmail($dirtyInput['siteEmail']);
if (isset($dirtyInput['siteCopyright'])) $this->setSiteCopyright($dirtyInput['siteCopyright']);
if (isset($dirtyInput['closeSite'])) $this->setCloseSite((int) $dirtyInput['closeSite']);
if (isset($dirtyInput['serverTimezone'])) $this->setServerTimezone($dirtyInput['serverTimezone']);
if (isset($dirtyInput['siteTimezone'])) $this->setSiteTimezone($dirtyInput['siteTimezone']);
if (isset($dirtyInput['minSearchLength'])) $this->setMinSearchLength((int) $dirtyInput['minSearchLength']);
if (isset($dirtyInput['searchPagination'])) $this->setSearchPagination((int) $dirtyInput['searchPagination']);
if (isset($dirtyInput['userPagination'])) $this->setUserPagination((int) $dirtyInput['userPagination']);
if (isset($dirtyInput['adminPagination'])) $this->setAdminPagination((int) $dirtyInput['adminPagination']);
if (isset($dirtyInput['galleryPagination'])) $this->setGalleryPagination((int) $dirtyInput['galleryPagination']);
if (isset($dirtyInput['rssPosts'])) $this->setRssPosts((int) $dirtyInput['rssPosts']);
if (isset($dirtyInput['paginationElements'])) $this->setPaginationElements((int) $dirtyInput['paginationElements']);
if (isset($dirtyInput['sessionName'])) $this->setSessionName($dirtyInput['sessionName']);
if (isset($dirtyInput['sessionLife'])) $this->setSessionLife((int) $dirtyInput['sessionLife']);
if (isset($dirtyInput['defaultLanguage'])) $this->setDefaultLanguage($dirtyInput['defaultLanguage']);
if (isset($dirtyInput['dateFormat'])) $this->setDateFormat($dirtyInput['dateFormat']);
if (isset($dirtyInput['enableCache'])) $this->setEnableCache((int) $dirtyInput['enableCache']);
if (isset($dirtyInput['cacheLife'])) $this->setCacheLife((int) $dirtyInput['cacheLife']);
}
/**
* Set the number of objects to display in a single admin page view.
*
* @param int $value Number of objects to view on a single page.
*/
public function setAdminPagination(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 1)) {
$this->adminPagination = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the number of objects to display in a single admin page view, XSS safe.
*
* @return int Admin pagination.
*/
public function getAdminPagination()
{
return (int) $this->adminPagination;
}
/**
* Set the name of the site author. Used to population page meta author tag.
*
* @param string $value Name of the site author.
*/
public function setSiteAuthor(string $value)
{
$this->siteAuthor = $this->validator->trimString($value);
}
/**
* Returns the site author XSS escaped for display.
*
* @return string Site author.
*/
public function getSiteAuthor()
{
return $this->validator->escapeForXss($this->siteAuthor);
}
/**
* Set life of items in cache (seconds).
*
* Items that expire will be rebuilt and re-written to the cache the next time the page is
* requested.
*
* @param int $value Expiry timer on cached items (seconds).
*/
public function setCacheLife(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 1)) {
$this->cacheLife = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the cache life in seconds, XSS safe.
*
* @return in Cache life in seconds.
*/
public function getCacheLife()
{
return (int) $this->cacheLife;
}
/**
* Open our close the site.
*
* @param int $value Site open (0) or closed (1).
*/
public function setCloseSite(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 0, 1)) {
$this->closeSite = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Return the human readable value of the site closed preference, XSS safe.
*
* @return string Yes if site open, no if site closed.
*/
public function getCloseSite()
{
if ($this->closeSite === 1) {
return TFISH_YES;
}
return TFISH_NO;
}
/**
* Set the date format, used to convert timestamps to human readable form.
*
* See the PHP manual for date formatting templates: http://php.net/manual/en/function.date.php
*
* @param string $value Template for formatting dates.
*/
public function setDateFormat(string $value)
{
$this->dateFormat = $this->validator->trimString($value);
}
/**
* Returns the data format used to convert timestamps to human readable form, XSS safe.
*
* @return string Date format.
*/
public function getDateFormat()
{
return $this->validator->escapeForXss($this->dateFormat);
}
/**
* Set the default language for this Tuskfish installation.
*
* @param string $value ISO 639-1 two-letter language codes.
*/
public function setDefaultLanguage(string $value)
{
$cleanValue = $this->validator->trimString($value);
if (!$this->validator->isAlpha($cleanValue)) {
trigger_error(TFISH_ERROR_NOT_ALPHA, E_USER_ERROR);
}
$languageWhitelist = $this->getListOfLanguages();
if (array_key_exists($cleanValue, $languageWhitelist)) {
$this->defaultLanguage = $cleanValue;
} else {
trigger_error(TFISH_ERROR_ILLEGAL_VALUE, E_USER_ERROR);
}
}
/**
* Returns the default site language as a two-letter code XSS escaped for display.
*
* @return string Two letter ISO 639-1 language code.
*/
public function getDefaultLanguage()
{
return $this->validator->escapeForXss($this->defaultLanguage);
}
/**
* Set the site description. Used in meta description tag.
*
* @param string $value Site description.
*/
public function setSiteDescription(string $value)
{
$this->siteDescription = $this->validator->trimString($value);
}
/**
* Returns the site meta tag description, XSS safe.
*
* @return string Site description.
*/
public function getSiteDescription()
{
return $this->validator->escapeForXss($this->siteDescription);
}
/**
* Set the admin email address for the site.
*
* Used in RSS feeds to populate the managingEditor and webmaster tags.
*
* @param string $value Email address.
*/
public function setSiteEmail(string $value)
{
$cleanValue = $this->validator->trimString($value);
if ($this->validator->isEmail($cleanValue)) {
$this->siteEmail = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_EMAIL, E_USER_ERROR);
}
}
/**
* Returns the site administration email address, XSS safe.
*
* @return string Site email address.
*/
public function getSiteEmail()
{
return $this->validator->escapeForXss($this->siteEmail);
}
/**
* Enable or disable the cache.
*
* @param int $value Enabled (1) or disabled (0).
*/
public function setEnableCache(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 0, 1)) {
$this->enableCache = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the human readable value of the enable cache preference, XSS safe.
*
* @return string Yes if enabled, no if not.
*/
public function getEnableCache()
{
if ($this->enableCache === 1) {
return TFISH_YES;
}
return TFISH_NO;
}
/**
* Set number of objects to display on the gallery page.
*
* @param int $value Number of objects to display on a single page view.
*/
public function setGalleryPagination(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 1)) {
$this->galleryPagination = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Return the number of objects to display on the gallery page, XSS safe.
*
* @return int Gallery pagination.
*/
public function getGalleryPagination()
{
return (int) $this->galleryPagination;
}
/**
* Set the minimum length of search terms (characters).
*
* Search terms less than this number of characters will be discarded. It is usually best to
* allow a minimum length of 3 characters; this allows searching for common acronyms without
* returning massive numbers of hits.
*
* @param int $value Minimum number of characters.
*/
public function setMinSearchLength(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 3)) {
$this->minSearchLength = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the minimum allowed length of search terms, XSS safe.
*
* @return int Minimum search length.
*/
public function getMinSearchLength()
{
return (int) $this->minSearchLength;
}
/**
* Set the default number of page slots to display in pagination elements.
*
* Can be overridden manually in TfishPaginationControl.
*
* @param int $value Number of page slots to display in pagination control.
*/
public function setPaginationElements(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 3)) {
$this->paginationElements = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the number of slots to display in user-side pagination control, XSS safe.
*
* @return int Number of pagination elements.
*/
public function getPaginationElements()
{
return (int) $this->paginationElements;
}
/**
* Set number of items to display in RSS feeds.
*
* @param int $value Number of items to include in feed.
*/
public function setRssPosts(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 1)) {
$this->rssPosts = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the number of content items to include in RSS feed, XSS safe.
*
* @return int Number of posts in RSS.
*/
public function getRssPosts()
{
return (int) $this->rssPosts;
}
/**
* Set number of results to display on a search page view.
*
* @param int $value Number of objects to display in a single page view.
*/
public function setSearchPagination(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 0)) {
$this->searchPagination = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the number of items to display in one page of search results, XSS safe.
*
* @return Search pagination.
*/
public function getSearchPagination()
{
return (int) $this->searchPagination;
}
/**
* Set the server timezone.
*
* @param string $value Timezone.
*/
public function setServerTimezone(string $value)
{
$this->serverTimezone = $this->validator->trimString($value);
}
/**
* Returns the server timezone, XSS safe.
*
* @return string Server timezone.
*/
public function getServerTimezone()
{
return $this->validator->escapeForXss($this->serverTimezone);
}
/**
* Set the life of an idle session.
*
* User will be logged out after being idle for this many minutes.
*
* @param int $value Session life (minutes).
*/
public function setSessionLife(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 0)) {
$this->sessionLife = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns session lifetime in seconds, XSS safe.
*
* @return int Session life.
*/
public function getSessionLife()
{
return (int) $this->sessionLife;
}
/**
* Set the name (prefix) used to identify Tuskfish sessions.
*
* If you change it, use something unpredictable.
*
* @param string $value Session name.
*/
public function setSessionName(string $value)
{
$cleanValue = $this->validator->trimString($value);
if ($this->validator->isAlnum($cleanValue)) {
$this->sessionName = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_ALNUM, E_USER_ERROR);
}
}
/**
* Returns the session name, XSS safe but should never be publicly displayed.
*
* This method should only be used in the edit preferences form.
*
* @return string Session name.
*/
public function getSessionName()
{
return $this->validator->escapeForXss($this->sessionName);
}
/**
* Set the site meta copyright.
*
* Used to populate the dcterms.rights meta tag in the theme. Can be overridden in the
* theme.html file.
*
* @param string $value Copyright statement.
*/
public function setSiteCopyright(string $value)
{
$this->siteCopyright = $this->validator->trimString($value);
}
/**
* Returns the site copyright notice, XSS escaped for display.
*
* @return string Site copyright notice.
*/
public function getSiteCopyright()
{
return $this->validator->escapeForXss($this->siteCopyright);
}
/**
* Set the title of the page.
*
* Used to populate the meta title tag. Usually each page / object will specify a title, but
* if none is set this value will be used as the default. The title can be manually overriden
* on each page using the TfishMetadata object (see comments at the bottom of controller
* scripts).
*
* @param string $value
*/
public function setSiteName(string $value)
{
$this->siteName = $this->validator->trimString($value);
}
/**
* Returns the site name XSS escaped for display.
*
* @return string Site name.
*/
public function getSiteName()
{
return $this->validator->escapeForXss($this->siteName);
}
/**
* Set the site timezone.
*
* This is normally the timezone for your principal target audience.
*
* @param string $value Timezone.
*/
public function setSiteTimezone(string $value)
{
$this->siteTimezone = $this->validator->trimString($value);
}
/**
* Returns the site timezone, XSS escaped for display.
*
* @return string Site timezone.
*/
public function getSiteTimezone()
{
return $this->validator->escapeForXss($this->siteTimezone);
}
/**
* Set the number of objects to display in a single page view on the public facing side of the
* site.
*
* @param int $value Number of objects to display.
*/
public function setUserPagination(int $value)
{
$cleanValue = (int) $value;
if ($this->validator->isInt($cleanValue, 1)) {
$this->userPagination = $cleanValue;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
}
/**
* Returns the number of objects to display in a single page view on the public side of the
* site, XSS escaped for display.
*
* @return User pagination.
*/
public function getUserPagination()
{
return (int) $this->userPagination;
}
}