PHP WebService - Accepts 3DES encrypted file and parse it to database
This webService implements, uploadFile method. After file acceptance, 3DES decryption is executed and the file content is inserted to CMS database.
<?php
require_once('./lib/nusoap.php');
global $lingua, $filtro_categorias;
date_default_timezone_set('Europe/Lisbon');
// webservice's folder
$wsfolder = 'wsdlfolder';
// Declare a namespace to be used below
$ns = "http://www.mywebsiteurl.com/$wsfolder";
// Setup the WSDL
$server = new soap_server();
$server->debug_flag = false;
$server->configureWSDL('FileTransferWSDL', $ns);
$server->wsdl->schemaTargetNamespace = $ns;
// Register the method to uplaod a file
$server->register('uploadFile', // method name
array('bytes' => 'xsd:string', 'filename' => 'xsd:string'), // input parameters
array('return' => 'xsd:boolean'), // output parameters
$ns, // namespace
$ns . '#uploadFile', // soapaction
'rpc', // style
'encoded', // use
'Upload a File' // documentation);
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
// Functions which are called when the above methods are used
function uploadFile($bytes, $filename)
{
$uploadFolder = "upload/" . date("Ymd");
if (!is_dir($uploadFolder))
mkdir($uploadFolder);
// Saveing file to server
$filePath = "$uploadFolder/$filename";
$fp = fopen($filePath, "w");
fwrite($fp, base64_decode($bytes));
fclose($fp);
xmlTOdatabase($filePath);
return;
}
function xmlTOdatabase($filePath)
{
$3des_key = "16_characters_length";
$fp = fopen($filePath, "r");
$fileContent = fread($fp, '9999');
fclose($fp);
$fileContentDecrypt = decrypt3DES($fileContent, $3des_key);
$xmlObject = simplexml_load_string($fileContentDecrypt, 'SimpleXMLElement', LIBXML_NOCDATA);
$item[providerid] = utf8_decode(strip_tags($xmlObject->NewsItem->Identification->NewsIdentifier->NewsItemId));
$item[title] = utf8_decode(strip_tags(trim($xmlObject->NewsItem->NewsComponent->NewsLines->HeadLine)));
$item[subtithe] = utf8_decode(strip_tags(trim($xmlObject->NewsItem->NewsComponent->NewsLines->SubHeadLine)));
$item[author] = utf8_decode(trim($xmlObject->NewsItem->NewsComponent->NewsLines->ByLine));
$item[date] = formatDate(utf8_decode(trim($xmlObject->NewsItem->NewsComponent->NewsLines->DateLine)));
$item[source] = utf8_decode(trim($xmlObject->NewsItem->NewsComponent->NewsLines->CreditLine));
$item[copyright] = utf8_decode(trim($xmlObject->NewsItem->NewsComponent->NewsLines->CopyrightLine));
$item[category] = (int)substr(strip_tags($xmlObject->NewsItem->NewsComponent->TopicSet->Topic[0]->FormalName), 0, 2);
$item[text] = utf8_decode(($xmlObject->NewsItem->NewsComponent->ContentItem->DataContent[0]));
$item[country] = utf8_decode(strip_tags($xmlObject->NewsItem->NewsComponent->DescriptiveMetadata->Property[Value]));
//print_r($item);
if ($item[providerid] && !itemExixts($item[providerid])) {
insertContent2DB($xmlObject);
}
return;
}
function decrypt3DES($string, $key)
{
$key_add = 24 - strlen($key);
$key .= substr($key, 0, $key_add);
$cipher_alg = MCRYPT_3DES;
$decrypted_string = mcrypt_decrypt($cipher_alg, $key, $string, MCRYPT_MODE_ECB, $iv);
return trim($decrypted_string);
}
function insertContent2DB($xmlObject)
{
//here, it depends on the used CMS
}
function itemExixts($providerid){
//here, it depends on the used CMS
}
function formatDate($date){
$aux = explode("-", $date);
return "--";
}
?>