vote.php
************
Código PHP :
<?php
require_once('xml_functions.php');
define('XML_PATH','../../'.$_POST['relativeXmlContentPath']);
//define('XML_PATH','poll.xml');
$pollId = $_POST['poll_id'];
$votedId = $_POST['vote_id'];
$votantIp = $_SERVER['REMOTE_ADDR'];
// restrict multiple votes from the same IP
$ipList = array();
$ipList_hdl = @fopen('iplist_'.$pollId.'.txt','a+');
if($ipList_hdl != null){
while(!feof($ipList_hdl)){
$one_ip = fgets($ipList_hdl);
$one_ip = str_replace(array("\r\n", "\n", "\r"),'',$one_ip);
if(strlen($one_ip)>6){
if($one_ip != $votantIp){
array_push($ipList,$one_ip);
}
else{
fclose($ipList_hdl);
die('2');
}
}
}
}
if(!file_exists(substr(XML_PATH,1))) {
die('0');
fclose($ipList_hdl);
}
if(!isset($votedId) or $votedId == ''){
die('0');
fclose($ipList_hdl);
}
$xml = xml2ary(@file_get_contents(substr(XML_PATH,1)));
$ok = false;
if(isset($xml['poll']['_c']['answer']) and sizeof($xml['poll']['_c']['answer'])>0){
$i = 0;
foreach($xml['poll']['_c']['answer'] as $answer){
if($answer['_a']['id'] == $votedId){
$xml['poll']['_c']['answer'][$i]['_a']['votes'] = $answer['_a']['votes'] + 1;
$f = @fopen(substr(XML_PATH,1),'w+');
fwrite($f,'<?xml version="1.0" encoding="UTF-8"?>'.ary2xml($xml));
fclose($f);
$ok = true;
}
$i++;
}
}
else{
die('0');
}
if($ok){
//log the votant IP
fwrite($ipList_hdl,$votantIp.'');
fclose($ipList_hdl);
echo '1';
}
else{
fclose($ipList_hdl);
echo '0';
}
?>xml_functions.php
*************************
Código PHP :
<?php
// XML to Array
function xml2ary(&$string) {
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $string, $vals, $index);
xml_parser_free($parser);
$mnary=array();
$ary=&$mnary;
foreach ($vals as $r) {
$t=$r['tag'];
if ($r['type']=='open') {
if (isset($ary[$t])) {
if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
$cv=&$ary[$t][count($ary[$t])-1];
} else $cv=&$ary[$t];
if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
$cv['_c']=array();
$cv['_c']['_p']=&$ary;
$ary=&$cv['_c'];
} elseif ($r['type']=='complete') {
if (isset($ary[$t])) { // same as open
if (isset($ary[$t][0])) $ary[$t][]=array(); else $ary[$t]=array($ary[$t], array());
$cv=&$ary[$t][count($ary[$t])-1];
} else $cv=&$ary[$t];
if (isset($r['attributes'])) {foreach ($r['attributes'] as $k=>$v) $cv['_a'][$k]=$v;}
$cv['_v']=(isset($r['value']) ? $r['value'] : '');
} elseif ($r['type']=='close') {
$ary=&$ary['_p'];
}
}
_del_p($mnary);
return $mnary;
}
// _Internal: Remove recursion in result array
function _del_p(&$ary) {
foreach ($ary as $k=>$v) {
if ($k==='_p') unset($ary[$k]);
elseif (is_array($ary[$k])) _del_p($ary[$k]);
}
}
// Array to XML
function ary2xml($cary, $d=0, $forcetag='') {
$res=array();
foreach ($cary as $tag=>$r) {
if (isset($r[0])) {
$res[]=ary2xml($r, $d, $tag);
} else {
if ($forcetag) $tag=$forcetag;
$sp=str_repeat("\t", $d);
$res[]="$sp<$tag";
if (isset($r['_a'])) {foreach ($r['_a'] as $at=>$av) $res[]=" $at=\"$av\"";}
$res[]=">".((isset($r['_c'])) ? "\n" : '');
if (isset($r['_c'])) $res[]=ary2xml($r['_c'], $d+1);
elseif (isset($r['_v'])) $res[]=$r['_v'];
$res[]=(isset($r['_c']) ? $sp : '')."</$tag>\n";
}
}
return implode('', $res);
}
// Insert element into array
function ins2ary(&$ary, $element, $pos) {
$ar1=array_slice($ary, 0, $pos); $ar1[]=$element;
$ary=array_merge($ar1, array_slice($ary, $pos));
}
?>eso seria y muchas muchas gracias por su pronta ayuda
gracias gracias
