Пример #1
0
/**
 * Save a new or edited file to database
 */
function dt_save_file($edit = 0)
{
    global $xoopsSecurity, $functions, $db;
    foreach ($_POST as $k => $v) {
        ${$k} = $v;
    }
    if (!$xoopsSecurity->check()) {
        $functions->dt_send_message(__('Session token not valid!', 'dtransport'), 1, 0);
    }
    //Verificamos si el software es válido
    if ($item <= 0) {
        $functions->dt_send_message(__('Item download ID not provided!', 'dtransport'), 1, 1);
    }
    //Verificamos si existe el software
    $sw = new DTSoftware($item);
    if ($sw->isNew()) {
        $functions->dt_send_message(__('Specified item download does nto exists!', 'dtransport'), 1, 1);
    }
    if ($edit) {
        //Verificamos si archivo es válido
        if ($id <= 0) {
            $functions->dt_send_message(__('File ID has not been provided!', 'dtransport'), 1, 1);
        }
        //Verificamos si existe archivo
        $fl = new DTFile($id);
        if ($fl->isNew()) {
            $functions->dt_send_message(__('Specified file does not exists!', 'dtransport'), 1, 1);
        }
        // Si es un archivo remoto eliminamos el archivo actual
        if (!$fl->remote() && $remote) {
            $rmu = RMUtilities::get();
            $mc = $rmu->module_config('dtransport');
            $dir = $sw->getVar('secure') ? $mc['directory_secure'] : $mc['directory_insecure'];
            if (file_exists($dir . '/' . $fl->file())) {
                unlink($dir . '/' . $fl->file());
            }
            unset($dir, $mc, $rmu);
        }
    } else {
        $fl = new DTFile();
    }
    $fl->setSoftware($item);
    $fl->setRemote($remote);
    $fl->setFile($file);
    $fl->setDefault($default);
    $fl->setGroup($group);
    $fl->setDate(time());
    $fl->setTitle(trim($title));
    $fl->setMime($mime);
    $fl->setSize($size);
    if (!$fl->save()) {
        $functions->dt_send_message(__('File could not be saved!', 'dtransport') . '<br />' . $fl->errors(), 1, 1);
    } else {
        if ($fl->isDefault()) {
            $db->queryF("UPDATE " . $db->prefix("dtrans_files") . " SET `default`=0 WHERE id_soft=" . $sw->id() . " AND id_file !=" . $fl->id());
        }
        $ret = array('message' => __('File saved successfully!', 'dtransport'));
        showMessage(sprintf(__('File %s saved successfully!', 'dtransport'), $fl->title()), RMMSG_SAVED);
        $functions->dt_send_message($ret, 0, 1);
    }
}