[output file]'.PHP_EOL; // prints text in quotes ('), PHP_EOL appends ("." before PHP_EOL is appending operator) a newline to the // text (moving the cursor to new line) exit; // stops program execution here } $key_len = strlen($key); // $key_len now contains the length of $key, in number of characters echo 'Crypting '.$fname." ...".PHP_EOL; $out = ''; // initialization of $out variable, it will hold encrypted bytes $cryption = false; // control variable, will be used later to test if we are crypting or decrypting if (USE_COMPRESSION) { // if compression is ON (USE_COMPRESSION = true) if ($fp = fopen($fname, 'rb')) { // open the input file for reading $header = fread($fp, strlen(C_HEADER)); // read header of the file and store into $header variable fclose($fp); // close the open file if ($header != C_HEADER) { // if the file is not already compressed file_put_contents($fname, gzdeflate(file_get_contents($fname))); // compress (gzdeflate) the file $out = C_HEADER; // insert header on the beginning, indicating compression was used; $cryption = true; } else // otherwise, remove the header before decryption file_put_contents($fname, preg_replace('/^'.preg_quote(C_HEADER, '/').'/', '', file_get_contents($fname))); } else die('Cannot open file'); } if ($fp = fopen($fname, 'rb')) { // fopen opens the file having a filename (filepath) equal to a string (array of characters) contained in $fname variable // in this case, file is opened for reading (specified by 'r' parameter to fopen function) // the return of fopen is saved in $fp variable and tested by "if" statement (fopen returns "false" on failure, otherwise it returns a file handle (pointer) // which will evaluate to "true" when tested by "if") // if the expression within () evaluates to "true", the code within {} is executed, otherwise the code enclosed by {} after the "else" statement is executed while (!feof($fp)) { // "while" repeatedly executes a code contained within {} as long as the condition inside () is satisfied // feof returns returns "true" if end of file (pointed to by $fp) has been reached (ie. by sequential reading) // in this case "while" executes code as long as !feof evaluates to "true" (here "!" is a function of inversion, if feof returns false, !feof will return true) $line = fread($fp, $key_len); // reads a maximum of $key_len bytes from a file pointed to by $fp and store in $line variable (bytes read will be less than $key_len if we are near the // end of file and number of bytes remaining is lower than $key_len if ($line === false) { // if reading failed (fread returns "false" on failure) execute code below (enclosed by {}) echo 'ERROR: read failed!'.PHP_EOL; exit; } $out .= $line ^ $key; // encrypts content stored in $line and appends encrypted content to content of $out variable (empty in the beginning) // the operator ".=" is used for appending (using assignment operator "=" would overwrite existing content) // note that encryption is simply an XOR operation (executed by "^" operator) between bits stored in $line and $key } fclose($fp); // closes the file (freeing memory by discarding the $fp pointer, so it cannot be used again as a reference to previously opened file) } else { echo 'ERROR: Could not read '.$fname.'!'.PHP_EOL; exit; } if (USE_COMPRESSION && !$cryption) // if compression is ON (USE_COMPRESSION = true) and we are not crypting ($cryption is false) $out = gzinflate($out); // decompress (gzinflate) the input if ($fname2 != '') { // if $fname2 is not empty (in this case $fname2 holds the filename of output file) if (file_put_contents($fname2, $out) !== false) // tries to save the contents of $out variable to a file whose filename is stored in $fname2 echo 'Output saved to '.$fname2.PHP_EOL; // if successful (file_put_contents did not return "false") print confirmation else // otherwise echo 'ERROR: Could not save output to '.$fname2.'!'.PHP_EOL; // print error } else echo $out; // in case $fname2 is empty (filename of output file was not specified), contents of $out variable (encrypted bytes) are printed to screen