📄 main.pm
字号:
#!/usr/bin/perl -w## Copyright (C) 2005 Luke Reeves## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307# USA#package Insipid::Main;use vars qw(@ISA @EXPORT @EXPORT_OK);require Exporter;@ISA = qw(Exporter);@EXPORT = qw(main);use strict;use Insipid::Bookmarks;use Insipid::Config;use Insipid::Database;use Insipid::Snapshots;use Insipid::Tags;use Insipid::Util;use CGI qw/:standard/;use CGI::Carp qw(fatalsToBrowser);use URI::Escape;use IO::File;use XML::Parser;use XML::Writer;use Date::Format;use Date::Parse;use DBI qw/:sql_types/;;use Digest::MD5 qw(md5 md5_hex);use MIME::Base64;use CGI::Session;use LWP::UserAgent;use HTTP::Request;use HTTP::Response;my $NL = "<br />\n";my @valid;my $duplicates = 0;my %options;my $tspec = "";my $query = "";my $last_page = 0;my $icount = 0; # For tracking importsmy $site_title;if(!defined($ENV{SERVER_NAME})) { $NL = "\n";}sub main { my $username = getconfig("username"); my $userpass = getconfig("userpass"); my $redirect = ""; my $et = ""; # Valid options: @valid = ("feed_name", "site_name", "public_searches", "proxy_host", "proxy_port"); # Create/find the session my $ctag = "INSIPID_$username"; my $sid = cookie($ctag) || undef; my $session = undef; if ($dbtype eq 'Pg') { $session = new CGI::Session("driver:PostgreSQL", $sid, {DataSource=>$dsn, User=>$dbuser, Password=>$dbpass}) or die $!; } else { $session = new CGI::Session("driver:MySQL", $sid, {DataSource=>$dsn, User=>$dbuser, Password=>$dbpass}) or die $!; } my $sessid = $session->id; print "Set-Cookie: $ctag=$sessid; path=/; expires=Fri, 03-Sep-2020 20:20:13 GMT/\n"; # Get the basic options $site_title = get_option("site_name"); if($site_title eq "") { $site_title = "Insipid Bookmarks"; } # Initialize variables that can be posted and in the URL. if(defined(url_param('q'))) { $query = url_param('q'); } if(defined(param('q'))) { $query = param('q'); } # Check to see if a username and password have been posted if(defined(param('password')) && defined(param('username'))) { if( (param('password') eq $userpass) && (param('username') eq $username) ) { $session->param('loggedin', 1); $logged_in = 1; } else { push(@errors, "Invalid username or password."); } } if($session->param('loggedin')) { $logged_in = 1; } # Operations for non-HTML content if(defined(url_param('op'))) { if(url_param('op') eq 'export') { if(defined(param('target'))) { my $filename = ""; if(param('filename')) { $filename = param('filename'); } do_export(param('target'), $filename); } } # RSS if(url_param('op') eq 'rss') { print "Content-Type: text/xml\r\n\r\n"; send_rss(); exit; } # Cache if(url_param('op') eq 'viewsnapshot') { check_access(); if(param('md5')) { show_snapshot(param('md5')); } } } # Allow redirections to a bookmark if the user's logged in. # This allows private bookmarks to not send a referer. if($logged_in eq 1) { if(param('go')) { my $bid = param('go'); my $sql = "select url from bookmarks where (bookmarks.id = ?)"; my $sth = $dbh->prepare($sql); $sth->execute($bid); my $hr = $sth->fetchrow_hashref; if(defined($hr->{'url'})) { print "Cache-Control: private, must-revalidate\n"; print "Content-Type: text/html; charset=UTF-8\n\n"; print "<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=$hr->{'url'}\">\n"; exit; } else { push(@errors, "Bookmark does not exist."); } } } # Add description to the HTML title tag. if(url_param('tag')) { $tspec = "/" . url_param('tag'); $tspec =~ s/ /\+/g; my $tt = url_param('tag'); $tt =~ s/ / \+ /g; $et = sprintf(" - %s", $tt); } if($query ne "") { $et = sprintf(" - search results for \"%s\"", $query); } if($logged_in eq 1) { print "Cache-Control: private, must-revalidate\n"; } print "Content-Type: text/html; charset=UTF-8\n\n"; print <<DOC; <html> <head> <title>$site_title$et</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="alternate" type="application/rss+xml" title="RSS" href="$site_url/feeds/bookmarks$tspec" /> <link rel="stylesheet" href="$site_url/insipid.css" type="text/css" title="Standard" /> </head> <body marginheight="0" marginwidth="0">DOC ###### Operations that don't touch the screen if(defined(param('op')) && defined(param('id'))) { if(param('op') eq 'delete_bookmark') { my $id = param('id'); delete_bookmark($id); } } if(defined(param('op'))) { if(param('op') eq 'logout') { $session->delete(); } } # If the user just saved a bookmark, redirect them now. if($redirect ne "") { print "<script language=\"JavaScript\">document.location = \"$redirect\";</script>"; print "</body></html>"; exit; } show_toolbar(); show_tags(); if(defined(url_param('op'))) { if(url_param('op') eq 'export') { if(!defined(param('target'))) { print "<br /><br /><form method=\"post\" class=\"formText\">"; print "Target:<br />"; print "<input type=\"radio\" checked=\"checked\" name=\"target\" value=\"L\" />"; print "Download<br />"; print "<input type=\"radio\" name=\"target\" value=\"S\" />"; print "Server-side file - <input type=\"text\" "; print "name=\"filename\" /><br />"; print "<input type=\"checkbox\" name=\"snapshots\" />Include Snapshots<br />"; print "<input type=\"submit\" value=\"Export\" /></form>"; } } if(url_param('op') eq 'import') { check_access(); if(param('from')) { do_import(); } else { print <<IFORM; <br /><br /> <form class="formtext" enctype="multipart/form-data" action="$site_url/insipid.cgi?op=import" method="post"> Import from:<br /> <input type="radio" name="from" value="S" />Server-side file - <input type="text" name="filename" /><br /> <input type="radio" name="from" value="L" checked="checked" />Uploaded file - <input type="file" name="fileupload" size="30"><br /> <input type="hidden" name="op" value="import" /> <input type="submit" value="Import" /> </form>IFORM } } } if(defined(param('op'))) { if(param('op') eq 'login') { login_form(); } if((param('op') eq 'add_bookmark') || (param('op') eq 'edit_bookmark') ) { #check to see if the url is bookmarked, then indicate that this is an edit. my ($id, $url, $title, $description, $button, $tags, $extra_params, $access_level, $access_box) = (-1, "", "", "", "", "", "", 0, ""); $access_level = 0; if(defined(param('save'))) { ($url, $title, $description, $tags) = (param('url'), param('title'), param('description'), param('tags')); if(defined(param('access_level'))) { if(param('access_level') eq 'on') { $access_level = 1; } else { $access_level = 0; } } if(param('id')) { update_bookmark(param('id'), $url, $title, $description, $access_level, $tags); } else { add_bookmark($url, $title, $description, $access_level, 0, $tags); if(param('snapshot')) { if(param('snapshot') eq 'on') { $id = get_bookmark_id(param('url')); do_snapshot($id); } } } if(param('redirect')) { if(param('redirect') eq 'on') { if(@errors eq 0) { $redirect = $url; } } } } else { # Show the form, populating from the database if it's an existing entry. my $utext = "URL:"; my $snapshot_params = ""; $id = "-1"; if(defined(param('id'))) { $id = param('id'); } if(defined(url_param('id'))) { $id = url_param('id'); } if($id eq "-1") { $id = get_bookmark_id(param('url')); } if($id ne -1) { ($url, $title, $description, $access_level) = get_bookmark($id); $tags = get_tags($url); $button = "Save"; $utext = "<span style=\"color:red\">URL (already bookmarked):</span>"; $extra_params = "<input type=\"hidden\" name=\"id\" value=\"$id\" />"; } else { # There has to be a nicer way to do this. if(param('url')) { $url = param('url'); } if(param('title')) { $title = param('title'); } if(param('description')) { $description = param('description'); } $access_level = 1; $button = "Add"; $snapshot_params = "<span class=\"formtext\">Snapshot:</span><input type=\"checkbox\" name=\"snapshot\" />\n"; } my $style = "style=\"width:500px\""; my $redir = "off"; my $redir_box = ""; if(param('redirect')) { if(param('redirect') eq 'on') { $redir = 'on'; } if(param('redirect') eq 'true') { $redir = 'on'; } } if($access_level eq 0) { $access_box = ""; } else { $access_box = "checked=\"true\" "; } if($redir eq 'on') { $redir_box = "checked=\"true\""; } print <<FORM; <br /> <form method="post"> <span class="formtext">$utext</span><br /> <input name="url" $style value="$url" /><br /> <span class="formtext">Title:</span><br /> <input name="title" $style value="$title" /><br /> <span class="formtext">Description:</span><br /> <input name="description" $style value="$description" /><br /> <span class="formtext">Tags:</span><br /> <input name="tags" $style value="$tags" /><br /> $snapshot_params <span class="formtext">Public:</span> <input type="checkbox" name="access_level" $access_box /> <span class="formtext">Return:</span> <input type="checkbox" name="redirect" $redir_box /> <input type="hidden" name="save" value="true" /> <input type="hidden" name="op" value="add_bookmark" /> $extra_params <input type="submit" value="$button" /> </form>FORM } } } # Late redirects. TODO: Get rid of this. if($redirect ne "") { print "<script language=\"JavaScript\">document.location = \"$redirect\";</script>"; print "</body></html>"; exit; } if(defined(param('op'))) { if($logged_in eq 1) { if(param('op') eq 'snapshots') { show_snapshots(); print "</body></html>"; exit; } if(param('op') eq 'snapshot') { if(defined(param('id'))) { do_snapshot(param('id')); print "</body></html>"; exit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -