Page.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * Page superclass file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. /**
  12. * Page superclass.
  13. *
  14. * Every visitable page type must inherit from this one.
  15. *
  16. * @category Page
  17. */
  18. abstract class Page{
  19. /**
  20. * @var bool Indicates if the page can be viewed publicly, os it's only for the owner.
  21. */
  22. private $public = false;
  23. /**
  24. * @var string[] List of CSS files required for the page.
  25. */
  26. private $css = [];
  27. /**
  28. * @var string Path to the view associated with the page.
  29. */
  30. private $view;
  31. /**
  32. * @var string Title of the page, in the defined language.
  33. */
  34. private $title;
  35. /**
  36. * @var string Site name.
  37. */
  38. private $name;
  39. /**
  40. * @var string Page description, in the defined language.
  41. */
  42. private $description;
  43. /**
  44. * @var string URL to the site favicon.
  45. */
  46. private $favicon;
  47. /**
  48. * @var string URL to the page icon or main image.
  49. */
  50. private $icon;
  51. /**
  52. * @var string Canonical URL.
  53. */
  54. private $canonical;
  55. /**
  56. * @var string Autjhor URL.
  57. */
  58. private $author;
  59. /**
  60. * @var integer HTTP status code.
  61. */
  62. private $code = 0;
  63. /**
  64. * @var string HTTP status message.
  65. */
  66. private $message = "";
  67. /**
  68. * Constructor.
  69. */
  70. public function __construct(){
  71. $this->favicon = URL::IMG["LOGO"] . "sw.png";
  72. $this->icon = URL::IMG["LOGO"] . "sw.png";
  73. $this->name = "SWDB";
  74. $this->author = URL::BASE;
  75. $this->add_css("ui.css");
  76. if (get_context()->get_game_mode() == GAME_MODE_ID::RTA){
  77. $this->add_css("ui-rta.css");
  78. }
  79. }
  80. /**
  81. * Sets the page visibility to public or private.
  82. *
  83. * A public page is considered if one of the following conditions apply.
  84. * - Doesn't present info from any player (i.e. static pages, such as the catalog.)
  85. * - When refering to a plblic profile, it can be accessed by users other than the owner, or
  86. * unregistered users.
  87. *
  88. * Note that for the second condition, this must be set to true even if the player profile
  89. * is private.
  90. *
  91. * @param bool True for public pages, false for private ones.
  92. */
  93. protected function set_public($public){
  94. if ($public === true){
  95. $this->public = true;
  96. }
  97. else{
  98. $this->public = false;
  99. }
  100. }
  101. /**
  102. * Checks if the page is public.
  103. *
  104. * A public page is considered if one of the following conditions apply.
  105. * - Doesn't present info from any player (i.e. static pages, such as the catalog.)
  106. * - When refering to a plblic profile, it can be accessed by users other than the owner, or
  107. * unregistered users.
  108. *
  109. * Note that for the second condition, this will still return true even if the player profile
  110. * is private.
  111. *
  112. * @return bool True for public pages, false for private ones.
  113. */
  114. public function is_public(){
  115. return $this->public;
  116. }
  117. /**
  118. * Adds a CSS file to the list.
  119. *
  120. * @param string $css Css file. Can be the absolute URL, or just the filename.
  121. */
  122. protected function add_css($css){
  123. if (strripos($css, URL::CSS) === false){
  124. $file = URL::CSS . $css;
  125. }
  126. else{
  127. $file = $css;
  128. }
  129. array_push($this->css, $file);
  130. }
  131. /**
  132. * Retrieves the CSS file URLs for the page.
  133. *
  134. * @return string[] CSS files (by absolute URL.)
  135. */
  136. public function get_css(){
  137. return $this->css;
  138. }
  139. /**
  140. * Sets the page view.
  141. *
  142. * @param string $view View file. Can be the relative path, or just the filename.
  143. */
  144. protected function set_view($view){
  145. if (strripos($view, PATH::VIEW) === false){
  146. $file = PATH::VIEW . $view;
  147. }
  148. else{
  149. $file = $view;
  150. }
  151. $this->view = $file;
  152. }
  153. /**
  154. * Retrieves the page view file.
  155. *
  156. * @return string Path to the view file.
  157. */
  158. public function get_view(){
  159. return $this->view;
  160. }
  161. /**
  162. * Sets the page title.
  163. *
  164. * The string " - SWDB" will always be added at the end.
  165. *
  166. * @param string $title Page title.
  167. */
  168. protected function set_title($title){
  169. $this->title = htmlentities($title, ENT_QUOTES) . " - SWDB";
  170. }
  171. /**
  172. * Retrieves the page title.
  173. *
  174. * @return string Page title.
  175. */
  176. public function get_title(){
  177. return $this->title;
  178. }
  179. /**
  180. * Retrieves the site name.
  181. *
  182. * @return string Site name.
  183. */
  184. public function get_name(){
  185. return $this->name;
  186. }
  187. /**
  188. * Sets the page description.
  189. *
  190. * The string " - Summoners War DataBase" will always be added at the end.
  191. *
  192. * @param string $description Page description text.
  193. */
  194. protected function set_description($description){
  195. $this->description = htmlentities($description, ENT_QUOTES) . " - Summoners War DataBase";
  196. }
  197. /**
  198. * Retrieves the page description
  199. *
  200. * @return string Page description.
  201. */
  202. public function get_description(){
  203. return $this->description;
  204. }
  205. /**
  206. * Retrieves the URL to the favicon.
  207. *
  208. * @return string Favicon URL.
  209. */
  210. public function get_favicon(){
  211. return $this->favicon;
  212. }
  213. /**
  214. * Retrieves the URL to the thumbnail image.
  215. *
  216. * @return string Thmbnail icon URL.
  217. */
  218. public function get_icon(){
  219. return $this->icon;
  220. }
  221. /**
  222. * Sets the page canonical URL.
  223. *
  224. * @param string $canonical Canonical URL, absolute or relative.
  225. */
  226. protected function set_canonical($canonical){
  227. if (strripos($canonical, URL::BASE) === false){
  228. $file = URL::BASE . $canonical;
  229. }
  230. else{
  231. $file = $canonical;
  232. }
  233. $this->canonical = $file;
  234. }
  235. /**
  236. * Retrieves the canonical URL to the page.
  237. *
  238. * @return string Canonical URL.
  239. */
  240. public function get_canonical(){
  241. return $this->canonical;
  242. }
  243. /**
  244. * Retrieves the author URL (site main URL).
  245. *
  246. * @return string AUthor URL.
  247. */
  248. public function get_author(){
  249. return $this->author;
  250. }
  251. /**
  252. * Sets the HTTP status code for the page to return.
  253. *
  254. * @param int HTTP status code.
  255. */
  256. protected function set_code($code){
  257. if (is_int($code) && $code >= 200 && $code <= 500){
  258. $this->code = $code;
  259. }
  260. }
  261. /**
  262. * Retrieves the page HTTP status code.
  263. *
  264. * @return int HTTP status code
  265. */
  266. public function get_code(){
  267. return $this->code;
  268. }
  269. /**
  270. * Sets the HTTP status message for the page to return.
  271. *
  272. * @param string HTTP status message.
  273. */
  274. protected function set_message($mesage){
  275. $this->message = strval($mesage);
  276. }
  277. /**
  278. * Retrieves the page HTTP status message.
  279. *
  280. * @return string HTTP status code
  281. */
  282. public function get_message(){
  283. return $this->message;
  284. }
  285. /**
  286. * Generates the content to be inserted in the HTML <head> section.
  287. *
  288. * It must go between the <head> and </head> tags, and anything can go behind it before closing
  289. * the section. It includes page title, description, css files and metadata.
  290. *
  291. * @return string HTML head content.
  292. */
  293. public function generate_head(){
  294. $head = "
  295. <meta content='text/html; charset=utf-8' http-equiv='content-type'/>
  296. <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1'/>
  297. <title>" . $this->get_title() . "</title>
  298. <link rel='shortcut icon' href='" . $this->get_favicon() . "'/>
  299. <!-- CSS files -->";
  300. foreach ($this->get_css() as $css){
  301. $head .= "
  302. <link rel='stylesheet' type='text/css' href='$css'/>";
  303. }
  304. $head .= "
  305. <!-- Meta tags -->
  306. <link rel='canonical' href='" . $this->get_canonical() . "'/>
  307. <link rel='author' href='" . $this->get_author() . "'/>
  308. <link rel='publisher' href='" . $this->get_author() . "'/>
  309. <meta name='description' content='" . $this->get_description() . "'/>
  310. <meta property='og:title' content='" . $this->get_title() . "'/>
  311. <meta property='og:url' content='" . $this->get_canonical() . "'/>
  312. <meta property='og:description' content='" . $this->get_description() . "'/>
  313. <meta property='og:image' content='" . $this->get_icon() . "'/>
  314. <meta property='og:site_name' content='" . $this->get_name() . "'/>
  315. <meta property='og:type' content='website'/>
  316. <meta property='og:locale' content='en'/>
  317. <meta name='twitter:card' content='summary'/>
  318. <meta name='twitter:title' content='" . $this->get_title() . "'/>
  319. <meta name='twitter:description' content='" . $this->get_description() . "'/>
  320. <meta name='twitter:image' content='" . $this->get_icon() . "'/>
  321. <meta name='twitter:url' content='" . $this->get_canonical() . "'/>
  322. <meta name='robots' content='index follow'/>\n";
  323. return $head;
  324. }
  325. }