/**
  * main action
  */
 public function mainAction()
 {
     /**
      * initialize
      */
     require_once 'models/common/common_file.php';
     $File = new common_file();
     /**
      * Setting input variables
      * 
      */
     if ($this->GET['directory']) {
         $base_folder = $this->GET['directory'];
     } else {
         $base_folder = 'var/files/';
     }
     if ($this->GET['open']) {
         $open_folder = $this->GET['open'];
     } else {
         if ($_POST['open']) {
             $open_folder = $_POST['open'];
         } else {
             if ($_SESSION['server_browser_last_open_folder'] != '') {
                 $open_folder = urlencode($_SESSION['server_browser_last_open_folder']);
             } else {
                 $open_folder = "";
             }
         }
     }
     $multiupload = $this->GET['multiupload'] == 'true';
     /**
      * Store opened folder to session
      */
     $_SESSION['server_browser_last_open_folder'] = urldecode($open_folder);
     /**
      * Setting base paths
      * 
      */
     $base_folder_full = ONXSHOP_PROJECT_DIR . $base_folder;
     $fullpath = $base_folder_full . urldecode($open_folder);
     $pathinfo = pathinfo($fullpath);
     $actual_folder = $pathinfo['dirname'] . '/';
     $actual_file = $pathinfo['basename'];
     if (is_dir($actual_folder . $actual_file)) {
         $actual_folder = $actual_folder . $actual_file . '/';
         $actual_file = '';
     }
     $relative_folder_path = str_replace($base_folder_full, '', $actual_folder);
     /**
      * Cleaning
      *
      */
     //stolen from common_uri_mapping
     if (function_exists("recode_string")) {
         $new_folder = recode_string("utf-8..flat", trim($_POST['new_folder']));
     } else {
         $new_folder = iconv("UTF-8", "ASCII//IGNORE", trim($_POST['new_folder']));
     }
     //$new_folder = strtolower($new_folder);
     $new_folder = preg_replace("/\\s/", "-", $new_folder);
     $new_folder = preg_replace("/&[^([a-zA-Z;)]/", 'and-', $new_folder);
     $new_folder = preg_replace("/[^\\w-\\/]/", '', $new_folder);
     $new_folder = preg_replace("/\\-{2,}/", '-', $new_folder);
     /**
      * Create a new folder
      */
     if ($new_folder != '' && $_POST['create']) {
         $new_folder_full = $actual_folder . $new_folder;
         if (!mkdir($new_folder_full)) {
             msg("Cannot create folder {$new_folder} in {$actual_folder}", 'error');
         }
     }
     /**
      * Delete file
      */
     if ($this->GET['delete_file']) {
         $File->deleteFile($this->GET['delete_file']);
         return true;
     }
     /**
      * Confirm overwrite
      */
     if ($_POST['overwrite'] == 'overwrite') {
         if ($File->overwriteFile($_POST['filename'], $_POST['save_dir'], $_POST['temp_file'])) {
             if ($multiupload) {
                 $this->jsonResponse("success");
             }
             msg('File has been overwritten');
         } else {
             msg("Can't overwrite file", 'error');
         }
     }
     //input
     $normal_formated_files = $_FILES;
     foreach ($normal_formated_files as $file_item) {
         if (is_uploaded_file($file_item['tmp_name'])) {
             $save_dir = $base_folder . $relative_folder_path;
             $upload = $File->getSingleUpload($file_item, $save_dir);
             /**
              * when array is returned by getSingleUpload, it's an existing file is in place
              */
             if (is_array($upload)) {
                 $this->tpl->assign("OVERWRITE_FILE", $upload);
                 if ($multiupload) {
                     $this->jsonResponse("file_exists", $upload);
                 }
                 $this->tpl->parse("content.confirm_overwrite");
                 $overwrite_show = 1;
             } else {
                 if ($upload) {
                     if ($multiupload) {
                         $this->jsonResponse("success");
                     }
                     msg("Uploaded {$file_item['name']}");
                 }
             }
         }
     }
     /**
      * prepare folder head string
      */
     $folder_head = str_replace('/', ' / ', $relative_folder_path);
     $folder_head = '/ ' . $folder_head;
     /**
      * Assign template variables
      * 
      */
     $this->tpl->assign('BASE', $base_folder);
     $this->tpl->assign('FOLDER_HEAD', $folder_head);
     $this->tpl->assign('FOLDER', $relative_folder_path);
     $this->tpl->assign('MAX_FILE_SIZE', round($File->convertBytes(ini_get('upload_max_filesize')) / 1048576));
     $this->tpl->assign('MAX_FILES', ini_get('max_file_uploads'));
     /**
      * allow to upload only in non-root folder
      */
     if ($relative_folder_path || ONXSHOP_MEDIA_LIBRARY_ROOT_UPLOAD) {
         $this->tpl->parse('content.add_new.upload_file');
     } else {
         $this->tpl->parse('content.add_new.upload_instruction');
     }
     //hide upload when overwrite?
     if ($overwrite_show == 0) {
         $this->tpl->parse("content.add_new");
     }
     /**
      * Get File List
      * 
      */
     //list content od folder
     $list = $File->getFlatArrayFromFs($actual_folder, '-type f -maxdepth 1');
     //FIND2GLOB PATCH: $list = $File->getFlatArrayFromFs($actual_folder, 'f', false);
     if (is_array($list) && count($list) > 0) {
         foreach ($list as $l) {
             $l['file_path'] = $actual_folder . $l['name'];
             $l['file_path_encoded'] = $File->encode_file_path($actual_folder . $l['name']);
             $l['file_path_encoded_relative'] = $File->encode_file_path($base_folder . $relative_folder_path . $l['name']);
             $this->tpl->assign("ITEM", $l);
             $relations_list = $File->getRelations($base_folder . $relative_folder_path . $l['name']);
             if ($relations_list['count'] == 0) {
                 $this->tpl->parse('content.list.item.delete');
             } else {
                 $this->tpl->assign("FILE_USAGE", $relations_list['count']);
                 $this->tpl->parse('content.list.item.usage');
                 //$_Onxshop_Request = new Onxshop_Request("bo/component/file_usage~file_path_encoded_relative={$l['file_path_encoded_relative']}~");
                 //$this->tpl->assign("FILE_USAGE", $_Onxshop_Request->getContent());
             }
             //if (preg_match('/^image\/.*/', $l['mime-type']))
             $this->tpl->parse('content.list.item.thumbnail');
             switch ($this->GET['type']) {
                 case 'add_to_node':
                 case 'RTE':
                 case 'add_to_product':
                 case 'add_to_product_variety':
                 case 'add_to_recipe':
                 case 'add_to_store':
                 case 'add_to_survey':
                 case 'add_to_taxonomy':
                 case 'CSS':
                 case 'file':
                     $this->tpl->parse('content.list.item.add_to_node');
                     break;
                 case 'database_import':
                     $this->tpl->parse('content.list.item.database_import');
                     break;
             }
             $this->tpl->parse('content.list.item');
         }
         $this->tpl->parse('content.list');
     } else {
         // contains no files, check if there aren't some folders, otherwise allow delete
         $list = $File->getFlatArrayFromFs($actual_folder, '-type d -maxdepth 1');
         //FIND2GLOB PATCH: $list = $File->getFlatArrayFromFs($actual_folder, 'd', false);
         if (count($list) == 0) {
             $this->tpl->parse('content.empty');
         }
     }
     return true;
 }
Example #2
0
 /**
  * send
  * internal function
  * 
  * @return boolean
  * send status
  */
 function send()
 {
     $email_data = $this->_format($this->template);
     $this->set('subject', $email_data['title']);
     require_once 'Zend/Mail.php';
     $mail = new Zend_Mail('utf-8');
     $mail->addHeader('X-MailGenerator', ONXSHOP_VERSION);
     $mail->setFrom($this->get('email_from'), $this->get('name_from'));
     $mail->addTo($this->get('email_recipient'), $this->get('name_recipient'));
     $mail->setSubject($this->get('subject'));
     //send BCC of all emails to specified address
     if ($this->conf['mail_bcc_address']) {
         $mail->addBcc($this->conf['mail_bcc_address'], $this->conf['mail_bcc_name']);
     }
     if ($this->conf['smtp_server_address']) {
         msg("use SMTP " . $this->conf['smtp_server_address'], 'ok', 2);
         require_once 'Zend/Mail/Transport/Smtp.php';
         if ($this->conf['smtp_server_username'] && $this->conf['smtp_server_password']) {
             msg('using SMTP auth', 'ok', 2);
             $config = array('auth' => 'login', 'username' => $this->conf['smtp_server_username'], 'password' => $this->conf['smtp_server_password']);
         } else {
             $config = array();
             msg("Not using SMTP auth", 'ok', 2);
         }
         $transport = new Zend_Mail_Transport_Smtp($this->conf['smtp_server_address'], $config);
         Zend_Mail::setDefaultTransport($transport);
     } else {
         msg('use internal mail()', 'ok', 2);
     }
     /**
      * attachment(s) via upload
      */
     if (count($_FILES) > 0) {
         foreach ($_FILES as $key => $file) {
             if (is_uploaded_file($file['tmp_name'])) {
                 /**
                  * file
                  */
                 require_once 'models/common/common_file.php';
                 //getSingleUpload could be static method
                 $CommonFile = new common_file();
                 $upload = $CommonFile->getSingleUpload($file, 'var/tmp/');
                 /**
                  * array indicated the same file name already exists in the var/tmp/ folder
                  * we can ignore it, as the previous attachement was overwritten
                  * FIXME: could be a problem when more users submit the same filename in the same time
                  * perhaps saving file with PHP session id or not saving in var/tmp would help
                  */
                 if (is_array($upload)) {
                     $attachment_saved_file = ONXSHOP_PROJECT_DIR . $upload['temp_file'];
                 } else {
                     $attachment_saved_file = ONXSHOP_PROJECT_DIR . $upload;
                 }
                 /**
                  * check if file exists and than add to email as attachemnt
                  */
                 if (file_exists($attachment_saved_file)) {
                     $attachment_info = $CommonFile->getFileInfo($attachment_saved_file);
                     $Attachment = $mail->createAttachment(file_get_contents($attachment_saved_file));
                     $Attachment->filename = $attachment_info['filename'];
                 }
             }
         }
     }
     /**
      * attachments
      * quick hack to add attachment functionality, curently used only in gift_voucher_generate
      */
     if (is_array($GLOBALS['onxshop_atachments']) && count($GLOBALS['onxshop_atachments']) > 0) {
         foreach ($GLOBALS['onxshop_atachments'] as $file) {
             if (file_exists($file)) {
                 require_once 'models/common/common_file.php';
                 $CommonFile = new common_file();
                 $attachment_info = $CommonFile->getFileInfo($file);
                 $Attachment = $mail->createAttachment(file_get_contents($file));
                 $Attachment->filename = $attachment_info['filename'];
             }
         }
     }
     /**
      * content alternative text
      */
     $mail->setBodyText($email_data['content']['txt']);
     $mail->setBodyHtml($email_data['content']['html']);
     /**
      * send
      */
     if (!$mail->send()) {
         msg('The email was not sent! Some problem with email sending.', 'error');
         return false;
     } else {
         msg("The email to {$this->email_recipient} has been sent successfully.", 'ok', 2);
         return true;
     }
 }
 /**
  * saveFile
  */
 public function saveFile($file_single, $survey_entry_id, $question_id, $answer_id)
 {
     // find survey_id by $survey_entry_id
     require_once 'models/education/education_survey_entry.php';
     $Survey_Entry = new education_survey_entry();
     $survey_entry_data = $Survey_Entry->detail($survey_entry_id);
     $survey_id = $survey_entry_data['survey_id'];
     /**
      * add prefix to filename (rename)
      */
     $file_single['name'] = $this->getFilenameToSave($file_single['name'], $survey_entry_id, $question_id, $answer_id);
     /**
      * file
      */
     require_once 'models/common/common_file.php';
     //getSingleUpload could be a static method
     $CommonFile = new common_file();
     $upload = $CommonFile->getSingleUpload($file_single, "var/surveys/{$survey_id}/");
     /**
      * array indicated the same file name already exists in the var/tmp/ folder
      * this should never happen as we have entry id in filename 
      */
     if (is_array($upload)) {
         $attachment_saved_file = ONXSHOP_PROJECT_DIR . $upload['temp_file'];
     } else {
         $attachment_saved_file = ONXSHOP_PROJECT_DIR . $upload;
     }
     /**
      * check if file exists and than return filename
      */
     if (file_exists($attachment_saved_file)) {
         $attachment_info = $CommonFile->getFileInfo($attachment_saved_file);
         return $attachment_info['filename'];
     }
 }