Exemplo n.º 1
0
function file_reset_count()
{
    extract(doSlash(gpsa(array('id', 'filename', 'category', 'description'))));
    if ($id) {
        $id = assert_int($id);
        if (safe_update('txp_file', 'downloads = 0', "id = {$id}")) {
            file_edit(gTxt('reset_file_count_success'), $id);
        }
    } else {
        file_list(gTxt('reset_file_count_failure'));
    }
}
Exemplo n.º 2
0
function file_replace()
{
    global $txp_user, $file_base_path;
    $id = assert_int(gps('id'));
    $rs = safe_row('filename, author', 'txp_file', "id = {$id}");
    if (!$rs) {
        file_list(array(messenger(gTxt('invalid_id'), $id), E_ERROR));
        return;
    }
    extract($rs);
    $filename = sanitizeForFile($filename);
    if (!has_privs('file.edit') && !($author === $txp_user && has_privs('file.edit.own'))) {
        require_privs();
    }
    $file = file_get_uploaded();
    $name = file_get_uploaded_name();
    if ($file === false) {
        // Could not get uploaded file.
        file_list(array(gTxt('file_upload_failed') . " {$name} " . upload_get_errormsg($_FILES['thefile']['error']), E_ERROR));
        return;
    }
    if (!$filename) {
        file_list(array(gTxt('invalid_filename'), E_ERROR));
    } else {
        $newpath = build_file_path($file_base_path, $filename);
        if (is_file($newpath)) {
            rename($newpath, $newpath . '.tmp');
        }
        if (!shift_uploaded_file($file, $newpath)) {
            safe_delete("txp_file", "id = {$id}");
            file_list(array($newpath . sp . gTxt('upload_dir_perms'), E_ERROR));
            // Rename tmp back.
            rename($newpath . '.tmp', $newpath);
            // Remove tmp upload.
            unlink($file);
        } else {
            file_set_perm($newpath);
            update_lastmod('file_replaced', compact('id', 'filename'));
            if ($size = filesize($newpath)) {
                safe_update('txp_file', 'size = ' . $size . ', modified = now()', 'id = ' . $id);
            }
            file_edit(gTxt('file_uploaded', array('{name}' => $name)), $id);
            // Clean up old.
            if (is_file($newpath . '.tmp')) {
                unlink($newpath . '.tmp');
            }
        }
    }
}
Exemplo n.º 3
0
function file_save()
{
    global $file_base_path, $txp_user;
    extract(doSlash(gpsa(array('id', 'filename', 'category', 'description', 'status', 'publish_now', 'year', 'month', 'day', 'hour', 'minute', 'second'))));
    $id = assert_int($id);
    $permissions = gps('perms');
    if (is_array($permissions)) {
        asort($permissions);
        $permissions = implode(",", $permissions);
    }
    $perms = doSlash($permissions);
    $rs = safe_row('filename, author', 'txp_file', "id={$id}");
    if (!has_privs('file.edit') && !($rs['author'] == $txp_user && has_privs('file.edit.own'))) {
        file_edit(gTxt('restricted_area'));
        return;
    }
    $old_filename = $rs['filename'];
    if ($old_filename != false && strcmp($old_filename, $filename) != 0) {
        $old_path = build_file_path($file_base_path, $old_filename);
        $new_path = build_file_path($file_base_path, $filename);
        if (file_exists($old_path) && shift_uploaded_file($old_path, $new_path) === false) {
            $message = gTxt('file_cannot_rename', array('{name}' => $filename));
            return file_list($message);
        } else {
            file_set_perm($new_path);
        }
    }
    $created_ts = @safe_strtotime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second);
    if ($publish_now) {
        $created = 'now()';
    } elseif ($created_ts > 0) {
        $created = "from_unixtime('" . $created_ts . "')";
    } else {
        $created = '';
    }
    $size = filesize(build_file_path($file_base_path, $filename));
    $rs = safe_update('txp_file', "\n\t\t\tfilename = '{$filename}',\n\t\t\tcategory = '{$category}',\n\t\t\tpermissions = '{$perms}',\n\t\t\tdescription = '{$description}',\n\t\t\tstatus = '{$status}',\n\t\t\tsize = '{$size}',\n\t\t\tmodified = now(),\n\t\t\tauthor = '{$txp_user}'" . ($created ? ", created = {$created}" : ''), "id = {$id}");
    if (!$rs) {
        // update failed, rollback name
        if (shift_uploaded_file($new_path, $old_path) === false) {
            $message = gTxt('file_unsynchronized', array('{name}' => $filename));
            return file_list($message);
        } else {
            $message = gTxt('file_not_updated', array('{name}' => $filename));
            return file_list($message);
        }
    }
    $message = gTxt('file_updated', array('{name}' => $filename));
    file_list($message);
}
Exemplo n.º 4
0
function file_save()
{
    global $file_base_path, $txp_user;
    $varray = array_map('assert_string', gpsa(array('id', 'category', 'title', 'description', 'status', 'publish_now', 'year', 'month', 'day', 'hour', 'minute', 'second')));
    extract(doSlash($varray));
    $filename = $varray['filename'] = sanitizeForFile(gps('filename'));
    if ($filename == '') {
        $message = gTxt('file_not_updated', array('{name}' => $filename));
        return file_list($message);
    }
    $id = $varray['id'] = assert_int($id);
    $permissions = gps('perms');
    if (is_array($permissions)) {
        asort($permissions);
        $permissions = implode(",", $permissions);
    }
    $varray['permissions'] = $permissions;
    $perms = doSlash($permissions);
    $rs = safe_row('filename, author', 'txp_file', "id={$id}");
    if (!has_privs('file.edit') && !($rs['author'] == $txp_user && has_privs('file.edit.own'))) {
        file_edit(gTxt('restricted_area'));
        return;
    }
    $old_filename = $varray['old_filename'] = sanitizeForFile($rs['filename']);
    if ($old_filename != false && strcmp($old_filename, $filename) != 0) {
        $old_path = build_file_path($file_base_path, $old_filename);
        $new_path = build_file_path($file_base_path, $filename);
        if (file_exists($old_path) && shift_uploaded_file($old_path, $new_path) === false) {
            $message = gTxt('file_cannot_rename', array('{name}' => $filename));
            return file_list($message);
        } else {
            file_set_perm($new_path);
        }
    }
    $created_ts = @safe_strtotime($year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minute . ':' . $second);
    if ($publish_now) {
        $created = 'now()';
    } elseif ($created_ts > 0) {
        $created = "from_unixtime('" . $created_ts . "')";
    } else {
        $created = '';
    }
    $size = filesize(build_file_path($file_base_path, $filename));
    $constraints = array('category' => new CategoryConstraint(gps('category'), array('type' => 'file')), 'status' => new ChoiceConstraint(gps('status'), array('choices' => array(STATUS_HIDDEN, STATUS_PENDING, STATUS_LIVE), 'message' => 'invalid_status')));
    callback_event_ref('file_ui', 'validate_save', 0, $varray, $constraints);
    $validator = new Validator($constraints);
    $rs = $validator->validate() && safe_update('txp_file', "\n\t\t\tfilename = '" . doSlash($filename) . "',\n\t\t\ttitle = '{$title}',\n\t\t\tcategory = '{$category}',\n\t\t\tpermissions = '{$perms}',\n\t\t\tdescription = '{$description}',\n\t\t\tstatus = '{$status}',\n\t\t\tsize = '{$size}',\n\t\t\tmodified = now()" . ($created ? ", created = {$created}" : ''), "id = {$id}");
    if (!$rs) {
        // update failed, rollback name
        if (isset($old_path) && shift_uploaded_file($new_path, $old_path) === false) {
            $message = gTxt('file_unsynchronized', array('{name}' => $filename));
            return file_list($message);
        } else {
            $message = gTxt('file_not_updated', array('{name}' => $filename));
            return file_list($message);
        }
    }
    update_lastmod();
    $message = gTxt('file_updated', array('{name}' => $filename));
    file_list($message);
}