This commit is contained in:
Shish 2013-03-07 03:25:23 +00:00
commit 3175e605da
5 changed files with 428 additions and 44 deletions

View File

@ -0,0 +1,114 @@
<?php
/**
* Name: Arrow Key Navigation
* Author: Drudex Software <support@drudexsoftware.com>
* Link: http://www.drudexsoftware.com/
* License: GPLv2
* Description: Allows viewers no navigate between images using the left & right arrow keys.
* Documentation:
* Simply enable this extention in the extention manager to enable arrow key navigation.
*/
class arrowkey_navigation extends Extension {
# Adds functionality for post/list
public function onPageRequest(PageRequestEvent $event) {
if ($event->page_matches("post/view")) {
$pageinfo = $this->get_view_pageinfo($event);
$prev_url = make_http(make_link("post/prev/".$pageinfo));
$next_url = make_http(make_link("post/next/".$pageinfo));
$this->add_arrowkeys_code($prev_url, $next_url);
}
else if ($event->page_matches("post/list")) {
$pageinfo = $this->get_list_pageinfo($event);
$prev_url = make_http(make_link("post/list/".$pageinfo["prev"]));
$next_url = make_http(make_link("post/list/".$pageinfo["next"]));
$this->add_arrowkeys_code($prev_url, $next_url);
}
}
# adds the javascript to the page with the given urls
private function add_arrowkeys_code($prev_url, $next_url) {
global $page;
$page->add_html_header("<script type=\"text/javascript\">
document.onkeyup=checkKeycode;
function checkKeycode(e)
{
var keycode;
if(window.event) keycode=window.event.keyCode;
else if(e) keycode=e.which;
if (e.srcElement.tagName != \"INPUT\")
{
if(keycode==\"37\") window.location.href='$prev_url' + window.location.hash;
else if(keycode==\"39\") window.location.href='$next_url' + window.location.hash;
}
}
</script>");
}
# returns info about the current page number
private function get_list_pageinfo($event) {
global $config, $database;
// get the amount of images per page
$images_per_page = $config->get_int('index_images');
// this occurs when viewing post/list without page number
if ($event->get_arg(0) == null) {// no page listed
$prefix = "";
$page_number = 1;
$total_pages = ceil($database->get_one(
"SELECT COUNT(*) FROM images") / $images_per_page);
}
// if there are no tags, use default
else if ($event->get_arg(1) == null){
$prefix = "";
$page_number = (int)$event->get_arg(0);
$total_pages = ceil($database->get_one(
"SELECT COUNT(*) FROM images") / $images_per_page);
}
else { // if there are tags, use pages with tags
$prefix = $event->get_arg(0)."/";
$page_number = (int)$event->get_arg(1);
$total_pages = ceil($database->get_one(
"SELECT count FROM tags WHERE tag=:tag",
array("tag"=>$event->get_arg(0))) / $images_per_page);
}
// creates previous & next values
// When previous first page, go to last page
if ($page_number <= 1) $prev = $total_pages;
else $prev = $page_number-1;
if ($page_number >= $total_pages) $next = 1;
else $next = $page_number+1;
// Create return array
$pageinfo = array(
"prev" => $prefix.$prev.$after,
"next" => $prefix.$next.$after,
);
return $pageinfo;
}
# returns url ext with any tags
private function get_view_pageinfo($event) {
// if there are no tags, use default
if ($event->get_arg(1) == null){
$prefix = "";
$image_id = (int)$event->get_arg(0);
}
else { // if there are tags, use pages with tags
$prefix = $event->get_arg(0)."/";
$image_id = (int)$event->get_arg(1);
}
// returns result
return $prefix.$image_id;
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
/**
* Name: Custom HTML Headers
* Author: Drudex Software <support@drudexsoftware.com>
* Link: http://www.drudexsoftware.com
* License: GPLv2
* Description: Allows admins to set custom <head> content
* Documentation:
* When you go to board config you can find a block named Custom HTML Headers.
* In that block you can simply place any thing you can place within <head></head>
*
* This can be useful if you want to add website tracking code or other javascript.
* NOTE: Only use if you know what you're doing.
*
*/
class custom_html_headers extends Extension {
# Adds setup block for custom <head> content
public function onSetupBuilding(SetupBuildingEvent $event) {
$sb = new SetupBlock("Custom HTML Headers");
$sb->add_longtext_option("custom_html_headers",
"HTML Code to place within &lt;head&gt;&lt;/head&gt; on all pages<br>");
$event->panel->add_block($sb);
}
# Load Analytics tracking code on page request
public function onPageRequest(PageRequestEvent $event) {
global $config, $page;
$header = $config->get_string('custom_html_headers','');
if ($header!='') $page->add_html_header($header);
}
}
?>

View File

@ -0,0 +1,108 @@
<?php
/**
* Name: Image View Counter
* Author: Drudex Software <support@drudexsoftware.com>
* Link: http://www.drudexsoftware.com/
* License: GPLv2
* Description: Tracks & displays how many times an image is viewed
* Documentation:
* Whenever anyone views an image, a view will be added to that image.
* This extension will also track any username & the IP adress.
* This is done to prevent duplicate views.
* A person can only count as a view again 1 hour after viewing the image initially.
*/
class image_view_counter extends Extension {
private $view_interval = 3600; # allows views to be added each hour
# Add Setup Block with options for view counter
public function onSetupBuilding(SetupBuildingEvent $event) {
$sb = new SetupBlock("Image View Counter");
$sb->add_bool_option("image_viewcounter_adminonly", "Display view counter only to admin");
$event->panel->add_block($sb);
}
# Adds view to database if needed
public function onDisplayingImage(DisplayingImageEvent $event) {
$imgid = $event->image->id; // determines image id
$this->addview($imgid); // adds a view
}
# display views to user or admin below image if allowed
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) {
global $user, $config;
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo0
if ($adminonly == false || ($adminonly && $user->is_admin()))
$event->add_part("<tr><th>Views:</th><td>".
$this->get_view_count($event->image->id) ."</th></tr>", 38);
}
# Installs DB table
public function onInitExt(InitExtEvent $event) {
global $database, $config;
// if the sql table doesn't exist yet, create it
if($config->get_bool("image_viewcounter_installed") == false) { //todo
$database->create_table("image_views","
id bigint(20) SCORE_AIPK,
image_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
timestamp INTEGER NOT NULL,
ipaddress SCORE_INET NOT NULL");
$config->set_bool("image_viewcounter_installed", true);
}
}
# Adds a view to the item if needed
private function addview($imgid)
{
global $database, $user;
// don't add view if person already viewed recently
if ($this->can_add_view($imgid) == false) return;
// Add view for current IP
$database->execute("INSERT INTO image_views (image_id, user_id, timestamp, ipaddress)
VALUES (:image_id, :user_id, :timestamp, :ipaddress)", array(
"image_id" => $imgid,
"user_id" => $user->id,
"timestamp" => time(),
"ipaddress" => $_SERVER['REMOTE_ADDR'],
));
}
# Returns true if this IP hasn't recently viewed this image
private function can_add_view($imgid)
{
global $database;
// counts views from current IP in the last hour
$recent_from_ip = (int)$database->get_one("SELECT COUNT(*) FROM image_views WHERE
ipaddress=:ipaddress AND timestamp >:lasthour AND image_id =:image_id",
array( "ipaddress" => $_SERVER['REMOTE_ADDR'],
"lasthour" => time() - $this->view_interval,
"image_id" => $imgid));
// if no views were found with the set criteria, return true
if($recent_from_ip == 0) return true;
else return false;
}
# Returns the int of the view count from the given image id
// $imgid - if not set or 0, return views of all images
private function get_view_count($imgid = 0)
{
global $database;
if ($imgid == 0) // return view count of all images
$view_count = (int)$database->get_one("SELECT COUNT(*) FROM image_views");
else // return view count of specified image
$view_count = (int)$database->get_one("SELECT COUNT(*) FROM image_views WHERE ".
"image_id =:image_id", array("image_id" => $imgid));
// returns the count as int
return $view_count;
}
}
?>

View File

@ -2,53 +2,164 @@
/*
* Name: XML Sitemap
* Author: Sein Kraft <mail@seinkraft.info>
* Author: Drudex Software <support@drudexsoftware.com>
* License: GPLv2
* Description: Adds sitemap.xml on request.
* Description: Sitemap with caching & advanced priorities
* Documentation:
*/
class XMLSitemap extends Extension {
public function onPageRequest(PageRequestEvent $event) {
if($event->page_matches("sitemap.xml")) {
$images = Image::find_images(0, 50, array());
$this->do_xml($images);
}
private $sitemap_queue = "";
private $sitemap_filepath = ""; // set onPageRequest
public function onPageRequest(PageRequestEvent $event) {
if($event->page_matches("sitemap.xml"))
{
global $config;
$this->sitemap_filepath = $_SERVER['DOCUMENT_ROOT']."/data/cache/sitemap.xml";
// determine if new sitemap needs to be generated
if ($this->new_sitemap_needed())
{
// determine which type of sitemap to generate
if ($config->get_bool("sitemap_generatefull",false))
$this->handle_full_sitemap(); // default false until cache fixed
else
$this->handle_smaller_sitemap();
}
else $this->display_existing_sitemap();
}
}
public function onSetupBuilding(SetupBuildingEvent $event) {
$sb = new SetupBlock("Sitemap");
$sb->add_bool_option("sitemap_generatefull", "Generate full sitemap");
$sb->add_label("<br>(Enabled: every image and tag in sitemap, generation takes longer)");
$sb->add_label("<br>(Disabled: only display the last 50 uploads in the sitemap)");
$event->panel->add_block($sb);
}
// sitemap with only the latest 50 images
private function handle_smaller_sitemap()
{
/* --- Add latest images to sitemap with higher priority --- */
$latestimages = Image::find_images(0, 50, array());
$latestimages_urllist = array();
foreach($latestimages as $arrayid => $image)
// create url from image id's
$latestimages_urllist[$arrayid] = "post/view/$image->id";
$this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", $image->posted_timestamp));
/* --- Display page --- */
// when sitemap is ok, display it from the file
$this->generate_display_sitemap();
}
// Full sitemap
private function handle_full_sitemap()
{
global $database, $config;
// add index
$index[0] = $base_href = $config->get_string("front_page");
$this->add_sitemap_queue($index, "weekly", "1");
/* --- Add 20 most used tags --- */
$popular_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 0,20");
foreach($popular_tags as $arrayid => $tag) {
$tag = $tag['tag'];
$popular_tags[$arrayid] = "post/list/$tag/";
}
$this->add_sitemap_queue($popular_tags, "monthly", "0.9" /* not sure how to deal with date here */);
/* --- Add latest images to sitemap with higher priority --- */
$latestimages = Image::find_images(0, 50, array());
$latestimages_urllist = array();
foreach($latestimages as $arrayid => $image)
// create url from image id's
$latestimages_urllist[$arrayid] = "post/view/$image->id";
$this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", $image->posted_timestamp));
/* --- Add other tags --- */
$other_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 21,10000000");
foreach($other_tags as $arrayid => $tag) {
$tag = $tag['tag'];
// create url from tags (tagme ignored)
if ($tag != "tagme")
$other_tags[$arrayid] = "post/list/$tag/";
}
$this->add_sitemap_queue($other_tags, "monthly", "0.7" /* not sure how to deal with date here */);
/* --- Add all other images to sitemap with lower priority --- */
$otherimages = Image::find_images(51, 10000000, array());
foreach($otherimages as $arrayid => $image)
// create url from image id's
$otherimages[$arrayid] = "post/view/$image->id";
$this->add_sitemap_queue($otherimages, "monthly", "0.6", date("Y-m-d", $image->posted_timestamp));
/* --- Display page --- */
// when sitemap is ok, display it from the file
$this->generate_display_sitemap();
}
private function do_xml(/*array(Image)*/ $images) {
global $page;
$page->set_mode("data");
$page->set_type("application/xml");
$data = "";
foreach($images as $image) {
$link = make_http(make_link("post/view/{$image->id}"));
$posted = date("Y-m-d", $image->posted_timestamp);
$data .= "
<url>
<loc>$link</loc>
<lastmod>$posted</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
";
}
$base_href = make_http(make_link("post/list"));
$xml = "<"."?xml version=\"1.0\" encoding=\"utf-8\"?".">
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
<url>
<loc>$base_href</loc>
<lastmod>2009-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
$data
</urlset>
";
$page->set_data($xml);
// Adds an array of urls to the sitemap with the given information
private function add_sitemap_queue(/*array(urls)*/ $urls, $changefreq="monthly", $priority="0.5", $date="2013-02-01") {
foreach($urls as $url) {
$link = make_http(make_link("$url"));
$this->sitemap_queue .= "
<url>
<loc>$link</loc>
<lastmod>$date</lastmod>
<changefreq>$changefreq</changefreq>
<priority>$priority</priority>
</url>";
}
}
// sets sitemap with entries in the queue
private function generate_display_sitemap()
{
global $page;
$page->set_mode("data");
$page->set_type("application/xml");
$xml = "<"."?xml version=\"1.0\" encoding=\"utf-8\"?".">
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
$this->sitemap_queue
</urlset>";
// Generate new sitemap & display
file_put_contents($this->sitemap_filepath, $xml);
$page->set_data($xml);
}
// returns true if a new sitemap is needed
private function new_sitemap_needed()
{
$sitemap_generation_interval = 86400; // allow new site map every day
$last_generated_time = filemtime($this->sitemap_filepath);
// if file doesn't exist, return true
if ($last_generated_time == false) return true;
// if it's been a day since last sitemap creation, return true
if ($last_generated_time + $sitemap_generation_interval < time())
return true;
else return false;
}
private function display_existing_sitemap()
{
global $page;
$page->set_mode("data");
$page->set_type("application/xml");
$xml = file_get_contents($this->sitemap_filepath);
$page->set_data($xml);
}
}
?>
?>

View File

@ -144,7 +144,14 @@ class UserPage extends Extension {
}
log_info("user", "Logged out");
$page->set_mode("redirect");
$page->set_redirect(make_link());
// Try forwarding to same page on logout unless user comes from registration page
if ($config->get_int("user_loginshowprofile",0) == 0 &&
isset($_SERVER['HTTP_REFERER']) &&
strstr($_SERVER['HTTP_REFERER'], "post/"))
$page->set_redirect ($_SERVER['HTTP_REFERER']);
else
$page->set_redirect(make_link());
}
if(!$user->check_auth_token()) {
@ -273,7 +280,11 @@ class UserPage extends Extension {
array('G'=>'g', 'PG'=>'pg', 'R'=>'r', 'X'=>'x'),
"<br>Rating: ");
}
$sb->add_choice_option("user_loginshowprofile", array(
"return to previous page" => 0, // 0 is default
"send to user profile" => 1),
"<br>When user logs in/out");
$event->panel->add_block($sb);
}
@ -314,6 +325,7 @@ class UserPage extends Extension {
// Things done *with* the user {{{
private function login(Page $page) {
global $user;
global $config;
$name = $_POST['user'];
$pass = $_POST['pass'];
@ -330,7 +342,13 @@ class UserPage extends Extension {
$this->set_login_cookie($duser->name, $pass);
log_info("user", "{$user->class->name} logged in");
$page->set_mode("redirect");
$page->set_redirect(make_link("user"));
// Try returning to previous page
if ($config->get_int("user_loginshowprofile",0) == 0 &&
isset($_SERVER['HTTP_REFERER']) &&
strstr($_SERVER['HTTP_REFERER'], "post/"))
$page->set_redirect($_SERVER['HTTP_REFERER']);
else $page->set_redirect(make_link("user"));
}
else {
log_warning("user", "Failed to log in as ".html_escape($name)." [$hash]");