1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/* $Id: html.cpp,v 1.6 2000/11/10 19:04:17 dbryson Exp $
Xbase project source code
This file contains the basic Xbase routines for creating html
Copyright (C) 1997 StarTech, Gary A. Kunkel
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact:
Mail:
Technology Associates, Inc.
XBase Project
1455 Deming Way #11
Sparks, NV 89434
USA
Email:
xbase@techass.com
See our website at:
xdb.sourceforge.net
V 1.0 10/10/97 - Initial release of software
V 1.5 1/2/98 - Added memo field support
V 1.6a 4/1/98 - Added expression support
V 1.6b 4/8/98 - Numeric index keys
V 1.7.4.c 7/7/98 - Continued development on GenFormFields method
*/
#ifdef __GNUG__
#pragma implementation "html.h"
#endif
#ifdef __WIN32__
#include <xbase/xbconfigw32.h>
#else
#include <xbase/xbconfig.h>
#endif
#include <xbase/xbase.h>
#ifdef XB_HTML
#include <ctype.h>
#include <fcntl.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/types.h> /* BSDI BSD/OS 3.1 */
#include <xbase/xbexcept.h>
/*! \file html.cpp
*/
/************************************************************************/
//! Short description
/*!
\param String
*/
void xbHtml::TextOut( const char * String )
{
std::cout << String;
}
/************************************************************************/
//! Short description
/*!
*/
void xbHtml::InitVals( void )
{
FieldNameArray = NULL;
DataValueArray = NULL;
NoOfDataFields = 0;
HtmlBufLen = 0;
HtmlWorkBuf = NULL;
LoadArray();
}
/************************************************************************/
//! Short description
/*!
*/
xbHtml::xbHtml( void )
{
InitVals();
}
/************************************************************************/
//! Short description
/*!
*/
void xbHtml::DumpArray( void )
{
/* dont forget Content-type */
xbShort i;
if( NoOfDataFields == 0 )
std::cout << "No Input Data From Form\n";
else
{
std::cout << "There are " << NoOfDataFields << " fields";
std::cout << "<BR>" << NoOfDataFields << " Field Name / Data Values received\n";
std::cout << "<BR>-----------------------------------\n";
for( i = 0; i < NoOfDataFields; i++ )
{
std::cout << "<br>" << FieldNameArray[i] << " => ";
// PrintEncodedString( DataValueArray[i] );
if( DataValueArray[i] )
std::cout << DataValueArray[i];
}
}
}
/***********************************************************************/
//! Short description
/*!
\param pos
*/
char * xbHtml::GetData( xbShort pos )
{
if( pos < NoOfDataFields && pos >= 0 )
return DataValueArray[pos];
else
return NULL;
}
/************************************************************************/
//! Short description
/*!
\param FieldName
*/
xbShort xbHtml::GetArrayNo( const char * FieldName )
{
xbShort i;
for( i = 0; i < NoOfDataFields; i++ )
if( strcmp( FieldName, FieldNameArray[i] ) == 0 )
return i;
return -1;
}
/************************************************************************/
//! Short description
/*!
\param FieldName
*/
char * xbHtml::GetDataForField( const char * FieldName )
{
return( GetData( GetArrayNo( FieldName )));
}
/************************************************************************/
//! Short description
/*!
*/
void xbHtml::LoadArray( void )
{
xbShort Len; /* length of data string from form */
xbShort Len1, Len2; /* tokenized string lengths */
xbShort i;
char *FormData; /* actual data from form */
char *QueryStringPtr;
char *Token;
char **Names, **Values;
if( !getenv( "REQUEST_METHOD" )) return;
/* put the input data into field FormData */
if( !strcmp( "POST", getenv( "REQUEST_METHOD" )))
{
if(( Len = atoi( getenv( "CONTENT_LENGTH" ))) == 0 )
return;
FormData = (char *) malloc( sizeof( char ) * ( Len + 1 ));
fgets( FormData, Len+1, stdin );
}
else /* GET */
{
if(( QueryStringPtr = getenv( "QUERY_STRING" )) == NULL ) return;
Len = strlen( QueryStringPtr );
if( Len == 0 ) return;
FormData = (char *) malloc( sizeof( char ) * ( Len + 1 ));
strcpy( FormData, QueryStringPtr );
}
/* count the number of data fields, & is the field separator */
Token = strchr( FormData, '&' );
NoOfDataFields++;
while( Token != NULL )
{
NoOfDataFields++;
Token++;
Token = strchr( Token, '&' );
}
Names = (char **)malloc(sizeof(char*)*NoOfDataFields);
Values = (char **)malloc(sizeof(char*)*NoOfDataFields);
for( i =0, Token=strtok(FormData, "&"); Token != NULL; i++ )
{
Len1 = strlen( Token );
Len2 = strcspn( Token, "=" );
Names[i] = (char *) malloc( sizeof(char) * (Len2 + 1));
strncpy( Names[i], Token, Len2 );
Names[i][Len2] = 0x00;
DeleteEscChars( Names[i] );
if( Len1 != Len2+1 )
{
Values[i] = (char *)malloc(sizeof(char) * (Len1 - Len2));
strcpy( Values[i], Token+Len2+1 );
DeleteEscChars( Values[i] );
}
else /* no data for field name=& */
Values[i] = NULL;
Token = strtok( NULL, "&" );
}
free( FormData );
FieldNameArray = Names;
DataValueArray = Values;
}
/************************************************************************/
//! Short description
/*!
\param String
*/
void xbHtml::DeleteEscChars( char * String )
{
xbShort s,t; /* source && target */
char HexVal[3];
xbShort EscCnt = 0;
for( s=0, t=0; String[s]; s++, t++ )
{
if( String[s] == '+' )
String[t] = ' ';
else if( String[s] == '%' )
{
HexVal[0] = String[s+1];
HexVal[1] = String[s+2];
HexVal[2] = 0x00;
String[t] = strtol( HexVal, NULL, 16 );
s+=2;
EscCnt++;
}
else
String[t] = String[s];
}
for( t = strlen( String ) - 1; t > 1 && EscCnt > 0; t-=2, EscCnt-- )
{
String[t] = 0x20;
String[t-1] = 0x20;
}
}
/************************************************************************/
//! Short description
/*!
\param File
*/
xbLong xbHtml::Tally( const char * File )
{
/* FIXME: Locking works under Unix only: <fcntl.h> does not warrant fcntl()
Noticed with Borland 4.5. Current workaround effectively disables locking
even with XB_LOCKING_ON -- willy */
/* must have write access to the directory for this routine to work */
#if defined(HAVE_FCNTL) && defined(XB_LOCKING_ON)
struct flock fl;
#endif
xbLong cnt;
FILE *f;
xbShort rc;
if(( f = fopen( File, "r+" )) == NULL )
{
/* land here if file does not exist - initialize it */
if(( f = fopen( File, "w+" )) == NULL ) return 0L;
rc = fprintf( f, "%08lu\n", 1L );
fclose( f );
if( rc == EOF )
return 0L;
else
return 1L;
}
/* lock the file */
#if defined(HAVE_FCNTL) && defined(XB_LOCKING_ON)
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0L;
fl.l_len = 1L;
fcntl( fileno( f ), F_SETLKW, &fl );
#endif
fseek( f, 0L, SEEK_SET );
fscanf( f, "%08lu", &cnt );
fseek( f, 0L, SEEK_SET );
rc = fprintf( f, "%08lu\n", ++cnt );
/* unlock the file */
#if defined(HAVE_FCNTL) && defined(XB_LOCKING_ON)
fl.l_type = F_UNLCK;
fcntl( fileno( f ), F_SETLKW, &fl );
#endif
fclose( f );
return cnt;
}
/************************************************************************/
//! Short description
/*!
*/
xbShort xbHtml::PostMethod( void )
{
char s[5];
xbShort i;
if( !getenv( "REQUEST_METHOD" ))
return 0;
else
{
memset( s, 0x00, 5 );
strncpy( s, getenv( "REQUEST_METHOD" ), 4 );
for( i = 0; i < 5; i++) s[i] = toupper( s[i] );
if( strcmp( s, "POST" ) == 0 ) return 1;
}
return 0;
}
/************************************************************************/
//! Short description
/*!
*/
xbShort xbHtml::GetMethod( void )
{
char s[4];
xbShort i;
if( !getenv( "REQUEST_METHOD" ))
return 0;
else
{
memset( s, 0x00, 4 );
strncpy( s, getenv( "REQUEST_METHOD" ), 3 );
for( i = 0; i < 4; i++) s[i] = toupper( s[i] );
if( strcmp( s, "GET" ) == 0 ) return 1;
}
return 0;
}
/************************************************************************/
//! Short description
/*!
\param d
\param Option
\param Title
\param fl
*/
xbShort xbHtml::GenFormFields(xbDbf *d, xbShort Option, const char *Title,
xbFieldList *fl) {
xbShort i;
char buf[255];
std::cout << "\n<TABLE>";
if( Title )
std::cout << "\n<CAPTION ALIGN=\"TOP\">" << Title << "</CAPTION><BR>";
i = 0;
while( fl[i].FieldLen != 0 )
{
std::cout << "\n<BR><TR><TH ALIGN=\"LEFT\">" << fl[i].Label;
if( fl[i].Option == 2 )
{
if( !d )
xb_error(XB_NOT_OPEN);
d->GetField( fl[i].FieldNo, buf );
std::cout << "<TD>" << buf;
}
else
{
std::cout << "<TD><INPUT TEXT NAME = \"" << fl[i].FieldName << "\"";
std::cout << " size = " << fl[i].FieldLen;
if( fl[i].Option == 1 )
std::cout << " TYPE=\"password\" ";
std::cout << " value = ";
if( Option == 1 )
{
if( !d )
xb_error(XB_NOT_OPEN);
d->GetField( fl[i].FieldNo, buf );
std::cout << "\"" << buf << "\"";
}
std::cout << ">";
}
i++;
}
std::cout << "\n</TABLE>";
return XB_NO_ERROR;
}
/************************************************************************/
//! Short description
/*!
\param Title
*/
void xbHtml::StartHtmlPage( const char * Title )
{
std::cout << "Content-type: text/html\n\n";
std::cout << "\n<HTML><HEAD><TITLE>" << Title << "</TITLE></HEAD><BODY>";
}
/************************************************************************/
//! Short description
/*!
\param c
*/
void xbHtml::PrintEncodedChar( char c )
{
switch( c )
{
case '<': std::cout << "<"; break;
case '>': std::cout << ">"; break;
case '&': std::cout << "&"; break;
case '"': std::cout << """; break;
default: std::cout << c; break;
}
return;
}
/************************************************************************/
//! Short description
/*!
\param s
*/
void xbHtml::PrintEncodedString( const char * s )
{
const char *p;
p = s;
while( *p ) PrintEncodedChar( *p++ );
return;
}
/************************************************************************/
//! Short description
/*!
\param Name
\param Value
\param ExpDate
\param ExpTime
\param TimeZone
\param Path
\param Domain
\param Secure
*/
xbShort xbHtml::SetCookie( const char * Name,const char * Value,
const char * ExpDate, const char * ExpTime, const char * TimeZone,
const char * Path, const char * Domain, xbShort Secure )
{
if(
( !Name || !Value ) ||
( ExpDate && !TimeZone)
)
xb_error(XB_INVALID_OPTION);
std::cout << "\nSet-Cookie: " << Name << "=" << Value << ";";
if( ExpDate )
{
std::cout << ExpDate << ";";
if( ExpTime )
std::cout << ExpTime;
else
std::cout << "00:00:00";
std::cout << TimeZone << ";";
}
if( Path )
std::cout << "\nPath=" << Path << ";";
if( Domain )
std::cout << "domain=" << Domain << ";";
if( Secure )
std::cout << "Secure";
std::cout << "\n";
return XB_NO_ERROR;
}
/************************************************************************/
//! Short description
/*!
\param String
*/
void xbHtml::SpaceToPlus( char * String )
{
char * p;
p = String;
while( *p )
{
if( *p == ' ' ) *p = '+';
p++;
}
/* eliminate trailing white space */
p--;
while( *p == '+' && p > String )
{
*p = 0x00;
p--;
}
}
/************************************************************************/
//! Short description
/*!
\param url
*/
void xbHtml::SendRedirect( char * url ) const
{
std::cout << "Location: " << url << std::endl << std::endl;
}
/************************************************************************/
//! Short description
/*!
\param String
*/
void xbHtml::PlusToSpace( char * String )
{
char * p;
p = String;
while( *p )
{
if( *p == '+' ) *p = ' ';
p++;
}
}
/************************************************************************/
//! Short description
/*!
\param CookieName
*/
const char * xbHtml::GetCookie( const char * CookieName )
{
char * CookieData;
char * TempPtr;
char * q;
xbShort len;
if(( CookieData = GetEnv( "HTTP_COOKIE" )) == NULL )
return NULL;
len = strlen( CookieName );
len += 2;
if(( TempPtr = (char *) malloc(len)) == NULL )
return NULL;
strcpy( TempPtr, CookieName );
strcat( TempPtr, "=" );
if(( q = strstr( CookieData, TempPtr )) == NULL )
{
free( TempPtr );
return NULL;
}
free( TempPtr );
len--;
q += len;
TempPtr = q;
len = 0;
while( *q && *q != ';' )
{
len++;
q++;
}
len++;
if( len > HtmlBufLen )
{
if( HtmlBufLen )
free( HtmlWorkBuf );
if(( HtmlWorkBuf = (char *) malloc( len )) == NULL )
return NULL;
}
memset( HtmlWorkBuf, 0x00, len );
q = HtmlWorkBuf;
while( *TempPtr && *TempPtr != ';' )
*q++ = *TempPtr++;
return HtmlWorkBuf;
}
/************************************************************************/
#endif /* XB_HTML */
|