From 325c9094326166bd998bf3aa328862ea717d7b0a Mon Sep 17 00:00:00 2001 From: james Date: Wed, 28 Nov 2007 18:46:32 +0000 Subject: [PATCH] Dont check CURL support inside the loop and dont allow checking to be called without CURL support Remove awards that were previously added but now dissappeared from the XML import --- admin/award_download.php | 438 +++++++++++++++++++++------------------ db/db.code.version.txt | 2 +- db/db.update.70.sql | 1 + 3 files changed, 240 insertions(+), 201 deletions(-) create mode 100644 db/db.update.70.sql diff --git a/admin/award_download.php b/admin/award_download.php index c6c28ad..eba3210 100644 --- a/admin/award_download.php +++ b/admin/award_download.php @@ -30,13 +30,19 @@ 'Administration' => 'admin/index.php', 'Awards Main' => 'admin/awards.php' )); + function array_remove($needle,&$haystack) { + if(in_array($needle,$haystack)) + unset($haystack[array_search($needle,$haystack)]); + } + if($_GET['action']=="check") { if(count($_GET['check'])) { require_once("xml.inc.php"); - foreach($_GET['check'] AS $ch) { - $q=mysql_query("SELECT * FROM award_sources WHERE id='".intval($ch)."'"); + foreach($_GET['check'] AS $checksource) { + $checksource=intval($checksource); + $q=mysql_query("SELECT * FROM award_sources WHERE id='$checksource'"); $r=mysql_fetch_object($q); echo i18n("Checking %1 for awards...",array($r->name)); echo "
"; @@ -52,219 +58,235 @@ xmlCreateRecurse($req); $xmldata=$output; - if(function_exists('curl_init')) - { - $ch = curl_init(); /// initialize a cURL session - curl_setopt ($ch, CURLOPT_URL,"$r->url"); - curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control - curl_setopt ($ch, CURLOPT_POST, 1); /// tell it to make a POST, not a GET - curl_setopt ($ch, CURLOPT_POSTFIELDS, "xml=".$xmldata); /// put the query string here starting with "?" - curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $datastream - curl_setopt ($ch, CURLOPT_POSTFIELDSIZE, 0); - curl_setopt ($ch, CURLOPT_TIMEOUT, 360); - curl_setopt ($ch, CURLOPT_SSLVERSION, 3); - $datastream = curl_exec ($ch); /// execute the curl session and return the output to a variable $datastream - $datastream = str_replace(" standalone=\"yes\"","",$datastream); - // echo "curl close
"; - curl_close ($ch); /// close the curl session + $ch = curl_init(); /// initialize a cURL session + curl_setopt ($ch, CURLOPT_URL,"$r->url"); + curl_setopt ($ch, CURLOPT_HEADER, 0); /// Header control + curl_setopt ($ch, CURLOPT_POST, 1); /// tell it to make a POST, not a GET + curl_setopt ($ch, CURLOPT_POSTFIELDS, "xml=".$xmldata); /// put the query string here starting with "?" + curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); /// This allows the output to be set into a variable $datastream + curl_setopt ($ch, CURLOPT_POSTFIELDSIZE, 0); + curl_setopt ($ch, CURLOPT_TIMEOUT, 360); + curl_setopt ($ch, CURLOPT_SSLVERSION, 3); + $datastream = curl_exec ($ch); /// execute the curl session and return the output to a variable $datastream + $datastream = str_replace(" standalone=\"yes\"","",$datastream); + // echo "curl close
"; + curl_close ($ch); /// close the curl session - echo i18n("The Remote Server said:")."
"; - //echo notice($datastream); - $response=xml_parsexml($datastream); - $keys=array_keys($response); - if($keys[0]=="awardresponse") { - echo ""; - $ar=$response['awardresponse'][0]; - $postback=$ar['postback'][0]; - echo i18n("Postback URL: %1",array($postback))."
"; - $numawards=count($ar['awards'][0]['award']); - echo i18n("Number of Awards: %1",array($numawards))."
"; - foreach($ar['awards'][0]['award'] AS $award) { - $identifier=$award['identifier'][0]; - $year=$award['year'][0]; - echo i18n("Award Identifier: %1",array($identifier))."   "; - echo i18n("Award Year: %1",array($year))."
"; - if($year==$config['FAIRYEAR']) { - $tq=mysql_query("SELECT * FROM award_awards WHERE external_identifier='$identifier' AND year='$year'"); - if($awardrecord=mysql_fetch_object($tq)) { - echo i18n("Award already exists, updating info")."
"; + echo i18n("The Remote Server said:")."
"; + //echo notice($datastream); + $response=xml_parsexml($datastream); + $keys=array_keys($response); + if($keys[0]=="awardresponse") { - //check if the sponsor exists, if not, add them - $sponsorq=mysql_query("SELECT * FROM award_sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."'"); - if($sponsorr=mysql_fetch_object($sponsorq)) { - $sponsor_id=$sponsorr->id; - } - else - { - mysql_query("INSERT INTO award_sponsors (organization,year,notes,confirmed) VALUES ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')"); - $sponsor_id=mysql_insert_id(); - } + //get a list of all the existing awards for this external source + $aq=mysql_query("SELECT * FROM award_awards WHERE award_sources_id='$checksource' AND year='".$config['FAIRYEAR']."'"); + $existingawards=array(); + while($ar=mysql_fetch_object($aq)) { + $existingawards[]=$ar->id; + } + + echo ""; + $ar=$response['awardresponse'][0]; + $postback=$ar['postback'][0]; + echo i18n("Postback URL: %1",array($postback))."
"; + $numawards=count($ar['awards'][0]['award']); + echo i18n("Number of Awards: %1",array($numawards))."
"; + foreach($ar['awards'][0]['award'] AS $award) { + $identifier=$award['identifier'][0]; + $year=$award['year'][0]; + echo i18n("Award Identifier: %1",array($identifier))."   "; + echo i18n("Award Year: %1",array($year))."
"; + echo i18n("Award Name: %1",array($award['name_en'][0]))."
"; - mysql_query("UPDATE award_awards SET - award_sponsors_id='$sponsor_id', - name='".mysql_escape_string($award['name_en'][0])."', - criteria='".mysql_escape_string($award['criteria_en'][0])."', - external_postback='".mysql_escape_string($postback)."' - WHERE - id='$awardrecord->id' - AND external_identifier='".mysql_escape_string($identifier)."' - AND year='$year' - "); + if($year==$config['FAIRYEAR']) { + $tq=mysql_query("SELECT * FROM award_awards WHERE external_identifier='$identifier' AND year='$year'"); + if($awardrecord=mysql_fetch_object($tq)) { + echo i18n("Award already exists, updating info")."
"; + + //remove it from the existingawards list + array_remove($awardrecord->id,$existingawards); + + + //check if the sponsor exists, if not, add them + $sponsorq=mysql_query("SELECT * FROM award_sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."'"); + if($sponsorr=mysql_fetch_object($sponsorq)) { + $sponsor_id=$sponsorr->id; + } + else + { + mysql_query("INSERT INTO award_sponsors (organization,year,notes,confirmed) VALUES ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')"); + $sponsor_id=mysql_insert_id(); + } + + mysql_query("UPDATE award_awards SET + award_sponsors_id='$sponsor_id', + name='".mysql_escape_string($award['name_en'][0])."', + criteria='".mysql_escape_string($award['criteria_en'][0])."', + external_postback='".mysql_escape_string($postback)."' + WHERE + id='$awardrecord->id' + AND external_identifier='".mysql_escape_string($identifier)."' + AND year='$year' + "); echo mysql_error(); - //update the prizes - if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0)) - { - $prizes=$award['prizes'][0]['prize']; - $numprizes=count($prizes); - echo i18n("Number of prizes: %1",array($numprizes))."
"; - $pq=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='$awardrecord->id'"); - //get a list of all the existing prizes - $existingprizes=array(); - while($pr=mysql_fetch_object($pq)) { - $existingprizes[$pr->external_identifier]=$pr; - } - - foreach($prizes AS $prize) { - //if it exists -> update it - if(array_key_exists($prize['identifier'][0],$existingprizes)) { - $ep=$existingprizes[$prize['identifier'][0]]; - - echo " ".i18n("Updating prize %1",array($ep->external_identifier))."
"; - mysql_query("UPDATE award_prizes SET - cash='".intval($prize['cash'][0])."', - scholarship='".intval($prize['scholarship'][0])."', - value='".intval($prize['value'][0])."', - prize='".mysql_escape_string($prize['prize_en'][0])."', - number='".intval($prize['number'][0])."', - `order`='".intval($prize['ord'][0])."' - WHERE - id='$ep->id'"); - - //remove it from the list - unset($existingprizes[$ep->external_identifier]); - } - else { //if it doesnt exist -> add it - echo " ".i18n("Adding prize %1",array($prize['identifier'][0]))."
"; - - mysql_query("INSERT INTO award_prizes ( - award_awards_id, - cash, - scholarship, - value, - prize, - number, - `order`, - `year`, - external_identifier - ) VALUES ( - '$awardrecord->id', - '".intval($prize['cash'][0])."', - '".intval($prize['scholarship'][0])."', - '".intval($prize['value'][0])."', - '".mysql_escape_string($prize['prize_en'][0])."', - '".intval($prize['number'][0])."', - '".intval($prize['ord'][0])."', - '$year', - '".mysql_escape_string($prize['identifier'][0])."' - )"); - - } - } - //if an entry exists thats not in the xml -> delete it - foreach($existingprizes AS $ep) { - echo " ".i18n("Removing prize %1",array($ep->external_identifier))."
"; - mysql_query("DELETE FROM award_prizes WHERE id='$ep->id'"); - } - } - //FIXME: update the translations - } - else { - //check if the sponsor exists, if not, add them - $sponsorq=mysql_query("SELECT * FROM award_sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."'"); - if($sponsorr=mysql_fetch_object($sponsorq)) { - $sponsor_id=$sponsorr->id; - } - else - { - mysql_query("INSERT INTO award_sponsors (organization,year,notes,confirmed) VALUES ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')"); - $sponsor_id=mysql_insert_id(); + //update the prizes + if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0)) + { + $prizes=$award['prizes'][0]['prize']; + $numprizes=count($prizes); + echo i18n("Number of prizes: %1",array($numprizes))."
"; + $pq=mysql_query("SELECT * FROM award_prizes WHERE award_awards_id='$awardrecord->id'"); + //get a list of all the existing prizes + $existingprizes=array(); + while($pr=mysql_fetch_object($pq)) { + $existingprizes[$pr->external_identifier]=$pr; } - echo i18n("Award does not exists, adding...")."
"; - mysql_query("INSERT INTO award_awards ( - award_types_id, - award_sponsors_id, - name, - criteria, - year, - external_identifier, - external_postback - ) - VALUES ( - 2, - $sponsor_id, - '".mysql_escape_string($award['name_en'][0])."', - '".mysql_escape_string($award['criteria_en'][0])."', - '".$year."', - '".mysql_escape_string($identifier)."', - '".mysql_escape_string($postback)."' - )"); - $award_id=mysql_insert_id(); + foreach($prizes AS $prize) { + //if it exists -> update it + if(array_key_exists($prize['identifier'][0],$existingprizes)) { + $ep=$existingprizes[$prize['identifier'][0]]; + + echo " ".i18n("Updating prize %1",array($ep->external_identifier))."
"; + mysql_query("UPDATE award_prizes SET + cash='".intval($prize['cash'][0])."', + scholarship='".intval($prize['scholarship'][0])."', + value='".intval($prize['value'][0])."', + prize='".mysql_escape_string($prize['prize_en'][0])."', + number='".intval($prize['number'][0])."', + `order`='".intval($prize['ord'][0])."' + WHERE + id='$ep->id'"); + + //remove it from the list + unset($existingprizes[$ep->external_identifier]); + } + else { //if it doesnt exist -> add it + echo " ".i18n("Adding prize %1",array($prize['identifier'][0]))."
"; - //import the prizes - if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0)) - { - $prizes=$award['prizes'][0]['prize']; - $numprizes=count($prizes); - echo i18n("Number of prizes: %1",array($numprizes))."
"; - foreach($prizes AS $prize) { mysql_query("INSERT INTO award_prizes ( - award_awards_id, - cash, - scholarship, - value, - prize, - number, - `order`, - `year`, - external_identifier - ) VALUES ( - '$award_id', - '".intval($prize['cash'][0])."', - '".intval($prize['scholarship'][0])."', - '".intval($prize['value'][0])."', - '".mysql_escape_string($prize['prize_en'][0])."', - '".intval($prize['number'][0])."', - '".intval($prize['ord'][0])."', - '$year', - '".mysql_escape_string($prize['identifier'][0])."' - )"); + award_awards_id, + cash, + scholarship, + value, + prize, + number, + `order`, + `year`, + external_identifier + ) VALUES ( + '$awardrecord->id', + '".intval($prize['cash'][0])."', + '".intval($prize['scholarship'][0])."', + '".intval($prize['value'][0])."', + '".mysql_escape_string($prize['prize_en'][0])."', + '".intval($prize['number'][0])."', + '".intval($prize['ord'][0])."', + '$year', + '".mysql_escape_string($prize['identifier'][0])."' + )"); + } } - else - echo error(i18n("No prizes associated with this award")); - - //FIXME: import the translations & prize translations + //if an entry exists thats not in the xml -> delete it + foreach($existingprizes AS $ep) { + echo " ".i18n("Removing prize %1",array($ep->external_identifier))."
"; + mysql_query("DELETE FROM award_prizes WHERE id='$ep->id'"); + } } + //FIXME: update the translations } - else - { - echo error(i18n("Award is not for the current fair year... skipping")); - } + else { + //check if the sponsor exists, if not, add them + $sponsorq=mysql_query("SELECT * FROM award_sponsors WHERE organization='".mysql_escape_string($award['sponsor'][0])."'"); + if($sponsorr=mysql_fetch_object($sponsorq)) { + $sponsor_id=$sponsorr->id; + } + else + { + mysql_query("INSERT INTO award_sponsors (organization,year,notes,confirmed) VALUES ('".mysql_escape_string($award['sponsor'][0])."','$year','".mysql_escape_string("Imported from external source: $r->name")."','yes')"); + $sponsor_id=mysql_insert_id(); + } + echo i18n("Award does not exists, adding...")."
"; + mysql_query("INSERT INTO award_awards ( + award_types_id, + award_sponsors_id, + name, + criteria, + year, + external_identifier, + external_postback, + award_sources_id + ) + VALUES ( + 2, + $sponsor_id, + '".mysql_escape_string($award['name_en'][0])."', + '".mysql_escape_string($award['criteria_en'][0])."', + '".$year."', + '".mysql_escape_string($identifier)."', + '".mysql_escape_string($postback)."', + '$checksource' + )"); + $award_id=mysql_insert_id(); + + //import the prizes + if(is_array($award['prizes'][0]) && count($award['prizes'][0]['prize']>0)) + { + $prizes=$award['prizes'][0]['prize']; + $numprizes=count($prizes); + echo i18n("Number of prizes: %1",array($numprizes))."
"; + foreach($prizes AS $prize) { + mysql_query("INSERT INTO award_prizes ( + award_awards_id, + cash, + scholarship, + value, + prize, + number, + `order`, + `year`, + external_identifier + ) VALUES ( + '$award_id', + '".intval($prize['cash'][0])."', + '".intval($prize['scholarship'][0])."', + '".intval($prize['value'][0])."', + '".mysql_escape_string($prize['prize_en'][0])."', + '".intval($prize['number'][0])."', + '".intval($prize['ord'][0])."', + '$year', + '".mysql_escape_string($prize['identifier'][0])."' + )"); + } + } + else + echo error(i18n("No prizes associated with this award")); + + //FIXME: import the translations & prize translations + } } - echo "
"; + else + { + echo error(i18n("Award is not for the current fair year... skipping")); + } + echo "
"; + + } + echo "
"; + + //remove any awards that are left in the $existingawards array, they must have been removed from the source + foreach($existingawards AS $aid) { + echo i18n("Removing award id %1 that was removed from external source",array($aid)); + mysql_query("DELETE FROM award_prizes WHERE award_awards_id='$aid'"); + mysql_query("DELETE FROM award_awards WHERE id='$aid'"); } - else - echo error(i18n("Invalid XML response. Expecting '%1', received '%2'",array("awardresponse",$keys[0]))); - } else - { - echo error("CURL Support Missing"); - echo i18n("Your PHP installation does not support CURL. You will need to login to the YSF system as the regional coodinator and upload the XML data manually"); - } + echo error(i18n("Invalid XML response. Expecting '%1', received '%2'",array("awardresponse",$keys[0]))); + echo "
"; } } @@ -273,6 +295,16 @@ echo mysql_error(); } else { + + if(!function_exists('curl_init')) + { + echo error(i18n("CURL Support Missing")); + echo notice(i18n("Your PHP installation does not support CURL. You will need to have CURL support added by your system administrator before being able to access external award sources")); + $links=false; + } + else + $links=true; + $q=mysql_query("SELECT * FROM award_sources ORDER BY name"); echo ""; echo ""; @@ -283,14 +315,20 @@ echo mysql_error(); echo ""; echo "\n"; echo ""; - echo ""; + echo ""; echo ""; $checkurl.="&check[]=$r->id"; } echo "
".i18n("Source Name")."
$r->name$r->urlid\">".i18n("check").""; + if($links) + echo "id\">".i18n("check").""; + else + echo "n/a"; + echo "
\n"; echo "
"; - - echo "".i18n("Check all sources").""; + + if($links) + echo "".i18n("Check all sources").""; } send_footer(); diff --git a/db/db.code.version.txt b/db/db.code.version.txt index b5489e5..2bbd69c 100644 --- a/db/db.code.version.txt +++ b/db/db.code.version.txt @@ -1 +1 @@ -69 +70 diff --git a/db/db.update.70.sql b/db/db.update.70.sql new file mode 100644 index 0000000..4191b38 --- /dev/null +++ b/db/db.update.70.sql @@ -0,0 +1 @@ +ALTER TABLE `award_awards` ADD `award_sources_id` INT UNSIGNED DEFAULT NULL ;