2012-06-18 11:26:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-03-09 19:44:14 +00:00
|
|
|
* @package Joomla.JEDChecker
|
|
|
|
*
|
2019-03-10 16:09:42 +00:00
|
|
|
* @copyright Copyright (C) 2017 - 2019 Open Source Matters, Inc. All rights reserved.
|
|
|
|
* Copyright (C) 2008 - 2016 compojoom.com . All rights reserved.
|
2019-03-10 08:49:52 +00:00
|
|
|
* @author Daniel Dimitrov <daniel@compojoom.com>
|
|
|
|
* eaxs <support@projectfork.net>
|
|
|
|
*
|
2019-03-09 19:44:14 +00:00
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
2012-06-18 11:26:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
2012-07-06 23:45:06 +00:00
|
|
|
// Include the rule base class
|
2013-11-05 20:17:39 +00:00
|
|
|
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';
|
2012-07-06 23:45:06 +00:00
|
|
|
|
2012-06-18 11:26:00 +00:00
|
|
|
/**
|
2013-11-05 20:17:39 +00:00
|
|
|
* class JedcheckerRulesJexec
|
|
|
|
*
|
2012-06-18 11:26:00 +00:00
|
|
|
* This class searches all files for the _JEXEC check
|
|
|
|
* which prevents direct file access.
|
|
|
|
*
|
2013-11-05 20:17:39 +00:00
|
|
|
* @since 1.0
|
2012-06-18 11:26:00 +00:00
|
|
|
*/
|
2013-11-05 20:17:39 +00:00
|
|
|
class JedcheckerRulesJexec extends JEDcheckerRule
|
2012-06-18 11:26:00 +00:00
|
|
|
{
|
2013-11-05 20:17:39 +00:00
|
|
|
/**
|
|
|
|
* The formal ID of this rule. For example: SE1.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $id = 'PH2';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The title or caption of this rule.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title = 'COM_JEDCHECKER_RULE_PH2';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The description of this rule.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'COM_JEDCHECKER_RULE_PH2_DESC';
|
|
|
|
|
2021-02-23 19:22:29 +00:00
|
|
|
/**
|
|
|
|
* Regexp to match _JEXEC-like guard
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $regex;
|
|
|
|
|
2021-03-09 20:41:06 +00:00
|
|
|
/**
|
|
|
|
* Regexp to match directories to skip
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $regexExcludeFolders;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of files related to libraries
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $libFiles;
|
|
|
|
|
2013-11-05 20:17:39 +00:00
|
|
|
/**
|
|
|
|
* Initiates the file search and check
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
2021-02-23 19:22:29 +00:00
|
|
|
$this->init_jexec();
|
|
|
|
|
2013-11-05 20:17:39 +00:00
|
|
|
// Find all php files of the extension
|
2021-03-09 20:41:06 +00:00
|
|
|
$files = $this->files($this->basedir);
|
2013-11-05 20:17:39 +00:00
|
|
|
|
|
|
|
// Iterate through all files
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// Try to find the _JEXEC check in the file
|
|
|
|
if (!$this->find($file))
|
|
|
|
{
|
|
|
|
// Add as error to the report if it was not found
|
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_JEXEC_NOT_FOUND'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a file and searches for the _JEXEC statement
|
|
|
|
*
|
|
|
|
* @param string $file - The path to the file
|
|
|
|
*
|
|
|
|
* @return boolean True if the statement was found, otherwise False.
|
|
|
|
*/
|
|
|
|
protected function find($file)
|
|
|
|
{
|
2021-02-23 19:20:25 +00:00
|
|
|
// load file and strip comments
|
|
|
|
$content = php_strip_whitespace($file);
|
|
|
|
|
|
|
|
// skip empty files
|
|
|
|
if ($content === '' || preg_match('#^<\?php\s+$#', $content))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-23 19:22:29 +00:00
|
|
|
// check guards
|
|
|
|
if (preg_match($this->regex, $content))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-05 20:17:39 +00:00
|
|
|
|
2021-02-23 19:22:29 +00:00
|
|
|
/**
|
|
|
|
* Prepare regexp aforehand
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function init_jexec()
|
|
|
|
{
|
2013-11-05 20:17:39 +00:00
|
|
|
$defines = $this->params->get('constants');
|
|
|
|
$defines = explode(',', $defines);
|
2012-06-27 13:53:50 +00:00
|
|
|
|
2021-02-23 19:22:29 +00:00
|
|
|
foreach ($defines as $i => $define)
|
2013-11-05 20:17:39 +00:00
|
|
|
{
|
2021-02-23 19:22:29 +00:00
|
|
|
$defines[$i] = preg_quote(trim($define), '#');
|
2013-11-05 20:17:39 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 19:22:29 +00:00
|
|
|
$this->regex
|
|
|
|
= '#^' // at the beginning of the file
|
|
|
|
. '<\?php\s+' // there is an opening php tag
|
2021-03-09 20:39:05 +00:00
|
|
|
. '(?:declare ?\(strict_types ?= ?1 ?\) ?; ?)?' // optionally followed by declare(strict_types=1) directive
|
|
|
|
. '(?:namespace [0-9A-Za-z_\\\\]+ ?; ?)?' // optionally followed by namespace directive
|
|
|
|
. '(?:use [0-9A-Za-z_\\\\]+ ?(?:as [0-9A-Za-z_]+ ?)?; ?)*' // optionally followed by use directives
|
2021-02-23 19:22:29 +00:00
|
|
|
. 'defined ?\( ?' // followed by defined test
|
|
|
|
. '([\'"])(?:' . implode('|', $defines) . ')\1' // of any of given constant
|
|
|
|
. ' ?\) ?(?:or |\|\| ?)(?:die|exit)\b' // or exit
|
|
|
|
. '#i'; // (case insensitive)
|
2021-03-09 20:41:06 +00:00
|
|
|
|
|
|
|
// Generate regular expression to match excluded directories
|
|
|
|
$libfolders = $this->params->get('libfolders');
|
|
|
|
$libfolders = explode(',', $libfolders);
|
|
|
|
|
|
|
|
foreach ($libfolders as &$libfolder)
|
|
|
|
{
|
|
|
|
$libfolder = preg_quote(trim($libfolder), '#');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend libFolders with default Joomla's exclude list
|
|
|
|
$this->regexExcludeFolders = '#^(?:\.svn|CVS|\.DS_Store|__MACOSX|' . implode('|', $libfolders) . ')$#';
|
|
|
|
|
|
|
|
// Generate list of libraries fingerprint files
|
|
|
|
$libFiles = $this->params->get('libfiles');
|
|
|
|
$this->libFiles = array_map('trim', explode(',', $libFiles));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collect php files to check (excluding external library directories)
|
|
|
|
*
|
|
|
|
* @param string $path The path of the folder to read.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @since 3.0
|
|
|
|
*/
|
|
|
|
protected function files($path)
|
|
|
|
{
|
|
|
|
$path = JPath::clean($path);
|
|
|
|
$arr = array();
|
|
|
|
|
|
|
|
// Read the source directory
|
|
|
|
if ($handle = @opendir($path))
|
|
|
|
{
|
|
|
|
while (($file = readdir($handle)) !== false)
|
|
|
|
{
|
|
|
|
// Skip excluded directories
|
|
|
|
if ($file !== '.' && $file !== '..' && !preg_match($this->regexExcludeFolders, $file))
|
|
|
|
{
|
|
|
|
$fullpath = $path . '/' . $file;
|
|
|
|
|
|
|
|
if (is_dir($fullpath))
|
|
|
|
{
|
|
|
|
// Detect and skip external library directories
|
|
|
|
foreach ($this->libFiles as $libFile)
|
|
|
|
{
|
|
|
|
if (is_file($fullpath . '/' . $libFile))
|
|
|
|
{
|
|
|
|
// Skip processing of this directory
|
|
|
|
continue 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$arr = array_merge($arr, $this->files($fullpath));
|
|
|
|
}
|
|
|
|
elseif (preg_match('/\.php$/', $file))
|
|
|
|
{
|
|
|
|
$arr[] = $fullpath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir($handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arr;
|
2013-11-05 20:17:39 +00:00
|
|
|
}
|
2012-06-18 11:26:00 +00:00
|
|
|
}
|