2024.8.23
Loading...
Searching...
No Matches
class.homepage.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (c) 2021-2024 Bearsampp
4 * License: GNU General Public License version 3 or later; see LICENSE.txt
5 * Author: Bear
6 * Website: https://bearsampp.com
7 * Github: https://github.com/Bearsampp
8 */
9
10/**
11 * Class Homepage
12 *
13 * This class handles the homepage functionalities of the Bearsampp application.
14 * It manages the page navigation, resource paths, and content refresh operations.
15 */
17{
18 const PAGE_INDEX = 'index';
19 const PAGE_PHPINFO = 'phpinfo';
20 const PAGE_STDL_APC = 'apc.php';
21
22 private $page;
23
24 /**
25 * @var array List of valid pages for the homepage.
26 */
27 private $pageList = array(
28 self::PAGE_INDEX,
29 self::PAGE_PHPINFO,
30 );
31
32 /**
33 * @var array List of standard pages for the homepage.
34 */
35 private $pageStdl = array(
36 self::PAGE_STDL_APC
37 );
38
39 /**
40 * Homepage constructor.
41 * Initializes the homepage class and sets the current page based on the query parameter.
42 */
43 public function __construct()
44 {
45 Util::logInitClass($this);
46
48 $this->page = !empty($page) && in_array($page, $this->pageList) ? $page : self::PAGE_INDEX;
49 }
50
51 /**
52 * Gets the current page.
53 *
54 * @return string The current page.
55 */
56 public function getPage()
57 {
58 return $this->page;
59 }
60
61 /**
62 * Constructs the page query string based on the provided query.
63 *
64 * @param string $query The query string to construct.
65 * @return string The constructed page query string.
66 */
67 public function getPageQuery($query)
68 {
69 $request = '';
70 if (!empty($query) && in_array($query, $this->pageList) && $query != self::PAGE_INDEX) {
71 $request = '?p=' . $query;
72 } elseif (!empty($query) && in_array($query, $this->pageStdl)) {
73 $request = $query;
74 } elseif (!empty($query) && self::PAGE_INDEX) {
75 $request = "index.php";
76 }
77 return $request;
78 }
79
80 /**
81 * Constructs the full URL for the given page query.
82 *
83 * @param string $query The query string to construct the URL for.
84 * @return string The constructed page URL.
85 */
86 public function getPageUrl($query)
87 {
88 global $bearsamppRoot;
89 return $bearsamppRoot->getLocalUrl($this->getPageQuery($query));
90 }
91
92 /**
93 * Gets the path to the homepage directory.
94 *
95 * @return string The homepage directory path.
96 */
97 public function getHomepagePath()
98 {
99 global $bearsamppCore;
100 return $bearsamppCore->getResourcesPath(false) . '/homepage';
101 }
102
103 /**
104 * Gets the path to the images directory.
105 *
106 * @return string The images directory path.
107 */
108 public function getImagesPath()
109 {
110 return $this->getResourcesPath(false) . '/img/';
111 }
112
113 /**
114 * Gets the path to the icons directory.
115 *
116 * @return string The icons directory path.
117 */
118 public function getIconsPath()
119 {
120 return $this->getResourcesPath(false) . '/img/icons/';
121 }
122
123 /**
124 * Gets the path to the resources directory.
125 *
126 * @return string The resources directory path.
127 */
128 public function getResourcesPath()
129 {
130 global $bearsamppCore;
131 return md5(APP_TITLE);
132 }
133
134 /**
135 * Gets the URL to the resources directory.
136 *
137 * @return string The resources directory URL.
138 */
139 public function getResourcesUrl()
140 {
141 global $bearsamppRoot;
142 return $bearsamppRoot->getLocalUrl($this->getResourcesPath());
143 }
144
145 /**
146 * Refreshes the alias content by updating the alias configuration file.
147 *
148 * @return bool True if the alias content was successfully refreshed, false otherwise.
149 */
150 public function refreshAliasContent()
151 {
152 global $bearsamppBins;
153
154 $result = $bearsamppBins->getApache()->getAliasContent(
155 $this->getResourcesPath(),
156 $this->getHomepagePath()
157 );
158
159 return file_put_contents($this->getHomepagePath() . '/alias.conf', $result) !== false;
160 }
161
162 /**
163 * Refreshes the commons JavaScript content by updating the _commons.js file.
164 */
165 public function refreshCommonsJsContent()
166 {
167 Util::replaceInFile($this->getHomepagePath() . '/js/_commons.js', array(
168 '/^\s\surl:.*/' => ' url: "' . $this->getResourcesPath() . '/ajax.php"',
169 '/AJAX_URL.*=.*/' => 'const AJAX_URL = "' . $this->getResourcesPath() . '/ajax.php"',
170 ));
171 }
172}
$result
global $bearsamppBins
global $bearsamppRoot
global $bearsamppCore
getPageQuery($query)
const PAGE_STDL_APC
refreshCommonsJsContent()
const PAGE_INDEX
const PAGE_PHPINFO
getPageUrl($query)
static replaceInFile($path, $replaceList)
static cleanGetVar($name, $type='text')
static logInitClass($classInstance)
const APP_TITLE
Definition root.php:12