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;
39 function __construct() {
41 $this->config = array();
42 $this->page = array();
43 $this->pager= array();
45 $this->scheme = ((isset($_SERVER['HTTPS'])
46 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
47 $this->hostname = str_replace('www.','',
48 $_SERVER['SERVER_NAME']);
49 set_include_path("include/$this->hostname"
50 . PATH_SEPARATOR . 'include'
51 . PATH_SEPARATOR . '.' );
53 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
54 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
55 $this->cmd = trim($_GET['q'],'/');
58 $this->argv = explode('/',$this->cmd);
59 $this->argc = count($this->argv);
60 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
61 $this->module = $this->argv[0];
64 $this->module = 'home';
66 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
67 $this->pager['itemspage'] = 50;
68 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
69 $this->pager['total'] = 0;
72 function get_baseurl($ssl = false) {
73 if(strlen($this->baseurl))
74 return $this->baseurl;
76 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
77 . ((isset($this->path) && strlen($this->path))
78 ? '/' . $this->path : '' );
79 return $this->baseurl;
82 function set_baseurl($url) {
83 $this->baseurl = $url;
87 function set_path($p) {
88 $this->path = ltrim(trim($p),'/');
91 function set_pager_total($n) {
92 $this->pager['total'] = intval($n);
94 function set_pager_itemspage($n) {
95 $this->pager['itemspage'] = intval($n);
96 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
100 function init_pagehead() {
101 if(file_exists("view/head.tpl"))
102 $s = file_get_contents("view/head.tpl");
103 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
109 if(! function_exists('x')) {
110 function x($s,$k = NULL) {
112 if((is_array($s)) && (array_key_exists($k,$s))) {
130 if(! function_exists('system_unavailable')) {
131 function system_unavailable() {
132 include('system_unavailable.php');
136 if(! function_exists('replace_macros')) {
137 function replace_macros($s,$r) {
142 if(is_array($r) && count($r)) {
143 foreach ($r as $k => $v ) {
148 return str_replace($search,$replace,$s);
153 if(! function_exists('fetch_url')) {
154 function fetch_url($url,$binary = false) {
155 $ch = curl_init($url);
156 if(! $ch) return false;
158 curl_setopt($ch, CURLOPT_HEADER, 0);
159 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
160 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
161 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
163 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
171 if(! function_exists('post_url')) {
172 function post_url($url,$params) {
173 $ch = curl_init($url);
174 if(! $ch) return false;
176 curl_setopt($ch, CURLOPT_HEADER, 0);
177 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
178 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
179 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
180 curl_setopt($ch, CURLOPT_POST,1);
181 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
189 if(! function_exists('random_string')) {
190 function random_string() {
191 return(hash('sha256',uniqid(rand(),true)));
194 if(! function_exists('notags')) {
195 function notags($string) {
196 // protect against :<> with high-bit set
197 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
200 // The PHP built-in tag escape function has traditionally been buggy
201 if(! function_exists('escape_tags')) {
202 function escape_tags($string) {
204 array('&', '"', "'", '<', '>'),
205 array('&', '"', ''', '<', '>'), $string));
208 if(! function_exists('login')) {
209 function login($register = false) {
211 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
214 if(x($_SESSION,'authenticated')) {
215 $o = file_get_contents("view/logout.tpl");
218 $o = file_get_contents("view/login.tpl");
220 $o = replace_macros($o,array('$register_html' => $register_html ));
226 if(! function_exists('autoname')) {
227 function autoname($len) {
229 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
230 if(mt_rand(0,5) == 4)
248 's','sc','sh','sm','sp','st',
256 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
257 'nd','ng','nk','nt','rn','rp','rt');
259 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
260 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
262 $start = mt_rand(0,2);
270 for ($x = 0; $x < $len; $x ++) {
271 $r = mt_rand(0,count($table) - 1);
274 if($table == $vowels)
275 $table = array_merge($cons,$midcons);
281 $word = substr($word,0,$len);
283 foreach($noend as $noe) {
284 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
285 $word = substr($word,0,-1);
289 if(substr($word,-1) == 'q')
290 $word = substr($word,0,-1);
294 if(! function_exists('killme')) {
296 session_write_close();
300 if(! function_exists('goaway')) {
301 function goaway($s) {
302 header("Location: $s");
307 if(! function_exists('xml_status')) {
308 function xml_status($st) {
309 header( "Content-type: text/xml");
310 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
311 echo "<result><status>$st</status></result>\r\n";
315 if(! function_exists('local_user')) {
316 function local_user() {
317 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
318 return $_SESSION['uid'];
322 if(! function_exists('remote_user')) {
323 function remote_user() {
324 if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
325 return $_SESSION['cid'];
329 function footer(&$a) {
331 $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
332 $a->page['content'] .= "<div class=\"fortune\" >$s</div>";
335 if(! function_exists('notice')) {
336 function notice($s) {
338 $_SESSION['sysmsg'] .= $s;
342 if(! function_exists('get_max_import_size')) {
343 function get_max_import_size() {
345 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
348 if(! function_exists('xmlify')) {
349 function xmlify($str) {
352 for($x = 0; $x < strlen($str); $x ++) {
383 $buffer = trim($buffer);
387 function unxmlify($s) {
388 $ret = str_replace('&','&', $s);
389 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
393 function hex2bin($s) {
394 return(pack("H*",$s));
398 function paginate(&$a) {
400 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
401 $stripped = str_replace('q=','',$stripped);
402 $stripped = trim($stripped,'/');
403 $url = $a->get_baseurl() . '/' . $stripped;
406 if($a->pager['total'] > $a->pager['itemspage']) {
407 $o .= '<div class="pager">';
408 if($a->pager['page'] != 1)
409 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
411 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
413 $numpages = $a->pager['total'] / $a->pager['itemspage'];
416 $numstop = $numpages;
419 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
420 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
423 for($i = $numstart; $i <= $numstop; $i++){
424 if($i == $a->pager['page'])
425 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
427 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
431 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
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 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
440 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
442 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
443 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
444 $o .= '</div>'."\r\n";
449 function expand_acl($s) {
452 $a = explode('<',$s);
453 for($x = 0; $x < count($a); $x ++) {
454 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));