Exemplo n.º 1
0
 /**
  * Do the job.
  * Throw exceptions on errors (the job will be retried).
  */
 public function execute()
 {
     global $CFG;
     $tmpdir = $CFG->tempdir;
     // Default to last weeks time.
     $time = time() - $CFG->tempdatafoldercleanup * 3600;
     $dir = new \RecursiveDirectoryIterator($tmpdir);
     // Show all child nodes prior to their parent.
     $iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);
     // An array of the full path (key) and date last modified.
     $modifieddateobject = array();
     // Get the time modified for each directory node. Nodes will be updated
     // once a file is deleted, so we need a list of the original values.
     for ($iter->rewind(); $iter->valid(); $iter->next()) {
         $node = $iter->getRealPath();
         if (!is_readable($node)) {
             continue;
         }
         $modifieddateobject[$node] = $iter->getMTime();
     }
     // Now loop through again and remove old files and directories.
     for ($iter->rewind(); $iter->valid(); $iter->next()) {
         $node = $iter->getRealPath();
         if (!is_readable($node)) {
             continue;
         }
         // Check if file or directory is older than the given time.
         if ($modifieddateobject[$node] < $time) {
             if ($iter->isDir() && !$iter->isDot()) {
                 // Don't attempt to delete the directory if it isn't empty.
                 if (!glob($node . DIRECTORY_SEPARATOR . '*')) {
                     if (@rmdir($node) === false) {
                         mtrace("Failed removing directory '{$node}'.");
                     }
                 }
             }
             if ($iter->isFile()) {
                 if (@unlink($node) === false) {
                     mtrace("Failed removing file '{$node}'.");
                 }
             }
         } else {
             // Return the time modified to the original date only for real files.
             if ($iter->isDir() && !$iter->isDot()) {
                 touch($node, $modifieddateobject[$node]);
             }
         }
     }
 }
 /**
  * Do the job.
  * Throw exceptions on errors (the job will be retried).
  */
 public function execute()
 {
     global $CFG;
     $tmpdir = $CFG->tempdir;
     // Default to last weeks time.
     $time = strtotime('-1 week');
     $dir = new \RecursiveDirectoryIterator($tmpdir);
     // Show all child nodes prior to their parent.
     $iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);
     for ($iter->rewind(); $iter->valid(); $iter->next()) {
         $node = $iter->getRealPath();
         if (!is_readable($node)) {
             continue;
         }
         // Check if file or directory is older than the given time.
         if ($iter->getMTime() < $time) {
             if ($iter->isDir() && !$iter->isDot()) {
                 // Don't attempt to delete the directory if it isn't empty.
                 if (!glob($node . DIRECTORY_SEPARATOR . '*')) {
                     if (@rmdir($node) === false) {
                         mtrace("Failed removing directory '{$node}'.");
                     }
                 }
             }
             if ($iter->isFile()) {
                 if (@unlink($node) === false) {
                     mtrace("Failed removing file '{$node}'.");
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Executes the transformation process.
  *
  * @throws Zend_Console_Getopt_Exception
  *
  * @return void
  */
 public function execute()
 {
     $results = array();
     $longest_name = 0;
     /** @var RecursiveDirectoryIterator $files */
     $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(dirname(__FILE__) . '/../'));
     while ($files->valid()) {
         // skip abstract files
         if (!$files->isFile() || $files->getBasename() == 'Abstract.php') {
             $files->next();
             continue;
         }
         // convert the filename to a class
         $class_name = 'DocBlox_Task_' . str_replace(DIRECTORY_SEPARATOR, '_', $files->getSubPath()) . '_' . $files->getBasename('.php');
         // check if the class exists, if so: add it to the list
         if (class_exists($class_name)) {
             $name = $files->getBasename('.php');
             $longest_name = max(strlen($name), $longest_name);
             $results[strtolower($files->getSubPath())][strtolower($name)] = $files->getRealPath();
         }
         $files->next();
     }
     // echo the list of namespaces with their tasks
     ksort($results, SORT_STRING);
     foreach ($results as $namespace => $tasks) {
         echo $namespace . PHP_EOL;
         asort($tasks, SORT_STRING);
         foreach ($tasks as $task => $filename) {
             // get the short description by reflecting the file.
             $refl = new DocBlox_Reflection_File($filename, false);
             $refl->setLogLevel(DocBlox_Core_Log::QUIET);
             $refl->process();
             /** @var DocBlox_Reflection_Class $class */
             $class = current($refl->getClasses());
             echo ' :' . str_pad($task, $longest_name + 2) . $class->getDocBlock()->getShortDescription() . PHP_EOL;
         }
     }
     echo PHP_EOL;
 }
Exemplo n.º 4
0
/**
 * Delete files and directories older than one week from directory provided by $CFG->tempdir.
 *
 * @exception Exception Failed reading/accessing file or directory
 * @return bool True on successful file and directory deletion; otherwise, false on failure
 */
function cron_delete_from_temp()
{
    global $CFG;
    $tmpdir = $CFG->tempdir;
    // Default to last weeks time.
    $time = strtotime('-1 week');
    try {
        $dir = new RecursiveDirectoryIterator($tmpdir);
        // Show all child nodes prior to their parent.
        $iter = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
        for ($iter->rewind(); $iter->valid(); $iter->next()) {
            $node = $iter->getRealPath();
            if (!is_readable($node)) {
                continue;
            }
            // Check if file or directory is older than the given time.
            if ($iter->getMTime() < $time) {
                if ($iter->isDir() && !$iter->isDot()) {
                    if (@rmdir($node) === false) {
                        mtrace("Failed removing directory '{$node}'.");
                    }
                }
                if ($iter->isFile()) {
                    if (@unlink($node) === false) {
                        mtrace("Failed removing file '{$node}'.");
                    }
                }
            }
        }
    } catch (Exception $e) {
        mtrace('Failed reading/accessing file or directory.');
        return false;
    }
    return true;
}
    /**
     * Test that the file_temp_cleanup_task removes directories and
     * files as expected.
     */
    public function test_file_temp_cleanup_task() {
        global $CFG;

        // Create directories.
        $dir = $CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR . 'courses';
        mkdir($dir, 0777, true);

        // Create files to be checked and then deleted.
        $file01 = $dir . DIRECTORY_SEPARATOR . 'sections.xml';
        file_put_contents($file01, 'test data 001');
        $file02 = $dir . DIRECTORY_SEPARATOR . 'modules.xml';
        file_put_contents($file02, 'test data 002');
        // Change the time modified for the first file, to a time that will be deleted by the task (greater than seven days).
        touch($file01, time() - (8 * 24 * 3600));

        $task = \core\task\manager::get_scheduled_task('\\core\\task\\file_temp_cleanup_task');
        $this->assertInstanceOf('\core\task\file_temp_cleanup_task', $task);
        $task->execute();

        // Scan the directory. Only modules.xml should be left.
        $filesarray = scandir($dir);
        $this->assertEquals('modules.xml', $filesarray[2]);
        $this->assertEquals(3, count($filesarray));

        // Change the time modified on modules.xml.
        touch($file02, time() - (8 * 24 * 3600));
        // Change the time modified on the courses directory.
        touch($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01' . DIRECTORY_SEPARATOR .
                'courses', time() - (8 * 24 * 3600));
        // Run the scheduled task to remove the file and directory.
        $task->execute();
        $filesarray = scandir($CFG->tempdir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'backup01');
        // There should only be two items in the array, '.' and '..'.
        $this->assertEquals(2, count($filesarray));

        // Change the time modified on all of the files and directories.
        $dir = new \RecursiveDirectoryIterator($CFG->tempdir);
        // Show all child nodes prior to their parent.
        $iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST);

        for ($iter->rewind(); $iter->valid(); $iter->next()) {
            $node = $iter->getRealPath();
            touch($node, time() - (8 * 24 * 3600));
        }

        // Run the scheduled task again to remove all of the files and directories.
        $task->execute();
        $filesarray = scandir($CFG->tempdir);
        // All of the files and directories should be deleted.
        // There should only be two items in the array, '.' and '..'.
        $this->assertEquals(2, count($filesarray));
    }
Exemplo n.º 6
0
 /**
  * Test removing files and directories from tempdir.
  *
  * @dataProvider cron_delete_from_temp_provider
  * @param array $nodes List of files and directories
  * @param array $expected The expected results
  */
 public function test_cron_delete_from_temp($nodes, $expected)
 {
     global $CFG;
     $tmpdir = $CFG->tempdir;
     foreach ($nodes as $data) {
         if ($data->isdir) {
             mkdir($tmpdir . $data->path, $CFG->directorypermissions, true);
         }
     }
     // We need to iterate through again since adding a file to a directory will
     // update the modified time of the directory.
     foreach ($nodes as $data) {
         touch($tmpdir . $data->path, time() + $data->time);
     }
     cron_delete_from_temp();
     $dir = new RecursiveDirectoryIterator($tmpdir);
     $iter = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
     $actual = array();
     for ($iter->rewind(); $iter->valid(); $iter->next()) {
         if (!$iter->isDot()) {
             $actual[] = $iter->getRealPath();
         }
     }
     // Sort results to guarantee actual order.
     sort($actual);
     $this->assertEquals($expected, $actual);
 }
Exemplo n.º 7
0
 public function build_files()
 {
     $id = 0;
     $partial_stop = 0;
     $maxSize = 0;
     $indexStep = 0;
     $maxInserts = 0;
     $files = array();
     if (isset($_POST['partialStop'])) {
         $partial_stop = (int) $_POST['partialStop'];
     }
     //Max Filesize check
     if (isset($_POST['maxSize'])) {
         $maxSize = (int) $_POST['maxSize'];
     }
     if (!$maxSize) {
         $maxSize = 5242880;
         //Default value.
     }
     //IndexStep check
     if (isset($_POST['indexStep'])) {
         $indexStep = (int) $_POST['indexStep'];
     }
     if (!$indexStep) {
         $indexStep = 3000;
     }
     //MaxInserts check
     if (isset($_POST['maxInserts'])) {
         $maxInserts = (int) $_POST['maxInserts'];
     }
     if (!$maxInserts) {
         $maxInserts = 300;
     }
     chdir(JPATH_ROOT);
     //If we don't have a partial stop count, then this is a new request.
     //We should therefore clear the database.
     if (!$partial_stop) {
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $db->setQuery("DELETE FROM " . $db->quoteName('#__jhackguard_scan_files'));
         $db->query();
     }
     $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./'));
     /* And off we go into the loop */
     $sql_delimiter = 0;
     $total_count = 0;
     $stopped = 0;
     while ($it->valid()) {
         //We do no need ., .., or directories. Only files.
         if (!$it->isDot() and !$it->isDir()) {
             if ($it->getSize() > 0 and $it->getSize() < $maxSize) {
                 //We also do not need empty files or files bigger than 5MB.
                 $total_count++;
                 if ($partial_stop > 0 and $partial_stop > $total_count) {
                     $it->next();
                     continue;
                     //We don't want these items. We indexed them the last run.
                 }
                 $files[] = $it->getRealPath();
                 $sql_delimiter++;
                 if ($sql_delimiter > $maxInserts) {
                     //Perform insert.
                     $db = JFactory::getDbo();
                     $query = $db->getQuery(true);
                     $query->insert($db->quoteName('#__jhackguard_scan_files'));
                     $query->columns('fname');
                     foreach ($files as $path) {
                         $query->values($db->quote($path));
                     }
                     $db->setQuery($query);
                     $db->query();
                     //Reset sql_delimiter
                     $sql_delimiter = 0;
                     //Reset files array
                     $files = array();
                 }
                 if ($total_count == $partial_stop + $indexStep) {
                     $stopped = 1;
                     break;
                     //We have reached the 3k limit per run.
                 }
             }
         }
         $it->next();
     }
     //Did we miss to import the last batch of the files?
     if (count($files) > 0) {
         //Yup..
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->insert($db->quoteName('#__jhackguard_scan_files'));
         $query->columns('fname');
         foreach ($files as $path) {
             $query->values($db->quote($path));
         }
         $db->setQuery($query);
         $db->query();
         $files = array();
     }
     if ($stopped) {
         $partial_stop = $partial_stop + $indexStep;
         echo json_encode(array("success" => false, "partialStop" => $partial_stop, "partialRun" => true));
     } else {
         //Seems like we finished successfully. WOOHOO!
         //And the total count is...
         $db = JFactory::getDbo();
         $query = $db->getQuery(true);
         $query->select("COUNT(*) as total");
         $query->from($db->quoteName('#__jhackguard_scan_files'));
         $db->setQuery($query);
         $list = $db->loadColumn();
         echo json_encode(array("success" => true, "count" => $list[0]));
     }
 }
Exemplo n.º 8
0
$infected = array();
$scanned = 0;
$hits = 0;
/* And off we go into the loop */
while ($it->valid()) {
    try {
        if (!$it->isDot() and !$it->isDir()) {
            // First check the MD5 sum of the file.
            // Matched files will not be opened for reading to save time.
            // Only scan files bigger than 0 bytes and less than 2MB
            $fmd5 = md5_file($it->key());
            // Check if AppFocus has been defined and process the file first.
            if ($config->app_focused_run) {
                if (!$config->afo->hash_match($it->key(), $fmd5)) {
                    $hits++;
                    $infected[$it->getRealPath()] = array('explain' => '[modified_core_file]', 'score' => 100);
                    $it->next();
                    continue;
                }
            }
            if ($it->getSize() > 0 and $it->getSize() < 2048576) {
                if (in_array($fmd5, $false_positives)) {
                    $it->next();
                    continue;
                }
                if (in_array($fmd5, $md5s)) {
                    //md5 hit
                    $hits++;
                    $infected[$it->getRealPath()] = array('explain' => '[md5sum_match]', 'score' => 100);
                } else {
                    $s = new FileScanner();