Plugin Directory

Changeset 3385766


Ignore:
Timestamp:
10/28/2025 10:39:06 AM (4 months ago)
Author:
nickclarkweb
Message:

'code-quality-control-tool'

Location:
code-quality-control-tool
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • code-quality-control-tool/tags/2.2/code-quality-control-tool.php

    r3303032 r3385766  
    427427    register_uninstall_hook(__FILE__, 'cqctphp_delete_plugin');
    428428    function cqctphp_delete_plugin() {
    429         $log_file = WP_CONTENT_DIR . '/_php_errors.log';
     429        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     430        PHPCodeControl_general::ensurePrivateDir($log_folder);
     431
     432        $log_file = $log_folder . '/_php_errors.log';
    430433        if (file_exists($log_file)) unlink($log_file);
    431434    }
     
    494497    }
    495498
     499    /**
     500     * Creates a directory (if not exists) and places a .htaccess file inside
     501     * to block direct access to its contents.
     502     *
     503     * @param string $dir Absolute or relative directory path
     504     * @param int $mode Permissions for the directory (default 0755)
     505     * @throws RuntimeException on creation/write errors
     506     */
     507    public static function ensurePrivateDir(string $dir, int $mode = 0755): void
     508    {
     509        // Normalize the path
     510        $dir = rtrim($dir, DIRECTORY_SEPARATOR);
     511
     512        // If directory does not exist — create it (including intermediate directories)
     513        if (!is_dir($dir)) {
     514            // Keep in mind that real permissions are affected by umask: $mode & ~umask()
     515            if (!mkdir($dir, $mode, true) && !is_dir($dir)) {
     516                throw new RuntimeException("Failed to create directory: {$dir}");
     517            }
     518        }
     519
     520        // Check if directory is writable
     521        if (!is_writable($dir)) {
     522            throw new RuntimeException("Directory exists but is not writable: {$dir}");
     523        }
     524
     525        // Path to .htaccess
     526        $htaccessPath = $dir . DIRECTORY_SEPARATOR . '.htaccess';
     527
     528        // .htaccess content for Apache 2.4+ (modern syntax)
     529        // Alternative (for older Apache): "Deny from all"
     530        $htaccess = <<<HTA
     531# Deny direct access to this directory
     532# Apache 2.4+:
     533Require all denied
     534
     535# Backwards compatibility:
     536<IfModule !mod_authz_core.c>
     537    Deny from all
     538</IfModule>
     539HTA;
     540
     541        // Write the file (overwrite if exists)
     542        if (file_put_contents($htaccessPath, $htaccess, LOCK_EX) === false) {
     543            throw new RuntimeException("Failed to write .htaccess to: {$htaccessPath}");
     544        }
     545
     546        // (Optional) set secure permissions on .htaccess
     547        @chmod($htaccessPath, 0644);
     548    }
     549
    496550    // Get log file path
    497551    public static function GetLogFile() {
    498         return WP_CONTENT_DIR . '/_php_errors.log';
     552        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     553        self::ensurePrivateDir($log_folder);
     554        return $log_folder . '/_php_errors.log';
    499555    }
    500556
    501557    // Get counter file path
    502558    public static function GetErrorCounterFile() {
    503         return WP_CONTENT_DIR . '/_php_errors.count.log';
     559        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     560        self::ensurePrivateDir($log_folder);
     561        return $log_folder . '/_php_errors.count.log';
    504562    }
    505563
    506564    // Get settings file path
    507565    public static function GetSettingsFile() {
    508         return WP_CONTENT_DIR . '/_php_code_control.ini';
     566        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     567        self::ensurePrivateDir($log_folder);
     568        return $log_folder . '/_php_code_control.ini';
    509569    }
    510570
  • code-quality-control-tool/tags/2.2/error_logger.php

    r3303032 r3385766  
    77{
    88    $WP_CONTENT_DIR = dirname(dirname(dirname(__FILE__)));
    9    
     9
     10    $WP_CONTENT_DIR = $WP_CONTENT_DIR . '/code-quality-logs';
     11    PHPCodeControl_logger::ensurePrivateDir($WP_CONTENT_DIR);
    1012
    1113    $settings = parse_ini_file($WP_CONTENT_DIR.'/_php_code_control.ini');
    12    
     14
    1315    // Check if active
    1416    if (!isset($settings['is_active']) && $settings['is_active'] == 0) return;
     
    152154
    153155$old_error_handler_5FB609D0735B = set_error_handler("cqctphp_start_phptrace_error_handler");
     156
     157
     158class PHPCodeControl_logger {
     159
     160    /**
     161     * Creates a directory (if not exists) and places a .htaccess file inside
     162     * to block direct access to its contents.
     163     *
     164     * @param string $dir Absolute or relative directory path
     165     * @param int $mode Permissions for the directory (default 0755)
     166     * @throws RuntimeException on creation/write errors
     167     */
     168    public static function ensurePrivateDir(string $dir, int $mode = 0755): void
     169    {
     170        // Normalize the path
     171        $dir = rtrim($dir, DIRECTORY_SEPARATOR);
     172
     173        // If directory does not exist — create it (including intermediate directories)
     174        if (!is_dir($dir)) {
     175            // Keep in mind that real permissions are affected by umask: $mode & ~umask()
     176            if (!mkdir($dir, $mode, true) && !is_dir($dir)) {
     177                throw new RuntimeException("Failed to create directory: {$dir}");
     178            }
     179        }
     180
     181        // Check if directory is writable
     182        if (!is_writable($dir)) {
     183            throw new RuntimeException("Directory exists but is not writable: {$dir}");
     184        }
     185
     186        // Path to .htaccess
     187        $htaccessPath = $dir . DIRECTORY_SEPARATOR . '.htaccess';
     188
     189        // .htaccess content for Apache 2.4+ (modern syntax)
     190        // Alternative (for older Apache): "Deny from all"
     191        $htaccess = <<<HTA
     192# Deny direct access to this directory
     193# Apache 2.4+:
     194Require all denied
     195
     196# Backwards compatibility:
     197<IfModule !mod_authz_core.c>
     198    Deny from all
     199</IfModule>
     200HTA;
     201
     202        // Write the file (overwrite if exists)
     203        if (file_put_contents($htaccessPath, $htaccess, LOCK_EX) === false) {
     204            throw new RuntimeException("Failed to write .htaccess to: {$htaccessPath}");
     205        }
     206
     207        // (Optional) set secure permissions on .htaccess
     208        @chmod($htaccessPath, 0644);
     209    }
     210}
  • code-quality-control-tool/tags/2.2/readme.txt

    r3303032 r3385766  
    44Requires at least: 3.0
    55Tested up to: 6.8
    6 Stable tag: 2.1
     6Stable tag: 2.2
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
  • code-quality-control-tool/trunk/code-quality-control-tool.php

    r3303032 r3385766  
    427427    register_uninstall_hook(__FILE__, 'cqctphp_delete_plugin');
    428428    function cqctphp_delete_plugin() {
    429         $log_file = WP_CONTENT_DIR . '/_php_errors.log';
     429        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     430        PHPCodeControl_general::ensurePrivateDir($log_folder);
     431
     432        $log_file = $log_folder . '/_php_errors.log';
    430433        if (file_exists($log_file)) unlink($log_file);
    431434    }
     
    494497    }
    495498
     499    /**
     500     * Creates a directory (if not exists) and places a .htaccess file inside
     501     * to block direct access to its contents.
     502     *
     503     * @param string $dir Absolute or relative directory path
     504     * @param int $mode Permissions for the directory (default 0755)
     505     * @throws RuntimeException on creation/write errors
     506     */
     507    public static function ensurePrivateDir(string $dir, int $mode = 0755): void
     508    {
     509        // Normalize the path
     510        $dir = rtrim($dir, DIRECTORY_SEPARATOR);
     511
     512        // If directory does not exist — create it (including intermediate directories)
     513        if (!is_dir($dir)) {
     514            // Keep in mind that real permissions are affected by umask: $mode & ~umask()
     515            if (!mkdir($dir, $mode, true) && !is_dir($dir)) {
     516                throw new RuntimeException("Failed to create directory: {$dir}");
     517            }
     518        }
     519
     520        // Check if directory is writable
     521        if (!is_writable($dir)) {
     522            throw new RuntimeException("Directory exists but is not writable: {$dir}");
     523        }
     524
     525        // Path to .htaccess
     526        $htaccessPath = $dir . DIRECTORY_SEPARATOR . '.htaccess';
     527
     528        // .htaccess content for Apache 2.4+ (modern syntax)
     529        // Alternative (for older Apache): "Deny from all"
     530        $htaccess = <<<HTA
     531# Deny direct access to this directory
     532# Apache 2.4+:
     533Require all denied
     534
     535# Backwards compatibility:
     536<IfModule !mod_authz_core.c>
     537    Deny from all
     538</IfModule>
     539HTA;
     540
     541        // Write the file (overwrite if exists)
     542        if (file_put_contents($htaccessPath, $htaccess, LOCK_EX) === false) {
     543            throw new RuntimeException("Failed to write .htaccess to: {$htaccessPath}");
     544        }
     545
     546        // (Optional) set secure permissions on .htaccess
     547        @chmod($htaccessPath, 0644);
     548    }
     549
    496550    // Get log file path
    497551    public static function GetLogFile() {
    498         return WP_CONTENT_DIR . '/_php_errors.log';
     552        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     553        self::ensurePrivateDir($log_folder);
     554        return $log_folder . '/_php_errors.log';
    499555    }
    500556
    501557    // Get counter file path
    502558    public static function GetErrorCounterFile() {
    503         return WP_CONTENT_DIR . '/_php_errors.count.log';
     559        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     560        self::ensurePrivateDir($log_folder);
     561        return $log_folder . '/_php_errors.count.log';
    504562    }
    505563
    506564    // Get settings file path
    507565    public static function GetSettingsFile() {
    508         return WP_CONTENT_DIR . '/_php_code_control.ini';
     566        $log_folder = WP_CONTENT_DIR . '/code-quality-logs';
     567        self::ensurePrivateDir($log_folder);
     568        return $log_folder . '/_php_code_control.ini';
    509569    }
    510570
  • code-quality-control-tool/trunk/error_logger.php

    r3303032 r3385766  
    77{
    88    $WP_CONTENT_DIR = dirname(dirname(dirname(__FILE__)));
    9    
     9
     10    $WP_CONTENT_DIR = $WP_CONTENT_DIR . '/code-quality-logs';
     11    PHPCodeControl_logger::ensurePrivateDir($WP_CONTENT_DIR);
    1012
    1113    $settings = parse_ini_file($WP_CONTENT_DIR.'/_php_code_control.ini');
    12    
     14
    1315    // Check if active
    1416    if (!isset($settings['is_active']) && $settings['is_active'] == 0) return;
     
    152154
    153155$old_error_handler_5FB609D0735B = set_error_handler("cqctphp_start_phptrace_error_handler");
     156
     157
     158class PHPCodeControl_logger {
     159
     160    /**
     161     * Creates a directory (if not exists) and places a .htaccess file inside
     162     * to block direct access to its contents.
     163     *
     164     * @param string $dir Absolute or relative directory path
     165     * @param int $mode Permissions for the directory (default 0755)
     166     * @throws RuntimeException on creation/write errors
     167     */
     168    public static function ensurePrivateDir(string $dir, int $mode = 0755): void
     169    {
     170        // Normalize the path
     171        $dir = rtrim($dir, DIRECTORY_SEPARATOR);
     172
     173        // If directory does not exist — create it (including intermediate directories)
     174        if (!is_dir($dir)) {
     175            // Keep in mind that real permissions are affected by umask: $mode & ~umask()
     176            if (!mkdir($dir, $mode, true) && !is_dir($dir)) {
     177                throw new RuntimeException("Failed to create directory: {$dir}");
     178            }
     179        }
     180
     181        // Check if directory is writable
     182        if (!is_writable($dir)) {
     183            throw new RuntimeException("Directory exists but is not writable: {$dir}");
     184        }
     185
     186        // Path to .htaccess
     187        $htaccessPath = $dir . DIRECTORY_SEPARATOR . '.htaccess';
     188
     189        // .htaccess content for Apache 2.4+ (modern syntax)
     190        // Alternative (for older Apache): "Deny from all"
     191        $htaccess = <<<HTA
     192# Deny direct access to this directory
     193# Apache 2.4+:
     194Require all denied
     195
     196# Backwards compatibility:
     197<IfModule !mod_authz_core.c>
     198    Deny from all
     199</IfModule>
     200HTA;
     201
     202        // Write the file (overwrite if exists)
     203        if (file_put_contents($htaccessPath, $htaccess, LOCK_EX) === false) {
     204            throw new RuntimeException("Failed to write .htaccess to: {$htaccessPath}");
     205        }
     206
     207        // (Optional) set secure permissions on .htaccess
     208        @chmod($htaccessPath, 0644);
     209    }
     210}
  • code-quality-control-tool/trunk/readme.txt

    r3303032 r3385766  
    44Requires at least: 3.0
    55Tested up to: 6.8
    6 Stable tag: 2.1
     6Stable tag: 2.2
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset for help on using the changeset viewer.