From 7f6a9301d6a5f24190ed0e0f1871415e27dd20d2 Mon Sep 17 00:00:00 2001 From: Mihail Date: Mon, 2 Nov 2015 18:17:28 +0200 Subject: [PATCH] add mails classes --- backend/controllers/RgGrupController.php | 4 ++-- backend/views/cart/index.php | 25 +++++++++++++++++-------- common/components/mail/ImapMailReader.php | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/components/mail/MailAttachmentsSaver.php | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/components/mail/MailReader.php | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ common/components/parsers/MailAttachmentsSaver.php | 110 -------------------------------------------------------------------------------------------------------------- common/models/CartBillsSearch.php | 24 +++++++++++++++++++----- console/controllers/ParserController.php | 24 ++++++++++++++++++++++++ 8 files changed, 275 insertions(+), 125 deletions(-) create mode 100644 common/components/mail/ImapMailReader.php create mode 100644 common/components/mail/MailAttachmentsSaver.php create mode 100644 common/components/mail/MailReader.php delete mode 100644 common/components/parsers/MailAttachmentsSaver.php diff --git a/backend/controllers/RgGrupController.php b/backend/controllers/RgGrupController.php index 04d43b0..3b15494 100644 --- a/backend/controllers/RgGrupController.php +++ b/backend/controllers/RgGrupController.php @@ -195,8 +195,8 @@ class RgGrupController extends BaseController public function actionMail() { - //$mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox', 'mt@soft-terra.com.ua', 'Wehrfyjd8'); - $mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'price@italauto.com.ua', '67853562'); + $mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'tsurkanovm@gmail.com', 'Wtvr@2000'); + //$mail_saver = new MailAttachmentsSaver('{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'price@italauto.com.ua', '67853562'); $mail_saver->saveAttachmentsTo(); } diff --git a/backend/views/cart/index.php b/backend/views/cart/index.php index a70caf8..9d2ef9a 100644 --- a/backend/views/cart/index.php +++ b/backend/views/cart/index.php @@ -10,6 +10,7 @@ use kartik\date\DatePicker; $this->title = Yii::t('app', 'Заказы'); $this->params['breadcrumbs'][] = $this->title; + ?>
@@ -25,6 +26,13 @@ $this->params['breadcrumbs'][] = $this->title; 'id', 'account_id', + ['label' =>'Информация', + 'value' =>function ($data) { + $info = $data->scode . ' /n'; + $info .= $data->name; + return $info; + }, + ], 'sum', [ 'label' =>'Статус', @@ -34,17 +42,18 @@ $this->params['breadcrumbs'][] = $this->title; 'manager_name', [ 'label' =>'Дата', + 'value' =>function ($data) { + return date('Y-m-d', $data->dt); + }, 'attribute' => 'dt', 'filter' => DatePicker::widget([ - 'name' => 'data1', - // 'value' => '01-Feb-2015', + 'model' =>$searchModel, + 'language' =>'ru', + 'size' =>'xs', + 'separator' =>'по', + 'attribute' => 'dt', 'type' => DatePicker::TYPE_RANGE, - 'name2' => 'data2', - // 'value2' => '27-Feb-2015', - 'pluginOptions' => [ - 'autoclose'=>true, - 'format' => 'dd-M-yyyy' - ] + 'attribute2' => 'date_to', ]), ], diff --git a/common/components/mail/ImapMailReader.php b/common/components/mail/ImapMailReader.php new file mode 100644 index 0000000..5f46a4f --- /dev/null +++ b/common/components/mail/ImapMailReader.php @@ -0,0 +1,54 @@ +connection = imap_open($hostname, $username, $password); + + if ($this->connection === false) + throw new \Exception('Cannot connect to Gmail: ' . imap_last_error()); + + } + + public function getEmails( $flag ) + { + return imap_search( $this->connection, $flag ); + } + + /** + * @return mixed + */ + public function getListMailboxes() + { + return imap_list($this->connection, $this->hostname, "*"); + } + + public function getCurrentEmailStructure( $email_number ) + { + return imap_fetchstructure($this->connection, $email_number); + } + + public function reOpen( $hostname ) + { + imap_reopen( $this->connection, $hostname ); + } + + function __destruct() + { + /* close the connection */ + imap_close( $this->connection ); + } + + +} \ No newline at end of file diff --git a/common/components/mail/MailAttachmentsSaver.php b/common/components/mail/MailAttachmentsSaver.php new file mode 100644 index 0000000..616f909 --- /dev/null +++ b/common/components/mail/MailAttachmentsSaver.php @@ -0,0 +1,109 @@ +mail_reader = $mail_reader; + $this->massage_type = $massage_type; + + } + + + public function saveAttachmentsTo( $destination ) + { + + $emails = $this->mail_reader->getEmails( $this->massage_type ); + + /* if emails are returned, cycle through each... */ + + if ($emails) { + + /* begin output var */ + $output = ''; + + /* put the newest emails on top */ + rsort($emails); + // CustomVarDamp::dump($emails); + foreach ($emails as $email_number) { + + $structure = $this->mail_reader->getCurrentEmailStructure($email_number); + $attachments = array(); + 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; + } + } + } + + 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($this->mail_reader->connection, $email_number, $i + 1); + if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 + $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); + } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE + $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); + } + } + } + + if (count($attachments) != 0) { + + foreach($attachments as $key => &$val){ + if ($val['is_attachment'] == 1) { + + if( isset($this->file_name_prefix) ){ + $name = $destination . '/' . $this->file_name_prefix . mb_decode_mimeheader($val['name']); + }else{ + $name = $destination . '/' . mb_decode_mimeheader($val['name']); + } + mb_internal_encoding("UTF-8"); + + //file_put_contents(\Yii::getAlias('@temp_upload') . '/' . $name, $val['attachment']); + file_put_contents( $name, $val['attachment'] ); + } + + } + } + + } + + } + + } + + } +} \ No newline at end of file diff --git a/common/components/mail/MailReader.php b/common/components/mail/MailReader.php new file mode 100644 index 0000000..eb778cc --- /dev/null +++ b/common/components/mail/MailReader.php @@ -0,0 +1,50 @@ +hostname = $hostname; + $this->username = $username; + $this->password = $password; + } + + /** + * @return mixed + */ + public function getHostname() + { + return $this->hostname; + } + + + + public abstract function getListMailboxes(); + + public abstract function getEmails( $flag ); + + public abstract function getCurrentEmailStructure( $email ); + + public abstract function reOpen( $hostname ); + + + +} \ No newline at end of file diff --git a/common/components/parsers/MailAttachmentsSaver.php b/common/components/parsers/MailAttachmentsSaver.php deleted file mode 100644 index 1a93b4b..0000000 --- a/common/components/parsers/MailAttachmentsSaver.php +++ /dev/null @@ -1,110 +0,0 @@ -connection = imap_open($hostname, $username, $password); - CustomVarDamp::dumpAndDie( $this->connection); - if ($this->connection === false) - throw new \Exception('Cannot connect to Gmail: ' . imap_last_error()); - } - - - public function saveAttachmentsTo() - { - - $emails = imap_search($this->connection, 'FROM forallthings'); - - /* if emails are returned, cycle through each... */ - if ($emails) { - - /* begin output var */ - $output = ''; - - /* put the newest emails on top */ - rsort($emails); - - foreach ($emails as $email_number) { - - /* get information specific to this email */ -// $overview = imap_fetch_overview($inbox, $email_number, 0); -// $message = imap_fetchbody($inbox, $email_number, 2); - $structure = imap_fetchstructure($this->connection, $email_number); - - $attachments = array(); - 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; - } - } - } - - 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($this->connection, $email_number, $i + 1); - if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 - $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); - } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE - $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); - } - } - } - - CustomVarDamp::dumpAndDie($attachments); - - if (count($attachments) != 0) { - - - foreach ($attachments as $at) { - - if ($at['is_attachment'] == 1) { - //die(__DIR__); - file_put_contents('test.csv', $at['attachment']); - - } - } - - } - - } - - // echo $output; - } - /* close the connection */ - imap_close($this->connection); - } - } -} \ No newline at end of file diff --git a/common/models/CartBillsSearch.php b/common/models/CartBillsSearch.php index 94bf8c1..2fbca4d 100644 --- a/common/models/CartBillsSearch.php +++ b/common/models/CartBillsSearch.php @@ -13,7 +13,7 @@ use common\models\CartBillsView; */ class CartBillsSearch extends CartBillsView { - + public $date_to; /** * @inheritdoc */ @@ -21,7 +21,7 @@ class CartBillsSearch extends CartBillsView { return [ [['id', 'account_id', 'status'], 'integer'], - [['data1,data2'], 'safe'], + [['dt', 'date_to'], 'string', 'max' => 10], ]; } @@ -50,8 +50,7 @@ class CartBillsSearch extends CartBillsView ]); $this->load($params); - //$timestamp= mktime($hours,$minutes,$seconds,$month,$day,$year); - CustomVarDamp::dumpAndDie(date_timestamp_get($params['data1'])); + if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); @@ -61,10 +60,20 @@ class CartBillsSearch extends CartBillsView $query->andFilterWhere([ 'id' => $this->id, 'account_id' => $this->account_id, - 'timestamp' => $this->dt, 'status_id' => $this->status, ]); + if($this->dt !== null || $this->date_to !== null){ + $date_from = mktime(0,0,0,(int)substr($this->dt,4,2),(int)substr($this->dt,1,2),(int)substr($this->dt,7,4)); + $date_to = mktime(23,59,59,(int)substr($this->date_to,4,2),(int)substr($this->date_to,1,2),(int)substr($this->date_to,7,4)); + + $query->andFilterWhere([ + 'between', 'dt', $date_from, $date_to + ]); + } + + + // $query->andFilterWhere(['like', 'f1', $this->f1]) // ->andFilterWhere(['like', 'f2', $this->f2]) // ->andFilterWhere(['like', 'f3', $this->f3]) @@ -73,4 +82,9 @@ class CartBillsSearch extends CartBillsView return $dataProvider; } + + public static function findById($id){ + + return CartBillsView::find()->where(['id' => $id])->one(); + } } diff --git a/console/controllers/ParserController.php b/console/controllers/ParserController.php index 4ec57f5..c43ba4e 100644 --- a/console/controllers/ParserController.php +++ b/console/controllers/ParserController.php @@ -2,6 +2,8 @@ namespace console\controllers; use common\components\CustomVarDamp; +use common\components\mail\ImapMailReader; +use common\components\mail\MailAttachmentsSaver; use yii\console\Controller; use yii\helpers\Console; use common\components\PriceWriter; @@ -118,4 +120,26 @@ class ParserController extends Controller \Yii::info('2', 'parser'); } + + public function actionSaveMailAttachments() + { + \Yii::info('Начало сохранения файлов почты', 'mail'); + + $mail_reader = new ImapMailReader( '{imap.gmail.com:993/imap/ssl/novalidate-cert}', 'tsurkanovm@gmail.com', 'Wtvr@2000' ); + $mailboxes = $mail_reader->getListMailboxes(); + foreach ($mailboxes as $custom_label) { + $words = explode(" ",str_replace(array($mail_reader->getHostname(),"!"),"",imap_utf7_decode($custom_label))); + $importer_id = (int)preg_replace("/[^A-Z0-9]+/","",strtoupper($words[0])); + + $mail_reader->reOpen($custom_label); + $saver = new MailAttachmentsSaver( $mail_reader, 'UNSEEN' ); + $saver->file_name_parefix = $importer_id . '~!~'; + $saver->saveAttachmentsTo(\Yii::getAlias('@temp_upload')); + +// $files_model = new ImportersFiles(); +// $files_model->importer_id = $importer_id; + } + + + } } \ No newline at end of file -- libgit2 0.21.4