more linty bits
This commit is contained in:
parent
cfd3a9d248
commit
4577ff70ef
4 changed files with 123 additions and 97 deletions
|
@ -390,8 +390,8 @@ function show_ip($ip, $ban_reason) {
|
|||
/**
|
||||
* Checks if a given string contains another at the beginning.
|
||||
*
|
||||
* @param $haystack String to examine.
|
||||
* @param $needle String to look for.
|
||||
* @param string $haystack String to examine.
|
||||
* @param string $needle String to look for.
|
||||
* @return bool
|
||||
*/
|
||||
function startsWith(/*string*/ $haystack, /*string*/ $needle) {
|
||||
|
@ -402,8 +402,8 @@ function startsWith(/*string*/ $haystack, /*string*/ $needle) {
|
|||
/**
|
||||
* Checks if a given string contains another at the end.
|
||||
*
|
||||
* @param $haystack String to examine.
|
||||
* @param $needle String to look for.
|
||||
* @param string $haystack String to examine.
|
||||
* @param string $needle String to look for.
|
||||
* @return bool
|
||||
*/
|
||||
function endsWith(/*string*/ $haystack, /*string*/ $needle) {
|
||||
|
@ -695,7 +695,7 @@ function is_https_enabled() {
|
|||
* from the "Amazon S3 PHP class" which is Copyright (c) 2008, Donovan Schönknecht
|
||||
* and released under the 'Simplified BSD License'.
|
||||
*
|
||||
* @param string &$file File path
|
||||
* @param string $file File path
|
||||
* @param string $ext
|
||||
* @param bool $list
|
||||
* @return string
|
||||
|
@ -1463,7 +1463,7 @@ function _dump_event_listeners($event_listeners, $path) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param $ext_name string
|
||||
* @param string $ext_name Main class name (eg ImageIO as opposed to ImageIOTheme or ImageIOTest)
|
||||
* @return bool
|
||||
*/
|
||||
function ext_is_live($ext_name) {
|
||||
|
|
|
@ -213,7 +213,7 @@ class CronUploader extends Extension {
|
|||
|
||||
/**
|
||||
* Returns amount of files & total size of dir.
|
||||
* @param $path string
|
||||
* @param string $path directory name to scan
|
||||
* @return multitype:number
|
||||
*/
|
||||
function scan_dir($path){
|
||||
|
@ -234,7 +234,7 @@ class CronUploader extends Extension {
|
|||
|
||||
/**
|
||||
* Uploads the image & handles everything
|
||||
* @param $upload_count int to upload a non-config amount of imgs
|
||||
* @param int $upload_count to upload a non-config amount of imgs
|
||||
* @return boolean returns true if the upload was successful
|
||||
*/
|
||||
public function process_upload($upload_count = 0) {
|
||||
|
|
|
@ -86,7 +86,7 @@ class IcoFileHandler extends Extension {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @param string $file
|
||||
* @return bool
|
||||
*/
|
||||
private function check_contents($file) {
|
||||
|
@ -98,7 +98,7 @@ class IcoFileHandler extends Extension {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param $hash
|
||||
* @param string $hash
|
||||
* @return bool
|
||||
*/
|
||||
private function create_thumb($hash) {
|
||||
|
|
|
@ -11,98 +11,124 @@
|
|||
* 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
|
||||
class ImageViewCounter 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");
|
||||
# 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);
|
||||
$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
|
||||
}
|
||||
# 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) {
|
||||
# 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 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;
|
||||
}
|
||||
|
||||
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo
|
||||
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 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
|
||||
* @param int $imgid
|
||||
*/
|
||||
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
|
||||
* @param int $imgid
|
||||
*/
|
||||
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
|
||||
* @param int $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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue