5 define ( 'BUILD_ID' , 1000 );
7 define ( 'EOL', "<br />\r\n");
9 define ( 'REGISTER_CLOSED', 0);
10 define ( 'REGISTER_APPROVE', 1);
11 define ( 'REGISTER_OPEN', 2);
13 define ( 'DIRECTION_ANY', 0);
14 define ( 'DIRECTION_IN', 1);
15 define ( 'DIRECTION_OUT', 2);
16 define ( 'DIRECTION_BOTH', 3);
18 define ( 'NOTIFY_INTRO', 0x0001 );
19 define ( 'NOTIFY_CONFIRM', 0x0002 );
20 define ( 'NOTIFY_WALL', 0x0004 );
21 define ( 'NOTIFY_COMMENT', 0x0008 );
22 define ( 'NOTIFY_MAIL', 0x0010 );
24 define ( 'NAMESPACE_DFRN' , 'http://purl.org/macgirvin/dfrn/1.0' );
27 if(! class_exists('App')) {
30 public $module_loaded = false;
39 public $error = false;
53 function __construct() {
55 $this->config = array();
56 $this->page = array();
57 $this->pager= array();
59 $this->scheme = ((isset($_SERVER['HTTPS'])
60 && ($_SERVER['HTTPS'])) ? 'https' : 'http' );
61 $this->hostname = str_replace('www.','',
62 $_SERVER['SERVER_NAME']);
63 set_include_path("include/$this->hostname"
64 . PATH_SEPARATOR . 'include'
65 . PATH_SEPARATOR . '.' );
67 if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
68 $_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'],2);
69 $this->cmd = trim($_GET['q'],'/');
72 $this->argv = explode('/',$this->cmd);
73 $this->argc = count($this->argv);
74 if((array_key_exists('0',$this->argv)) && strlen($this->argv[0])) {
75 $this->module = $this->argv[0];
78 $this->module = 'home';
81 if($this->cmd == '.well-known/host-meta')
82 require_once('include/hostxrd.php');
84 $this->pager['page'] = ((x($_GET,'page')) ? $_GET['page'] : 1);
85 $this->pager['itemspage'] = 50;
86 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
87 $this->pager['total'] = 0;
90 function get_baseurl($ssl = false) {
91 if(strlen($this->baseurl))
92 return $this->baseurl;
94 $this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
95 . ((isset($this->path) && strlen($this->path))
96 ? '/' . $this->path : '' );
97 return $this->baseurl;
100 function set_baseurl($url) {
101 $this->baseurl = $url;
102 $this->hostname = basename($url);
105 function get_hostname() {
106 return $this->hostname;
109 function set_hostname($h) {
110 $this->hostname = $h;
113 function set_path($p) {
114 $this->path = ltrim(trim($p),'/');
117 function get_path() {
121 function set_pager_total($n) {
122 $this->pager['total'] = intval($n);
125 function set_pager_itemspage($n) {
126 $this->pager['itemspage'] = intval($n);
127 $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
131 function init_pagehead() {
132 if(file_exists("view/head.tpl"))
133 $s = file_get_contents("view/head.tpl");
134 $this->page['htmlhead'] = replace_macros($s,array(
135 '$baseurl' => $this->get_baseurl()
142 if(! function_exists('x')) {
143 function x($s,$k = NULL) {
145 if((is_array($s)) && (array_key_exists($k,$s))) {
163 if(! function_exists('system_unavailable')) {
164 function system_unavailable() {
165 include('system_unavailable.php');
170 if(! function_exists('check_config')) {
171 function check_config(&$a) {
173 $build = get_config('system','build');
175 $build = set_config('system','build',BUILD_ID);
177 $url = get_config('system','url');
179 $url = set_config('system','url',$a->get_baseurl());
181 if($build != BUILD_ID) {
182 $stored = intval($build);
183 $current = intval(BUILD_ID);
184 if(($stored < $current) && file_exists('update.php')) {
186 // We're reporting a different version than what is currently installed.
187 // Run any existing update scripts to bring the database up to current.
189 require_once('update.php');
190 for($x = $stored; $x <= $current; $x ++) {
191 if(function_exists('update_' . $x)) {
192 $func = 'update_' . $x;
196 set_config('system','build', BUILD_ID);
204 if(! function_exists('replace_macros')) {
205 function replace_macros($s,$r) {
210 if(is_array($r) && count($r)) {
211 foreach ($r as $k => $v ) {
216 return str_replace($search,$replace,$s);
220 if(! function_exists('load_translation_table')) {
221 function load_translation_table($lang) {
226 if(! function_exists('t')) {
231 return $a->strings[$s];
235 if(! function_exists('fetch_url')) {
236 function fetch_url($url,$binary = false) {
237 $ch = curl_init($url);
238 if(! $ch) return false;
240 curl_setopt($ch, CURLOPT_HEADER, 0);
241 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
242 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
243 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
245 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
253 if(! function_exists('post_url')) {
254 function post_url($url,$params) {
255 $ch = curl_init($url);
256 if(! $ch) return false;
258 curl_setopt($ch, CURLOPT_HEADER, 0);
259 curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
260 curl_setopt($ch, CURLOPT_MAXREDIRS,8);
261 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
262 curl_setopt($ch, CURLOPT_POST,1);
263 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
271 if(! function_exists('random_string')) {
272 function random_string() {
273 return(hash('sha256',uniqid(rand(),true)));
276 if(! function_exists('notags')) {
277 function notags($string) {
278 // protect against :<> with high-bit set
279 return(str_replace(array("<",">","\xBA","\xBC","\xBE"), array('[',']','','',''), $string));
282 if(! function_exists('escape_tags')) {
283 function escape_tags($string) {
285 return(htmlspecialchars($string));
288 if(! function_exists('login')) {
289 function login($register = false) {
291 $register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
294 if(x($_SESSION,'authenticated')) {
295 $o = file_get_contents("view/logout.tpl");
298 $o = file_get_contents("view/login.tpl");
300 $o = replace_macros($o,array('$register_html' => $register_html ));
306 if(! function_exists('autoname')) {
307 function autoname($len) {
309 $vowels = array('a','a','ai','au','e','e','e','ee','ea','i','ie','o','ou','u');
310 if(mt_rand(0,5) == 4)
328 's','sc','sh','sm','sp','st',
336 $midcons = array('ck','ct','gn','ld','lf','lm','lt','mb','mm', 'mn','mp',
337 'nd','ng','nk','nt','rn','rp','rt');
339 $noend = array('bl', 'br', 'cl','cr','dr','fl','fr','gl','gr',
340 'kh', 'kl','kr','mn','pl','pr','rh','tr','qu','wh');
342 $start = mt_rand(0,2);
350 for ($x = 0; $x < $len; $x ++) {
351 $r = mt_rand(0,count($table) - 1);
354 if($table == $vowels)
355 $table = array_merge($cons,$midcons);
361 $word = substr($word,0,$len);
363 foreach($noend as $noe) {
364 if((strlen($word) > 2) && (substr($word,-2) == $noe)) {
365 $word = substr($word,0,-1);
369 if(substr($word,-1) == 'q')
370 $word = substr($word,0,-1);
374 if(! function_exists('killme')) {
376 session_write_close();
380 if(! function_exists('goaway')) {
381 function goaway($s) {
382 header("Location: $s");
387 if(! function_exists('xml_status')) {
388 function xml_status($st) {
389 header( "Content-type: text/xml" );
390 echo '<?xml version="1.0" encoding="UTF-8"?>'."\r\n";
391 echo "<result><status>$st</status></result>\r\n";
395 if(! function_exists('local_user')) {
396 function local_user() {
397 if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
398 return $_SESSION['uid'];
402 if(! function_exists('remote_user')) {
403 function remote_user() {
404 if((x($_SESSION,'authenticated')) && (x($_SESSION,'visitor_id')))
405 return $_SESSION['visitor_id'];
409 if(! function_exists('notice')) {
410 function notice($s) {
412 $_SESSION['sysmsg'] .= $s;
416 if(! function_exists('get_max_import_size')) {
417 function get_max_import_size() {
419 return ((x($a->config,'max_import_size')) ? $a->config['max_import_size'] : 0 );
422 if(! function_exists('xmlify')) {
423 function xmlify($str) {
426 for($x = 0; $x < strlen($str); $x ++) {
457 $buffer = trim($buffer);
461 if(! function_exists('unxmlify')) {
462 function unxmlify($s) {
463 $ret = str_replace('&','&', $s);
464 $ret = str_replace(array('<','>','"','''),array('<','>','"',"'"),$ret);
468 if(! function_exists('hex2bin')) {
469 function hex2bin($s) {
470 return(pack("H*",$s));
474 if(! function_exists('paginate')) {
475 function paginate(&$a) {
477 $stripped = ereg_replace("(&page=[0-9]*)","",$_SERVER['QUERY_STRING']);
478 $stripped = str_replace('q=','',$stripped);
479 $stripped = trim($stripped,'/');
480 $url = $a->get_baseurl() . '/' . $stripped;
483 if($a->pager['total'] > $a->pager['itemspage']) {
484 $o .= '<div class="pager">';
485 if($a->pager['page'] != 1)
486 $o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
488 $o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
490 $numpages = $a->pager['total'] / $a->pager['itemspage'];
493 $numstop = $numpages;
496 $numstart = (($pagenum > 7) ? ($pagenum - 7) : 1);
497 $numstop = (($pagenum > ($numpages - 7)) ? $numpages : ($numstart + 14));
500 for($i = $numstart; $i <= $numstop; $i++){
501 if($i == $a->pager['page'])
502 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
504 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
508 if(($a->pager['total'] % $a->pager['itemspage']) != 0) {
509 if($i == $a->pager['page'])
510 $o .= '<span class="pager_current">'.(($i < 10) ? ' '.$i : $i);
512 $o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? ' '.$i : $i)."</a>";
516 $lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
517 $o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
519 if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
520 $o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
521 $o .= '</div>'."\r\n";
526 if(! function_exists('expand_acl')) {
527 function expand_acl($s) {
530 $a = explode('<',$s);
531 for($x = 0; $x < count($a); $x ++) {
532 $a[$x] = intval(str_replace(array('<','>'),array('',''),$a[$x]));
539 if(! function_exists('sanitise_acl')) {
540 function sanitise_acl(&$item) {
542 $item = '<' . intval(notags(trim($item))) . '>';
547 if(! function_exists('get_config')) {
548 function get_config($family,$key) {
549 $ret = q("SELECT `v` FROM `config` WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",
558 if(! function_exists('set_config')) {
559 function set_config($family,$key,$value) {
560 if(get_config($family,$key) === false) {
561 $ret = q("INSERT INTO `config` ( `cat`, `k`, `v` ) VALUES ( '%s', '%s', '%s' ) ",
570 $ret = q("SUPDATE `config` SET `v` = '%s' WHERE `cat` = '%s' AND `k` = '%s' LIMIT 1",