Example #1
0
 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;
 }
 /**
  * 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);
     $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);
 }
 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;
 }
Example #6
0
 /**
  * entodo: add FTP write
  * Write data to file, trying to change permissions if needed
  * @param $data
  * @return unknown_type
  */
 function write($data = '')
 {
     $dir = dirname($this->_filename);
     $dirCreated = false;
     // Create the destination directory if it doesn't exist
     if (!file_exists($dir)) {
         if (!JFolder::create($dir)) {
             $this->setError(JText::_('Cannot create directory') . ': ' . $dir);
             return false;
         }
         $dirCreated = true;
     }
     @clearstatcache();
     $fileExists = $this->exists();
     $fileChmodded = false;
     $filePerms = 0644;
     $dirPerms = fileperms($dir) & 0777;
     $chmodder = $this->_getChmodderInfo();
     if ($fileExists) {
         if (is_writable($this->_filename)) {
             return @file_put_contents($this->_filename, $data);
         }
         $fileInfo = $this->getFileInfo();
         if (!$fileInfo) {
             return false;
         }
         $filePerms = $fileInfo['mode'] & 0777;
         $ownerInfo = $this->_getFileOwnerInfo($fileInfo);
         // Defined the mask
         $mask = false;
         if ($ownerInfo['user'] == $chmodder['user']) {
             $mask = 0200;
         } elseif ($ownerInfo['user'] != $chmodder['user'] && $ownerInfo['group'] == $chmodder['group']) {
             $mask = 020;
         } else {
             $mask = 02;
         }
         if (!($filePerms & $mask)) {
             if (false === $this->chmod($filePerms | $mask)) {
                 return false;
             }
             $fileChmodded = true;
         }
     }
     $failed = false;
     $dirPermsChanged = false;
     $theDir = new JD_File($dir);
     if (!$fileExists) {
         if (false === JFile::write($this->_filename, $data)) {
             if (false === @file_put_contents($this->_filename, $data)) {
                 if (false === $theDir->chmod($dirPerms | 0222)) {
                     $this->setError($theDir->getError());
                     return false;
                 }
             }
         }
     }
     $result = JFile::write($this->_filename, $data);
     if ($fileChmodded) {
         $this->chmod($filePerms);
     }
     if (!$dirCreated) {
         $theDir->chmod($dirPerms);
     }
     return $result;
 }