Example #1
0
/**
 * Polish the $info global from create_table_info.  adds some
 * heuristic defaults.
 */
function enhance_info()
{
    global $info, $config;
    $title_field = field_get_title_field($info);
    for ($i = 0; $i < count($info); ++$i) {
        // per default text fields are searchable:
        switch (Horde_String::lower($info[$i]['type'])) {
            // String types
            case 'string':
            case 'char':
            case 'varchar':
            case 'blob':
            case 'tinyblob':
            case 'tinytext':
            case 'mediumblob':
            case 'mediumtext':
            case 'longblob':
            case 'longtext':
                $info[$i]['search'] = 1;
                break;
            default:
                $info[$i]['search'] = 0;
        }
        // per default all non blob fields are displayed in list view
        if (is_blob($info[$i])) {
            $info[$i]['list'] = 0;
        } else {
            $info[$i]['list'] = 2;
        }
        // per default all fields are editable, except the primary_key
        // and timestamp fields
        $pk = field_get_primary_key();
        if ($info[$i]['name'] == $pk['name'] || Horde_String::lower($info[$i]['type']) == 'timestamp') {
            $info[$i]['edit'] = 0;
        } else {
            $info[$i]['edit'] = 1;
        }
        $info[$i]['view'] = 1;
        // view everything
        // Field description (displayed to user). Defaults to name.
        // Please note that underscores here result in hotkeys.
        $info[$i]['desc'] = Horde_String::ucwords($info[$i]['name']);
        // Set the flag for the title field.
        if ($info[$i]['name'] == $title_field['name']) {
            $info[$i]['flags'] .= ' title';
        }
    }
}
Example #2
0
function post(&$vars)
{
    extract($vars);
    global $request;
    $modelvar = classify($request->resource);
    trigger_before('insert_from_post', ${$modelvar}, $request);
    $table = $request->resource;
    $content_type = 'text/html';
    $rec = ${$modelvar}->base();
    if (!${$modelvar}->can_create($table)) {
        trigger_error("Sorry, you do not have permission to " . $request->action . " " . $table, E_USER_ERROR);
    }
    $fields = ${$modelvar}->fields_from_request($request);
    $fieldlist = $fields[$table];
    foreach ($fieldlist as $field => $type) {
        if (${$modelvar}->has_metadata && is_blob($table . '.' . $field)) {
            if (isset($_FILES[strtolower(classify($table))]['name'][$field])) {
                $content_type = type_of($_FILES[strtolower(classify($table))]['name'][$field]);
            }
        }
        $rec->set_value($field, $request->params[strtolower(classify($table))][$field]);
    }
    $rec->set_value('profile_id', get_profile_id());
    $result = $rec->save_changes();
    if (!$result) {
        trigger_error("The record could not be saved into the database.", E_USER_ERROR);
    }
    $atomentry = ${$modelvar}->set_metadata($rec, $content_type, $table, 'id');
    ${$modelvar}->set_categories($rec, $request, $atomentry);
    if (is_upload($table, 'attachment')) {
        $upload_types = environment('upload_types');
        if (!$upload_types) {
            $upload_types = array('jpg', 'jpeg', 'png', 'gif');
        }
        $ext = extension_for(type_of($_FILES[strtolower(classify($table))]['name']['attachment']));
        if (!in_array($ext, $upload_types)) {
            trigger_error('Sorry, this site only allows the following file types: ' . implode(',', $upload_types), E_USER_ERROR);
        }
        $url = $request->url_for(array('resource' => $table, 'id' => $rec->id));
        $title = substr($rec->title, 0, 140);
        $over = strlen($title) + strlen($url) + 1 - 140;
        if ($over > 0) {
            $rec->set_value('title', substr($title, 0, -$over) . " " . $url);
        } else {
            $rec->set_value('title', $title . " " . $url);
        }
        $rec->save_changes();
        $tmp = $_FILES[strtolower(classify($table))]['tmp_name']['attachment'];
        if (is_jpg($tmp)) {
            $thumbsize = environment('max_pixels');
            $Thumbnail =& $db->model('Thumbnail');
            $t = $Thumbnail->base();
            $newthumb = tempnam("/tmp", "new" . $rec->id . ".jpg");
            resize_jpeg($tmp, $newthumb, $thumbsize);
            $t->set_value('target_id', $atomentry->id);
            $t->save_changes();
            update_uploadsfile('thumbnails', $t->id, $newthumb);
            $t->set_etag();
        }
    }
    trigger_after('insert_from_post', ${$modelvar}, $rec);
    header_status('201 Created');
    redirect_to($request->base);
}
Example #3
0
 function update_from_post(&$req)
 {
     trigger_before('update_from_post', $this, $req);
     global $db;
     $fields = $this->fields_from_request($req);
     if (isset($fields[$req->resource])) {
         $fieldsarr = $fields[$req->resource];
     }
     if (!isset($fieldsarr)) {
         trigger_error("The fields were not found in the request." . print_r($fields), E_USER_ERROR);
     }
     if ($this->has_metadata) {
         $Person =& $db->model('Person');
         $Group =& $db->model('Group');
         if (!isset($req->params['entry']['etag'])) {
             trigger_error("Sorry, the etag was not submitted with the database entry", E_USER_ERROR);
         }
         $atomentry = $db->models['entries']->find_by('etag', $req->params['entry']['etag']);
         if (!$atomentry->exists) {
             $atomentry = $db->models['entries']->base();
             $atomentry->set_value('etag', getEtag(srand(date("s"))));
             $atomentry->set_value('resource', $req->resource);
             $atomentry->set_value('record_id', $rec->{$pkfield});
             $atomentry->set_value('content_type', $content_type);
             $atomentry->set_value('last_modified', timestamp());
             $atomentry->set_value('person_id', get_person_id());
             $aresult = $atomentry->save_changes();
         }
         $p = $Person->find(get_person_id());
         if (!($p->id == $atomentry->attributes['person_id']) && !$this->can_superuser($req->resource)) {
             trigger_error("Sorry, your id does not match the owner of the database entry", E_USER_ERROR);
         }
         $recid = $atomentry->attributes['record_id'];
         if (empty($recid)) {
             trigger_error('The input form eTag did not match a record_id in entries.', E_USER_ERROR);
         }
     } else {
         $recid = $req->id;
         if (empty($recid)) {
             trigger_error('The record id was not found in the "id" form field.', E_USER_ERROR);
         }
     }
     $rec = $this->find($recid);
     foreach ($fieldsarr as $field => $type) {
         if ($this->has_metadata && is_blob($rec->table . '.' . $field)) {
             if (isset($_FILES[strtolower(classify($rec->table))]['name'][$field])) {
                 if ($this->has_metadata) {
                     $content_type = type_of($_FILES[strtolower(classify($rec->table))]['name'][$field]);
                     $atomentry->set_value('content_type', $content_type);
                 }
             }
         }
         $rec->set_value($field, $req->params[strtolower(classify($rec->table))][$field]);
     }
     $result = $rec->save_changes();
     foreach ($fields as $table => $fieldlist) {
         // for each table in the submission do
         $mdl =& $db->get_table($table);
         if (!$mdl->can_write_fields($fieldlist)) {
             trigger_error("Sorry, you do not have permission to " . $req->action . " " . $table, E_USER_ERROR);
         }
         if (!in_array($table, array('entries', $rec->table), true)) {
             $rel = $rec->FirstChild($table);
             foreach ($fieldlist as $field => $type) {
                 $rel->set_value($field, $req->params[strtolower(classify($table))][$field]);
             }
             $rel->save_changes();
         }
     }
     if ($result) {
         $req->set_param('id', $rec->id);
         if ($this->has_metadata) {
             $atomentry->set_value('last_modified', timestamp());
             $atomentry->save_changes();
         }
     } else {
         trigger_error("The record could not be updated in the database.", E_USER_ERROR);
     }
     trigger_after('update_from_post', $this, $rec);
 }
Example #4
0
function cout_data($SIDU, $link, $conn, $sql)
{
    if ($_POST['cmd'] == 'data_save' || $_POST['cmd'] == 'data_del') {
        @save_data($SIDU, $conn[1], $_POST['cmd']);
    }
    $url = !$sql ? "tab" : "sql";
    echo "<form id='dataTab' name='dataTab' action='{$url}.php?id={$link['0']},{$link['1']},{$link['2']},{$link['3']},{$link['4']},{$SIDU['5']},{$SIDU['6']}' method='post'>";
    if (!$sql) {
        echo "<p style='padding:3px'>where ", @html_form("text", "f[sql]", $SIDU['f']['sql'], 300), " <img src='img/tool-run.png' onclick=\"submitForm('cmd','p1')\" class='vm'/> eg col='abc'</p>";
        foreach ($SIDU['g'] as $j => $gSize) {
            if ($gSize == -1) {
                $hidden .= "<a href='#' onclick=\"submitForm('showCol',{$j})\">{$SIDU[col][$j][0]}</a> ";
            }
        }
        if ($hidden) {
            echo "<p>", @lang(104), ": {$hidden}</p>";
        }
    }
    if (isset($SIDU['pk'])) {
        $pk = $SIDU['pk'];
    }
    foreach ($SIDU['col'] as $j => $v) {
        $disp[$j] = $SIDU['g'][$j] == -1 ? " style='display:none'" : "";
        $title = "{$v['0']} " . @str_replace("'", "", $v[1]);
        $color = '';
        if (@in_array($j, $SIDU['pk'])) {
            $title = "PK {$title}";
            $color = '06c';
        }
        if ($v[5] == 'auto_increment' || $v[1] == 'serial' || $v[1] == 'bigserial') {
            $color = 'c00';
        }
        $colH .= "<td class='td{$j}' title='{$title}'{$disp[$j]}><div class='gridH' id='gH{$j}'" . (!$SIDU['gridMode'] ? " style='width:{$SIDU[g][$j]}px'" : "") . ">";
        if (!$sql) {
            $colH .= "<a" . @get_sort_css($v[0], $SIDU[5], $SIDU[6]) . " href='#' onclick=\"submitForm('sidu7','{$v['0']}')\">" . ($color ? "<span style='color:#{$color}'>{$v['0']}</span>" : $v[0]) . "</a>";
        } else {
            $colH .= "<a>{$v['0']}</a>";
        }
        $colH .= "</div></td>";
        $jsStr .= "xHRD.init('gH{$j}',10);";
        $filter .= "<td class='td{$j}'{$disp[$j]}><input type='text' size='1' id='f{$j}' name='f[{$j}]' value='" . @html8($SIDU['f'][$j]) . "'/></td>";
        $grid .= "<td class='td{$j}'{$disp[$j]}><input type='text' size='1' name='g[{$j}]' id='g{$j}' value='" . $SIDU['g'][$j] . "'/></td>";
        if (!$sql) {
            $gridShow .= "<td class='td{$j}'{$disp[$j]}><img src='img/tool-sys.png' title='" . @lang(124) . "' onclick=\"submitForm('sidu7','del:{$v['0']}')\"/> <a href='#' onclick=\"submitForm('hideCol',{$j})\">" . @html_img('img/tool-x') . "</a></td>";
        }
        if ($v[3] == 'CURRENT_TIMESTAMP' || $v[3] == 'now()') {
            $v[3] = "'+his.getFullYear()+'-'+(parseInt(his.getMonth())+1)+'-'+his.getDate()+' '+his.getHours()+':'+his.getMinutes()+':'+his.getSeconds()+'";
        } elseif (@substr($v[3], 0, 9) == "nextval('") {
            $v[3] = '';
        } else {
            $v[3] = @html8($v[3] ? $v[3] : ($v[2] == 'YES' || $v[2] == 'f' ? 'NULL' : ''));
        }
        $align = $SIDU['col'][$j][8] == 'i' ? ' style="text-align:right"' : '';
        $id = 'data_new\'+id+\'_' . $j;
        $is_blob = @is_blob($SIDU['col'][$j]) ? ' onclick="editBlob(\\\'' . $id . '\\\')"' : '';
        $jsColNew .= '<td class="blue td' . $j . '"' . @str_replace("'", "\"", $disp[$j]) . '>';
        if ($is_blob) {
            $jsColNew .= '<input type="hidden" name="' . $id . '" id="' . $id . '" value="' . $v[3] . '"/><input type="text" value="' . $v[3] . '" size="1" id="blob' . $id . '"' . $is_blob . ' style="background:#ddc"/></td>';
        } else {
            $jsColNew .= '<input type="text" size="1" name="' . $id . '" id="' . $id . '" value="' . $v[3] . '"' . $align . $is_blob . ' onchange="document.dataTab.cbox_data_new\'+id+\'.checked=\\\'checked\\\'"/></td>';
        }
        if (!isset($SIDU['pk'])) {
            $pk[] = $j;
        }
        //no pk table with blob col will be slow here
    }
    echo "\n<table class='grid' id='dataTable'>";
    if (!$sql) {
        echo "\n<tr id='trhide' title='", @lang(105), "' style='opacity:0.6", $SIDU['gridShow'] ? "" : ";display:none", "'><td class='cbox'></td>{$gridShow}</tr>";
    }
    echo "\n<tr class='th'><td class='cbox'><input type='checkbox' onclick='checkedAll()'/></td>{$colH}</tr>";
    if (!$sql) {
        echo "\n<tr id='trgrid' title='", @lang(106), "'", $SIDU['gridShow'] ? "" : " style='display:none'", " class='grey'><td class='cbox'></td>{$grid}</tr>\n\t\t<tr class='gridf' title='", @lang(107), " eg: =12'><td class='cbox'><a href='tab.php?id={$SIDU['0']},{$SIDU['1']},{$SIDU['2']},{$SIDU['3']},{$SIDU['4']}' title='", @lang(108), "'>", @html_img("img/tool-find"), "</a></td>{$filter}</tr>";
    }
    foreach ($SIDU['data'] as $i => $row) {
        echo "\n<tr id='tr_{$i}'><td class='cbox'><input type='checkbox' name='cbox_data_{$i}'/></td>";
        foreach ($row as $j => $v) {
            $align = $SIDU['col'][$j][8] == 'i' ? " style='text-align:right'" : "";
            if (is_null($v)) {
                $v = 'NULL';
                $classNull = " null";
            } else {
                $classNull = "";
            }
            $v8 = @html8($v);
            $id = "data_{$i}" . "_{$j}";
            $is_blob = @is_blob($SIDU['col'][$j]) ? " onclick=\"editBlob('{$id}')\"" : "";
            echo "<td class='td{$j}{$classNull}'{$disp[$j]}{$align}>";
            if ($SIDU['gridMode']) {
                if ($is_blob || $sql) {
                    echo @nl2br($v8);
                } else {
                    $v8str = $v8 === 'NULL' ? "IS NULL" : "=\\'" . @strtr($v8, @array("&#039;" => "\\&#039;\\&#039;", "\\" => "\\\\\\\\")) . "\\'";
                    echo "<a href='#' onclick=\"setv('f{$j}','{$v8str}');submitForm('cmd','p1')\">" . @nl2br($v8) . "</a>";
                }
            } else {
                if ($is_blob) {
                    echo "<input type='hidden' name='{$id}' id='{$id}' value='{$v8}'/><input type='text' value='" . @substr($v8, 0, 30) . "' size='1' id='blob{$id}'{$is_blob} style='background:#ddc", $classNull ? ";color:#888;font-style:italic" : "", "'/>";
                } else {
                    echo "<input type='text' size='1'", $classNull ? " class='null'" : "", " name='{$id}' id='{$id}' value='{$v8}' onchange=\"document.dataTab.cbox_data_{$i}.checked='checked'\"{$align}/>";
                }
            }
            if (!$sql && @in_array($j, $pk)) {
                echo "<input type='hidden' name='pkV[{$i}][{$j}]' value='{$v8}'/>";
            }
            echo "</td>";
        }
        echo "</tr>";
    }
    echo "\n</table>";
    $arrH = @array('cmd', 'sidu7', 'sidu8', 'sidu9', 'showCol', 'hideCol');
    foreach ($arrH as $v) {
        echo @html_form("hidden", $v);
    }
    echo "<input type='hidden' id='gridShow' name='gridShow' value='{$SIDU['gridShow']}'/><input type='hidden' id='gridMode' name='gridMode' value='{$SIDU['gridMode']}'/>";
    echo "</form>\n<div id='blobDiv' style='display:none;width:99%;max-width:700px'>\n<input type='button' value='", @lang(109), "' onclick='editBlobSave()'/><input type='button' value='", @lang(110), "' onclick=\"showHide('blobDiv',-1)\"/><input type='hidden' id='blobTxtID'/>\n<br/><textarea id='blobTxt' style='width:99%;height:280px'></textarea>\n</div>\n<iframe name='hiddenfr' src='#' style='width:600px;height:200px;display:none'></iframe>\n<script type='text/javascript'>\nwindow.onload = function(){" . $jsStr . "}";
    if (!$sql) {
        echo "\nfunction addRow(){\n\tvar his = new Date();\n\tvar id = his.getHours()+his.getMinutes()+his.getSeconds();\n\tvar row = document.getElementById('dataTable').insertRow(4);\n\trow.innerHTML='<td class=\"cbox\"><input type=\"checkbox\" name=\"cbox_data_new'+id+'\"></td>{$jsColNew}';\n}";
    }
    echo "</script>";
}