2001-06-19 06:55:19 +00:00
< ?
2000-12-23 23:25:28 +00:00
2001-02-18 15:14:56 +00:00
function conf_init () {
2001-02-04 22:09:38 +00:00
global $HTTP_HOST , $REQUEST_URI ;
2001-03-10 18:38:47 +00:00
$file = strtolower ( strtr ( $HTTP_HOST . " " . substr ( $REQUEST_URI , 0 , strrpos ( $REQUEST_URI , " / " )), " /: " , " .. " ));
2001-05-15 18:38:57 +00:00
while ( $file && ! file_exists ( " includes/ $file .php " )) $file = substr ( $file , 0 , strrpos ( $file , " . " ));
2001-05-20 13:51:40 +00:00
return $file ? $file : " conf " ;
2001-02-04 22:09:38 +00:00
}
2001-05-06 17:19:58 +00:00
function error_handler ( $errno , $message , $filename , $line , $variables ) {
$types = array ( 1 => " error " , 2 => " warning " , 4 => " parse error " , 8 => " notice " , 16 => " core error " , 32 => " core warning " , 64 => " compile error " , 128 => " compile warning " , 256 => " user error " , 512 => " user warning " , 1024 => " user notice " );
$entry = $types [ $errno ] . " : $message in $filename on line $line . " ;
2001-06-04 19:02:38 +00:00
if (( $errno == 1 || $errno == 2 || $errno == 4 ) && error_reporting ()) {
2001-05-06 17:19:58 +00:00
watchdog ( " error " , $types [ $errno ] . " : $message in $filename on line $line . " );
print $entry ;
}
}
2001-04-06 14:14:16 +00:00
function watchdog ( $type , $message ) {
2001-06-13 21:37:19 +00:00
global $user ;
db_query ( " INSERT INTO watchdog (user, type, message, location, hostname, timestamp) VALUES (' $user->id ', ' " . check_input ( $type ) . " ', ' " . check_input ( $message ) . " ', ' " . check_input ( getenv ( " REQUEST_URI " )) . " ', ' " . check_input ( getenv ( " REMOTE_ADDR " )) . " ', ' " . time () . " ') " );
2001-04-06 14:14:16 +00:00
}
function throttle ( $type , $rate ) {
2001-06-29 22:08:57 +00:00
if ( ! user_access ( " access administration pages " )) {
2001-04-06 14:14:16 +00:00
if ( $throttle = db_fetch_object ( db_query ( " SELECT * FROM watchdog WHERE type = ' $type ' AND hostname = ' " . getenv ( " REMOTE_ADDR " ) . " ' AND " . time () . " - timestamp < $rate " ))) {
watchdog ( " warning " , " throttle: ' " . getenv ( " REMOTE_ADDR " ) . " ' exceeded submission rate - $throttle->type " );
2001-06-13 21:37:19 +00:00
die ( message_throttle ());
2001-04-06 14:14:16 +00:00
}
else {
2001-06-13 21:37:19 +00:00
watchdog ( $type , " throttle " );
2001-04-06 14:14:16 +00:00
}
}
}
2001-05-15 18:38:57 +00:00
function path_uri () {
global $HTTP_HOST , $REQUEST_URI ;
return " http:// " . $HTTP_HOST . substr ( $REQUEST_URI , 0 , strrpos ( $REQUEST_URI , " / " )) . " / " ;
}
function path_img () {
// use "http://your-image-server.com/ if you want to host images on a seperate server.
return " ./images/ " ;
}
2001-06-20 20:00:40 +00:00
function message_access () {
return t ( " You are not authorized to access to this page. " );
2001-05-15 18:38:57 +00:00
}
2001-04-29 12:39:55 +00:00
2001-07-03 19:09:51 +00:00
function message_na () {
return t ( " n/a " );
}
2001-06-13 21:37:19 +00:00
function message_throttle () {
return t ( " You exceeded the submission rate exceeded. Please wait a few minutes and try again. " );
}
2001-05-20 13:51:40 +00:00
function check_form ( $text ) {
return htmlspecialchars ( stripslashes ( $text ));
2001-04-29 12:39:55 +00:00
}
2001-05-20 13:51:40 +00:00
function check_export ( $text ) {
return htmlspecialchars ( stripslashes ( $text ));
2001-04-29 12:39:55 +00:00
}
2001-05-20 13:51:40 +00:00
function check_code ( $text ) {
return $text ;
2001-04-29 12:39:55 +00:00
}
2001-05-20 13:51:40 +00:00
function check_preview ( $text ) {
2001-05-24 08:53:07 +00:00
return check_output ( check_input ( $text ));
2001-05-20 13:51:40 +00:00
}
2001-05-20 19:30:39 +00:00
function check_query ( $text ) {
return addslashes ( stripslashes ( $text ));
}
2001-05-20 13:51:40 +00:00
function check_input ( $text ) {
2001-07-14 12:12:41 +00:00
foreach ( module_list () as $name ) {
if ( module_hook ( $name , " filter " )) $text = module_invoke ( $name , " filter " , $text );
}
2001-05-20 13:51:40 +00:00
return addslashes ( stripslashes ( substr ( $text , 0 , variable_get ( " max_input_size " , 10000 ))));
}
function check_output ( $text , $nl2br = 0 ) {
2001-07-03 19:09:51 +00:00
return ( $text ) ? ( $nl2br ? nl2br ( stripslashes ( $text )) : stripslashes ( $text )) : message_na ();
2001-04-29 12:39:55 +00:00
}
2001-07-14 12:12:41 +00:00
function format_info ( $body , $block ) {
return " <table><tr><td><table align= \" right \" border= \" 1 \" width= \" 180 \" ><tr><td> $block </td></tr></table> $body </td></tr></table> \n " ;
}
2001-07-14 13:40:47 +00:00
function format_rss_channel ( $title , $link , $description , $items , $language = " en " ) {
2001-07-14 13:36:38 +00:00
$output .= " <channel> \n " ;
$output .= " <title> " . strip_tags ( $title ) . " </title> \n " ;
$output .= " <link> " . strip_tags ( $link ) . " </link> \n " ;
$output .= " <description> " . htmlentities ( $description ) . " </description> \n " ;
2001-07-14 13:40:47 +00:00
$output .= " <language> $language </language> \n " ;
2001-07-14 13:36:38 +00:00
$output .= $items ;
$output .= " </channel> \n " ;
return $output ;
}
function format_rss_item ( $title , $link , $description ) {
$output .= " <item> \n " ;
$output .= " <title> " . strip_tags ( $title ) . " </title> \n " ;
$output .= " <link> " . strip_tags ( $link ) . " </link> \n " ;
$output .= " <description> " . htmlentities ( $description ) . " </description> \n " ;
$output .= " </item> \n " ;
return $output ;
}
2001-04-29 12:39:55 +00:00
function format_plural ( $count , $singular , $plural ) {
return ( $count == 1 ) ? " $count " . t ( $singular ) : " $count " . t ( $plural );
}
2001-07-17 09:04:33 +00:00
function format_size ( $size ) {
$suffix = " bytes " ;
if ( $size > 1024 ) {
$size = round ( $size / 1024 , 2 );
$suffix = " KB " ;
}
if ( $size > 1024 ) {
$size = round ( $size / 1024 , 2 );
$suffix = " MB " ;
}
return " $size $suffix " ;
}
2001-06-30 20:23:33 +00:00
function cache_clear ( $interval = 0 ) {
db_query ( " DELETE FROM cache WHERE " . time () . " - timestamp > $interval " );
}
function cache_get () {
global $user , $REQUEST_URI , $REQUEST_METHOD ;
2001-07-01 11:27:10 +00:00
if ( ! $user -> id && $REQUEST_METHOD == " GET " ) {
if ( $cache = db_fetch_object ( db_query ( " SELECT * FROM cache WHERE url = ' " . check_input ( $REQUEST_URI ) . " ' " ))) {
cache_clear ( variable_get ( " cache_clear " , 30 ));
}
else {
ob_start ();
}
2001-06-30 20:23:33 +00:00
}
2001-07-01 11:27:10 +00:00
return $cache -> data ? $cache -> data : 0 ;
2001-06-30 20:23:33 +00:00
}
function cache_set () {
2001-07-01 11:27:10 +00:00
global $user , $REQUEST_URI , $REQUEST_METHOD ;
2001-06-30 20:23:33 +00:00
2001-07-01 11:27:10 +00:00
if ( ! $user -> id && $REQUEST_METHOD == " GET " ) {
if ( $data = ob_get_contents ()) {
db_query ( " INSERT INTO cache (url, data, timestamp) VALUES(' " . addslashes ( $REQUEST_URI ) . " ', ' " . addslashes ( $data ) . " ', ' " . time () . " ') " );
}
}
2001-06-30 20:23:33 +00:00
}
2001-04-29 12:39:55 +00:00
function format_interval ( $timestamp ) {
2001-05-17 19:14:50 +00:00
$units = array ( " year|years " => 31536000 , " week|weeks " => 604800 , " day|days " => 86400 , " hour|hours " => 3600 , " min|min " => 60 , " sec|sec " => 1 );
2001-05-17 18:25:08 +00:00
foreach ( $units as $key => $value ) {
2001-05-17 19:14:50 +00:00
$key = explode ( " | " , $key );
2001-05-17 18:25:08 +00:00
if ( $timestamp >= $value ) {
$output .= ( $output ? " " : " " ) . format_plural ( floor ( $timestamp / $value ), $key [ 0 ], $key [ 1 ]);
$timestamp %= $value ;
}
2001-04-29 12:39:55 +00:00
}
return ( $output ) ? $output : " 0 sec " ;
}
function format_date ( $timestamp , $type = " medium " , $format = " " ) {
global $user ;
$timestamp += ( $user -> timezone ) ? $user -> timezone - date ( " Z " ) : 0 ;
switch ( $type ) {
case " small " :
$date = date ( " m/d/y - H:i " , $timestamp );
break ;
case " medium " :
$date = t ( date ( " l " , $timestamp )) . " , " . date ( " m/d/Y - H:i " , $timestamp );
break ;
case " large " :
$date = t ( date ( " l " , $timestamp )) . " , " . t ( date ( " F " , $timestamp )) . " " . date ( " d, Y - H:i " , $timestamp );
break ;
case " custom " :
2001-05-17 19:14:50 +00:00
for ( $i = strlen ( $format ); $i >= 0 ; $c = $format [ -- $i ]) {
if ( strstr ( " DFlMSw " , $c )) {
2001-07-07 16:37:27 +00:00
$date = t ( date ( $c , $timestamp )) . $date ;
2001-05-17 19:14:50 +00:00
}
else if ( strstr ( " AaBdgGhHiIjLmnrstTUYyZz " , $c )) {
$date = date ( $c , $timestamp ) . $date ;
}
else {
$date = $c . $date ;
}
2001-05-17 17:56:52 +00:00
}
2001-04-29 12:39:55 +00:00
break ;
default :
$date = t ( date ( " l " , $timestamp )) . " , " . date ( " m/d/Y - H:i " , $timestamp );
}
return $date ;
}
function format_username ( $username ) {
2001-07-07 16:37:27 +00:00
if ( $username ) return ( user_access ( " administer users " ) ? " <A HREF= \" admin.php?mod=account&op=view&name= " . urlencode ( $username ) . " \" > $username </A> " : " <A HREF= \" account.php?op=view&name= " . urlencode ( $username ) . " \" > $username </A> " );
2001-04-29 12:39:55 +00:00
else return variable_get ( anonymous , " Anonymous " );
}
function format_email ( $address ) {
2001-07-03 19:09:51 +00:00
return ( $address ) ? " <a href= \" mailto: $address\ " > $address </ A > " : message_na();
2001-04-29 12:39:55 +00:00
}
2001-07-11 22:06:24 +00:00
function format_url ( $address , $description = 0 ) {
2001-04-29 12:39:55 +00:00
$description = ( $description ) ? $description : $address ;
2001-07-03 19:09:51 +00:00
return ( $address ) ? " <a href= \" $address\ " > " . check_output( $description ) . " </ a > " : message_na();
2001-04-29 12:39:55 +00:00
}
function format_tag ( $link , $text ) {
return " '<a href= \" node.php?title='. urlencode(' $link ') .' \" >'. (' $text ' ? ' $text ' : ' $link ') .'</a>' " ;
}
2001-05-17 19:14:50 +00:00
function form ( $action , $form , $method = " post " , $options = 0 ) {
2001-05-31 18:47:43 +00:00
return " <form action= \" $action\ " method = \ " $method\ " " . ( $options ? " $options " : " " ) . " > \n $form </ form > \n " ;
2001-04-30 17:13:08 +00:00
}
2001-04-29 12:39:55 +00:00
function form_item ( $title , $value , $description = 0 ) {
2001-05-31 18:47:43 +00:00
return ( $description ) ? " <b> $title :</b><br /> $value <br /><small><i> $description </i></small><p /> \n " : " <b> $title :</b><br /> $value <p /> \n " ;
2001-04-29 12:39:55 +00:00
}
function form_textfield ( $title , $name , $value , $size , $maxlength , $description = 0 ) {
2001-05-31 18:47:43 +00:00
return form_item ( $title , " <input maxlength= \" $maxlength\ " name = \ " edit[ $name ] \" size= \" $size\ " value = \ " " . check_form ( $value ) . " \" /> " , $description );
2001-04-29 12:39:55 +00:00
}
function form_textarea ( $title , $name , $value , $cols , $rows , $description = 0 ) {
2001-05-31 18:47:43 +00:00
return form_item ( $title , " <textarea wrap= \" virtual \" cols= \" $cols\ " rows = \ " $rows\ " name = \ " edit[ $name ] \" > " . check_form ( $value ) . " </textarea> " , $description );
2001-04-29 12:39:55 +00:00
}
2001-06-18 20:51:58 +00:00
function form_select ( $title , $name , $value , $options , $description = 0 , $extra = 0 ) {
2001-06-04 15:06:47 +00:00
if ( count ( $options ) > 0 ) {
2001-06-18 20:51:58 +00:00
foreach ( $options as $key => $choice ) $select .= " <option value= \" $key\ " " . (is_array( $value ) ? (in_array( $key , $value ) ? " selected " : " " ) : ( $key == $value ? " selected " : " " )) . " > " . check_form( $choice ) . " </ option > " ;
return form_item ( $title , " <select name= \" edit[ $name ] \" " . ( $extra ? " $extra " : " " ) . " > $select </select> " , $description );
2001-06-04 15:06:47 +00:00
}
2001-04-29 12:39:55 +00:00
}
2001-05-18 09:17:00 +00:00
function form_file ( $title , $name , $size , $description = 0 ) {
2001-05-31 18:47:43 +00:00
return form_item ( $title , " <input type= \" file \" name= \" edit[ $name ] \" size= \" $size\ " /> \n " , $description );
2001-04-29 12:39:55 +00:00
}
2001-05-18 09:17:00 +00:00
function form_hidden ( $name , $value ) {
2001-05-31 18:47:43 +00:00
return " <input type= \" hidden \" name= \" edit[ $name ] \" value= \" " . check_form ( $value ) . " \" /> \n " ;
2001-05-17 11:49:19 +00:00
}
2001-04-29 12:39:55 +00:00
function form_submit ( $value ) {
2001-05-31 18:47:43 +00:00
return " <input type= \" submit \" name= \" op \" value= \" " . check_form ( $value ) . " \" /> \n " ;
2001-04-29 12:39:55 +00:00
}
2001-05-17 20:50:15 +00:00
function field_get ( $string , $name ) {
2001-06-24 18:33:13 +00:00
ereg ( " , $name =([^,]+) " , " , $string " , $regs );
return $regs [ 1 ];
2001-05-16 20:54:37 +00:00
}
2001-05-17 20:50:15 +00:00
function field_set ( $string , $name , $value ) {
2001-06-24 18:33:13 +00:00
$rval = ereg_replace ( " , $name =[^,]+ " , " " , " , $string " );
if ( $value ) $rval .= ( $rval == " , " ? " " : " , " ) . " $name = $value " ;
return substr ( $rval , 1 );
2001-05-16 20:54:37 +00:00
}
2001-05-25 16:03:02 +00:00
function field_merge ( $a , $b ) {
2001-06-11 20:01:13 +00:00
foreach ( explode ( " , " , $b ) as $data ) {
$entry = explode ( " = " , $data );
2001-05-25 16:03:02 +00:00
$a = field_set ( $a , $entry [ 0 ], $entry [ 1 ]);
}
return $a ;
}
2001-06-29 22:08:57 +00:00
function link_page () {
$links [] = " <a href= \" index.php \" > " . t ( " home " ) . " </a> " ;
$links [] = " <a href= \" submit.php \" > " . t ( " submit " ) . " </a> " ;
$links [] = " <a href= \" account.php \" > " . t ( " account " ) . " </a> " ;
foreach ( module_list () as $name ) {
if ( module_hook ( $name , " link " )) {
$links = array_merge ( $links , module_invoke ( $name , " link " , " page " ));
}
}
return $links ;
}
function link_node ( $node ) {
2001-07-14 12:12:41 +00:00
foreach ( module_list () as $name ) {
if ( module_hook ( $name , " link " )) {
$links = array_merge ( $links , module_invoke ( $name , " link " , " node " , $node ));
}
}
return $links ? $links : array ();
2001-06-29 22:08:57 +00:00
}
2001-05-20 13:51:40 +00:00
function timer_start () {
global $timer ;
$timer = explode ( " " , microtime ());
}
function timer_print () {
global $timer ;
$stop = explode ( " " , microtime ());
$diff = $stop [ 0 ] - $timer [ 0 ];
print " <PRE>execution time: $diff ms</PRE> " ;
}
function page_header () {
2001-06-17 20:35:48 +00:00
global $user ;
if ( variable_get ( " dev_timer " , 0 )) {
timer_start ();
}
2001-06-30 20:23:33 +00:00
if ( variable_get ( " cache " , 0 )) {
if ( $data = cache_get ()) {
print $data ;
exit ();
}
}
2001-05-20 13:51:40 +00:00
}
function page_footer () {
2001-06-17 20:35:48 +00:00
if ( variable_get ( " dev_timer " , 0 )) {
timer_print ();
}
2001-07-01 11:27:10 +00:00
2001-06-30 20:23:33 +00:00
if ( variable_get ( " cache " , 0 )) {
cache_set ();
}
2001-05-20 13:51:40 +00:00
}
2001-02-18 15:14:56 +00:00
$conf = conf_init ();
2001-02-04 22:09:38 +00:00
2001-05-14 19:41:18 +00:00
include_once " includes/ $conf .php " ;
2000-12-23 23:25:28 +00:00
include_once " includes/database.inc " ;
2001-04-05 20:33:36 +00:00
include_once " includes/variable.inc " ;
2001-03-18 08:44:01 +00:00
include_once " includes/comment.inc " ;
2001-02-10 11:59:06 +00:00
include_once " includes/module.inc " ;
2001-02-17 12:59:24 +00:00
include_once " includes/locale.inc " ;
include_once " includes/search.inc " ;
2000-12-23 23:25:28 +00:00
include_once " includes/theme.inc " ;
include_once " includes/user.inc " ;
2001-03-24 17:04:11 +00:00
include_once " includes/node.inc " ;
2000-12-23 23:25:28 +00:00
2001-06-24 11:38:08 +00:00
// initialize configuration variables:
$conf = variable_init ();
2001-05-06 17:19:58 +00:00
// initialize user session:
2001-02-18 15:14:56 +00:00
user_init ();
2001-05-06 17:19:58 +00:00
// initialize installed modules:
2001-05-05 13:57:29 +00:00
module_init ();
2001-05-06 17:19:58 +00:00
// initialize localization system:
2001-02-18 15:14:56 +00:00
$locale = locale_init ();
2001-05-06 17:19:58 +00:00
// initialize theme:
2001-02-18 15:14:56 +00:00
$theme = theme_init ();
2000-12-23 23:25:28 +00:00
2001-05-06 17:19:58 +00:00
// set error handler:
set_error_handler ( " error_handler " );
2001-05-17 19:14:50 +00:00
?>