예제 #1
0
파일: lxlib.php 프로젝트: zseand/kloxo
function lfile_put_contents($file, $data, $flag = null)
{
    $file = expand_real_root($file);
    if (is_soft_or_hardlink($file)) {
        log_log("link_error", "{$file} is hard or symlink. Not writing\n");
        return;
    }
    if (char_search_a($data, "__path_")) {
        dprint("<font color=red>Warning : Trying to write __path into a file {$file}: </font> {$data} <br> \n", 3);
    }
    lxfile_mkdir(dirname($file));
    if (file_exists($file)) {
        if (is_readable($file)) {
            if (is_writable($file)) {
                return file_put_contents($file, $data, $flag);
            } else {
                $error_msg = 'Could not write the file \'' . $file . '\' with permissions: ' . substr(sprintf('%o', fileperms($file)), -4);
                dprint($error_msg);
                log_log('filesys', $error_msg);
                return false;
            }
        } else {
            $error_msg = 'Could not read the file \'' . $file . '\' with permissions: ' . substr(sprintf('%o', fileperms($file)), -4);
            dprint($error_msg);
            log_log('filesys', $error_msg);
            return false;
        }
    } else {
        if (file_put_contents($file, $data, $flag) === false) {
            $error_msg = 'File \'' . $file . '\' could not be created.';
            dprint($error_msg);
            log_log('filesys', $error_msg);
            return false;
        }
        return true;
    }
}
예제 #2
0
/**
 * Writes a string to a file
 * 
 * @param string $username  file owner
 * @param string $file path to the file
 * @param mixed $data the data to write
 * @param int $flag
 */
function lxuser_put_contents($username, $file, $data, $flag = 0)
{
    $file = expand_real_root($file);
    dprint("Debug: filename is: " . $file . "\n");
    if (is_soft_or_hardlink($file)) {
        log_log("link_error", "{$file} is hard or symlink. Not writing\n");
        return false;
    }
    if (!lxuser_mkdir($username, dirname($file))) {
        return false;
    }
    if ($flag == FILE_APPEND) {
        $mode = 'a';
    } else {
        $mode = 'w';
    }
    $f = fopen($file, $mode);
    if (!$f) {
        return false;
    } else {
        if (flock($f, LOCK_EX)) {
            $bytes = fwrite($f, $data);
            lxfile_generic_chown($file, $username);
            flock($f, LOCK_UN);
            fclose($f);
            return $bytes;
        } else {
            return false;
        }
    }
}
예제 #3
0
function lfile_put_contents($file, $data, $flag = null)
{
    $file = expand_real_root($file);
    if (is_soft_or_hardlink($file)) {
        log_log("link_error", "{$file} is hard or symlink. Not writing\n");
        return;
    }
    if (char_search_a($data, "__path_")) {
        dprint("<font color=red>Warning : Trying to write __path into a file {$file}: </font> {$data} <br> \n", 3);
    }
    lxfile_mkdir(dirname($file));
    if (file_exists($file)) {
        if (is_readable($file)) {
            if (is_writable($file)) {
                $result = file_put_contents($file, $data, $flag);
                chown($file, 'lxlabs');
                return $result;
            } else {
                $posix_data = posix_getpwuid(fileowner($file));
                $error_msg = 'Could not write the file \'' . $file . '\' with permissions: ' . substr(sprintf('%o', fileperms($file)), -4) . ' UID: ' . $posix_data['name'] . ':' . $posix_data['uid'] . ' GID: ' . $posix_data['gecos'] . ':' . $posix_data['gid'] . (PHP_SAPI !== 'cgi-fcgi' ? PHP_EOL : '<br />');
                dprint($error_msg);
                //log_log('filesys', $error_msg);
                return false;
            }
        } else {
            $error_msg = 'Could not read the file \'' . $file . '\' with permissions: ' . substr(sprintf('%o', fileperms($file)), -4) . (PHP_SAPI !== 'cgi-fcgi' ? PHP_EOL : '<br />');
            dprint($error_msg);
            //log_log('filesys', $error_msg);
            return false;
        }
    } else {
        $result = file_put_contents($file, $data, $flag);
        chown($file, 'lxlabs');
        if ($result === false) {
            $error_msg = 'File \'' . $file . '\' could not be created.';
            dprint($error_msg);
            //log_log('filesys', $error_msg);
            return false;
        }
        return true;
    }
}