From 00d9b3618a60104a044a9f98b88c0e5db1339615 Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Fri, 24 Jul 2009 10:52:24 +0000 Subject: [PATCH] E-mail sending only in Editor git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@899 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- adminer/include/adminer.inc.php | 16 +++++ adminer/select.inc.php | 123 ++++++++++++++------------------ changes.txt | 6 +- editor/include/adminer.inc.php | 31 ++++++++ todo.txt | 2 +- 5 files changed, 104 insertions(+), 74 deletions(-) diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index a91850f7..bfb1263f 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -120,6 +120,22 @@ function adminer_select_val($val, $link, $field) { return call_adminer('select_val', ($link ? "$return" : $return), $val, $link); } +/** Print extra text in the end of a select form +* @param array fields holding e-mails +* @return bool whether to print default extra +*/ +function adminer_select_extra_display($email_fields) { + call_adminer('select_extra_display', false, $email_fields); +} + +/** Process extras in select form +* @param array AND conditions +* @return bool true if processed, false to process other parts of form +*/ +function adminer_select_extra_process($where) { + return call_adminer('select_extra_process', false, $where); +} + /** Query printed after execution in the message * @param string executed query * @return string diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 537928b5..0db2463b 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -109,72 +109,63 @@ if ($_POST && !$error) { } exit; } - if ($_POST["email"]) { - $sent = 0; - if ($_POST["all"] || $_POST["check"]) { - $field = idf_escape($_POST["email_field"]); - $result = $dbh->query("SELECT DISTINCT $field FROM " . idf_escape($_GET["select"]) . " WHERE $field IS NOT NULL AND $field != ''" . ($where ? " AND " . implode(" AND ", $where) : "") . ($_POST["all"] ? "" : " AND ($where_check)")); - while ($row = $result->fetch_row()) { - $sent += mail($row[0], email_header($_POST["email_subject"]), $_POST["email_message"], "MIME-Version: 1.0\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: 8bit" . ($_POST["email_from"] ? "\nFrom: " . email_header($_POST["email_from"]) : "")); - } - $result->free(); - } - redirect(remove_from_uri(), lang('%d e-mail(s) have been sent.', $sent)); - } elseif (!$_POST["import"]) { // edit - $result = true; - $affected = 0; - $command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]); - if (!$_POST["delete"]) { - $set = array(); - foreach ($columns as $name => $val) { //! should check also for edit or insert privileges - $val = process_input($name, $fields[$name]); - if ($_POST["clone"]) { - $set[] = ($val !== false ? $val : idf_escape($name)); - } elseif ($val !== false) { - $set[] = idf_escape($name) . " = $val"; - } - } - $command .= ($_POST["clone"] ? "\nSELECT " . implode(", ", $set) . "\nFROM " . idf_escape($_GET["select"]) : " SET\n" . implode(",\n", $set)); - } - if ($_POST["delete"] || $set) { - if ($_POST["all"] || ($primary === array() && $_POST["check"])) { - $result = queries($command . ($_POST["all"] ? ($where ? "\nWHERE " . implode(" AND ", $where) : "") : "\nWHERE $where_check")); - $affected = $dbh->affected_rows; - } else { - foreach ((array) $_POST["check"] as $val) { - // where is not unique so OR can't be used - $result = queries($command . "\nWHERE " . where_check($val) . "\nLIMIT 1"); - if (!$result) { - break; + if (!adminer_select_extra_process($where)) { + if (!$_POST["import"]) { // edit + $result = true; + $affected = 0; + $command = ($_POST["delete"] ? ($_POST["all"] && !$where ? "TRUNCATE " : "DELETE FROM ") : ($_POST["clone"] ? "INSERT INTO " : "UPDATE ")) . idf_escape($_GET["select"]); + if (!$_POST["delete"]) { + $set = array(); + foreach ($columns as $name => $val) { //! should check also for edit or insert privileges + $val = process_input($name, $fields[$name]); + if ($_POST["clone"]) { + $set[] = ($val !== false ? $val : idf_escape($name)); + } elseif ($val !== false) { + $set[] = idf_escape($name) . " = $val"; + } + } + $command .= ($_POST["clone"] ? "\nSELECT " . implode(", ", $set) . "\nFROM " . idf_escape($_GET["select"]) : " SET\n" . implode(",\n", $set)); + } + if ($_POST["delete"] || $set) { + if ($_POST["all"] || ($primary === array() && $_POST["check"])) { + $result = queries($command . ($_POST["all"] ? ($where ? "\nWHERE " . implode(" AND ", $where) : "") : "\nWHERE $where_check")); + $affected = $dbh->affected_rows; + } else { + foreach ((array) $_POST["check"] as $val) { + // where is not unique so OR can't be used + $result = queries($command . "\nWHERE " . where_check($val) . "\nLIMIT 1"); + if (!$result) { + break; + } + $affected += $dbh->affected_rows; } - $affected += $dbh->affected_rows; } } - } - query_redirect(queries(), remove_from_uri("page"), lang('%d item(s) have been affected.', $affected), $result, false, !$result); - //! display edit page in case of an error - } elseif (is_string($file = get_file("csv_file"))) { - $file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set - $cols = ""; - $rows = array(); //! packet size - preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches); - foreach ($matches[0] as $key => $val) { - $row = array(); - preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2); - if (!$key && !array_diff($matches2[1], array_keys($fields))) { //! doesn't work with column names containing ",\n - // first row corresponds to column names - use it for table structure - $cols = " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")"; - } else { - foreach ($matches2[1] as $col) { - $row[] = (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col)))); + query_redirect(queries(), remove_from_uri("page"), lang('%d item(s) have been affected.', $affected), $result, false, !$result); + //! display edit page in case of an error + } elseif (is_string($file = get_file("csv_file"))) { + $file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set + $cols = ""; + $rows = array(); //! packet size + preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches); + foreach ($matches[0] as $key => $val) { + $row = array(); + preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2); + if (!$key && !array_diff($matches2[1], array_keys($fields))) { //! doesn't work with column names containing ",\n + // first row corresponds to column names - use it for table structure + $cols = " (" . implode(", ", array_map('idf_escape', $matches2[1])) . ")"; + } else { + foreach ($matches2[1] as $col) { + $row[] = (!strlen($col) ? "NULL" : $dbh->quote(str_replace('""', '"', preg_replace('~^"|"$~', '', $col)))); + } + $rows[] = "\n(" . implode(", ", $row) . ")"; } - $rows[] = "\n(" . implode(", ", $row) . ")"; } + $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . "$cols VALUES" . implode(",", $rows)); + query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) has been imported.', $dbh->affected_rows), $result, false, !$result); + } else { + $error = upload_error($file); } - $result = queries("INSERT INTO " . idf_escape($_GET["select"]) . "$cols VALUES" . implode(",", $rows)); - query_redirect(queries(), remove_from_uri("page"), lang('%d row(s) has been imported.', $dbh->affected_rows), $result, false, !$result); - } else { - $error = upload_error($file); } } @@ -390,17 +381,7 @@ if (!$columns) { } echo "
" . lang('CSV Import') . "
\n"; - //! Editor only - $email_fields = array_filter($email_fields); //! should use strlen but compile.php doesn't support array_filter - if ($email_fields) { - echo '
' . lang('E-mail') . "
\n"; - } + adminer_select_extra_display(array_filter($email_fields)); //! should use strlen but compile.php doesn't support array_filter echo "\n"; } diff --git a/changes.txt b/changes.txt index 2679f4cc..0f75f025 100644 --- a/changes.txt +++ b/changes.txt @@ -1,10 +1,12 @@ Adminer 2.0.0: Editor: User friendly data editor Customization: Adminer class -E-mail sending -Table relations (Editor) Create single column foreign key in table structure +Table relations (Editor) +Send e-mails (Editor) +Display images in blob (Editor) Separate types to groups in table creation +Link e-mails in select Show type in field name title (thanks to Jakub Sochor) Preselect now() for timestamp columns (thanks to paranoiq) Clear history (thanks to paranoiq) diff --git a/editor/include/adminer.inc.php b/editor/include/adminer.inc.php index 8103e43b..b5d51d70 100644 --- a/editor/include/adminer.inc.php +++ b/editor/include/adminer.inc.php @@ -118,6 +118,37 @@ function adminer_select_val($val, $link, $field) { return call_adminer('select_val', ($link ? "$return" : $return), $val, $link); } +function adminer_select_extra_display($email_fields) { + global $confirm; + if (call_adminer('select_extra_display', true, $email_fields) && $email_fields) { + echo '
' . lang('E-mail') . "
\n"; + } +} + +function adminer_select_extra_process($where) { + global $dbh; + if ($_POST["email"]) { + $sent = 0; + if ($_POST["all"] || $_POST["check"]) { + $where_check = "(" . implode(") OR (", array_map('where_check', (array) $_POST["check"])) . ")"; + $field = idf_escape($_POST["email_field"]); + $result = $dbh->query("SELECT DISTINCT $field FROM " . idf_escape($_GET["select"]) . " WHERE $field IS NOT NULL AND $field != ''" . ($where ? " AND " . implode(" AND ", $where) : "") . ($_POST["all"] ? "" : " AND ($where_check)")); + while ($row = $result->fetch_row()) { + $sent += mail($row[0], email_header($_POST["email_subject"]), $_POST["email_message"], "MIME-Version: 1.0\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: 8bit" . ($_POST["email_from"] ? "\nFrom: " . email_header($_POST["email_from"]) : "")); + } + $result->free(); + } + redirect(remove_from_uri(), lang('%d e-mail(s) have been sent.', $sent)); + } + return call_adminer('select_extra_process', false, $where); +} + function adminer_message_query($query) { return call_adminer('message_query', "", $query); } diff --git a/todo.txt b/todo.txt index 4a540ba9..c909968a 100644 --- a/todo.txt +++ b/todo.txt @@ -28,4 +28,4 @@ JavaScript data validation Joining tables - PRIMARY KEY (table, joining) Simplify search - textual, from-to Rank, Tree structure -Treat tinyint(1) as bool +Treat tinyint(1) as bool, format date