#include "EnvObject.h"
#include "Configuration.h"
#include "Security.h"
#include "DBGate.h"
#include "Result.h"
#include "EvalParser.h"
#include "string_util.h"
#include "time_util.h"
#ifndef NO_CRYPT
extern "C" {
extern char *crypt(const char *, const char *);
}
#endif
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifndef EVALTEST
extern EnvObject * userdata;
extern Configuration * config;
extern Security * security;
extern DBGate * dbgate;
#endif
// set up output for fcgi processes
#ifdef USE_FCGI
#include "fcgi-cout-redirect.h"
extern fcgicout fcgi_cout;
#endif
/**************************************************
* Date and Time Functions
*
* The standard date format will be mm-dd-yyyy
* and all date/time functions will start with "dt"
****************************************************/
/*
@usehtml
@usage $return = dtTime([$date, [$time])
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $time Time (in HHMM, HH:MM, or HH:MM AM|PM|am|pm)
@arg $return Integer
@index Returns the number of seconds since Jan 1 1970 for the current or given date and time
Returns the number of seconds since Jan 1 1970 for the current or
given date and time.
<P>
It can take an optional date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy,
mm/dd/yyyy or yyyyddmm) format, in which it returns the seconds for
12:01 AM of that date.
<P>
If the date is provided, the time can be given in a second argument
(in "HHMM", "HH:MM" 24 hour formats or "HH:MM AM/PM/am/pm").
<P>
Two digit years are converted to four digit years using a sliding window
of fifty years based on today's date.
@seealso _dtStrFromTime_ _dtEpoc_ _dtLegalDate_
*/
void EVLF_dtTime(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
BETWEEN(dtTime,0,2);
char temp[20];
if (argc >= 1){ // Optional date
tm * ptm;
if (argc >= 2){ // Optional time
ptm = MakeTMFromDateTime(argv[0],argv[1]);
if (ptm == NULL){
err_obj.addFatalError("dtTime(): Invalid date or time string");
return;
}
} else {
ptm = MakeTMFromDate(argv[0]);
if (ptm == NULL){
err_obj.addFatalError("dtTime(): Invalid date string");
return;
}
}
time_t t = mktime(ptm);
sprintf (temp, "%d", (int) t);
ret_val = temp;
} else {
time_t t = time (NULL);
sprintf (temp, "%d", (int) t);
ret_val = temp;
}
}
/*
@usehtml
@usage $return = dtLegalDate($date)
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $return Boolean
@index Returns true if the specified date is valid
Returns true ("1") if the specified date is legal (a valid date of the
format mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm).
<P>
Two digit years are converted to four digit years using a sliding window
of fifty years based on today's date.
*/
void EVLF_dtLegalDate(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(dtLegalDate,1);
ret_val = "1";
tm * ptm;
ptm = MakeTMFromDate(argv[0]);
if (ptm == NULL){
ret_val = "0";
}
}
/*
@usehtml
@usage $return = dtEpoc([$date])
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $return Integer
@index Returns the number of days since Jan 1 1970 for the current or given date
Returns the number of days since Jan 1 1970 for the current or given date.
<P>
It can take an optional date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy,
mm/dd/yyyy or yyyyddmm) format, in which it returns the days since
Jan 1 1970 for that date.
<P>
Two digit years are converted to four digit years using a sliding window
of fifty years based on today's date.
@seealso _dtStrFromEpoc_ _dtTime_ _dtLegalDate_
*/
void EVLF_dtEpoc(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char temp[20];
time_t t;
tm * ptm;
int adjust;
ANYARGS;
if (argc == 0) {
t = time (NULL);
// now find the time zone adjustment...there has got
// to be a better way!!!
adjust = atoi ( ( (*config )["timezone"]).c_str() );
adjust *= (60 * 60);
int i = (t + adjust) / (60 * 60 * 24) + 1;
// add 1 to maintain DAT compatibility
sprintf (temp, "%d", i);
} else if (argc == 1) {
// they gave an arg, so get a tm_struct for that date
// then get it's epoc
ptm = MakeTMFromDate (argv[0]);
if (ptm == NULL){
err_obj.addFatalError("dtEpoc(): Invalid date string");
return;
}
t = mktime (ptm);
int i = ( (t+100) / (60 * 60 * 24) ) + 1;
// add 1 to maintain DAT compatibility
sprintf (temp, "%d", i);
} else {
temp[0] = '\0';
}
ret_val = temp;
}
/*
@usehtml
@usage $return = dtToday()
@arg $return Date (mm-dd-yyyy)
@index Returns today's date in mm-dd-yyyy format
Returns today's date in mm-dd-yyyy format
*/
void EVLF_dtToday(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char temp[15];
time_t t = time (NULL);
tm * l = localtime(&t);
ANYARGS;
sprintf (temp, "%02d-%02d-%4d", (l->tm_mon +1), l->tm_mday, (l->tm_year + 1900) );
ret_val = temp;
}
/*
@usehtml
@usage $return = dtStrFromNow($format)
@arg $format Date format string
@arg $return String of current date and time according to the format string
@index Returns today's date and time according to the format string
Returns today's date and time according to the format string.
Any characters that are not format commands are passed directly into
the string.
<P>
The following are the possible format commands:
<P>
<table border=1>
<tr><th colspan=2>Date Format Commands</th></tr>
<tr><th>Format</th><th>Result</th></tr>
<tr><td>%a</td><td>Abbreviated weekday name</td></tr>
<tr><td>%A</td><td>Full weekday name</td></tr>
<tr><td>%b</td><td>Abbreviated month name</td></tr>
<tr><td>%B</td><td>Full month name</td></tr>
<tr><td>%c</td><td>Full date and time representation</td></tr>
<tr><td>%C</td><td>Full date and time representation produced by the UNIX date(1) command</td></tr>
<tr><td>%d</td><td>Day of month ["01","02",...,"31"]</td></tr>
<tr><td>%D</td><td>Date as %m/%d/%y</td></tr>
<tr><td>%e</td><td>Day of month [" 1"," 2",...,"31"]</td></tr>
<tr><td>%g</td><td>Week-based year within century [00,99]</td></tr>
<tr><td>%G</td><td>Week-based year with centry [0000,9999]</td></tr>
<tr><td>%h</td><td>Abbreviated month name</td></tr>
<tr><td>%H</td><td>Hour ["00","01",...,"23"]</td></tr>
<tr><td>%I</td><td>Hour ["01","02",...,"12"]</td></tr>
<tr><td>%J</td><td>Day number of the year ["001",...,"366"]</td></tr>
<tr><td>%k</td><td>Hour [" 0"," 1",...,"23"]</td></tr>
<tr><td>%l</td><td>Hour [" 1"," 2",...,"12"]</td></tr>
<tr><td>%m</td><td>Month ["01","02",...,"12"]</td></tr>
<tr><td>%n</td><td>Newline</td></tr>
<tr><td>%p</td><td>a.m. or p.m.</td></tr>
<tr><td>%r</td><td>12-hour time format with %p</td></tr>
<tr><td>%R</td><td>Time with %H:%M</td></tr>
<tr><td>%S</td><td>Seconds [00,...,61] (2 extra seconds to accommodate leap seconds)</td></tr>
<tr><td>%t</td><td>Insert a TAB</td></tr>
<tr><td>%T</td><td>Time with %H:%M:%S</td></tr>
<tr><td>%V</td><td>ISO 8601 week number [01,...,53]. Week 01 begins with Monday on the week that includes both January 4th and the first Thursday of the year.</td></tr>
<tr><td>%w</td><td>Weekday [0,...,6] (0 is Sunday)</td></tr>
<tr><td>%W</td><td>Week number [00,...,53] (Sunday is the first day of week 1)</td></tr>
<tr><td>%x</td><td>Full date</td></tr>
<tr><td>%X</td><td>Full time</td></tr>
<tr><td>%y</td><td>Year within century [00,99]</td></tr>
<tr><td>%Y</td><td>Year including centry (i.e. 1999)</td></tr>
<tr><td>%Z</td><td>Time zone abbreviation</td></tr>
</table>
@ex print(dtStrFromNow("%m/%d/%y %I:%M %p"))
@ex
@ex // printed "12/10/99 09:10 PM" when this was written
@seealso _dtStrFromDate_ _dtFormatTime_ _dtStrFromTime_ _dtStrFromEpoc_ _dtFormatMonth_
*/
void EVLF_dtStrFromNow(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char temp[512];
time_t t = time (NULL);
tm * l = localtime(&t);
EXACTLY(dtStrFromNow,1);
strftime (temp, 511, argv[0].c_str(), l);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtStrFromDate($format, $date, [$time])
@arg $format Date format string
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $time Time (in HHMM, HH:MM, or HH:MM AM|PM|am|pm)
@arg $return String of given date (and time) according to the format string
@index Returns the specified date (and time if supplied) according to the format string
Returns the specified date (and time if supplied) according to the format
string. Any characters that are not format commands are passed
directly into the string.
<P>
Please see _dtStrFromNow_ for the possible format commands.
<P>
Two digit years are converted to four digit years using a sliding window
of fifty years based on today's date.
@ex print(dtStrFromDate("%m/%d/%Y %I:%M %p","2/12/03","21:03"))
@ex
@ex // prints "02/12/2003 09:03 PM"
@seealso _dtStrFromNow_ _dtFormatTime_ _dtStrFromTime_ _dtStrFromEpoc_
*/
void EVLF_dtStrFromDate(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
BETWEEN(dtStrFromDate,2,3);
char temp[512];
tm * l;
if (argc == 2){
l = MakeTMFromDate (argv[1].c_str());
if (l == NULL){
err_obj.addFatalError("dtStrFromDate(): Invalid date");
return;
}
} else { // Both date and time specified
l = MakeTMFromDateTime(argv[1].c_str(),argv[2].c_str());
if (l == NULL){
err_obj.addFatalError("dtStrFromDate(): Invalid date or time string");
return;
}
}
// we've got to convert to time_t and back to fill in
// the gaps in the tm structure
time_t t = mktime(l);
l = localtime (&t);
strftime (temp, 511, argv[0].c_str(), l);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtFormatTime($format, $time)
@arg $format Date format string
@arg $time Time (in HHMM, HH:MM, or HH:MM AM|PM|am|pm)
@arg $return String of the given time according to the format string
@index Returns the given time according to the format string
Returns the given time according to the format string. Any characters
that are not format commands are passed directly into the string. For
the string formatting commands, the date is assumed to be 1/1/1970.
<P>
Please see _dtStrFromNow_ for the possible format commands.
@ex print(dtFormatTime("%I:%M %p","21:03"))
@ex
@ex // prints "09:03 PM"
@seealso _dtStrFromNow_ _dtStrFromDate_ _dtStrFromTime_ _dtStrFromEpoc_
*/
void EVLF_dtFormatTime(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(dtFromatTime,2);
char temp[512];
tm * l;
l = MakeTMFromDateTime("1/1/1970",argv[1].c_str());
if (l == NULL){
err_obj.addFatalError("dtFormatTime(): Invalid time string");
return;
}
// we've got to convert to time_t and back to fill in
// the gaps in the tm structure
time_t t = mktime(l);
l = localtime (&t);
strftime (temp, 511, argv[0].c_str(), l);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtStrFromTime($format, $seconds)
@arg $format Date format string
@arg $seconds Number of seconds of the date and time since Jan 1, 1970
@arg $return String of given date (seconds since Jan 1, 1970) according to the format string
@index Returns specified date and time (seconds since Jan 1, 1970) according to the format string
Returns specified date and time (seconds since Jan 1, 1970) according
to the format string. Any characters that are not format commands are
passed directly into the string.
<P>
Please see _dtStrFromNow_ for the possible format commands.
@ex print(dtStrFromTime("%m/%d/%Y %I:%M %p",944879848))
@ex
@ex // prints "12/10/1999 09:37 PM"
@seealso _dtTime_ _dtStrFromNow_ _dtStrFromDate_ _dtFormatTime_ _dtStrFromEpoc_ */
void EVLF_dtStrFromTime(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
char temp[512];
time_t t = (time_t) (atoi(argv[1].c_str()));
tm * l = localtime (&t);
EXACTLY(dtStrFromTime,2);
strftime (temp, 511, argv[0].c_str(), l);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtStrFromEpoc($format, $epoc_day)
@arg $format Date format string
@arg $epoc_day Number of days of the date since Jan 1, 1970
@arg $return String of given date (days since Jan 1, 1970) according to the format string
@index Returns specified date (days since Jan 1, 1970) according to the format string
Returns specified date (days since Jan 1, 1970) according to the format
string. Any characters that are not format commands are passed
directly into the string.
<P>
Please see _dtStrFromNow_ for the possible format commands.
@ex print(dtStrFromEpoc("%m/%d/%Y",11000))
@ex
@ex // prints "02/12/2000"
@seealso _dtEpoc_ _dtStrFromNow_ _dtStrFromDate_ _dtFormatTime_ _dtStrFromTime_
Like dtStrFromNow, but it takes a second arg which is the
days since Jan 1 1970 */
void EVLF_dtStrFromEpoc(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
char temp[512];
int seconds = (atoi(argv[1].c_str())*60*60*24);
time_t t = (time_t) (seconds);
tm * l = localtime (&t);
EXACTLY(dtStrFromEpoc,2);
strftime (temp, 511, argv[0].c_str(), l);
ret_val = temp;
}
/*
@nodoc
given a date, returns the date of the following Sunday in mm-dd-yyyy format
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a date in mm-dd-yyyy format
@index given a date, returns the date of the following Sunday
@arg1 a date
*/
void EVLF_dtNextSunday(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
char temp[512];
tm * d ;
time_t t;
tm * tm_ptr;
EXACTLY(NextSunday,1);
d = MakeTMFromDate (argv[0]);
if (d == NULL){
err_obj.addFatalError("dtNextSunday(): Invalid date string");
return;
}
t = mktime (d);
t += ( 7 * 60 * 60 * 24);
tm_ptr = localtime (&t);
int weekday = tm_ptr->tm_wday;
while (weekday != 0)
{
tm * d = tm_ptr;
time_t t;
tm * tm_ptr;
t = mktime (d);
t -= ( 60 * 60 * 24);
tm_ptr = localtime (&t);
weekday = tm_ptr->tm_wday;
}
sprintf (temp, "%02d-%02d-%02d", tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_year + 1900);
ret_val = temp;
}
/*
@nodoc
given a date, returns the date of the following Saturday in mm-dd-yyyy format
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a date in mm-dd-yyyy format
@index given a date, returns the date of the following Saturday
@arg1 a date
*/
void EVLF_dtNextSaturday(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
char temp[512];
tm * d;
time_t t;
tm * tm_ptr;
EXACTLY(NextSaturday,1);
d = MakeTMFromDate (argv[0]);
if (d == NULL){
err_obj.addFatalError("dtNextSaturday(): Invalid date string");
return;
}
t = mktime (d);
t += ( 7 * 60 * 60 * 24);
tm_ptr = localtime (&t);
int weekday = tm_ptr->tm_wday;
while (weekday != 6)
{
// tm * dd = tm_ptr;
// time_t t;
// tm * tm_ptr;
// t = mktime (dd);
t -= ( 60 * 60 * 24);
tm_ptr = localtime (&t);
weekday = tm_ptr->tm_wday;
}
sprintf (temp, "%02d-%02d-%02d", tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_year + 1900);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtNextDayOfWeek($date,$day_of_week)
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $day_of_week the day of the week [0,...,6] (0 is Sunday)
@arg $return Date of next given day of the week after the given date
@index Returns the date of the next given day of the week after the specified date
Given a date and a day of the week [0,...,6] (0 is Sunday), returns
the date of the next day matching the day of the week after the given
date.
@ex print(dtNextDayOfWeek("12/10/99",0));
@ex
@ex // prints "12-12-1999" (the Sunday after 12/10/99) */
void EVLF_dtNextDayOfWeek(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
char temp[512];
tm * d ;
time_t t;
tm * tm_ptr;
EXACTLY(dtNextDayOfWeek,2);
d = MakeTMFromDate (argv[0]);
if (d == NULL){
err_obj.addFatalError("dtNextDayOfWeek(): Invalid date string");
return;
}
t = mktime (d);
t += ( 7 * 60 * 60 * 24);
tm_ptr = localtime (&t);
int weekday = tm_ptr->tm_wday;
while (weekday != (atoi (argv[1].c_str()) % 7))
{
tm * d = tm_ptr;
time_t t;
tm * tm_ptr;
t = mktime (d);
t -= ( 60 * 60 * 24);
tm_ptr = localtime (&t);
weekday = tm_ptr->tm_wday;
}
sprintf (temp, "%02d-%02d-%02d", tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_year + 1900);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtAddDays($date,$num_days)
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $num_days Integer (positive or negative) representing the number of days
@arg $return Date
@index Returns a date a given number of days from a specified date
Given a date and a number of days (positive or negative), returns
a date separated from the specified date by the number of days.
@ex print(dtAddDays("1/2/2000",-3))
@ex
@ex // prints "12-30-1999"
*/
void EVLF_dtAddDays(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char temp[512];
tm * d;
time_t t;
tm * tm_ptr;
EXACTLY(AddDays,2);
d = MakeTMFromDate (argv[0]);
if (d == NULL){
err_obj.addFatalError("dtAddDays(): Invalid date string");
return;
}
t = mktime (d);
// now add the right number of seconds to t
t += ( atoi ((argv[1]).c_str()) * 60 * 60 * 24);
tm_ptr = localtime (&t);
sprintf (temp, "%02d-%02d-%02d", tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_year + 1900);
ret_val = temp;
}
/*
@usehtml
@usage $return = dtFormatMonth($format,$month)
@arg $format Month format string
@arg $month Month as either a number, a month name, or a month abbreviation
@index Converts between different month formats
Converts between different month formats. It uses the format
commands in _dtStrFromNow_ to determine the output format. This includes
"%b" for the abbreviated month name, "%B" for the full month name,
and "%m" for the month as a number ["01",...,"12"].
@ex print(dtFormatMonth("%b %B %m","March"))
@ex
@ex // prints "Mar March 03"
@seealso _dtStrFromNow_
*/
void EVLF_dtFormatMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtFormatMonth,2);
int month = atoi(argv[1].c_str());
string mstr = upcase(argv[1]);
tm * d;
char temp[512];
string date_str = "";
mstr = mstr.substr(0,3);
if (mstr == "JAN") {month = 1;}
else if (mstr == "FEB") {month = 2;}
else if (mstr == "MAR") {month = 3;}
else if (mstr == "APR") {month = 4;}
else if (mstr == "MAY") {month = 5;}
else if (mstr == "JUN") {month = 6;}
else if (mstr == "JUL") {month = 7;}
else if (mstr == "AUG") {month = 8;}
else if (mstr == "SEP") {month = 9;}
else if (mstr == "OCT") {month = 10;}
else if (mstr == "NOV") {month = 11;}
else if (mstr == "DEC") {month = 12;}
if ((month < 1) || (month > 12)){
err_obj.addFatalError("dtFormatMonth(): Invalid month");
return;
}
// Make a date for day of week conversion
sprintf(temp,"%d/1/1970",month);
date_str = temp;
d = MakeTMFromDate(date_str);
// we've got to convert to time_t and back to fill in
// the gaps in the tm structure
time_t t = mktime(d);
d = localtime (&t);
strftime(temp,511,argv[0].c_str(), d);
ret_val = temp;
}
/*
@nodoc
given a month number, returns the month abbreviation
@funcname dtn2s_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a month abbreviation, ie Jan, Feb, Mar...
@index given a month number, returns the abbreviation
@arg1 a number between 1 and 12
@ex dtn2s_Month('8') will return Aug
*/
void EVLF_dtNumberToShortMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtN2S_Month,1);
int month = atoi(argv[0].c_str());
ret_val = argv[0];
if (month == 1)
ret_val="Jan";
else if (month == 2)
ret_val="Feb";
else if (month == 3)
ret_val="Mar";
else if (month == 4)
ret_val="Apr";
else if (month == 5)
ret_val="May";
else if (month == 6)
ret_val="Jun";
else if (month == 7)
ret_val="Jul";
else if (month == 8)
ret_val="Aug";
else if (month == 9)
ret_val="Sep";
else if (month == 10)
ret_val="Oct";
else if (month == 11)
ret_val="Nov";
else if (month == 12)
ret_val="Dec";
}
/*
@nodoc
given a month number, returns the month name
@funcname dtN2L_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a month name, ie January, February...
@index given a month number, returns the name
@arg1 a number between 1 and 12
@ex dtN2L_Month('8') will return August
*/
void EVLF_dtNumberToLongMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtN2L_Month,1);
int month = atoi(argv[0].c_str());
ret_val = argv[0];
if (month == 1)
ret_val="January";
else if (month == 2)
ret_val="February";
else if (month == 3)
ret_val="March";
else if (month == 4)
ret_val="April";
else if (month == 5)
ret_val="May";
else if (month == 6)
ret_val="June";
else if (month == 7)
ret_val="July";
else if (month == 8)
ret_val="August";
else if (month == 9)
ret_val="September";
else if (month == 10)
ret_val="October";
else if (month == 11)
ret_val="November";
else if (month == 12)
ret_val="December";
}
/*
@nodoc
given a month abbreviation, returns the month name
@funcname dtS2L_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a month name, ie January, February...
@index given a month abbreviation, returns the month name
@arg1 a month abbreviation, et Jan, Feb...
@ex dtS2L_Month('Aug') will return August
*/
void EVLF_dtShortToLongMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtS2L_Month,1);
if (argv[0] == "Jan")
ret_val="January";
else if (argv[0] == "Feb")
ret_val="February";
else if (argv[0] == "Mar")
ret_val="March";
else if (argv[0] == "Apr")
ret_val="April";
else if (argv[0] == "May")
ret_val="May";
else if (argv[0] == "Jun")
ret_val="June";
else if (argv[0] == "Jul")
ret_val="July";
else if (argv[0] == "Aug")
ret_val="August";
else if (argv[0] == "Sep")
ret_val="September";
else if (argv[0] == "Oct")
ret_val="October";
else if (argv[0] == "Nov")
ret_val="November";
else if (argv[0] == "Dec")
ret_val="December";
}
/*
@nodoc
given a month abbreviation, returns the month number
@funcname dtS2N_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a number between 1 and 12
@index given a month abbreviation, returns the month number
@arg1 a month abbreviation, ie Jan, Feb, ...
@ex dtS2N_Month('Aug') will return 8
*/
void EVLF_dtShortToNumberMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtS2N_Month,1);
if (argv[0] == "Jan")
ret_val="1";
else if (argv[0] == "Feb")
ret_val="2";
else if (argv[0] == "Mar")
ret_val="3";
else if (argv[0] == "Apr")
ret_val="4";
else if (argv[0] == "May")
ret_val="5";
else if (argv[0] == "Jun")
ret_val="6";
else if (argv[0] == "Jul")
ret_val="7";
else if (argv[0] == "Aug")
ret_val="8";
else if (argv[0] == "Sep")
ret_val="9";
else if (argv[0] == "Oct")
ret_val="10";
else if (argv[0] == "Nov")
ret_val="11";
else if (argv[0] == "Dec")
ret_val="12";
}
/*
@nodoc
given a month abbreviation, returns the month number with a leading 0
@funcname dtS2N2_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a number between 01 and 12
@index given a month abbreviation, returns the month number with a leading 0
@arg1 a month abbreviation, ie Jan, Feb, ...
@ex dtS2N2_Month('Aug') will return 08
*/
void EVLF_dtShortToNumberMonth2(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtS2N2_Month2,1);
if (argv[0] == "Jan")
ret_val="01";
else if (argv[0] == "Feb")
ret_val="02";
else if (argv[0] == "Mar")
ret_val="03";
else if (argv[0] == "Apr")
ret_val="04";
else if (argv[0] == "May")
ret_val="05";
else if (argv[0] == "Jun")
ret_val="06";
else if (argv[0] == "Jul")
ret_val="07";
else if (argv[0] == "Aug")
ret_val="08";
else if (argv[0] == "Sep")
ret_val="09";
else if (argv[0] == "Oct")
ret_val="10";
else if (argv[0] == "Nov")
ret_val="11";
else if (argv[0] == "Dec")
ret_val="12";
}
/*
@nodoc
given a month name, returns the month abbreviation
@funcname dtL2S_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a month abbreviation, ie Jan, Feb, Mar...
@index given a month name, returns the month abbreviation
@arg1 a month name, ie January, February...
@ex dtL2S_Month('August') will return Aug
*/
void EVLF_dtLongToShortMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtL2S_Month,1);
if (argv[0] == "January")
ret_val="Jan";
else if (argv[0] == "February")
ret_val="Feb";
else if (argv[0] == "March")
ret_val="Mar";
else if (argv[0] == "April")
ret_val="Apr";
else if (argv[0] == "May")
ret_val="May";
else if (argv[0] == "June")
ret_val="Jun";
else if (argv[0] == "July")
ret_val="Jul";
else if (argv[0] == "August")
ret_val="Aug";
else if (argv[0] == "September")
ret_val="Sep";
else if (argv[0] == "October")
ret_val="Oct";
else if (argv[0] == "November")
ret_val="Nov";
else if (argv[0] == "December")
ret_val="Dec";
}
/*
@nodoc
given a month name, returns the month number
@funcname dtL2N_Month
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@return a number between 1 and 12
@index given a month name, returns the month number
@arg1 a month name, ie January, February...
@ex dtL2N_Month('August') will return 8
*/
void EVLF_dtLongToNumberMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtL2N_Month,1);
if (argv[0] == "January")
ret_val="1";
else if (argv[0] == "February")
ret_val="2";
else if (argv[0] == "March")
ret_val="3";
else if (argv[0] == "April")
ret_val="4";
else if (argv[0] == "May")
ret_val="5";
else if (argv[0] == "June")
ret_val="6";
else if (argv[0] == "July")
ret_val="7";
else if (argv[0] == "August")
ret_val="8";
else if (argv[0] == "September")
ret_val="9";
else if (argv[0] == "October")
ret_val="10";
else if (argv[0] == "November")
ret_val="11";
else if (argv[0] == "December")
ret_val="12";
}
/*
@usehtml
@usage $return = dtFullWeekday($weekday_num)
@arg $weekday_num Weekday number [1,...,7] (1 is Sunday)
@arg $return Full name of the given weekday
@index Returns the full weekday name for a given weekday number
Returns the full weekday name for a given weekday number. The week number is an integer [1,...,7] with 1 as Sunday.
@ex print(dtFullWeekday(3));
@ex
@ex // prints "Tuesday"
*/
void EVLF_dtFullWeekday(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
EXACTLY(dtFullWeekday,1);
int day = atoi(argv[0].c_str());
if (day == 1)
ret_val="Sunday";
else if (day == 2)
ret_val="Monday";
else if (day == 3)
ret_val="Tuesday";
else if (day == 4)
ret_val="Wednesday";
else if (day == 5)
ret_val="Thursday";
else if (day == 6)
ret_val="Friday";
else if (day == 7)
ret_val="Saturday";
else {
err_obj.addFatalError("dtFullWeekday(): Invalid weekday number");
}
}
/*
@nodoc
given a month, day, and year, returns a date in mm-dd-yyyy format
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@index given a month, day, and year returns a date
@arg1 a month number, 1-12, should also be able to take a name, but I haven't gotten around to that
@arg2 a day of a month, 1-31
@arg3 a year, yy or yyyy format
@return a date in mm-dd-yyyy format
*/
void EVLF_dtMakeDate(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
string month, day;
int year;
char date[255];
EXACTLY(dtMakeDate,3);
if (strlen(argv[0].c_str())==1)
month = "0"+argv[0];
else if (strlen(argv[0].c_str())==2)
month = argv[0];
else if (strlen(argv[0].c_str())==3)
{
}
if (strlen(argv[1].c_str())==1)
day = "0"+argv[1];
else if (strlen(argv[1].c_str())==2)
day = argv[1];
year = atoi(argv[2].c_str());
// Get the current year
time_t t = time(NULL);
tm * l = localtime(&t);
int current_year = 1900 + l->tm_year;
if (year < 100){ // Convert to 4 digit year
year = year + ((current_year / 100) * 100);
if ((current_year - year) > 50){
year = year + 100;
} else if ((year - current_year) > 50){
year = year - 100;
}
}
sprintf (date, "%s-%s-%d",month.c_str(),day.c_str(),year);
ret_val=date;
}
/*
@usehtml
@usage $return = dtDaysInMonth($date)
@arg $date Date (in mm-dd-yy, mm/dd/yy, mm-dd-yyyy, mm/dd/yyyy, or yyyyddmm)
@arg $return Number of days in the month of the given date
@index Returns the number of days in the month of a specified date
Returns the number of days in the month of a specified date. The day
of the month in the date is ignored.
@ex print(dtDaysInMonth('8-23-2002'))
@ex
@ex // prints "31" */
void EVLF_dtDaysInMonth(string & ret_val, string argv[], string argn[], int argc, Error & err_obj)
{
char temp[15];
tm * l;
EXACTLY(dtDaysInMonth,1);
l = MakeTMFromDate (argv[0]);
if (l == NULL){
err_obj.addFatalError("dtDaysInMonth(): Invalid date string");
return;
}
int year = l->tm_year + 1900;
int month = l->tm_mon +1;
if (month == 1 || month == 3 || month == 5 || month ==7 || month == 8 || month == 10 || month == 12)
ret_val = "31";
else if (month == 4 || month == 6 || month ==9 || month == 11)
ret_val = "30";
else if (((year % 4) == 0) && ( (year % 100) || ((year % 400)==0) ))
ret_val = "29";
else
ret_val = "28";
}
/***** Non-Date Functions *****/
/*
@usehtml
@usage $return = regExpFind($string,$regexp)
@arg $string String in which to search
@arg $regexp Regular expression for which to search
@arg $return Boolean - True if the regular expression matches a string
@index Returns true if a specified regular expression matches a given string
Returns true if a specified regular expression matches a given string.
Searches are done across multiple lines. Matches are "greedy" in that
every section of the regular expression will match as large a string
as possible.
<P>
The following tables lists special regular expression characters. In order
to use the respective real characters in the search string, they must be
escaped with the character "\". (Due to the escape processing of the Eval
language, the "\" character itself may need to be escaped.)
<P>
<table border=1>
<tr><th colspan=2>Regular Expression Characters</th></tr>
<tr><th>Characters</th><th>Purpose</th></tr>
<tr><td align=center>(...)</td><td>Groups contents together</td></tr>
<tr><td align=center>[...]</td><td>Matches a single character in a
list of possible characters (i.e. [abc] will match a, b, or
c)</td></tr>
<tr><td align=center>[^...] </td><td>Matches a all characters not
in the list of possible characters. (i.e. [^abc] will match all characters not a, b, or c.)</td></tr>
<tr><td align=center> - </td><td>Enables a range of characters (i.e. [0-9] represents all digits</td></tr>
<tr><td align=center>.</td><td>Matches all characters</td></tr>
<tr><td align=center>|</td><td>Logical OR. (i.e. (dog|cat) matches "dog" or "cat)</td></tr>
<tr><td align=center>?</td><td>Zero or one. (i.e. b(a?)t will match "bat" or "bt")</td></tr>
<tr><td align=center>+</td><td>One or more. (i.e. [0-9]+ matches one or more numerical digits)</td></tr>
<tr><td align=center>*</td><td>Zero or more. (i.e. [0-9]* matches zero or more numerical digits)</td></tr>
<tr><td align=center>^</td><td>Matches the beginning of the string</td></tr>
<tr><td align=center>$</td><td>Matches the end of the string</td></tr>
</table>
@ex $integer_regexp = "[0-9]+";
@ex print(regExpFind("Spot jumped 23 times.",$integer_regexp));
@ex // prints "1"
@ex print(regExpFind("Spot didn't jump.",$integer_regexp));
@ex // prints "0"
@seealso _regExpReplace_ _regExpGrab_ */
void EVLF_regExpFind(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(regExpFind,2);
RegExp re(argv[1].c_str());
char buf[10];
sprintf(buf,"%d",re.Match(argv[0].c_str()));
ret_val = buf;
}
/*
@usehtml
@usage $return = regExpReplace($string,$regexp,$replace_str,[$global_bool])
@arg $string String in which to search and replace
@arg $regexp Regular expression for which to search
@arg $replace_str String with which to replace matched regions
@arg $global_bool If true ("1"), the search is done globally. If false ("0"), the search is done only once. (Defaults to true)
@arg $return Replaced string
@index Executes regular expression search and replace on a string
Executes regular expression search and replace on a string. Please see
_regExpFind_ for the list of regular expression command strings.
<P>
In the replace string, the command strings "\1", "\2", etc. can be
used in the first, second, etc. matched parenthetical items in the
regular expression. The string "\0" represents the complete match.
(Due to the escape processing of the Eval language, the "\" character
itself may need to be escaped.)
<P>
@ex print(regExpReplace("butch is nice, al is nice","is","is not",0));
@ex // prints "butch is not nice, al is nice"
@ex
@ex print(regExpReplace('butch@tcg-inc.com',
@ex '([^@]*)@([^\\.]*).*',
@ex '\\0 -> user \\1 from \\2'));
@ex // prints "butch@tcg-inc.com -> user butch from tcg-inc"
@seealso _regExpFind_ _regExpGrab_
*/
void EVLF_regExpReplace(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
BETWEEN(regExpReplace,3,4);
int global_replace = 1;
int match_start;
char *newreplstr;
string str = argv[0];
RegExp re(argv[1].c_str());
unsigned int str_pointer=0;
string replstr = argv[2];
if(argc > 3){
global_replace = atoi(argv[3].c_str());
}
ret_val = "";
do
{
match_start = re.RegFind(str.c_str() + str_pointer);
if (match_start >= 0)
{
ret_val += str.substr(str_pointer,match_start);
newreplstr = re.GetReplaceString(replstr.c_str());
ret_val += newreplstr;
free(newreplstr);
str_pointer += match_start + re.GetFindLen();
}
} while((match_start >= 0) && (global_replace) &&
(str_pointer < argv[0].length()));
if (str_pointer < argv[0].length()){
ret_val += str.substr(str_pointer);
}
}
/*
@usehtml
@usage $return = regExpGrab($string,$regexp,$return_pattern)
@arg $string String in which to search and replace
@arg $regexp Regular expression for which to search
@arg $return_pattern Pattern used to format return string
@arg $return Matched pattern string
@index Executes regular expression search and returns a matched pattern string
Executes regular expression search and returns a matched pattern string.
Please see _regExpFind_ for the list of regular expression command strings.
<P>
In the matched pattern string, the command strings "\1", "\2", etc. can be
used in the first, second, etc. matched parenthetical items in the
regular expression. The string "\0" represents the complete match.
(Due to the escape processing of the Eval language, the "\" character
itself may need to be escaped.)
<P>
@ex $date_regexp = "([0-9]+)(/|-)([0-9]+)(/|-)([0-9]+)";
@ex print(regExpGrab("Today is 12/11/99",$date_regexp,
@ex "\\1-\\3-\\5"))
@ex // prints "12-11-99"
@ex
@seealso _regExpFind_ _regExpReplace_
*/
void EVLF_regExpGrab(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(regExpReplace,3);
char *newreplstr;
string str = argv[0];
RegExp re(argv[1].c_str());
string replstr = argv[2];
int match_start;
ret_val = "";
match_start = re.RegFind(str.c_str());
if (match_start >= 0){
newreplstr = re.GetReplaceString(replstr.c_str());
ret_val = newreplstr;
free(newreplstr);
}
}
/*
@usehtml
@funcname crypt
@usage $return = crypt($string,$salt)
@arg $string String to encrypt
@arg $salt Salt used for encryption
@arg $return Encrypted string
@index One way encryption for added security with passwords
One way encryption for added security with passwords. For distributions
of SteelBlue compiled without the NO_CRYPT flag, this function calls
the crypt(3C) one way encryption algorithm. Only the first 8 characters
in $string and the first two characters of $salt are used.
@ex print(crypt("test","aa"))
@ex
@ex // prints "aaqPiZY5xR5l."
*/
void EVLF_Crypt(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
string pass;
EXACTLY(Crypt,2);
#ifdef NO_CRYPT
// don't crypt...return clear text
ret_val = argv[0];
#else
pass = crypt ((char *) argv[0].c_str(), (char *) argv[1].c_str());
ret_val = pass;
#endif
}
/*
@usehtml
@usage $return = upcase($string)
@arg $string String to convert into uppercase
@arg $return Uppercase version of the string
@index Converts a string into uppercase
Converts a string into uppercase.
@ex print(upcase("this works 2"))
@ex
@ex // prints "THIS WORKS 2"
*/
void EVLF_upcase(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(upcase,1);
ret_val = upcase(argv[0]);
}
/*
@nodoc
lets you use printf
@author Amy Humphries <A HREF="mailto:amyt@tcg-inc.com">amyt@tcg-inc.com</A>
@index lets you use printf
@return the result of sprintf
@arg1 the format
@arg2 the string
@ex printf('%-23s',$stuff{name})
*/
void EVLF_printf(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(printf,2);
char temp[512];
sprintf (temp, (char *) argv[0].c_str(), argv[1].c_str());
ret_val = temp;
}
/*
@usehtml
@usage $return = return2BR($string)
@arg $string String to convert
@arg $return Converted string
@index Converts newlines to BR tags in a string
Converts newlines to BR tags in a string.
@ex print(return2BR("One line\nAnd the next"))
@ex
@ex // prints "One line<BR>And the next"
*/
void EVLF_return2BR (string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(return2BR,1);
string result = argv[0];
char unEscapedQuote = '\n';
string escapedQuote = "<BR>";
int loc,length;
length = result.length();
for(loc=length-1;loc>=0;loc--){
if (result.at(loc) == unEscapedQuote){
result.replace(loc,1,escapedQuote);
}
}
ret_val=result;
}
/*
@nodoc
*/
void EVLF_jsEscapeQuote(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(escapeQuote,1);
string result = argv[0];
char unEscapedQuote = '\'';
string escapedQuote = "\\\'";
int loc,length;
length = result.length();
for(loc=length-1;loc>=0;loc--){
if (result.at(loc) == unEscapedQuote){
result.replace(loc,1,escapedQuote);
}
}
ret_val=result;
}
/*
@usehtml
@funcname loadFile
@usage $return = loadFile($filename)
@arg $filename Path and name of the file to load
@arg $return Contents of the file (if successful)
@index Loads the contents of a file into a string (in binary mode)
Loads the contents of a file (in binary mode). If the file cannot be
read, it returns the empty string.
<P>
Note: this file can be used to load any file readable by the user
of the Web server. Therefore, care should be taken to restrict
outside access to this command.
@ex $contents = loadFile("/foo.txt")
@ex \\ $contents is set to the contents of foo.txt
@ex \\ (read as a binary file)
@seealso _saveFile_
*/
void EVLF_LoadFile(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(LoadFile,1);
FILE * infile;
struct stat file_stat;
char * buf;
ret_val = "";
infile = fopen(argv[0].c_str(),"rb");
if (infile == NULL){
return;
}
fstat(fileno(infile),&file_stat);
if (file_stat.st_size > 0){
buf = (char *) malloc(sizeof(char) * file_stat.st_size);
fread(buf,sizeof(char),file_stat.st_size,infile);
ret_val.assign(buf,file_stat.st_size);
free(buf);
}
fclose(infile);
}
/*
@usehtml
@funcname saveFile
@usage $return = saveFile($filename,$string)
@arg $filename Path and name of the file to save
@arg $string Binary string to store in the file
@arg $return Boolean denoting success
@index Saves a binary file on the server.
Saves a binary file on the server. If the file exists, it is overwritten.
<P>
Note: this file can be used to save any file writable by the user
of the Web server. Therefore, care should be taken to restrict
outside access to this command.
@ex if(! saveFile("/foo.txt","test")){
@ex print("Test save not successful");
@ex }
@ex \\ Stores the string "test" in "/foo.txt"
@seealso _loadFile_
*/
void EVLF_SaveFile(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(SaveFile,2);
FILE * outfile;
ret_val = "0";
outfile = fopen(argv[0].c_str(),"wb");
if (outfile == NULL){
return;
}
fwrite(argv[1].data(),sizeof(char),argv[1].length(),outfile);
fclose(outfile);
ret_val = "1";
}
/*
@usehtml
@funcname execSteelBlue
@usage execSteelBlue($filename,$tags,$output_file,[$sbexecutable])
@arg $filename Full path to the SteelBlue page
@arg $tags Tags/value pairs in the form "tag1 value1 tag2 value2"
@arg $output_file Full path to output results of SteelBlue execution
@arg $sbexecutable Full path to the SteelBlue executable
@index Executes a command-line SteelBlue page (UNIX only)
Executes a command-line SteelBlue page (UNIX only). If the configuration
option "SB-Executable" is not set, the fourth argument $sbexecutable
is required to specify the full path the steelblue executable. Tags and values
passed into the second argument are set in the $this namespace of the
executed page.
<P>
For high traffic public sites, execSteelBlue can
be used to easily create static pages from database data (like a Calendar).
*/
void EVLF_ExecSteelBlue(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
#ifndef WIN32
BETWEEN(ExecSteelBlue,3,4);
string sb,cmd;
char buf[1024];
FILE * INFILE;
FILE * OUTFILE;
int nchar;
int achar;
int newlines = 0;
if (argc > 3) {
sb = argv[3];
} else if ((*config)["sbexecutable"].length() > 0) {
sb = (*config)["sbexecutable"];
} else {
err_obj.addFatalError("I don't know what executable to use for ExecSteelBlue");
}
cmd = "unset HTTP_USER_AGENT REQUEST_URI SERVER_SOFTWARE SCRIPT_NAME SERVER_SOFTWARE QUERY_STRING; "+sb+" "+argv[0]+" 1 1 "+argv[1];
ret_val = cmd;
INFILE = popen(cmd.c_str(),"r");
if (INFILE == NULL){
return;
}
OUTFILE = fopen(argv[2].c_str(),"w");
if (OUTFILE == NULL){
pclose(INFILE);
return;
}
// Skip next two lines
while ((!feof(INFILE)) && (newlines < 2)){
if (getc(INFILE) == '\n'){
newlines ++;
}
}
while((nchar = fread(buf,sizeof(char),1023,INFILE)) > 0){
fwrite(buf,sizeof(char),nchar,OUTFILE);
}
pclose(INFILE);
fclose(OUTFILE);
#endif
}
/*
@usehtml
@funcname execCmd
@usage $return = execCmd($cmd)
@arg $cmd Shell command to execute
@arg $return STDOUT of the executed command
@index Executes a shell command and captures STDOUT (UNIX only)
Executes a shell command and captures STDOUT (UNIX only).
@ex print(execCmd('date'));
@ex
@ex \\ Prints the date returned by the shell command 'date'
*/
void EVLF_ExecCmd(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
#ifndef WIN32
EXACTLY(ExecCmd,1);
char buf[1024];
FILE *INFILE;
int nchar;
ret_val = "";
INFILE = popen(argv[0].c_str(),"r");
if (INFILE == NULL){
return;
}
while((nchar = fread(buf,sizeof(char),1023,INFILE)) > 0){
buf[nchar] = '\0'; // Terminate the string
ret_val += buf; // Append to return value
}
pclose(INFILE);
#endif
}
/*
@usehtml
@funcname fileExists
@usage $return = execCmd($filename)
@arg $filename Full path of the filename
@arg $return Boolean if file exists and able to be read
@index Checks for existence and readability of a file
Checks for existence and readability of a file.
@ex if(!fileExists("/foo.txt")){
@ex print("Cannot read /foo.txt");
@ex }
*/
void EVLF_FileExists(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(FileExists,1);
const char *filename;
FILE *infile;
filename = argv[0].c_str();
infile = fopen(filename,"r");
if (infile != NULL){ // The file exists and is opened for reading
fclose(infile);
ret_val = "1";
} else {
ret_val = "0";
}
}
/*
@usehtml
@usage $return = length($string)
@arg $string String from which to determine length
@arg $return Integer length of the string
@index Determines length of a binary string
Determines length of a binary string.
@ex print(length("test"));
@ex
@ex // prints "4"
*/
void EVLF_length(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char buf[255];
EXACTLY(length,1);
sprintf(buf,"%d",argv[0].length());
ret_val = buf;
}
/*
@usehtml
@usage $return = substr($string,$start,$num_chars)
@arg $string String from which to get substring
@arg $start Location of the beginning of the substring (0 is the first character)
@arg $num_chars Length of the substring
@arg $return Substring found
@index Returns a specified substring of a string
@ex print(substr("This is a test",10,4));
@ex
@ex // prints "test"
*/
void EVLF_substr(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
int a, b;
EXACTLY(substr,3);
a = atoi (argv[1].c_str());
b = atoi (argv[2].c_str());
ret_val = argv[0].substr (a, b);
}
/*
@usehtml
@usage $return = unpad($string)
@arg $string String to process
@arg $return String with a leading 0 removed
@index Removes a possible leading 0 from numbers
Removes a possible leading 0 from numbers resulting from
_dtStrFromDate_ with %d, %m, etc..
@ex print(unpad(dtStrFromDate("%d","01/02/99")))
@ex
@ex // prints "2"
*/
void EVLF_unpad(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj)
{
EXACTLY(unpad,1);
if ( (strcmp((argv[0].substr(0,1)).c_str(), "0") == 0))
ret_val = argv[0].substr(1,argv[0].length()-1);
else
ret_val = argv[0];
}
/*
@usehtml
@usage $return = removeSpaces($string)
@arg $string String to process
@arg $return String with a leading spaces removed
@index Removes leading spaces from a string
Removes leading spaces from a string.
@ex print("'" . removeSpaces(" Test") . "'");
@ex
@ex // prints "'Test'"
*/
void EVLF_removeSpaces(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj)
{
EXACTLY(removeSpaces,1);
ret_val = argv[0];
while ( (strcmp((ret_val.substr(0,1)).c_str(), " ") == 0))
ret_val = ret_val.substr(1,ret_val.length());
}
/*
@usehtml
@usage $return = binToTxt($string)
@arg $string Binary string to process
@arg $return Text representation of the binary string
@index Converts binary data to text (for insertion into a database).
Converts binary data to text (for insertion into a database). This is
done by mapping every 6 bits of the binary string into 64 printable
characters. The first character of the result holds the original
binary string length mod 3 (for the purpose of proper string
reconstruction).
@ex print(binToTxt("\0\r\nabcd"));
@ex
@ex // prints "10oU2V9qMY100"
@seealso _txtToBin_
*/
void EVLF_binToTxt(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj)
{
EXACTLY(binToTxt,1);
string bin_data = argv[0];
int length_offset = bin_data.length() % 3;
unsigned int i,j;
unsigned char binc[3];
unsigned char txtc[4];
// Pad the end until it is divisible by 3
while ((bin_data.length() % 3) != 0){
bin_data += (char) '\0';
}
// Record the offset
ret_val = (char) (length_offset + 48);
for (i=0; i < (bin_data.length() / 3); i++){
binc[0] = bin_data[i*3];
binc[1] = bin_data[i*3 + 1];
binc[2] = bin_data[i*3 + 2];
txtc[0] = binc[0] & 63;
txtc[1] = ((binc[0] & 192) >> 6) + ((binc[1] & 15) << 2);
txtc[2] = ((binc[1] & 240) >> 4) + ((binc[2] & 3) << 4);
txtc[3] = ((binc[2] & 252) >> 2);
for(j=0;j<4;j++){
if (txtc[j] <= 11){
txtc[j] += 48; // 0-9 : ;
} else if ((txtc[j] >= 12) && (txtc[j] <= 37)){
txtc[j] += 53; // A-Z
} else {
txtc[j] += 59; // a-z
}
ret_val += (unsigned char) txtc[j];
}
}
}
/*
@usehtml
@usage $return = txtToBin($string)
@arg $string Text string to process
@arg $return Binary restoration of the text string
@index Converts text data (processesd by binToTxt()) back to binary.
Converts text data (processed by bintoTxt()) back to binary. The process of
converting binary data to and from text is useful to easily store binary data
into a database that may only accept text data.
@ex print(txtToBin("08Z68oVKNmJK8"));
@ex
@ex // prints "Hi there!"
@seealso _binToTxt_
*/
void EVLF_txtToBin(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj)
{
EXACTLY(binToTxt,1);
string txt_data = argv[0];
int length_offset = txt_data[0] - 48;
unsigned int i,j;
unsigned char binc[3];
unsigned char txtc[4];
if ((length_offset < 0) || (length_offset > 2) ||
((txt_data.length() % 4) != 1)){
err_obj.addFatalError("binToTxt(): Invalid text string format");
return;
}
for (i=0; i < (txt_data.length() / 4); i++){
for(j=0;j < 4; j++){
txtc[j] = txt_data[i*4 + j + 1];
if (txtc[j] <= 59) {
txtc[j] -= 48;
} else if (txtc[j] <= 90) {
txtc[j] -= 53;
} else {
txtc[j] -= 59;
}
}
binc[0] = txtc[0] + ((txtc[1] & 3) << 6);
binc[1] = ((txtc[1] & 60) >> 2) + ((txtc[2] & 15) << 4);
binc[2] = ((txtc[2] & 48) >> 4) + (txtc[3] << 2);
for (j=0;j<3;j++){
ret_val += (unsigned char) binc[j];
}
}
if (length_offset > 0){
length_offset = 3-length_offset;
}
ret_val = ret_val.substr(0,(unsigned int)
(ret_val.length() - length_offset));
}
/*
@usehtml
@usage $return = find($string,$search_string,[$start_index])
@arg $string String in which to search
@arg $search_string Substring for which to search
@arg $start_index Index (starting at 0) in which to start looking (optional)
@arg $return Location (starting at 0) of found substring (-1 if not found)
@index Returns the location of a specified substring in a string.
Returns the location of a specified substring in a string.
@ex print(find("This is a test","test"));
@ex
@ex // prints "10"
*/
void EVLF_find(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
BETWEEN(find,2,3);
char result[255];
int pos = 0;
if (argc == 3) {
pos = atoi (argv[2].c_str());
}
unsigned int res = argv[0].find(argv[1], pos);
if (res == string::npos) {
//save a sprintf
result[0]='-';
result[1]='1';
result[2]='\0';
} else {
sprintf (result, "%d", res);
}
ret_val = result;
}
/*
@usehtml
@usage $return = concat($arg1,...,$argn)
@arg $arg1,...,$argn Variable number of strings
@arg $return All strings concatenated (joined) together
@index Concatenates a variable number of strings
Concatenates a variable number of strings.
@ex print(concat("this"," is"," a"," test"));
@ex
@ex // prints "this is a test"
*/
void EVLF_concat(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
int i;
ANYARGS;
ret_val = "";
for(i=0;i<argc;i++){
ret_val += argv[i];
}
}
/*
@usehtml
@usage $return = eval($evalcmd)
@arg $evalcmd Eval language command
@arg $return Result of Eval command
@index Executes a dynamic Eval command
Executes a dynamic Eval command.
@ex $i = 2;
@ex $name = "i";
@ex print(eval("$".$name." + 1"));
@ex
@ex // prints "3"
*/
void EVLF_eval(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
int i;
EvalParser evalparser;
ANYARGS;
ret_val = "";
for(i=0;i<argc;i++){
evalparser.newString(argv[i],err_obj);
ret_val = evalparser.returnValue();
}
}
/*
@usehtml
@usage print($arg1,...,$argn)
@arg $arg1,...,$argn Variable number of (binary) strings
@index Prints (binary) strings to STDOUT
Prints a variable number of (binary) strings to STDOUT.
@ex $i = 2;
@ex print("$i = ",$i);
@ex
@ex // prints "$i = 2"
*/
void EVLF_print(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
int i;
ANYARGS;
ret_val = "";
for(i=0;i<argc;i++){
cout << argv[i];
}
}
/*
@usehtml
@usage flush()
@index Flushes the STDOUT buffer
Flushes the STDOUT buffer
@ex print("Please wait"); flush();
@ex execCmd('sleep 15');
@ex print("\nDone");
@ex
@ex // prints "Please wait" before pausing 15 seconds. Becuase
@ex // of the flush, the message should be visible to the user
@ex // before the pause,
*/
void EVLF_flush(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
ANYARGS;
cout.flush();
}
/*
@usehtml
@usage $return = group($grouplist)
@arg $grouplist Comma delineated list of groups
@arg $return Boolean - true if the current user is a member of one of the groups
@index Returns true if the user is part of a list of comma separated groups
Returns true if the user is part of a list of comma separated groups.
Group memebership is determined if an appropriate entry is placed in
the Groups database table.
<P>
For speed, the first group call causes all groups to which the current
user belongs to be stored in an associative array. Subsequent
group calls in the page use this cached information.
@ex if(group('admin,tcg')){
@ex // Do something special since the user is either in
@ex // the group "admin" or "tcg"
@ex }
*/
void EVLF_group(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
string group;
string split = ",";
string item;
ret_val = "0";
EXACTLY(group,1);
group = argv[0];
// The environment now has the groups, now split the group string and check
do {
if (group.find(split) != string::npos){
item = group.substr(0, group.find(split));
group = group.substr(group.find(split) + split.length());
} else {
item = group;
group = ""; // Signal end
}
if (security->getUserInfo().belongsToGroup(item)){ // WE MATCHED ONE
ret_val = "1";
group = ""; // Signal end
}
} while (group.length() > 0);
}
/*
@usehtml
@usage $return = getSequence ($seq_name)
@arg $seq_name String of sequence names
@arg $return Resulting sequence
@index Simulates a sequence using Access or mySQL autonumber columns.
Simulates a sequence using Access or mySQL autonumber columns.
For Access, it requires a table to be created for each sequence.
The format should be:
<blockquote>
<pre>
create table seq_XXXXXXXX {
seq autonumber,
pid long
}
</pre>
</blockquote>
where XXXXXXXX is the name of sequence.
@ex $pid = getSequence('person') //uses seq_person table
*/
#ifdef WIN32
#include <process.h>
#endif
void EVLF_getSequence(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(sql,1);
try {
char sql[512];
#ifdef WIN32
//int pid = _getpid();
//pid isn't working...it's returning garbage
int pid = rand();
#else
//put in UNIX get pid code here!
int pid = rand();
#endif
//do a cautionary cleaning
sprintf (sql, "delete from seq_%s where pid = %d", argv[0].c_str(), pid);
dbgate->setSQL (sql);
dbgate->execute();
// make the sql to insert into the seq table
sprintf (sql, "insert into seq_%s (pid) values (%d)", argv[0].c_str(), pid);
dbgate->setSQL (sql);
dbgate->execute();
// grab the autonumbered field
sprintf (sql, "select seq from seq_%s where pid = %d", argv[0].c_str(), pid);
dbgate->setSQL (sql);
dbgate->execute();
AssocArray * row;
Result * r;
r = dbgate->getResult();
if (r == NULL) {
ret_val ="it was null!";
}
while ((r != NULL) && (row = r->nextRow()) != NULL){
ret_val = (*row)["0"];
delete(row);
}
if (r != NULL){
delete(r);
}
//now clean out the old values
sprintf (sql, "delete from seq_%s where pid = %d", argv[0].c_str(), pid);
dbgate->setSQL (sql);
dbgate->execute();
} catch (char * err) {
cout << "Content-type: text/html\n\n\nGot " << err << " from the db";
exit(1);
}
}
/*
@usehtml
@usage sql($sqlcmd)
@arg $sqlcmd SQL Command to execute
@index Executes an arbitrary SQL command (no data returned)
Executes an arbitrary SQL command (no data returned)
@ex sql("delete from Swap where username='"
@ex .$USER{username}."'")
@ex // Completely clears the Swap for a given username
*/
void EVLF_sql(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(sql,1);
try {
dbgate->setSQL (argv[0].c_str());
dbgate->execute();
} catch (char * err) {
cout << "Content-type: text/html\n\n\nGot " << err << " from the db";
exit(1);
}
}
/*
@usehtml
@usage $return = escapeQuote($string)
@arg $string String to process
@arg $return String with quotes escaped appropriate for SQL statements
@index Escapes the quotes in a string appropriate for use in SQL statements
Escapes the quotes in a string appropriate for use in SQL statements
@ex sql("insert into Notes values ('"
@ex .escapeQuote($this{NOTES})."'")
@ex // If NOTES has quotes, it is properly escaped before entry
@ex // into the database
*/
void EVLF_escapeQuote(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(escapeQuote,1);
//cout << "\n\nlength of input is: " << argv[0].length() << "\n<br>";
ret_val = dbgate->escapeQuote (argv[0]);
//cout << "\n\nlength of output is: " << ret_val.length() << "\n<br>";
}
#include <fstream>
/*
@nodoc
The size restriction of 500K should be removed
the first arg is a file name, the second is SQL with ":1" in
it. The contents of the file will be put (after escaping the sql)
into the place of the :1 */
void EVLF_filetoblob(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
string fname;
string sql;
char contents [500000];
char escaped [500000];
int bytes_read,i,j;
ifstream data;
EXACTLY(filetoblob,2);
fname = argv[0];
sql = argv[1];
data.open (fname.c_str());
data.read (contents, 500000);
bytes_read = data.gcount();
contents[bytes_read] = '\0';
data.close();
j = 0;
for (i=0; i < bytes_read; i++, j++) {
if (contents[i] == '\''){
escaped[j] = '\\';
j++;
escaped[j] = contents[i];
} else if (contents[i] == '\0') {
escaped[j] = '\\';
j++;
escaped[j] = '0';
} else if (contents[i] == '\\') {
escaped[j] = '\\';
j++;
escaped[j] = '\\';
} else if (contents[i] == '\"') {
escaped[j] = '\\';
j++;
escaped[j] = '\"';
} else {
escaped[j] = contents[i];
}
}
//sql.at (":1") = escaped;
sql.replace(sql.find(":1"), 2, escaped);
dbgate->setSQL (sql.c_str());
dbgate->execute();
}
#ifdef USE_MYSQL
#include <mysql.h>
/*
@nodoc
Function is not finished
*/
void EVLF_printblob(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
string sql;
MYSQL * dbh = NULL;
MYSQL_RES * res;
MYSQL_ROW row;
MYSQL_FIELD * field;
char * buf;
int r;
unsigned int i,len;
EXACTLY(printblob,1);
sql = argv[0];
dbh = mysql_connect (dbh, "localhost", "www", "");
r = mysql_select_db (dbh , "Magic");
if (r) {
// we couldn't access this db, throw an error
cout << (mysql_error (dbh) );
}
mysql_query (dbh, sql.c_str());
res = mysql_store_result(dbh);
row = mysql_fetch_row (res);
mysql_field_seek (res, 0);
field = mysql_fetch_field (res);
len = (mysql_fetch_lengths (res))[0];
for (i=0; i < len; i++) {
cout << row[0][i];
}
}
#endif
/*
@usehtml
@usage $return = strcmp($stra,$strb)
@arg $stra,$strb Two strings to compare
@arg $return 0 if strings are the same, negative if $stra is before $strb, positive if $stra is after $strb
@index Compares two strings for lexicographic order
Compares two strings for lexicographic order. The function returns
"0" if the strings are the same, a negative number if the first string
is before the second string, and a positive number if the first string
is after the second string.
@ex print(strcmp("hi","hello"));
@ex
@ex // prints a positive number
*/
void EVLF_strcmp(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char buf[80];
EXACTLY(strcmp,2);
sprintf(buf,"%d",strcmp(argv[0].c_str(),argv[1].c_str()));
ret_val = buf;
}
/*
@usehtml
@usage $return = rand([$maxnum])
@arg $maxnum Max value for the random numbers (default 32767)
@arg $return The random number
@index Returns a random integer
Returns a random integer in the range 0 to $maxnum (inclusive). If
$maxnum is not given, it defaults to 32767.
@ex for($i=0;$i<10;$i=$i+1){
@ex print(rand(10)," ");
@ex }
@ex // prints 10 random integers in [0,10]
*/
void EVLF_rand(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
int r;
int top;
char result [255];
BETWEEN(rand,0,1);
if (argc > 0) {
top = 1 + atoi ( argv[0].c_str() );
} else {
top = 32768;
}
r = rand() % top;
sprintf (result , "%d", r);
ret_val = result;
}
/*
@usehtml
@usage $return = formatNum($number)
@arg $number Number to format
@arg $return The formatted number
@index Formats a number using sprintf format strings.
Formats a number using sprintf format strings.
@ex print(formatNum("$%.2f","100.3"));
@ex
@ex // prints "$100.30"
*/
void EVLF_formatNum(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char buf[80];
EXACTLY(formatNum,2);
sprintf(buf,argv[0].c_str(),atof(argv[1].c_str()));
ret_val = buf;
}
/*
@usehtml
@usage $return = round($number)
@arg $number Number to round
@arg $return Nearest integer of provided number
@index Rounds a number to the nearest integer
Rounds a number to the nearest integer.
@ex print(round(1.6))
@ex
@ex // prints "2"
*/
void EVLF_round(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char buff[80];
double i;
double frac;
EXACTLY(round,1);
frac = modf (atof(argv[0].c_str()) , &i);
// find the sign of the number
if (i < 0) {
// the number is negative
// In which case so is the fractional component.
if (frac < -0.5) {
i = i - 1;
} else {
i = i;
}
} else {
//the number is positive
if (frac < .5) {
i = i;
} else {
i = i + 1;
}
}
sprintf (buff, "%.0f", i);
ret_val = buff;
}
/*
@usehtml
@usage $return = ceil($number)
@arg $number Number to process
@arg $return Closest integer with greater absolute value of the provided number
@index Rounds a number to the nearest integer with greater absolute value
Rounds a number to the nearest integer with greater absolute value.
@ex print(ceil(3.1))
@ex
@ex // prints "4"
*/
void EVLF_ceil(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char buff[80];
double i;
double frac;
EXACTLY(ceil,1);
frac = modf (atof(argv[0].c_str()) , &i);
// find the sign of the number
if (i < 0) {
// the number is negative
// In which case so is the fractional component.
if (frac != 0) {
i = i - 1;
}
} else {
//the number is positive
if (frac != 0) {
i = i + 1;
}
}
sprintf (buff, "%.0f", i);
ret_val = buff;
}
/*
@usehtml
@usage $return = floor($number)
@arg $number Number to process
@arg $return Closest integer with lesser absolute value of the provided number
@index Rounds a number to the nearest integer with lesser absolute value
Rounds a number to the nearest integer with lesser absolute value.
@ex print(floor(2.6))
@ex
@ex // prints "2"
*/
void EVLF_floor(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
char buff[80];
double i;
double frac;
EXACTLY(floor,1);
frac = modf (atof(argv[0].c_str()) , &i);
sprintf (buff, "%.0f", i);
ret_val = buff;
}
/******************************
variable manipulation stuff
*******************************/
/*
@usehtml
@usage my($var1,...,$varn)
@arg $var1,...,$varn variable names
@index Declares variables in the local scope
Declares variables in the local scope. New scopes are defined in the
<a href="tags.html#LOOP">LOOP</a> and <a
href="tags.html#DEFFUN">DEFFUN</a> command.
<P>
Please see the <a href="eval.html#functions">Functions</a> section of
the <A href="eval.html">Eval Language of SteelBlue</a> for an example
of my() used in a recursive function.
*/
void EVLF_my(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
int i;
string v;
ANYARGS;
for (i=0; i < argc; i++) {
v = argn[i];
if (v.length() > 0) {
userdata->declareVar(v, "");
}
}
}
/*
@usehtml
@usage $return = nextKey($namespace, $previouskey)
@arg $namespace Namespace of associative array (actual namespace, not a string)
@arg $previouskey Key before the next key returned ("" to get the first key)
@arg $return Next key after the given key ("" is at the end of the list)
@index Iterates through the keys of an associative array
Iterates through the keys of an associative array. The first key is
returned by setting $previouskey to "". The next key is then returned
by setting $previouskey to the previously returned key. "" is returned
when all keys have been listed.
@ex $test{a} = "hi"; $test{b} = "there"; $test{c} = 3;
@ex for($key=nextKey($test,"");
@ex $key ne "";
@ex $key=nextKey($test,$key)){
@ex print("$test{",$key,"} = '",$test{$key},"'<br>");
@ex }
@ex
@ex // Prints the three assignments to the $test namespace
*/
void EVLF_nextKey(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
AssocArray::iterator i;
AssocArray * assoc_array;
char buf[80];
EXACTLY(nextKey,2);
assoc_array = userdata->getAssocArray(argn[0]);
if (assoc_array == NULL){
ret_val = "";
return;
}
// If second arg is "", send the first elem
if (argv[1].length() == 0){
i = assoc_array->begin();
if (i == assoc_array->end()){
ret_val = "";
} else {
ret_val = i->first;
}
return;
}
// Else find the key and send the next elem
i = assoc_array->find(argv[1]);
if (i == assoc_array->end()){
ret_val = "";
return;
}
i++;
if (i == assoc_array->end()){
ret_val = "";
return;
}
ret_val = i->first;
}
/*
@nodoc
@usehtml
@usage getGroupByIP()
@index For public users, assigns groups to the user based on their IP address
For public users, assigns groups to the user based on their IP address.
It uses pattern matching from the group_by_ip table. It should be called
right after the user initially access the application.
*/
void EVLF_GetGroupByIP(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
ANYARGS;
const char *username;
string u= security->getUsername();
string institution = security->getInstitution();
username = u.c_str();
RegExp re("Guest.*");
if (re.Match(username))
{
char * ip = getenv("REMOTE_ADDR");
char sql[512];
AssocArray * row;
Result * r;
const char *group;
const char *pattern;
StringDLList groups;
StringDLList::iterator i;
sprintf (sql, "select ip, groupname from group_by_ip");
dbgate->setSQL (sql);
dbgate->execute();
r = dbgate->getResult();
//Store up the groups we'll want to insert
while ((row = r->nextRow()) != NULL) {
pattern = (*row)["0"].c_str();
RegExp ip_pattern(pattern);
if (ip_pattern.Match(ip))
{
groups.push_back((*row)["1"]);
}
delete(row);
}
if (r != NULL){
delete(r);
}
for (i=groups.begin(); i != groups.end(); i++)
{
sprintf (sql, "insert into %s (username, institution, grp) values ('%s', '%s', '%s')", (*config)["groupstable"].c_str(), username, institution.c_str(), (*i).c_str());
dbgate->setSQL (sql);
dbgate->execute();
}
}
ret_val="1";
}
/*
@usehtml
@funcname getEnv
@usage $return = getEnv($env_var)
@arg $env_var Environment variable name
@arg $return Value of the environment variable
@index Gets the value of an environment variable
Gets the value of an environment variable
@ex print(getenv('REQUEST_URI'))
@ex
@ex // prints the CGI command string
*/
void EVLF_GetEnv(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(GetEnv,1);
char * out = getenv (argv[0].c_str());
ret_val = out;
}
/*
@usehtml
@funcname getConfig
@usage $return = getConfig($config_name)
@arg $config_name Configuration variable name
@arg $return Value of the configuration variable
@index Gets the value of a configuration variable
Gets the value of a configuration variable. The configuration
variable name ($config_name) is case insensitive and ignores "-" and
"_" characters.
@ex print(getconfig('UseDB'))
@ex
@ex //prints the type of database currently used */
void EVLF_GetConfig(string & ret_val, string argv[], string argn[],
int argc, Error & err_obj){
EXACTLY(GetConfig,1);
char arraykey[MAX_CONF_LEN];
const char * buff = argv[0].c_str();
int i, j;
// now we will eliminate any hyphens and underscores in the key as if
// they didn't exist, and also downcase the characters
for (i=0,j=0; buff[i] != '\0' && i < (MAX_CONF_LEN-1); i++) {
if (buff[i] != '-' && buff[i] != '_') {
arraykey[j] = tolower (buff[i]) ;
j++;
}
}
arraykey[j] = '\0';
ret_val = (*config)[arraykey];
}
#include "BaseSBModule.h"
/*
@usehtml
@usage $return = makeHREF($screen)
@arg $screen SteelBlue screen name
@arg $return URL to access SteelBlue screen
@index Creates a URL to access a given SteelBlue screen
Creates a URL to access a given SteelBlue screen. This is useful for creating
a pop-up pages that access SteelBlue screens.
@ex print(makeHREF('welcome.sb'))
@ex
@ex // prints a URL to the SteelBlue screen 'welcome.sb'
*/
void EVLF_makeHREF(string & ret_val, string argv[], string argn[], int argc,
Error & err_obj){
EXACTLY(makeHREF,1);
ret_val = ((BaseSBModule *) userdata->getExec())->makeHref(argv[0]);
}
void evalFuncInit(EnvObject * env_obj){
// time and date
env_obj->registerEvalFunc("DTTIME",&EVLF_dtTime);
env_obj->registerEvalFunc("DTLEGALDATE",&EVLF_dtLegalDate);
env_obj->registerEvalFunc("DTEPOC",&EVLF_dtEpoc);
env_obj->registerEvalFunc("DTTODAY",&EVLF_dtToday);
env_obj->registerEvalFunc("DTSTRFROMNOW",&EVLF_dtStrFromNow);
env_obj->registerEvalFunc("DTSTRFROMDATE",&EVLF_dtStrFromDate);
env_obj->registerEvalFunc("DTFORMATTIME",&EVLF_dtFormatTime);
env_obj->registerEvalFunc("DTFORMATMONTH",&EVLF_dtFormatMonth);
env_obj->registerEvalFunc("DTSTRFROMTIME",&EVLF_dtStrFromTime);
env_obj->registerEvalFunc("DTSTRFROMEPOC",&EVLF_dtStrFromEpoc);
env_obj->registerEvalFunc("DTADDDAYS",&EVLF_dtAddDays);
env_obj->registerEvalFunc("DTN2S_MONTH",&EVLF_dtNumberToShortMonth);
env_obj->registerEvalFunc("DTN2L_MONTH",&EVLF_dtNumberToLongMonth);
env_obj->registerEvalFunc("DTS2L_MONTH",&EVLF_dtShortToLongMonth);
env_obj->registerEvalFunc("DTS2N_MONTH",&EVLF_dtShortToNumberMonth);
env_obj->registerEvalFunc("DTS2N_MONTH2",&EVLF_dtShortToNumberMonth2);
env_obj->registerEvalFunc("DTL2N_MONTH",&EVLF_dtLongToNumberMonth);
env_obj->registerEvalFunc("DTL2S_MONTH",&EVLF_dtLongToShortMonth);
env_obj->registerEvalFunc("DTMAKEDATE",&EVLF_dtMakeDate);
env_obj->registerEvalFunc("DTNEXTSUNDAY",&EVLF_dtNextSunday);
env_obj->registerEvalFunc("DTNEXTSATURDAY",&EVLF_dtNextSaturday);
env_obj->registerEvalFunc("DTNEXTDAY",&EVLF_dtNextDayOfWeek);
env_obj->registerEvalFunc("DTNEXTDAYOFWEEK",&EVLF_dtNextDayOfWeek);
env_obj->registerEvalFunc("DTDAYSINMONTH",&EVLF_dtDaysInMonth);
env_obj->registerEvalFunc("DTFULLWEEKDAY",&EVLF_dtFullWeekday);
//basic stuff
env_obj->registerEvalFunc("CONCAT",&EVLF_concat);
env_obj->registerEvalFunc("EVAL",&EVLF_eval);
env_obj->registerEvalFunc("FORMATNUM",&EVLF_formatNum);
env_obj->registerEvalFunc("ROUND",&EVLF_round);
env_obj->registerEvalFunc("CEIL",&EVLF_ceil);
env_obj->registerEvalFunc("FLOOR",&EVLF_floor);
env_obj->registerEvalFunc("FLUSH",&EVLF_flush);
env_obj->registerEvalFunc("GROUP",&EVLF_group);
env_obj->registerEvalFunc("LENGTH",&EVLF_length);
env_obj->registerEvalFunc("SUBSTR",&EVLF_substr);
env_obj->registerEvalFunc("FIND",&EVLF_find);
env_obj->registerEvalFunc("PRINT",&EVLF_print);
env_obj->registerEvalFunc("PRINTF",&EVLF_printf);
env_obj->registerEvalFunc("STRCMP",&EVLF_strcmp);
env_obj->registerEvalFunc("RAND",&EVLF_rand);
env_obj->registerEvalFunc("CRYPT",&EVLF_Crypt);
env_obj->registerEvalFunc("GETENV",&EVLF_GetEnv);
env_obj->registerEvalFunc("GETCONFIG",&EVLF_GetConfig);
env_obj->registerEvalFunc("UPCASE",&EVLF_upcase);
env_obj->registerEvalFunc("LOADFILE",&EVLF_LoadFile);
env_obj->registerEvalFunc("SAVEFILE",&EVLF_SaveFile);
env_obj->registerEvalFunc("EXECSTEELBLUE",&EVLF_ExecSteelBlue);
env_obj->registerEvalFunc("EXECCMD",&EVLF_ExecCmd);
env_obj->registerEvalFunc("FILEEXISTS",&EVLF_FileExists);
env_obj->registerEvalFunc("GETGROUPBYIP",&EVLF_GetGroupByIP);
// variable manipulation stuff
env_obj->registerEvalFunc("NEXTKEY",&EVLF_nextKey);
env_obj->registerEvalFunc("MY", &EVLF_my);
env_obj->registerEvalFunc("RETURN2BR",&EVLF_return2BR);
env_obj->registerEvalFunc("ESCAPEQUOTE",&EVLF_escapeQuote);
env_obj->registerEvalFunc("JSESCAPEQUOTE",&EVLF_jsEscapeQuote);
env_obj->registerEvalFunc("REGEXPFIND",&EVLF_regExpFind);
env_obj->registerEvalFunc("REGEXPGRAB",&EVLF_regExpGrab);
env_obj->registerEvalFunc("REGEXPREPLACE",&EVLF_regExpReplace);
env_obj->registerEvalFunc("UNPAD",&EVLF_unpad);
env_obj->registerEvalFunc("REMOVESPACES",&EVLF_removeSpaces);
env_obj->registerEvalFunc("BINTOTXT",&EVLF_binToTxt);
env_obj->registerEvalFunc("TXTTOBIN",&EVLF_txtToBin);
//BLOB handlers and other SQL
env_obj->registerEvalFunc("SQL",&EVLF_sql);
env_obj->registerEvalFunc("GETSEQUENCE",&EVLF_getSequence);
env_obj->registerEvalFunc("FILETOBLOB",&EVLF_filetoblob);
#ifdef USE_MYSQL
env_obj->registerEvalFunc("PRINTBLOB",&EVLF_printblob);
#endif
// SB specific stuff
env_obj->registerEvalFunc ("MAKEHREF", &EVLF_makeHREF);
/* We need to initalize the random seed somewhere! */
srand (time(NULL));
}