-1

I have designed a script which will upload log file data to oracle database. However i want to filter images in my data upload. Here in this case,i don't want my code to upload CLIENT_REQUEST("GET /icons/back.gif HTTP/1.1") having .gif extensions in my request. Can someone please help me with the code.

My LOG file :-

127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/back.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/blank.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/unknown.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/image2.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/text.gif HTTP/1.1",304,-,"-"

My php code

<?php
$conn = oci_connect('XYZ', 'XYZxyz1', 'abcdef1/ABC');
if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
else {

$d = new DateTime();
$yesterday = $d->sub(new DateInterval('P1D'))->format('Y.m.d');
$filename = "access.$yesterday.txt";


$myfile = fopen($filename, "r") or die("Unable to open file!");
while(!feof($myfile)) {
    $content= fgets($myfile);
    $carray=explode(',',$content);
    list($IP_ADDRESS, $USER_IDENTIFIER, $USERID , $REQUEST_TIME , $CLIENT_REQUEST ,$RESPONSE_CODE ,$SIZEOFOBJECT, $COOKIES)=$carray;

    $stdii = 'INSERT INTO LOGS(IP_ADDRESS, USER_IDENTIFIER, USERID , REQUEST_TIME , CLIENT_REQUEST ,RESPONSE_CODE ,SIZEOFOBJECT, COOKIES)'.
    'values(:IP_ADDRESS, :USER_IDENTIFIER, :USERID , :REQUEST_TIME , :CLIENT_REQUEST ,:RESPONSE_CODE ,:SIZEOFOBJECT, :COOKIES)';
    $compiled1 = oci_parse($conn, $stdii);
    oci_bind_by_name($compiled1, ':IP_ADDRESS', $IP_ADDRESS);
    oci_bind_by_name($compiled1, ':USER_IDENTIFIER', $USER_IDENTIFIER);
    oci_bind_by_name($compiled1,':USERID', $USERID);
    oci_bind_by_name($compiled1, ':REQUEST_TIME', $REQUEST_TIME);
    oci_bind_by_name($compiled1, ':CLIENT_REQUEST', $CLIENT_REQUEST);
    oci_bind_by_name($compiled1, ':RESPONSE_CODE', $RESPONSE_CODE);
    oci_bind_by_name($compiled1, ':SIZEOFOBJECT', $SIZEOFOBJECT);
    oci_bind_by_name($compiled1, ':COOKIES', $COOKIES);
    oci_execute($compiled1, OCI_COMMIT_ON_SUCCESS);
}
}
echo "File Uploaded";
oci_close($conn);
fclose($myfile);
?>
SSneha
  • 75
  • 10
  • Possible duplicate of [php check file extension in upload form](https://stackoverflow.com/questions/10456113/php-check-file-extension-in-upload-form) – Ne Ma Aug 06 '18 at 08:20
  • Here in my code i don't want to filter my filename which i want to upload in database. I want to filter my data which i am uploading. In this code i want to put filters on CLIENT_REQUEST so that it doesn't upload .gif or .jpg etc requests. "GET /icons/back.gif HTTP/1.1" – SSneha Aug 06 '18 at 09:08

1 Answers1

0

If i understand correctly from your comment you want to add everything except images(gif, jpeg, etc) into LOGS table. There is several ways you can achieve this.

Do a string length check. This removes the check value from the log and if it does not match the original you have an image.

while() {
    if (strlen(str_replace(['.gif', '.jpeg'], '', $log)) !== strlen($log)) {
        // Found an image
        continue;
    }

    // Insert
}

Or do a regular expression check which searches the log for any matching strings.

while() {
     if (preg_match("/(.*)(gif|jpeg)(.*)/", $log);) {
         // Found an image
         continue;
     }

     // Insert
}

Or iterate an array of banned strings and use strpos to check if the log contains any of the banned strings.

$bannedStrings = ['.gif', '.jpeg'];

while() {
    foreach ($bannedStrings as $string) {
        if (strpos($log, $string) !== false) {
            // Found a banned string
            continue;
        }
    }

    // Insert
}

Stackoverflows editor keeps messing up the links to the docs that I am trying to add so giving up.

  1. How do I check if a string contains a specific word?
  2. http://php.net/manual/en/function.preg_match.php
  3. http://php.net/manual/en/function.str_replace.php
  4. http://php.net/manual/en/function.strlen.php
  5. http://php.net/manual/en/function.strpos.php
Ne Ma
  • 1,719
  • 13
  • 19