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) {
194 array('&', '"', "'", '<', '>'),
195 array('&', '"', ''', '<', '>'), $string));
198 if(! function_exists('login')) {
199 function login($register = false) {
201 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
204 if(x($_SESSION,'authenticated')) {
205 $o = file_get_contents("view/logout.tpl");
208 $o = file_get_contents("view/login.tpl");
210 $o = replace_macros($o,array('$register_html' => $register_html ));
216 if(! function_exists('autoname')) {
217 function autoname($len) {
219 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
220 if(mt_rand(0,5) == 4)
238 's','sc','sh','sm','sp','st',
246 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
247 'nd','ng','nk','nt','rn','rp','rt');
249 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
250 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
252 $start = mt_rand(0,2);
260 for ($x = 0; $x < $len; $x ++) {
261 $r = mt_rand(0,count($table) - 1);
264 if($table == $vowels)
265 $table = array_merge($cons,$midcons);
271 $word = substr($word,0,$len);
273 foreach($noend as $noe) {
274 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
275 $word = substr($word,0,-1);
279 if(substr($word,-1) == 'q')
280 $word = substr($word,0,-1);
284 if(! function_exists('killme')) {
286 session_write_close();
290 if(! function_exists('goaway')) {
291 function goaway($s) {
292 header("Location: $s");
297 if(! function_exists('xml_status')) {
298 function xml_status($st) {
299 header( "Content-type: text/xml");
300 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
301 echo "<result><status>$st</status></result>\r\n";
305 if(! function_exists('local_user')) {
306 function local_user() {
307 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
308 return $_SESSION['uid'];
312 if(! function_exists('remote_user')) {
313 function remote_user() {
314 if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
315 return $_SESSION['cid'];
319 function footer(&$a) {
321 $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
322 $a->page['content'] .= "<div class=\"fortune\" >$s</div>";
325 if(! function_exists('notice')) {
326 function notice($s) {
328 $_SESSION['sysmsg'] .= $s;
333 if(! function_exists('xmlify')) {
334 function xmlify($str) {
337 for($x = 0; $x < strlen($str); $x ++) {
368 $buffer = trim($buffer);
373 function hex2bin($s) {
374 return(pack("H*",$s));
378 function paginate(&$a) {
380 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
381 $stripped = str_replace('q=','',$stripped);
382 $stripped = trim($stripped,'/');
383 $url = $a->get_baseurl() . '/' . $stripped;
386 if($a->pager['total'] > $a->pager['itemspage']) {
387 $o .= '<div class="pager">';
388 if($a->pager['page'] != 1)
389 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
391 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
393 $numpages = $a->pager['total'] / $a->pager['itemspage'];
396 $numstop = $numpages;
399 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
400 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
403 for($i = $numstart; $i <= $numstop; $i++){
404 if($i == $a->pager['page'])
405 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
407 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
411 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
412 if($i == $a->pager['page'])
413 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
415 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
419 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
420 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
422 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
423 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
424 $o .= '</div>'."\r\n";