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;
26 public $error = false;
37 function __construct() {
39 $this->config = array();
40 $this->page = array();
41 $this->pager= array();
43 $this->scheme = ((isset($_SERVER['HTTPS'])
44 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
45 $this->hostname = str_replace('www.','',
46 $_SERVER['SERVER_NAME']);
47 set_include_path("include/$this->hostname"
48 . PATH_SEPARATOR . 'include'
49 . PATH_SEPARATOR . '.' );
51 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
52 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
53 $this->cmd = trim($_GET['q'],'/');
56 $this->argv = explode('/',$this->cmd);
57 $this->argc = count($this->argv);
58 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
59 $this->module = $this->argv[0];
62 $this->module = 'home';
64 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
65 $this->pager['itemspage'] = 50;
66 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
67 $this->pager['total'] = 0;
70 function get_baseurl($ssl = false) {
72 return (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
73 . ((isset($this->path) && strlen($this->path))
74 ? '/' . $this->path : '' );
77 function set_path($p) {
78 $this->path = ltrim(trim($p),'/');
81 function set_pager_total($n) {
82 $this->pager['total'] = intval($n);
84 function set_pager_itemspage($n) {
85 $this->pager['itemspage'] = intval($n);
86 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
90 function init_pagehead() {
91 if(file_exists("view/head.tpl"))
92 $s = file_get_contents("view/head.tpl");
93 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
99 if(! function_exists('x')) {
100 function x($s,$k = NULL) {
102 if((is_array($s)) && (array_key_exists($k,$s))) {
120 if(! function_exists('system_unavailable')) {
121 function system_unavailable() {
122 include('system_unavailable.php');
126 if(! function_exists('replace_macros')) {
127 function replace_macros($s,$r) {
132 if(is_array($r) && count($r)) {
133 foreach ($r as $k => $v ) {
138 return str_replace($search,$replace,$s);
143 if(! function_exists('fetch_url')) {
144 function fetch_url($url,$binary = false) {
145 $ch = curl_init($url);
146 if(! $ch) return false;
148 curl_setopt($ch, CURLOPT_HEADER, 0);
149 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
150 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
151 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
153 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
161 if(! function_exists('post_url')) {
162 function post_url($url,$params) {
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);
170 curl_setopt($ch, CURLOPT_POST,1);
171 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
179 if(! function_exists('random_string')) {
180 function random_string() {
181 return(hash('sha256',uniqid(rand(),true)));
184 if(! function_exists('notags')) {
185 function notags($string) {
186 // protect against :<> with high-bit set
187 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
190 // The PHP built-in tag escape function has traditionally been buggy
191 if(! function_exists('escape_tags')) {
192 function escape_tags($string) {
193 return(str_replace(array("<",">","&"), array('<','>','&'), $string));
196 if(! function_exists('login')) {
197 function login($register = false) {
199 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
202 if(x($_SESSION,'authenticated')) {
203 $o = file_get_contents("view/logout.tpl");
206 $o = file_get_contents("view/login.tpl");
208 $o = replace_macros($o,array('$register_html' => $register_html ));
214 if(! function_exists('autoname')) {
215 function autoname($len) {
217 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
218 if(mt_rand(0,5) == 4)
236 's','sc','sh','sm','sp','st',
244 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
245 'nd','ng','nk','nt','rn','rp','rt');
247 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
248 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
250 $start = mt_rand(0,2);
258 for ($x = 0; $x < $len; $x ++) {
259 $r = mt_rand(0,count($table) - 1);
262 if($table == $vowels)
263 $table = array_merge($cons,$midcons);
269 $word = substr($word,0,$len);
271 foreach($noend as $noe) {
272 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
273 $word = substr($word,0,-1);
277 if(substr($word,-1) == 'q')
278 $word = substr($word,0,-1);
282 if(! function_exists('killme')) {
284 session_write_close();
288 if(! function_exists('goaway')) {
289 function goaway($s) {
290 header("Location: $s");
295 if(! function_exists('xml_status')) {
296 function xml_status($st) {
297 header( "Content-type: text/xml");
298 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
299 echo "<result><status>$st</status></result>\r\n";
303 if(! function_exists('local_user')) {
304 function local_user() {
305 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
306 return $_SESSION['uid'];
310 if(! function_exists('remote_user')) {
311 function remote_user() {
312 if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
313 return $_SESSION['cid'];
317 function footer(&$a) {
319 $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
320 $a->page['content'] .= "<div class=\"fortune\" >$s</div>";
323 if(! function_exists('notice')) {
324 function notice($s) {
326 $_SESSION['sysmsg'] .= $s;
331 if(! function_exists('xmlify')) {
332 function xmlify($str) {
335 for($x = 0; $x < strlen($str); $x ++) {
366 $buffer = trim($buffer);
371 function hex2bin($s) {
372 return(pack("H*",$s));
376 function paginate(&$a) {
378 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
379 $stripped = str_replace('q=','',$stripped);
380 $stripped = trim($stripped,'/');
381 $url = $a->get_baseurl() . '/' . $stripped;
384 if($a->pager['total'] > $a->pager['itemspage']) {
385 $o .= '<div class="pager">';
386 if($a->pager['page'] != 1)
387 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
389 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
391 $numpages = $a->pager['total'] / $a->pager['itemspage'];
394 $numstop = $numpages;
397 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
398 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
401 for($i = $numstart; $i <= $numstop; $i++){
402 if($i == $a->pager['page'])
403 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
405 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
409 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
410 if($i == $a->pager['page'])
411 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
413 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
417 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
418 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
420 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
421 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
422 $o .= '</div>'."\r\n";