Monthly Archives: July 2009

Restore spam email amavis with amavisd-release

To restore email marked as spam by amavis we going to use the command amavisd-release

First edit the amavisd-release script to ensure that $socketname is properly configured, got to be something like this:

 $socketname = '/var/vscan/amavisd.sock';

next check the log file for the serial of the mail, eg:

mavis[29297]: (29297-01-6) Blocked SPAM, ... <...> -> <...>,
  quarantine: spam/U/UM3XM3XDbN52.gz,
              ^^^^^^^^^^^^^^^^^^^^^^
  Message-ID:<...>, mail_id: UM3XM3XDbN52, Hits: 13.365,

and to recover that specific message run the command:

 amavisd-release spam-UM3XM3XDbN52.gz mail@example.com

where mail@example.com is the recipient address that will receive the recovered email

Regex 01 – Alphanumeric Strings

// Allow alphanumeric only
$pattern = "/^[A-Za-z0-9]+$/";

// Allow alphanumeric with spaces
$pattern = "/^[A-Za-z0-9 ]+$/ ";

// Allow alphanumeric with underscore and dashes
$pattern = "/^[A-Za-z0-9_-]+$/";

// Allow alphanumeric with underscore, dashes, and spaces
$pattern = "/^[A-Za-z0-9_- ]+$/";

Testing Strings Using preg_match()

if( preg_match( $pattern, "This is a string 1234" ) ) {
    echo "Match";
} else {
    echo "No match";
}

Upload files using PHP Function

UPDATE: DON’T USE THIS I WAS YOUNG AND STUPID!

Here is a simple couple of function that allow you to upload files to your server, read the php comments for how to

	/* Upload file to the webserver
	 * Parameters:
	 * $fileObjectArray - The $_FILE array, you should pass something like $_FILE['myUploadedFile']
	 * $AllowExtensions - The extensions that you want to allow to be uploaded - Eg: $AllowExtensions = array('jpg','gif'); 
	 * $AllowedSize - Max allowed size in Bytes
	 * $destinyFolder - Your files destiny folder - Eg: Uploads
	 * $fileName - Optinional file name, if you don't pass it, will be generated a unique file name
	 */
	function uploadFile($fileObjectArray, $AllowExtensions, $AllowedSize, $destinyFolder, $fileName = null){
		/* $_FILE Array proprieties explanation
		* [name] => MyFile.txt (comes from the browser, so treat as tainted)
		* [type] => text/plain  (not sure where it gets this from - assume the browser, so treat as tainted)
		* [tmp_name] => /tmp/php/php1h4j1o (could be anywhere on your system, depending on your config settings, but the user has no control, so this isn't tainted)
		* [error] => UPLOAD_ERR_OK  (= 0)
		* [size] => 123   (the size in bytes)
		*/

		/* Error values
		* 1 - $_FILE array not been set
		* 2 - File extension not allowed
		* 3 - File size bigger than defined
		* 4 - Could not move file from tmp do specified location
		* 5 - destiny folder does not exists
		*/

		//Check if $_FILE as been set
		if ($fileObjectArray['error'] != 0) {
			return 1;
		}else{
			$formFileName = $fileObjectArray['name'];
			$formFileExtension = $this->findFileExtension($formFileName);
			$formTmpPath = $fileObjectArray['tmp_name'];
			$formFileSize = $fileObjectArray['size'];
		}

		//Check if a custom file name as been passed if not gives unique id to it
		if ($this->is_empty($fileName)) {
			$fileName = $this->unique_id().'.'.$formFileExtension;
		}

		//Validates extension based on $AllowExtensions array parameter
		foreach ($AllowExtensions as $ext){
			if ($ext == $formFileExtension){
				break;
			}else{
				return 2;
			}
		}

		//Validates filesize based on $AllowedSize parameter
		if ($formFileSize > $AllowedSize) {
			return 3;
		}
		
		if (!file_exists($destinyFolder)) {
			return 5;
		}

		//Main part
		$fullDestinyPath = $destinyFolder.'/'.$fileName;
		if (!move_uploaded_file($formTmpPath,$fullDestinyPath)) {
			return 4;
		}else{
			return $fileName;
		}

	}

	function unique_id(){
		$better_token = md5(uniqid(rand(), true));
		$unique_code = substr($better_token, 16);
		$uniqueid = $unique_code;
		return $uniqueid;
	}


	function is_empty($var, $allow_false = false, $allow_ws = false) {
		if (!isset($var) || is_null($var) || ($allow_ws == false && trim($var) == "" && !is_bool($var)) || ($allow_false === false && is_bool($var) && $var === false) || (is_array($var) && empty($var))) {
			return true;
		} else {
			return false;
		}
	}

	function findFileExtension($filename){
		$filename = strtolower($filename) ;
		$exts = split("[/\\.]", $filename) ;
		$n = count($exts)-1;
		$exts = $exts[$n];
		return $exts;
	}