Overview
Comment:Utilisation de safe_unlink/safe_mkdir partout
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: 85d5a50a945511222fdf17a1630179b6efcc6a31
User & Date: bohwaz on 2018-09-13 22:21:52
Other Links: branch diff | manifest | tags
Context
2018-09-22
09:59
Utilisation de safe_unlink/safe_mkdir partout check-in: 933b330272 user: bohwaz tags: trunk, stable
2018-09-19
13:54
Correction filtrage liste plugins check-in: 50e72bff3f user: bohwaz tags: dev
2018-09-13
22:21
Utilisation de safe_unlink/safe_mkdir partout check-in: 85d5a50a94 user: bohwaz tags: dev
22:06
Générer une erreur quand même quand la suppression échoue et que le fichier existe toujours check-in: 1bdf70a9d5 user: bohwaz tags: dev
Changes

Modified src/include/lib/Garradin/Install.php from [ba81d4bd4a] to [683f6f27e6].

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
	static public function checkAndCreateDirectories()
	{
		// Vérifier que les répertoires vides existent, sinon les créer
		$paths = [DATA_ROOT, PLUGINS_ROOT, CACHE_ROOT, CACHE_ROOT . '/static', CACHE_ROOT . '/compiled'];

		foreach ($paths as $path)
		{
		    if (!file_exists($path))
		    {
		        @mkdir($path, 0777, true);
		    }

		    if (!is_dir($path))
		    {
		    	throw new UserException('Le répertoire '.$path.' n\'existe pas ou n\'est pas un répertoire.');
		    }

		    // On en profite pour vérifier qu'on peut y lire et écrire







<
<
|
<







145
146
147
148
149
150
151


152

153
154
155
156
157
158
159
	static public function checkAndCreateDirectories()
	{
		// Vérifier que les répertoires vides existent, sinon les créer
		$paths = [DATA_ROOT, PLUGINS_ROOT, CACHE_ROOT, CACHE_ROOT . '/static', CACHE_ROOT . '/compiled'];

		foreach ($paths as $path)
		{


			Utils::safe_mkdir($path);


		    if (!is_dir($path))
		    {
		    	throw new UserException('Le répertoire '.$path.' n\'existe pas ou n\'est pas un répertoire.');
		    }

		    // On en profite pour vérifier qu'on peut y lire et écrire

Modified src/include/lib/Garradin/Plugin.php from [0e12346d98] to [e947eb781e].

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
		catch (\Exception $e)
		{
			throw new UserException('Le téléchargement du plugin '.$id.' a échoué : ' . $e->getMessage());
		}

		if (!self::checkHash($id))
		{
			unlink(PLUGINS_ROOT . '/' . $id . '.tar.gz');
			throw new \RuntimeException('L\'archive du plugin '.$id.' est corrompue (le hash SHA1 ne correspond pas).');
		}

		self::install($id, true);

		return true;
	}







|







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
		catch (\Exception $e)
		{
			throw new UserException('Le téléchargement du plugin '.$id.' a échoué : ' . $e->getMessage());
		}

		if (!self::checkHash($id))
		{
			Utils::safe_unlink(PLUGINS_ROOT . '/' . $id . '.tar.gz');
			throw new \RuntimeException('L\'archive du plugin '.$id.' est corrompue (le hash SHA1 ne correspond pas).');
		}

		self::install($id, true);

		return true;
	}

Modified src/include/lib/Garradin/Sauvegarde.php from [ba865c4dc1] to [3200010089].

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	{
		if (preg_match('!\.\.+!', $file) || !preg_match('!^[\w\d._-]+\.sqlite$!i', $file) 
			|| $file == basename(DB_FILE))
		{
			throw new UserException('Nom de fichier non valide.');
		}

		return unlink(DATA_ROOT . '/' . $file);
	}

	/**
	 * Renvoie sur la sortie courante le contenu du fichier de base de données courant
	 * @return boolean true
	 */
	public function dump()







|







142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	{
		if (preg_match('!\.\.+!', $file) || !preg_match('!^[\w\d._-]+\.sqlite$!i', $file) 
			|| $file == basename(DB_FILE))
		{
			throw new UserException('Nom de fichier non valide.');
		}

		return Utils::safe_unlink(DATA_ROOT . '/' . $file);
	}

	/**
	 * Renvoie sur la sortie courante le contenu du fichier de base de données courant
	 * @return boolean true
	 */
	public function dump()
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
			}
		}

		$r = $this->restoreDB($file['tmp_name'], $user_id);

		if ($r)
		{
			unlink($file['tmp_name']);
		}

		return $r;
	}

	/**
	 * Vérifie l'intégrité d'une sauvegarde Garradin







|







227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
			}
		}

		$r = $this->restoreDB($file['tmp_name'], $user_id);

		if ($r)
		{
			Utils::safe_unlink($file['tmp_name']);
		}

		return $r;
	}

	/**
	 * Vérifie l'intégrité d'une sauvegarde Garradin

Modified src/include/lib/Garradin/Squelette.php from [a6a2b8cd25] to [fc1698f7ac].

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896

897

898
899
900
901
902
903
904

    static private function compile_store($tpl, $content)
    {
        $path = self::compile_get_path($tpl);

        if (!file_exists(dirname($path)))
        {
            mkdir(dirname($path), 0777, true);
        }

        file_put_contents($path, $content);
        return true;
    }

    static public function compile_clear($tpl)
    {
        $path = self::compile_get_path($tpl);

        if (file_exists($path))

            unlink($path);


        return true;
    }

    protected function getVariable($var)
    {
        if (isset($this->current[$var]))







|











>
|
>







878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906

    static private function compile_store($tpl, $content)
    {
        $path = self::compile_get_path($tpl);

        if (!file_exists(dirname($path)))
        {
            Utils::safe_mkdir(dirname($path), 0777, true);
        }

        file_put_contents($path, $content);
        return true;
    }

    static public function compile_clear($tpl)
    {
        $path = self::compile_get_path($tpl);

        if (file_exists($path))
        {
            Utils::safe_unlink($path);
        }

        return true;
    }

    protected function getVariable($var)
    {
        if (isset($this->current[$var]))
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
    static public function resetSource($template)
    {
        if (!preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!i', $template))
            return false;

        if (file_exists(DATA_ROOT . '/www/squelettes/' . $template))
        {
            return unlink(DATA_ROOT . '/www/squelettes/' . $template);
        }

        return false;
    }

    static public function listSources()
    {
        if (!file_exists(DATA_ROOT . '/www/squelettes'))
        {
            mkdir(DATA_ROOT . '/www/squelettes', 0775, true);
        }

        $sources = [];

        $dir = dir(ROOT . '/www/squelettes-dist');

        while ($file = $dir->read())







|









|







953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
    static public function resetSource($template)
    {
        if (!preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!i', $template))
            return false;

        if (file_exists(DATA_ROOT . '/www/squelettes/' . $template))
        {
            return Utils::safe_unlink(DATA_ROOT . '/www/squelettes/' . $template);
        }

        return false;
    }

    static public function listSources()
    {
        if (!file_exists(DATA_ROOT . '/www/squelettes'))
        {
            Utils::safe_mkdir(DATA_ROOT . '/www/squelettes', 0775, true);
        }

        $sources = [];

        $dir = dir(ROOT . '/www/squelettes-dist');

        while ($file = $dir->read())

Modified src/include/lib/Garradin/Static_Cache.php from [453588f5aa] to [1abb2553b4].

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

	protected static function _getCacheDir()
	{
		$dir = CACHE_ROOT . '/static';

		if (!file_exists($dir))
		{
			mkdir(CACHE_ROOT . '/static', 0777, true);
		}

		return CACHE_ROOT . '/static';
	}

	protected static function _getCachePath($id)
	{







|







9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

	protected static function _getCacheDir()
	{
		$dir = CACHE_ROOT . '/static';

		if (!file_exists($dir))
		{
			Utils::safe_mkdir(CACHE_ROOT . '/static', 0777, true);
		}

		return CACHE_ROOT . '/static';
	}

	protected static function _getCachePath($id)
	{
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	{
		return file_exists(self::_getCachePath($id));
	}

	static public function remove($id)
	{
		$path = self::_getCachePath($id);
		return @unlink($path);
	}

	static public function clean($expire = self::CLEAN_EXPIRE)
	{
		$dir = self::_getCacheDir();
		$d = dir($dir);

		$expire = time() - $expire;

		while ($file = $d->read())
		{
			if ($file[0] == '.')
			{
				continue;
			}

			if (filemtime($dir . '/' . $file) > $expire)
			{
				unlink($dir . '/' . $file);
			}
		}

		$d->close();

		return true;
	}
}







|


















|








82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
	{
		return file_exists(self::_getCachePath($id));
	}

	static public function remove($id)
	{
		$path = self::_getCachePath($id);
		return Utils::safe_unlink($path);
	}

	static public function clean($expire = self::CLEAN_EXPIRE)
	{
		$dir = self::_getCacheDir();
		$d = dir($dir);

		$expire = time() - $expire;

		while ($file = $d->read())
		{
			if ($file[0] == '.')
			{
				continue;
			}

			if (filemtime($dir . '/' . $file) > $expire)
			{
				Utils::safe_unlink($dir . '/' . $file);
			}
		}

		$d->close();

		return true;
	}
}

Modified src/include/lib/Garradin/Template.php from [f146e5dca4] to [bc26b664b5].

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

	public function __construct()
	{
		parent::__construct();

		if (!file_exists(CACHE_ROOT . '/compiled'))
		{
			mkdir(CACHE_ROOT . '/compiled', 0777, true);
		}

		$this->setTemplatesDir(ROOT . '/templates');
		$this->setCompiledDir(CACHE_ROOT . '/compiled');

		// Hash de la version pour les éléments statiques (cache)
		// On ne peut pas utiliser la version directement comme query string







|







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

	public function __construct()
	{
		parent::__construct();

		if (!file_exists(CACHE_ROOT . '/compiled'))
		{
			Utils::safe_mkdir(CACHE_ROOT . '/compiled', 0777, true);
		}

		$this->setTemplatesDir(ROOT . '/templates');
		$this->setCompiledDir(CACHE_ROOT . '/compiled');

		// Hash de la version pour les éléments statiques (cache)
		// On ne peut pas utiliser la version directement comme query string

Modified src/include/lib/Garradin/Utils.php from [d74b84d7b0] to [c76bd4d270].

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409






















410
411
412
413
414
415
416
        $path = CACHE_ROOT . '/' . $path;
        $dir = dir($path);

        while ($file = $dir->read())
        {
            if ($file[0] != '.')
            {
            	if (file_exists($path . DIRECTORY_SEPARATOR . $file))
            	{
                	if (!@unlink($path . DIRECTORY_SEPARATOR . $file))
                    {
                        if (file_exists($path . DIRECTORY_SEPARATOR . $file))
                        {
                            throw new \RuntimeException(sprintf('Impossible de supprimer le fichier %s: %s', $path . DIRECTORY_SEPARATOR . $file, error_get_last()));
                        }
                    }
                }
            }
        }

        $dir->close();
        return true;
    }























    static public function suggestPassword()
    {
        return Security::getRandomPassphrase(ROOT . '/include/data/dictionary.fr');
    }

    static public function checkIBAN($value)







<
<
|
<
<
<
<
<
<
<






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







387
388
389
390
391
392
393


394







395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
        $path = CACHE_ROOT . '/' . $path;
        $dir = dir($path);

        while ($file = $dir->read())
        {
            if ($file[0] != '.')
            {


                self::safe_unlink($path . DIRECTORY_SEPARATOR . $file);







            }
        }

        $dir->close();
        return true;
    }

    static public function safe_unlink($path)
    {
        if (!@unlink($path))
        {
            return true;
        }
        
        if (!file_exists($path))
        {
            return true;
        }

        throw new \RuntimeException(sprintf('Impossible de supprimer le fichier %s: %s', $path, error_get_last()));

        return true;
    }

    static public function safe_mkdir($path, $mode = 0777, $recursive = false)
    {
        return @mkdir($path, $mode, $recursive) || is_dir($path);
    }

    static public function suggestPassword()
    {
        return Security::getRandomPassphrase(ROOT . '/include/data/dictionary.fr');
    }

    static public function checkIBAN($value)
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
            if (is_dir($path . '/' . $file))
            {
                if (!self::deleteRecursive($path . '/' . $file))
                    return false;
            }
            else
            {
                unlink($path . '/' . $file);
            }
        }

        $dir->close();
        rmdir($path);

        return true;







|







588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
            if (is_dir($path . '/' . $file))
            {
                if (!self::deleteRecursive($path . '/' . $file))
                    return false;
            }
            else
            {
                utils::safe_unlink($path . '/' . $file);
            }
        }

        $dir->close();
        rmdir($path);

        return true;