1<?php
2
3/**
4 * This file is part of the Froxlor project.
5 * Copyright (c) 2003-2009 the SysCP Team (see authors).
6 * Copyright (c) 2010 the Froxlor Team (see authors).
7 *
8 * For the full copyright and license information, please view the COPYING
9 * file that was distributed with this source code. You can also view the
10 * COPYING file online at http://files.froxlor.org/misc/COPYING.txt
11 *
12 * @copyright  (c) the authors
13 * @author     Florian Lippert <flo@syscp.org> (2003-2009)
14 * @author     Froxlor team <team@froxlor.org> (2010-)
15 * @license    GPLv2 http://files.froxlor.org/misc/COPYING.txt
16 * @package    Functions
17 *
18 */
19
20/**
21 * Creates a directory below a users homedir and sets all directories,
22 * which had to be created below with correct Owner/Group
23 * (Copied from cron_tasks.php:rev1189 as we'll need this more often in future)
24 *
25 * @param  string The homedir of the user
26 * @param  string The dir which should be created
27 * @param  int    The uid of the user
28 * @param  int    The gid of the user
29 * @param  bool   Place standard-index.html into the new folder
30 * @param  bool   Allow creating a directory out of the customers docroot
31 *
32 * @return bool   true if everything went okay, false if something went wrong
33 *
34 * @author Florian Lippert <flo@syscp.org>
35 * @author Martin Burchert <martin.burchert@syscp.org>
36 */
37
38function mkDirWithCorrectOwnership($homeDir, $dirToCreate, $uid, $gid, $placeindex = false, $allow_notwithinhomedir = false)
39{
40	$returncode = true;
41
42	if($homeDir != ''
43	   && $dirToCreate != '')
44	{
45		$homeDir = makeCorrectDir($homeDir);
46		$dirToCreate = makeCorrectDir($dirToCreate);
47
48		if(substr($dirToCreate, 0, strlen($homeDir)) == $homeDir)
49		{
50			$subdir = substr($dirToCreate, strlen($homeDir) - 1);
51			$within_homedir = true;
52		}
53		else
54		{
55			$subdir = $dirToCreate;
56			$within_homedir = false;
57		}
58
59		$subdir = makeCorrectDir($subdir);
60		$subdirs = array();
61
62		if($within_homedir || !$allow_notwithinhomedir)
63		{
64			$subdirlen = strlen($subdir);
65			$offset = 0;
66
67			while($offset < $subdirlen)
68			{
69				$offset = strpos($subdir, '/', $offset);
70				$subdirelem = substr($subdir, 0, $offset);
71				$offset++;
72				array_push($subdirs, makeCorrectDir($homeDir . $subdirelem));
73			}
74		}
75		else
76		{
77			array_push($subdirs, $dirToCreate);
78		}
79
80		$subdirs = array_unique($subdirs);
81		sort($subdirs);
82		foreach($subdirs as $sdir)
83		{
84			if(!is_dir($sdir))
85			{
86				$sdir = makeCorrectDir($sdir);
87				safe_exec('mkdir -p ' . escapeshellarg($sdir));
88
89				/**
90				 * #68
91				 */
92				if ($placeindex) {
93					$loginname = getLoginNameByUid($uid);
94					if ($loginname !== false) {
95						storeDefaultIndex($loginname, $sdir, null);
96					}
97				}
98
99				safe_exec('chown -R ' . (int)$uid . ':' . (int)$gid . ' ' . escapeshellarg($sdir));
100			}
101		}
102	}
103	else
104	{
105		$returncode = false;
106	}
107
108	return $returncode;
109}
110