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 define ( 'NOTIFY_INTRO', 0x0001 );
17 define ( 'NOTIFY_CONFIRM', 0x0002 );
18 define ( 'NOTIFY_WALL', 0x0004 );
19 define ( 'NOTIFY_COMMENT', 0x0008 );
20 define ( 'NOTIFY_MAIL', 0x0010 );
25 if(! class_exists('App')) {
28 public $module_loaded = false;
36 public $error = false;
49 function __construct() {
51 $this->config = array();
52 $this->page = array();
53 $this->pager= array();
55 $this->scheme = ((isset($_SERVER['HTTPS'])
56 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
57 $this->hostname = str_replace('www.','',
58 $_SERVER['SERVER_NAME']);
59 set_include_path("include/$this->hostname"
60 . PATH_SEPARATOR . 'include'
61 . PATH_SEPARATOR . '.' );
63 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
64 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
65 $this->cmd = trim($_GET['q'],'/');
68 $this->argv = explode('/',$this->cmd);
69 $this->argc = count($this->argv);
70 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
71 $this->module = $this->argv[0];
74 $this->module = 'home';
77 if($this->cmd == '.well-known/host-meta')
78 require_once('include/hostxrd.php');
80 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
81 $this->pager['itemspage'] = 50;
82 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
83 $this->pager['total'] = 0;
86 function get_baseurl($ssl = false) {
87 if(strlen($this->baseurl))
88 return $this->baseurl;
90 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
91 . ((isset($this->path) && strlen($this->path))
92 ? '/' . $this->path : '' );
93 return $this->baseurl;
96 function set_baseurl($url) {
97 $this->baseurl = $url;
100 function get_hostname() {
101 return $this->hostname;
104 function set_hostname($h) {
105 $this->hostname = $h;
108 function set_path($p) {
109 $this->path = ltrim(trim($p),'/');
112 function get_path() {
116 function set_pager_total($n) {
117 $this->pager['total'] = intval($n);
120 function set_pager_itemspage($n) {
121 $this->pager['itemspage'] = intval($n);
122 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
126 function init_pagehead() {
127 if(file_exists("view/head.tpl"))
128 $s = file_get_contents("view/head.tpl");
129 $this->page['htmlhead'] = replace_macros($s,array('$baseurl' => $this->get_baseurl()));
135 if(! function_exists('x')) {
136 function x($s,$k = NULL) {
138 if((is_array($s)) && (array_key_exists($k,$s))) {
156 if(! function_exists('system_unavailable')) {
157 function system_unavailable() {
158 include('system_unavailable.php');
162 if(! function_exists('replace_macros')) {
163 function replace_macros($s,$r) {
168 if(is_array($r) && count($r)) {
169 foreach ($r as $k => $v ) {
174 return str_replace($search,$replace,$s);
179 if(! function_exists('fetch_url')) {
180 function fetch_url($url,$binary = false) {
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);
189 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
197 if(! function_exists('post_url')) {
198 function post_url($url,$params) {
199 $ch = curl_init($url);
200 if(! $ch) return false;
202 curl_setopt($ch, CURLOPT_HEADER, 0);
203 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
204 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
205 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
206 curl_setopt($ch, CURLOPT_POST,1);
207 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
215 if(! function_exists('random_string')) {
216 function random_string() {
217 return(hash('sha256',uniqid(rand(),true)));
220 if(! function_exists('notags')) {
221 function notags($string) {
222 // protect against :<> with high-bit set
223 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
226 if(! function_exists('escape_tags')) {
227 function escape_tags($string) {
229 return(htmlspecialchars($string));
232 if(! function_exists('login')) {
233 function login($register = false) {
235 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
238 if(x($_SESSION,'authenticated')) {
239 $o = file_get_contents("view/logout.tpl");
242 $o = file_get_contents("view/login.tpl");
244 $o = replace_macros($o,array('$register_html' => $register_html ));
250 if(! function_exists('autoname')) {
251 function autoname($len) {
253 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
254 if(mt_rand(0,5) == 4)
272 's','sc','sh','sm','sp','st',
280 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
281 'nd','ng','nk','nt','rn','rp','rt');
283 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
284 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
286 $start = mt_rand(0,2);
294 for ($x = 0; $x < $len; $x ++) {
295 $r = mt_rand(0,count($table) - 1);
298 if($table == $vowels)
299 $table = array_merge($cons,$midcons);
305 $word = substr($word,0,$len);
307 foreach($noend as $noe) {
308 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
309 $word = substr($word,0,-1);
313 if(substr($word,-1) == 'q')
314 $word = substr($word,0,-1);
318 if(! function_exists('killme')) {
320 session_write_close();
324 if(! function_exists('goaway')) {
325 function goaway($s) {
326 header("Location: $s");
331 if(! function_exists('xml_status')) {
332 function xml_status($st) {
333 header( "Content-type: text/xml" );
334 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
335 echo "<result><status>$st</status></result>\r\n";
339 if(! function_exists('local_user')) {
340 function local_user() {
341 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
342 return $_SESSION['uid'];
346 if(! function_exists('remote_user')) {
347 function remote_user() {
348 if((x($_SESSION,'authenticated')) && (x($_SESSION,'cid')))
349 return $_SESSION['cid'];
353 function footer(&$a) {
355 $s = fetch_url("http://fortunemod.com/cookie.php?equal=1");
356 $a->page['content'] .= "<div class=\"fortune\" >$s</div>";
359 if(! function_exists('notice')) {
360 function notice($s) {
362 $_SESSION['sysmsg'] .= $s;
366 if(! function_exists('get_max_import_size')) {
367 function get_max_import_size() {
369 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
372 if(! function_exists('xmlify')) {
373 function xmlify($str) {
376 for($x = 0; $x < strlen($str); $x ++) {
407 $buffer = trim($buffer);
411 function unxmlify($s) {
412 $ret = str_replace('&','&', $s);
413 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
417 function hex2bin($s) {
418 return(pack("H*",$s));
422 function paginate(&$a) {
424 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
425 $stripped = str_replace('q=','',$stripped);
426 $stripped = trim($stripped,'/');
427 $url = $a->get_baseurl() . '/' . $stripped;
430 if($a->pager['total'] > $a->pager['itemspage']) {
431 $o .= '<div class="pager">';
432 if($a->pager['page'] != 1)
433 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">prev</a></span> ';
435 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">first</a></span> ";
437 $numpages = $a->pager['total'] / $a->pager['itemspage'];
440 $numstop = $numpages;
443 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
444 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
447 for($i = $numstart; $i <= $numstop; $i++){
448 if($i == $a->pager['page'])
449 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
451 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
455 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
456 if($i == $a->pager['page'])
457 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
459 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
463 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
464 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">last</a></span> ";
466 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
467 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">next</a></span>';
468 $o .= '</div>'."\r\n";
473 function expand_acl($s) {
476 $a = explode('<',$s);
477 for($x = 0; $x < count($a); $x ++) {
478 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));