bbcode
[watermeloncms.git] / wtrmln / helpers / helpers.php
blob3f5acf5ca9f998de71b44b1e9f18dc685c168f6a
1 <?php
2 /********************************************************************
4 Watermelon CMS
6 Copyright 2008-2009 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 include 'gui.php';
24 include 'text.php';
25 include 'bbcode/bbcode.php';
28 * void setH1(string $value)
30 * ustawia nagłówek (nazwę) danej podstrony.
32 * string $value - nazwa podstrony
35 function setH1($value)
37 define('WTRMLN_H1',$value);
39 return '<h1>' . $value . '</h1>';
43 * object arrayToObject(array $array)
45 * Zamienia tablicę na obiekt
47 * array $array - tablica do zamiany na obiekt
50 function arrayToObject(array $array)
52 foreach($array as $key => $var)
54 if(is_array($var))
56 $object->$key = arrayToObject($var);
58 else
60 $object->$key = $var;
64 return $object;
68 * array objectToArray(object $object)
70 * Zamienia obiekt na tablicę
72 * object $object - obiekt do zamiany na tablicę
75 function objectToArray($object)
77 foreach($object as $key => $var)
79 if(is_object($var))
81 $array[$key] = objectToArray($var);
83 else
85 $array[$key] = $var;
89 return $array;
93 * string arrayToHTMLArguments(array $array)
94 * string objectToHTMLArguments(object $object)
96 * Zamiana tablicy lub obiektu na listę argumentów
97 * HTML/XML, np.
99 * array('foo1' => 'bar1', 'foo2' => 'bar2')
101 * zostanie zamienione na:
103 * 'foo1="bar1" foo2="bar2"'
106 function arrayToHTMLArguments($array)
108 $arguments = '';
110 foreach($array as $key => $var)
112 $arguments .= ' ' . $key . '="' . $var . '"';
115 $arguments = substr($arguments, 1);
117 return $arguments;
120 function objectToHTMLArguments($object)
122 return arrayToHTMLArguments($object);
125 /*********************************************/
127 /* zwraca element tablicy $_POST
128 ********************************/
130 function _POST($key)
132 return $_POST[$key];
136 /**********************************************/
139 * string site_url(string $url)
141 * Tworzy URL do danej podstrony
143 * string $url - podstrona, np.: 'blog/foo/bar/', albo '' - pusty
144 * string, zwróci URL do strony głównej
147 function site_url($url)
149 return WTRMLN_SITEURL . $url;
153 * void redirect(string $url)
155 * Przekierowuje na stronę o URL=$url
158 function redirect($url)
160 header('Location: ' . $url);
161 exit;
165 * string ClientIP()
167 * zwraca IP odwiedzającego.
169 * funkcja pochodzi oryginalnie z: http://php.org.pl/artykuly/3/22
172 function ClientIP()
174 $ip = 0;
176 if(!empty($_SERVER['HTTP_CLIENT_IP']))
178 $ip = $_SERVER['HTTP_CLIENT_IP'];
181 if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
183 $ipList = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
185 if($ip)
187 array_unshift($ipList, $ip);
188 $ip = 0;
191 foreach($ipList as $v)
193 if(!ereg('^(192\.168|172\.16|10|224|240|127|0)\.', $v))
195 return $v;
199 return $ip ? $ip : $_SERVER['REMOTE_ADDR'];
203 * string strHash(string $string[, string/int $algo])
205 * tworzy hash z $string
207 * jeśli $algo nie zostało podane:
209 * tworzy hash według domyślnego algorytmu haszującego
211 * jeśli $algo jest stringiem
213 * tworzy hash według nazwy $algo
215 * jeśli $algo jest intem
217 * tworzy hash na podstawie numeru algrorytmu haszującego
219 * string $string - tekst do zahaszowania
220 * string/int $algo - nazwa lub numer algorytmu haszującego
223 function strHash($string, $algo = NULL)
225 if($algo === NULL)
227 $algo = Config::$hashAlgo;
228 $algo = $algo[Config::$defaultHashAlgo];
230 elseif(is_int($algo))
232 $algo_id = $algo;
233 $algo = Config::$hashAlgo;
234 $algo = $algo[$algo_id];
237 $algoType = $algo[0];
239 $algo = substr($algo, 1);
241 // jeśli pierwszy znak to "x", używamy do haszowania funkcji hash. Jeśli
242 // inny - używamy standardowej funkcji (obecnie są chyba tylko trzy,
243 // może w PHP6 będzie więcej)
245 if($algoType == 'x')
247 return hash($algo, $string);
249 else
251 return $algo($string);