Commit 02359b64571a6e0c215548498a773ea9c47bae83
1 parent
d6f2b06d
add mail parser and change parser form
Showing
3 changed files
with
121 additions
and
7 deletions
Show diff stats
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: Cibermag | ||
| 5 | + * Date: 01.09.2015 | ||
| 6 | + * Time: 10:53 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace backend\components; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +class MailParser { | ||
| 13 | + public static function requestAction(){ | ||
| 14 | + $hostname = '{imap.gmail.com:993/imap/ssl/novalidate-cert/norsh}Inbox'; | ||
| 15 | + $username = 'сюда твой логин'; | ||
| 16 | + $password = 'сюда твой пароль'; | ||
| 17 | + | ||
| 18 | + /* try to connect */ | ||
| 19 | + $inbox = imap_open($hostname,$username,$password) or die('Cannot connect to Gmail: ' . imap_last_error()); | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + $emails = imap_search($inbox,'FROM forallthings'); | ||
| 23 | + | ||
| 24 | + /* if emails are returned, cycle through each... */ | ||
| 25 | + /* if emails are returned, cycle through each... */ | ||
| 26 | + if($emails) { | ||
| 27 | + | ||
| 28 | + /* begin output var */ | ||
| 29 | + $output = ''; | ||
| 30 | + | ||
| 31 | + /* put the newest emails on top */ | ||
| 32 | + rsort($emails); | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + foreach ($emails as $email_number) { | ||
| 36 | + | ||
| 37 | + /* get information specific to this email */ | ||
| 38 | + $overview = imap_fetch_overview($inbox, $email_number, 0); | ||
| 39 | + $message = imap_fetchbody($inbox, $email_number, 2); | ||
| 40 | + $structure = imap_fetchstructure($inbox, $email_number); | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + $attachments = array(); | ||
| 46 | + if (isset($structure->parts) && count($structure->parts)) { | ||
| 47 | + for ($i = 0; $i < count($structure->parts); $i++) { | ||
| 48 | + $attachments[$i] = array( | ||
| 49 | + 'is_attachment' => false, | ||
| 50 | + 'filename' => '', | ||
| 51 | + 'name' => '', | ||
| 52 | + 'attachment' => ''); | ||
| 53 | + | ||
| 54 | + if ($structure->parts[$i]->ifdparameters) { | ||
| 55 | + foreach ($structure->parts[$i]->dparameters as $object) { | ||
| 56 | + if (strtolower($object->attribute) == 'filename') { | ||
| 57 | + $attachments[$i]['is_attachment'] = true; | ||
| 58 | + $attachments[$i]['filename'] = $object->value; | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + if ($structure->parts[$i]->ifparameters) { | ||
| 64 | + foreach ($structure->parts[$i]->parameters as $object) { | ||
| 65 | + if (strtolower($object->attribute) == 'name') { | ||
| 66 | + $attachments[$i]['is_attachment'] = true; | ||
| 67 | + $attachments[$i]['name'] = $object->value; | ||
| 68 | + } | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + if ($attachments[$i]['is_attachment']) { | ||
| 73 | + $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i + 1); | ||
| 74 | + if ($structure->parts[$i]->encoding == 3) { // 3 = BASE64 | ||
| 75 | + $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); | ||
| 76 | + } elseif ($structure->parts[$i]->encoding == 4) { // 4 = QUOTED-PRINTABLE | ||
| 77 | + $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + } // for($i = 0; $i < count($structure->parts); $i++) | ||
| 81 | + } // if(isset($structure->parts) && count($structure->parts)) | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + if (count($attachments) != 0) { | ||
| 85 | + | ||
| 86 | + | ||
| 87 | + foreach ($attachments as $at) { | ||
| 88 | + | ||
| 89 | + if ($at['is_attachment'] == 1) { | ||
| 90 | + //die(__DIR__); | ||
| 91 | + file_put_contents('test.csv', $at['attachment']); | ||
| 92 | + | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + // echo $output; | ||
| 101 | + } | ||
| 102 | + /* close the connection */ | ||
| 103 | + imap_close($inbox); | ||
| 104 | + } | ||
| 105 | +} | ||
| 0 | \ No newline at end of file | 106 | \ No newline at end of file |
backend/models/UploadFileParsingForm.php
| @@ -15,8 +15,11 @@ class UploadFileParsingForm extends Model | @@ -15,8 +15,11 @@ class UploadFileParsingForm extends Model | ||
| 15 | * @var UploadedFile file attribute | 15 | * @var UploadedFile file attribute |
| 16 | */ | 16 | */ |
| 17 | public $file; | 17 | public $file; |
| 18 | - public $first_line; | ||
| 19 | - public $first_column; | 18 | + public $importer; |
| 19 | + public $action; | ||
| 20 | +// public $delimiter; | ||
| 21 | +// public $delimiter_flag; | ||
| 22 | +// public $delete_prefix; | ||
| 20 | 23 | ||
| 21 | /** | 24 | /** |
| 22 | * @return array the validation rules. | 25 | * @return array the validation rules. |
| @@ -25,8 +28,11 @@ class UploadFileParsingForm extends Model | @@ -25,8 +28,11 @@ class UploadFileParsingForm extends Model | ||
| 25 | { | 28 | { |
| 26 | return [ | 29 | return [ |
| 27 | [['file'], 'file', 'extensions' => ['csv', 'xml'] ], | 30 | [['file'], 'file', 'extensions' => ['csv', 'xml'] ], |
| 28 | - ['first_line', 'integer'], | ||
| 29 | - ['first_column', 'integer'] | 31 | + ['importer', 'safe'], |
| 32 | + ['action', 'boolean'] | ||
| 33 | +// ['delimiter', 'string', 'max' => 1], | ||
| 34 | +// ['delimiter', 'default', 'value' => ';'], | ||
| 35 | + | ||
| 30 | ]; | 36 | ]; |
| 31 | } | 37 | } |
| 32 | 38 |
backend/views/parser/index.php
| @@ -8,10 +8,12 @@ use yii\helpers\Html; | @@ -8,10 +8,12 @@ use yii\helpers\Html; | ||
| 8 | <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data',],'action'=>['parser/results']]) ?> | 8 | <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data',],'action'=>['parser/results']]) ?> |
| 9 | <h3>Загрузка прайсов поставщиков</h3> | 9 | <h3>Загрузка прайсов поставщиков</h3> |
| 10 | 10 | ||
| 11 | - <?= $form->field($model, 'first_line') ?> | ||
| 12 | - <?= $form->field($model, 'first_column') ?> | ||
| 13 | 11 | ||
| 12 | + <?= $form->field($model, 'importer')->dropDownList(['1'=>'test1', '2'=>'test2', '3'=>'test3' ]); ?> | ||
| 14 | <?= $form->field($model, 'file')->fileInput() ?> | 13 | <?= $form->field($model, 'file')->fileInput() ?> |
| 14 | + <?= Html::radio('action', true, ['label' => 'Radio1']); ?> | ||
| 15 | + <?= Html::radio('action', true, ['label' => 'Radio2']); ?> | ||
| 16 | + <?= Html::checkbox('agree', true, ['label' => 'Checkbox']); ?> | ||
| 15 | 17 | ||
| 16 | <div class="form-group"> | 18 | <div class="form-group"> |
| 17 | <?= Html::submitButton(Yii::t('app', 'Прочитать'), ['class' => 'btn btn-primary']) ?> | 19 | <?= Html::submitButton(Yii::t('app', 'Прочитать'), ['class' => 'btn btn-primary']) ?> |
| @@ -19,4 +21,5 @@ use yii\helpers\Html; | @@ -19,4 +21,5 @@ use yii\helpers\Html; | ||
| 19 | 21 | ||
| 20 | <?php ActiveForm::end() ?> | 22 | <?php ActiveForm::end() ?> |
| 21 | </div> | 23 | </div> |
| 22 | -</div> | ||
| 23 | \ No newline at end of file | 24 | \ No newline at end of file |
| 25 | +</div> | ||
| 26 | + |