1 <?php
if(!defined('WTRMLN_IS')) die;
2 /********************************************************************
6 Copyright 2008 Radosław Pietruszewski
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 version 2 as published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ********************************************************************/
23 header("Content-Type: text/html; charset=UTF-8");
25 ########################################
27 ########################################
29 include(WTRMLN_LIBS
. 'url.php');
30 include(WTRMLN_LIBS
. 'db.php');
31 include(WTRMLN_LIBS
. 'loader.php');
32 include(WTRMLN_LIBS
. 'plugins_handle.php');
33 include(WTRMLN_LIBS
. 'controller.php');
34 include(WTRMLN_LIBS
. 'model.php');
35 include(WTRMLN_LIBS
. 'plugin.php');
37 include(WTRMLN_HELPERS
. 'helpers.php');
39 ////////////////////////////////////////
42 /* zamiast die i exit w przypadku poważnego błędu.
43 ************************/
45 function panic($text = 'noname error')
47 die('<div style="position: absolute;
54 <big>Błąd krytyczny uniemożliwiający kontynuowanie.</big><br>Debug: ' . $text . '</div>');
60 * public static array $metaSrc
62 * dane meta (tagi z sekcji <head>)
64 * $metaSrc = array(string $head_element[, $head_element[, ... ]])
65 * $head_element - pojedynczy element do umieszczenia w sekcji <head>
67 public static $metaSrc = array();
77 * public void Watermelon(string $dbHost, string $dbUser, string $dbPass, string $dbName,
78 * string $dbPrefix, array $autoload, array $metaSrc)
80 * konstuktor. Odpala najważniejsze biblioteki, odpala odpowiedni kontroler
83 * string $dbHost - host bazy danych
84 * string $dbUser - użytkownik bazy danych
85 * string $dbPass - hasło do bazy danych
86 * string $dbName - nazwa bazy danych
87 * string $dbPrefix - prefiks do tabel
88 * array $autoload - pluginy i kod związany z nimi do automatycznego załadowania
89 * array $metaSrc - dane do wstawienia w sekcji <head>
91 * $autoload = array(array(string $plugin_name, string $eval)[, array(string $plugin_name, string $eval)[, ... ]]
92 * $plugin_name - nazwa plugina
93 * $eval - związany z tym pluginem kod do wykonania
95 * $metaSrc = array(string $head_element[, $head_element[, ... ]])
96 * $head_element - pojedynczy element do umieszczenia w sekcji <head>
99 public function Watermelon($dbHost, $dbUser, $dbPass, $dbName, $dbPrefix, $autoload, array $metaSrc)
101 $this->url
= new URL();
103 $db->connect($dbHost, $dbUser, $dbPass, $dbName, $dbPrefix);
105 $this->LoadPlugins($autoload);
107 self
::$metaSrc = $metaSrc;
109 $content = $this->loadController();
111 $this->generatePage($content);
115 * private void loadController()
117 * ładuje odpowiedni kontroler (wykonuje pracę Front Controllera)
120 private function loadController()
122 // zamieniamy _ na /, tak aby można było robić kontrolery w podfolderach
124 $_w_controllerPath = str_replace('_', '/', $this->url
->class);
126 $_w_controllerPath = WTRMLN_CONTROLLERS
. $_w_controllerPath . '.php';
128 // sprawdzanie, czy istnieje plik controllera
130 if(file_exists($_w_controllerPath))
132 include $_w_controllerPath;
134 array_shift(URL
::$segments);
135 array_shift(URL
::$segments);
139 //jeśli nie można znaleźć kontrolera, niech Pages przejmie stery
140 include WTRMLN_CONTROLLERS
. 'pages.php';
142 $_controller = new pages();
144 $this->url
->method
= 'index';
145 $this->url
->class = 'pages';
148 // sprawdzanie, czy istnieje klasa controllera
150 if(class_exists($this->url
->class))
152 $_controller = new $this->url
->class();
156 panic('Nie moge znalesc klasy podanego controllera (' . $this->url
->class . ')');
159 // sprawdzanie czy istnieje dana funkcja składowa controllera.
161 if(!method_exists($_controller, $this->url
->method
))
163 panic('Nie moge znalesc podanej funkcji składowej controllera (' . $this->url
->method
. ')');
166 // przystepujemy do roboty
168 $_controller->{$this->url
->method
}();
170 $content = ob_get_contents(); //wyciagamy dane z bufora wyjścia
177 * private void LoadPlugins(array $plugins)
179 * Ładuje pluginy i wykonuje kod związany z tymi pluginami
181 * $plugins = array(array(string $plugin_name, string $eval)[, array(string $plugin_name, string $eval)[, ... ]]
182 * $plugin_name - nazwa plugina
183 * $eval - związany z tym pluginem kod do wykonania
186 private function LoadPlugins(array $plugins)
188 foreach($plugins as $plugin)
190 list($plugin_name, $eval) = $plugin;
192 if(file_exists(WTRMLN_PLUGINS
. $plugin_name . '.php'))
194 include(WTRMLN_PLUGINS
. $plugin_name . '.php');
202 * private void generatePage(string $content)
204 * ostatecznie generuje stronę (dodaje znaczniki do head, oczyszcza treść itd.)
206 * string $content - treść wygenerowana przez kontroler
209 private function generatePage($content)
211 // umożliwiamy w prosty sposób tworzenie ścieżek do podstron
213 $content = str_replace('href="$/', 'href="' . WTRMLN_SITEURL
, $content);
214 $content = str_replace('action="$/', 'action="' . WTRMLN_SITEURL
, $content);
216 // preparujemy zawartość <title> :)
218 $siteTitle = (defined('WTRMLN_H1') ? WTRMLN_H1
. ' » ' : '') . WTRMLN_SITENAME
;
222 $metaSrc = Watermelon
::$metaSrc;
224 // żeby array_unshift się nie czepiał,
225 // gdyby wcześniej nie było żadnych elementów
232 // wsadzamy na początek tablicy <title>
234 array_unshift($metaSrc, '<title>' . $siteTitle . '</title>');
236 // zmieniamy nazwy, żeby skin.php wiedział odzochodzi
238 $_w_metaSrc = $metaSrc;
239 $_w_content = $content;
243 include WTRMLN_THEMEPATH
. 'skin.php';
247 new Watermelon($_w_dbHost, $_w_dbUser, $_w_dbPass, $_w_dbName, $_w_dbPrefix, $_w_autoload, $_w_metaSrc);