Skip to content

Use sandbox to include every user defined PHP files #3321

@ozh

Description

@ozh

Since YOURLS 1.9 we have a foolproof sandbox to safely include PHP files, used for plugins :

/**
* Plugin activation sandbox
*
* @since 1.8.3
* @param string $pluginfile Plugin filename (full path)
* @return string|true string if error or true if success
*/
function yourls_activate_plugin_sandbox( $pluginfile ) {
try {
include_once $pluginfile;
return true;
} catch ( \Throwable $e ) {
return $e->getMessage();
}
}

Rename this function to make it more generic, update tests accordingly, and reuse it for :

Pages:

include_once( YOURLS_PAGEDIR . "/$page.php" );

Custom DB error:

include_once( YOURLS_USERDIR . '/db_error.php' );

Custom maintenance page:

include_once( YOURLS_USERDIR.'/maintenance.php' );

Custom DB or cache layer:

/**
* @since 1.7.3
* @return void
*/
public function include_db_files() {
// Allow drop-in replacement for the DB engine
if (file_exists(YOURLS_USERDIR.'/db.php')) {
require_once YOURLS_USERDIR.'/db.php';
} else {
require_once YOURLS_INC.'/class-mysql.php';
yourls_db_connect();
}
}
/**
* @since 1.7.3
* @return void
*/
public function include_cache_files() {
if (file_exists(YOURLS_USERDIR.'/cache.php')) {
require_once YOURLS_USERDIR.'/cache.php';
}
}

(by the way, rename that "cache.php" to something understandable -- must-use-plugin.php? unconditional-use-plugin.php?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions