public function test_ensure_folder_exists_and_is_writable__and__is_writable() { global $wp_filesystem; $folder_path = '/test/'; // Test creation/exists checks $this->assertFalse($wp_filesystem->is_dir($folder_path)); $this->assertTrue(EEH_File::ensure_folder_exists_and_is_writable($folder_path)); $this->assertTrue(EEH_File::verify_is_writable($folder_path)); $wp_filesystem->chmod($folder_path, '000'); try { $this->assertFalse(EEH_File::ensure_folder_exists_and_is_writable($folder_path)); $this->fail(sprintf(__('An exception SHOULD have been thrown but wasn\'t', 'event_espresso'))); } catch (EE_Error $e) { $this->assertTrue(TRUE); } try { $this->assertFalse(EEH_File::verify_is_writable($folder_path)); $this->fail(sprintf(__('An exception SHOULD have been thrown but wasn\'t', 'event_espresso'))); } catch (EE_Error $e) { $this->assertTrue(TRUE); } }
/** * create_upload_directories * Creates folders in the uploads directory to facilitate addons and templates * * @access public * @static * @return boolean success of verifying upload directories exist */ public static function create_upload_directories() { EE_Registry::instance()->load_helper('File'); // Create the required folders $folders = array(EVENT_ESPRESSO_TEMPLATE_DIR, EVENT_ESPRESSO_GATEWAY_DIR, EVENT_ESPRESSO_UPLOAD_DIR . 'logs/', EVENT_ESPRESSO_UPLOAD_DIR . 'css/', EVENT_ESPRESSO_UPLOAD_DIR . 'tickets/'); foreach ($folders as $folder) { try { EEH_File::ensure_folder_exists_and_is_writable($folder); @chmod($folder, 0755); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('Could not create the folder at "%1$s" because: %2$s', 'event_espresso'), $folder, '<br />' . $e->getMessage()), __FILE__, __FUNCTION__, __LINE__); //indicate we'll need to fix this later update_option(EEH_Activation::upload_directories_incomplete_option_name, true); return FALSE; } } //just add the .htaccess file to the logs directory to begin with. Even if logging //is disabled, there might be activation errors recorded in there EEH_File::add_htaccess_deny_from_all(EVENT_ESPRESSO_UPLOAD_DIR . 'logs/'); //remember EE's folders are all good delete_option(EEH_Activation::upload_directories_incomplete_option_name); return TRUE; }
/** * 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 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_folder_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs'); EEH_File::add_htaccess_deny_from_all(EVENT_ESPRESSO_UPLOAD_DIR . 'logs'); EEH_File::ensure_file_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . self::$_exception_log_file); 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; } }
/** * ensure_file_exists_and_is_writable * ensures that a file exists and is writable, will attempt to create file if it does not exist. * Also ensures all the parent folders exist, and if not tries to create them. * @param string $full_file_path * @throws EE_Error if filesystem credentials are required * @return bool */ public static function ensure_file_exists_and_is_writable($full_file_path = '') { // load WP_Filesystem and set file permissions $wp_filesystem = EEH_File::_get_wp_filesystem($full_file_path); $full_file_path = EEH_File::standardise_directory_separators($full_file_path); $parent_folder = EEH_File::get_parent_folder($full_file_path); if (!EEH_File::exists($full_file_path)) { if (!EEH_File::ensure_folder_exists_and_is_writable($parent_folder)) { return false; } if (!$wp_filesystem->touch(EEH_File::convert_local_filepath_to_remote_filepath($full_file_path))) { if (defined('WP_DEBUG') && WP_DEBUG) { $msg = sprintf(__('The "%s" file could not be created.', 'event_espresso'), $full_file_path); $msg .= EEH_File::_permissions_error_for_unreadable_filepath($full_file_path); throw new EE_Error($msg); } return false; } } if (!EEH_File::verify_is_writable($full_file_path, 'file')) { return false; } return true; }
/** * create_upload_directories * Creates folders in the uploads directory to facilitate addons and templates * * @access public * @static * @return boolean success of verifying upload directories exist */ public static function create_upload_directories() { EE_Registry::instance()->load_helper('File'); // Create the required folders $folders = array(EVENT_ESPRESSO_UPLOAD_DIR, EVENT_ESPRESSO_TEMPLATE_DIR, EVENT_ESPRESSO_GATEWAY_DIR, EVENT_ESPRESSO_UPLOAD_DIR . '/logs/', EVENT_ESPRESSO_UPLOAD_DIR . '/css/', EVENT_ESPRESSO_UPLOAD_DIR . '/tickets/'); foreach ($folders as $folder) { try { EEH_File::ensure_folder_exists_and_is_writable($folder); @chmod($folder, 0755); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('Could not create the folder at "%1$s" because: %2$s', 'event_espresso'), $folder, '<br />' . $e->getMessage()), __FILE__, __FUNCTION__, __LINE__); 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); } }
/** * @group 9059 * @global type $wp_filesystem */ function test_ensure_folder_exists_and_is_writable__recursive_folders() { global $wp_filesystem; $folder_path = '/test/new/thing'; // Test creation/exists checks $this->assertFalse($wp_filesystem->is_dir($folder_path)); $this->assertTrue(EEH_File::ensure_folder_exists_and_is_writable($folder_path)); $folders_in_new_folder = $wp_filesystem->dirlist('/test/new/'); $this->assertTrue(isset($folders_in_new_folder['thing'])); $folders_in_new_folder = $wp_filesystem->dirlist('/test/new'); $this->assertTrue(isset($folders_in_new_folder['thing'])); }
/** * verify_filesystem * tests that the required files and folders exist and are writable * */ public function verify_filesystem() { try { EE_Registry::instance()->load_helper('File'); EEH_File::ensure_folder_exists_and_is_writable(EVENT_ESPRESSO_UPLOAD_DIR); EEH_File::ensure_folder_exists_and_is_writable($this->_logs_folder); EEH_File::add_htaccess_deny_from_all($this->_logs_folder); EEH_File::ensure_file_exists_and_is_writable($this->_logs_folder . $this->_log_file); EEH_File::ensure_file_exists_and_is_writable($this->_logs_folder . $this->_debug_file); } catch (EE_Error $e) { EE_Error::add_error(sprintf(__('Event Espresso logging could not be setup because: %s', 'event_espresso'), ' ' . $e->getMessage()), __FILE__, __FUNCTION__, __LINE__); return; } }