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),'/');
103 function set_pager_total($n) {
104 $this->pager['total'] = intval($n);
107 function set_pager_itemspage($n) {
108 $this->pager['itemspage'] = intval($n);
109 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
113 function init_pagehead() {
114 if(file_exists("view/head.tpl"))
115 $s = file_get_contents("view/head.tpl");
116 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
122 if(! function_exists('x')) {
123 function x($s,$k = NULL) {
125 if((is_array($s)) && (array_key_exists($k,$s))) {
143 if(! function_exists('system_unavailable')) {
144 function system_unavailable() {
145 include('system_unavailable.php');
149 if(! function_exists('replace_macros')) {
150 function replace_macros($s,$r) {
155 if(is_array($r) && count($r)) {
156 foreach ($r as $k => $v ) {
161 return str_replace($search,$replace,$s);
166 if(! function_exists('fetch_url')) {
167 function fetch_url($url,$binary = false) {
168 $ch = curl_init($url);
169 if(! $ch) return false;
171 curl_setopt($ch, CURLOPT_HEADER, 0);
172 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
173 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
174 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
176 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
184 if(! function_exists('post_url')) {
185 function post_url($url,$params) {
186 $ch = curl_init($url);
187 if(! $ch) return false;
189 curl_setopt($ch, CURLOPT_HEADER, 0);
190 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
191 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
192 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
193 curl_setopt($ch, CURLOPT_POST,1);
194 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
202 if(! function_exists('random_string')) {
203 function random_string() {
204 return(hash('sha256',uniqid(rand(),true)));
207 if(! function_exists('notags')) {
208 function notags($string) {
209 // protect against :<> with high-bit set
210 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
213 if(! function_exists('escape_tags')) {
214 function escape_tags($string) {
216 return(htmlspecialchars($string));
219 if(! function_exists('login')) {
220 function login($register = false) {
222 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
225 if(x($_SESSION,'authenticated')) {
226 $o = file_get_contents("view/logout.tpl");
229 $o = file_get_contents("view/login.tpl");
231 $o = replace_macros($o,array('$register_html' => $register_html ));
237 if(! function_exists('autoname')) {
238 function autoname($len) {
240 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
241 if(mt_rand(0,5) == 4)
259 's','sc','sh','sm','sp','st',
267 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
268 'nd','ng','nk','nt','rn','rp','rt');
270 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
271 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
273 $start = mt_rand(0,2);
281 for ($x = 0; $x < $len; $x ++) {
282 $r = mt_rand(0,count($table) - 1);
285 if($table == $vowels)
286 $table = array_merge($cons,$midcons);
292 $word = substr($word,0,$len);
294 foreach($noend as $noe) {
295 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
296 $word = substr($word,0,-1);
300 if(substr($word,-1) == 'q')
301 $word = substr($word,0,-1);
305 if(! function_exists('killme')) {
307 session_write_close();
311 if(! function_exists('goaway')) {
312 function goaway($s) {
313 header("Location: $s");
318 if(! function_exists('xml_status')) {
319 function xml_status($st) {
320 header( "Content-type: text/xml" );
321 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
322 echo "<result><status>$st</status></result>\r\n";
326 if(! function_exists('local_user')) {
327 function local_user() {
328 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
329 return $_SESSION['uid'];
333 if(! function_exists('remote_user')) {
334 function remote_user() {
335 if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
336 return $_SESSION['cid'];
340 function footer(&$a) {
342 $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
343 $a->page['content'] .= "<div class=\"fortune\" >$s</div>";
346 if(! function_exists('notice')) {
347 function notice($s) {
349 $_SESSION['sysmsg'] .= $s;
353 if(! function_exists('get_max_import_size')) {
354 function get_max_import_size() {
356 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
359 if(! function_exists('xmlify')) {
360 function xmlify($str) {
363 for($x = 0; $x < strlen($str); $x ++) {
394 $buffer = trim($buffer);
398 function unxmlify($s) {
399 $ret = str_replace('&','&', $s);
400 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
404 function hex2bin($s) {
405 return(pack("H*",$s));
409 function paginate(&$a) {
411 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
412 $stripped = str_replace('q=','',$stripped);
413 $stripped = trim($stripped,'/');
414 $url = $a->get_baseurl() . '/' . $stripped;
417 if($a->pager['total'] > $a->pager['itemspage']) {
418 $o .= '<div class="pager">';
419 if($a->pager['page'] != 1)
420 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
422 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
424 $numpages = $a->pager['total'] / $a->pager['itemspage'];
427 $numstop = $numpages;
430 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
431 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
434 for($i = $numstart; $i <= $numstop; $i++){
435 if($i == $a->pager['page'])
436 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
438 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
442 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
443 if($i == $a->pager['page'])
444 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
446 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
450 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
451 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
453 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
454 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
455 $o .= '</div>'."\r\n";
460 function expand_acl($s) {
463 $a = explode('<',$s);
464 for($x = 0; $x < count($a); $x ++) {
465 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));