private function write_uploaded_data_to_file($file_path)
 {
     $base_dir = dirname($file_path);
     if (!file_exists($base_dir) && !mkdir($base_dir, awpcp_directory_permissions(), true)) {
         throw new AWPCP_Exception(__("Temporary directory doesn't exists and couldn't be created.", 'AWPCP'));
     }
     if (!empty($_FILES) && isset($_FILES['file'])) {
         if ($_FILES['file']['error']) {
             list($error_code, $error_message) = awpcp_uploaded_file_error($_FILES['file']);
             throw new AWPCP_Exception($error_message);
         }
         if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
             throw new AWPCP_Exception(__('There was an error trying to move the uploaded file to a temporary location.', 'AWPCP'));
         }
         move_uploaded_file($_FILES['file']['tmp_name'], $file_path);
     } else {
         if (!($input = fopen('php://input', 'rb'))) {
             throw new AWPCP_Exception(__("There was an error trying to open PHP's input stream.", 'AWPCP'));
         }
         if (!($output = fopen($file_path, 'wb'))) {
             throw new AWPCP_Exception(__('There was an error trying to open the output stream.', 'AWPCP'));
         }
         while ($buffer = fread($input, 4096)) {
             fwrite($output, $buffer);
         }
         fclose($output);
         fclose($input);
     }
 }
 public function add_image($ad, $media)
 {
     $action = awpcp_post_param('awpcp_action', false);
     $errors = array();
     if ($action !== 'add_image' || is_null($ad)) {
         return $this->show_images($ad);
     }
     if ($_FILES['awpcp_add_file']['error'] !== 0) {
         $message = awpcp_uploaded_file_error($_FILES['awpcp_add_file']);
         awpcp_flash(end($message), 'error');
     } else {
         if (wp_verify_nonce($_POST['_wpnonce'], 'awpcp_upload_image')) {
             $files = array('awpcp_add_file' => $_FILES['awpcp_add_file']);
             $uploaded = awpcp_upload_files($ad, $files, $errors);
             if (empty($uploaded)) {
                 $message = _x('There was an error trying to upload your file.', 'media manager', 'AWPCP');
                 awpcp_flash(awpcp_array_data('awpcp_add_file', $message, $errors), 'error');
             } else {
                 $admin_must_approve = get_awpcp_option('imagesapprove');
                 $is_admin_user = awpcp_current_user_is_admin();
                 if (!$is_admin_user && $admin_must_approve) {
                     awpcp_ad_awaiting_approval_email($ad, false, true);
                 }
                 awpcp_flash(_x('The file was properly uploaded.', 'media manager', 'AWPCP'));
             }
         }
     }
     return $this->show_images($ad);
 }
 public function dispatch()
 {
     global $awpcp_plugin_path;
     global $start_date;
     global $end_date;
     global $import_date_format;
     global $date_sep;
     global $time_sep;
     global $auto_cat;
     global $assign_user;
     global $assigned_user;
     global $test_import;
     global $import_count;
     global $reject_count;
     global $pic_import_count;
     global $import_errors;
     $do_import = awpcp_post_param('do_import');
     $do_test_import = awpcp_post_param('do_test_import');
     if (!empty($do_import)) {
         $import_type = 'Import';
     } else {
         if (!empty($do_test_import)) {
             $import_type = 'Test Import';
         } else {
             $import_type = false;
         }
     }
     $test_import = strcmp($import_type, "Test Import") === 0;
     $start_date = awpcp_post_param("startDate", '');
     $end_date = awpcp_post_param("endDate", '');
     $import_date_format = awpcp_post_param("date_fmt", 'us_date');
     $date_sep = awpcp_post_param("sep_date", '/');
     $time_sep = awpcp_post_param("sep_time", ':');
     $auto_cat = awpcp_post_param("auto_cat", 0);
     $assign_user = awpcp_post_param('assign_user', 0);
     $assigned_user = intval(awpcp_post_param('user', 0));
     // Original implementation used a global var to pass errors.
     // That is still used until I got a change to refactor the
     // existing functions to use an errors array passed by reference.
     // The messages array is only used to report when a new user
     // is created.
     $errors = array();
     $messages = array();
     $form_errors = array();
     $importer = null;
     if (!empty($import_type)) {
         $msg = __('There was an error with your CSV file: %s', 'AWPCP');
         list($csv_error, $message) = awpcp_uploaded_file_error($_FILES['import']);
         if (!in_array($csv_error, array(UPLOAD_ERR_OK))) {
             $form_errors['import'] = sprintf($msg, $message);
         } else {
             $csv_file_name = $_FILES['import']['name'];
             $ext = trim(strtolower(substr(strrchr($csv_file_name, "."), 1)));
             if ($ext != "csv") {
                 $form_errors['import'] = sprintf($msg, __('Please upload a valid CSV file.', 'AWPCP'));
             }
         }
         $msg = __('There was an error with your ZIP file: %s', 'AWPCP');
         list($zip_error, $message) = awpcp_uploaded_file_error($_FILES['import_zip']);
         if (!in_array($zip_error, array(UPLOAD_ERR_OK, UPLOAD_ERR_NO_FILE))) {
             $form_errors['import_zip'] = sprintf($msg, $message);
         } else {
             if ($zip_error === UPLOAD_ERR_OK) {
                 $zip_file_name = $_FILES['import_zip']['name'];
                 $ext = trim(strtolower(substr(strrchr($zip_file_name, "."), 1)));
                 if ($ext != "zip") {
                     $form_errors['import_zip'] = sprintf($msg, __('Please upload a valid ZIP file.', 'AWPCP'));
                 }
             }
         }
         if (!empty($start_date)) {
             $date_arr = explode("/", $start_date);
             if (!is_valid_date($date_arr[0], $date_arr[1], $date_arr[2])) {
                 $form_errors['startDate'] = __('Invalid Start Date.', 'AWPCP');
             } else {
                 if (strlen($date_arr[2]) != 4) {
                     $form_errors['startDate'] = __('Invalid Start Date -- Year Must be of Four Digit.', 'AWPCP');
                 }
             }
         }
         if (!empty($end_date)) {
             $date_arr = explode("/", $end_date);
             if (!is_valid_date($date_arr[0], $date_arr[1], $date_arr[2])) {
                 $form_errors['endDate'] = __('Invalid End Date.', 'AWPCP');
             } else {
                 if (strlen($date_arr[2]) != 4) {
                     $form_errors['endDate'] = __('Invalid End Date -- Year Must be of Four Digit.', 'AWPCP');
                 }
             }
         }
         if (empty($form_errors)) {
             if (empty($errors)) {
                 $csv = $_FILES['import']['tmp_name'];
                 $zip = $_FILES['import_zip']['tmp_name'];
                 $importer = new AWPCP_CSV_Importer(array('start-date' => $start_date, 'end-date' => $end_date, 'date-format' => $import_date_format, 'date-separator' => $date_sep, 'time-separator' => $time_sep, 'autocreate-categories' => $auto_cat, 'assign-user' => $assign_user, 'default-user' => $assigned_user, 'test-import' => $test_import));
                 $importer->import($csv, $zip, $errors, $messages);
             }
         }
     }
     ob_start();
     include AWPCP_DIR . '/admin/templates/admin-panel-csv-importer.tpl.php';
     $html = ob_get_contents();
     ob_end_clean();
     echo $html;
 }