2004-02-02 23:34:39 +00:00
< ? php
require_once ( " ../inc/db.inc " );
require_once ( " ../inc/util.inc " );
require_once ( " ../inc/sanitize_html.inc " );
// TODO: Determine if we can always assume these will be the same number.
define ( 'SMALL_IMG_WIDTH' , 64 );
define ( 'SMALL_IMG_HEIGHT' , 64 );
define ( 'MAX_IMG_WIDTH' , 800 );
define ( 'MAX_IMG_HEIGHT' , 600 );
define ( 'MAX_DESC_LENGTH' , 90 );
define ( 'GALLERY_WIDTH' , 7 );
define ( 'GALLERY_HEIGHT' , 4 );
db_init ();
2004-05-30 21:47:11 +00:00
function get_profile ( $userid ) {
$result = mysql_query ( " SELECT * FROM profile WHERE userid = $userid " );
2004-06-15 22:55:54 +00:00
if ( ! $result ) {
return NULL ;
}
2004-05-30 21:47:11 +00:00
$profile = mysql_fetch_object ( $result );
mysql_free_result ( $result );
return $profile ;
}
2004-02-02 23:34:39 +00:00
2004-05-30 21:47:11 +00:00
function show_profile_creation_page ( $user ) {
2004-03-24 22:53:47 +00:00
// If the user already has a profile,
// fill in the fields with their current values.
2004-05-30 21:47:11 +00:00
//
$profile = get_profile ( $user -> id );
if ( $_POST [ 'submit' ]) {
process_create_results ( $user , $profile );
exit ();
2004-02-02 23:34:39 +00:00
}
2004-05-30 21:47:11 +00:00
if ( $profile ) {
page_head ( " Edit your Profile " );
} else {
page_head ( " Create a Profile " );
}
setup_form ( $user , $profile );
2004-02-02 23:34:39 +00:00
start_table_noborder ();
2004-05-30 21:47:11 +00:00
show_description ( $user , $profile );
show_questions ( $profile );
show_picture_option ( $profile );
2004-02-02 23:34:39 +00:00
show_finale ();
end_table ();
close_form ();
page_tail ();
}
2004-05-30 21:47:11 +00:00
function setup_form ( $user , $profile ) {
2004-02-02 23:34:39 +00:00
2004-07-21 22:09:42 +00:00
// <form action=", $_SERVER['PHP_SELF'], " method=\"POST\", ENCTYPE=\"multipart/form-data\">
// multipart/form-data doesn't always work (?)
2004-02-02 23:34:39 +00:00
echo "
2004-07-21 22:09:42 +00:00
< form action = " , $_SERVER['PHP_SELF'] , " method = \ " POST \" >
2004-02-02 23:34:39 +00:00
" ;
}
function close_form () {
echo " </form> " ;
}
2004-05-30 21:47:11 +00:00
function show_description ( $user , $profile ) {
2004-02-02 23:34:39 +00:00
2004-05-30 21:47:11 +00:00
if ( $profile ) {
row1 ( " Edit your profile " );
} else {
row1 ( " Create a profile " );
}
2004-02-02 23:34:39 +00:00
rowify ( "
2004-05-30 21:47:11 +00:00
Your < b > profile </ b > lets you share your opinions and background
with the " .PROJECT. " community .
< br >< a href = html . php >< font size =- 2 > You may use HTML tags </ font ></ a >
2004-02-02 23:34:39 +00:00
" );
rowify ( " <br> " );
}
2004-05-30 21:47:11 +00:00
function show_questions ( $profile ) {
2004-02-02 23:34:39 +00:00
show_profile_heading1 ();
show_profile_question1 ();
rowify ( " <br> " );
2004-05-30 21:47:11 +00:00
show_textarea ( " response1 " , $profile -> response1 );
2004-02-02 23:34:39 +00:00
rowify ( " <br> " );
show_profile_heading2 ();
show_profile_question2 ();
rowify ( " <br> " );
2004-05-30 21:47:11 +00:00
show_textarea ( " response2 " , $profile -> response2 );
2004-02-02 23:34:39 +00:00
rowify ( " <br> " );
2004-05-30 21:47:11 +00:00
show_language_selection ( $profile );
2004-02-02 23:34:39 +00:00
rowify ( " <br> " );
}
2004-05-30 21:47:11 +00:00
function show_textarea ( $name , $text ) {
rowify ( " <textarea name= \" $name\ " cols = 80 rows = 20 > " . $text . " </ textarea > " );
2004-02-02 23:34:39 +00:00
}
2004-05-30 21:47:11 +00:00
function show_picture_option ( $profile ) {
2004-02-02 23:34:39 +00:00
row1 ( " Your Picture " );
2004-05-30 21:47:11 +00:00
if ( $profile -> has_picture ) {
2004-02-02 23:34:39 +00:00
echo "
< tr >< td colspan = 2 >
< table border = 0 cellpadding = 5
< tr >
2004-05-30 21:47:11 +00:00
< td >< a href = \ " " . IMAGE_URL . $profile -> userid . '.jpg' . " \" ><img src= \" " . IMAGE_URL . $profile -> userid . '_sm.jpg' . " \" ></a><p>
2004-02-02 23:34:39 +00:00
< input type = \ " checkbox \" name= \" delete_pic \" >Delete
</ td >
< td > You have previously uploaded a picture of yourself
to accompany your profile , shown at left .
If you would like to replace it ,
please click the \ " Browse \" button and select the JPEG or PNG file
you would like to use instead .
If you would rather not have a picture with your profile ,
click the \ " Delete \" checkbox;
your picture will be deleted as soon as you submit this form .
If you ' re happy with your current picture ,
there is no need to do anything .< p >
< input name = \ " picture \" type= \" file \" ><br>
< font size = 2 >
< b > NOTE : </ b > Please keep your image small ( less than 50 K bytes ) < br ></ font ></ td ></ tr > " ;
rowify ( " <br> " );
end_table ();
echo " </td></tr> " ;
}
else {
rowify ( "
Do you have a picture of yourself on your computer ?
If you would like us to include it with your profile ,
please click the \ " Browse \" button and select the JPEG or PNG file
you want to send . ( No animated GIFs , please ! ) < br >
< input name = \ " picture \" type= \" file \" ><br>
< font size = 2 >< b > NOTE : </ b > Please keep your image small ( less than 50 K bytes ) < br ></ font ></ td ></ tr > " );
rowify ( " <br> " );
}
}
2004-05-30 21:47:11 +00:00
function show_language_selection ( $profile ) {
2004-02-02 23:34:39 +00:00
row1 ( " Select Your Primary Language " );
rowify ( " Selecting a language will help others with the same language preference to find each others' profiles and message board postings. " );
echo " <tr><td> " ;
2004-05-30 21:47:11 +00:00
if ( strlen ( $profile -> language )) {
show_combo_box ( " language " , LANGUAGE_FILE , $profile -> language );
2004-02-02 23:34:39 +00:00
} else {
show_combo_box ( " language " , LANGUAGE_FILE , " English " );
}
echo " </td></tr> \n " ;
}
function show_finale () {
row1 ( " Submit Your Profile " );
2004-03-24 22:53:47 +00:00
rowify ( " <p>
< input type = submit value = Submit name = submit >
& nbsp ; & nbsp ; & nbsp ; & nbsp ; & nbsp ; & nbsp ; & nbsp ;
< input type = reset value = Clear name = clear > "
);
2004-02-02 23:34:39 +00:00
}
// If the user with id = $userid has uploaded a picture his/herself,
// delete it and its thumbnail.
//
function delete_user_pictures ( $userid ) {
$filename1 = IMAGE_PATH . $userid . '.jpg' ;
$filename2 = IMAGE_PATH . $userid . '_sm.jpg' ;
if ( file_exists ( $filename1 )) {
unlink ( $filename1 );
}
if ( file_exists ( $filename2 )) {
unlink ( $filename2 );
}
}
2004-05-30 21:47:11 +00:00
function process_create_results ( $user , $profile ) {
2004-02-02 23:34:39 +00:00
$response1 = $_POST [ 'response1' ];
$response2 = $_POST [ 'response2' ];
$language = $_POST [ 'language' ];
$delete_pic = $_POST [ 'delete_pic' ];
if ( strlen ( $response1 ) == 0 &&
strlen ( $response2 ) == 0 &&
$delete_pic != " on " &&
! is_uploaded_file ( $_FILES [ 'picture' ][ 'tmp_name' ])
) {
profile_error_page ( " Your profile submission was empty. " );
exit ();
}
if ( $delete_pic == " on " ) {
2004-05-30 21:47:11 +00:00
delete_user_pictures ( $profile -> userid );
$profile -> has_picture = false ;
2004-02-02 23:34:39 +00:00
}
2004-05-30 21:47:11 +00:00
$profile ? $hasPicture = $profile -> has_picture : $hasPicture = false ;
2004-02-02 23:34:39 +00:00
if ( is_uploaded_file ( $_FILES [ 'picture' ][ 'tmp_name' ])) {
$hasPicture = true ;
/*
echo " <br>Name: " . $_FILES [ 'picture' ][ 'name' ];
echo " <br>Type: " . $_FILES [ 'picture' ][ 'type' ];
echo " <br>Size: " . $_FILES [ 'picture' ][ 'size' ];
echo " <br>Temp name: " . $_FILES [ 'picture' ][ 'tmp_name' ];
*/
$images = getImages ( $_FILES [ 'picture' ][ 'tmp_name' ]);
// Write the original image file to disk.
// TODO: define a constant for image quality.
ImageJPEG ( $images [ 0 ], IMAGE_PATH . $user -> id . '.jpg' );
ImageJPEG ( $images [ 1 ], IMAGE_PATH . $user -> id . '_sm.jpg' );
}
$response1 = sanitize_html ( $response1 );
$response2 = sanitize_html ( $response2 );
2004-05-30 21:47:11 +00:00
if ( $profile ) {
2004-02-02 23:34:39 +00:00
$query = 'UPDATE profile SET '
. " response1 = ' $response1 ', "
. " response2 = ' $response2 ', "
. " language = ' $language ', "
2004-03-24 22:53:47 +00:00
. " has_picture = ' $hasPicture ' "
2004-02-02 23:34:39 +00:00
. " WHERE userid = ' $user->id ' " ;
$result = mysql_query ( $query );
if ( ! $result ) {
profile_error_page ( " Couldn't update profile: database error " );
exit ();
}
} else {
$query = 'INSERT INTO profile SET '
. " userid = ' $user->id ', "
. " language = ' $language ', "
. " response1 = ' $response1 ', "
. " response2 = ' $response2 ', "
2004-06-09 21:42:23 +00:00
. " has_picture = ' $hasPicture ', "
. " verification=0 " ;
2004-02-02 23:34:39 +00:00
$result = mysql_query ( $query );
if ( ! $result ) {
profile_error_page ( " Couldn't create profile: database error " );
exit ();
}
$q = " update user set has_profile=1 where id= $user->id " ;
mysql_query ( $q );
}
2004-05-30 21:47:11 +00:00
show_result_page ( $user );
2004-02-02 23:34:39 +00:00
}
// Returns an array containing:
// [0]: The original image refered to by $fileName if its dimensions are
// less than MAX_IMG_WIDTH x MAX_IMG_HEIGHT, or a version scaled to
// those dimensions if it was too large.
// [1]: A scaled version of the above.
function getImages ( $fileName ) {
$size = getImageSize ( $fileName );
// Determine if the filetype uploaded is supported.
// TODO: Change these to constants.
switch ( $size [ 2 ]) {
case '2' : // JPEG
$image = imageCreateFromJPEG ( $fileName );
break ;
case '3' : // PNG
$image = imageCreateFromPNG ( $fileName );
break ;
default :
profile_error_page ( " The format of your uploaded image is not supported by our system. " );
exit ();
}
$width = $size [ 0 ];
$height = $size [ 1 ];
$smallImage = scale_image ( $image , $width , $height , SMALL_IMG_WIDTH , SMALL_IMG_HEIGHT );
if ( $width > MAX_IMG_WIDTH || $height > MAX_IMG_HEIGHT ) {
$image = scale_image ( $image , $width , $height , MAX_IMG_WIDTH , MAX_IMG_HEIGHT );
}
/*
echo " <br><br>Image type: $size[2] " ;
echo " <br>Original width: $width " ;
echo " <br>Original height: $height " ;
echo " <br>Scalar: $scalar " ;
echo " <br>Dest width: " . ( $width / $scalar );
echo " <br>Dest height: " . ( $height / $scalar );
echo " <br>Horizontal offset: $horiz_offset " ;
echo " <br>Vertical offset: $vert_offset " ;
echo " <br><br><a href= \" images/user_profile/test.jpg \" >View result</a> " ;
*/
return array ( $image , $smallImage );
}
function scale_image ( $image , $origWidth , $origHeight , $targetWidth , $targetHeight ) {
// If the image is already smaller than the target dimensions, just return it.
if ( $origWidth <= $targetWidth && $origHeight <= $targetHeight ) {
return $image ;
}
( $origWidth > $origHeight ) ? $scalar = ( $origWidth / $targetWidth ) : $scalar = ( $origHeight / $targetHeight );
if ( $scalar != 0 ) {
$destWidth = $origWidth / $scalar ;
$destHeight = $origHeight / $scalar ;
} else {
$destWidth = $origWidth ;
$destHeight = $origHeight ;
}
$newImage = ImageCreateTrueColor ( $destWidth , $destHeight );
ImageCopyResampled ( $newImage , $image , 0 , 0 , 0 , 0 , $destWidth , $destHeight , $origWidth , $origHeight );
return $newImage ;
}
2004-05-30 21:47:11 +00:00
function show_result_page ( $user ) {
2004-02-02 23:34:39 +00:00
page_head ( " Profile Saved " );
echo "
2004-05-14 22:57:59 +00:00
< h2 > Congratulations !</ h2 >< p >
2004-02-02 23:34:39 +00:00
Your profile was successfully entered into our database .< br >< br >
< a href = view_profile . php ? userid = $user -> id > View your profile </ a >< br >
" ;
page_tail ();
}
2004-06-15 18:50:57 +00:00
// Builds a summary table of user profiles.
2004-02-02 23:34:39 +00:00
//
// $members is an array of userIDs;
// $offset indicates which entry to begin the table with
// $numToDisplay indicates how many profiles to display in this table
// $cols indicates how many profile summaries should be written per row
// $descriptor is an optional file descriptor to write the table to.
2004-06-15 18:50:57 +00:00
function show_user_table ( $members , $offset , $numToDisplay , $cols ) {
echo " <table class=bordered border=1 cellpadding=5> \n " ;
2004-02-02 23:34:39 +00:00
$rows = ceil ( $numToDisplay / $cols );
$count = $offset ;
$numMembers = count ( $members );
for ( $row = 0 ; $row < $rows ; $row ++ ) {
if ( $count >= $numMembers ) {
break ;
}
2004-06-15 18:50:57 +00:00
echo " <tr> \n " ;
2004-02-02 23:34:39 +00:00
for ( $col = 0 ; $col < $cols ; $col ++ ) {
if ( $count < $numMembers ) {
2004-06-15 22:55:54 +00:00
$profile = get_profile ( $members [ $count ]);
if ( ! $profile ) {
$numMembers -- ;
continue ;
}
2004-06-15 18:50:57 +00:00
echo " <td class=bordered width=7% height=64><center> " ;
2004-02-02 23:34:39 +00:00
// Only link an image if the user has uploaded one.;
2004-05-30 21:47:11 +00:00
if ( $profile -> has_picture ) {
2004-06-15 18:50:57 +00:00
echo " <a href= \" " , URL_BASE , " view_profile.php?userid= { $members [ $count ] } \" ><img src= \" " , URL_BASE , IMAGE_URL , " { $members [ $count ] } _sm.jpg \" ></a> " ;
2004-02-02 23:34:39 +00:00
} else {
2004-06-15 18:50:57 +00:00
echo " " ;
2004-02-02 23:34:39 +00:00
}
2004-06-15 18:50:57 +00:00
echo " </center></td><td class=bordered width=33% height=64> \n " , get_profile_summary ( $profile ), " </td> " ;
2004-02-02 23:34:39 +00:00
$count ++ ;
} else {
2004-06-15 18:50:57 +00:00
echo " <td width=7% height=64></td><td width=33% height=64></td> " ;
2004-02-02 23:34:39 +00:00
}
}
2004-06-15 18:50:57 +00:00
echo " </tr> \n " ;
2004-02-02 23:34:39 +00:00
}
2004-06-15 18:50:57 +00:00
echo " </table> \n " ;
2004-02-02 23:34:39 +00:00
}
// Generates a string containing:
// 1) the name of the user with ID == $userid, with a link to a view of their profile
// 2) the first MAX_DESC_LENGTH characters from the response1 field of said user's profile.
function get_profile_summary ( $profile ) {
2004-05-30 21:47:11 +00:00
$user = get_user_from_id ( $profile -> userid );
2004-02-02 23:34:39 +00:00
2004-05-30 21:47:11 +00:00
if ( ! $user || ! $profile ) {
2004-02-02 23:34:39 +00:00
echo " Database error! " ; // Change this to a standard error page.
exit ();
}
$description = " " ;
2004-05-30 21:47:11 +00:00
if ( strlen ( $profile -> response1 ) != 0 ) {
$temp = $profile -> response1 ;
2004-02-02 23:34:39 +00:00
$description = " ( \" " . sub_sentence ( strip_tags ( $temp ), ' ' , MAX_DESC_LENGTH , true ) . " \" ) " ;
}
2004-05-30 21:47:11 +00:00
$summary = " <a href= \" " . URL_BASE . " view_profile.php?userid= " . $profile -> userid . " \" > " . $user -> name . " </a> " . $description ;
2004-02-02 23:34:39 +00:00
return $summary ;
}
// Displays a user's profile (if they have one);
function show_profile ( $userid , $verify_mode = false ) {
$user = get_user_from_id ( $userid );
if ( ! $user ) {
profile_error_page ( " No user exists for that ID, or there was a database error.<p> " );
exit ();
}
2004-05-30 21:47:11 +00:00
$profile = get_profile ( $userid );
2004-02-02 23:34:39 +00:00
2004-05-30 21:47:11 +00:00
if ( ! $profile ) {
2004-02-02 23:34:39 +00:00
echo " No user profile exists for that user ID. " ;
exit ();
}
if ( ! $verify_mode ) {
$logged_in_user = get_logged_in_user ( false ); // (false) since anyone can look at profiles.;
}
$can_edit = $logged_in_user && $user -> id == $logged_in_user -> id ;
if ( ! $verify_mode ) {
2004-05-12 17:54:23 +00:00
page_head ( " Profile: " . $user -> name );
2004-02-02 23:34:39 +00:00
}
if ( $can_edit ) {
echo " <a href=create_profile.php>[Edit Your Profile]</a> " ;
}
2004-05-30 21:47:11 +00:00
show_profile_summary ( $user , $profile , $can_edit , $verify_mode );
2004-02-02 23:34:39 +00:00
echo " <br><br> " ;
show_profile_heading1 ();
2004-05-30 21:47:11 +00:00
echo " : " , $profile -> response1 ;
2004-02-02 23:34:39 +00:00
echo " <br><br> " ;
show_profile_heading2 ();
2004-05-30 21:47:11 +00:00
echo " : " , $profile -> response2 ;
2004-02-02 23:34:39 +00:00
if ( ! $verify_mode ) {
page_tail ();
}
}
2004-05-30 21:47:11 +00:00
function show_profile_summary ( $user , $profile , $can_edit , $verify_mode ) {
2004-02-02 23:34:39 +00:00
if ( ! $can_edit && ! $verify_mode ) {
show_view_buttons ( $user -> id );
}
// Only display an image if the user has uploaded one;
2004-05-30 21:47:11 +00:00
if ( $profile -> has_picture ) {
2004-02-10 07:02:38 +00:00
//echo "<a href=\"" , URL_BASE, IMAGE_URL , $user->id , '.jpg' . "\"><img align=left vspace=6 hspace=9 src=\"" , URL_BASE, IMAGE_URL , $user->id , '_sm.jpg' . "\"></a>\n";
echo " <br><img vspace=6 hspace=9 src= \" " , URL_BASE , IMAGE_URL , $user -> id , '.jpg' . " \" > \n " ;
2004-02-02 23:34:39 +00:00
}
echo "
< br >
< font size =- 1 >
< b > Country :</ b > " , $user->country ,
2004-05-30 21:47:11 +00:00
" <br><b>Language:</b> " , $profile -> language , " <br>
2004-02-02 23:34:39 +00:00
" ;
echo " <b>Total Credit:</b> " , $user -> total_credit , " <br> " ;
if ( $user -> teamid ) {
$result = mysql_query ( " select * from team where id = $user->teamid " );
$team = mysql_fetch_object ( $result );
echo " <b>Team:</b> <a href=team_display.php?teamid= $team->id > $team->name </a><br> " ;
}
echo "
< b > Date Registered :</ b > " , date_str( $user->create_time ), "
</ font >
</ td ></ tr >
</ table >
< br > \n
" ;
}
function show_view_buttons ( $userid ) {
echo "
< form action = view_profile . php ? userid = $userid method = \ " POST \" >
< input type = submit name = recommend value = Recommend >
< font size =- 1 >< a href = \ " javascript:; \" onClick= \" window.open ('explanation.php?val=recommend','_blank','width=350,height=200,left=50,top=150,menubar=0,directories=0,scrollbars=0,resizable=0,status=0') \" >what is recommend?</a></font>
< br >
< input type = submit name = reject value = \ " Vote to reject \" >
< font size =- 1 >< a href = \ " javascript:; \" onClick= \" window.open ('explanation.php?val=reject','_blank','width=350,height=200,left=50,top=150,menubar=0,directories=0,scrollbars=0,resizable=0,status=0') \" >what is vote to reject?</a></font>
</ form >
" ;
}
function process_view_results ( $vote , $userid ) {
if ( $vote != " recommend " && $vote != " reject " ) {
echo " Invalid vote type.<br> " ;
exit ();
}
$result = mysql_query ( " SELECT * FROM profile WHERE userid = $userid " );
$profile = mysql_fetch_array ( $result );
$newValue = $profile [ $vote ] + 1 ;
$newresult = mysql_query ( " UPDATE profile SET $vote = $newValue WHERE userid = $userid " );
page_head ( " Vote Recorded " );
start_table_noborder ();
row1 ( " Thank you " );
if ( $vote == " recommend " ) {
rowify ( " Your recommendation has been recorded. " );
} else {
rowify ( " Your vote to reject has been recorded. " );
}
end_table ();
echo " <br><a href= \" view_profile.php?userid= " , $userid , " \" >Return to profile.</a> " ;
page_tail ();
}
?>