5 define('EOL', "<br />\r\n");
7 define('REGISTER_CLOSED', 0);
8 define('REGISTER_APPROVE', 1);
9 define('REGISTER_OPEN', 2);
11 define ( 'DIRECTION_ANY', 0);
12 define ( 'DIRECTION_IN', 1);
13 define ( 'DIRECTION_OUT', 2);
14 define ( 'DIRECTION_BOTH', 3);
16 if(! class_exists('App')) {
19 public $module_loaded = false;
27 public $error = false;
40 function __construct() {
42 $this->config = array();
43 $this->page = array();
44 $this->pager= array();
46 $this->scheme = ((isset($_SERVER['HTTPS'])
47 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
48 $this->hostname = str_replace('www.','',
49 $_SERVER['SERVER_NAME']);
50 set_include_path("include/$this->hostname"
51 . PATH_SEPARATOR . 'include'
52 . PATH_SEPARATOR . '.' );
54 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
55 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
56 $this->cmd = trim($_GET['q'],'/');
59 $this->argv = explode('/',$this->cmd);
60 $this->argc = count($this->argv);
61 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
62 $this->module = $this->argv[0];
65 $this->module = 'home';
67 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
68 $this->pager['itemspage'] = 50;
69 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
70 $this->pager['total'] = 0;
73 function get_baseurl($ssl = false) {
74 if(strlen($this->baseurl))
75 return $this->baseurl;
77 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
78 . ((isset($this->path) && strlen($this->path))
79 ? '/' . $this->path : '' );
80 return $this->baseurl;
83 function set_baseurl($url) {
84 $this->baseurl = $url;
87 function get_hostname() {
88 return $this->hostname;
91 function set_hostname($h) {
95 function set_path($p) {
96 $this->path = ltrim(trim($p),'/');
99 function set_pager_total($n) {
100 $this->pager['total'] = intval($n);
102 function set_pager_itemspage($n) {
103 $this->pager['itemspage'] = intval($n);
104 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
108 function init_pagehead() {
109 if(file_exists("view/head.tpl"))
110 $s = file_get_contents("view/head.tpl");
111 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
117 if(! function_exists('x')) {
118 function x($s,$k = NULL) {
120 if((is_array($s)) && (array_key_exists($k,$s))) {
138 if(! function_exists('system_unavailable')) {
139 function system_unavailable() {
140 include('system_unavailable.php');
144 if(! function_exists('replace_macros')) {
145 function replace_macros($s,$r) {
150 if(is_array($r) && count($r)) {
151 foreach ($r as $k => $v ) {
156 return str_replace($search,$replace,$s);
161 if(! function_exists('fetch_url')) {
162 function fetch_url($url,$binary = false) {
163 $ch = curl_init($url);
164 if(! $ch) return false;
166 curl_setopt($ch, CURLOPT_HEADER, 0);
167 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
168 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
169 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
171 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
179 if(! function_exists('post_url')) {
180 function post_url($url,$params) {
181 $ch = curl_init($url);
182 if(! $ch) return false;
184 curl_setopt($ch, CURLOPT_HEADER, 0);
185 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
186 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
187 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
188 curl_setopt($ch, CURLOPT_POST,1);
189 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
197 if(! function_exists('random_string')) {
198 function random_string() {
199 return(hash('sha256',uniqid(rand(),true)));
202 if(! function_exists('notags')) {
203 function notags($string) {
204 // protect against :<> with high-bit set
205 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
208 // The PHP built-in tag escape function has traditionally been buggy
209 if(! function_exists('escape_tags')) {
210 function escape_tags($string) {
212 array('&', '"', "'", '<', '>'),
213 array('&', '"', ''', '<', '>'), $string));
216 if(! function_exists('login')) {
217 function login($register = false) {
219 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
222 if(x($_SESSION,'authenticated')) {
223 $o = file_get_contents("view/logout.tpl");
226 $o = file_get_contents("view/login.tpl");
228 $o = replace_macros($o,array('$register_html' => $register_html ));
234 if(! function_exists('autoname')) {
235 function autoname($len) {
237 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
238 if(mt_rand(0,5) == 4)
256 's','sc','sh','sm','sp','st',
264 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
265 'nd','ng','nk','nt','rn','rp','rt');
267 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
268 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
270 $start = mt_rand(0,2);
278 for ($x = 0; $x < $len; $x ++) {
279 $r = mt_rand(0,count($table) - 1);
282 if($table == $vowels)
283 $table = array_merge($cons,$midcons);
289 $word = substr($word,0,$len);
291 foreach($noend as $noe) {
292 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
293 $word = substr($word,0,-1);
297 if(substr($word,-1) == 'q')
298 $word = substr($word,0,-1);
302 if(! function_exists('killme')) {
304 session_write_close();
308 if(! function_exists('goaway')) {
309 function goaway($s) {
310 header("Location: $s");
315 if(! function_exists('xml_status')) {
316 function xml_status($st) {
317 header( "Content-type: text/xml" );
318 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
319 echo "<result><status>$st</status></result>\r\n";
323 if(! function_exists('local_user')) {
324 function local_user() {
325 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
326 return $_SESSION['uid'];
330 if(! function_exists('remote_user')) {
331 function remote_user() {
332 if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
333 return $_SESSION['cid'];
337 function footer(&$a) {
339 $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
340 $a->page['content'] .= "<div class=\"fortune\" >$s</div>";
343 if(! function_exists('notice')) {
344 function notice($s) {
346 $_SESSION['sysmsg'] .= $s;
350 if(! function_exists('get_max_import_size')) {
351 function get_max_import_size() {
353 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
356 if(! function_exists('xmlify')) {
357 function xmlify($str) {
360 for($x = 0; $x < strlen($str); $x ++) {
391 $buffer = trim($buffer);
395 function unxmlify($s) {
396 $ret = str_replace('&','&', $s);
397 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
401 function hex2bin($s) {
402 return(pack("H*",$s));
406 function paginate(&$a) {
408 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
409 $stripped = str_replace('q=','',$stripped);
410 $stripped = trim($stripped,'/');
411 $url = $a->get_baseurl() . '/' . $stripped;
414 if($a->pager['total'] > $a->pager['itemspage']) {
415 $o .= '<div class="pager">';
416 if($a->pager['page'] != 1)
417 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
419 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
421 $numpages = $a->pager['total'] / $a->pager['itemspage'];
424 $numstop = $numpages;
427 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
428 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
431 for($i = $numstart; $i <= $numstop; $i++){
432 if($i == $a->pager['page'])
433 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
435 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
439 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
440 if($i == $a->pager['page'])
441 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
443 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
447 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
448 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
450 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
451 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
452 $o .= '</div>'."\r\n";
457 function expand_acl($s) {
460 $a = explode('<',$s);
461 for($x = 0; $x < count($a); $x ++) {
462 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));