📄 tmplout.c
字号:
}
ldap_msgfree( ldmp );
return( err );
}
int
ldap_vals2text(
LDAP *ld,
char *buf, /* NULL for "use internal" */
char **vals,
char *label,
int labelwidth, /* 0 means use default */
unsigned long syntaxid,
writeptype writeproc,
void *writeparm,
char *eol,
int rdncount
)
{
Debug( LDAP_DEBUG_TRACE, "ldap_vals2text\n", 0, 0, 0 );
return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
writeproc, writeparm, eol, rdncount, NULL ));
}
int
ldap_vals2html(
LDAP *ld,
char *buf, /* NULL for "use internal" */
char **vals,
char *label,
int labelwidth, /* 0 means use default */
unsigned long syntaxid,
writeptype writeproc,
void *writeparm,
char *eol,
int rdncount,
char *urlprefix
)
{
Debug( LDAP_DEBUG_TRACE, "ldap_vals2html\n", 0, 0, 0 );
if ( urlprefix == NULL ) {
urlprefix = DEF_LDAP_URL_PREFIX;
}
return( do_vals2text( ld, buf, vals, label, labelwidth, syntaxid,
writeproc, writeparm, eol, rdncount, urlprefix ));
}
static int
do_vals2text(
LDAP *ld,
char *buf, /* NULL for "use internal" */
char **vals,
char *label,
int labelwidth, /* 0 means use default */
unsigned long syntaxid,
writeptype writeproc,
void *writeparm,
char *eol,
int rdncount,
char *urlprefix
)
{
int i, html, writeoutval, freebuf, notascii;
char *p, *s, *outval;
if ( vals == NULL ) {
return( LDAP_SUCCESS );
}
html = ( urlprefix != NULL );
switch( LDAP_GET_SYN_TYPE( syntaxid )) {
case LDAP_SYN_TYPE_TEXT:
case LDAP_SYN_TYPE_BOOLEAN:
break; /* we only bother with these two types... */
default:
return( LDAP_SUCCESS );
}
if ( labelwidth == 0 || labelwidth < 0 ) {
labelwidth = DEF_LABEL_WIDTH;
}
if ( buf == NULL ) {
if (( buf = malloc( LDAP_DTMPL_BUFSIZ )) == NULL ) {
ld->ld_errno = LDAP_NO_MEMORY;
return( ld->ld_errno );
}
freebuf = 1;
} else {
freebuf = 0;
}
output_label( buf, label, labelwidth, writeproc, writeparm, eol, html );
for ( i = 0; vals[ i ] != NULL; ++i ) {
for ( p = vals[ i ]; *p != '\0'; ++p ) {
if ( !isascii( *p )) {
break;
}
}
notascii = ( *p != '\0' );
outval = notascii ? "(unable to display non-ASCII text value)"
: vals[ i ];
writeoutval = 0; /* if non-zero, write outval after switch */
switch( syntaxid ) {
case LDAP_SYN_CASEIGNORESTR:
++writeoutval;
break;
case LDAP_SYN_RFC822ADDR:
if ( html ) {
strcpy( buf, "<DD><A HREF=\"mailto:" );
strcat_escaped( buf, outval );
sprintf( buf + strlen( buf ), "\">%s</A><BR>%s", outval, eol );
(*writeproc)( writeparm, buf, strlen( buf ));
} else {
++writeoutval;
}
break;
case LDAP_SYN_DN: /* for now */
output_dn( buf, outval, labelwidth, rdncount, writeproc,
writeparm, eol, urlprefix );
break;
case LDAP_SYN_MULTILINESTR:
if ( i > 0 && !html ) {
output_label( buf, label, labelwidth, writeproc,
writeparm, eol, html );
}
p = s = outval;
while (( s = strchr( s, '$' )) != NULL ) {
*s++ = '\0';
while ( isspace( *s )) {
++s;
}
if ( html ) {
sprintf( buf, "<DD>%s<BR>%s", p, eol );
} else {
sprintf( buf, "%-*s%s%s", labelwidth, " ", p, eol );
}
(*writeproc)( writeparm, buf, strlen( buf ));
p = s;
}
outval = p;
++writeoutval;
break;
case LDAP_SYN_BOOLEAN:
outval = toupper( outval[ 0 ] ) == 'T' ? "TRUE" : "FALSE";
++writeoutval;
break;
case LDAP_SYN_TIME:
case LDAP_SYN_DATE:
outval = time2text( outval, syntaxid == LDAP_SYN_DATE );
++writeoutval;
break;
case LDAP_SYN_LABELEDURL:
if ( !notascii && ( p = strchr( outval, '$' )) != NULL ) {
*p++ = '\0';
while ( isspace( *p )) {
++p;
}
s = outval;
} else if ( !notascii && ( s = strchr( outval, ' ' )) != NULL ) {
*s++ = '\0';
while ( isspace( *s )) {
++s;
}
p = outval;
} else {
s = "URL";
p = outval;
}
/*
* at this point `s' points to the label & `p' to the URL
*/
if ( html ) {
sprintf( buf, "<DD><A HREF=\"%s\">%s</A><BR>%s", p, s, eol );
} else {
sprintf( buf, "%-*s%s%s%-*s%s%s", labelwidth, " ",
s, eol, labelwidth + 2, " ",p , eol );
}
(*writeproc)( writeparm, buf, strlen( buf ));
break;
default:
sprintf( buf, " Can't display item type %ld%s",
syntaxid, eol );
(*writeproc)( writeparm, buf, strlen( buf ));
}
if ( writeoutval ) {
if ( html ) {
sprintf( buf, "<DD>%s<BR>%s", outval, eol );
} else {
sprintf( buf, "%-*s%s%s", labelwidth, " ", outval, eol );
}
(*writeproc)( writeparm, buf, strlen( buf ));
}
}
if ( freebuf ) {
free( buf );
}
return( LDAP_SUCCESS );
}
static int
max_label_len( struct ldap_disptmpl *tmpl )
{
struct ldap_tmplitem *rowp, *colp;
int len, maxlen;
maxlen = 0;
for ( rowp = ldap_first_tmplrow( tmpl ); rowp != NULLTMPLITEM;
rowp = ldap_next_tmplrow( tmpl, rowp )) {
for ( colp = ldap_first_tmplcol( tmpl, rowp ); colp != NULLTMPLITEM;
colp = ldap_next_tmplcol( tmpl, rowp, colp )) {
if (( len = strlen( colp->ti_label )) > maxlen ) {
maxlen = len;
}
}
}
return( maxlen );
}
static int
output_label( char *buf, char *label, int width, writeptype writeproc,
void *writeparm, char *eol, int html )
{
char *p;
if ( html ) {
sprintf( buf, "<DT><B>%s</B>", label );
} else {
sprintf( buf, " %s:", label );
p = buf + strlen( buf );
while ( p - buf < width ) {
*p++ = ' ';
}
*p = '\0';
strcat( buf, eol );
}
return ((*writeproc)( writeparm, buf, strlen( buf )));
}
static int
output_dn( char *buf, char *dn, int width, int rdncount,
writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
{
char **dnrdns;
int i;
if (( dnrdns = ldap_explode_dn( dn, 1 )) == NULL ) {
return( -1 );
}
if ( urlprefix != NULL ) {
sprintf( buf, "<DD><A HREF=\"%s", urlprefix );
strcat_escaped( buf, dn );
strcat( buf, "\">" );
} else if ( width > 0 ) {
sprintf( buf, "%-*s", width, " " );
} else {
*buf = '\0';
}
for ( i = 0; dnrdns[ i ] != NULL && ( rdncount == 0 || i < rdncount );
++i ) {
if ( i > 0 ) {
strcat( buf, ", " );
}
strcat( buf, dnrdns[ i ] );
}
if ( urlprefix != NULL ) {
strcat( buf, "</A><BR>" );
}
ldap_value_free( dnrdns );
strcat( buf, eol );
return ((*writeproc)( writeparm, buf, strlen( buf )));
}
#define HREF_CHAR_ACCEPTABLE( c ) (( c >= '-' && c <= '9' ) || \
( c >= '@' && c <= 'Z' ) || \
( c == '_' ) || \
( c >= 'a' && c <= 'z' ))
static void
strcat_escaped( char *s1, char *s2 )
{
char *p, *q;
char *hexdig = "0123456789ABCDEF";
p = s1 + strlen( s1 );
for ( q = s2; *q != '\0'; ++q ) {
if ( HREF_CHAR_ACCEPTABLE( *q )) {
*p++ = *q;
} else {
*p++ = '%';
*p++ = hexdig[ *q >> 4 ];
*p++ = hexdig[ *q & 0x0F ];
}
}
*p = '\0';
}
#define GET2BYTENUM( p ) (( *p - '0' ) * 10 + ( *(p+1) - '0' ))
static char *
time2text( char *ldtimestr, int dateonly )
{
struct tm t;
char *p, *timestr, zone, *fmterr = "badly formatted time";
time_t gmttime;
memset( (char *)&t, 0, sizeof( struct tm ));
if ( (int) strlen( ldtimestr ) < 13 ) {
return( fmterr );
}
for ( p = ldtimestr; p - ldtimestr < 12; ++p ) {
if ( !isdigit( *p )) {
return( fmterr );
}
}
p = ldtimestr;
t.tm_year = GET2BYTENUM( p ); p += 2;
t.tm_mon = GET2BYTENUM( p ) - 1; p += 2;
t.tm_mday = GET2BYTENUM( p ); p += 2;
t.tm_hour = GET2BYTENUM( p ); p += 2;
t.tm_min = GET2BYTENUM( p ); p += 2;
t.tm_sec = GET2BYTENUM( p ); p += 2;
if (( zone = *p ) == 'Z' ) { /* GMT */
zone = '\0'; /* no need to indicate on screen, so we make it null */
}
gmttime = gtime( &t );
timestr = ctime( &gmttime );
timestr[ strlen( timestr ) - 1 ] = zone; /* replace trailing newline */
if ( dateonly ) {
strcpy( timestr + 11, timestr + 20 );
}
return( timestr );
}
/* gtime.c - inverse gmtime */
#if !defined( MACOS ) && !defined( _WIN32 ) && !defined( DOS )
#include <sys/time.h>
#endif /* !MACOS */
/* gtime(): the inverse of localtime().
This routine was supplied by Mike Accetta at CMU many years ago.
*/
static int dmsize[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
#define dysize(y) \
(((y) % 4) ? 365 : (((y) % 100) ? 366 : (((y) % 400) ? 365 : 366)))
#define YEAR(y) ((y) >= 100 ? (y) : (y) + 1900)
/* */
static long gtime ( struct tm *tm )
{
register int i,
sec,
mins,
hour,
mday,
mon,
year;
register long result;
if ((sec = tm -> tm_sec) < 0 || sec > 59
|| (mins = tm -> tm_min) < 0 || mins > 59
|| (hour = tm -> tm_hour) < 0 || hour > 24
|| (mday = tm -> tm_mday) < 1 || mday > 31
|| (mon = tm -> tm_mon + 1) < 1 || mon > 12)
return ((long) -1);
if (hour == 24) {
hour = 0;
mday++;
}
year = YEAR (tm -> tm_year);
result = 0L;
for (i = 1970; i < year; i++)
result += dysize (i);
if (dysize (year) == 366 && mon >= 3)
result++;
while (--mon)
result += dmsize[mon - 1];
result += mday - 1;
result = 24 * result + hour;
result = 60 * result + mins;
result = 60 * result + sec;
return result;
}
static int
searchaction( LDAP *ld, char *buf, char *base, LDAPMessage *entry, char *dn,
struct ldap_tmplitem *tip, int labelwidth, int rdncount,
writeptype writeproc, void *writeparm, char *eol, char *urlprefix )
{
int err, lderr, i, count, html;
char **vals, **members;
char *value, *filtpattern, *attr, *selectname;
char *retattrs[2], filter[ 256 ];
LDAPMessage *ldmp;
struct timeval timeout;
html = ( urlprefix != NULL );
for ( i = 0; tip->ti_args != NULL && tip->ti_args[ i ] != NULL; ++i ) {
;
}
if ( i < 3 ) {
return( LDAP_PARAM_ERROR );
}
attr = tip->ti_args[ 0 ];
filtpattern = tip->ti_args[ 1 ];
retattrs[ 0 ] = tip->ti_args[ 2 ];
retattrs[ 1 ] = NULL;
selectname = tip->ti_args[ 3 ];
vals = NULL;
if ( attr == NULL ) {
value = NULL;
} else if ( strcasecmp( attr, "-dnb" ) == 0 ) {
return( LDAP_PARAM_ERROR );
} else if ( strcasecmp( attr, "-dnt" ) == 0 ) {
value = dn;
} else if (( vals = ldap_get_values( ld, entry, attr )) != NULL ) {
value = vals[ 0 ];
} else {
value = NULL;
}
ldap_build_filter( filter, sizeof( filter ), filtpattern, NULL, NULL, NULL,
value, NULL );
if ( html ) {
/*
* if we are generating HTML, we add an HREF link that embodies this
* search action as an LDAP URL, instead of actually doing the search
* now.
*/
sprintf( buf, "<DT><A HREF=\"%s", urlprefix );
if ( base != NULL ) {
strcat_escaped( buf, base );
}
strcat( buf, "??sub?" );
strcat_escaped( buf, filter );
sprintf( buf + strlen( buf ), "\"><B>%s</B></A><DD><BR>%s",
tip->ti_label, eol );
if ((*writeproc)( writeparm, buf, strlen( buf )) < 0 ) {
return( LDAP_LOCAL_ERROR );
}
return( LDAP_SUCCESS );
}
timeout.tv_sec = SEARCH_TIMEOUT_SECS;
timeout.tv_usec = 0;
#ifdef CLDAP
if ( LDAP_IS_CLDAP( ld ))
lderr = cldap_search_s( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
0, &ldmp, NULL );
else
#endif /* CLDAP */
lderr = ldap_search_st( ld, base, LDAP_SCOPE_SUBTREE, filter, retattrs,
0, &timeout, &ldmp );
if ( lderr == LDAP_SUCCESS || NONFATAL_LDAP_ERR( lderr )) {
if (( count = ldap_count_entries( ld, ldmp )) > 0 ) {
if (( members = (char **)malloc( (count + 1) * sizeof(char *)))
== NULL ) {
err = LDAP_NO_MEMORY;
} else {
for ( i = 0, entry = ldap_first_entry( ld, ldmp );
entry != NULL;
entry = ldap_next_entry( ld, entry ), ++i ) {
members[ i ] = ldap_get_dn( ld, entry );
}
members[ i ] = NULL;
ldap_sort_values( ld, members, ldap_sort_strcasecmp );
err = do_vals2text( ld, NULL, members, tip->ti_label,
html ? -1 : 0, LDAP_SYN_DN, writeproc, writeparm,
eol, rdncount, urlprefix );
ldap_value_free( members );
}
}
ldap_msgfree( ldmp );
}
if ( vals != NULL ) {
ldap_value_free( vals );
}
return(( err == LDAP_SUCCESS ) ? lderr : err );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -