#!/usr/bin/php # http://www.julianmenendez.es # # Initially a PHPrewrite of: # http://wpkg.org/email2fax/index.php/Main_Page # # Feel free to use it, but give credit if you do ;) # # You will need # * Working Hylafax installation # * RipMIME: http://www.pldaniels.com/ripmime/ # * MTA configured to call this script # # Testing # cat mailmessage | email2fax # Example: # cat mailmessage | email2fax julianjm@gmail.com 003492800000@fax.yourdomain # # 2007-01-12: * Eliminada la funcion file_get_contents, por ser solo para PHP5 # * Eliminado parametro -S de sendfax (no disponible en todas las # distribuciones # * Anyadida mas depuracion, controlada mediante define DEBUG # 2007-07-30: * Aditional debug, in $tmpdir/results define("RIPMIME","/usr/local/bin/ripmime"); define("SENDFAX","/usr/bin/sendfax"); define("DEBUG",false); //Poner a true para mensajes de depuracion $warningmsg=""; $errormsg=""; $argv=$_SERVER['argv']; $argc=$_SERVER['argc']; if ($argc<2) die("{$argv[0]} \n"); $sender=$argv[1]; $dest=$argv[2]; $tmp=split("@",$dest); $destfax=$tmp[0]; ///////////// COMPROBAR destfax if (!is_numeric($destfax)) die("Destino no numerico..."); $prefix=date("YmdHis"); $tmpdir=tempdir("/tmp","faxjob_${prefix}_"); debug("Creando directorio temporal: $tmpdir\n\n"); $filesdir=$tmpdir."/files"; $messagefile=$tmpdir."/msg"; $headersfile=$tmpdir."/headers"; //Volcamos el mensaje original a un archivo temporal $msg=dumpstdin($messagefile); //Creamos el directorio donde almacenar los adjuntos mkdir($filesdir); exec(RIPMIME." -i $messagefile -d $filesdir --no-nameless --recursion-max 2 --no-tnef --no-ole --no-mht -v --verbose-contenttype"); $files=get_file_list($filesdir); debug("Ficheros Adjuntos:\n"); $filelist=""; foreach ($files as $file=>$type) { debug("\t$file ($type)\n"); switch ($type) { case "application/pdf": $filelist.=" ". escapeshellarg("$filesdir/$file"); break; // EJEMPLO DE SOPORTE DE NUEVOS TIPOS DE FICHERO // case "application/ms-word": // // Convertimos $file a $file.doc // conversor_word_a_pdf("$filesdir/$file","$filesdir/$file.doc"); // $filelist.=" ". escapeshellarg("$filesdir/$file.doc"); // break; default: //No hacemos nada, ignorando el archivo $warningmsg.="\nIgnorando archivo $file. Tipo $type no soportado."; } } debug("\n"); preg_match('/Subject:(.+)/',$msg,$matches); $subject=trim($matches[1]); preg_match('/From:(.+)/',$msg,$matches); $from=trim($matches[1]); debug("Remitente: $from\n\n"); debug("Destino: $destfax\n\n"); $faxcmd=sprintf(SENDFAX." -n -l -D -f %s -r %s $filelist -F %s -d %s", escapeshellarg($from), escapeshellarg($subject), escapeshellarg("De %%s|%c|Pagina %%P de %%T"), escapeshellarg($destfax)); debug("Ejecutando: $faxcmd\n\n"); exec ("echo \"$faxcmd\" > $tmpdir/cmd"); exec($faxcmd ."> $tmpdir/result"); debug("\n"); exit(0); ////////////////////////// ////////////////////////// function debug($str) { if (defined("DEBUG") && DEBUG==true) echo $str; } function get_file_list($dir) { $res=array(); if (is_dir($dir)) { if ($dh=opendir($dir)) { while(($file=readdir($dh)) !== false) { if (filetype($dir."/".$file)=='dir') continue; $type=mime_content_type($dir."/".$file); $res[$file]=$type; } closedir($dh); } } return $res; } function dumpstdin($filename) { $handle = fopen("php://stdin", "rb"); $cnt = ''; while (!feof($handle)) { $cnt .= fread($handle, 8192); } fclose($handle); $handle = fopen($filename,"wb"); fwrite($handle,$cnt); fclose($handle); return $cnt; } function tempdir($dir, $prefix='', $mode=0700) { if (substr($dir, -1) != '/') $dir .= '/'; do { $path = $dir.$prefix.mt_rand(0, 9999999); } while (!mkdir($path, $mode)); return $path; } ?>