/** * Creates a file * * @param string $job_id * @param string $filename * @param string $filetype * @return string * @throws \EventEspressoBatchRequest\Helpers\BatchRequestException */ public function create_file_from_job_with_name($job_id, $filename, $filetype = 'application/ms-excel') { $filepath = ''; try { $base_folder = $this->get_base_folder(); $success = $this->_file_helper->ensure_folder_exists_and_is_writable($base_folder . JobHandlerFile::temp_folder_name); if ($success) { $success = $this->_file_helper->ensure_folder_exists_and_is_writable($base_folder . JobHandlerFile::temp_folder_name . DS . $job_id); } if ($success) { $filepath = $base_folder . JobHandlerFile::temp_folder_name . DS . $job_id . DS . $filename; $success = $this->_file_helper->ensure_file_exists_and_is_writable($filepath); } //let's add the .htaccess file so safari will open the file properly if ($success) { $extension = \EEH_File::get_file_extension($filepath); \EEH_File::write_to_file($base_folder . JobHandlerFile::temp_folder_name . DS . $job_id . DS . '.htaccess', 'AddType ' . $filetype . ' ' . $extension, '.htaccess'); } //those methods normally fail with an exception, but if not, let's do it if (!$success) { throw new \EE_Error(__('Could not create temporary file, an unknown error occurred', 'event_espresso')); } } catch (\EE_Error $e) { throw new BatchRequestException(sprintf(__('Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso'), $job_id, $e->getMessage()), 500, $e); } return $filepath; }
/** * Writes $data to the csv file open in $filehandle. uses the array indices of $data for column headers * * @param string $filepath * @param array $data 2D array, first numerically-indexed, * and next-level-down preferably indexed by string * @param boolean $write_column_headers whether or not we should add the keys in the bottom-most array * as a row for headers in the CSV. * Eg, if $data looked like: * array( * 0=>array('EVT_ID'=>1,'EVT_name'=>'monkey'...), * 1=>array(...,...) * ) * * @return boolean if we successfully wrote to the CSV or not. If there's no $data, * we consider that a success (because we wrote everything there was...nothing) * @throws EE_Error */ public static function write_data_array_to_csv($filepath, $data, $write_column_headers = true) { $new_file_contents = ''; //determine if $data is actually a 2d array if ($data && is_array($data) && is_array(EEH_Array::get_one_item_from_array($data))) { //make sure top level is numerically indexed, if (EEH_Array::is_associative_array($data)) { throw new EE_Error(sprintf(__("top-level array must be numerically indexed. Does these look like numbers to you? %s", "event_espresso"), implode(",", array_keys($data)))); } $item_in_top_level_array = EEH_Array::get_one_item_from_array($data); //now, is the last item in the top-level array of $data an associative or numeric array? if ($write_column_headers && EEH_Array::is_associative_array($item_in_top_level_array)) { //its associative, so we want to output its keys as column headers $keys = array_keys($item_in_top_level_array); $new_file_contents .= EEH_Export::get_csv_row($keys); } //start writing data foreach ($data as $data_row) { $new_file_contents .= EEH_Export::get_csv_row($data_row); } return EEH_File::write_to_file($filepath, EEH_File::get_file_contents($filepath) . $new_file_contents); } else { //no data TO write... so we can assume that's a success return true; } }
public function test_write_to_file() { global $wp_filesystem; $wp_filesystem->chmod('/', '755'); $file_path = '/test.txt'; $content1 = '<html><body>hello</body></html>'; $content2 = '<xml><monkeys><monkey/></monkeys></xml>'; $this->assertFalse($wp_filesystem->exists($file_path)); $wp_filesystem->put_contents($file_path, $content1, '644'); $this->assertTrue($wp_filesystem->exists($file_path)); $this->assertEquals($content1, EEH_File::get_file_contents($file_path)); //now add to it EEH_File::write_to_file($file_path, $content2); $this->assertEquals($content2, EEH_File::get_file_contents($file_path)); }
/** * write exception details to log file * * @access public * @ param timestamp $time * @ param object $ex * @ return void */ public function write_to_error_log($time = FALSE, $ex = FALSE, $clear = FALSE) { if (!$ex) { return; } if (!$time) { $time = time(); } $exception_log = '----------------------------------------------------------------------------------------' . PHP_EOL; $exception_log .= '[' . date('Y-m-d H:i:s', $time) . '] Exception Details' . PHP_EOL; $exception_log .= 'Message: ' . $ex['msg'] . PHP_EOL; $exception_log .= 'Code: ' . $ex['code'] . PHP_EOL; $exception_log .= 'File: ' . $ex['file'] . PHP_EOL; $exception_log .= 'Line No: ' . $ex['line'] . PHP_EOL; $exception_log .= 'Stack trace: ' . PHP_EOL; $exception_log .= $ex['string'] . PHP_EOL; $exception_log .= '----------------------------------------------------------------------------------------' . PHP_EOL; EE_Registry::instance()->load_helper('File'); try { EEH_File::ensure_file_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file); EEH_File::add_htaccess_deny_from_all(EVENT_ESPRESSO_UPLOAD_DIR . 'logs'); if (!$clear) { //get existing log file and append new log info $exception_log = EEH_File::get_file_contents(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file) . $exception_log; } EEH_File::write_to_file(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file, $exception_log); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('Event Espresso error logging could not be setup because: %s', 'event_espresso'), $e->getMessage())); return; } }
/** * add_htaccess_deny_from_all * @param string $folder * @return bool */ public static function add_htaccess_deny_from_all($folder = '') { $folder = EEH_File::standardise_and_end_with_directory_separator($folder); if (!EEH_File::exists($folder . '.htaccess')) { if (!EEH_File::write_to_file($folder . '.htaccess', 'deny from all', '.htaccess')) { return FALSE; } } return TRUE; }
/** * captures plugin activation errors for debugging * * @return void */ public static function ee_plugin_activation_errors() { if (WP_DEBUG) { $activation_errors = ob_get_contents(); if (!empty($activation_errors)) { $activation_errors = date('Y-m-d H:i:s') . "\n" . $activation_errors; } espresso_load_required('EEH_File', EE_HELPERS . 'EEH_File.helper.php'); if (class_exists('EEH_File')) { try { EEH_File::ensure_file_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html'); EEH_File::write_to_file(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html', $activation_errors); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('The Event Espresso activation errors file could not be setup because: %s', 'event_espresso'), $e->getMessage()), __FILE__, __FUNCTION__, __LINE__); } } else { // old school attempt file_put_contents(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html', $activation_errors); } $activation_errors = get_option('ee_plugin_activation_errors', '') . $activation_errors; update_option('ee_plugin_activation_errors', $activation_errors); } }
/** * captures plugin activation errors for debugging * * @return void */ public static function ee_plugin_activation_errors() { if (defined('WP_DEBUG') && WP_DEBUG) { $activation_errors = ob_get_contents(); if (class_exists('EE_Registry')) { EE_Registry::instance()->load_helper('File'); } else { include_once EE_HELPERS . 'EEH_File.helper.php'; } if (class_exists('EEH_File')) { try { EEH_File::ensure_folder_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS); EEH_File::ensure_file_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html'); EEH_File::write_to_file(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html', $activation_errors); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('The Event Espresso activation errors file could not be setup because: %s', 'event_espresso'), $e->getMessage())); } } else { // old school attempt file_put_contents(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html', $activation_errors); } $activation_errors = get_option('ee_plugin_activation_errors', '') . $activation_errors; update_option('ee_plugin_activation_errors', $activation_errors); } }
/** * write_debug * writes the contents of the current request's $_GET and $_POST arrays to a log file. * previous entries are overwritten */ public function write_debug() { if (defined('WP_DEBUG') && WP_DEBUG) { $this->_debug_log = ''; foreach ($_GET as $key => $value) { $this->_debug_log .= '$_GET["' . $key . '"] = "' . serialize($value) . '"' . PHP_EOL; } foreach ($_POST as $key => $value) { $this->_debug_log .= '$_POST["' . $key . '"] = "' . serialize($value) . '"' . PHP_EOL; } try { EEH_File::write_to_file($this->_logs_folder . $this->_debug_file, $this->_debug_log, 'Event Espresso Debug Log'); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('Could not write to the Event Espresso debug log file because: %s', 'event_espresso'), ' ' . $e->getMessage()), __FILE__, __FUNCTION__, __LINE__); return; } } }
/** * Adds an index file to this folder, so folks can't list all the file's contents * @param string $folder * @throws EE_Error if filesystem credentials are required * @return boolean */ public static function add_index_file($folder) { $folder = EEH_File::standardise_and_end_with_directory_separator($folder); if (!EEH_File::exists($folder . 'index.php')) { if (!EEH_File::write_to_file($folder . 'index.php', 'You are not permitted to read from this folder', '.php')) { return false; } } return true; }
/** * captures plugin activation errors for debugging * * @return void */ public function ee_plugin_activation_errors() { if (WP_DEBUG === TRUE) { $errors = ob_get_contents(); if (include_once EE_HELPERS . 'EEH_File.helper.php') { try { EEH_File::ensure_folder_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS); EEH_File::ensure_file_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html'); EEH_File::write_to_file(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html', $errors); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('The Event Espresso activation errors file could not be setup because: %s', 'event_espresso'), $e->getMessage())); } } else { // old school attempt file_put_contents(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html', $errors); } update_option('ee_plugin_activation_errors', $errors); } }