Example #1
0
 /**
  * Remove a temp file and the temp folder that is in.
  */
 public function actionRemoveTmpUpload()
 {
     if (isset($_POST['id'])) {
         $id = $_POST['id'];
         if (is_numeric($id)) {
             $tempFile = TempFile::model()->findByPk($id);
             if ($tempFile) {
                 $folder = $tempFile->folder;
                 $name = $tempFile->name;
                 if (file_exists('uploads/media/temp/' . $folder . '/' . $name)) {
                     unlink('uploads/media/temp/' . $folder . '/' . $name);
                 }
                 // delete file
                 if (file_exists('uploads/media/temp/' . $folder)) {
                     rmdir('uploads/media/temp/' . $folder);
                 }
                 // delete folder
                 $tempFile->delete();
                 // delete database entry tracking temp file
             }
         }
     }
 }
Example #2
0
 public function run()
 {
     if (Yii::app()->user->isGuest) {
         Yii::app()->controller->redirect(Yii::app()->controller->createUrl('/site/login'));
     }
     $this->attachBehaviors($this->behaviors);
     // Safety net of handlers - they ensure that errors can be caught and seen easily:
     $scenario = 'custom';
     if (empty($this->model)) {
         $model = new InlineEmail();
     } else {
         $model = $this->model;
     }
     if (isset($_POST['contactFlag'])) {
         $model->contactFlag = $_POST['contactFlag'];
     }
     $makeEvent = isset($_GET['skipEvent']) ? !(bool) $_GET['skipEvent'] : 1;
     // Check to see if the user is requesting a new template
     if (isset($_GET['template'])) {
         $scenario = 'template';
     }
     $model->setScenario($scenario);
     $attachments = array();
     if (isset($_POST['InlineEmail'])) {
         // This could indicate either a template change or a form submission.
         $model->attributes = $_POST['InlineEmail'];
         // Prepare attachments that may have been uploaded on-the-fly (?)
         $mediaLibraryUsed = false;
         // is there an attachment from the media library?
         if (isset($_POST['AttachmentFiles'], $_POST['AttachmentFiles']['id'], $_POST['AttachmentFiles']['types'])) {
             $ids = $_POST['AttachmentFiles']['id'];
             $types = $_POST['AttachmentFiles']['types'];
             $attachments = array();
             for ($i = 0; $i < count($ids); $i++) {
                 $type = $types[$i];
                 switch ($type) {
                     case 'temp':
                         // attachment is a temp file
                         $tempFile = TempFile::model()->findByPk($ids[$i]);
                         $attachments[] = array('filename' => $tempFile->name, 'folder' => $tempFile->folder, 'type' => $type, 'id' => $tempFile->id);
                         break;
                     case 'media':
                         // attachment is from media library
                         $mediaLibraryUsed = true;
                         $media = Media::model()->findByPk($ids[$i]);
                         $attachments[] = array('filename' => $media->fileName, 'folder' => $media->uploadedBy, 'type' => $type, 'id' => $media->id);
                         break;
                     default:
                         throw new CException('Invalid attachment type: ' . $type);
                 }
             }
         }
         $model->attachments = $attachments;
         // Validate/prepare the body, and send if no problems occur:
         $sendStatus = array_fill_keys(array('code', 'message'), '');
         $failed = false;
         $message = '';
         $postReplace = isset($_GET['postReplace']) ? $_GET['postReplace'] : 0;
         if (isset($_GET['loadTemplate'])) {
             // A special override for when it's not possible to include the template in $_POST
             $model->template = $_GET['loadTemplate'];
         }
         if ($model->prepareBody($postReplace)) {
             if ($scenario != 'template') {
                 // Sending the email, not merely requesting a template change
                 //
                 // First check that the user has permission to use the
                 // specified credentials:
                 if ($model->credId != Credentials::LEGACY_ID) {
                     if (!Yii::app()->user->checkAccess('CredentialsSelect', array('model' => $model->credentials))) {
                         $this->respond(Yii::t('app', 'Did not send email because you do not have ' . 'permission to use the specified credentials.'), 1);
                     }
                 }
                 $sendStatus = $model->send($makeEvent);
                 // $sendStatus = array('code'=>'200','message'=>'sent (testing)');
                 $failed = $sendStatus['code'] != '200';
                 $message = $sendStatus['message'];
             } else {
                 if ($model->modelName == 'Quote' && empty($model->template)) {
                     // Fill in the gap with the default / "semi-legacy" quotes view
                     $model->message = $this->controller->renderPartial('application.modules.quotes.views.quotes.print', array('model' => $model->targetModel, 'email' => true), true);
                     // Add a linebreak at the beginning for user-entered notes in the email:
                     $model->insertInBody('<br />', 1);
                 }
             }
         }
         // Populate response data:
         $modelHasErrors = $model->hasErrors();
         $failed = $failed || $modelHasErrors;
         $response = array('scenario' => $scenario, 'sendStatus' => $sendStatus, 'attributes' => $model->attributes, 'modelErrors' => $model->errors, 'modelHasErrors' => $modelHasErrors, 'modelErrorHtml' => CHtml::errorSummary($model, Yii::t('app', "Please fix the following errors:"), null, array('style' => 'margin-bottom: 5px;')));
         if ($scenario == 'template') {
             // There's a chance the inline email form is switching gears into
             // quote mode, in which case we need to include templates and
             // insertable attributes for setting it all up properly:
             $response['insertableAttributes'] = $model->insertableAttributes;
             $templates = array(0 => Yii::t('docs', 'Custom Message')) + Docs::getEmailTemplates($model->modelName == 'Quote' ? 'quote' : 'email', $_POST['associationType']);
             $response['templateList'] = array();
             foreach ($templates as $id => $templateName) {
                 $response['templateList'][] = array('id' => $id, 'name' => $templateName);
             }
         }
         $this->mergeResponse($response);
         $this->respond($message, $failed);
     } else {
         $this->respond(Yii::t('app', 'Inline email model missing from the request to the server.'), true);
     }
 }
Example #3
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionUpload()
 {
     $model = new Media();
     if (isset($_POST['Media'])) {
         $temp = TempFile::model()->findByPk($_POST['TempFileId']);
         $userFolder = Yii::app()->user->name;
         // place uploaded files in a folder named with the username of the user that uploaded the file
         $userFolderPath = 'uploads/media/' . $userFolder;
         // if user folder doesn't exit, try to create it
         if (!(file_exists($userFolderPath) && is_dir($userFolderPath))) {
             if (!@mkdir('uploads/media/' . $userFolder, 0777, true)) {
                 // make dir with edit permission
                 // ERROR: Couldn't create user folder
                 var_dump($userFolder);
                 exit;
             }
         }
         rename($temp->fullpath(), $userFolderPath . '/' . $temp->name);
         // save media info
         $model->fileName = $temp->name;
         $model->createDate = time();
         $model->lastUpdated = time();
         $model->uploadedBy = Yii::app()->user->name;
         $model->associationType = $_POST['Media']['associationType'];
         $model->associationId = $_POST['Media']['associationId'];
         $model->private = $_POST['Media']['private'];
         $model->path;
         // File type setter is embedded in the magic getter for path
         $model->name = $_POST['Media']['name'];
         if (empty($model->name)) {
             $model->name = $model->fileName;
         }
         if ($_POST['Media']['description']) {
             $model->description = $_POST['Media']['description'];
         }
         /*     
                     uncomment when media module supports custom forms
                     if(isset($_POST['x2ajax'])){
                         $ajaxErrors = $this->quickCreate ($model);
                         if (!$ajaxErrors) {
                             $this->createAttachmentAction ($model);
                         }
                     }else{*/
         if ($model->save()) {
             $this->createAttachmentAction($model);
             $this->redirect(array('view', 'id' => $model->id));
         }
         //}
     }
     /*
             uncomment when media module supports custom forms
             if(isset($_POST['x2ajax'])){
                 $this->renderInlineCreateForm ($model, isset ($ajaxErrors) ? $ajaxErrors : false);
             } else {*/
     $this->render('upload', array('model' => $model));
     //}
 }
Example #4
0
 public static function createTempFile($name)
 {
     // delete old temp files if they exist
     $old = time() - 86400;
     // 1 day old
     $oldTempFiles = TempFile::model()->findAll("createDate < {$old}");
     foreach ($oldTempFiles as $oldTempFile) {
         $oldFolder = $oldTempFile->folder;
         $oldName = $oldTempFile->name;
         if (file_exists('uploads/protected/media/temp/' . $oldFolder . '/' . $oldName)) {
             unlink('uploads/protected/media/temp/' . $oldFolder . '/' . $oldName);
         }
         // delete file
         if (file_exists('uploads/protected/media/temp/' . $oldFolder)) {
             rmdir('uploads/protected/media/temp/' . $oldFolder);
         }
         // delete folder
         $oldTempFile->delete();
         // delete database entry tracking temp file
     }
     // generate temp folder name
     $folder = substr(md5(rand()), 0, 10);
     // try to create temp folder
     if (!@mkdir('uploads/protected/media/temp/' . $folder, 0777, true)) {
         return false;
     }
     // couldn't create temp folder
     $tempFile = new TempFile();
     // track temp file in database
     $tempFile->folder = $folder;
     $tempFile->name = $name;
     $tempFile->createDate = time();
     if ($tempFile->save()) {
         return $tempFile;
     } else {
         return false;
     }
 }
 public function clearTemporaryFiles(array $attachments = array())
 {
     foreach ($attachments as $attachment) {
         $type = $attachment['type'];
         if ($type === 'temp') {
             $file = 'uploads/protected/media/temp/' . $attachment['folder'] . '/' . $attachment['filename'];
             $folder = 'uploads/protected/media/temp/' . $attachment['folder'];
             if (file_exists($file)) {
                 unlink($file);
             }
             // delete temp file
             if (file_exists($folder)) {
                 rmdir($folder);
             }
             // delete temp folder
             TempFile::model()->deleteByPk($attachment['id']);
         }
     }
 }
Example #6
0
 /**
  * Perform the email delivery with PHPMailer.
  *
  * Any special authentication and security should take place in here.
  *
  * @param array $addresses This array must contain "to", "cc" and/or "bcc"
  *  keys, and values must be arrays of recipients. Each recipient is expressed
  *  as a 2-element array with the first element being the name, and the second
  *  the email address.
  * @throws Exception
  * @return array
  */
 public function deliverEmail($addresses, $subject, $message, $attachments = array())
 {
     if (YII_DEBUG && self::DEBUG_EMAIL) {
         // Fake a successful send
         /**/
         AuxLib::debugLog('Faking an email delivery to address(es): ' . var_export($addresses, 1));
         return $this->status = $this->getDebugStatus();
     }
     $phpMail = $this->mailer;
     try {
         $this->addEmailAddresses($phpMail, $addresses);
         $phpMail->Subject = $subject;
         // $phpMail->AltBody = $message;
         $phpMail->MsgHTML($message);
         // $phpMail->Body = $message;
         // add attachments, if any
         if ($attachments) {
             foreach ($attachments as $attachment) {
                 $type = $attachment['type'];
                 switch ($type) {
                     case 'temp':
                         // stored as a temp file?
                         $file = 'uploads/media/temp/' . $attachment['folder'] . '/' . $attachment['filename'];
                         if (file_exists($file)) {
                             // check file exists
                             if ($this->validateFileSize(filesize($file))) {
                                 $phpMail->AddAttachment($file);
                             }
                         }
                         break;
                     case 'media':
                         // stored in media library
                         $file = 'uploads/media/' . $attachment['folder'] . '/' . $attachment['filename'];
                         if (file_exists($file)) {
                             // check file exists
                             if ($this->validateFileSize(filesize($file))) {
                                 $phpMail->AddAttachment($file);
                             }
                         }
                         break;
                     default:
                         throw new CException('Invalid attachment type');
                 }
             }
         }
         $phpMail->Send();
         // delete temp attachment files, if they exist
         if ($attachments) {
             foreach ($attachments as $attachment) {
                 $type = $attachment['type'];
                 if ($type === 'temp') {
                     $file = 'uploads/media/temp/' . $attachment['folder'] . '/' . $attachment['filename'];
                     $folder = 'uploads/media/temp/' . $attachment['folder'];
                     if (file_exists($file)) {
                         unlink($file);
                     }
                     // delete temp file
                     if (file_exists($folder)) {
                         rmdir($folder);
                     }
                     // delete temp folder
                     TempFile::model()->deleteByPk($attachment['id']);
                 }
             }
         }
         $this->status['code'] = '200';
         $this->status['exception'] = null;
         $this->status['message'] = Yii::t('app', 'Email Sent!');
     } catch (phpmailerException $e) {
         // Catch PHPMailer specific exceptions for pretty error printing
         $this->status['code'] = '500';
         $this->status['exception'] = $e;
         $this->status['message'] = $phpMail->ErrorInfo . " " . $e->getFile() . " L" . $e->getLine();
     } catch (Exception $e) {
         $this->status['code'] = '500';
         $this->status['exception'] = $e;
         $this->status['message'] = $e->getMessage() . " " . $e->getFile() . " L" . $e->getLine();
     }
     return $this->status;
 }
Example #7
0
 /**
  * Perform the email delivery with PHPMailer.
  *
  * Any special authentication and security should take place in here.
  *
  * @param array $addresses This array must contain "to", "cc" and/or "bcc"
  *  keys, and values must be arrays of recipients. Each recipient is expressed
  *  as a 2-element array with the first element being the name, and the second
  *  the email address.
  * @throws Exception
  * @return array
  */
 public function deliverEmail($addresses, $subject, $message, $attachments = array())
 {
     if (YII_DEBUG && self::DEBUG_EMAIL) {
         // Fake a successful send
         /**/
         AuxLib::debugLog('Faking an email delivery to address(es): ' . var_export($addresses, 1));
         return $this->status = $this->getDebugStatus();
     }
     try {
         $phpMail = $this->mailer;
     } catch (phpmailerException $e) {
         // escalate error to force campaigns to halt
         $escalated = new phpmailerException($e->getMessage(), PHPMailer::STOP_CRITICAL);
         $this->status['code'] = '500';
         $this->status['exception'] = $escalated;
         $this->status['message'] = $e->getMessage();
         return $this->status;
     }
     // attempt smpt connect before attempting to send so that we can escalate exception
     // severity if connection fails. Ideally we would be able to detect exactly the type of
     // exception that PHPMailer throws but unfortunately the only way at the time of this
     // writing would be to use its translated exception messages (brittle).
     if ($this->credentials) {
         try {
             $phpMail->smtpConnect();
         } catch (phpmailerException $e) {
             $escalated = new phpmailerException($e->getMessage(), PHPMailer::STOP_CRITICAL);
             $this->status['code'] = '500';
             $this->status['exception'] = $escalated;
             $this->status['message'] = $phpMail->ErrorInfo . " " . $e->getFile() . " L" . $e->getLine();
             return $this->status;
         }
     }
     try {
         $this->addEmailAddresses($phpMail, $addresses);
         $phpMail->Subject = $subject;
         // $phpMail->AltBody = $message;
         $phpMail->MsgHTML($message);
         // $phpMail->Body = $message;
         // add attachments, if any
         if ($attachments) {
             foreach ($attachments as $attachment) {
                 $type = $attachment['type'];
                 switch ($type) {
                     case 'temp':
                         // stored as a temp file?
                         $file = 'uploads/media/temp/' . $attachment['folder'] . '/' . $attachment['filename'];
                         if (file_exists($file)) {
                             // check file exists
                             if ($this->validateFileSize(filesize($file))) {
                                 $phpMail->AddAttachment($file);
                             }
                         }
                         break;
                     case 'media':
                         // stored in media library
                         $file = 'uploads/media/' . $attachment['folder'] . '/' . $attachment['filename'];
                         if (file_exists($file)) {
                             // check file exists
                             if ($this->validateFileSize(filesize($file))) {
                                 $phpMail->AddAttachment($file);
                             }
                         }
                         break;
                     default:
                         throw new CException('Invalid attachment type');
                 }
             }
         }
         $phpMail->Send();
         // delete temp attachment files, if they exist
         if ($attachments) {
             foreach ($attachments as $attachment) {
                 $type = $attachment['type'];
                 if ($type === 'temp') {
                     $file = 'uploads/media/temp/' . $attachment['folder'] . '/' . $attachment['filename'];
                     $folder = 'uploads/media/temp/' . $attachment['folder'];
                     if (file_exists($file)) {
                         unlink($file);
                     }
                     // delete temp file
                     if (file_exists($folder)) {
                         rmdir($folder);
                     }
                     // delete temp folder
                     TempFile::model()->deleteByPk($attachment['id']);
                 }
             }
         }
         $this->status['code'] = '200';
         $this->status['exception'] = null;
         $this->status['message'] = Yii::t('app', 'Email Sent!');
     } catch (phpmailerException $e) {
         // Catch PHPMailer specific exceptions for pretty error printing
         $this->status['code'] = '500';
         $this->status['exception'] = $e;
         $this->status['message'] = $phpMail->ErrorInfo . " " . $e->getFile() . " L" . $e->getLine();
     } catch (Exception $e) {
         $this->status['code'] = '500';
         $this->status['exception'] = $e;
         $this->status['message'] = $e->getMessage() . " " . $e->getFile() . " L" . $e->getLine();
     }
     return $this->status;
 }