Detects if the given folder contains an unpacked moodle2 backup

@param string $tempdir the name of the backup directory
@return boolean true if moodle2 format detected, false otherwise
505189
public static function detect_moodle2_format($tempdir) {
        $dirpath = make_backup_temp_directory($tempdir, false);
        if (!is_dir($dirpath)) {
            throw new convert_helper_exception('tmp_backup_directory_not_found', $dirpath);
        }

        $filepath = $dirpath . '/moodle_backup.xml';
        if (!file_exists($filepath)) {
            return false;
        }

        $handle     = fopen($filepath, 'r');
        $firstchars = fread($handle, 200);
        $status     = fclose($handle);

        if (strpos($firstchars,'<?xml version="1.0" encoding="UTF-8"?>') !== false and
            strpos($firstchars,'<moodle_backup>') !== false and
            strpos($firstchars,'<information>') !== false) {
                return true;
        }

        return false;
    }
476402
public static function detect_format($tempdir) {
        $filepath = make_backup_temp_directory($tempdir, false);
        if (!is_dir($filepath)) {
            throw new convert_helper_exception('tmp_backup_directory_not_found', $filepath);
        }
        $manifest = cc112moodle::get_manifest($filepath);
        if (file_exists($manifest)) {
            // Looks promising, lets load some information.
            $handle = fopen($manifest, 'r');
            $xml_snippet = fread($handle, 1024);
            fclose($handle);

            // Check if it has the required strings.

            $xml_snippet = strtolower($xml_snippet);
            $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet);
            $xml_snippet = str_replace("'", '', $xml_snippet);
            $xml_snippet = str_replace('"', '', $xml_snippet);

            $search_string = "xmlns=http://www.imsglobal.org/xsd/imsccv1p1/imscp_v1p1";
            if (strpos($xml_snippet, $search_string) !== false) {
                return backup::FORMAT_IMSCC11;
            }
        }

        return null;
    }
187577
protected static function tempDirectory()
    {
        $tempDir = eZSys::varDirectory() . '/webdav/tmp';
        if ( !file_exists( $tempDir ) )
        {
            eZDir::mkdir( $tempDir, eZDir::directoryPermission(), true );
        }
        return $tempDir;
    }
1228705
public function setTempDir($dir)
    {
        $dir = $this->addTrailingSlash($dir);

        if (!is_dir($dir)) {
            $this->log->addDebug(sprintf('Creating new temporary directory "%s"', $dir));

            if (!mkdir($dir, 0755, true)) {
                $this->log->addCritical(sprintf('Could not create temporary directory "%s"', $dir));

                return false;
            }
        }

        $this->tempDir = $dir;

        return true;
    }
1094025
public static boolean verifyDir(File dir,Logger logger){
		if (dir==null){
			logger.error("El directorio es nulo.");
			return false;
		}
		String fileName=dir.getAbsolutePath();
		if (fileName==null){
			return false;
		}
		
		if (!dir.exists()){
			logger.error("El path '"+fileName+"' no existe.");
			return false;
		}

		if (!dir.isDirectory()){
			logger.error("El path '"+fileName+"' no es un directorio.");
			return false;
		}
		
		if (!dir.canRead()){
			logger.error("No tenemos permisos de lectura en el path '"+fileName+"'.");
			return false;
		}
		
		if (!dir.canWrite()){
			logger.error("No tenemos permisos de escritura en el path '"+fileName+"'.");
			return false;
		}

		return true;				
	}
536490
public function rmdir($dir)
    {
        return ($this->isConnected() === true) ? ftp_rmdir($this->getConnection(), $dir) : false;
    }
163351
protected function createTmpDir() {

		$temp_file = tempnam( sys_get_temp_dir(), '' );
		@unlink( $temp_file ); // @codingStandardsIgnoreLine
		mkdir( $temp_file );
		if ( ! is_dir( $temp_file ) ) {
			return '';

		}

		return untrailingslashit( $temp_file );
	}
1050490
public static function isDirCS($sDir)
    {
        if (array_key_exists($sDir, static::$aIsDirCache)) {
            return static::$aIsDirCache[$sDir];
        }

        $aDirBits = explode(DIRECTORY_SEPARATOR, $sDir);
        $bResult  = true;
        while (count($aDirBits) > 1) {
            $sDirectory     = array_pop($aDirBits);
            $sDirectoryPath = implode(DIRECTORY_SEPARATOR, $aDirBits);
            $aDirectories   = array_map(function ($sDirectory) {
                return basename($sDirectory);
            }, glob($sDirectoryPath . DIRECTORY_SEPARATOR . '*', GLOB_NOSORT | GLOB_ONLYDIR));

            if (!in_array($sDirectory, $aDirectories)) {
                $bResult = false;
                break;
            }
        }

        static::$aIsDirCache[$sDir] = $bResult;
        return $bResult;
    }
830922
function rmdir($dir)
    {
        if (!($this->bitmap & NET_SSH2_MASK_LOGIN)) {
            return false;
        }

        $dir = $this->_realpath($dir);
        if ($dir === false) {
            return false;
        }

        if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($dir), $dir))) {
            return false;
        }

        $response = $this->_get_sftp_packet();
        if ($this->packet_type != NET_SFTP_STATUS) {
            user_error('Expected SSH_FXP_STATUS', E_USER_NOTICE);
            return false;
        }

        extract(unpack('Nstatus', $this->_string_shift($response, 4)));
        if ($status != NET_SFTP_STATUS_OK) {
            // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED?
            $this->_logError($response, $status);
            return false;
        }

        $this->_remove_dir($dir);

        return true;
    }
1032695
public function checkDirectory(): bool
    {
        if ($this->directory == null) {
            throw new LoggerException('Log directory is not defined yet');
        }

        self::$directoryChecked = self::$directoryChecked ?: is_dir($this->directory);
        if (!self::$directoryChecked) {
            self::$directoryChecked = (bool) @mkdir($this->directory, 0644, true);
            if (self::$directoryChecked === false) {
                throw new LoggerException(sprintf('Cannot make directory, error[%s]',
                    error_get_last()['message'] ?? 'Unknown'));
            }
        }

        return self::$directoryChecked;
    }
609107
public function hasDir($path)
    {
        $meta = $this->getMetadata($path);
        return ($meta && isset($meta['hasdir'])) ? $meta : [
            'hasdir' => true
        ];
    }
37838
private void initTempDirectory(String tempDirRoot, boolean createTempSubDir) {

		tempDirPath = tempDirRoot;

		// In windows, pathnames with spaces are returned as %20
		if (tempDirPath.contains("%20"))
			tempDirPath = tempDirPath.replaceAll("%20", " ");

		this.textDirPath = tempDirPath + File.separator + TEMP_TEXT_SUBDIR;
		this.gzipDirPath = tempDirPath + File.separator + TEMP_GZIP_SUBDIR;
		this.cssClasspathDirPath = tempDirPath + File.separator + TEMP_CSS_CLASSPATH_SUBDIR;

		if (createTempSubDir) {
			try {
				createDir(tempDirPath);
				createDir(textDirPath);
				createDir(gzipDirPath);
				createDir(cssClasspathDirPath);
			} catch (IOException e) {
				throw new BundlingProcessException("Unexpected IOException creating temporary jawr directory", e);
			}
		}
	}
742581
public static function checkTemporaryFolder() {
		
		if (!file_exists(static::TEMP_FOLDER) or !is_dir(static::TEMP_FOLDER)) {
			
			// remove any file named as wanted temporary folder
			@unlink(static::TEMP_FOLDER);

			// create the folder
			$old = umask(0);
			if (!mkdir(static::TEMP_FOLDER, 0777, TRUE)) {
				trigger_error('Directory creation on ' . static::TEMP_FOLDER . ' failed');
				$ret = FALSE;
			}
			umask($old);
			
			// sets full permissions
			if (!chmod(static::TEMP_FOLDER, 0777)) {
				trigger_error('Set permissions on directory ' . static::TEMP_FOLDER . ' failed');
				$ret = FALSE;
			}
			
		}
		
	}
757298
public function isExtension($directory)
    {
        $returnValue = (bool) false;

        
        $hasStructure = $this->findStructureManifest($directory) !== false;
        $hasPHPManifest = false;
        
        $files = scandir($this->options['input']);
        if ($files !== false) {
        	foreach ($files as $f) {
				if (is_file($this->options['input'] . '/' .$f) && is_readable($this->options['input'] . '/' . $f)) {
					if ($f == 'manifest.php') {
						$hasPHPManifest = true;
					}
				}
        	}
        }
        
        $returnValue = $hasStructure || $hasPHPManifest;
        

        return (bool) $returnValue;
    }
720394
public function main($tempDir = null)
    {
        if (empty($tempDir)) {
            $tempDir = Configure::read('Attachments.tmpUploadsPath');
        }

        if (!Folder::isAbsolute($tempDir)) {
            $this->out('The path must be absolute, "' . $tempDir . '" given.');
            exit;
        }

        $Folder = new Folder();
        if ($Folder->cd($tempDir) === false) {
            $this->out('Path "' . $tempDir . '" doesn\'t seem to exist.');
            exit;
        }

        $dir = new Folder($tempDir);
        $folders = $dir->read();
        $files = $dir->findRecursive();
        $deletedFiles = 0;
        $deletedFolders = 0;

        $this->out('Found ' . count($folders[0]) . ' folders and ' . count($files) . ' files');

        foreach ($files as $filePath) {
            $file = new File($filePath);
            // only delete if last change is longer than 24 hours ago
            if ($file->lastChange() < (time() - 24 * 60 * 60) && $file->delete()) {
                $deletedFiles++;
            }
            $file->close();
        }

        foreach ($folders[0] as $folderName) {
            $folder = new Folder($dir->pwd() . DS . $folderName);
            // only delete if folder is empty
            if ($folder->dirsize() === 0 && $folder->delete()) {
                $deletedFolders++;
            }
        }


        $this->out('Deleted ' . $deletedFolders . ' folders and ' . $deletedFiles . ' files.');
    }
942466
public static function detect_format($tempdir) {
        $filepath = make_backup_temp_directory($tempdir, false);
        if (!is_dir($filepath)) {
            throw new convert_helper_exception('tmp_backup_directory_not_found', $filepath);
        }
        $manifest = cc2moodle::get_manifest($filepath);
        if (!empty($manifest)) {
            // Looks promising, lets load some information.
            $handle = fopen($manifest, 'r');
            $xml_snippet = fread($handle, 1024);
            fclose($handle);

            // Check if it has the required strings.

            $xml_snippet = strtolower($xml_snippet);
            $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet);
            $xml_snippet = str_replace("'", '', $xml_snippet);
            $xml_snippet = str_replace('"', '', $xml_snippet);

            $search_string = "xmlns=http://www.imsglobal.org/xsd/imscc/imscp_v1p1";
            if (strpos($xml_snippet, $search_string) !== false) {
                return backup::FORMAT_IMSCC1;
            }
        }

        return null;
    }
330903
public static function sys_get_temp_dir()
    {
    // Moodle hack!
    if (function_exists('make_temp_directory')) {
        $temp = make_temp_directory('phpexcel');
        return realpath(dirname($temp));
    }

        if (self::$useUploadTempDirectory) {
            //  use upload-directory when defined to allow running on environments having very restricted
            //      open_basedir configs
            if (ini_get('upload_tmp_dir') !== false) {
                if ($temp = ini_get('upload_tmp_dir')) {
                    if (file_exists($temp)) {
                        return realpath($temp);
                    }
                }
            }
        }

        // sys_get_temp_dir is only available since PHP 5.2.1
        // http://php.net/manual/en/function.sys-get-temp-dir.php#94119
        if (!function_exists('sys_get_temp_dir')) {
            if ($temp = getenv('TMP')) {
                if ((!empty($temp)) && (file_exists($temp))) {
                    return realpath($temp);
                }
            }
            if ($temp = getenv('TEMP')) {
                if ((!empty($temp)) && (file_exists($temp))) {
                    return realpath($temp);
                }
            }
            if ($temp = getenv('TMPDIR')) {
                if ((!empty($temp)) && (file_exists($temp))) {
                    return realpath($temp);
                }
            }

            // trick for creating a file in system's temporary dir
            // without knowing the path of the system's temporary dir
            $temp = tempnam(__FILE__, '');
            if (file_exists($temp)) {
                unlink($temp);
                return realpath(dirname($temp));
            }

            return null;
        }

        // use ordinary built-in PHP function
        //    There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
        //        be called if we're running 5.2.1 or earlier
        return realpath(sys_get_temp_dir());
    }