+
+ /**
+ * @brief Returns the system user that is executing the script
+ *
+ * This mostly returns something like "www-data".
+ *
+ * @return string system username
+ */
+ static function systemuser() {
+ if (!function_exists('posix_getpwuid') OR !function_exists('posix_geteuid')) {
+ return '';
+ }
+
+ $processUser = posix_getpwuid(posix_geteuid());
+ return $processUser['name'];
+ }
+
+ /**
+ * @brief Checks if a given directory is usable for the system
+ *
+ * @return boolean the directory is usable
+ */
+ static function directory_usable($directory) {
+
+ if ($directory == '') {
+ logger("Directory is empty. This shouldn't happen.", LOGGER_DEBUG);
+ return false;
+ }
+
+ if (!file_exists($directory)) {
+ logger('Path "'.$directory.'" does not exist for user '.self::systemuser(), LOGGER_DEBUG);
+ return false;
+ }
+ if (is_file($directory)) {
+ logger('Path "'.$directory.'" is a file for user '.self::systemuser(), LOGGER_DEBUG);
+ return false;
+ }
+ if (!is_dir($directory)) {
+ logger('Path "'.$directory.'" is not a directory for user '.self::systemuser(), LOGGER_DEBUG);
+ return false;
+ }
+ if (!is_writable($directory)) {
+ logger('Path "'.$directory.'" is not writable for user '.self::systemuser(), LOGGER_DEBUG);
+ return false;
+ }
+ return true;
+ }