2004-02-02 23:34:39 +00:00
< ? php
2008-08-05 22:43:14 +00:00
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
2004-02-02 23:34:39 +00:00
2008-06-10 18:12:29 +00:00
require_once ( " ../inc/util_basic.inc " );
2004-02-02 23:34:39 +00:00
require_once ( " ../project/project.inc " );
require_once ( " ../inc/countries.inc " );
2004-09-14 20:45:17 +00:00
require_once ( " ../inc/db.inc " );
2007-10-26 21:14:35 +00:00
require_once ( " ../inc/boinc_db.inc " );
2005-10-01 16:07:11 +00:00
require_once ( " ../inc/translation.inc " );
2007-12-23 23:09:10 +00:00
require_once ( " ../inc/profile.inc " );
2004-09-14 20:45:17 +00:00
2010-10-08 23:18:12 +00:00
// don't allow /... at the end of URL
if ( array_key_exists ( " PATH_INFO " , $_SERVER )) {
die ( " bad URL " );
}
2011-11-14 23:31:12 +00:00
ini_set ( " memory_limit " , " 256M " );
2008-08-15 22:07:24 +00:00
2010-09-15 23:47:49 +00:00
if ( ! defined ( 'SECURE_URL_BASE' )) {
define ( 'SECURE_URL_BASE' , URL_BASE );
}
2012-05-18 17:57:14 +00:00
// define TIMEZONE in project.inc
//
if ( defined ( 'TIMEZONE' )) {
date_default_timezone_set ( TIMEZONE );
} else {
date_default_timezone_set ( 'UTC' );
}
2006-09-06 20:56:55 +00:00
$generating_xml = false ;
2008-04-24 16:16:36 +00:00
$caching = false ;
2011-04-26 19:04:11 +00:00
$did_page_head = false ;
2006-09-06 20:56:55 +00:00
2008-02-15 15:25:44 +00:00
function send_cookie ( $name , $value , $permanent , $ops = false ) {
2008-02-13 19:02:44 +00:00
// the following allows independent login for projects on the same server
//
2006-05-09 18:25:15 +00:00
$master_url = parse_config ( get_config (), " <master_url> " );
$url = parse_url ( $master_url );
2004-03-26 18:37:46 +00:00
$path = $url [ 'path' ];
2009-09-15 04:48:08 +00:00
if ( $ops ) {
$path = substr ( $path , 0 , - 1 );
$path .= " _ops/ " ;
}
2008-02-13 19:02:44 +00:00
$expire = $permanent ? time () + 3600 * 24 * 365 : 0 ;
setcookie ( $name , $value , $expire , $path );
}
2008-01-28 15:48:09 +00:00
2009-09-15 04:48:08 +00:00
function clear_cookie ( $name , $ops = false ) {
2008-02-13 19:02:44 +00:00
$master_url = parse_config ( get_config (), " <master_url> " );
$url = parse_url ( $master_url );
$path = $url [ 'path' ];
2009-09-15 04:48:08 +00:00
if ( $ops ) {
$path = substr ( $path , 0 , - 1 );
$path .= " _ops/ " ;
}
2008-02-13 19:02:44 +00:00
setcookie ( $name , '' , time () - 3600 , $path );
2004-02-02 23:34:39 +00:00
}
function get_user_from_id ( $id ) {
if ( $id ) return lookup_user_id ( $id );
return NULL ;
}
2007-01-29 18:15:16 +00:00
$g_logged_in_user = null ;
2011-04-20 14:50:50 +00:00
$got_logged_in_user = false ;
2007-01-29 18:15:16 +00:00
2004-02-03 21:48:49 +00:00
function get_logged_in_user ( $must_be_logged_in = true ) {
2012-06-03 17:31:00 +00:00
global $g_logged_in_user , $got_logged_in_user ;
2011-04-20 14:50:50 +00:00
if ( $got_logged_in_user ) return $g_logged_in_user ;
2008-01-28 15:48:09 +00:00
2009-07-24 19:09:37 +00:00
check_web_stopped ();
2008-07-08 21:13:27 +00:00
2008-02-13 19:02:44 +00:00
$authenticator = null ;
if ( isset ( $_COOKIE [ 'auth' ])) $authenticator = $_COOKIE [ 'auth' ];
2008-12-14 22:18:49 +00:00
$authenticator = BoincDb :: escape_string ( $authenticator );
2007-10-26 21:14:35 +00:00
if ( $authenticator ) {
2007-10-29 16:38:25 +00:00
$g_logged_in_user = BoincUser :: lookup ( " authenticator=' $authenticator ' " );
2007-10-26 21:14:35 +00:00
}
2010-08-29 10:24:39 +00:00
if ( $must_be_logged_in && ! $g_logged_in_user ) {
2011-02-13 19:00:08 +00:00
$next_url = '' ;
if ( array_key_exists ( 'REQUEST_URI' , $_SERVER )) {
$next_url = $_SERVER [ 'REQUEST_URI' ];
$n = strrpos ( $next_url , " / " );
if ( $n ) {
$next_url = substr ( $next_url , $n + 1 );
}
2010-08-29 10:24:39 +00:00
}
2011-05-04 15:37:09 +00:00
$next_url = urlencode ( $next_url );
2010-08-29 10:24:39 +00:00
Header ( " Location: login_form.php?next_url= $next_url " );
2011-02-08 21:38:16 +00:00
exit ;
2004-02-02 23:34:39 +00:00
}
2012-06-03 17:31:00 +00:00
$got_logged_in_user = true ;
2007-01-29 18:15:16 +00:00
return $g_logged_in_user ;
2004-02-02 23:34:39 +00:00
}
2011-04-20 16:06:35 +00:00
function show_login_info ( $prefix = " " ) {
2011-04-20 14:50:50 +00:00
$user = get_logged_in_user ( false );
echo "
2011-08-29 05:37:52 +00:00
< table width = \ " 100% \" cellpadding=0 cellspacing=0>
< tr >< td align = right >
2011-04-20 14:50:50 +00:00
" ;
2004-02-02 23:34:39 +00:00
if ( $user ) {
2011-04-20 14:50:50 +00:00
$url_tokens = url_tokens ( $user -> authenticator );
2012-04-27 06:10:08 +00:00
echo " <nobr> $user->name | <a href= " . $prefix . " logout.php? $url_tokens > " . tra ( " log out " ) . " </a></nobr> " ;
2004-02-02 23:34:39 +00:00
} else {
2011-04-20 14:50:50 +00:00
echo " <a href= " . $prefix . " login_form.php> " . tra ( " log in " ) . " </a> " ;
2004-02-02 23:34:39 +00:00
}
2011-04-20 14:50:50 +00:00
echo "
</ td >
</ tr >
</ table >
" ;
2004-02-02 23:34:39 +00:00
}
2008-06-12 04:50:10 +00:00
$cache_control_extra = " " ;
2007-06-22 03:46:42 +00:00
// Page_head() is overridable so that projects that want to integrate BOINC
// with an existing web framework can more easily do so.
2008-04-24 16:16:36 +00:00
// To do so, define page_head() in the project include file.
2007-06-22 03:46:42 +00:00
//
2006-08-22 08:33:15 +00:00
if ( ! function_exists ( " page_head " )){
2009-06-23 17:13:35 +00:00
function page_head (
$title , $java_onload = null , $title_plain = null , $prefix = " " , $head_extra = null
) {
2011-04-26 19:04:11 +00:00
global $caching , $cache_control_extra , $did_page_head ;
2008-04-24 16:16:36 +00:00
2011-08-26 18:30:13 +00:00
$did_page_head = true ;
2008-02-01 23:11:09 +00:00
$stylesheet = URL_BASE . STYLESHEET ;
2004-10-20 05:45:43 +00:00
$rssname = PROJECT . " RSS 2.0 " ;
2007-11-18 22:42:47 +00:00
$rsslink = URL_BASE . " rss_main.php " ;
2012-03-09 21:40:57 +00:00
if ( defined ( 'STYLESHEET2' )) {
$stylesheet2 = URL_BASE . STYLESHEET2 ;
} else {
$stylesheet2 = null ;
}
2010-11-04 18:20:57 +00:00
2010-01-27 04:04:17 +00:00
header ( " Content-type: text/html; charset=utf-8 " );
2008-04-24 16:16:36 +00:00
if ( ! $caching ) {
header ( " Expires: Mon, 26 Jul 1997 05:00:00 UTC " ); // Date in the past
header ( " Last-Modified: " . gmdate ( " D, d M Y H:i:s " ) . " UTC " ); // always modified
2008-06-12 04:50:10 +00:00
header ( " Cache-Control: $cache_control_extra no-cache, must-revalidate, post-check=0, pre-check=0 " ); // HTTP/1.1
2008-04-24 16:16:36 +00:00
header ( " Pragma: no-cache " ); // HTTP/1.0
}
2010-11-04 18:20:57 +00:00
2009-04-15 20:09:42 +00:00
echo " <!DOCTYPE html PUBLIC \" -//W3C//DTD HTML 4.01 Transitional//EN \" \" http://www.w3.org/TR/html4/loose.dtd \" > " ;
2007-10-22 19:36:01 +00:00
2011-08-28 21:27:52 +00:00
echo " <html><head> \n " ;
if ( $head_extra ) {
echo " \n $head_extra\n " ;
}
2005-10-29 05:30:57 +00:00
if ( ! $title_plain ) {
2011-08-28 21:27:52 +00:00
echo " <title> " . sanitize_tags ( $title ) . " </title> \n " ;
2005-10-29 05:30:57 +00:00
} else {
2011-08-28 21:27:52 +00:00
echo " <title> " . sanitize_tags ( $title_plain ) . " </title> \n " ;
2005-10-29 05:30:57 +00:00
}
2011-08-29 05:37:52 +00:00
echo " <link rel=stylesheet type= \" text/css \" href= \" " . URL_BASE . " main.css \" media= \" all \" >
2010-11-04 18:20:57 +00:00
< link rel = stylesheet type = \ " text/css \" href= \" $stylesheet\ " >
2012-03-09 21:40:57 +00:00
" ;
if ( $stylesheet2 ) {
echo " <link rel=stylesheet type= \" text/css \" href= \" $stylesheet2\ " > \n " ;
}
echo "
2008-08-16 20:59:53 +00:00
< link rel = alternate type = \ " application/rss+xml \" title= \" $rssname\ " href = \ " $rsslink\ " >
2007-10-22 19:36:01 +00:00
</ head >
" ;
2005-10-17 03:03:48 +00:00
if ( $java_onload ){
2008-08-05 15:06:42 +00:00
echo " <body onload= \" " . $java_onload . " \" > " ;
2007-11-07 17:23:29 +00:00
} else {
2008-08-05 15:06:42 +00:00
echo " <body> " ;
2005-10-17 03:03:48 +00:00
}
2005-01-12 13:25:33 +00:00
display_cvs_versions ();
2011-04-20 14:50:50 +00:00
2005-10-17 03:03:48 +00:00
project_banner ( $title , $prefix );
2011-04-20 15:30:35 +00:00
switch ( $title ) { //kludge
case tra ( " Log in " ) :
case tra ( " Create an account " ) :
2011-05-05 14:51:10 +00:00
case tra ( " Server status page " ) :
2011-04-20 15:30:35 +00:00
break ;
default :
2011-04-20 16:06:35 +00:00
show_login_info ( $prefix );
2011-04-20 15:30:35 +00:00
}
2011-04-20 14:50:50 +00:00
2004-02-02 23:34:39 +00:00
}
2006-08-22 08:33:15 +00:00
}
2004-02-02 23:34:39 +00:00
2011-08-28 05:15:16 +00:00
// the following must be included in header
// for Recaptcha to work with some IE browsers
//
define ( 'IE_COMPAT_MODE' , '<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" >' );
2005-10-29 05:30:57 +00:00
function page_tail_aux ( $show_return , $show_date , $prefix = " " ) {
project_footer ( $show_return , $show_date , $prefix );
2008-08-16 20:59:53 +00:00
echo " </body>
</ html >
" ;
2004-11-18 20:01:12 +00:00
}
function page_tail_main ( $show_date = false ) {
page_tail_aux ( false , $show_date );
}
2004-02-02 23:34:39 +00:00
2007-11-06 18:25:44 +00:00
// See the comments for page_head()
//
2006-08-22 08:33:15 +00:00
if ( ! function_exists ( " page_tail " )){
2005-10-17 03:03:48 +00:00
function page_tail ( $show_date = false , $prefix = " " ) {
page_tail_aux ( true , $show_date , $prefix );
2004-02-02 23:34:39 +00:00
}
2006-08-22 08:33:15 +00:00
}
2004-02-02 23:34:39 +00:00
2005-01-12 13:25:33 +00:00
function display_cvs_versions (){
global $cvs_version_tracker ;
2007-05-15 10:06:38 +00:00
echo " \n <!-- SVN VERSIONS --> \n " ;
2005-10-29 05:30:57 +00:00
for ( $i = 0 ; $i < sizeof ( $cvs_version_tracker ); $i ++ ) {
echo " <!-- " . $cvs_version_tracker [ $i ] . " --> \n " ;
2005-01-12 13:25:33 +00:00
}
}
2004-02-02 23:34:39 +00:00
function db_error_page () {
page_head ( " Database error " );
2010-09-07 18:20:29 +00:00
echo tra ( " A database error occurred while handling your request; please try again later. " );
2004-02-02 23:34:39 +00:00
page_tail ();
}
2004-11-21 18:56:30 +00:00
function error_page ( $msg ) {
2006-09-06 20:56:55 +00:00
global $generating_xml ;
if ( $generating_xml ) {
xml_error ( - 1 , $msg );
}
2010-02-16 01:06:03 +00:00
page_head ( tra ( " Unable to handle request " ));
2004-11-21 18:56:30 +00:00
echo $msg ;
page_tail ();
exit ();
}
2004-11-29 11:56:25 +00:00
// takes argument in second and returns a human formatted time string
// in the form D days + h Hours + m Min + s sec.
function time_diff ( $x ) {
2005-10-29 05:30:57 +00:00
$days = ( int )( $x / 86400 );
$hours = ( int )(( $x - $days * 86400 ) / 3600 );
$minutes = ( int )(( $x - $days * 86400 - $hours * 3600 ) / 60 );
$seconds = ( int )( $x % 60 );
$datestring = " " ;
if ( $days ) {
2010-09-07 18:20:29 +00:00
$datestring .= " $days " . tra ( " days " ) . " " ;
2005-10-29 05:30:57 +00:00
}
if ( $hours || strlen ( $datestring )) {
2010-09-07 18:20:29 +00:00
$datestring .= " $hours " . tra ( " hours " ) . " " ;
2005-10-29 05:30:57 +00:00
}
if ( $minutes || strlen ( $datestring )) {
2010-09-07 18:20:29 +00:00
$datestring .= " $minutes " . tra ( " min " ) . " " ;
2005-10-29 05:30:57 +00:00
}
if ( $seconds ) {
2010-09-07 18:20:29 +00:00
$datestring .= " $seconds " . tra ( " sec " ) . " " ;
2005-10-29 05:30:57 +00:00
}
return $datestring ;
2004-11-29 11:56:25 +00:00
}
2004-02-02 23:34:39 +00:00
function date_str ( $x ) {
if ( $x == 0 ) return " --- " ;
2005-01-08 19:45:26 +00:00
return gmdate ( 'j M Y' , $x );
2004-02-02 23:34:39 +00:00
}
function time_str ( $x ) {
if ( $x == 0 ) return " --- " ;
2010-11-03 21:48:39 +00:00
return gmdate ( 'j M Y | G:i:s' , $x ) . " UTC " ;
2004-02-02 23:34:39 +00:00
}
2011-07-27 06:20:48 +00:00
function local_time_str ( $x ) {
if ( $x == 0 ) return " --- " ;
2011-07-29 00:07:20 +00:00
return date ( 'j M Y, H:i T' , $x );
2011-07-27 06:20:48 +00:00
}
2004-02-02 23:34:39 +00:00
function pretty_time_str ( $x ) {
2004-03-23 01:44:13 +00:00
return time_str ( $x );
2004-02-02 23:34:39 +00:00
}
2008-10-30 18:27:22 +00:00
2007-10-22 19:36:01 +00:00
function start_table ( $extra = " width= \" 100% \" " ) {
2008-10-30 18:27:22 +00:00
echo " <table class=bordered $extra > " ;
2004-02-02 23:34:39 +00:00
}
function start_table_noborder ( $width = " 100% " ) {
2008-10-30 18:27:22 +00:00
echo " <table cellpadding=5 width= \" $width\ " > " ;
2004-02-02 23:34:39 +00:00
}
function end_table () {
echo " </table> \n " ;
}
2007-11-05 23:55:33 +00:00
// Table header row with unlimited number of columns
2007-08-26 12:01:41 +00:00
function table_header () {
echo " <tr> \n " ;
for ( $i = 0 ; $i < func_num_args (); $i ++ ) {
if ( is_array ( func_get_arg ( $i ))) {
$col = func_get_arg ( $i );
echo " <th " . $col [ 1 ] . " > " . $col [ 0 ] . " </th> \n " ;
} else {
echo " <th> " . func_get_arg ( $i ) . " </th> \n " ;
}
}
echo " </tr> \n " ;
}
2007-11-05 23:55:33 +00:00
// Table row with unlimited number of columns
2007-08-26 12:01:41 +00:00
function table_row () {
echo " <tr> \n " ;
for ( $i = 0 ; $i < func_num_args (); $i ++ ) {
if ( is_array ( func_get_arg ( $i ))) {
$col = func_get_arg ( $i );
echo " <td " . $col [ 1 ] . " > " . $col [ 0 ] . " </td> \n " ;
} else {
echo " <td> " . func_get_arg ( $i ) . " </td> \n " ;
}
}
echo " </tr> \n " ;
}
2004-12-16 19:37:26 +00:00
function row1 ( $x , $ncols = 2 , $class = " heading " ) {
2007-10-30 19:36:27 +00:00
echo " <tr><td class= \" $class\ " colspan = \ " $ncols\ " > $x </ td ></ tr > \n " ;
2004-02-02 23:34:39 +00:00
}
2006-08-13 21:51:15 +00:00
function row2 ( $x , $y , $show_error = false ) {
2004-02-02 23:34:39 +00:00
if ( $x == " " ) $x = " <br> " ;
2011-08-07 22:36:17 +00:00
if ( $y === " " ) $y = " <br> " ;
2006-08-13 21:51:15 +00:00
if ( $show_error ) {
$class1 = 'fieldname_error' ;
$class2 = 'fieldvalue_error' ;
} else {
$class1 = 'fieldname' ;
$class2 = 'fieldvalue' ;
}
2008-08-16 20:59:53 +00:00
echo " <tr><td width= \" 40% \" class= $class1 > $x </td><td class= $class2 > $y </td></tr> \n " ;
2004-02-02 23:34:39 +00:00
}
2006-08-13 21:51:15 +00:00
2004-02-02 23:34:39 +00:00
function row2_init ( $x , $y ) {
2008-08-16 20:59:53 +00:00
echo " <tr><td class=fieldname width= \" 40% \" > $x </td><td class=fieldvalue> $y\n " ;
2004-02-02 23:34:39 +00:00
}
function row2_plain ( $x , $y ) {
echo " <tr><td> $x </td><td> $y </td></tr> \n " ;
}
function row3 ( $x , $y , $z ) {
2008-08-16 20:59:53 +00:00
echo " <tr><td width= \" 30% \" align= \" right \" > $x </td><td> $y </td><td> $z </td></tr> \n " ;
2004-02-02 23:34:39 +00:00
}
2004-09-23 00:32:10 +00:00
function row4 ( $xx , $xy , $yx , $yy ) {
2008-08-16 20:59:53 +00:00
echo " <tr><td width= \" 25% \" > $xx </td><td width= \" 25% \" > $xy </td> "
2008-08-05 15:06:42 +00:00
. " <td width= \" 25% \" > $yx </td><td width= \" %25 \" > $yy </td></tr>
2005-04-07 20:46:25 +00:00
" ;
2004-09-23 00:12:19 +00:00
}
2004-02-02 23:34:39 +00:00
function rowify ( $string ) {
echo " <tr><td> $string </td></tr> " ;
}
2005-04-08 00:06:52 +00:00
function row_array ( $x ) {
2005-04-07 20:46:25 +00:00
echo " <tr> " ;
foreach ( $x as $h ) {
2006-01-31 22:21:11 +00:00
echo " <td> $h </td> " ;
2005-04-07 20:46:25 +00:00
}
echo " </tr> \n " ;
}
2005-04-08 00:06:52 +00:00
function row_heading_array ( $x ) {
2005-04-07 20:46:25 +00:00
echo " <tr> " ;
foreach ( $x as $h ) {
2008-08-07 20:43:52 +00:00
echo " <th> $h </th> " ;
2005-04-07 20:46:25 +00:00
}
echo " </tr> \n " ;
}
2004-02-02 23:34:39 +00:00
function random_string () {
2004-04-20 05:05:52 +00:00
return md5 ( uniqid ( rand (), true ));
2004-02-02 23:34:39 +00:00
}
2007-01-29 18:15:16 +00:00
function url_tokens ( $auth ) {
$now = time ();
$ttok = md5 (( string ) $now . $auth );
2007-10-30 19:36:27 +00:00
return " &tnow= $now &ttok= $ttok " ;
2007-01-29 18:15:16 +00:00
}
function form_tokens ( $auth ) {
$now = time ();
$ttok = md5 (( string ) $now . $auth );
2007-10-30 19:36:27 +00:00
return " <input type= \" hidden \" name= \" tnow \" value= \" $now\ " >
< input type = \ " hidden \" name= \" ttok \" value= \" $ttok\ " >
2007-01-29 18:15:16 +00:00
" ;
}
function valid_tokens ( $auth ) {
2007-10-29 16:38:25 +00:00
$tnow = get_str ( 'tnow' , true );
$ttok = get_str ( 'ttok' , true );
2007-10-28 15:03:14 +00:00
if ( ! $tnow ) {
$tnow = $_POST [ 'tnow' ];
}
if ( ! $ttok ) {
$ttok = $_POST [ 'ttok' ];
}
2007-01-29 18:15:16 +00:00
if ( ! $tnow ) return false ;
if ( ! $ttok ) return false ;
$t = md5 (( string ) $tnow . $auth );
if ( $t != $ttok ) return false ;
if ( time () > $tnow + 86400 ) return false ;
return true ;
}
function check_tokens ( $auth ) {
if ( valid_tokens ( $auth )) return ;
error_page (
2010-09-07 18:20:29 +00:00
tra ( " Link has timed out. Please click Back, refresh the page, and try again. " )
2007-01-29 18:15:16 +00:00
);
}
2008-05-15 22:05:05 +00:00
function no_computing () {
return parse_bool ( get_config (), " no_computing " );
}
2004-02-02 23:34:39 +00:00
// Generates a legal filename from a parameter string.
function get_legal_filename ( $name ) {
2009-09-06 05:33:19 +00:00
return strtr ( $name , array ( ',' => '' , ' ' => '_' ));
2004-02-02 23:34:39 +00:00
}
// Returns a string containing as many words
// (being collections of characters separated by the character $delimiter)
// as possible such that the total string length is <= $chars characters long.
// If $ellipsis is true, then an ellipsis is added to any sentence which
// is cut short.
function sub_sentence ( $sentence , $delimiter , $max_chars , $ellipsis = false ) {
$words = explode ( $delimiter , $sentence );
$total_chars = 0 ;
2008-09-22 17:14:03 +00:00
$trunc = false ;
$result = null ;
2004-02-02 23:34:39 +00:00
2008-09-22 17:14:03 +00:00
foreach ( $words as $word ) {
if ( strlen ( $result ) + strlen ( $word ) > $max_chars ) {
$trunc = true ;
break ;
}
if ( $result ) {
$result .= " $word " ;
2004-02-02 23:34:39 +00:00
} else {
2008-09-22 17:14:03 +00:00
$result = $word ;
2004-02-02 23:34:39 +00:00
}
2008-09-22 17:14:03 +00:00
}
2004-02-02 23:34:39 +00:00
2008-09-22 17:14:03 +00:00
if ( $ellipsis && $trunc ) {
$result .= " ... " ;
2004-02-02 23:34:39 +00:00
}
return $result ;
}
2007-10-07 19:27:59 +00:00
// use this for user RAC and result credit
//
2004-12-15 23:50:00 +00:00
function format_credit ( $x ) {
2007-10-07 19:27:59 +00:00
return number_format ( $x , 2 );
}
// use this when credit is likely to be large, e.g. team RAC
//
function format_credit_large ( $x ) {
2007-10-02 15:32:28 +00:00
return number_format ( $x , 0 );
2004-02-02 23:34:39 +00:00
}
2008-01-01 22:29:10 +00:00
function friend_links ( $user ) {
2007-12-02 21:11:17 +00:00
if ( is_banished ( $user )) {
2008-01-01 22:29:10 +00:00
return " " ;
2007-12-02 21:11:17 +00:00
}
2008-08-05 15:06:42 +00:00
$x = " <table height= \" 100 \" width= \" 150 \" border= \" 0 \" cellpadding= \" 4 \" ><tr><td class= \" friend \" > " ;
2004-02-02 23:34:39 +00:00
if ( $user -> has_profile ) {
2008-01-01 22:29:10 +00:00
$profile = BoincProfile :: lookup_fields ( " has_picture " , " userid= $user->id " );
if ( $profile && $profile -> has_picture ) {
$img_url = profile_thumb_url ( $user -> id );
2007-12-23 23:09:10 +00:00
} else {
$img_url = URL_BASE . " img/head_20.png " ;
}
2010-09-07 18:20:29 +00:00
$title = tra ( " View the profile of %1 " , $user -> name );
$alt = tra ( " Profile " );
2011-02-10 22:45:39 +00:00
$x .= ' <a href="' . URL_BASE . 'view_profile.php?userid=' . $user -> id . '"><img title="' . $title . '" src="' . $img_url . '" alt="' . $alt . '"></a><br>' ;
2008-01-01 22:29:10 +00:00
}
$x .= " <a href= \" " . URL_BASE . " show_user.php?userid= " . $user -> id . " \" > " . $user -> name . " </a> " ;
2010-11-04 18:20:57 +00:00
if ( $user -> donated == 1 ) {
2008-01-01 22:29:10 +00:00
require_once ( " ../project/donations.inc " );
$x .= DONATION_LINK ;
}
$x .= " </td></tr></table> \n " ;
return $x ;
}
function user_links ( $user ) {
BoincForumPrefs :: lookup ( $user );
if ( is_banished ( $user )) {
return " (banished: ID $user->id ) " ;
}
2009-02-08 17:35:54 +00:00
$x = " " ;
2008-01-01 22:29:10 +00:00
if ( $user -> has_profile ) {
$img_url = URL_BASE . " img/head_20.png " ;
2011-02-10 22:45:39 +00:00
$x .= ' <a href="' . URL_BASE . 'view_profile.php?userid=' . $user -> id . '"><img title="View the profile of ' . $user -> name . '" src="' . $img_url . '" alt="Profile"></a>' ;
2004-02-02 23:34:39 +00:00
}
2007-12-24 03:45:20 +00:00
$x .= " <a href= \" " . URL_BASE . " show_user.php?userid= " . $user -> id . " \" > " . $user -> name . " </a> " ;
2010-11-04 18:20:57 +00:00
if ( $user -> donated == 1 ) {
2005-09-15 20:45:11 +00:00
require_once ( " ../project/donations.inc " );
$x .= DONATION_LINK ;
}
2009-05-03 19:17:20 +00:00
if ( function_exists ( " project_user_links " )){
$x .= project_user_links ( $user );
}
2004-02-02 23:34:39 +00:00
return $x ;
}
2004-02-05 21:35:48 +00:00
function host_link ( $hostid ) {
if ( $hostid ) {
2008-08-05 15:06:42 +00:00
return " <a href= \" show_host_detail.php?hostid= $hostid\ " > $hostid </ a > " ;
2004-02-05 21:35:48 +00:00
} else {
return " --- " ;
}
}
2004-06-15 18:50:57 +00:00
function open_output_buffer () {
ob_start ();
ob_implicit_flush ( 0 );
}
function close_output_buffer ( $filename ) {
$fh = fopen ( $filename , " w " );
$page = ob_get_contents ();
ob_end_clean ();
fwrite ( $fh , $page );
fclose ( $fh );
}
2005-01-30 20:15:18 +00:00
function html_info () {
2012-03-04 20:37:10 +00:00
return " <br><a href=bbcode.php target=new><span class=smalltext> " . tra ( " Use BBCode tags to format your text " ) . " </span></a> \n " ;
2005-01-30 20:15:18 +00:00
}
2008-06-05 20:32:21 +00:00
// strip slashes if magic quotes in effect
function undo_magic_quotes ( $x ) {
if ( get_magic_quotes_gpc ()) {
return stripslashes ( $x );
}
return $x ;
}
2011-02-09 19:10:10 +00:00
// check for bogus GET args
//
function check_get_args ( $args ) {
foreach ( $_GET as $key => $val ) {
if ( ! in_array ( $key , $args )) {
2011-02-09 22:11:34 +00:00
Header ( " Location: extra_arg_ $key .html " );
die ;
2011-02-09 19:10:10 +00:00
}
}
}
2005-02-13 06:13:33 +00:00
function get_int ( $name , $optional = false ) {
2005-05-11 09:48:07 +00:00
$x = null ;
if ( isset ( $_GET [ $name ])) $x = $_GET [ $name ];
2005-02-13 06:13:33 +00:00
if ( ! is_numeric ( $x )) {
if ( $optional ) {
2011-02-09 22:11:34 +00:00
if ( $x ) {
Header ( " Location: non_num_arg.html " );
die ;
}
2005-02-13 06:13:33 +00:00
return null ;
} else {
2011-05-04 15:37:09 +00:00
Header ( " Location: missing_arg_ $name .html " );
2011-02-09 22:11:34 +00:00
die ;
2005-02-13 06:13:33 +00:00
}
}
return ( int ) $x ;
}
function post_int ( $name , $optional = false ) {
2005-05-11 10:30:28 +00:00
$x = null ;
if ( isset ( $_POST [ $name ])) $x = $_POST [ $name ];
2005-02-13 06:13:33 +00:00
if ( ! is_numeric ( $x )) {
if ( $optional ) {
return null ;
} else {
2007-09-13 09:46:36 +00:00
error_page ( " missing or bad parameter: $name ; supplied: " . htmlspecialchars ( $x ));
2005-02-13 06:13:33 +00:00
}
}
return ( int ) $x ;
}
function get_str ( $name , $optional = false ) {
2011-10-17 06:13:51 +00:00
if ( isset ( $_GET [ $name ])) {
$x = $_GET [ $name ];
} else {
2008-10-30 18:27:22 +00:00
if ( ! $optional ) {
error_page ( " missing or bad parameter: $name " );
}
$x = null ;
2005-02-13 06:13:33 +00:00
}
2008-06-11 19:36:10 +00:00
return undo_magic_quotes ( $x );
2005-02-13 06:13:33 +00:00
}
function post_str ( $name , $optional = false ) {
2011-10-17 06:13:51 +00:00
if ( isset ( $_POST [ $name ])) {
$x = $_POST [ $name ];
} else {
if ( ! $optional ) {
error_page ( " missing or bad parameter: $name " );
}
$x = null ;
2005-02-13 06:13:33 +00:00
}
2008-06-11 19:36:10 +00:00
return undo_magic_quotes ( $x );
2005-02-13 06:13:33 +00:00
}
2005-10-12 22:51:55 +00:00
function is_ascii ( $str ) {
// the mb_* functions are not included by default
// return (mb_detect_encoding($passwd) -= 'ASCII');
for ( $i = 0 ; $i < strlen ( $str ); $i ++ ) {
$c = ord ( substr ( $str , $i ));
if ( $c < 32 || $c > 127 ) return false ;
}
return true ;
}
2006-02-20 19:11:51 +00:00
2006-08-13 21:51:15 +00:00
// This function replaces some often made mistakes while entering numbers
// and gives back an error if there are false characters
// It will also be checked if the value is within certain borders
// @param string &$value reference to the value that should be verified
// @param double $low the lowest number of value if verified
// @param double $high the highest number of value if verified
2007-12-24 21:34:21 +00:00
// @return bool true if $value is numeric and within the defined borders,
// false if $value is not numeric, no changes were made in this case
2006-08-14 17:04:57 +00:00
//
2006-08-13 21:51:15 +00:00
function verify_numeric ( & $value , $low , $high = false ) {
$number = trim ( $value );
$number = str_replace ( 'o' , '0' , $number );
$number = str_replace ( 'O' , '0' , $number );
$number = str_replace ( 'x' , '' , $number ); //if someone enters '0x100'
$number = str_replace ( ',' , '.' , $number ); // replace the german decimal separator
// if no value was entered and this is ok
if ( $number == '' && $low == '' ) return true ;
2006-10-31 17:14:03 +00:00
2006-08-13 21:51:15 +00:00
// the supplied value contains alphabetic characters
if ( ! is_numeric ( $number )) return false ;
2006-10-31 17:14:03 +00:00
2007-12-24 21:34:21 +00:00
//if ($number < $low) $number = $low;
if ( $number < $low ) return false ;
2006-08-13 21:51:15 +00:00
if ( $high ) {
2007-12-24 21:34:21 +00:00
//if ($number > $high) $number = $high;
if ( $number > $high ) return false ;
2006-08-13 21:51:15 +00:00
}
$value = ( double ) $number ;
return true ;
}
2007-01-03 18:07:44 +00:00
// Generate a "select" HTML element from an array of values
function select_from_array ( $name , $array , $selection ) {
$out = " <select name= \" $name\ " > " ;
foreach ( $array as $key => $value ) {
if ( $value ) {
$out .= " <option " ;
if ( $key == $selection ) {
$out .= " selected " ;
}
$out .= " value= \" " . $key . " \" > " . $value . " </option> " ;
}
}
$out .= " </select> " ;
return $out ;
}
2010-11-04 18:20:57 +00:00
// Convert to entities, while preserving already-encoded entities.
2007-05-18 15:02:02 +00:00
// Do NOT use if $str contains valid HTML tags.
2007-07-25 03:17:31 +00:00
//
2010-11-04 18:20:57 +00:00
function boinc_htmlentities ( $str ) {
$str = html_entity_decode ( $str , ENT_COMPAT , " UTF-8 " );
$str = htmlentities ( $str , ENT_COMPAT , " UTF-8 " );
return $str ;
}
2007-05-18 15:02:02 +00:00
2007-06-27 20:44:56 +00:00
function strip_bbcode ( $string ){
return preg_replace ( " /(( \ [.+ \ ])+?)(.+?)(( \ [ \ /.+ \ ])+?)/ " , " " , $string );
}
2007-08-18 16:45:54 +00:00
function current_url () {
$url = " http " ;
if ( isset ( $_SERVER [ 'HTTPS' ]) && $_SERVER [ 'HTTPS' ] == " on " ) {
$url .= " s " ;
}
$url .= " :// " ;
$url .= $_SERVER [ 'SERVER_NAME' ];
$url .= " : " . $_SERVER [ 'SERVER_PORT' ];
if ( isset ( $_SERVER [ 'REQUEST_URI' ])) {
$url .= $_SERVER [ 'REQUEST_URI' ];
} else {
if ( $_SERVER [ 'QUERY_STRING' ]) {
$url .= " ? " . $_SERVER [ 'QUERY_STRING' ];
}
}
return $url ;
}
2009-09-28 04:24:18 +00:00
// Show a single link formatted to look like a button.
// @param url The destination URL of the button
// @param text The text to display on the button
// @param desc The title of the destination - typically used as a popup
// @param class The optional CSS class of the button. Defaults to a standard button
2010-11-04 18:20:57 +00:00
//
2011-07-26 15:13:10 +00:00
function show_button ( $url , $text , $desc = null , $class = " button " ) {
if ( ! $desc ) $desc = $text ;
2011-10-01 16:23:28 +00:00
echo " <a href= \" " . $url . " \" title= \" $desc\ " class = \ " " . $class . " \" > " . $text . " </a> \n " ;
2007-11-01 20:41:24 +00:00
}
2012-01-02 05:53:42 +00:00
function button_text ( $url , $text , $desc = null , $class = " button " ) {
if ( ! $desc ) $desc = $text ;
return " <a href= \" " . $url . " \" title= \" $desc\ " class = \ " " . $class . " \" > " . $text . " </a> \n " ;
}
2007-11-01 20:41:24 +00:00
2009-09-28 04:24:18 +00:00
// When multiple buttons (or actions) are presented in a list you can
// use this convenience method to avoid having to wrap each button in <li></li> elements
// @param url The destination URL of the button
// @param text The text to display on the button
// @param desc The title of the destination - typically used as a popup
// @param class The optional CSS class of the button. Defaults to a standard button
//
2008-09-27 09:51:29 +00:00
function show_actionlist_button ( $url , $text , $desc , $class = " button " ){
echo " <li> " ;
echo show_button ( $url , $text , $desc , $class );
echo " </li> " ;
}
2007-11-23 21:05:52 +00:00
function show_image ( $src , $title , $alt , $height = null ) {
2007-11-01 20:41:24 +00:00
$h = " " ;
if ( $height ) {
$h = " height= \" $height\ " " ;
}
2008-08-05 15:06:42 +00:00
echo " <img border= \" 0 \" title= \" $title\ " alt = \ " $alt\ " src = \ " $src\ " $h > " ;
2007-10-31 23:50:21 +00:00
}
2008-07-01 22:11:28 +00:00
function check_web_stopped () {
2011-04-26 19:04:11 +00:00
global $generating_xml , $did_page_head ;
2008-07-01 22:11:28 +00:00
if ( web_stopped ()) {
if ( $generating_xml ) {
xml_error ( - 183 );
} else {
2011-04-26 19:04:11 +00:00
if ( ! $did_page_head ) {
page_head ( tra ( " Project down for maintenance " ));
}
echo tra (
" %1 is temporarily shut down for maintenance. Please try again later. " ,
PROJECT
);
2008-07-01 22:11:28 +00:00
page_tail ();
exit ();
}
}
}
// Connects to database server and selects database as noted in config.xml
// If only read-only access is necessary,
// tries instead to connect to <replica_db_host> if tag exists.
2009-04-17 02:08:05 +00:00
// DEPRECATED - use boinc_db.inc
2008-07-01 22:11:28 +00:00
//
function db_init ( $try_replica = false ) {
check_web_stopped ();
$retval = db_init_aux ( $try_replica );
if ( $retval == 1 ) {
2010-09-07 18:20:29 +00:00
echo tra ( " Unable to connect to database - please try again later " );
echo tra ( " Error: " ), " " , mysql_errno (), mysql_error ();
2008-07-01 22:11:28 +00:00
exit ();
}
if ( $retval == 2 ) {
2010-09-07 18:20:29 +00:00
echo tra ( " Unable to select database - please try again later " );
2008-07-01 22:11:28 +00:00
echo mysql_error ();
exit ();
}
return 0 ;
}
2009-09-28 04:24:18 +00:00
// return a structure indicating whether project has non-deprecated apps
// for various resource types.
//
function get_app_types () {
$t = null ;
$t -> cpu = false ;
$t -> cuda = false ;
$t -> ati = false ;
$t -> count = 0 ;
$avs = BoincAppVersion :: enum ( " deprecated=0 " );
foreach ( $avs as $av ) {
if ( strstr ( $av -> plan_class , " ati " )) {
$t -> ati = true ;
$t -> count ++ ;
} else if ( strstr ( $av -> plan_class , " cuda " )) {
$t -> cuda = true ;
$t -> count ++ ;
2011-11-03 05:26:19 +00:00
} else if ( strstr ( $av -> plan_class , " nvidia " )) {
$t -> cuda = true ;
$t -> count ++ ;
2009-09-28 04:24:18 +00:00
} else {
$t -> cpu = true ;
$t -> count ++ ;
}
}
return $t ;
}
2010-09-04 22:13:27 +00:00
// Functions to sanitize GET and POST args
// "next_url" arguments (must be local, not full URLs)
2010-08-28 00:06:29 +00:00
//
2010-09-04 22:13:27 +00:00
function sanitize_local_url ( $x ) {
2011-08-01 21:09:29 +00:00
$x = trim ( $x , " / " );
2010-08-29 10:24:39 +00:00
if ( strstr ( $x , " / " )) return " " ;
2010-09-04 22:13:27 +00:00
if ( strstr ( $x , " < " )) return " " ;
if ( strstr ( $x , " \" " )) return " " ;
2010-08-28 00:06:29 +00:00
return $x ;
}
2010-09-04 22:13:27 +00:00
// strip HTML tags
//
function sanitize_tags ( $x ) {
2012-03-04 04:58:41 +00:00
return strip_tags ( $x );
2010-09-04 22:13:27 +00:00
}
function sanitize_numeric ( $x ) {
2010-09-16 18:53:39 +00:00
if ( is_numeric ( $x )) {
return $x ;
} else {
2010-09-16 18:55:48 +00:00
return " not numeric " ;
2010-09-16 18:53:39 +00:00
}
2010-09-04 22:13:27 +00:00
}
2010-09-16 18:53:39 +00:00
function sanitize_email ( $x ) {
if ( function_exists ( 'filter_var' )) {
2010-10-04 23:48:01 +00:00
return filter_var ( $x , FILTER_SANITIZE_EMAIL );
2010-09-16 18:53:39 +00:00
} else {
return strip_tags ( $x );
}
}
2011-08-07 22:36:17 +00:00
function flops_to_credit ( $f ) {
return $f * ( 200 / 86400e9 );
}
function credit_to_gflop_hours ( $c ) {
return $c / ( 200 / 24 );
}
2010-09-16 18:53:39 +00:00
2007-11-14 16:03:47 +00:00
$cvs_version_tracker [] = " \$ Id $ " ; //Generated automatically - do not edit
2004-02-02 23:34:39 +00:00
?>