PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
print "</pre>";
?>
The above code is just from the manual to show what I just made for highlighting php. I just knocked up a short code that will turn your php into BBcode while the PHP tags are offline. Just a couple of things I need to straighten out first (Nesting and the initial [color.. tags) but I'll put the code in snippets tomorrow (18hrs) if anybody wishes to use it for posting code while we await the return of the lifesaving [php.. tags. I'll use this in the meantime.
It's only 20 lines long or so.
Last edited by Chris Corbyn on Wed Mar 02, 2005 7:02 pm, edited 2 times in total.
Yeah isn't that weird It's supposed to... that's the point
I don't think the BBcode that PHPDN is using allows space characters to output so I'm almost resorting to clear gifs to do the tabs for nesting content. This isn't too friendly for any copy & pasters who want to play with code posted in here... Still trying to find a better way
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
mysql_select_db("my_database") or die("Could not select database");
/* Performing SQL query */
$query = "SELECT * FROM my_table";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
/* Printing results in HTML */
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
/* Free resultset */
mysql_free_result($result);
/* Closing connection */
mysql_close($link);
?> <some html tag>
<br> More html
The output of highlight_string() puts <font color="#000000"> at the start and then opens another <font color="#0000BB"> tag with closing the 000000 one. But you can see it closes at the end. BBcode doesn't like this - hence the stray [color] tags.
I need to close the first tag (keeping any content inside it with newlines etc). I also need to open a tag before the final color tag closes (keeping any content inside it again).
I know it should be easy but I'm getting frustrated now
OK here it is at the moment... it's ok to use now, but it requires a small amount of user compliance to ensure it works.
PLEASE READ BELOW TO NOTE THE CURRENT LIMITATIONS IN THIS UNFINISHED VERSION.
OK don't laugh at the code, I know it's amateur but it works on all I've thrown at it so far
The only thing you need to be aware of is that in order to get the PHP to highlight the code, the chunk of code must be opened by <?php or <? and closed by ?>. I'll iron this out. It's taken a bit longer than expected to get this far since I made it a bit deeper (Not the GUI as you'll discover ) Just copy and paste this code to a blank file and save it as php. It'll get some highlighting back in the forum for the time being anyway
<?php
/************************************************************************
*
* BBCode Generator for PHP Source Code Highlighting
*
*------------------------------------------------------------------------
* Author: d11wtq [Chris Corbyn]
* Date: 1st March 2005
*------------------------------------------------------------------------
* Thanks to feyd and other PHPDN members at
* http://forums.devnetwork.net/
* for help with BBCode outputs and some RegExp's!
*------------------------------------------------------------------------
*
* Known issues that can be fixed:
* * PHP code posted without <?php ?> tags isn't highlighted
* * The GUI is rubbish
* * Copying code to the clipboard isn't automated
* * For code sent from textarea the output is very buggy
* * NON-PHP files may be uploaded!
* Known issues that cannot be fixed:
* * Posted code can be previewed and editted but once submitted the
* special chars are parsed so the edited version needs to be
* run through this script again
*
* The known issues which can be fixed will be fixed in the first
* official release.
* Those issues that cannot be fixed are server based issues so will
* remain until the
tags return to PHPDN
*
*************************************************************************/
//Source file for input from upload
[/color][color=#007700]if (!empty([/color][color=#0000BB]$_FILES[/color][color=#007700][[/color][color=#DD0000]'sourcefile'[/color][color=#007700]])) {
[/color][color=#0000BB]$filename [/color][color=#007700]= [/color][color=#0000BB]$_FILES[/color][color=#007700][[/color][color=#DD0000]'sourcefile'[/color][color=#007700]][[/color][color=#DD0000]'name'[/color][color=#007700]];
[/color][color=#0000BB]$filedata [/color][color=#007700]= [/color][color=#0000BB]$_FILES[/color][color=#007700][[/color][color=#DD0000]'sourcefile'[/color][color=#007700]][[/color][color=#DD0000]'tmp_name'[/color][color=#007700]];
} elseif (!empty([/color][color=#0000BB]$_POST[/color][color=#007700][[/color][color=#DD0000]'pastebox'[/color][color=#007700]])) {
[/color][color=#0000BB]$filename [/color][color=#007700]= [/color][color=#DD0000]'Pasted'[/color][color=#007700];
[/color][color=#0000BB]$filedata [/color][color=#007700]= [/color][color=#DD0000]'none'[/color][color=#007700];
} [/color][color=#FF9900]//End if
//Generate the BBCode
[/color][color=#007700]if(!empty([/color][color=#0000BB]$filedata[/color][color=#007700]) && !empty([/color][color=#0000BB]$filename[/color][color=#007700])) {
[/color][color=#FF9900]//Check if code was upload from file or pasted
[/color][color=#007700]if (!empty([/color][color=#0000BB]$_FILES[/color][color=#007700][[/color][color=#DD0000]'sourcefile'[/color][color=#007700]])) {
[/color][color=#0000BB]$source [/color][color=#007700]= [/color][color=#0000BB]file_get_contents[/color][color=#007700]([/color][color=#0000BB]$filedata[/color][color=#007700]);
} else {
[/color][color=#0000BB]$source [/color][color=#007700]= [/color][color=#0000BB]stripslashes[/color][color=#007700]([/color][color=#0000BB]$_POST[/color][color=#007700][[/color][color=#DD0000]'pastebox'[/color][color=#007700]]);
} [/color][color=#FF9900]//End if
[/color][color=#0000BB]$htmhighlight [/color][color=#007700]= [/color][color=#0000BB]highlight_string[/color][color=#007700]([/color][color=#0000BB]$source[/color][color=#007700], [/color][color=#0000BB]true[/color][color=#007700]); [/color][color=#FF9900]//The HTML equivalent of the BBCode
//Output BBCode chars as special chars (so they dont parse)
[/color][color=#0000BB]$sc_chars [/color][color=#007700]= [/color][color=#0000BB]preg_replace[/color][color=#007700]([/color][color=#DD0000]'/&#(\d+);/'[/color][color=#007700], [/color][color=#DD0000]'&#$1;'[/color][color=#007700], [/color][color=#0000BB]$htmhighlight[/color][color=#007700]);
[/color][color=#0000BB]$sc_tag_open [/color][color=#007700]= [/color][color=#0000BB]str_replace[/color][color=#007700]([/color][color=#DD0000]'['[/color][color=#007700], [/color][color=#DD0000]'['[/color][color=#007700], [/color][color=#0000BB]$sc_chars[/color][color=#007700]);
[/color][color=#0000BB]$sc_tag_close [/color][color=#007700]= [/color][color=#0000BB]str_replace[/color][color=#007700]([/color][color=#DD0000]']'[/color][color=#007700], [/color][color=#DD0000]']'[/color][color=#007700], [/color][color=#0000BB]$sc_tag_open[/color][color=#007700]);
[/color][color=#FF9900]//Turn html tags created by highlight_string() into BBCode
[/color][color=#0000BB]$bb_quote_open [/color][color=#007700]= [/color][color=#0000BB]preg_replace[/color][color=#007700]([/color][color=#DD0000]'/^<code>/'[/color][color=#007700], [/color][color=#DD0000]'[quote]'[/color][color=#007700], [/color][color=#0000BB]$sc_tag_close[/color][color=#007700]);
[/color][color=#0000BB]$bb_quote_close [/color][color=#007700]= [/color][color=#0000BB]preg_replace[/color][color=#007700]([/color][color=#DD0000]'/<\/code>$/'[/color][color=#007700], [/color][color=#DD0000]'[/quote]'[/color][color=#007700], [/color][color=#0000BB]$bb_quote_open[/color][color=#007700]);
[/color][color=#0000BB]$bb_font_open [/color][color=#007700]= [/color][color=#0000BB]preg_replace[/color][color=#007700]([/color][color=#DD0000]'/<font color="(#?[a-z0-9\-]+)">/i'[/color][color=#007700], [/color][color=#DD0000]'[color=$1]'[/color][color=#007700], [/color][color=#0000BB]$bb_quote_close[/color][color=#007700]);
[/color][color=#0000BB]$bb_font_close [/color][color=#007700]= [/color][color=#0000BB]str_replace[/color][color=#007700]([/color][color=#DD0000]'</font>'[/color][color=#007700], [/color][color=#DD0000]'[/color]'[/color][color=#007700], [/color][color=#0000BB]$bb_font_open[/color][color=#007700]);
[/color][color=#0000BB]$bb_norm_st_kill [/color][color=#007700]= [/color][color=#0000BB]str_replace[/color][color=#007700]([/color][color=#DD0000]'[color=#000000]'[/color][color=#007700], [/color][color=#DD0000]''[/color][color=#007700], [/color][color=#0000BB]$bb_font_close[/color][color=#007700]); [/color][color=#FF9900]//Remove the black color tags and let BBCode do it itself **
[/color][color=#0000BB]$bb_norm_fin_kill [/color][color=#007700]= [/color][color=#0000BB]preg_replace[/color][color=#007700]([/color][color=#DD0000]'/\[\/color\]\s*?\[\/quote\]$/'[/color][color=#007700], [/color][color=#DD0000]'[/quote]'[/color][color=#007700], [/color][color=#0000BB]$bb_norm_st_kill[/color][color=#007700]); [/color][color=#FF9900]//Remove the final [/color] tag **
[/color][color=#0000BB]$bb_indent [/color][color=#007700]= [/color][color=#0000BB]str_replace[/color][color=#007700]([/color][color=#DD0000]' '[/color][color=#007700], [/color][color=#DD0000]' '[/color][color=#007700], [/color][color=#0000BB]$bb_norm_fin_kill[/color][color=#007700]);
[/color][color=#FF9900]//Evade feyd's filter for carriage returns
[/color][color=#0000BB]$sc_carr [/color][color=#007700]= [/color][color=#0000BB]str_replace[/color][color=#007700]([/color][color=#DD0000]'\r'[/color][color=#007700], [/color][color=#DD0000]'\r'[/color][color=#007700], [/color][color=#0000BB]$bb_indent[/color][color=#007700]);
[/color][color=#FF9900]//The final output
[/color][color=#0000BB]$bbcodehighlight [/color][color=#007700]= [/color][color=#0000BB]$sc_carr[/color][color=#007700];
} [/color][color=#FF9900]//End if
[/color][color=#0000BB]?>
[/color]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>BBCode Generator for PHP Source Code Highlighting</title>
<style type="text/css">
body {color:#007700; font-size:9pt; font-family:verdana,tahoma,arial,sans-serif}
form {margin:0px; padding:0px}
.bblack {color:#000000; font-weight:bold; border-bottom:1px solid #AAAAAA}
.grey {color:#444444; font-size:9pt; font-family:verdana,tahoma,arial,sans-serif}
.borange {color:#FE7726; font-weight:bold; border-bottom:1px solid #AAAAAA}
#framework {border:1px solid #CCCCCC}
#bbcode_output {width:100%; height:300px; background-color:#F5F5F5}
#pastebox {width:100%; height:120px; color:#000000; font-size:9pt; font-family:verdana,tahoma,arial,sans-serif}
</style>
<script language="javascript" type="text/javascript">
<!-- Hide from older browsers
function clearForm(el) {
return true
el.reset()
}
// End -->
</script>
</head>
<body>
[color=#0000BB]<?php
[/color][color=#FF9900]//Decide what to output (BBCode panel or main page) *** See iframe below
[/color][color=#007700]if (!empty([/color][color=#0000BB]$_GET[/color][color=#007700][[/color][color=#DD0000]'show_only_iframe_content'[/color][color=#007700]])) {
if(isset([/color][color=#0000BB]$bbcodehighlight[/color][color=#007700])) {
echo [/color][color=#DD0000]'<div id="code_div">'[/color][color=#007700].[/color][color=#0000BB]$bbcodehighlight[/color][color=#007700].[/color][color=#DD0000]'</div>'[/color][color=#007700];
} else {
echo [/color][color=#DD0000]'BBCode Output Panel'[/color][color=#007700];
}
} else {
echo [/color][color=#DD0000]'<table id="framework" width="100%" cellpadding="3" cellspacing="0" border="0">'[/color][color=#007700].[/color][color=#DD0000]"\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t<tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t"[/color][color=#007700].[/color][color=#DD0000]'<td class="bblack">'[/color][color=#007700];
[/color][color=#FF9900]//Show the file that has been processed if it is set
[/color][color=#007700]if(isset([/color][color=#0000BB]$bbcodehighlight[/color][color=#007700])) {
echo [/color][color=#DD0000]'BBCode output for '[/color][color=#007700].[/color][color=#0000BB]$filename[/color][color=#007700].[/color][color=#DD0000]' source code highlighting:'[/color][color=#007700];
} else {
echo [/color][color=#DD0000]'Upload a file to view the BBCode for source code highlighting'[/color][color=#007700];
}
echo [/color][color=#DD0000]"</td>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t</tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t<tr>\r\n"[/color][color=#007700];
[/color][color=#FF9900]//The iframe calls this script but the GET var changes the output ***
[/color][color=#007700]echo [/color][color=#DD0000]"\t\t"[/color][color=#007700].[/color][color=#DD0000]'<td><iframe id="bbcode_output" name="bbcode_output" src="'[/color][color=#007700].[/color][color=#0000BB]$_SERVER[/color][color=#007700][[/color][color=#DD0000]'PHP_SELF'[/color][color=#007700]].[/color][color=#DD0000]'?show_only_iframe_content=true"></iframe></td>'[/color][color=#007700].[/color][color=#DD0000]"\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t</tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t<tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t"[/color][color=#007700].[/color][color=#DD0000]'<td class="borange"><form action="'[/color][color=#007700].[/color][color=#0000BB]$_SERVER[/color][color=#007700][[/color][color=#DD0000]'PHP_SELF'[/color][color=#007700]].[/color][color=#DD0000]'?show_only_iframe_content=true" method="post" enctype="multipart/form-data" target="bbcode_output">'[/color][color=#007700].[/color][color=#DD0000]"\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t"[/color][color=#007700].[/color][color=#DD0000]'Choose a PHP file <input type="file" name="sourcefile"> <input type="submit" value="Upload"> OR...'[/color][color=#007700].[/color][color=#DD0000]"\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t</form></td>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t</tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t<tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t"[/color][color=#007700].[/color][color=#DD0000]'<td class="grey"><form action="'[/color][color=#007700].[/color][color=#0000BB]$_SERVER[/color][color=#007700][[/color][color=#DD0000]'PHP_SELF'[/color][color=#007700]].[/color][color=#DD0000]'?show_only_iframe_content=true" method="post" enctype="application/x-httpd-php" target="bbcode_output" onSubmit="return clearForm(this)">'[/color][color=#007700].[/color][color=#DD0000]"\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t"[/color][color=#007700].[/color][color=#DD0000]'Paste your code<br><textarea id="pastebox" name="pastebox"></textarea><br><input type="submit" value="Create BBCode">'[/color][color=#007700].[/color][color=#DD0000]"\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t\t</form></td>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"\t</tr>\r\n"[/color][color=#007700];
echo [/color][color=#DD0000]"</table>\r\n"[/color][color=#007700];
} [/color][color=#FF9900]//End if
[/color][color=#0000BB]?>
[/color]</body>
</html>[/quote]
To use it, you can either write/paste PHP to a textarea or just upload the file to the script. It outputs something that looks a bit of a mess but that cos it's BBCode. Then all you have to do is copy the outputted BBCode and paste it in your post (without any tags - it's got them already so it'll just display inline).
Hope it gets some use until the
mssql_query($msqry); // get id for file inserted...burrito (ohh how I miss mysql_insert_id) $getmax = mssql_query("select max(id) as thisone from lp_files where ipadd = '".$_SERVER['REMOTE_ADDR']."'");
if($gtmax = mssql_fetch_assoc($getmax)){ $fid = $gtmax['thisone'];
}
}else{ $message = "You can not upload files with the \".".$ext."\" extension to this web site";
}
} ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
Added this little bit to auto copy the results to the clipboard...might be IE only???
//Decide what to output (BBCode panel or main page) *** See iframe below
if (!empty($_GET['show_only_iframe_content'])) {
if(isset($bbcodehighlight)) {
echo '<div id="code_div">'.$bbcodehighlight.'</div>';
echo '<script>';
echo ' window.clipboardData.setData("Text",document.getElementById("code_div").innerText);';
echo '</script>';
} else {
echo 'BBCode Output Panel';
}
}
and yes, I just used it to copy and paste it in here wewt!