array('lib' => 'login.inc', 'func' => 'logout'), 'login.html' => array('lib' => 'login.inc', 'func' => 'process_login'), 'activate.html' => array('lib' => 'login.inc', 'func' => 'process_activate'), 'passwd.html' => array('lib' => 'login.inc', 'func' => 'process_passwd'), 'options.html' => array('lib' => 'login.inc', 'func' => 'process_options'), 'search.html' => array('lib' => 'search.inc', 'func' => 'process_search'), 'topic.html' => array('lib' => 'board.inc', 'func' => 'process_topic'), 'reply.html' => array('lib' => 'board.inc', 'func' => 'process_reply'), 'tags.html' => array('lib' => 'tags.inc', 'func' => 'process_tags'), 'edit.html' => array('lib' => 'zeitung.inc', 'func' => 'process_edit'), 'new.html' => array('lib' => 'zeitung.inc', 'func' => 'process_new'), 'bookmark.html' => array('lib' => 'bookmarks.inc', 'func' => 'process_bookmark'), 'contact.html' => array('func' => 'process_contact'), 'sitemap.html' => array('lib' => 'layout.inc', 'func' => 'layout_sitemap'), ); $dirs = array('zeitung' => array('func' => 'layout_showpaper'), 'archiv' => array('func' => 'layout_archive'), 'tag' => array('func' => 'layout_showtag'), 'topic' => array('func' => 'layout_topic'), 'admin' => array('lib' => 'admin.inc', 'func' => 'layout_admin'), ); /* * ==================== Commonly use code =============================== */ include_once('layout.inc'); function carp($msg) { error_log($msg); exit; } function userstatus() { if (isset($_SESSION['uid'])) $info = array($_SESSION['online'], $_SESSION['users'], $_SESSION['zeitungen'], $_SESSION['ztags'], $_SESSION['tags']); else $info = userstatus_info(); return sprintf('%d Nutzer von %d online | %d Zeitungen | Bewertungen: %d | Tags: %d', $info[0], $info[1], $info[2], $info[3], $info[4]); } function dispatch() { global $cfg; global $zlist; global $pages; global $dirs; $zlist['info'] = array('info_searchform', 'info_new', 'info_tags', 'info_tagcloud', 'info_actions', 'info_bookmarks','info_hitlist'); if (strlen($cfg['path']) && array_key_exists($cfg['path'], $pages)) { if (array_key_exists('lib', $pages[$cfg['path']])) include_once($pages[$cfg['path']]['lib']); if (array_key_exists('func', $pages[$cfg['path']])) { if (function_exists($pages[$cfg['path']]['func'])) $body = $pages[$cfg['path']]['func'](); else $body = notfound(); } } elseif (strlen($cfg['dir']) && array_key_exists($cfg['dir'], $dirs)) { if (array_key_exists('lib', $dirs[$cfg['dir']])) include_once($dirs[$cfg['dir']]['lib']); if (array_key_exists('func', $dirs[$cfg['dir']])) { if (function_exists($dirs[$cfg['dir']]['func'])) $body = $dirs[$cfg['dir']]['func'](); else $body = notfound(); } } elseif (empty($_SERVER['QUERY_STRING']) && empty($cfg['path']) && empty($cfg['dir'])) { $body .= load_template('main.html'); $zlist['page'] = 'index'; } else { $body = notfound(); } return layout_page($body); } function tagcloud_min() { $query = 'SELECT count(uid) AS count FROM zeitung_tags GROUP BY zeitung,tag ORDER BY count ASC LIMIT 1'; $sth = db_query($query); if ($sth === false) return 1; if (pg_num_rows($sth) === 0) return 1; $row = pg_fetch_array($sth, 0); return $row['count']; } function tagcloud_max() { $query = 'SELECT count(uid) AS count FROM zeitung_tags GROUP BY zeitung,tag ORDER BY count DESC LIMIT 1'; $sth = db_query($query); if ($sth === false) return 10; if (pg_num_rows($sth) === 0) return 10; $row = pg_fetch_array($sth, 0); return $row['count']; } function tag_class($count) { if (isset($_SESSION['uid'])) { if (!isset($_SESSION['tagcloud_lastupdate']) || $_SESSION["tagcloud_lastupdate"] < time() - 60*60*12) { $min = $_SESSION["tagcloud_min"] = tagcloud_min(); $max = $_SESSION["tagcloud_max"] = tagcloud_max(); $_SESSION["tagcloud_lastupdate"] = time(); } } if (!isset($min)) { $min = tagcloud_min(); $max = tagcloud_max(); } if ($count > (int)($min + ($max - $min) * 0.8)) return 4; elseif ($count > (int)($min + ($max - $min) * 0.6)) return 3; elseif ($count > (int)($min + ($max - $min) * 0.4)) return 2; elseif ($count > (int)($min + ($max - $min) * 0.2)) return 1; else return 0; } function load_template($template, $replace=false) { global $cfg; $fname = $cfg['tmpldir'] . '/' . $template; if (!file_exists($fname)) return false; $f = fopen($fname, 'r'); $content = fread($f, filesize($fname)); fclose($f); if (preg_match_all('/@([^@]+)@/', $content, $matches)) { $fields = array(); $values = array(); $found = array_unique($matches[1]); foreach ($found as $field) { $fields[] = '/@'.$field.'@/'; if ($replace != false && array_key_exists($field, $replace)) $values[] = $replace[$field]; else $values[] = ''; } $content = preg_replace($fields, $values, $content); } return $content; } function load_javascript($file) { global $cfg; if (!javascript_ok()) return; $fname = $cfg['tmpldir'] . '/' . $file; if (!file_exists($fname)) return; $f = fopen($fname, 'r'); $content = fread($f, filesize($fname)); fclose($f); $ret = "\n" . '' . "\n"; return $ret; } function format_date($date) { setlocale(LC_TIME, "de_DE.UTF-8"); return strftime("%e. %B %Y, %H:%M", strtotime($date)); } function format_newspaper($id) { global $cfg; global $zlist; $query = sprintf("SELECT * FROM zeitungen WHERE id = %d AND deleted IS false", $id); $sth = db_query($query) or carp("format_newspaper"); if (pg_num_rows ($sth) == 0) return false; $row = pg_fetch_array ($sth, 0); $ret = '
'; $ret .= sprintf('

%s

', $row['name']); $zlist['newspaper'] = $row['name']; $zlist['city'] = $row['city']; $ret .= sprintf('

%s
Ort: %s
URL: %s

', $row['description'], $row['city'], $row['url'], $row['url']); $ret .= '
'; return $ret; } function format_topten($uid) { global $cfg; if ($uid > 0) $query = sprintf("SELECT zeitung,name,counter FROM hits " . "INNER JOIN zeitungen ON id = zeitung " . "WHERE deleted IS false AND uid = %d " . "ORDER BY counter DESC LIMIT 10", $uid); else $query = "SELECT zeitung,name,sum(counter) as counter FROM hits " . "INNER JOIN zeitungen ON id = zeitung " . "WHERE deleted IS false " . "GROUP BY zeitung,name ORDER BY counter DESC LIMIT 10"; $sth = db_query($query) or carp("format_topten"); if (pg_num_rows ($sth) == 0) return; $ret = '

Top 10

'; $ret .= '

'; return $ret; } function format_topic($topic) { global $cfg; global $zlist; $query = sprintf("SELECT topic,archived,zeitung FROM topics WHERE id = %d", $topic); if (($sth = db_query($query)) === false) return warning('Es ist ein Datenbankfehler aufgetreten.'); if (pg_num_rows ($sth) == 0) return warning('Keine passende Diskussion gefunden.'); if (($info = pg_fetch_array ($sth, 0)) == false) return warning('Es ist ein Datenbankfehler aufgetreten.'); $query = sprintf("SELECT nickname,url,created,body FROM article " . "JOIN users ON users.id = uid " . "WHERE topic = %d AND article.status = 1 " . "ORDER BY created", $topic); if (($sth2 = db_query($query)) === false) return false; if (pg_num_rows ($sth2) > 0) { $ret .= '
'; $ret .= sprintf ('

%s

', htmlspecialchars($info['topic'])); $col = 0; $zlist['zid'] = $info['zeitung']; $zlist['topic'] = $info['topic']; $zlist['archived'] = $info['archived'] == 't'; for ($j=0; $j < pg_num_rows ($sth2); $j++) { $row = pg_fetch_array ($sth2, $j); $ret .= sprintf('
', $col); if (strlen($row['url'])) $author = sprintf('%s', $row['url'], $row['nickname']); else $author = $row['nickname']; $ret .= sprintf('

%s, %s

', $author, format_date($row['created'])); $ret .= sprintf('

%s

', $row['body']); $ret .= '
'; $col = 1-$col; } if ($info['archived'] == 'f' && strpos($_SERVER['REQUEST_URI'], "/reply.html", 0) === false) { $ret .= '

'; if (logged_in()) { $link_rep = sprintf('%sreply.html?topic=%d', $cfg['basepath'], $topic); } else { $link_rep = sprintf('%slogin.html?from=article', $cfg['basepath']); } $ret .= sprintf(' ', $cfg['basepath']); $ret .= sprintf('antworten', $link_rep); $ret .= '

'; } $ret .= '
'; } return $ret; } function format_board($zid, $archived=false) { global $cfg; global $zlist; $query = sprintf("SELECT id FROM topics " . "WHERE zeitung = %d AND archived IS %s " . "ORDER BY created DESC", $zid, $archived?'true':'false'); if (($sth = db_query($query)) === false) return false; if (pg_num_rows ($sth) == 0 && !$archived) { $zlist['notopic'] = true; return $ret; } if (pg_num_rows ($sth) > 0) { if ($archived) $ret = '

Abgeschlossene Diskussionen

'; else $ret = '

Diskussion

'; } for ($i=0; $i < pg_num_rows ($sth); $i++) { $row = pg_fetch_array ($sth, $i); $ret .= format_topic($row['id']); } return $ret; } function fix_url($url) { if (!strlen($url)) return false; if (strpos($url, "http://") === false) $url = "http://" . $url; $parts = parse_url($url); if ($parts === false) return false; if (empty($parts['path'])) $url .= '/'; return $url; } function is_valid_url($url) { if (strpos($url, '.') === false) return false; $parts = parse_url($url); if (empty($parts['host']) || empty($parts['scheme']) || empty($parts['path'])) return false; if ($parts['scheme'] != 'http' && $parts['scheme'] != 'https') return false; if (!preg_match ('/^[a-zA-Z][a-zA-Z0-9\.-]+$/', $parts['host'], $matches)) return false; if (preg_match ('/[\\\\<>"\'\(\)\[\]]/', $parts['path'], $matches)) return false; if (!empty($parts['query']) && preg_match ('/[\\\\<>"\'\(\)\[\]]/', $parts['query'], $matches)) return false; return true; } function ajax_check_url() { if (!empty($_POST['url']) && is_valid_url($_POST['url'])) return true; return false; } function sendmail($to, $name, $subject, $body, $header=array()) { global $cfg; if (empty($to)) return false; $header[] = 'From: ' . $cfg['from']; if (empty($name)) $header[] = 'To: ' . $to; else $header[] = sprintf('To: %s <%s>', $name, $to); $header[] = 'MIME-Version: 1.0'; $header[] = 'Content-type: text/plain; charset=utf-8'; $header[] = 'Content-Disposition: inline'; $header[] = 'Content-Transfer-Encoding: 8bit'; $sig = load_template('signature'); if (!empty($sig)) $body .= $sig; $subject = mb_encode_mimeheader($subject,"UTF-8", "Q", "\n"); if (mail ($to, $subject, $body, implode("\n", $header)) === false) return false; return true; } function logbook($table,$refid,$column,$old,$new) { $query = sprintf("INSERT INTO logbook (uid,tab,refid,col,oldval,newval,modified) " . "VALUES (%d,'%s',%d,'%s','%s','%s',now())", $_SESSION['uid'], $table,$refid,$column, pg_escape_string($old), pg_escape_string($new)); db_query($query); } function hits_inc($zeitung) { global $cfg; if (is_spider()) return; $uid = isset($_SESSION['uid'])?$_SESSION['uid']:0; $query = sprintf("SELECT counter FROM hits WHERE uid = %d AND zeitung = %d", $uid, $zeitung); $sth = db_query($query); if (pg_num_rows ($sth) == 0) $query = sprintf("INSERT INTO hits (zeitung,uid,counter) " . "VALUES (%d,%d,1)", $zeitung, $uid); else $query = sprintf("UPDATE hits SET counter = counter + 1 " . "WHERE zeitung = %d AND uid = %d", $zeitung, $uid); db_query($query); }