#!/usr/bin/perl -I.
use strict;
use Pos::Webtop::Web::ContentModule;
use Pos::Policy::LayeredBurrito ();
use Pos::Webtop::OcxLauncher;
package Pos::Webtop::Web::ContentModule::Intranet;
my $global_scheme = "https";
our @ISA = qw(
Pos::Webtop::Web::ContentModule
);
sub new
{
my $this = shift;
my $class = ref($this) || $this;
my ($self) = @{{@_}}{qw/self/};
if ( not defined $self )
{
$self = {};
bless($self,$class);
}
# setup base class
my %args = @_;
$args{'self'} = $self;
Pos::Webtop::Web::ContentModule->new( %args );
return $self;
}
# (pure virtual defined in Pos::Webtop::Web::ContentModule)
sub main
{
my $self = shift;
my $toolBox = $self->getToolBox();
my $client = $toolBox->getClient();
my $cgi = $toolBox->getCgi();
my $ret = undef;
my $action = $cgi->param('action');
not defined($action) and $action = 0;
$client->checkSession();
if (($action ne 'rwPopup') and ($action ne 'rwGetPage') and ($action ne 'rwTopframe'))
{
my $p = $toolBox->getPage();
if (!$action) {
print $p->getHeaderHtml(title => "Webtop: Intranet interface: Main") . "
";
$lastCategory = $category;
if ($currTd eq 'BOXTD2') { $currTd = 'BOXTD3'; }
else { $currTd = 'BOXTD2'; }
}
# This part prints bookmarks that weren't in a category
if (scalar(@generalBookmarks) > 0) {
if (defined($lastCategory)) {
print "
";
}
$lastCategory = 'No Grouping';
print"
No Grouping
";
foreach my $general (@generalBookmarks) {
my $cleanName = $general->{'name'};
$cleanName =~ s/\\/\\\\/g;
$cleanName =~ s/\'/\\\'/g;
print "
"
}
print " ";
print $box1->getFooterHtml();
}
sub printPersonalBookmarks {
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbReader();
my $client = $tb->getClient();
my ($manage) = @{{@_}}{qw/manage/};
my $isMobile = $client->isMobileWebtop();
my $sessionId = $client->getSessionId;
my $sessionHash = $client->getSessionHash;
if (not defined($manage)) { $manage = 0; }
my $userId = $client->getUserId;
my $lastCategory = undef;
if ($manage) {
print "
";
$self->printPersonalBookmarkAdditionBoxes();
}
my $box1 = $tb->createBox(title => (($manage && !$isMobile)?"Remove Bookmarks":"Personal Bookmarks"), width => '300');
my @bookmarks = $db->query(sql => "
select
wubc.category_name,
wub.display_name,
wub.url
from
w_user_bookmarks wub,
w_user_bookmark_categories wubc
where
wub.category_id = wubc.category_id
AND wubc.user_id = " . $client->getUserId() . "
order by
wubc.category_name,
wub.display_name
asc");
print $box1->getHeaderHtml();
if ($manage == 1)
{
!$isMobile and print "
You have chosen to manage your bookmarks.
You can remove categories and bookmarks by clicking on them.
";
}
else
{
print "
" . $client->getFormGetCode(
text => "Manage Bookmarks",
values => [
{
name => "action",
value => "manageBookmarks"
}
]) . "
";
print scalar(@bookmarks) . " personal bookmarks
";
}
if (scalar(@bookmarks) > 0)
{
print "
";
}
foreach my $bookmark (@bookmarks)
{
my ($category, $displayName, $bookmarkUrl) = @{ $bookmark };
if ($category ne $lastCategory)
{
if (defined($lastCategory))
{
print "
";
print $bookmarkBox->getFooterHtml();
print " ";
}
sub printAddressBar
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbWriter();
my $client = $tb->getClient();
my ($nobox,$isMobile) = @{{@_}}{qw/nobox isMobile/};
my $box;
my $buf = "";
if (not defined($nobox) or $nobox != 1) {
$box = $tb->createBox(title => "Browse the Intranet", width => '300');
print $box->getHeaderHtml;
}
$buf .= "
";
if (not defined($nobox) or $nobox != 1) {
print $buf . $box->getFooterHtml;
}
return $buf;
}
sub manageCreateCategory
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbWriter();
my $client = $tb->getClient();
my ($categoryName) = @{{@_}}{qw/categoryName/};
if (not defined ($categoryName)) { return; }
my $cleanCategoryName = $db->escape(dirty => $categoryName);
my ($exists) = $db->query(sql => "
select
category_name
from
w_user_bookmark_categories
where
category_name = '$cleanCategoryName'
AND user_id = '" . $client->getUserId() . "'
");
if (not defined($exists)) {
$db->do(sql => "
insert into
w_user_bookmark_categories
(
user_id,
category_id,
category_name
) values(
" . $client->getUserId() . ",
" . $db->getNextValueString( sequence => 'w_user_bookmark_categories_pk') . ",
'$cleanCategoryName'
)");
}
print "
Successfully created category '$categoryName'.
";
}
sub manageCreateBookmark
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbWriter();
my $client = $tb->getClient();
my ($bookmarkName, $bookmarkUrl, $bookmarkCategory) = @{{@_}}{qw/bookmarkName bookmarkUrl bookmarkCategory/};
if (not defined($bookmarkCategory)) {
print "
You did not specify a category.
";
return;
}
my $cleanBookmarkName = $db->escape(dirty => $bookmarkName);
my $cleanBookmarkUrl = $db->escape(dirty => $bookmarkUrl);
my $cleanBookmarkCatId= $db->escape(dirty => $bookmarkCategory);
$db->do(sql => "
insert into
w_user_bookmarks
(
category_id,
user_bookmark_id,
url,
display_name
) values (
'$cleanBookmarkCatId',
(
select " .
( ( Pos::defaults::isPostgresDb()) ? "case when max(user_bookmark_id) is null then 0 else max(user_bookmark_id) end + 1" :
"decode(max(user_bookmark_id), null, 0, max(user_bookmark_id))+1" ) . "
from
w_user_bookmarks wub,
w_user_bookmark_categories wubc
where
wubc.category_id = wub.category_id
AND wubc.user_id = '" . $client->getUserId . "'
),
'$cleanBookmarkUrl',
'$cleanBookmarkName'
)");
print "
Successfully created bookmark '$bookmarkName'.
";
}
sub manageRemoveThings
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbWriter();
my $client = $tb->getClient();
my ($things) = @{{@_}}{qw/things/};
my $catRemove = 0;
my $bookRemove = 0;
foreach my $thing (@{ $things }) {
my ($type, $categoryName, $bookmarkName) = split /;/, $thing;
if ($type eq 'category') {
my $cleanCategoryName = $db->escape(dirty => $categoryName);
my ($numBooksRemoved) = $db->query(sql => "
select
count(user_bookmark_id)
from
w_user_bookmarks a
inner join w_user_bookmark_categories b on a.category_id = b.category_id
where
user_id = '" . $client->getUserId . "'
");
$db->do(sql => "
delete from
w_user_bookmark_categories
where
category_name = '$cleanCategoryName'
AND user_id = '" . $client->getUserId . "'
");
$bookRemove += $numBooksRemoved;
$catRemove++;
} else {
my $cleanBookmarkName = $db->escape(dirty => $bookmarkName);
my $cleanCategoryName = $db->escape(dirty => $categoryName);
$db->do(sql => "
delete from
w_user_bookmarks
where
user_bookmark_id = (
select
max(wub.user_bookmark_id)
from
w_user_bookmarks wub,
w_user_bookmark_categories wubc
where
wubc.category_name = '$cleanCategoryName'
AND wub.display_name = '$cleanBookmarkName'
AND wubc.category_id = wub.category_id
AND wubc.user_id = '" . $client->getUserId . "'
)
");
$bookRemove++;
}
}
my $message = "Successfully removed";
$message .= " $bookRemove bookmark" . (($bookRemove != 1)?"s":"") if ($bookRemove > 0);
$message .= " and " if ($bookRemove > 0 and $catRemove > 0);
$message .= " $catRemove categor" . (($catRemove != 1)?"ies":"y") if ($catRemove > 0);
print "
$message.
";
}
sub rwPopupHtml
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbReader();
my $client = $tb->getClient();
my ($url, $displayName) = @{{@_}}{qw/url displayName/};
my $sessionId = $client->getSessionId;
my $sessionHash = $client->getSessionHash;
my $oem = $tb->getOem;
$self->updateBookmarkRank(displayName => $displayName, url => $url);
print "Content-type: text/html\r\n\r\n
" . $oem->getSetting(setting => 'webtopProductName') . " Intranet Proxy
";
}
sub rwTopframeHtml
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbReader();
my $client = $tb->getClient();
my ($url, $notype) = @{{@_}}{qw/url notype/};
if (not defined($notype)) {
print "Content-type: text/html\r\n\r\n";
}
my $oem = $tb->getOem;
print "
" . $oem->getSetting(setting => 'webtopProductName') . " Intranet Browser
";
}
sub rwGetPageHtml
{
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbReader();
my $client = $tb->getClient();
my ($url, $initial) = @{{@_}}{qw/url initial/};
my $origurl = $url;
my $prefix = "http";
my $url_chars = "A-Za-z0-9\-\.";
my $scheme;
my $host;
my $port;
my $req;
my $out;
#add a scheme
if($url !~ /^https?:\/\//)
{
$url =~ s/(.*)/http:\/\/$1/;
}
#add a port
if($url =~ /^(https?):\/\//) {
$scheme = $1;
} else {
$scheme = "http";
}
if($url =~ /(^https?:\/\/|)([$url_chars]+\.?[$url_chars]*)(:.*|\/.*|$)/) {
$host = $2;
}
if($url =~ /(^https?:\/\/|)([$url_chars]+\.?[$url_chars]*):([\d]+)(\/.*|$)/)
{
$port = $3;
}
if($url =~ /(https?:\/\/|)[$url_chars]+\.[$url_chars]+(:[\d]{1-5}|)(\/[\S]+)/) {
$req = $3;
} else {
$req = "/";
}
my ($oemName) = $db->query(sql => "
select rewriter_prefix
from oems
where oem_id = (
select oem_id
from customers
where customer_id = (
select customer_id
from groups
where group_id = (
select group_id
from users
where user_id = '". $client->getUserId() . "'
)
)
)
");
if(not defined($oemName))
{
$oemName = Pos::defaults::getRewriterPrefix;
}
if($port) {
# $out = $global_scheme . "://rewrite.stage.positivenetworks.net/$scheme:\/\/$host:$port$req";
# $out = Pos::defaults::getRewriterPrefix."$scheme:\/\/$host:$port$req";
$out = "$oemName$scheme:\/\/$host:$port$req";
} else {
# $out = $global_scheme . "://rewrite.stage.positivenetworks.net/$scheme:\/\/$host$req";
# $out = Pos::defaults::getRewriterPrefix."$scheme:\/\/$host$req";
$out = "$oemName$scheme:\/\/$host$req";
}
$url = $out;
if (not defined($initial) or ($initial == 0)) {
$self->rwTopframeHtml(url => $origurl);
} else {
print "Content-type: text/html\r\n\r\n";
}
#print "
#
# ";
print "
";
}
sub updateBookmarkRank {
my $self = shift;
my $tb = $self->getToolBox();
my $db = $tb->getWebtopDbReader();
my $client = $tb->getClient();
my ($url, $displayName) = @{{@_}}{qw/url displayName/};
if (defined($displayName) and length($displayName) > 0) {
# Do mark up
my ($rankId) = $db->query(sql => "
select
bookmark_rank_id
from
w_user_bookmark_ranks
where
url = '" . $db->escape(dirty => $url) . "'
AND display_name = '" . $db->escape(dirty => $displayName) . "'
AND user_id = '" . $client->getUserId() . "'
");
if (not defined($rankId)) {
$db->do(sql => "
insert into
w_user_bookmark_ranks
(
user_id,
bookmark_rank_id,
url,
display_name,
hits
) values(
'" . $client->getUserId() . "',
" . $db->getNextValueString('w_user_bookmark_ranks_pk') . ",
'" . $db->escape(dirty => $url) . "',
'" . $db->escape(dirty => $displayName) . "',
1
)");
} else {
$db->do(sql => "
update
w_user_bookmark_ranks
set
hits = hits+1
where
bookmark_rank_id = '$rankId'
");
}
}
}
1;