306 lines
No EOL
11 KiB
PHP
306 lines
No EOL
11 KiB
PHP
<?php
|
||
|
||
|
||
/**
|
||
* Gmail attachment extractor.
|
||
*
|
||
* Downloads attachments from Gmail and saves it to a file.
|
||
* Uses PHP IMAP extension, so make sure it is enabled in your php.ini,
|
||
* extension=php_imap.dll
|
||
*
|
||
*/
|
||
//-----------------------------------------------------------------------
|
||
error_reporting(E_ALL);
|
||
ini_set('display_errors', 1);
|
||
//----------------------------------------------------------------------
|
||
$kw_aktuell = (int)date('W');
|
||
$a_jahr = (int)date('Y');
|
||
require('lib/config.php');
|
||
session_name(CONFIG_SESSION);
|
||
session_start(); require('lib/mysql.class.php');
|
||
$GLOBALS['mysql'] = new mysql(CONFIG_MYSQL_HOST,CONFIG_MYSQL_USER,CONFIG_MYSQL_PW,CONFIG_MYSQL_DB);
|
||
date_default_timezone_set('Europe/London');
|
||
//-----------------------------------------------------------------------
|
||
|
||
set_time_limit(3000);
|
||
|
||
|
||
/* connect to gmail with your credentials */
|
||
$hostname = '{mx2f41.netcup.net:143/imap/tls}INBOX';
|
||
$username = 'faz@jb-transport.de'; # e.g somebody@gmail.com
|
||
$password = 'laden0815';
|
||
|
||
|
||
/* try to connect */
|
||
$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error());
|
||
|
||
|
||
/* get all new emails. If set to 'ALL' instead
|
||
* of 'NEW' retrieves all the emails, but can be
|
||
* resource intensive, so the following variable,
|
||
* $max_emails, puts the limit on the number of emails downloaded.
|
||
*
|
||
*/
|
||
$emails = imap_search($inbox,'UNSEEN');
|
||
/* useful only if the above search is set to 'ALL' */
|
||
$max_emails = 9999;
|
||
|
||
|
||
/* if any emails found, iterate through each email */
|
||
if($emails) {
|
||
$count = 1;
|
||
/* put the newest emails on top */
|
||
rsort($emails);
|
||
/* for every email... */
|
||
foreach($emails as $email_number) {
|
||
/* get information specific to this email */
|
||
$overview = imap_fetch_overview($inbox,$email_number,0);
|
||
/* get mail message */
|
||
$message = imap_fetchbody($inbox,$email_number,2);
|
||
/* get mail structure */
|
||
$structure = imap_fetchstructure($inbox, $email_number);
|
||
$attachments = array();
|
||
/* if any attachments found... */
|
||
if(isset($structure->parts) && count($structure->parts)) {
|
||
for($i = 0; $i < count($structure->parts); $i++) {
|
||
$attachments[$i] = array(
|
||
'is_attachment' => false,
|
||
'filename' => '',
|
||
'name' => '',
|
||
'attachment' => ''
|
||
);
|
||
|
||
if($structure->parts[$i]->ifdparameters) {
|
||
foreach($structure->parts[$i]->dparameters as $object) {
|
||
if(strtolower($object->attribute) == 'filename') {
|
||
$attachments[$i]['is_attachment'] = true;
|
||
$attachments[$i]['filename'] = $object->value;
|
||
// echo $attachments[$i]['filename'];
|
||
}
|
||
}
|
||
}
|
||
|
||
if($structure->parts[$i]->ifparameters)
|
||
{
|
||
foreach($structure->parts[$i]->parameters as $object)
|
||
{
|
||
if(strtolower($object->attribute) == 'name')
|
||
{
|
||
$attachments[$i]['is_attachment'] = true;
|
||
$attachments[$i]['name'] = $object->value;
|
||
}
|
||
}
|
||
}
|
||
|
||
if($attachments[$i]['is_attachment'])
|
||
{
|
||
$attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1);
|
||
|
||
/* 4 = QUOTED-PRINTABLE encoding */
|
||
if($structure->parts[$i]->encoding == 3)
|
||
{
|
||
$attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']);
|
||
}
|
||
/* 3 = BASE64 encoding */
|
||
elseif($structure->parts[$i]->encoding == 4)
|
||
{
|
||
$attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/* iterate through each attachment and save it */
|
||
foreach($attachments as $attachment)
|
||
{
|
||
if($attachment['is_attachment'] == 1)
|
||
{
|
||
$filename = $attachment['name'];
|
||
if(empty($filename)) $filename = $attachment['filename'];
|
||
|
||
// if(empty($filename)) $filename = time() . ".dat";
|
||
|
||
/* prefix the email number to the filename in case two emails
|
||
* have the attachment with the same file name.
|
||
*/
|
||
|
||
$fp = fopen('upload/'.$filename, "w+"); // $email_number . "-" .
|
||
fwrite($fp, $attachment['attachment']);
|
||
fclose($fp);
|
||
|
||
}
|
||
//echo '##';
|
||
$filename='upload/'.$attachment['name'];
|
||
$datei=$attachment['name'];
|
||
//-----------------------------------------------------------------------------------------------------
|
||
//-----------------------------------------------------------------------------------------------------
|
||
|
||
$datb=substr($filename,-3);
|
||
|
||
|
||
|
||
if ($datb=='csv') { //-------------------------------------------- Excel csv file
|
||
//-----------------------------------------------------------------------------------------------------
|
||
$dat=substr($filename, 7,8); echo $dat;
|
||
if ($dat=='zb_Leip_') {
|
||
$tag =substr($filename, -6,2); //echo "Tag: ",$tag;
|
||
$monat=substr($filename, -8,2); //echo "Monat: ",$monat;
|
||
$jahr =substr($filename,-12,4); //echo "Jahr: ",$jahr;
|
||
$kw= date("W", mktime(0, 0, 0,$monat,$tag,$jahr)); // Ausgabe: KW
|
||
$kwjahr= date("o", mktime(0, 0, 0,$monat,$tag,$jahr)); // Ausgabe: jahr kw -- übergreifend wenn jaheswechsel
|
||
|
||
$handle = fopen ($filename,'r'); $key=0;
|
||
while (($fd = fgetcsv ($handle, 1000, ';')) !== FALSE ) {
|
||
|
||
$replace="_";
|
||
$search="'";
|
||
$fd4=str_replace($search, $replace,$fd[4]);
|
||
$fd5=str_replace($search, $replace,$fd[5]);
|
||
$fd8=str_replace($search, $replace,$fd[8]);
|
||
$fd9=str_replace($search, $replace,$fd[9]);
|
||
$search="/";
|
||
$fd4=str_replace($search, $replace,$fd[4]);
|
||
$fd5=str_replace($search, $replace,$fd[5]);
|
||
$fd8=str_replace($search, $replace,$fd[8]);
|
||
$fd9=str_replace($search, $replace,$fd[9]);
|
||
$search="´";
|
||
$fd4=str_replace($search, $replace,$fd[4]);
|
||
$fd5=str_replace($search, $replace,$fd[5]);
|
||
$fd8=str_replace($search, $replace,$fd[8]);
|
||
$fd9=str_replace($search, $replace,$fd[9]);
|
||
$search="'";
|
||
$fd4=str_replace($search, $replace,$fd[4]);
|
||
$fd5=str_replace($search, $replace,$fd[5]);
|
||
$fd8=str_replace($search, $replace,$fd[8]);
|
||
$fd9=str_replace($search, $replace,$fd[9]);
|
||
|
||
if ($fd[0]!="VBDAT2") {
|
||
$GLOBALS['mysql']->insert("INSERT INTO fazs (kw,kwjahr,tag,monat,jahr,zustelldate,anrede,titel,name,name2,name3,name4,strasse,bemerkung,hnr,hnrz,plz,ort,abo,pausevon,pausebis,stadtteil,kdnr,auftragnr)
|
||
VALUES(
|
||
'".$kw."',
|
||
'".$kwjahr."',
|
||
'".$tag."',
|
||
'".$monat."',
|
||
'".$jahr."',
|
||
'".$fd[0]."',
|
||
'".$fd[2]."',
|
||
'".$fd[3]."',
|
||
'".$fd4."',
|
||
'".$fd5."',
|
||
'".$fd[6]."',
|
||
'".$fd[7]."',
|
||
'".$fd8."',
|
||
'".$fd9."',
|
||
'".$fd[10]."',
|
||
'".$fd[11]."',
|
||
'".$fd[12]."',
|
||
'".$fd[13]."',
|
||
'".$fd[15]."',
|
||
'".$fd[23]."',
|
||
'".$fd[24]."',
|
||
'".$fd[27]."',
|
||
'".$fd[19]."',
|
||
'".$fd[20]."'
|
||
)
|
||
");
|
||
}
|
||
}
|
||
fclose($handle);
|
||
}
|
||
//------------------------------------------------------------
|
||
//------------------------------------ Veränderungen
|
||
//------------------------------------------------------------
|
||
if ($dat=='zva_Leip') {
|
||
$tag =substr($filename, -6,2); //echo "Tag: ",$tag;
|
||
$monat=substr($filename, -8,2); //echo "Monat: ",$monat;
|
||
$jahr =substr($filename,-12,4); //echo "Jahr: ",$jahr;
|
||
$kw= date("W", mktime(0, 0, 0,$monat,$tag,$jahr)); // Ausgabe: KW
|
||
$kwjahr= date("o", mktime(0, 0, 0,$monat,$tag,$jahr)); // Ausgabe: jahr kw -- übergreifend wenn jaheswechsel
|
||
$datum=$tag.".".$monat.".".$jahr;
|
||
$handle = fopen ($filename,'r'); $key=0;
|
||
while (($fd = fgetcsv ($handle, 1000, ';')) !== FALSE ) {
|
||
|
||
$replace="_";
|
||
$search="'";
|
||
$fd12=str_replace($search, $replace,$fd[12]);
|
||
$fd13=str_replace($search, $replace,$fd[13]);
|
||
$fd16=str_replace($search, $replace,$fd[16]);
|
||
$fd17=str_replace($search, $replace,$fd[17]);
|
||
$search="/";
|
||
$fd12=str_replace($search, $replace,$fd[12]);
|
||
$fd13=str_replace($search, $replace,$fd[13]);
|
||
$fd16=str_replace($search, $replace,$fd[16]);
|
||
$fd17=str_replace($search, $replace,$fd[17]);
|
||
$search="´";
|
||
$fd12=str_replace($search, $replace,$fd[12]);
|
||
$fd13=str_replace($search, $replace,$fd[13]);
|
||
$fd16=str_replace($search, $replace,$fd[16]);
|
||
$fd17=str_replace($search, $replace,$fd[17]);
|
||
$search="'";
|
||
$fd12=str_replace($search, $replace,$fd[12]);
|
||
$fd13=str_replace($search, $replace,$fd[13]);
|
||
$fd16=str_replace($search, $replace,$fd[16]);
|
||
$fd17=str_replace($search, $replace,$fd[17]);
|
||
|
||
if ($fd[0]!="FCODE") {
|
||
$GLOBALS['mysql']->insert("INSERT INTO fazv (kw,kwjahr,tag,monat,jahr,zustelldate,status,auftragnr,pausevon,pausebis,abo,anrede,titel,name,name2,name3,name4,strasse,hnr,hnrz,plz,ort,bemerkung)
|
||
VALUES(
|
||
'".$kw."',
|
||
'".$kwjahr."',
|
||
'".$tag."',
|
||
'".$monat."',
|
||
'".$jahr."',
|
||
'".$datum."',
|
||
'".$fd[0]."',
|
||
'".substr($fd[1],0,10)."',
|
||
'".$fd[4]."',
|
||
'".$fd[5]."',
|
||
'".$fd[7]."',
|
||
'".$fd[8]."',
|
||
'".$fd[9]."',
|
||
'".$fd12."',
|
||
'".$fd13."',
|
||
'".$fd[14]."',
|
||
'".$fd[15]."',
|
||
'".$fd16."',
|
||
'".$fd17."',
|
||
'".$fd[18]."',
|
||
'".$fd[19]."',
|
||
'".$fd[20]."',
|
||
'".$fd[22]."'
|
||
)
|
||
");
|
||
|
||
}
|
||
}
|
||
fclose($handle);
|
||
}
|
||
|
||
//------------------------------------------------------------
|
||
// echo "<pre>"; print_r($test); echo "</pre>";
|
||
//------------------------------------------------------------
|
||
}
|
||
}
|
||
unlink($filename);
|
||
//-----------------------------------------------------------------------------------------------------
|
||
//-----------------------------------------------------------------------------------------------------
|
||
//-----------------------------------------------------------------------------------------------------
|
||
//-----------------------------------------------------------------------------------------------------
|
||
|
||
|
||
|
||
|
||
|
||
//---------------------------------------------------------------------------------
|
||
|
||
if($count++ >= $max_emails) break;
|
||
}
|
||
|
||
}
|
||
|
||
/* close the connection */
|
||
imap_close($inbox);
|
||
|
||
echo "Done";
|
||
|
||
?>
|