function unblockPHP($file) { $f = new JD_File($file); $blockString = '<?php /* ATTENTION!! DEFENDER GENERATED CODE. DO NOT DELETE!! */ return; ?>'; // Try to read file $contents = $f->read(); if ($contents === false) { return JText::_('Cannot unblock file') . ': ' . $f->getError(); } $toWrite = str_replace($blockString, '', $contents); // Try to write if (!$f->write($toWrite)) { return JText::_('Cannot unblock file') . ': ' . $f->getError(); } return true; }
function fix() { if (empty($this->_fixes)) { return 0; } list($files, $ids) = $this->_getFilesAndIds($this->_fixes); $config =& $this->getConfig(); $filePerms = intval($config->get('permission_max_file_permission', '644'), 8); $dirPerms = intval($config->get('permission_max_dir_permission', '755'), 8); $chmoddedFiles = array(); $rollback = false; @clearstatcache(); foreach ($files as $file) { if (!JFile::exists($file) && !JFolder::exists($file)) { continue; } $info = new stdClass(); $info->perms = fileperms($file); $info->file = $file; $isFile = is_file($file); $isDir = is_dir($file); if ($isFile || $isDir) { $f = new JD_File($file); $perms = $isFile ? $filePerms : $dirPerms; if ($f->chmod($perms) === false) { $this->setError($f->getError()); $rollback = true; break; } } $chmoddedFiles[] = $info; } if ($rollback) { foreach ($chmoddedFiles as $info) { $f = new JD_File($info->file); $f->chmod($info->perms); } $this->_fixes = array(); return false; } parent::setLogStatus($ids, 'fixed'); parent::refreshFilesystemTable($files); $this->_fixes = array(); return count($chmoddedFiles); }
/** * Reverts file to it's initial state. */ function fix() { if (empty($this->_fixes)) { return 0; } list($files, $ids) = $this->_getFilesAndIds($this->_fixes); $count = 0; $fixedFiles = array(); $fixedIds = array(); // now, fix the files. foreach ($files as $id => $file) { if (JFile::exists($file)) { $row =& JTable::getInstance('Filesystem', 'Table'); if (!$row->loadByFilename($file)) { $this->setError(JText::_('Cannot load filesystem data') . ': ' . $file); continue; } $f = new JD_File($file); if (!$f->write($row->contents)) { $this->setError($f->getError()); continue; } if ($row->permission) { if (!$f->chmod($row->permission)) { $this->setError($f->getError()); continue; } } $fixedFiles[] = $file; if (strpos($id, ':')) { $realId = explode(':', $id); $realId = $realId[1]; $fixedIds[] = (int) $realId; } $count++; } } parent::setLogStatus($fixedIds, 'reverted'); parent::refreshFilesystemTable($fixedFiles); // Empty array $this->_fixes = array(); return $count; }
function fix() { if (empty($this->_fixes)) { return 0; } list($files, $ids) = $this->_getFilesAndIds($this->_fixes); $newFiles = array(); $rollback = false; foreach ($files as $dir) { if (!is_dir($dir)) { continue; } $indexFile = JPath::clean($dir . DS . 'index.html'); if (JFile::exists($indexFile)) { continue; } $fixFile = new JD_File($indexFile); // Try to write the file if (!$fixFile->write($this->indexHTMLContents)) { $this->setError($fixFile->getError()); $rollback = true; break; } $newFiles[] = $indexFile; } if ($rollback) { foreach ($newFiles as $file) { if (JFile::exists($file)) { JFile::delete($file); } } $this->_fixes = array(); return false; } parent::refreshFilesystemTable($newFiles); parent::setLogStatus($ids, 'fixed'); $this->_fixes = array(); return count($newFiles); }
/** * Fix the missing _JEXEC issue * Note that processed items are removed from buffer * (non-PHPdoc) * @see components/com_jdefender/lib/actions/JD_Action#fix() */ function fix() { if (empty($this->_fixes)) { return 0; } list($files, $ids) = $this->_getFilesAndIds($this->_fixes); $count = 0; $fixedFiles = array(); $fixedIds = array(); // now, fix the files. $error = false; foreach ($files as $k => $file) { if (JFile::exists($file)) { $dir = dirname($file); $f = new JD_File($file); $contents = $f->read(); if (!$f->write($this->fixString . $contents)) { $error = true; $this->setError(JText::_('Cannot write to file') . ': ' . $f->getError()); continue; } $fixedFiles[] = $file; if (strpos($k, ':')) { $id = explode(':', $k); $id = $id[1]; $fixedIds[] = $id; } $count++; } } parent::setLogStatus($fixedIds, 'fixed'); parent::refreshFilesystemTable($fixedFiles); // Empty array $this->_fixes = array(); return $error ? false : $count; }
/** * Load properties from a given file properties * * @param $file string The filename to scan * @param $contents boolean Load the contents * @param $loadId boolean Load id from database * @return boolean result */ function loadFromFile($file, $contents = false, $loadId = false) { if (!JFile::exists($file) && !JFolder::exists($file . DS)) { return false; } $info = @stat($file); $this->scandate = $this->_db->getNullDate(); $this->filename = basename($file); $this->fullpath = $file; $this->permission = fileperms($file) & 0777; $this->size = filesize($file); $ctime =& JFactory::getDate($info['ctime']); $mtime =& JFactory::getDate($info['mtime']); $this->ctime = $ctime->toMySQL(); $this->mtime = $mtime->toMySQL(); $this->uid = $info['uid']; $this->gid = $info['gid']; $this->type = ''; if (is_file($file)) { $this->type = 'file'; $this->hash_md = md5_file($file); if ($contents) { $f = new JD_File($file); $this->contents = $f->read(); } } elseif (is_dir($file)) { $this->type = 'dir'; } if ($loadId) { $this->_db->setQuery('SELECT id FROM #__jdefender_filesystem WHERE fullpath = ' . $this->fullpath . ' LIMIT 1'); $this->id = $this->_db->loadResult(); } return true; }
/** * Check, whether we can chmod the path * @param string $path * @return boolean */ function canChmod($path = false) { if ($path == false) { $path = $this->_filename; } $perms = @fileperms($path); if ($perms !== false) { $f = new JD_File($path); if ($f->chmod($path, $perms ^ 01)) { $f->chmod($path, $perms); return true; } } return false; }
function scanFilesAndDirs($baseDir, $theFiles = false, $theDirs = false) { $session =& JFactory::getSession(); $doLog = $session->get('doLog', false, 'jdefender'); $baseDir = JPath::clean($baseDir); // Remove the trailing slash if (in_array(substr($baseDir, -1), array('/', '\\'))) { $baseDir = substr($baseDir, 0, -1); } $files = array(); $dirs = array(); if ($theFiles !== false) { $files = $theFiles; } else { $files = JFolder::files($baseDir, '.', true, true, array_keys($this->filter->excludedDirs)); } if ($theDirs !== false) { $dirs = $theDirs; } else { $dirs = $this->_listFolders($baseDir, $this->filter->getExcludedDirPregexp(), true, true, array_keys($this->filter->excludedDirs)); } foreach ($files as $file) { $contents = null; if (!$this->filter->isFileOK($file, true)) { continue; } if ($this->isReadFiles()) { $f = new JD_File($file); $contents = $f->read($file); if (false === $contents) { $contents = null; } } $this->trigger('onFile', array($file, &$contents)); $this->_filesScanned++; } if ($doLog) { JD_Vars_Helper::setVar('files', 'jdefender_scan', $this->_filesScanned); } foreach ($dirs as $dir) { if (!$this->filter->isDirOK($dir)) { continue; } $this->trigger('onDir', array(&$dir)); $this->_foldersScanned++; } if ($doLog) { JD_Vars_Helper::setVar('dirs', 'jdefender_scan', $this->_foldersScanned); } }