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.
|
* Checks if a given string contains another at the beginning.
|
||||||
*
|
*
|
||||||
* @param $haystack String to examine.
|
* @param string $haystack String to examine.
|
||||||
* @param $needle String to look for.
|
* @param string $needle String to look for.
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function startsWith(/*string*/ $haystack, /*string*/ $needle) {
|
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.
|
* Checks if a given string contains another at the end.
|
||||||
*
|
*
|
||||||
* @param $haystack String to examine.
|
* @param string $haystack String to examine.
|
||||||
* @param $needle String to look for.
|
* @param string $needle String to look for.
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function endsWith(/*string*/ $haystack, /*string*/ $needle) {
|
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
|
* from the "Amazon S3 PHP class" which is Copyright (c) 2008, Donovan Schönknecht
|
||||||
* and released under the 'Simplified BSD License'.
|
* and released under the 'Simplified BSD License'.
|
||||||
*
|
*
|
||||||
* @param string &$file File path
|
* @param string $file File path
|
||||||
* @param string $ext
|
* @param string $ext
|
||||||
* @param bool $list
|
* @param bool $list
|
||||||
* @return string
|
* @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
|
* @return bool
|
||||||
*/
|
*/
|
||||||
function ext_is_live($ext_name) {
|
function ext_is_live($ext_name) {
|
||||||
|
|
|
@ -213,7 +213,7 @@ class CronUploader extends Extension {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns amount of files & total size of dir.
|
* Returns amount of files & total size of dir.
|
||||||
* @param $path string
|
* @param string $path directory name to scan
|
||||||
* @return multitype:number
|
* @return multitype:number
|
||||||
*/
|
*/
|
||||||
function scan_dir($path){
|
function scan_dir($path){
|
||||||
|
@ -234,7 +234,7 @@ class CronUploader extends Extension {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uploads the image & handles everything
|
* 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
|
* @return boolean returns true if the upload was successful
|
||||||
*/
|
*/
|
||||||
public function process_upload($upload_count = 0) {
|
public function process_upload($upload_count = 0) {
|
||||||
|
|
|
@ -86,7 +86,7 @@ class IcoFileHandler extends Extension {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $file
|
* @param string $file
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function check_contents($file) {
|
private function check_contents($file) {
|
||||||
|
@ -98,7 +98,7 @@ class IcoFileHandler extends Extension {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $hash
|
* @param string $hash
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
private function create_thumb($hash) {
|
private function create_thumb($hash) {
|
||||||
|
|
|
@ -11,98 +11,124 @@
|
||||||
* This is done to prevent duplicate views.
|
* This is done to prevent duplicate views.
|
||||||
* A person can only count as a view again 1 hour after viewing the image initially.
|
* A person can only count as a view again 1 hour after viewing the image initially.
|
||||||
*/
|
*/
|
||||||
class image_view_counter extends Extension {
|
class ImageViewCounter extends Extension {
|
||||||
private $view_interval = 3600; # allows views to be added each hour
|
private $view_interval = 3600; # allows views to be added each hour
|
||||||
|
|
||||||
# Add Setup Block with options for view counter
|
# Add Setup Block with options for view counter
|
||||||
public function onSetupBuilding(SetupBuildingEvent $event) {
|
public function onSetupBuilding(SetupBuildingEvent $event) {
|
||||||
$sb = new SetupBlock("Image View Counter");
|
$sb = new SetupBlock("Image View Counter");
|
||||||
$sb->add_bool_option("image_viewcounter_adminonly", "Display view counter only to admin");
|
$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
|
# Adds view to database if needed
|
||||||
public function onDisplayingImage(DisplayingImageEvent $event) {
|
public function onDisplayingImage(DisplayingImageEvent $event) {
|
||||||
$imgid = $event->image->id; // determines image id
|
$imgid = $event->image->id; // determines image id
|
||||||
$this->addview($imgid); // adds a view
|
$this->addview($imgid); // adds a view
|
||||||
}
|
}
|
||||||
|
|
||||||
# display views to user or admin below image if allowed
|
# display views to user or admin below image if allowed
|
||||||
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) {
|
public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) {
|
||||||
global $user, $config;
|
global $user, $config;
|
||||||
|
|
||||||
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo0
|
$adminonly = $config->get_bool("image_viewcounter_adminonly"); // todo
|
||||||
if ($adminonly == false || ($adminonly && $user->is_admin()))
|
if ($adminonly == false || ($adminonly && $user->is_admin()))
|
||||||
$event->add_part("<tr><th>Views:</th><td>".
|
$event->add_part(
|
||||||
$this->get_view_count($event->image->id) ."</th></tr>", 38);
|
"<tr><th>Views:</th><td>".
|
||||||
}
|
$this->get_view_count($event->image->id) .
|
||||||
|
"</th></tr>", 38);
|
||||||
|
}
|
||||||
|
|
||||||
# Installs DB table
|
# Installs DB table
|
||||||
public function onInitExt(InitExtEvent $event) {
|
public function onInitExt(InitExtEvent $event) {
|
||||||
global $database, $config;
|
global $database, $config;
|
||||||
|
|
||||||
// if the sql table doesn't exist yet, create it
|
// if the sql table doesn't exist yet, create it
|
||||||
if($config->get_bool("image_viewcounter_installed") == false) { //todo
|
if($config->get_bool("image_viewcounter_installed") == false) { //todo
|
||||||
$database->create_table("image_views","
|
$database->create_table("image_views","
|
||||||
id SCORE_AIPK,
|
id SCORE_AIPK,
|
||||||
image_id INTEGER NOT NULL,
|
image_id INTEGER NOT NULL,
|
||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
timestamp INTEGER NOT NULL,
|
timestamp INTEGER NOT NULL,
|
||||||
ipaddress SCORE_INET NOT NULL");
|
ipaddress SCORE_INET NOT NULL");
|
||||||
$config->set_bool("image_viewcounter_installed", true);
|
$config->set_bool("image_viewcounter_installed", true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Adds a view to the item if needed
|
/**
|
||||||
private function addview($imgid)
|
* Adds a view to the item if needed
|
||||||
{
|
* @param int $imgid
|
||||||
global $database, $user;
|
*/
|
||||||
|
private function addview($imgid)
|
||||||
|
{
|
||||||
|
global $database, $user;
|
||||||
|
|
||||||
// don't add view if person already viewed recently
|
// don't add view if person already viewed recently
|
||||||
if ($this->can_add_view($imgid) == false) return;
|
if ($this->can_add_view($imgid) == false) return;
|
||||||
|
|
||||||
// Add view for current IP
|
// Add view for current IP
|
||||||
$database->execute("INSERT INTO image_views (image_id, user_id, timestamp, ipaddress)
|
$database->execute(
|
||||||
VALUES (:image_id, :user_id, :timestamp, :ipaddress)", array(
|
"
|
||||||
"image_id" => $imgid,
|
INSERT INTO image_views (image_id, user_id, timestamp, ipaddress)
|
||||||
"user_id" => $user->id,
|
VALUES (:image_id, :user_id, :timestamp, :ipaddress)
|
||||||
"timestamp" => time(),
|
",
|
||||||
"ipaddress" => $_SERVER['REMOTE_ADDR'],
|
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)
|
* Returns true if this IP hasn't recently viewed this image
|
||||||
{
|
* @param int $imgid
|
||||||
global $database;
|
*/
|
||||||
|
private function can_add_view($imgid)
|
||||||
|
{
|
||||||
|
global $database;
|
||||||
|
|
||||||
// counts views from current IP in the last hour
|
// counts views from current IP in the last hour
|
||||||
$recent_from_ip = (int)$database->get_one("SELECT COUNT(*) FROM image_views WHERE
|
$recent_from_ip = (int)$database->get_one(
|
||||||
ipaddress=:ipaddress AND timestamp >:lasthour AND image_id =:image_id",
|
"
|
||||||
array( "ipaddress" => $_SERVER['REMOTE_ADDR'],
|
SELECT COUNT(*)
|
||||||
"lasthour" => time() - $this->view_interval,
|
FROM image_views
|
||||||
"image_id" => $imgid));
|
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 no views were found with the set criteria, return true
|
||||||
if($recent_from_ip == 0) return true;
|
if($recent_from_ip == 0) return true;
|
||||||
else return false;
|
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
|
* Returns the int of the view count from the given image id
|
||||||
private function get_view_count($imgid = 0)
|
* @param int $imgid - if not set or 0, return views of all images
|
||||||
{
|
*/
|
||||||
global $database;
|
private function get_view_count($imgid = 0)
|
||||||
|
{
|
||||||
|
global $database;
|
||||||
|
|
||||||
if ($imgid == 0) // return view count of all images
|
if ($imgid == 0) // return view count of all images
|
||||||
$view_count = (int)$database->get_one("SELECT COUNT(*) FROM image_views");
|
$view_count = (int)$database->get_one(
|
||||||
else // return view count of specified image
|
"SELECT COUNT(*) FROM image_views"
|
||||||
$view_count = (int)$database->get_one("SELECT COUNT(*) FROM image_views WHERE ".
|
);
|
||||||
"image_id =:image_id", array("image_id" => $imgid));
|
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
|
// returns the count as int
|
||||||
return $view_count;
|
return $view_count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Reference in a new issue