| 1 | #!/usr/local/bin/perl -w | ||
| 2 | |||
| 3 | # $Id: serv1.pl,v 1.8 2000/12/21 22:18:07 aigan Exp $ -*-perl-*- | ||
| 4 | |||
| 5 | #===================================================================== | ||
| 6 | # | ||
| 7 | # DESCRIPTION | ||
| 8 | # CGI server for person records | ||
| 9 | # | ||
| 10 | # AUTHOR | ||
| 11 | # Jonas Liljegren <jonas@paranormal.se> | ||
| 12 | # | ||
| 13 | # COPYRIGHT | ||
| 14 | # Copyright (C) 2000 Jonas Liljegren. All Rights Reserved. | ||
| 15 | # | ||
| 16 | # This module is free software; you can redistribute it and/or | ||
| 17 | # modify it under the same terms as Perl itself. | ||
| 18 | # | ||
| 19 | #===================================================================== | ||
| 20 | |||
| 21 | use 5.006; | ||
| 22 | use strict; | ||
| 23 | use POSIX; | ||
| 24 | use IO::Socket 1.18; | ||
| 25 | use IO::Select; | ||
| 26 | use Socket; | ||
| 27 | use Data::Dumper; | ||
| 28 | use Carp; | ||
| 29 | use Time::HiRes qw( time ); | ||
| 30 | use CGI; | ||
| 31 | use Template 2; | ||
| 32 | use FreezeThaw qw( thaw ); | ||
| 33 | |||
| 34 | # TODO: use FindBin; | ||
| 35 | # use FindBin; use lib $FindBin::Bin; # Gives tainted data! | ||
| 36 | |||
| 37 | use lib "../lib"; | ||
| 38 | |||
| 39 | use Wraf::Result; | ||
| 40 | use RDF::Service; | ||
| 41 | use RDF::Service::Constants qw( :all ); | ||
| 42 | use RDF::Service::Cache qw( $Level debug get_unique_id reset_level | ||
| 43 | time_string $DEBUG ); | ||
| 44 | |||
| 45 | our $q = undef; | ||
| 46 | our $s = undef; | ||
| 47 | |||
| 48 | our $th = Template->new( | ||
| 49 | INTERPOLATE => 1, | ||
| 50 | INCLUDE_PATH => 'tmpl', | ||
| 51 | PRE_PROCESS => 'header', | ||
| 52 | POST_PROCESS => 'footer', | ||
| 53 | ); | ||
| 54 | |||
| 55 | |||
| 56 | { | ||
| 57 | my $port=7789; | ||
| 58 | |||
| 59 | # Set up the tcp server. Must do this before chroot. | ||
| 60 | my $server= IO::Socket::INET->new( | ||
| 61 | LocalPort => $port, | ||
| 62 | Proto => 'tcp', | ||
| 63 | Listen => 10, | ||
| 64 | Reuse => 1, | ||
| 65 | ) or (die "Cannot connect to socket $port: $@\n"); | ||
| 66 | |||
| 67 | print("Connected to port $port.\n"); | ||
| 68 | |||
| 69 | |||
| 70 | my %inbuffer=(); | ||
| 71 | my %length=(); | ||
| 72 | NonBlock($server); | ||
| 73 | my $select=IO::Select->new($server); | ||
| 74 | |||
| 75 | print("Setup complete, accepting connections.\n"); | ||
| 76 | |||
| 77 | open STDERR, ">/tmp/RDF-Service-0_04.log" or die $!; | ||
| 78 | |||
| 79 | main_loop: | ||
| 80 | while (1) | ||
| 81 | { | ||
| 82 | # The algorithm was adopted from perlmoo by Joey Hess | ||
| 83 | # <joey@kitenet.net>. | ||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | # warn "...\n"; | ||
| 88 | # my $t0 = [gettimeofday]; | ||
| 89 | |||
| 90 | my $client; | ||
| 91 | my $rv; | ||
| 92 | my $data; | ||
| 93 | |||
| 94 | # See if clients have sent any data. | ||
| 95 | # my @client_list = $select->can_read(1); | ||
| 96 | # print "T 1: ", tv_interval ( $t0, [gettimeofday]), "\n"; | ||
| 97 | |||
| 98 | foreach $client ($select->can_read(5)) | ||
| 99 | { | ||
| 100 | if ($client == $server) | ||
| 101 | { | ||
| 102 | # New connection. | ||
| 103 | my($iaddr, $address, $port, $peer_host); | ||
| 104 | $client = $server->accept; | ||
| 105 | if(!$client) | ||
| 106 | { | ||
| 107 | warn("Problem with accept(): $!"); | ||
| 108 | next; | ||
| 109 | } | ||
| 110 | ($port, $iaddr) = sockaddr_in(getpeername($client)); | ||
| 111 | $peer_host = gethostbyaddr($iaddr, AF_INET) || inet_ntoa($iaddr); | ||
| 112 | $select->add($client); | ||
| 113 | NonBlock($client); | ||
| 114 | |||
| 115 | warn "\n\nNew client connected\n" if $DEBUG > 3; | ||
| 116 | } | ||
| 117 | else | ||
| 118 | { | ||
| 119 | # Read data from client. | ||
| 120 | $data=''; | ||
| 121 | $rv = $client->recv($data,POSIX::BUFSIZ, 0); | ||
| 122 | |||
| 123 | warn "Read data...\n" if $DEBUG > 3; | ||
| 124 | |||
| 125 | unless (defined $rv && length $data) | ||
| 126 | { | ||
| 127 | # EOF from client. | ||
| 128 | CloseCallBack($client,'eof'); | ||
| 129 | warn "End of file\n"; | ||
| 130 | next; | ||
| 131 | } | ||
| 132 | |||
| 133 | $inbuffer{$client} .= $data; | ||
| 134 | unless( $length{$client} ) | ||
| 135 | { | ||
| 136 | warn "Length of record?\n" if $DEBUG > 3; | ||
| 137 | # Read the length of the data string | ||
| 138 | # | ||
| 139 | if( $inbuffer{$client} =~ s/^(\d+)\x00// ) | ||
| 140 | { | ||
| 141 | warn "Setting length to $1\n" if $DEBUG > 3; | ||
| 142 | $length{$client} = $1; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | if( $length{$client} ) | ||
| 147 | { | ||
| 148 | warn "End of record?\n" if $DEBUG > 3; | ||
| 149 | # Have we read the full record of data? | ||
| 150 | # | ||
| 151 | if( length $inbuffer{$client} >= $length{$client} ) | ||
| 152 | { | ||
| 153 | warn "The whole length read\n" if $DEBUG > 3; | ||
| 154 | handle_request( $client, \$inbuffer{$client} ); | ||
| 155 | $inbuffer{$client} = ''; | ||
| 156 | $length{$client} = 0; | ||
| 157 | CloseCallBack($client); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | sub NonBlock | ||
| 167 | { | ||
| 168 | my $socket=shift; | ||
| 169 | |||
| 170 | # Set a socket into nonblocking mode. I guess that the 1.18 | ||
| 171 | # defaulting to autoflush makes this function redundant | ||
| 172 | |||
| 173 | use Fcntl; | ||
| 174 | my $flags= fcntl($socket, F_GETFL, 0) | ||
| 175 | or die "Can't get flags for socket: $!\n"; | ||
| 176 | fcntl($socket, F_SETFL, $flags | O_NONBLOCK) | ||
| 177 | or die "Can't make socket nonblocking: $!\n"; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub CloseCallBack | ||
| 181 | { | ||
| 182 | my( $client, $reason ) = @_; | ||
| 183 | |||
| 184 | # Someone disconnected or we want to close the i/o channel. | ||
| 185 | |||
| 186 | delete $inbuffer{$client}; | ||
| 187 | $select->remove($client); | ||
| 188 | close($client); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub handle_request | ||
| 193 | { | ||
| 194 | my( $client, $recordref ) = @_; | ||
| 195 | |||
| 196 | my( $value ) = thaw( $$recordref ); | ||
| 197 | $q = $value->[0]; | ||
| 198 | %ENV = @{$value->[1]}; | ||
| 199 | |||
| 200 | # We can't have the CGI module trying to read POST data | ||
| 201 | $ENV{'REQUEST_METHOD'} = 'GET'; | ||
| 202 | |||
| 203 | my( $me ) = $ENV{'SCRIPT_NAME'} =~ m!/([^/]+)$!; | ||
| 204 | |||
| 205 | |||
| 206 | if( $DEBUG > 8 ) | ||
| 207 | { | ||
| 208 | $client->send( $q->header ); | ||
| 209 | $client->send( "<h1>Got something!</h1>" ); | ||
| 210 | $client->send("<plaintext>\n"); | ||
| 211 | foreach my $key ( $q->param() ) | ||
| 212 | { | ||
| 213 | my $value = $q->param($key); | ||
| 214 | $value =~ s/\x00/?/g; | ||
| 215 | $client->send(" $key:\t$value\n"); | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | warn "Constructing RDF::Service object\n" if $DEBUG; | ||
| 221 | my $offset = &dlines(); | ||
| 222 | my $result = new Wraf::Result; | ||
| 223 | my $s_cookie = $q->cookie('wraf_session'); | ||
| 224 | $Level = 0; | ||
| 225 | $s = &get_session( $s_cookie ); | ||
| 226 | |||
| 227 | my $params = | ||
| 228 | { | ||
| 229 | 'cgi' => $q, | ||
| 230 | 'me' => $me, | ||
| 231 | 'result' => $result, | ||
| 232 | 'ENV' => \%ENV, | ||
| 233 | 'VERSION' => $RDF::Service::Resource::VERSION, | ||
| 234 | 's' => $s, | ||
| 235 | |||
| 236 | 'NS_LS' => NS_LS, | ||
| 237 | 'NS_LD' => NS_LD, | ||
| 238 | 'NS_RDF' => NS_RDF, | ||
| 239 | 'NS_RDFS' => NS_RDFS, | ||
| 240 | |||
| 241 | 'dump' => \&Dumper, | ||
| 242 | 'reset' => \&reset_level, | ||
| 243 | 'offset' => $offset, | ||
| 244 | 'dlines' => \&dlines, | ||
| 245 | 'warn' => sub { debug "NOTE: $_[0]\n", 1; '' }, | ||
| 246 | }; | ||
| 247 | |||
| 248 | |||
| 249 | # Performe the actions (anything that changes the database) | ||
| 250 | # | ||
| 251 | my $action = $q->param('action'); | ||
| 252 | if( $action ) | ||
| 253 | { | ||
| 254 | eval | ||
| 255 | { | ||
| 256 | no strict 'refs'; | ||
| 257 | $result->message( &{'do_'.$action} ); | ||
| 258 | ### Other info is stored in $result->{'info'} | ||
| 259 | 1; | ||
| 260 | } | ||
| 261 | or $result->exception($@); | ||
| 262 | } | ||
| 263 | |||
| 264 | |||
| 265 | # Set the handler depending of the action result | ||
| 266 | # | ||
| 267 | my $handler = ""; | ||
| 268 | $handler = $q->param('previous') if $result->{'error'}; | ||
| 269 | $handler ||= ($q->param('handler')||'person_list'); | ||
| 270 | $params->{'handler'} = $handler; | ||
| 271 | warn "Porcessing template $handler\n" if $DEBUG > 3; | ||
| 272 | |||
| 273 | |||
| 274 | # Construct and return the response (handler) page | ||
| 275 | # | ||
| 276 | warn "\n********************\n". | ||
| 277 | "*** Returning page\n". | ||
| 278 | "********************\n\n" if $DEBUG; | ||
| 279 | $Level = 0; | ||
| 280 | $client->print( $q->header ); | ||
| 281 | my $handler_file = $handler; #.'.html'; | ||
| 282 | $th->process($handler_file, $params, $client) | ||
| 283 | or do | ||
| 284 | { | ||
| 285 | &reset_level; | ||
| 286 | warn "$$: Oh no!\n" if $DEBUG; #Some error sent to browser | ||
| 287 | my $error = $th->error(); | ||
| 288 | if( ref $error ) | ||
| 289 | { | ||
| 290 | $result->error($error->type(), | ||
| 291 | $error->info() | ||
| 292 | ); | ||
| 293 | } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | $result->error('funny', $error); | ||
| 297 | } | ||
| 298 | $th->process('error', $params, $client) | ||
| 299 | or die( "Fatal template error: ". | ||
| 300 | $th->error()."\n"); | ||
| 301 | }; | ||
| 302 | |||
| 303 | |||
| 304 | warn "Done!\n\n" if $DEBUG; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub get_session | ||
| 308 | { | ||
| 309 | my( $session_key ) = @_; | ||
| 310 | |||
| 311 | our %session; | ||
| 312 | |||
| 313 | if( $session{$session_key} ) | ||
| 314 | { | ||
| 315 | warn "Found old session $session_key\n" if $DEBUG; | ||
| 316 | return $session{$session_key}; | ||
| 317 | } | ||
| 318 | else | ||
| 319 | { | ||
| 320 | warn "New session $session_key\n" if $DEBUG; | ||
| 321 | $session_key =~ s/[^\w\-\.]//g; | ||
| 322 | |||
| 323 | $session_key = $q->param('s') if $q->param('s'); | ||
| 324 | |||
| 325 | my $session_res = new RDF::Service( NS_LD."/service/$session_key" ); | ||
| 326 | |||
| 327 | $session_res->connect("RDF::Service::Interface::DBI::V01", | ||
| 328 | { | ||
| 329 | connect => "dbi:Pg:dbname=wraf_v01a", | ||
| 330 | name => "wwwdata", | ||
| 331 | }); | ||
| 332 | $session_res->init_rev_subjs; | ||
| 333 | unless( $session_res->exist_pred( NS_LS.'#updated' ) ) | ||
| 334 | { | ||
| 335 | $session_res->set_props( | ||
| 336 | { NS_LS.'#updated' => [time_string()] }, 1 ); | ||
| 337 | } | ||
| 338 | |||
| 339 | return $session{$session_key} = $session_res; | ||
| 340 | } | ||
| 341 | |||
| 342 | } | ||
| 343 | |||
| 344 | ######## Action functions ######################### | ||
| 345 | |||
| 346 | sub do_person_add | ||
| 347 | { | ||
| 348 | # my $model = $s->get_model(NS_LD.'#M1'); | ||
| 349 | my $model = $s; | ||
| 350 | |||
| 351 | my $person = $model->get(); | ||
| 352 | |||
| 353 | |||
| 354 | my $r_fn = $q->param('r_fn') or die "No first name specified"; | ||
| 355 | my $r_ln = $q->param('r_ln') or die "No last name specified"; | ||
| 356 | |||
| 357 | # my $l_fn = $model->create_literal(NS_LD.'#Person_1-fn', \$r_fn); | ||
| 358 | # my $l_ln = $model->create_literal(NS_LD.'#Person_1-ln', \$r_ln); | ||
| 359 | my $l_fn = $model->create_literal(undef, \$r_fn); | ||
| 360 | my $l_ln = $model->create_literal(undef, \$r_ln); | ||
| 361 | |||
| 362 | my $types = [$model->get(NS_LD.'/Class#Person')]; | ||
| 363 | my $props = | ||
| 364 | { | ||
| 365 | NS_LD.'/Property#first_name' => [$l_fn], | ||
| 366 | NS_LD.'/Property#last_name' => [$l_ln], | ||
| 367 | }; | ||
| 368 | |||
| 369 | $person->set( $types, $props ); | ||
| 370 | |||
| 371 | return "Person created"; | ||
| 372 | } | ||
| 373 | |||
| 374 | sub do_person_delete | ||
| 375 | { | ||
| 376 | my $r_person = $q->param('r_person') or die "No node specified"; | ||
| 377 | # my $model = $s->get_model(NS_LD.'#M1'); | ||
| 378 | my $model = $s; | ||
| 379 | my $person = $model->get($r_person); | ||
| 380 | if( $person->delete_node_cascade() ) | ||
| 381 | { | ||
| 382 | return "Deleted person"; | ||
| 383 | } | ||
| 384 | else | ||
| 385 | { | ||
| 386 | return "Person NOT deleted"; | ||
| 387 | } | ||
| 388 | } | ||
| 389 | |||
| 390 | sub do_initiate_db | ||
| 391 | { | ||
| 392 | my $model = $s->get_model(NS_LD.'#M1'); | ||
| 393 | |||
| 394 | $model->get(NS_LD.'/Class#Person')->set( [NS_RDFS.'Class'] ); | ||
| 395 | $model->get(NS_LD.'/Property#first_name')->set( [NS_RDF.'Property'] ); | ||
| 396 | $model->get(NS_LD.'/Property#last_name')->set( [NS_RDF.'Property'] ); | ||
| 397 | |||
| 398 | return "DB initiated"; | ||
| 399 | } | ||
| 400 | |||
| 401 | sub do_person_edit | ||
| 402 | { | ||
| 403 | my $r_person = $q->param('r_person') or die "No node specified"; | ||
| 404 | # my $model = $s->get_model(NS_LD.'#M1'); | ||
| 405 | my $model = $s; | ||
| 406 | my $person = $model->get($r_person); | ||
| 407 | |||
| 408 | my $r_fn = $q->param('r_fn') or die "No first name specified"; | ||
| 409 | my $r_ln = $q->param('r_ln') or die "No last name specified"; | ||
| 410 | |||
| 411 | $person->arc_obj(NS_LD.'/Property#first_name')->li->set_literal(\$r_fn); | ||
| 412 | $person->arc_obj(NS_LD.'/Property#last_name')->li->set_literal(\$r_ln); | ||
| 413 | |||
| 414 | return "Person edited"; | ||
| 415 | } | ||
| 416 | |||
| 417 | sub do_person_login | ||
| 418 | { | ||
| 419 | my $r_person = $q->param('r_person') or die "No node specified"; | ||
| 420 | my $model = $s; | ||
| 421 | my $person = $model->get($r_person); | ||
| 422 | |||
| 423 | $s->set_props({NS_LS.'#agent' => [$person]}); | ||
| 424 | |||
| 425 | return "Logging in"; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub dlines | ||
| 429 | { | ||
| 430 | open FILE, "/tmp/RDF-Service-0_04.log" or die $!; | ||
| 431 | |||
| 432 | use Fcntl; | ||
| 433 | our $llines = 0; | ||
| 434 | our $loffset = 0; | ||
| 435 | |||
| 436 | unless( seek FILE, $loffset, SEEK_SET ) | ||
| 437 | { | ||
| 438 | $llines = 0; | ||
| 439 | $loffset = 0; | ||
| 440 | } | ||
| 441 | while( <FILE> ) | ||
| 442 | { | ||
| 443 | $llines++; | ||
| 444 | $loffset += length; | ||
| 445 | } | ||
| 446 | close FILE; | ||
| 447 | return $llines; | ||
| 448 | } |