#!/usr/bin/perl # PBaseUpload # Author - Srijith (http://srijith.net) # License - GPL Version 2 or higher # Licensed on Feb 16, 2005. # Tool to upload snaps directly to PBase # Written for Windows, but should be portable to Linux use strict; use WWW::Mechanize; use HTML::TokeParser; use HTTP::Cookies; use Carp::Heavy; use XML::TreeBuilder; use URI::Escape; use Term::ReadKey; use Crypt::DES; use Crypt::CBC; my $Version=0.6; my $DEBUG=0; my $file_to_upload = shift || blurp_intro(1); my $password_file='C:/pbase-pwd'; my $key='random'; my $username_cipher=""; my $username =""; my $passwd=""; my $passwd_cipher=""; my $proxy="none"; my $gallery_name_list; my $gallery_url_list; my %gallery_list=(); my $secure_base_url='https://secure.pbase.com'; my $base_url='http://www.pbase.com'; my $upload_base_url='http://upload.pbase.com'; my $create_gallery_base_url='http://www.pbase.com/'; my $gallery_listing_xml="$base_url/client?view=galleries"; my $line; #Intro Blurp# blurp_intro(); again: open(PASSWDFILE,"<$password_file") || ask_passwd(); if($line eq "") { $line=; ($username_cipher,$passwd_cipher)=split /\|\|/,$line; close PASSWDFILE; my $cipher = new Crypt::CBC($key,'DES'); $cipher->start('decrypting'); $username=$cipher->decrypt($username_cipher); $passwd=$cipher->decrypt($passwd_cipher); $cipher->finish(); } print "\n\nUsername=$username, Password=$passwd\n" if $DEBUG; print "Logging into your account\n"; my $cookie_jar= HTTP::Cookies->new(); my $agent = WWW::Mechanize->new(); $agent->cookie_jar($cookie_jar); $agent->get("$base_url/logout"); #logout any earlier sessions if ( $agent->success() ) { #$agent->form_name('login'); $agent->form_number(1); $agent->field("login_id", $username); $agent->field("password", $passwd); $agent->click(); print_choose_gallery(); } else { print "Connection Error!"; } sleep (1); exit; sub blurp_intro () { print " -- PBaseUpload (v $Version) --\n"; print 'http://srijith.net/codes/pbaseupload/'; print "\n\n"; if(shift) { exit; } } sub print_choose_gallery(){ print "\nRetrieving your gallery list...\n"; my $cookie_jar1= HTTP::Cookies->new(); my $agent1 = WWW::Mechanize->new(); $agent1->cookie_jar($cookie_jar1); $agent1->get("$base_url/logout"); #$agent1->form_name('login'); $agent1->form_number(1); $agent1->field("login_id", $username); $agent1->field("password", $passwd); $agent1->click(); $agent1->get($gallery_listing_xml); my $temp_page=$agent1->content; $temp_page =~ s/(\)(.*?)(\<\/title\>)/"$1".uri_escape($2)."$3"/eg; $temp_page =~ s/(\)(.*?)(\<\/name\>)/"$1".uri_escape($2)."$3"/eg; my $tree = XML::TreeBuilder->new(); eval { $tree->parse($temp_page) }; my $i=0;my $j=0;my $input=0; foreach my $gallery ($tree->find_by_tag_name('gallery')){ $i=$i+1; $gallery_list{$i}{'id'}=$gallery->find_by_tag_name('id')->as_text; $gallery_list{$i}{'name'}=$gallery->find_by_tag_name('name')->as_text; $gallery_list{$i}{'title'}=$gallery->find_by_tag_name('title')->as_text; } my $total_galleries=keys(%gallery_list); if ($total_galleries < 1) { print "*** Your login failed! Try again ***\n"; goto again; } print "\nDo you want to create a new gallery and upload into it? Choose \'0\'"; print "\nOr do you want to upload into an existing gallery? Choose \'1\'\nChoice:"; my $input=; chomp $input; my $upload_to_existing_gallery=$input; print "\n\n -- Your gallery list --\n\n"; my $unescaped_title=''; for ($j=0;$j<($total_galleries/10);$j++) { my $end=($j*10+10>$total_galleries)?$total_galleries:$j*10+10; for ($i=($j*10)+1; $i<=$end;$i++) { $unescaped_title=uri_unescape($gallery_list{$i}{'title'}); print "$i - $unescaped_title ($gallery_list{$i}{'name'})\n" ; } if($upload_to_existing_gallery == 0) { print "\nChoose parent gallery from the list above, enter the number to the left of the gallery name."; } else { print "\nTo choose which existing galley to upload to,\nenter the number to the left of the gallery name."; } if ($end < $total_galleries) { print "\n\nTo list more galleries, press 0."; } print "\nYour choice? :"; $input = ; chomp $input; if($input != 0) { last; } else { if ($end == $total_galleries) {print "\n\nHuh? You don't have any more galleries! Quit playing games!\n\n";exit;} } print "\n"; } my $gallery_choice=$input; if($upload_to_existing_gallery==0) { create_new_gallery($gallery_choice); } else { upload_to_gallery($gallery_choice); } } sub upload_to_gallery() { my $choice=shift; print "\nYou have chosen to upload to \"$gallery_list{$choice}{'name'}\"\n"; my $gallery_id=$gallery_list{$choice}{'id'}; my $gallery_name=$gallery_list{$choice}{'name'}; my $gallery_upload_url="$upload_base_url/edit_gallery/$username/$gallery_name"; print "Going to $gallery_upload_url\n" if $DEBUG; print "\nStarting upload. Please wait. This might take some time...\n"; $agent->get($gallery_upload_url); $agent->form_number(3); $agent->field("fname",$file_to_upload); $agent->click(); print "\nUpload complete!\n"; } sub create_new_gallery () { my $choice=shift; my $parent_gallery_name=$gallery_list{$choice}{'name'}; print "\nEnter you new gallery title:"; my $input = ; chomp $input; my $gallery_title=$input; print "\nEnter your new gallery name\n(Spaces not allowed. Use \"_\" to connect words eg. my_snaps):"; $input = ; chomp $input; my $gallery_name=$input; $gallery_name =~ s/ /_/gi; print "\nEnter your new gallery description:"; $input = ; chomp $input; my $gallery_description=$input; ## CREATE NEW GALLERY ## print "\nHold on! Off I go to create your gallery\n"; $agent->get("$upload_base_url/edit_gallery/$username/$parent_gallery_name"); print "Going to - $upload_base_url/edit_gallery/$username/$parent_gallery_name\n" if $DEBUG; $agent->form_number(4); $agent->field("gallery_name",$gallery_name); $agent->untick( "root_flag",'Y'); $agent->click(); print "Created the gallery. Wait, there is more...\n\nUpdating gallery details..."; ## UPDATE GALLERY DETAILS ## $agent->get("$upload_base_url/edit_gallery/$username/$gallery_name"); $agent->form_number(1); $agent->field("title",$gallery_title); $agent->field("description",$gallery_description); $agent->click(); print "done!\n"; print "\nStarting upload. Please wait. This might take some time...\n"; $agent->get("http://upload.pbase.com/edit_gallery/$username/$gallery_name"); $agent->form_number(3); $agent->field("fname",$file_to_upload); $agent->click(); print "\nUpload complete!\n"; } sub ask_passwd { print "Enter your PBase username:"; my $input = ; chomp $input; $username=$input; print "Enter password (will not be printed on screen):"; ReadMode('noecho'); $passwd = ReadLine(0); ReadMode('restore'); chomp($passwd); print "\n\n"; my $cipher = new Crypt::CBC($key,'DES'); $cipher->start('encrypting'); $username_cipher = $cipher->encrypt($username); $passwd_cipher = $cipher->encrypt($passwd); $cipher->finish(); $line="$username_cipher||$passwd_cipher"; open(PASSWDFILE,">$password_file"); print PASSWDFILE $line; close PASSWDFILE; }