blib/lib/TAPORlib.pm untested

filter: Statementstmt 0.0% 0 / 783 Branchbran 0.0% 0 / 364 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 44 totaltotal 0.0% 0 / 1320 CC 183 SCAR 65.0
CC
183
Cov
0%
GetPageNow_4
1980.0
CorrectLinksOnPage
1056.0
IsSearchEngine
930.0
CRAP 665.7
1 package TAPORlib;
2  
3 =head1 NAME
4  
5 C<TAPORlib> - TAPORlib is a Perl module that contains some useful functions.
6  
7 =head1 DESCRIPTION
8  
9 This library used by some great modules as WWW::Promotion, etc.
10 It is provided by TAPOR, Inc.
11  
12 =cut
13  
14 ##############################################################################
15 use strict;
16 use vars qw($VERSION
17 @ISA
18 @EXPORT
19 @EXPORT_OK
20 %countrycodes
21 %countrycodesbyname
22 %operation_systems
23 %monthbynumber
24 %monthbyname
25 %color
26 @rndletters
27 $eol
28 );
29  
30 require Exporter;
31  
32 use IO::Socket;
33 use IO::Select;
34 use Socket;
35 use Carp;
36 use POSIX;
37  
38 @ISA = qw(Exporter);
39 @EXPORT = qw(&Delete_CRLF_from_End_Of_String
40 &add_string_to_file
41 &add_string_to_file_spec_output
42 &GetAllFilesInDir
43 &GetAllFilesContaningInDir
44 &GetPageNow_4
45 &uri_escape
46 &HTMLdie
47 &isrunninglocaly
48 &newsocketto
49 &GenerateRandomString
50 &SelectRandomStringFromFile
51 &CreateAndSendOutHtmlPage
52 &CheckForDomain
53 &SendToDomainIfNotThisDomain
54 &MassiveToString
55 &CheckProxy
56 &win2koi
57 &koi2win
58 &GetScriptName
59 &SetScriptDirAsCurrent
60 &IsDateValid
61 &parse_form_2
62 &GetDate_2
63 &GetDateRus_2
64 &SendEmailMsg
65 &SendEmailMultiMsg
66 &getcookies
67 &joincookies
68 &joincookiesinhash
69 &create_error_string
70 &LimitMassive
71 &GetTops
72 &IsSearchEngine
73 &DetectOperationSystem
74 &IsAllreadyRunning
75 &IsAllreadyRunning_2
76 &change_spec_labels
77 &ViewSpecHtmFile
78 &SelectRandomProxyServerFromFile
79 &CorrectLinksOnPage
80 &send_null_image
81 %countrycodes
82 %countrycodesbyname
83 %operation_systems
84 %monthbynumber
85 %monthbyname
86 %color
87 @rndletters
88 $eol
89 );
90  
91 # create_error_string - /No desc
92 # change_spec_labels - /No desc
93 # ViewSpecHtmFile - /No desc
94 # CorrectLinksOnPage - /No desc
95  
96 @EXPORT_OK = qw();
97  
98 $VERSION = "8.70";
99  
100 ##############################################################################
101  
102 =head1 IMPORTED FUNCTIONS/VARS
103  
104 =head1
105  
106 =head2 $eol
107
108 Description:
109  
110 $eol = "\x0D\x0A";
111  
112 =cut
113  
114 $eol = "\x0D\x0A"; # "\r\n";
115  
116 ##############################################################################
117  
118 =head2 @rndletters
119
120 Description:
121  
122 @rndletters = qw(q w e r t y u i o p a s d f g h j k l z x c v b n m);
123  
124 =cut
125  
126 @rndletters = qw(q w e r t y u i o p a s d f g h j k l z x c v b n m);
127  
128 ##############################################################################
129  
130 =head2 %countrycodes / %countrycodesbyname
131
132 Description:
133  
134 %countrycodes = (
135 'ca' => 'Canada',
136 'af' => 'Afghanistan',
137 'al' => 'Albania',
138 'dz' => 'Algeria',
139 ...
140 );
141  
142 %countrycodesbyname = reverse %countrycodes;
143  
144 =cut
145  
146 %countrycodes = (
147 'ca' => 'Canada',
148 'af' => 'Afghanistan',
149 'al' => 'Albania',
150 'dz' => 'Algeria',
151 'as' => 'American Samoa',
152 'ad' => 'Andorra',
153 'ao' => 'Angola',
154 'ai' => 'Anguilla',
155 'aq' => 'Antarctica',
156 'ag' => 'Antigua and Barbuda',
157 'ar' => 'Argentina',
158 'am' => 'Armenia',
159 'aw' => 'Aruba',
160 'au' => 'Australia',
161 'at' => 'Austria',
162 'az' => 'Azerbaijan',
163 'bs' => 'Bahamas',
164 'bh' => 'Bahrain',
165 'bd' => 'Bangladesh',
166 'bb' => 'Barbados',
167 'by' => 'Belarus',
168 'be' => 'Belgium',
169 'bz' => 'Belize',
170 'bj' => 'Benin',
171 'bm' => 'Bermuda',
172 'bt' => 'Bhutan',
173 'bo' => 'Bolivia',
174 'ba' => 'Bosnia and Herzegovina',
175 'bw' => 'Botswana',
176 'bv' => 'Bouvet Island',
177 'br' => 'Brazil',
178 'io' => 'British Indian Ocean Territory',
179 'vg' => 'British Virgin Islands',
180 'bn' => 'Brunei',
181 'bg' => 'Bulgaria',
182 'bf' => 'Burkina Faso',
183 'bi' => 'Burundi',
184 'kh' => 'Cambodia',
185 'cm' => 'Cameroon',
186 'cv' => 'Cape Verde',
187 'ky' => 'Cayman Islands',
188 'cf' => 'Central African Republic',
189 'td' => 'Chad',
190 'cl' => 'Chile',
191 'cn' => 'China',
192 'cx' => 'Christmas Island',
193 'cc' => 'Cocos Islands',
194 'co' => 'Colombia',
195 'km' => 'Comoros',
196 'cg' => 'Congo',
197 'ck' => 'Cook Islands',
198 'cr' => 'Costa Rica',
199 'hr' => 'Croatia',
200 'cu' => 'Cuba',
201 'cy' => 'Cyprus',
202 'cz' => 'Czech Republic',
203 'dk' => 'Denmark',
204 'dj' => 'Djibouti',
205 'dm' => 'Dominica',
206 'do' => 'Dominican Republic',
207 'tp' => 'East Timor',
208 'ec' => 'Ecuador',
209 'eg' => 'Egypt',
210 'sv' => 'El Salvador',
211 'gq' => 'Equatorial Guinea',
212 'er' => 'Eritrea',
213 'ee' => 'Estonia',
214 'et' => 'Ethiopia',
215 'fk' => 'Falkland Islands',
216 'fo' => 'Faroe Islands',
217 'fj' => 'Fiji',
218 'fi' => 'Finland',
219 'fr' => 'France',
220 'gf' => 'French Guiana',
221 'pf' => 'French Polynesia',
222 'tf' => 'French Southern Territories',
223 'ga' => 'Gabon',
224 'gm' => 'Gambia',
225 'ge' => 'Georgia',
226 'de' => 'Germany',
227 'gh' => 'Ghana',
228 'gi' => 'Gibraltar',
229 'gr' => 'Greece',
230 'gl' => 'Greenland',
231 'gd' => 'Grenada',
232 'gp' => 'Guadeloupe',
233 'gu' => 'Guam',
234 'gt' => 'Guatemala',
235 'gn' => 'Guinea',
236 'gw' => 'Guinea-Bissau',
237 'gy' => 'Guyana',
238 'ht' => 'Haiti',
239 'hm' => 'Heard and McDonald Islands',
240 'hn' => 'Honduras',
241 'hk' => 'Hong Kong',
242 'hu' => 'Hungary',
243 'is' => 'Iceland',
244 'in' => 'India',
245 'id' => 'Indonesia',
246 'ir' => 'Iran',
247 'iq' => 'Iraq',
248 'ie' => 'Ireland',
249 'il' => 'Israel',
250 'it' => 'Italy',
251 'ci' => 'Ivory Coast',
252 'jm' => 'Jamaica',
253 'jp' => 'Japan',
254 'jo' => 'Jordan',
255 'kz' => 'Kazakhstan',
256 'ke' => 'Kenya',
257 'ki' => 'Kiribati',
258 'kp' => 'Korea, North',
259 'kr' => 'Korea, South',
260 'kw' => 'Kuwait',
261 'kg' => 'Kyrgyzstan',
262 'la' => 'Laos',
263 'lv' => 'Latvia',
264 'lb' => 'Lebanon',
265 'ls' => 'Lesotho',
266 'lr' => 'Liberia',
267 'ly' => 'Libya',
268 'li' => 'Liechtenstein',
269 'lt' => 'Lithuania',
270 'lu' => 'Luxembourg',
271 'mo' => 'Macau',
272 'mk' => 'Macedonia, Former Yugoslav Republic of',
273 'mg' => 'Madagascar',
274 'mw' => 'Malawi',
275 'my' => 'Malaysia',
276 'mv' => 'Maldives',
277 'ml' => 'Mali',
278 'mt' => 'Malta',
279 'mh' => 'Marshall Islands',
280 'mq' => 'Martinique',
281 'mr' => 'Mauritania',
282 'mu' => 'Mauritius',
283 'yt' => 'Mayotte',
284 'mx' => 'Mexico',
285 'fm' => 'Micronesia, Federated States of',
286 'md' => 'Moldova',
287 'mc' => 'Monaco',
288 'mn' => 'Mongolia',
289 'ms' => 'Montserrat',
290 'ma' => 'Morocco',
291 'mz' => 'Mozambique',
292 'mm' => 'Myanmar',
293 'na' => 'Namibia',
294 'nr' => 'Nauru',
295 'np' => 'Nepal',
296 'nl' => 'Netherlands',
297 'an' => 'Netherlands Antilles',
298 'nc' => 'New Caledonia',
299 'nz' => 'New Zealand',
300 'ni' => 'Nicaragua',
301 'ne' => 'Niger',
302 'ng' => 'Nigeria',
303 'nu' => 'Niue',
304 'nf' => 'Norfolk Island',
305 'mp' => 'Northern Mariana Islands',
306 'no' => 'Norway',
307 'om' => 'Oman',
308 'pk' => 'Pakistan',
309 'pw' => 'Palau',
310 'pa' => 'Panama',
311 'pg' => 'Papua New Guinea',
312 'py' => 'Paraguay',
313 'pe' => 'Peru',
314 'ph' => 'Philippines',
315 'pn' => 'Pitcairn Island',
316 'pl' => 'Poland',
317 'pt' => 'Portugal',
318 'pr' => 'Puerto Rico',
319 'qa' => 'Qatar',
320 're' => 'Reunion',
321 'ro' => 'Romania',
322 'ru' => 'Russia',
323 'rw' => 'Rwanda',
324 'gs' => 'S. Georgia and S. Sandwich Isls.',
325 'kn' => 'Saint Kitts & Nevis',
326 'lc' => 'Saint Lucia',
327 'vc' => 'Saint Vincent and The Grenadines',
328 'ws' => 'Samoa',
329 'sm' => 'San Marino',
330 'st' => 'Sao Tome and Principe',
331 'sa' => 'Saudi Arabia',
332 'sn' => 'Senegal',
333 'sc' => 'Seychelles',
334 'sl' => 'Sierra Leone',
335 'sg' => 'Singapore',
336 'sk' => 'Slovakia',
337 'si' => 'Slovenia',
338 'so' => 'Somalia',
339 'za' => 'South Africa',
340 'es' => 'Spain',
341 'lk' => 'Sri Lanka',
342 'sh' => 'St. Helena',
343 'pm' => 'St. Pierre and Miquelon',
344 'sd' => 'Sudan',
345 'sr' => 'Suriname',
346 'sj' => 'Svalbard and Jan Mayen Islands',
347 'sz' => 'Swaziland',
348 'se' => 'Sweden',
349 'ch' => 'Switzerland',
350 'sy' => 'Syria',
351 'tw' => 'Taiwan',
352 'tj' => 'Tajikistan',
353 'tz' => 'Tanzania',
354 'th' => 'Thailand',
355 'tg' => 'Togo',
356 'tk' => 'Tokelau',
357 'to' => 'Tonga',
358 'tt' => 'Trinidad and Tobago',
359 'tn' => 'Tunisia',
360 'tr' => 'Turkey',
361 'tm' => 'Turkmenistan',
362 'tc' => 'Turks and Caicos Islands',
363 'tv' => 'Tuvalu',
364 'um' => 'U.S. Minor Outlying Islands',
365 'ug' => 'Uganda',
366 'ua' => 'Ukraine',
367 'ae' => 'United Arab Emirates',
368 'uk' => 'United Kingdom',
369 'us' => 'United States of America',
370 'uy' => 'Uruguay',
371 'uz' => 'Uzbekistan',
372 'vu' => 'Vanuatu',
373 'va' => 'Vatican City',
374 've' => 'Venezuela',
375 'vn' => 'Vietnam',
376 'vi' => 'Virgin Islands',
377 'wf' => 'Wallis and Futuna Islands',
378 'eh' => 'Western Sahara',
379 'ye' => 'Yemen',
380 'yu' => 'Yugoslavia (Former)',
381 'zr' => 'Zaire',
382 'zm' => 'Zambia',
383 'zw' => 'Zimbabwe',
384 );
385  
386 %countrycodesbyname = reverse %countrycodes;
387  
388 ##############################################################################
389  
390 =head2 %operation_systems
391
392 Description:
393  
394 %operation_systems = (
395 'Windows 98' => 'Windows 98',
396 'Win98' => 'Windows 98',
397 'Windows 95' => 'Windows 95',
398 'Win95' => 'Windows 95',
399 'Mozilla/3.01 (compatible;)' => 'Windows 95',
400  
401 =cut
402  
403 %operation_systems = (
404 'Windows 98' => 'Windows 98',
405 'Win98' => 'Windows 98',
406 'Windows 95' => 'Windows 95',
407 'Win95' => 'Windows 95',
408 'Mozilla/3.01 (compatible;)' => 'Windows 95',
409 'Windows NT 5.0' => 'Windows NT 5.0',
410 'Windows NT 4.0' => 'Windows NT 4.0',
411 'Windows NT' => 'Windows NT 4.0',
412 'WinNT' => 'Windows NT 4.0',
413 'Mac_PowerPC' => 'Macintosh',
414 'Mac_PPC' => 'Macintosh',
415 'Macintosh' => 'Macintosh',
416 'Mac_68000' => 'Macintosh',
417 'Linux' => 'Linux',
418 'FreeBSD' => 'FreeBSD',
419 'Windows 2000' => 'Windows 2000',
420 'Win2000' => 'Windows 2000',
421 'Windows ME' => 'Windows ME',
422 'WinME' => 'Windows ME',
423 'Konqueror' => 'Linux',
424 'Windows 3.1' => 'Windows 3.1',
425 'Win3.1' => 'Windows 3.1',
426 'SunOS' => 'SunOS',
427 'Irix' => 'Unix',
428 'Unix' => 'Unix',
429 );
430  
431 ##############################################################################
432  
433 =head2 %monthbynumber / %monthbyname
434
435 Description:
436  
437 %monthbynumber = (
438 '1' => "January",
439 '2' => "February",
440 '3' => "March",
441 '4' => "April",
442 ...);
443
444 %monthbyname = reverse %monthbynumber;
445  
446 =cut
447  
448 %monthbynumber = (
449 '1' => "January",
450 '2' => "February",
451 '3' => "March",
452 '4' => "April",
453 '5' => "May",
454 '6' => "June",
455 '7' => "July",
456 '8' => "August",
457 '9' => "September",
458 '10' => "October",
459 '11' => "November",
460 '12' => "December",
461 );
462 %monthbyname = reverse %monthbynumber;
463  
464 ##############################################################################
465  
466 =head2 %color
467
468 Description:
469  
470 %color =(
471 'normal' => "",
472 'black' => "",
473 'red' => "" ,
474 'ligthred' => "",
475 ...
476 );
477
478 =cut
479  
480 %color =(
481 'normal' => "",
482 'black' => "",
483 'red' => "" ,
484 'ligthred' => "",
485 'green' => "",
486 'ligthgreen' => "",
487 'blue' => "",
488 'ligthblue' => "",
489 'white' => "",
490 'yelow' => "" ,
491 'yellow' => "" ,
492 '0' => "",
493 '1' => "",
494 '2' => "",
495 '3' => "",
496 '4' => "",
497 '5' => "",
498 '6' => "",
499 '7' => "",
500 '8' => "",
501 '9' => "",
502 'sim' => "",
503 );
504  
505 ##############################################################################
506  
507 =head2 $string = &Delete_CRLF_from_End_Of_String($string);
508
509 Description:
510  
511 Function removes trailing "r" and "\n" from end of $string.
512  
513 =cut
514  
515 sub Delete_CRLF_from_End_Of_String {
516 local($_) = shift;
517
518 while(1)
519 {
520 if(/\r$/) { $_ = substr($_,0,-1); }
521 elsif(/\n$/) { $_ = substr($_,0,-1); }
522 else { return $_ };
523 }
524 }
525 ###############################################################################
526  
527 =head2 &add_string_to_file("filename",$string);
528  
529 Description:
530  
531 This function adds $string to the end of file "filename".
532  
533 =cut
534  
535 sub add_string_to_file {
536 my ($file,$data) = @_;
537
538 if(open(FILE,">>$file"))
539 {
540 print FILE $data;
541 close(FILE);
542 }
543 }
544  
545 =head2 &add_string_to_file_spec_output("filename",$who,$string,$end);
546  
547 Description:
548  
549 This function makes special output to "filename".
550 String shows like this:
551
552 --- $who ---------------- $string --------------------$end
553  
554 =cut
555  
556 sub add_string_to_file_spec_output {
557 my ($file,$who,$data,$end) = @_;
558  
559 # Create string
560
561 my $maxtotal = 70;
562 my $body = "-";
563 my $prefix = $body . $body . $body;
564
565 my $len = (length($prefix) + 1 + length($who) + 3 + length($data) + 2);
566  
567 if($len >= ($maxtotal-1))
568 {
569 add_string_to_file($file,"$prefix $who $body $data $body" . $end);
570 }
571 else
572 {
573 my $len1 = int(($maxtotal - $len)/2);
574 my $len2 = $maxtotal - $len - $len1;
575  
576 my $string1;
577 my $string2;
578  
579 while(length($string1)<$len1) { $string1 = $string1 . $body;};
580 while(length($string2)<$len2) { $string2 = $string2 . $body;};
581 add_string_to_file($file,"$prefix $who $string1 $data $string2" . $end);
582 }
583 }
584 ###############################################################################
585  
586 =head2 @allfiles = &GetAllFilesInDir($dirname);
587  
588 Description:
589  
590 This function returns massive that contains filenames with
591 path in directory $dirname and filenames with path in subdirs also.
592  
593 =cut
594  
595 sub GetAllFilesInDir {
596 my($usedir) = @_;
597 my(@allfiles,@bodydir,$fileindir);
598  
599 @allfiles = ();
600 if(opendir(DIR,$usedir))
601 {
602 @bodydir = readdir(DIR);
603 close(DIR);
604 foreach $fileindir (@bodydir)
605 {
606 if($fileindir eq '.' || $fileindir eq '..') {next;}
607 if(-d $usedir . "/" . $fileindir)
608 {
609 push(@allfiles,&GetAllFilesInDir("$usedir/$fileindir"));
610 }
611 else
612 {
613 push(@allfiles,"$usedir/$fileindir");
614 }
615 }
616 }
617 return @allfiles;
618 }
619 ##############################################################################
620  
621 =head2 @allfilescontents = &GetAllFilesContaningInDir($dirname);
622  
623 Description:
624  
625 This function returns massive that contains content of all
626 files in directory $dirname (subdirs also).
627  
628 =cut
629  
630 sub GetAllFilesContaningInDir {
631 my($dir) = @_;
632  
633 my(@keys,@returnkeys);
634
635 foreach (GetAllFilesInDir($dir))
636 {
637 if(!($_)) {next;}
638 if(open(FILE,$_))
639 {
640 @keys = <FILE>;
641 close(FILE);
642 foreach (@keys)
643 {
644 $_ = VoidCRLF($_);
645 if(!($_)) {next;}
646 push(@returnkeys,$_);
647 }
648 }
649 }
650 return @returnkeys;
651 }
652 ###############################################################################
653  
654 =head2 %out = &GetPageNow_4(%pagenow);
655  
656 Description:
657
658 Use this function to get page from website.
659  
660 Usage:
661  
662 $pagenow{'url'} = "http://www.any.com/anyware";
663 $pagenow{'method'} = "POST|GET";
664 $pagenow{'referer'} = "http://www.any.com/anyware/ref";
665 $pagenow{'content'} = "user=blah\&info=blah-blah";
666  
667 # If defined this agent string will be used insted Netscape
668 $pagenow{'agent'} = "My AGENT";
669  
670 # If specified Print some useful information to this logfile;
671 $pagenow{'logfile'} = "logfile.log";
672  
673 # If proxy not specified then Get Page without usage of proxy.
674 $pagenow{'proxy'} = "proxy.online.ru:8080";
675  
676 # If specified then send to page this cookies:
677 $pagenow{'cookies'} = "C=12345; F=1";
678  
679 # TimeOut to Connect To Proxy/Host. Default: 60
680 $pagenow{'timeoutconnect'} = 60;
681  
682 # TimeOut to Request Page. Default: 300
683 $pagenow{'timeoutrequest'} = 180;
684  
685 # No Request. GetPageNow_4() returns simple 'FAST MODE' page.
686 $pagenow{'norequest'} = 1;
687  
688 # Show Error Page If Error Detected
689 $pagenow{'showerrors'} = 1;
690  
691 %out = &GetPageNow_4(%pagenow);
692  
693 Output:
694
695 $out{'error'} == 0 - No errors
696 $out{'error'} == 1 - Some Error.
697 $out{'errortxt'} - Error description if $out{'error'} == 1
698 $out{'status'} - Status of downloaded page
699 $out{'headers'} - Header of downloaded page
700 $out{'body'} - Body of downloaded page
701 $out{'cookies'} - Cookies If page returns some cookies
702  
703 =cut
704  
705 sub GetPageNow_4 {
706 my(%pagenow) = @_;
707 my(%out);
708  
709 my($logfile) = $pagenow{'logfile'}; # // ?
710
711 $pagenow{'timeoutconnect'} = 60 if(!$pagenow{'timeoutconnect'});
712 $pagenow{'timeoutrequest'} = 300 if(!$pagenow{'timeoutrequest'});
713  
714 add_string_to_file($logfile,"\n");
715 add_string_to_file_spec_output($logfile,"TAPORlib::GetPageNow_4()","Input Data","\n");
716  
717 foreach (sort keys %pagenow)
718 {
719 $pagenow{$_} = Delete_CRLF_from_End_Of_String($pagenow{$_});
720 add_string_to_file($logfile,"\$pagenow{'$_'} = '$pagenow{$_}'\n");
721 }
722  
723 # Checking input data
724  
725 if($pagenow{'method'} ne 'POST' && $pagenow{'method'} ne 'GET')
726 {
727 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Invalid method: '$pagenow{'method'}'");
728 goto print_error_to_log_and_exit;
729 }
730 if($pagenow{'method'} eq 'POST' && !$pagenow{'content'})
731 {
732 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","POST without content not allowed");
733 goto print_error_to_log_and_exit;
734 }
735 if($pagenow{'url'} !~ m|^(http://)([^/\?\\]+)|i)
736 {
737 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Invalid url format: '$pagenow{'url'}'");
738 goto print_error_to_log_and_exit;
739 }
740 my($hostaddr,$hostport) = split(/\:/,$2);
741 if($hostport && $hostport !~ m/^[0-9]+$/)
742 {
743 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Invalid host/port format: '$pagenow{'url'}'");
744 goto print_error_to_log_and_exit;
745 }
746 $hostport = $hostport ? $hostport : 80;
747 add_string_to_file($logfile,"Host: '$hostaddr' Port: '$hostport'\n");
748
749 $pagenow{'savedurl'} = $pagenow{'url'};
750
751 $out{'error'}=1;
752  
753 #----------------- Change Reserved syms to %xx -----------------------
754 if($pagenow{'content'})
755 {
756 my($reserved) = ";\\/?:\\@#";
757 my($unsafe) = "\x00-\x20{}|\\\\^\\[\\]`<>\"\x7F-\xFF";
758 $pagenow{'content'} =~ s/ /+/g;
759 $pagenow{'content'} = &uri_escape($pagenow{'content'},$reserved . $unsafe);
760 $pagenow{'contentlen'} = length($pagenow{'content'});
761 }
762 #---------------------------------------------------------------------
763  
764 my($hostconnectaddr,$hostconnectport);
765
766 if($pagenow{'proxy'})
767 {
768 ($hostconnectaddr,$hostconnectport) = split(/\:/,$pagenow{'proxy'});
769 if($hostconnectport && $hostconnectport !~ m/^[0-9]+$/)
770 {
771 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Invalid host/port format for proxy: '$pagenow{'proxy'}'");
772 goto print_error_to_log_and_exit;
773 }
774 $hostconnectport = $hostconnectport ? $hostconnectport : 80;
775 add_string_to_file($logfile,"ProxyHost: '$hostconnectaddr' ProxyPort: '$hostconnectport'\n");
776 }
777 else
778 {
779 ($hostconnectaddr,$hostconnectport) = ($hostaddr,$hostport);
780 $pagenow{'url'} =~ m|^(http://)([^/\?\\]*)([^\s\r\n]*)$|i;
781 $pagenow{'url'} = $3;
782 if(!$pagenow{'url'}) { $pagenow{'url'} = "/";}
783 add_string_to_file($logfile,"New \$pagenow{'url'} = '$pagenow{'url'}' (because no proxy)\n");
784 }
785  
786 # Connecting
787  
788 my $sock = IO::Socket->new(Timeout => $pagenow{'timeoutconnect'});
789
790 $sock->socket(AF_INET, SOCK_STREAM, 0);
791 if(!$sock)
792 {
793 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Error in socket()");
794 goto print_error_to_log_and_exit;
795 }
796
797 my $iaddr = inet_aton($hostconnectaddr);
798 if(!$iaddr)
799 {
800 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Error resolving host '$hostconnectaddr'");
801 goto print_error_to_log_and_exit;
802 }
803
804 my $paddr = sockaddr_in($hostconnectport,$iaddr);
805
806 $sock->connect($paddr);
807
808 if($@)
809 {
810 if($@ =~ /timeout/i)
811 {
812 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Error connecting to $hostconnectaddr:$hostconnectport after $pagenow{'timeoutconnect'} seconds");
813 goto print_error_to_log_and_exit;
814 }
815 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()",$@);
816 goto print_error_to_log_and_exit;
817 }
818  
819 add_string_to_file($logfile,"Connected to: '$hostconnectaddr:$hostconnectport'\n");
820
821 # POST ------------------------------------------------------------------------
822 my(@data);
823  
824 if($pagenow{'method'} eq 'POST') {
825  
826 @data = ("POST $pagenow{'url'} HTTP/1.0$eol");
827  
828 if($pagenow{'referer'}) {push(@data,("Referer: $pagenow{'referer'}$eol"));}
829  
830 if($pagenow{'proxy'})
831 {
832 push(@data,("Proxy-Connection: Keep-Alive$eol"));
833 }
834 else
835 {
836 # push(@data,("Connection: Keep-Alive$eol"));
837 push(@data,("Connection: Close$eol"));
838 }
839  
840 if($pagenow{'agent'})
841 {
842 push(@data,("User-Agent: $pagenow{'agent'}$eol"));
843 }
844 else
845 {
846 push(@data,("User-Agent: Mozilla/4.7 [en] (Win98; I)$eol"));
847 }
848  
849 if($hostport==80)
850 {
851 push(@data,("Host: $hostaddr$eol"));
852 }
853 else
854 {
855 push(@data,("Host: $hostaddr:$hostport$eol"));
856 }
857  
858 push(@data,("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*$eol",
859 # "Accept-Encoding: gzip$eol",
860 "Accept-Language: en$eol",
861 "Accept-Charset: iso-8859-1,*,utf-8$eol",
862 ));
863  
864 if($pagenow{'cookies'})
865 {
866 push(@data,("Cookie: $pagenow{'cookies'}$eol"));
867 }
868  
869 push(@data,("Content-type: application/x-www-form-urlencoded$eol",
870 "Content-length: $pagenow{'contentlen'}$eol$eol",
871 ));
872
873 push(@data,("$pagenow{'content'}"));
874  
875 }
876  
877 # GET ------------------------------------------------------------------------
878 if($pagenow{'method'} eq 'GET') {
879  
880 if(!$pagenow{'content'}) {@data = ("GET $pagenow{'url'} HTTP/1.0$eol");}
881 else {@data = ("GET $pagenow{'url'}?$pagenow{'content'} HTTP/1.0$eol");}
882  
883 if($pagenow{'referer'}) {push(@data,("Referer: $pagenow{'referer'}$eol"));}
884  
885 if($pagenow{'proxy'})
886 {
887 push(@data,("Proxy-Connection: Keep-Alive$eol"));
888 }
889 else
890 {
891 # push(@data,("Connection: Keep-Alive$eol"));
892 push(@data,("Connection: Close$eol"));
893 }
894  
895 if($pagenow{'agent'})
896 {
897 push(@data,("User-Agent: $pagenow{'agent'}$eol"));
898 }
899 else
900 {
901 push(@data,("User-Agent: Mozilla/4.7 [en] (Win98; I)$eol"));
902 }
903  
904 if($hostport==80)
905 {
906 push(@data,("Host: $hostaddr$eol"));
907 }
908 else
909 {
910 push(@data,("Host: $hostaddr:$hostport$eol"));
911 }
912  
913 push(@data,("Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*$eol",
914 # "Accept-Encoding: gzip$eol",
915 "Accept-Language: en$eol",
916 "Accept-Charset: iso-8859-1,*,utf-8$eol",
917 "Pragma: No-Cache$eol"));
918  
919 if($pagenow{'cookies'})
920 {
921 push(@data,("Cookie: $pagenow{'cookies'}$eol$eol"));
922 }
923 else
924 {
925 push(@data,("$eol"));
926 }
927  
928 }
929  
930  
931 # REQUEST TO LOG ---
932  
933 add_string_to_file($logfile,"\n");
934 add_string_to_file_spec_output($logfile,"TAPORlib::GetPageNow_4()","Request (AS-IS)","\n");
935 foreach (@data) {add_string_to_file($logfile,$_);}
936 add_string_to_file($logfile,"\n");
937 add_string_to_file_spec_output($logfile,"TAPORlib::GetPageNow_4()","Request End","\n");
938  
939 # SEND REQUEST ---
940  
941 my($timeout) = $pagenow{'timeoutrequest'};
942  
943 my $select = IO::Select->new();
944 $select->add($sock);
945  
946 if(!$select->can_write($timeout))
947 {
948 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Timeout sending request after $timeout seconds");
949 goto print_error_to_log_and_exit;
950 }
951  
952 print $sock @data;
953  
954 my(@body);
955
956 # GET PAGE ---------------------------------------------------------------
957 if($pagenow{'norequest'})
958 {
959 $sock->close();
960  
961 add_string_to_file($logfile,"\n");
962 add_string_to_file_spec_output($logfile,"TAPORlib::GetPageNow_4()",">>> FASTMODE[tm] >>>","\n");
963  
964 @body = ();
965 push @body, <<FASTBODY;
966
967 <HTML>
968 <BODY>
969 <CENTER>FAST</CENTER>
970 </BODY>
971 </HTML>
972
973 FASTBODY
974  
975 $out{'error'} = 0;
976 $out{'status'} = "HTTP/1.1 200 OK";
977 delete($out{'headers'});
978 $out{'body'} = &MassiveToString(@body);
979 return %out;
980 }
981  
982 # Status ----------------------------------------------------------------------
983 $out{'status'} = undef;
984  
985 if(!$select->can_read($timeout))
986 {
987 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Timeout recv status after $timeout seconds");
988 goto print_error_to_log_and_exit;
989 }
990 $out{'status'} = <$sock>;
991  
992 # Headers ---
993  
994 $out{'headers'} = undef;
995  
996 do
997 {
998 if(!$select->can_read($timeout))
999 {
1000 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","TimeOut recv headers after $timeout seconds");
1001 goto print_error_to_log_and_exit;
1002 }
1003 $out{'headers'} .= $_ = <$sock> ; # $headers includes last blank line
1004 } until (/^(\015\012|\012)$/) ; # lines may be terminated with LF or CRLF
1005  
1006 # Unfold long header lines, a la RFC 822 section 3.1.1
1007  
1008 $out{'headers'} =~ s/(\015\012|\012)[ \t]/ /g ;
1009  
1010 if(!$select->can_read($timeout))
1011 {
1012 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","TimeOut recv body after $timeout seconds");
1013 goto print_error_to_log_and_exit;
1014 }
1015  
1016 @body=<$sock>;
1017  
1018 $out{'body'} = "@body";
1019  
1020 $sock->close();
1021  
1022 my($cookies)=getcookies($out{'headers'});
1023  
1024 if($cookies && !$pagenow{'cookies'})
1025 {
1026 $pagenow{'cookies'} = $cookies;
1027 }
1028 elsif($cookies && $pagenow{'cookies'})
1029 {
1030 $pagenow{'cookies'} = joincookies($pagenow{'cookies'},$cookies);
1031 }
1032
1033 add_string_to_file($logfile,"STATUS:\n$out{'status'}\n");
1034 add_string_to_file($logfile,"HEADERS:\n$out{'headers'}\n");
1035  
1036 if($pagenow{'cookies'})
1037 {
1038 add_string_to_file($logfile,"Cookies: '$pagenow{'cookies'}'\n\n");
1039 }
1040 else
1041 {
1042 add_string_to_file($logfile,"Cookies: <NONE>\n\n");
1043 }
1044  
1045 #add_string_to_file($logfile,"@body\n\n");
1046 add_string_to_file($logfile,">--- BODY ---<\n\n");
1047  
1048 if ($out{'status'} =~ m#^HTTP/[0-9.]*\s*[45]\d\d#)
1049 {
1050 if($pagenow{'showerrors'})
1051 {
1052 &HTMLdie("@body");
1053 }
1054 else
1055 {
1056 $out{'errortxt'} = create_error_string("TAPORlib::GetPageNow_4()","Server answer: $out{'status'}");
1057 goto print_error_to_log_and_exit;
1058 }
1059 }
1060  
1061 # If REDIRECT
1062  
1063 if($out{'headers'} =~ m|^(Location:\s+)(.+?)[\s\r\n]*$|im)
1064 {
1065 add_string_to_file_spec_output($logfile,"TAPORlib::GetPageNow_4()",">>>> REDIRECT >>>>","\n\n");
1066  
1067 my($location) = $2;
1068
1069 if($location !~ m|^(http://)|i)
1070 {
1071 $location = $pagenow{'savedurl'} . "/" . $location;
1072 }
1073 $pagenow{'url'} = $location;
1074
1075 # if (http://)(some.host/test/)?(content=content)
1076 # $1 $2 $3
1077  
1078 if($location=~ m|^(http://)([^\?]+)\?(.*?)[\s\r\n]*$|i)
1079 {
1080 $pagenow{'url'} = $1 . $2;
1081 $pagenow{'content'} = $3;
1082 add_string_to_file($logfile,"NEW URL: '$pagenow{'url'}'\n");
1083 add_string_to_file($logfile,"NEW CONTENT: '$pagenow{'content'}'\n");
1084 }
1085 else
1086 {
1087 delete($pagenow{'content'});
1088 add_string_to_file($logfile,"NEW URL: '$pagenow{'url'}'\n");
1089 }
1090  
1091 delete($pagenow{'contentlen'});
1092 delete($pagenow{'savedurl'});
1093  
1094 $pagenow{'method'} = "GET";
1095 return GetPageNow_4(%pagenow);
1096 }
1097  
1098 if($pagenow{'cookies'}) {$out{'cookies'} = $pagenow{'cookies'};}
1099 $out{'error'} = 0;
1100 return %out;
1101  
1102 print_error_to_log_and_exit:
1103 add_string_to_file($logfile,"\n$out{'errortxt'}\n");
1104 return %out;
1105 }
1106 ###############################################################################
1107  
1108 =head2 $text = &uri_escape($text,$pattern);
1109  
1110 Description:
1111  
1112 This function escapes url, commonly used to changes
1113 special symbols in url.
1114  
1115 Usage:
1116  
1117 $text = "sss"
1118 $pattern = "\x00-\xFF";
1119 $text = &uri_escape($text,$pattern);
1120
1121 Output:
1122
1123 $text = "%73%73%73";
1124  
1125 =cut
1126  
1127 sub uri_escape {
1128 my($text,$patn) = @_;
1129 return undef unless defined $text;
1130  
1131 my %escapes;
1132 # Build a char->hex map
1133 for (0..255)
1134 {
1135 $escapes{chr($_)} = sprintf("%%%02X", $_);
1136 }
1137  
1138 my %subst;
1139 if(defined $patn)
1140 {
1141 unless (exists $subst{$patn})
1142 {
1143 # Because we can't compile regex we fake it with a cached sub
1144 $subst{$patn} =
1145 eval "sub {\$_[0] =~ s/([$patn])/\$escapes{\$1}/g; }";
1146 Carp::croak("uri_escape: $@") if $@;
1147 }
1148 &{$subst{$patn}}($text);
1149 }
1150 else
1151 {
1152 # Default unsafe characters. (RFC1738 section 2.2)
1153 $text =~ s/([\x00-\x20"#%;<>?{}|\\\\^~`\[\]\x7F-\xFF])/$escapes{$1}/g; #"
1154 }
1155 return $text;
1156 }
1157 ###############################################################################
1158  
1159 =head2 &HTMLdie($text);
1160  
1161 Description:
1162  
1163 Send HTML page with $text to STDOUT and exit program.
1164  
1165 =cut
1166  
1167 sub HTMLdie {
1168 my($msg)= @_ ;
1169 print "Content-type: text/html\n\n";
1170 print "<html><body>\n";
1171 print "$msg\n";
1172 print "</body></html>\n";
1173 exit;
1174 }
1175 ##############################################################################
1176  
1177 =head2 &isrunninglocaly()
1178  
1179 Description:
1180  
1181 Function returns TRUE if script executed in console, FALSE
1182 otherwise, e.g. when running under Apache.
1183  
1184 =cut
1185  
1186 sub isrunninglocaly {
1187 my($method) = defined($ENV{'REQUEST_METHOD'}) ? $ENV{'REQUEST_METHOD'} : 'LOCAL';
1188
1189 if ($method eq 'GET' || $method eq 'POST')
1190 {
1191 return 0;
1192 }
1193 return 1;
1194 }
1195 ##############################################################################
1196  
1197 =head2 $cookies = &getcookies($headers)
1198  
1199 Example:
1200
1201 $headers = "Server: Netscape-Enterprise/2.01
1202 Date: Thu, 29 Nov 2001 08:28:33 GMT
1203 Set-Cookie: registered=no; path=/; domain=.excite.com; expires=Wednesday, 31-Dec-2010 12:00:00 GMT
1204 Set-Cookie: UID=35813BB13C05F10C; path=/; domain=.excite.com; expires=Wednesday, 31-Dec-2010 12:00:00 GMT
1205 Location: http://www.excite.com/info/add_url/thanks/?url=&email=&country=&brand=excite
1206 X-Cache: MISS from ns.ahxk.ru
1207 Proxy-Connection: close";
1208
1209 $cookies = &getcookies($headers);
1210 print "$cookies\n";
1211  
1212 Output:
1213  
1214 UID=35813BB13C05F10C; registered=no
1215  
1216 =cut
1217  
1218 sub getcookies {
1219 my($headers)= @_ ;
1220  
1221 my %totalcookies = ();
1222  
1223 while($headers =~ m|^(Set-Cookie:\s+)(.*?)[\r\n\s]*$|img)
1224 {
1225 my @cookies = split(/;/,$2);
1226 $cookies[0] =~ /^\s*(.*?)\s*$/i;
1227 $cookies[0] = $1;
1228 $cookies[0] =~ m/^(.*?)=(.*)$/;
1229 $totalcookies{$1} = $2;
1230 }
1231  
1232 return joincookiesinhash(%totalcookies);
1233 }
1234 ##############################################################################
1235  
1236 =head2 $cookies = &joincookies($cookies,$newcookies)
1237  
1238 Example:
1239  
1240 $cookies = joincookies("C=1; D=2","D=3; F=2");
1241 print "$cookies\n";
1242  
1243 Output:
1244  
1245 C=1; D=3; F=2
1246  
1247 =cut
1248  
1249 sub joincookies {
1250 my($cookies,$newcookies) = @_;
1251  
1252 my(@cookies)=split(/;/,$cookies);
1253 my(@newcookies)=split(/;/,$newcookies);
1254  
1255 my %totalcookies = ();
1256  
1257 foreach (@cookies)
1258 {
1259 # Removing spaces from begin/end of string.
1260 $_ =~ /^\s*(.*?)\s*$/i;
1261 $_ = $1;
1262 my @pairs = split(/=/,$_);
1263 $totalcookies{$pairs[0]} = $pairs[1];
1264 }
1265  
1266 foreach (@newcookies)
1267 {
1268 # Removing spaces from begin/end of string.
1269 $_ =~ /^\s*(.*?)\s*$/i;
1270 $_ = $1;
1271 my @pairs = split(/=/,$_);
1272 $totalcookies{$pairs[0]} = $pairs[1];
1273 }
1274 return joincookiesinhash(%totalcookies);
1275 }
1276 ##############################################################################
1277  
1278 =head2 $cookies = &joincookiesinhash(%cookies)
1279  
1280 Example:
1281  
1282 %cookies = (D => 1,
1283 F => 2);
1284
1285 $cookies = &joincookiesinhash(%cookies);
1286 print "$cookies\n";
1287  
1288 Output:
1289  
1290 D=1; F=2
1291  
1292 =cut
1293  
1294 sub joincookiesinhash {
1295 my(%totalcookies) = @_;
1296  
1297 my $totalcookies = undef;
1298 my $cookiescount = 0;
1299  
1300 foreach (keys %totalcookies)
1301 {
1302 $cookiescount++;
1303 }
1304  
1305 if($cookiescount>=1)
1306 {
1307 foreach (keys %totalcookies)
1308 {
1309 $totalcookies = "$_=$totalcookies{$_}";
1310 delete($totalcookies{$_});
1311 last;
1312 }
1313 }
1314 if($cookiescount>1)
1315 {
1316 foreach (keys %totalcookies)
1317 {
1318 $totalcookies = $totalcookies . "; $_=$totalcookies{$_}";
1319 }
1320 }
1321 return $totalcookies;
1322 }
1323 ##############################################################################
1324  
1325 =head2 $rndstring = &GenerateRandomString($number);
1326  
1327 Description:
1328  
1329 This function returns string with $number random chars.
1330
1331 =cut
1332  
1333 sub GenerateRandomString {
1334 my($num)= @_ ;
1335
1336 my $outstring ='';
1337 my $y;
1338 for($y=0;$y<$num;$y++)
1339 {
1340 my $rndnum = int rand($#rndletters);
1341 my $letter = $rndletters[$rndnum];
1342 $outstring = "$outstring$letter";
1343 }
1344 return $outstring;
1345 }
1346 ##############################################################################
1347  
1348 =head2 $rndstring = &SelectRandomStringFromFile($file);
1349  
1350 Description:
1351  
1352 This function returns random string selected from text file $text.
1353 Returns undef if no selection available.
1354
1355 =cut
1356  
1357 sub SelectRandomStringFromFile {
1358 local($_) = shift;
1359
1360 my(@strings);
1361  
1362 if(!open(FILE,$_)) { return undef; }
1363 @strings = <FILE>;
1364 close(FILE);
1365  
1366 $_ = $strings[int(rand(@strings))];
1367 $_ = &Delete_CRLF_from_End_Of_String($_);
1368
1369 return $_;
1370 }
1371 ##############################################################################
1372  
1373 =head2 &CreateAndSendOutHtmlPage($a,$type);
1374  
1375 Description:
1376  
1377 This function will create HTML page, print it to stdout and exit.
1378 $type may be:
1379 1 - CreateAndSendOutHtmlPage() will execute script in
1380 $a path, print it output to stdout and exit.
1381 2 - CreateAndSendOutHtmlPage() will read file $a,
1382 print it contents to stdout and exit.
1383 3 - CreateAndSendOutHtmlPage() will print string
1384 "Location: $a\n\n" to stdout and exit.
1385  
1386 Examples:
1387 CreateAndSendOutHtmlPage("/path/index.cgi",1);
1388 CreateAndSendOutHtmlPage("/path/index.html",2);
1389 CreateAndSendOutHtmlPage("http://www.tapor.com",3);
1390
1391 =cut
1392  
1393 sub CreateAndSendOutHtmlPage {
1394 my($a,$type)= @_ ;
1395  
1396 my $output;
1397
1398 if($type == 1)
1399 {
1400 $output = `$a`;
1401 print $output;
1402 exit;
1403 }
1404  
1405 my @body;
1406  
1407 if($type == 2)
1408 {
1409 if(open(FILE,$a))
1410 {
1411 @body = <FILE>;
1412 close(FILE);
1413 }
1414
1415 print "Content-type: text/html\n\n";
1416 print @body;
1417 exit;
1418 }
1419  
1420 if($type == 3)
1421 {
1422 print "Location: http://$a\n\n";
1423 exit;
1424 }
1425
1426 }
1427 ##############################################################################
1428  
1429 =head2 &CheckForDomain($domain);
1430  
1431 Description:
1432
1433 This function does very simple task, it compare $domain with $ENV{'HTTP_HOST'}.
1434 If equal function returns TRUE otherwise it returns FALSE.
1435  
1436 =cut
1437
1438 sub CheckForDomain {
1439 my($domain)= @_;
1440
1441 if($domain ne $ENV{'HTTP_HOST'}) {return 0;}
1442  
1443 return 1;
1444 }
1445 ###############################################################################
1446  
1447 =head2 &SendToDomainIfNotThisDomain($domain);
1448  
1449 Description:
1450  
1451 This function resend Web user to new location if domain not $domain.
1452 See previous function description.
1453  
1454 =cut
1455  
1456 sub SendToDomainIfNotThisDomain {
1457 my($domain)= @_;
1458  
1459 if(!&CheckForDomain($domain))
1460 {
1461 print "Location: http://$domain\n\n";
1462 exit;
1463 }
1464 }
1465 ###############################################################################
1466  
1467 =head2 $string = &MassiveToString(@massive);
1468  
1469 Description:
1470
1471 This function does very simple task, it convert massive @massive to string
1472 $string.
1473  
1474 =cut
1475  
1476 sub MassiveToString {
1477 my(@body) = @_;
1478 my($commonline);
1479
1480 foreach (@body) { $commonline = $commonline . $_; }
1481 return $commonline;
1482 }
1483 ###############################################################################
1484  
1485 =head2 %out = &newsocketto(*S,$host,$port,$timeoutconnect);
1486  
1487 Description:
1488  
1489 This function returns connected socket to $host:$port if no error.
1490 $timeoutconnect is timeout to connect to $host:$port.
1491
1492 Usage:
1493  
1494 %out = &newsocketto(*S,$host,$port,$timeoutconnect);
1495
1496 Output:
1497  
1498 $out{'error'} == 0 - No errors
1499 $out{'error'} == 1 - Some Error.
1500 $out{'errortxt'} - Error description if $out{'error'} == 1
1501 S - connected socket to $host:$port.
1502  
1503 =cut
1504  
1505 sub newsocketto {
1506 local(*S) = shift;
1507 my($host,$port,$timeout) = @_;
1508 my($ok,$result);
1509
1510 my(%out);
1511 $out{'error'} = 1;
1512
1513 local($SIG{ALRM});
1514
1515 if ($^O ne 'MSWin32')
1516 {
1517 $SIG{ALRM} = sub { $out{'errortxt'} = create_error_string("TAPORlib::newsocketto()","Error connecting to $host:$port after $timeout seconds"); die;} if $timeout;
1518 }
1519  
1520 $result = eval {
1521 if ($^O ne 'MSWin32') {alarm($timeout) if ($timeout);}
1522  
1523 my $iaddr;
1524 if(!($iaddr=inet_aton($host)))
1525 {
1526 $out{'errortxt'} = create_error_string("TAPORlib::newsocketto()","Error resolving '$host'");
1527 if ($^O ne 'MSWin32') {alarm(0) if($timeout);}
1528 die;
1529 }
1530 my $paddr = sockaddr_in($port,$iaddr);
1531 if(!socket(S, AF_INET, SOCK_STREAM, 0))
1532 {
1533 $out{'errortxt'} = create_error_string("TAPORlib::newsocketto()","Error getting socket()");
1534 if ($^O ne 'MSWin32') {alarm(0) if($timeout);}
1535 die;
1536 }
1537 $ok = connect(S, $paddr);
1538 if(!$ok)
1539 {
1540 $out{'errortxt'} = create_error_string("TAPORlib::newsocketto()","Can't connect to '$host'");
1541 if ($^O ne 'MSWin32') {alarm(0) if($timeout);}
1542 die;
1543 }
1544 if ($^O ne 'MSWin32') {alarm(0) if($timeout);}
1545 1;
1546 };
1547 unless($result) {return %out;}
1548 $out{'error'} = 0;
1549 return %out;
1550 }
1551 ###############################################################################
1552  
1553 =head2 &CheckProxy($proxy);
1554  
1555 Description:
1556  
1557 This function checks for proxy server.
1558 $proxy server must be passed in the following format:
1559 129.168.1.1:80
1560
1561 Output:
1562
1563 TRUE if proxy server is valid.
1564 FALSE if proxy server is invalid.
1565  
1566 =cut
1567  
1568 sub CheckProxy {
1569 my($proxy,$logfile) = @_;
1570 my(%pagenow,%out);
1571  
1572 $pagenow{'method'} = "GET";
1573 $pagenow{'url'} = "http://www.yahoo.com";
1574 $pagenow{'proxy'} = $proxy;
1575 $pagenow{'logfile'} = $logfile ? $logfile : "/dev/null";
1576
1577 $pagenow{'timeoutconnect'} = 5;
1578 $pagenow{'timeoutrequest'} = 30;
1579
1580 my @body;
1581
1582 %out = GetPageNow_4(%pagenow);
1583 if($out{'error'}) { return 0; }
1584
1585 @body = $out{'body'};
1586
1587 foreach (@body) { if(/Yahoo!/i) { return 1; } }
1588
1589 return 1;
1590 }
1591 ##############################################################################
1592  
1593 =head2 $text_t=&win2koi($text) and $text_t=&koi2win($text)
1594  
1595 Description:
1596
1597 Functions does Win -> Koi and Koi -> Win translation.
1598  
1599 =cut
1600
1601 sub win2koi()
1602 {
1603 local($_) = shift;
1604 tr /¿áâ÷çäåöúéêëìíîïðòóôõæèãþûýÿùøüàñÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ/¿Â×ÞÚÄÅÃßÊËÌÍÎÏÐÒÔÕÆÈÖÉÇÀÙÜÑÝÛØÁÓâ÷þúäåãÿêëìíîïðòôõæèöéçàùüñýûøáó/;
1605 return $_;
1606 }
1607 sub koi2win()
1608 {
1609 local($_) = shift;
1610 tr /¿Â×ÞÚÄÅÃßÊËÌÍÎÏÐÒÔÕÆÈÖÉÇÀÙÜÑÝÛØÁÓâ÷þúäåãÿêëìíîïðòôõæèöéçàùüñýûøáó/¿áâ÷çäåöúéêëìíîïðòóôõæèãþûýÿùøüàñÁÂ×ÇÄÅÖÚÉÊËÌÍÎÏÐÒÓÔÕÆÈÃÞÛÝßÙØÜÀÑ/;
1611 return $_;
1612 }
1613  
1614 ##############################################################################
1615  
1616 =head2 $scriptname = &GetScriptName();
1617  
1618 Description:
1619
1620 Function returns current script name.
1621  
1622 =cut
1623  
1624 sub GetScriptName {
1625  
1626 if(isrunninglocaly()) { $_ = $ENV{'_'}; }
1627 else { $_ = $ENV{'SCRIPT_FILENAME'}; }
1628
1629 /([^\r\n]*\/)([^\/]*)/;
1630 $_ = $2;
1631
1632 return $_;
1633 }
1634 ###############################################################################
1635  
1636 =head2 &SetScriptDirAsCurrent();
1637  
1638 Description:
1639
1640 Function sets current directory to script directory.
1641  
1642 =cut
1643  
1644 sub SetScriptDirAsCurrent {
1645  
1646 if(isrunninglocaly()) { $_ = $ENV{'_'}; }
1647 else { $_ = $ENV{'SCRIPT_FILENAME'}; }
1648
1649 $_ =~ m/([^\r\n]*\/)[^\/]*/;
1650 $_ = $1;
1651
1652 chdir($_);
1653 }
1654 ###############################################################################
1655  
1656 =head2 &send_null_image();
1657  
1658 Description:
1659
1660 Sends NULL GIF image to client browser and exit.
1661  
1662 =cut
1663  
1664 sub send_null_image {
1665  
1666 print "Content-type: image/gif\n";
1667 print "Content-Length: 43\n\n";
1668  
1669 print "GIF89a\x01\0\x01\0\x80\0\0\0\0\0\xff\xff\xff\x21\xf9\x04\x01\0\0\0\0\x2c\0\0\0\0\x01\0\x01\0\x40\x02\x02\x44\x01\0\x3b";
1670 exit;
1671 }
1672 ##############################################################################
1673  
1674 =head2 &IsDateValid($day,$month,$year);
1675  
1676 Description:
1677  
1678 If date invalid function returns FALSE, otherwise TRUE.
1679  
1680 =cut
1681  
1682 sub IsDateValid {
1683 my($day,$month,$year) = @_;
1684  
1685 my %daysinmonth = (
1686 1 => 31,
1687 2 => 28,
1688 3 => 31,
1689 4 => 30,
1690 5 => 31,
1691 6 => 30,
1692 7 => 31,
1693 8 => 31,
1694 9 => 30,
1695 10 => 31,
1696 11 => 30,
1697 12 => 31,
1698 );
1699
1700 if($month < 1 || $month >12) { return 0;}
1701  
1702 if(int($year/4)*4 == $year) {$daysinmonth{'2'} = 29;}
1703 if(int($year/100)*100 == $year) {$daysinmonth{'2'} = 28;}
1704 if($day < 1 || $day > $daysinmonth{$month}) { return 0;}
1705
1706 return 1;
1707 }
1708 ###############################################################################
1709  
1710 =head2 %Config = &parse_form_2();
1711  
1712 Description:
1713  
1714 Parse forms content and returns it as hash %Config.
1715  
1716 =cut
1717  
1718 sub parse_form_2 {
1719  
1720 my($method) = defined($ENV{'REQUEST_METHOD'}) ? $ENV{'REQUEST_METHOD'} : 'LOCAL';
1721
1722 my(@pairs,$buffer,%Config);
1723
1724 if ($method eq 'GET') {
1725 @pairs = split(/&/, $ENV{'QUERY_STRING'});
1726 }
1727 elsif ($method eq 'POST') {
1728 read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
1729 @pairs = split(/&/, $buffer);
1730 }
1731 else
1732 { @pairs = @ARGV; }
1733  
1734 foreach my $pair (@pairs) {
1735  
1736 my($name, $value) = split(/=/, $pair);
1737  
1738 $name =~ s/\+/ /g;
1739 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
1740  
1741 $value =~ s/\+/ /g;
1742 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
1743 $value =~ s/<!--(.|\n)*-->//g;
1744  
1745 $Config{$name} = $value;
1746 }
1747  
1748 return %Config;
1749 }
1750 ##############################################################################
1751  
1752 =head2 $date = &GetDate_2();
1753  
1754 Description:
1755  
1756 Returns $date in string format.
1757 For Russian users also GetDateRus_2() function available.
1758  
1759 =cut
1760  
1761 sub GetDate_2 {
1762 my($timemodifier) = @_;
1763  
1764 if(!$timemodifier) {$timemodifier = 0;}
1765  
1766 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time + $timemodifier*3600);
1767  
1768 if (length ($min) eq 1) {$min= '0'.$min;}
1769  
1770 $mon ++;
1771 $year = $year + 1900;
1772  
1773 my $date = "$mon/$mday/$year, $hour:$min:$sec";
1774 my $daterus = "$mday/$mon/$year, $hour:$min:$sec";
1775  
1776 return ($daterus,$date);
1777 }
1778 sub GetDateRus_2 {
1779 my($timemodifier) = @_;
1780
1781 return (GetDate_2($timemodifier))[0];
1782 }
1783 ##############################################################################
1784  
1785 =head2 $flag = &SendEmailMsg($from,$email,$msg);
1786  
1787 Example:
1788
1789 $flag = &SendEmailMsg("from@tapor.com","to@tapor.com","test message");
1790 if(!$flag)
1791 {
1792 print "Can't e-mail to to@tapor.com :(\n";
1793 }
1794  
1795 =cut
1796  
1797 sub SendEmailMsg {
1798 my($from,$email,$msg) = @_;
1799  
1800 my($mailprog) = "/usr/sbin/sendmail";
1801  
1802 my $date = &GetDate_2;
1803  
1804 if(open(MAIL,"|$mailprog -t"))
1805 {
1806 print MAIL "To: <$email>\n";
1807 print MAIL "From: $from <$email>\n";
1808 print MAIL "Subject: $date\n\n";
1809  
1810 print MAIL "$msg\n";
1811 close (MAIL);
1812 return 1;
1813 }
1814 return 0;
1815 }
1816 ###############################################################################
1817  
1818 =head2 $flag = &SendEmailMultiMsg($from,$email,@msg);
1819  
1820 Example:
1821
1822 $flag = &SendEmailMultiMsg("from@tapor.com","to@tapor.com",("test message","2 line"));
1823 if(!$flag)
1824 {
1825 print "Can't e-mail to to@tapor.com :(\n";
1826 }
1827  
1828 =cut
1829  
1830 sub SendEmailMultiMsg {
1831 my($from,$email,@msg) = @_;
1832  
1833 my($mailprog) = "/usr/sbin/sendmail";
1834  
1835 my $date = &GetDate_2;
1836  
1837 if(open(MAIL,"|$mailprog -t"))
1838 {
1839 print MAIL "To: <$email>\n";
1840 print MAIL "From: $from <$email>\n";
1841 print MAIL "Subject: $date\n\n";
1842  
1843 foreach my $line (@msg)
1844 {
1845 $line = Delete_CRLF_from_End_Of_String($line);
1846 print MAIL "$line\n";
1847 }
1848
1849 close (MAIL);
1850 return 1;
1851 }
1852 return 0;
1853 }
1854 ###############################################################################
1855 sub create_error_string {
1856 my ($who,$error,$end) = @_;
1857  
1858 return "$who error - \"$error\"" . $end;
1859 }
1860 ###############################################################################
1861  
1862 =head2 @cuttedmsv = &LimitMassive(10,@msv);
1863  
1864 Description:
1865  
1866 Function will cut massive @msv to size 10.
1867  
1868 =cut
1869  
1870 sub LimitMassive {
1871 my($limit,@msv) = @_;
1872  
1873 if(($#msv+1)>$limit) { $#msv = ($limit - 1); };
1874 return @msv;
1875 }
1876 ###############################################################################
1877  
1878 =head2 @tops = &GetTops(@msv);
1879  
1880 Example:
1881
1882 @msv = qw(test
1883 test
1884 test
1885 test2
1886 test2
1887 );
1888 @tops = &GetTops(@msv);
1889  
1890 Output:
1891
1892 @tops = ("test - 3","test2 - 2");
1893  
1894 =cut
1895  
1896 sub GetTops {
1897 my(@Keys) = @_;
1898  
1899 my(%Tops,@Tops);
1900
1901 foreach (@Keys)
1902 {
1903 if(!$_) {next;}
1904
1905 if($Tops{$_}) { $Tops{$_}++; }
1906 else { $Tops{$_} = 1; }
1907 }
1908  
1909 foreach (keys %Tops) { $Tops{$_} = "$Tops{$_}:$_"; }
1910  
1911 %Tops = reverse %Tops;
1912  
1913 foreach (sort {$a <=> $b} keys %Tops)
1914 {
1915 $_ =~ m/([0-9]+):(.*)/;
1916 push(@Tops,"$Tops{$_} - $1");
1917 }
1918
1919 return reverse @Tops;
1920 }
1921 ###############################################################################
1922  
1923 =head2 &IsSearchEngine("./robots");
1924  
1925 Description:
1926
1927 Function will test visitor of your page.
1928 It will return Search Engine Name if visitor is Serch Engine.
1929 It will return FALSE if visitor is surfer.
1930 The "./robots" is directory. See "./txt/robots" in the package.
1931
1932 =cut
1933  
1934 sub IsSearchEngine {
1935 my($enginelistdir) = (@_);
1936  
1937 #------------------------------------------------------------------------------
1938 my $gethost = $ENV{'REMOTE_HOST'};
1939 my $getaddr = $ENV{'REMOTE_ADDR'};
1940 my $getagent = $ENV{'HTTP_USER_AGENT'};
1941 my $getreferer = $ENV{'HTTP_REFERER'};
1942  
1943 if(!$gethost) { $gethost = $getaddr; }
1944  
1945 my @exclude = ("204.123.9.65", # @exclude is for certain applications
1946 "204.123.9.66", # sponsored by search engines such
1947 "204.123.9.67", # as Babelfish that translate pages into
1948 "204.123.9.68", # other languages and display them. You
1949 "204.123.9.106", # don't want humans seeing this, so I
1950 "204.123.9.107", # made an exclude list.
1951 "204.152.191.27",
1952 "204.152.191.28",
1953 "204.152.191.29",
1954 "204.152.190.27",
1955 "204.152.190.28",
1956 "204.152.190.29",
1957 "204.152.190.37",
1958 "204.152.190.154",
1959 "204.162.96.104",
1960 "204.162.96.154",
1961 "204.162.96.176",
1962 "209.247.194.100",);
1963  
1964 #------------------------------------------------------------------------------
1965 my $count;
1966 my (@enginelist1,@enginelist2,@enginelist3,@enginelist4,@enginelist5);
1967 my (@enginelist6,@enginelist7,@enginelist8,@enginelist9);
1968  
1969 #------------------------------------------------------------------------------
1970 # Openning
1971 for ($count=1; $count<=9; $count++)
1972 {
1973 open (ENGINELIST, "$enginelistdir/$count" . ".list");
1974 flock(ENGINELIST, 2);
1975 @enginelist1 =<ENGINELIST> if $count eq 1;
1976 @enginelist2 =<ENGINELIST> if $count eq 2;
1977 @enginelist3 =<ENGINELIST> if $count eq 3;
1978 @enginelist4 =<ENGINELIST> if $count eq 4;
1979 @enginelist5 =<ENGINELIST> if $count eq 5;
1980 @enginelist6 =<ENGINELIST> if $count eq 6;
1981 @enginelist7 =<ENGINELIST> if $count eq 7;
1982 @enginelist8 =<ENGINELIST> if $count eq 8;
1983 @enginelist9 =<ENGINELIST> if $count eq 9;
1984 flock(ENGINELIST, 8);
1985 close (ENGINELIST);
1986 }
1987  
1988 #------------------------------------------------------------------------------
1989 # Checking
1990 my @list;
1991 my ($robotname,$isRobot);
1992  
1993 for ($count=1;$count<=9;$count++)
1994 {
1995 @list = @enginelist1 if $count eq 1; # Infoseek
1996 @list = @enginelist2 if $count eq 2; # Alta Vista
1997 @list = @enginelist3 if $count eq 3; # Lycos
1998 @list = @enginelist4 if $count eq 4; # Inktomi
1999 @list = @enginelist5 if $count eq 5; # Excite
2000 @list = @enginelist6 if $count eq 6; # Google
2001 @list = @enginelist7 if $count eq 7; # Northern Light
2002 @list = @enginelist8 if $count eq 8; # Misc
2003 @list = @enginelist9 if $count eq 9; # Custom 1
2004  
2005 $robotname = undef;
2006 foreach my $line (@list)
2007 {
2008 chomp $line;
2009 $line =~ s/ //g;
2010 next if (!$line);
2011 if($line =~ /^\#/)
2012 {
2013 $robotname = $line;
2014 next;
2015 }
2016 if (($gethost =~ /$line/i) || ($getaddr =~ /$line/i))
2017 {
2018 $isRobot = $count;
2019 if ($gethost =~ /babelfish/i) { $isRobot = 0; }
2020 foreach my $ip (@exclude)
2021 {
2022 if ($getaddr =~ /$ip/i) { $isRobot = 0; }
2023 }
2024 }
2025 last if $isRobot eq $count;
2026 }
2027 last if $isRobot eq $count;
2028 }
2029  
2030 if($isRobot) { return $robotname; }
2031  
2032 #------------------------------------------------------------------------------
2033 my @ROBOTS;
2034  
2035 open (FILE,"$enginelistdir/robots.txt");
2036 @ROBOTS=<FILE>;
2037 close FILE;
2038 chomp (@ROBOTS);
2039  
2040 foreach (@ROBOTS)
2041 {
2042 my ($robotname,$AGENT,$IP)=split(/\|/,$_);
2043 if ($ENV{'REMOTE_ADDR'} =~ /$IP/i) { return $robotname; }
2044 }
2045 #------------------------------------------------------------------------------
2046 my @useragents = ("Mozilla/",
2047 "Opera/",
2048 "Lynx/");
2049  
2050 foreach (@useragents)
2051 {
2052 if($getagent =~ m/^$_/i) { return undef; }
2053 }
2054 return "UnknownRobot";
2055 }
2056 ###############################################################################
2057  
2058 =head2 $os = &DetectOperationSystem($ENV{'HTTP_USER_AGENT'});
2059  
2060 Description:
2061
2062 Function returns operation system which visitor is uses.
2063
2064 =cut
2065  
2066 sub DetectOperationSystem {
2067 my($agent) = @_;
2068  
2069 foreach (keys %operation_systems)
2070 {
2071 if($agent =~ m/$_/) { return $operation_systems{$_}; }
2072 }
2073 return "UNKNOWN";
2074 }
2075 ##############################################################################
2076  
2077 =head2 &IsAllreadyRunning($numstarts); / &IsAllreadyRunning_2($numstarts);
2078  
2079 Description:
2080  
2081 Functions checks if script allready executed.
2082 If script allready executed functions returns TRUE, otherwise FALSE.
2083 $numstarts defines what maximum executions are allowed.
2084 We recomend you to use IsAllreadyRunning_2() function.
2085
2086 =cut
2087  
2088 sub IsAllreadyRunning {
2089 my($numstarts,$LISTEN_PORT) = @_;
2090  
2091 my($file) = "__listenport.txt";
2092
2093 unless (-e $file)
2094 {
2095 if(!$LISTEN_PORT)
2096 {
2097 $LISTEN_PORT = int rand(500);
2098 $LISTEN_PORT = $LISTEN_PORT + 21600;
2099 }
2100 open(FILE,">$file");
2101 print FILE $LISTEN_PORT;
2102 close(FILE);
2103 }
2104 open(FILE,$file);
2105 $LISTEN_PORT = <FILE>;
2106 chomp($LISTEN_PORT);
2107 close(FILE);
2108
2109 socket(SE, PF_INET, SOCK_STREAM, 'udp');
2110 setsockopt(SE, SOL_SOCKET, SO_REUSEADDR, 1);
2111
2112 my $x;
2113
2114 for($x=0;$x<$numstarts;$x++,$LISTEN_PORT++)
2115 {
2116 bind(SE, sockaddr_in($LISTEN_PORT, INADDR_ANY)) or next;
2117 return 0;
2118 }
2119 return 1;
2120 }
2121  
2122 sub IsAllreadyRunning_2 {
2123 my($numstarts,$savefilesto) = @_;
2124  
2125 if(!defined($savefilesto)) {$savefilesto = "./";}
2126  
2127 my($lockfile) = "__lock";
2128 my($x,$flockret,$commonret);
2129
2130 if($numstarts <= 0) {return 1;}
2131
2132 $commonret = 1;
2133 for($x=1;$x<=$numstarts;$x++)
2134 {
2135 if(!open(LOCKING_FILE_SPEC,">$savefilesto/" . $lockfile . "_" . $x)) {next;}
2136 $flockret = flock(LOCKING_FILE_SPEC,2 + 4);
2137  
2138 if($flockret)
2139 {
2140 $commonret = 0;
2141 last;
2142 }
2143 }
2144 return $commonret;
2145 }
2146 ##############################################################################
2147 sub change_spec_labels {
2148 my($ll)= @_ ;
2149  
2150 my @envkeys = qw (
2151 HTTP_USER_AGENT
2152 HTTP_REFERER
2153 SERVER_SOFTWARE
2154 SERVER_NAME
2155 GATEWAY_INTERFACE
2156 SERVER_NAME
2157 SERVER_PROTOCOL
2158 SERVER_PORT
2159 REQUEST_METHOD
2160 HTTP_ACCEPT
2161 PATH_INFO
2162 PATH_TRANSLATED
2163 SCRIPT_NAME
2164 QUERY_STRING
2165 REMOTE_HOST
2166 REMOTE_ADDR
2167 REMOTE_USER
2168 AUTH_TYPE
2169 CONTENT_TYPE
2170 CONTENT_LENGTH
2171 HTTP_FROM
2172 REMOTE_IDENT
2173 );
2174  
2175 my $serverport = defined($ENV{'SERVER_PORT'}) ? $ENV{'SERVER_PORT'} : undef;
2176 my $portst = $serverport==80 ? '' : ':' . $serverport;
2177
2178 my $thisurl = join('','http://',defined($ENV{'SERVER_NAME'}) ? $ENV{'SERVER_NAME'} : '',$portst,defined($ENV{'SCRIPT_NAME'}) ? $ENV{'SCRIPT_NAME'} : '');
2179 $thisurl =~ m|^([^\s\?]+)(/{1}?)|i;
2180 my $thisshorturl = $1;
2181
2182 $ll =~ s|###RANDOM:([0-9]+)-([0-9]+)###|$1+(int rand($2-$1+1))|igme;
2183 $ll =~ s|###THISURL###|defined($thisurl) ? $thisurl : ''|igse;
2184 $ll =~ s|###THISURL_WITHOUT_SCRIPT_NAME###|defined($thisshorturl) ? $thisshorturl : ''|igse;
2185  
2186 foreach (@envkeys)
2187 {
2188 $ll =~ s|###ENV_$_###|defined($ENV{$_}) ? $ENV{$_} : ''|gise;
2189 }
2190  
2191 return $ll;
2192 }
2193 ##############################################################################
2194 sub ViewSpecHtmFile {
2195 my($htmfile) = @_;
2196  
2197 open(FILE,"$htmfile") || &HTMLdie("ViewSpecHtmFile: Can't open file: '$htmfile'");
2198 my @htmfile = <FILE>;
2199 close(FILE);
2200  
2201 my $commonline_htm = MassiveToString(@htmfile);
2202 $commonline_htm = &change_spec_labels($commonline_htm);
2203  
2204 print "Content-type: text/html\n\n";
2205 print "$commonline_htm";
2206  
2207 exit;
2208 }
2209 ###############################################################################
2210  
2211 =head2 ($proxyaddr,$proxyport) = &SelectRandomProxyServerFromFile($proxy);
2212  
2213 Description:
2214  
2215 This function returns random proxy server selected from file $proxy.
2216
2217 =cut
2218  
2219 sub SelectRandomProxyServerFromFile {
2220 my($proxyfile) = @_;
2221  
2222 my $hostport = &SelectRandomStringFromFile($proxyfile);
2223 if(!$hostport) { return (undef,undef); }
2224 $hostport = Delete_CRLF_from_End_Of_String($hostport);
2225 $hostport =~ s/ //g;
2226  
2227 my (@pairs,$proxyaddr,$proxyport);
2228  
2229 @pairs = split(/:/,$hostport);
2230  
2231 return ($pairs[0],$pairs[1]);
2232 }
2233 ###############################################################################
2234 sub CorrectLinksOnPage {
2235 my($string,$url) = @_;
2236
2237 if(!$string || !$url) { return undef;};
2238  
2239 my ($baseurl,$relurl,$relurl2) = GetRelativeUrls($url);
2240  
2241 my @body = split(/>/,$string);
2242
2243 foreach (@body) {
2244  
2245 if((/<\s*form\b/im) && !(/\baction\s*=/im) && !(/\bscript\s*=/im))
2246 {
2247 s|<\s*form\b|<form action="$url"|im;
2248 }
2249  
2250 # Put the most common cases first
2251  
2252 s/(<[^>]*\bhref\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2253 next if /<\s*a\b/im;
2254  
2255 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2256 s/(<[^>]*\blowsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2257 s/(<[^>]*\blongdesc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2258 s/(<[^>]*\busemap\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2259 s/(<[^>]*\bdynsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2260 next if /<\s*img\b/im;
2261  
2262 s/(<[^>]*\bbackground\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2263 next if /<\s*body\b/im;
2264  
2265 s/(<[^>]*\bhref\s*=\s*["']?)([^\s"'>]*)/ $1 . &GetRelativeUrls(&full_url($2,$url)) /ime,
2266 next if /<\s*base\b/im ; # has special significance
2267  
2268 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2269 s/(<[^>]*\blongdesc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2270 next if /<\s*frame\b/im ;
2271  
2272 s/(<[^>]*\baction\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2273 s/(<[^>]*\bscript\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2274 next if /<\s*form\b/im ; # needs special attention
2275  
2276 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2277 s/(<[^>]*\busemap\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2278 next if /<\s*input\b/im ;
2279  
2280 s/(<[^>]*\bhref\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2281 next if /<\s*area\b/im ;
2282  
2283 s/(<[^>]*\bcodebase\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2284 s/(<[^>]*\bcode\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2285 s/(<[^>]*\bobject\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2286 s/(<[^>]*\barchive\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2287 next if /<\s*applet\b/im ;
2288  
2289  
2290 # These are seldom-used tags, or tags that seldom have URLs in them
2291  
2292 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2293 next if /<\s*bgsound\b/im ; # Microsoft only
2294  
2295 s/(<[^>]*\bcite\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2296 next if /<\s*blockquote\b/im ;
2297  
2298 s/(<[^>]*\bcite\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2299 next if /<\s*del\b/im ;
2300  
2301 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2302 next if /<\s*embed\b/im ; # Netscape only
2303  
2304 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2305 s/(<[^>]*\bimagemap\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2306 next if /<\s*fig\b/im ; # HTML 3.0
2307  
2308 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2309 next if /<\s*h[1-6]\b/im ; # HTML 3.0
2310  
2311 s/(<[^>]*\bprofile\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2312 next if /<\s*head\b/im ;
2313  
2314 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2315 next if /<\s*hr\b/im ; # HTML 3.0
2316  
2317 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2318 s/(<[^>]*\blongdesc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2319 next if /<\s*iframe\b/im ;
2320  
2321 s/(<[^>]*\bcite\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2322 next if /<\s*ins\b/im ;
2323  
2324 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2325 next if /<\s*layer\b/im ;
2326  
2327 s/(<[^>]*\bhref\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2328 s/(<[^>]*\burn\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2329 next if /<\s*link\b/im ;
2330  
2331 s/(<[^>]*\burl\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2332 next if /<\s*meta\b/im ; # Netscape only
2333  
2334 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2335 next if /<\s*note\b/im ; # HTML 3.0
2336  
2337 s/(<[^>]*\busemap\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2338 s/(<[^>]*\bcodebase\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2339 s/(<[^>]*\bdata\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2340 s/(<[^>]*\barchive\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2341 s/(<[^>]*\bclassid\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2342 s/(<[^>]*\bname\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2343 next if /<\s*object\b/im ;
2344  
2345 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2346 s/(<[^>]*\bimagemap\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2347 next if /<\s*overlay\b/im ; # HTML 3.0
2348  
2349 s/(<[^>]*\bcite\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2350 next if /<\s*q\b/im ;
2351  
2352 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2353 s/(<[^>]*\bfor\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2354 next if /<\s*script\b/im ;
2355  
2356 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2357 next if /<\s*select\b/im ; # HTML 3.0
2358  
2359 s/(<[^>]*\bsrc\s*=\s*["']?)([^\s"'>]*)/ $1 . &full_url($2,$url) /ime,
2360 next if /<\s*ul\b/im ; # HTML 3.0
2361  
2362 } # foreach (@body)
2363  
2364 my ($commonline) = undef;
2365 foreach (@body) {$commonline = $commonline . $_ . ">";}
2366 substr($commonline,-1) = "";
2367  
2368 return $commonline;
2369 }
2370 ###############################################################################
2371 sub full_url{
2372 my($link,$url)= @_ ;
2373  
2374 my $oldlink=$link;
2375  
2376 my ($baseurl,$relurl,$relurl2) = GetRelativeUrls($url);
2377  
2378 if($link=~ m|^(http://)|i) {goto exit_full_url;}
2379 if($link=~ m|^(mailto:)|i) {goto exit_full_url;}
2380 if($link=~ m|^(javascript:)|i) {goto exit_full_url;}
2381 if($link=~ m|^(#)|i) {goto exit_full_url;}
2382  
2383 $link=~ s|^/|$baseurl/|i;
2384 $link=~ s|^\./|$relurl/|i;
2385 $link=~ s|^\.\./|$relurl2/|i;
2386  
2387 if(!($link=~ m|^(http://)|i)) {$link = "$relurl/$link";}
2388  
2389 exit_full_url:
2390  
2391 return $link;
2392 }
2393 ###############################################################################
2394 sub GetRelativeUrls {
2395 my($url) = @_;
2396  
2397 $url =~ m|^(http://)([^/\?\r\n]*)|i;
2398 my $host = $2;
2399  
2400 ### http://adm.ict.nsc.ru/rus/docs/perl/ - http://adm.ict.nsc.ru
2401 ### http://www.irtel.ru/ - http://www.irtel.ru
2402 ### http://www.irtel.ru - http://www.irtel.ru
2403  
2404 my $baseurl = "http://$host";
2405  
2406 ### http://adm.ict.nsc.ru/rus/docs/perl/ - http://adm.ict.nsc.ru/rus/docs/perl
2407 ### http://www.irtel.ru/ - http://www.irtel.ru
2408 ### http://www.irtel.ru - http://www.irtel.ru
2409  
2410 my $relurl;
2411  
2412 if($url =~ m|^(http://)(.*)[/]+|i) {$relurl = "http://$2";}
2413 else {$relurl = $baseurl;}
2414  
2415 ### http://adm.ict.nsc.ru/rus/docs/perl/ - http://adm.ict.nsc.ru/rus/docs
2416 ### http://www.irtel.ru/ - http://www.irtel.ru
2417 ### http://www.irtel.ru - http://www.irtel.ru
2418  
2419 my $relurl2;
2420  
2421 if($relurl =~ m|^(http://)(.*)[/]+|i) {$relurl2 = "http://$2";}
2422 else {$relurl2 = $baseurl;}
2423  
2424 return ($baseurl,$relurl,$relurl2);
2425 }
2426 ###############################################################################
2427  
2428 =head1 COPYRIGHT
2429  
2430 Copyright (c) 2000-2001 TAPOR, Inc. All rights reserved.
2431 This program is free software; you can redistribute it and/or modify
2432 it under the same terms as Perl itself.
2433  
2434 http://www.tapor.com/TAPORlib/
2435  
2436 =cut