Ok I am doing a project for a company, and I contracted a programmer to do a mailing list application for us, and it also includes an administration section. Unfortunately they mislead us and gave us an application that does not fully work. I am not the greatest when it comes to php programming, but I have an idea how some of this application works. The fact that the code is not commented also makes it difficult.
After going through the code that I am about to mention below, I have found that the following 2 files "management.php" & "required.inc" does all of main communication with the MySQL database, the reading,writing and updating. They will allow updates and reading of records, but will not allow writing of new records. This file also should format the records with alternating row colors, however the alternating row code does not work and all style for the records table is lost.
This is the code for "management.php"
<?php
session_start();
include_once('required.inc
');
authenticate();
reset($_GET);reset($_POST)
;
HTML CODE HERE
$action = $_GET['action'];
if($action!='editing'){ech
o '<div align="left"><img src="../images/adminpanel.
gif" alt="Join Mailing List Header" width="400" height="60" border="0"></div>';}
$db_name='mailinglist_db';
$tb_name='promo_list';
if($_SESSION['authorized']
){
connect_to_database('local
host','use
rname','pa
ssword');
use_database($db_name);
$table_fields = get_table_fields($db_name,
$tb_name);
array_pop($table_fields);a
rray_pop($
table_fiel
ds);
$where = "join_list='TRUE'";
if($action=='delete'){remo
ve_record(
$tb_name,(
'record = '.$_GET['record']));}
else if($action=='editing'){$wh
ere = 'record = '.$_GET['record'];}
else if($action=='updating'){
for($inc=0;$inc<count($tab
le_fields)
;$inc++){
$index = $table_fields[$inc];
$updating[$index] = $_POST[$index];
}
update_record($tb_name,$up
dating,('r
ecord = '.$_POST['record']));
}
if($action=='delete'||$act
ion=='upda
ting'){ech
o '<script language="JavaScript" type="text/javascript">par
ent.editin
g.document
.location=
\'registra
tion.php\'
;</script>
';}
if(count($listings=retrive
_records($
tb_name,'*
',$where))
>0){
foreach($listings as $listing){
array_pop($listing);array_
pop($listi
ng);
$records[] = $listing;
}
echo report_table($tb_name,$tab
le_fields,
$records);
}else{echo 'There are no records available at this time.<br />The database appears to be empty, please input new records.';}
}else{echo '<script language="JavaScript" type="text/javascript">top
.document.
location.h
ref=\'inde
x.html\'</
script>';}
?>
This is the code for "required.inc"
<?php
define('_emp_','');
define('_spc_',' ');
define('_nl_',"\n");
define('_tab_',"\t");
define('_brk_','<br />');
define('_blank_',' ')
;
function authenticate(){
if(!$authorized){
if((substr($_SERVER['SERVE
R_SOFTWARE
'],0,9)=="
Microsoft"
)&&(!isset
($_SERVER[
'PHP_AUTH_
USER']))&&
(!isset($_
SERVER['PH
P_AUTH_PW'
]))&&(subs
tr($_SERVE
R['HTTP_AU
THORIZATIO
N'],0,6)==
"Basic")){
list($_SER
VER['PHP_A
UTH_USER']
,$_SERVER[
'PHP_AUTH_
PW'])=expl
ode(":",ba
se64_decod
e(substr($
_SERVER['H
TTP_AUTHOR
IZATION'],
6)));}
if(($_SERVER['PHP_AUTH_USE
R']==='adm
in')&&($_S
ERVER['PHP
_AUTH_PW']
==='demo')
){$authori
zed=true;}
else{
header('WWW-Authenticate: Basic realm="Administration"');
if(substr($_SERVER['SERVER
_SOFTWARE'
],0,9)=="M
icrosoft")
{header("S
tatus: 401 Unauthorized");}
else{header("HTTP/1.0 401 Unauthorized");}
$authorized=false;
}
}
$_SESSION['authorized']=$a
uthorized;
}
function connect_to_database($db_ho
st,$db_log
in,$db_pas
sword){
mysql_pconnect($db_host,$d
b_login,$d
b_password
);
}
function use_database($db_name){
mysql_query('USE '.$db_name);
}
function scriptless_record($record)
{
foreach($record as $field=>$content){
if(preg_match("/script/",$
content)){
$content=p
reg_replac
e("/script
/",_emp_,$
content);}
$validated[$field]=$conten
t;
}
return($validated);
}
function insert_record($tb_name,$tb
_fields,$f
lds_values
){
$flds_values=scriptless_re
cord(array
_values($f
lds_values
));
foreach($flds_values as $field=>$value){$flds_valu
es[$field]
=htmlentit
ies(addsla
shes(trim(
nl2br($val
ue))),ENT_
QUOTES);}
$tb_fields=implode(',',$tb
_fields);
$flds_values=implode("','"
,$flds_val
ues);
mysql_query("INSERT INTO ".$tb_name." ( ".$tb_fields." ) VALUES ( '".$flds_values."' );");
}
function get_table_fields($db_name,
$tb_name){
$fields;$columns;$xecho;
$fields=mysql_list_fields(
$db_name,$
tb_name);
$columns=mysql_num_fields(
$fields);
for($i=0;$i<$columns;$i++)
{$xecho[$i
]=mysql_fi
eld_name($
fields,$i)
;}
return($xecho);
};
function retrive_records($table,$fi
elds='*',$
condition=
false,$for
mat='array
'){
$result=mysql_query('SELEC
T '.$fields.' FROM '.$table.(($condition)?(' WHERE '.$condition):('')).' ORDER BY record ASC;');
$records=mysql_num_rows($r
esult);
if(count($records)>0){
for($inc=0;$inc<$records;$
inc++){$re
trived[$in
c]=($forma
t=='object
')?(mysql_
fetch_obje
ct($result
)):(mysql_
fetch_arra
y($result,
MYSQL_ASSO
C));}
return($retrived);
}else{return(false);}
}
function update_record($tb_name,$tb
_content,$
condition=
false){
$tb_content=scriptless_rec
ord($tb_co
ntent);
if(count($tb_content)>1){a
rray_shift
($tb_conte
nt);}
foreach($tb_content as $field=>$attribute){$updat
e[]=$field
." = '".$attribute."'";}
mysql_query('UPDATE '.$tb_name.' SET '.join(', ',$update).(($condition)?(
' WHERE '.$condition):('')).';');
}
function remove_record($tb_name,$co
ndition){
mysql_query('DELETE FROM '.$tb_name.' WHERE '.$condition.';');
}
function mysq_php_timestamp($indexe
s,$formats
,$tb_name,
$timestamp
,$record_i
d){
$date_format=explode(' ',array_shift(mysql_fetch_
row(mysql_
query("SEL
ECT DATE_FORMAT( ".$timestamp.", ".$formats." ) FROM ".$tb_name." WHERE record = '".$record_id."';"))));
$timestamp=array();
foreach($date_format as $index=>$format){$timestam
p[$indexes
[$index]]=
$format;}
return($timestamp);
}
function report_table($tb_name,$tab
le_fields,
$listings)
{
if(count($listings)>0){
$tb_body=array();
$swap=true;
foreach($listings as $listing){
$tb_tr=array();
$tb_tr[]='<tr>';
$color=($swap)?('on'):('of
f');
$rec_id=array_shift($listi
ng);
$index=array();
$index[]='<td id="'.$rec_id.'" class="cell_'.$color.'" style="text-align:right;pa
dding:0px 0px;background:#d4d0c8">';
$features='<table border="0" align="right" cellpadding="0" cellspacing="0"><tr><td><l
abel style="font-size:10pt;colo
r:#000000;
">'.$rec_i
d.' <
/label></t
d>';
if($_GET['action']=='editi
ng'){
$features.='<td><input type="image" border="0" name="updating" src="images/save01.gif" alt="Save Changes"><input type="hidden" name="record" value="'.$rec_id.'"></td>'
;
}
else{
$timestamp=mysq_php_timest
amp(array(
'weekday',
'month','m
onthday','
year','tim
e','sectio
n'),"'%W %M %D %Y %T %p'",$tb_name,'timestamp',
$rec_id);
$timestamp=$timestamp['wee
kday'].', '.$timestamp['month'].' '.$timestamp['monthday'].'
. '.$timestamp['year'].' - '.$timestamp['time'].' '.$timestamp['section'];
$features.='<td><a href="'.$_SERVER['PHP_SELF
'].'?actio
n=editing&
record='.$
rec_id.'" target="editing"><img id="" class="" src="images/edit01.gif" border="0" alt="Edit Record: '.$timestamp.'" /></a></td>';
}
$features.='<td><a href="./confirmation.php?a
ction=dele
te&record=
'.$rec_id.
'" target="report"><img id="" class="" src="images/delete01.gif" border="0" alt="Delete Record" /></a></td></tr></table>';
$index[]=$features.'</td>'
;
$tb_tr[]=join(_nl_,$index)
;
$tr_td=array();
foreach($listing as $index=>$record){
if($_GET['action']=='editi
ng'){
$tr_td[]='<td style="padding:0px 2px;background:#d4d0c8" valign="top">';
if($index!=='comments'){$t
r_td[]='<i
nput class="txt" name="'.$index.'" type="text" size="20" value="'.htmlentities(stri
pslashes($
record),EN
T_QUOTES).
'"></td>';
}
else{$tr_td[]='<textarea id="'.$index.'_'.$rec_id.'
" class="txt" name="'.$index.'" rows="5" wrap="virtual">'.htmlentit
ies(strips
lashes($re
cord),ENT_
QUOTES).'<
/textarea>
';}
$tr_td[]='</td>';
}else{
$tr_td[]='<td id="'.$index.'" class="cell_'.$color.'" valign="top">';
$link=(($color=='on')?('#f
fffff'):('
#6989A8'))
;
if($index==='e_mail'){
$contact=(!empty($listing[
'first_nam
e'])?($lis
ting['firs
t_name'].'
'):(_emp_)).(!empty($listi
ng['last_n
ame'])?($l
isting['la
st_name'].
' '):(_emp_)).(!empty($listi
ng['alias'
])?("- ".$listing['alias']):(_emp
_));
$tr_td[]='<a id="" style="color:'.$link.';tex
t-decorati
on:none;" href="mailto:'.$contact.'<
'.$record.
'>">'.$rec
ord.'</a>'
;
}
else if(strstr($record,"
http://")){$tr_td
[]='<a id="" style="color:'.$link.';tex
t-decorati
on:none;" href="'.$record.'" target="portfolio">'.$reco
rd.'</a>';
}
else{
$booled=strtoupper($record
);
if(empty($record)){$tr_td[
]=' '
;}
else if(($booled=='TRUE')||($bo
oled=='FAL
SE')){$tr_
td[]=($boo
led=='TRUE
')?('Yes')
:('No');}
else{$tr_td[]=stripslashes
(html_enti
ty_decode(
$record));
}
}
$tr_td[]='</td>';
}
}
$tb_tr[]=join(_nl_,$tr_td)
.'</tr>';
$tb_body[]=join(_nl_,$tb_t
r);
$swap=!$swap;
}
}
$tb_head=array();
$tb_head[]='<tr>';
$tr_td=array();
foreach($table_fields as $field){
$tr_td[]='<td id="'.$field.'" class="header" nowrap>';
$column=ucwords(strtolower
(str_repla
ce('_',_sp
c_,$field)
));
if($column=='Record'){$col
umn=(count
($listings
)>1)?('Rec
s( '.count($listings).' )'):('Rec( 1 )');}
$tr_td[]=$column.'</td>';
}
$tb_head[]=join(_nl_,$tr_t
d).'</tr>'
;
$report=array();
if(count($tb_body)>0){
$report[]='<table bordercolor="#6989A8" border="1" align="left" cellpadding="2" cellspacing="0">'.join(_nl
_,$tb_head
).join(_nl
_,$tb_body
).'</table
>';
$output=array();
if($_GET['action']=='editi
ng'){
$form[]='<form name="updating" action="./management.php?a
ction=upda
ting" method="POST" target="report" enctype="application/x-www
-form-urle
ncoded">'.
join(_nl_,
$report).'
</form>';
$output=join(_nl_,$form);
}else{$output=join(_nl_,$r
eport);}
}else{$output='No records were found!';}
return($output);
}
function tracer($content){
echo '<pre>';
var_dump($content);
echo '</pre>';
}
?>