1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
<?php
/**
***********************************************************************************************
* Class manages the AdmMyFiles folder
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class MyFiles
* This class supports the assignment of rights to every folder of adm_my_files
* It's easy to create new folders and get detailed error messages if there are
* problems with folder rights
*
* Beside the methods of the parent class there are the following additional methods:
*
* checkSettings() - method checks if adm_my_files folder has all necessary rights
* getServerPath() - returns the current path
* setSubFolder($folder) - open a folder in the current module folder
* if that folder doesn't exists than it will be created
*/
class MyFiles extends Folder
{
protected $module; // name of the module and name of the folder in adm_my_files
protected $modulePath; // absolute path of the module
protected $currentPath; // absolute path that is set with setSubFolder
protected $webPath; // the path starts with adm_my_file
public $errorText;
public $errorPath;
/**
* module name should be the folder name in adm_my_files for this module
* Example: 'PHOTOS', 'BACKUP', 'DOWNLOAD'
* @param string $module
*/
public function __construct($module)
{
global $g_root_path, $gCurrentOrganization;
if($module === 'DOWNLOAD')
{
$folderName = TableFolder::getRootFolderName();
}
else
{
$folderName = strtolower($module);
}
$this->module = $module;
$this->modulePath = SERVER_PATH.'/adm_my_files/'.$folderName;
$this->currentPath = SERVER_PATH.'/adm_my_files/'.$folderName;
$this->webPath = $g_root_path.'/adm_my_files';
parent::__construct($this->modulePath);
}
/**
* method checks if adm_my_files folder has all necessary rights
* the method is designed to make as little as possible checks
*
* [1] (!@mkdir($dirPath, 0777) && !is_dir($dirPath))
* This issue is difficult to reproduce, as any of concurrency-related issues. Appears when several
* processes attempting to create a directory which is not yet existing, but between is_dir() and mkdir()
* calls another process already managed to create a directory.
* @return bool if false than check the parameters $errorText, $errorPath
*/
public function checkSettings()
{
if(!is_writable($this->modulePath))
{
if(!is_dir($this->modulePath))
{
$serverPathAdmMyFiles = SERVER_PATH.'/adm_my_files';
if(!is_writable($serverPathAdmMyFiles))
{
if(!is_dir($serverPathAdmMyFiles))
{
// create folder "adm_my_files"
if(!@mkdir($serverPathAdmMyFiles, 0777) && !is_dir($serverPathAdmMyFiles)) // [1]
{
$this->errorText = 'SYS_FOLDER_NOT_CREATED';
$this->errorPath = $this->webPath;
return false;
}
}
if(!is_writable($serverPathAdmMyFiles))
{
// set "adm_my_files" writable
if(!@chmod($serverPathAdmMyFiles, 0777))
{
$this->errorText = 'SYS_FOLDER_WRITE_ACCESS';
$this->errorPath = $this->webPath;
return false;
}
}
}
// create module folder
if(!@mkdir($this->modulePath, 0777) && !is_dir($this->modulePath)) // [1]
{
$this->errorText = 'SYS_FOLDER_NOT_CREATED';
$this->errorPath = $this->webPath;
return false;
}
// create ".htaccess" file for folder "adm_my_files"
if (!is_file($serverPathAdmMyFiles.'/.htaccess'))
{
$protection = new Htaccess($serverPathAdmMyFiles);
if (!$protection->protectFolder())
{
return false;
}
}
}
if(!is_writable($this->modulePath))
{
// set module folder writable
if(!@chmod($this->folderWithPath, 0777))
{
$this->errorText = 'SYS_FOLDER_WRITE_ACCESS';
$this->errorPath = $this->webPath;
return false;
}
}
}
$this->setFolder($this->modulePath);
return true;
}
/**
* @return string returns the current path
*/
public function getServerPath()
{
return $this->currentPath;
}
/**
* open a folder in the current module folder
* if that folder doesn't exists than it will be created
*
* [1] (!@mkdir($dirPath, 0777) && !is_dir($dirPath))
* This issue is difficult to reproduce, as any of concurrency-related issues. Appears when several
* processes attempting to create a directory which is not yet existing, but between is_dir() and mkdir()
* calls another process already managed to create a directory.
* @param string $folder subfolder name
* @return bool Returns true if folder is successfully created and writable.
*/
public function setSubFolder($folder)
{
if(!admStrIsValidFileName($folder))
{
return false;
}
$tempServerPath = $this->modulePath.'/'.$folder;
$tempWebPath = $this->webPath.'/'.$folder;
// create folder
if (!is_dir($tempServerPath))
{
if (!@mkdir($tempServerPath, 0777) && !is_dir($tempServerPath)) // [1]
{
$this->errorText = 'SYS_FOLDER_NOT_CREATED';
$this->errorPath = $tempWebPath;
return false;
}
}
// set folder writable
if (!is_writable($tempServerPath))
{
if (!@chmod($tempServerPath, 0777))
{
$this->errorText = 'SYS_FOLDER_WRITE_ACCESS';
$this->errorPath = $tempWebPath;
return false;
}
}
$this->currentPath = $tempServerPath;
$this->webPath = $tempWebPath;
return true;
}
}