newticket.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:18k
- <?php
- // +-------------------------------------------------------------+
- // | DeskPRO v [2.0.1 Production]
- // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
- // | Supplied by WTN-WDYL
- // | Nullified by WTN-WDYL
- // | Distribution via WebForum, ForumRU and associated file dumps
- // +-------------------------------------------------------------+
- // | DESKPRO IS NOT FREE SOFTWARE
- // +-------------------------------------------------------------+
- // | License ID : Full Enterprise License =) ...
- // | License Owner : WTN-WDYL Team
- // +-------------------------------------------------------------+
- // | $RCSfile: newticket.php,v $
- // | $Date: 2004/02/12 21:16:57 $
- // | $Revision: 1.72 $
- // +-------------------------------------------------------------+
- // | File Details:
- // | - Ticket creation interface.
- // +-------------------------------------------------------------+
- error_reporting(E_ALL & ~E_NOTICE);
- require_once('./global.php');
- //Nullify WTN-WDYL Team
- max_limits('tickets');
- $template_cache = templatecache('NEW_newticket,HF_footer,HF_header');
- ############################### NEED TO BE REGISTERED ###############################
- // run user checks
- user_p_checks();
- // not logged in? do we require registration?
- if ($settings[require_registration] AND !$session[userid]) {
- login_form();
- }
- // is the user expired?
- if ($user['id']) {
- $exp = user_expired($user['id']);
- if (is_array($exp)) {
- $user_expired = 1;
- $user_expired_tickets = $exp[0];
- $user_expired_date = $exp[1];
- }
- }
- if ($user_expired) {
- error($dplang['expired_user']);
- }
- ############################### START PROCESSING ###############################
- // default do
- $_REQUEST['do'] = trim($_REQUEST['do']);
- if (!isset($_REQUEST['do']) or $_REQUEST['do'] == "") {
- $_REQUEST['do'] = "start";
- }
- // globalise variables
- $global = array (
- array('tempticketid'), // used for attachments
- array('email')
- );
- rg($global);
- $page = 'newquestion';
- $navigation = ' // ' . "<a class="light" href="newticket.php">$dplang[ask_question]</a>";
- ############################### PROCESS A TEMPORARY ATTACHMENT ###############################
- if ($_REQUEST['attachment_upload']) {
- $attachment = validate_attachment($attachment_error);
- if ($attachment) {
- $attach = add_attachment();
- $attachment_yes = 1;
-
- // blobid is unique
- if ($tempticketid == '' OR !$tempticketid) {
- $tempticketid = $attach[blobid];
- }
- $db->query("
- INSERT INTO ticket_attachments SET
- blobid = '$attach[blobid]',
- filename = '" . mysql_escape_string($attach[name]) . "',
- filesize = '" . mysql_escape_string($attach[size]) . "',
- extension = '" . mysql_escape_string($attach[extension]) . "',
- temporaryid = '" . mysql_escape_string($tempticketid) . "',
- timestamp = '" . mktime() . "'" .
- iff($user['id'], ", userid = '$user[id]'")
- );
- $redo = 1;
-
- } else {
-
- if ($attachment_error) {
- // attachment error type
- if ($attachment_error == 1) {
- $attachment_no_big = 1;
- } elseif ($attachment_error == 2) {
- $attachment_no_filetype = 1;
- } else {
- $attachment_no = 1;
- }
- $stop = 1;
- }
- }
- $_REQUEST['do'] = "start";
- }
- ############################### DELETE A TEMPORARY ATTACHMENT ###############################
- if ($_REQUEST['delattachid']) {
- $result = $db->query_return("
- SELECT blobid
- FROM ticket_attachments
- WHERE id = '" . mysql_escape_string($_REQUEST['delattachid']) . "'
- ");
- $db->query("DELETE FROM blobs WHERE id = '$result[blobid]'");
- $db->query("DELETE FROM ticket_attachments WHERE
- id = '" . mysql_escape_string($_REQUEST['delattachid']) . "'
- ");
- $_REQUEST['do'] = "start";
- $redo = 1;
- }
- ############################### PROCESS SUBMITTED FORM ###############################
- if ($_REQUEST['do'] == "submit") {
- //////////////// Check to see if ticket has already been created ////////////////
-
- // last hour. We don't want to scan whole database
- $check_time = mktime() - 3600;
- $check_ticket = $db->query_return("
- SELECT ticket.ref
- FROM ticket
- LEFT JOIN ticket_message ON (ticket.id = ticket_message.ticketid)
- WHERE email = '" . addslashes($_REQUEST[email]) . "'
- AND (ticket.date_opened > $check_time)
- AND ticket_message.message = '" . addslashes($_REQUEST[message]) . "'
- ");
- if ($db->num_rows()) {
- jump("view.php?ticketref=$check_ticket[ref]", 'redirect_new_ticket');
- }
- // term for category and priority to only allow cats/pris that are open to unregistered users
- if (!$user) {
- $term = 'AND !require_registration';
- } else {
- unset($term);
- }
- //////////////// category ////////////////
- if ($settings['category_user_viewable'] AND $settings['category_user_editable']) {
- $db->query("SELECT * FROM ticket_cat
- WHERE user_view
- AND user_select
- $term
- ORDER by cat_order
- ");
- while ($cat = $db->row_array()) {
- $cats[] = $cat['id'];
- if ($cat['id'] == $_REQUEST['category']) {
- $cat_data = $cat;
- }
- }
- // only valid categories
- if (!(@in_array($_REQUEST['category'], $cats))) {
- unset($_REQUEST['category']);
- }
- // category required
- if ($settings['category_require_selection'] AND !$_REQUEST['category']) {
- $stop = 1;
- $error_category = 1;
- }
- }
- //////////////// priority ////////////////
-
- if (!$settings[priority_disable] AND $settings[priority_user_viewable] AND $settings[priority_user_editable]) {
-
- $db->query(" SELECT * FROM ticket_pri
- WHERE user_view
- AND user_select
- $term
- ORDER by pri_order
- ");
- while ($pri = $db->row_array()) {
- $pris[] = $pri[id];
- if ($pri[id] == $_REQUEST[priority]) {
- $pri_data = $pri;
- }
- }
- // only valid priorities
- if (!(@in_array($_REQUEST[priority], $pris))) {
- unset($_REQUEST[priority]);
- }
- // priority required
- if ($settings[priority_require_selection] AND !$_REQUEST[priority]) {
- $stop = 1;
- $error_priority = 1;
- }
- }
- //////////////// email ////////////////
- if ($user) {
- // must be an email associated with the user
- if ($email == $user[email] OR $email == '') {
- $email = $user[email];
-
- } else {
- $db->query("SELECT email FROM user_email WHERE userid = '$session[userid]' AND validated");
- while ($result = $db->row_array()) {
- $email_array[] = $result[email];
- }
- if (!@in_array($email, $email_array)) {
- $stop = 1;
- $error_notyou_email = 1;
- $error_email = 1;
- }
- }
- } else {
- // check email is valid
- if (!validate_email($email)) {
- $stop = 1;
- $error_bad_email = 1;
- $error_email = 1;
- } elseif (banned_email($email)) {
- $stop = 1;
- $error_banned_email = 1;
- $error_email = 1;
- }
- }
- //////////////// subject ////////////////
- if (strlen($_REQUEST[subject]) < 5) {
- $stop = 1;
- $error_subject = 1;
- }
- //////////////// message ////////////////
- // check that the post box is not too large
- if (strlen($_REQUEST[message]) > $settings[max_size] AND $settings[max_size] != 0) {
- $stop = 1;
- $error_post_big = 1;
- $error_post = 1;
- }
-
- // check that the post box is not empty as well
- if (strlen($_REQUEST[message]) < 5) {
- $stop = 1;
- $error_post_small = 1;
- $error_post = 1;
- }
- //////////////// custom fields ////////////////
-
- $db->query("SELECT * from ticket_def WHERE ticket_start");
-
- while ($result = $db->row_array()) {
- $data = field_def_val($result, $_REQUEST[custom_fields][$result[name]], $_REQUEST[custom_fields]["extra" . $result[name]]);
- if ($data === NULL) {
- $tmp = unserialize($result[error_message]);
- $error_message = $tmp[$session[language]];
- // form errors;
- $custom_error[] = array('error' => $error_message);
- // for form design
- $custom_errors[] = $result[name];
- $stop = 1;
-
- } else {
- $query2 .= " $result[name] = '" . mysql_escape_string($data) . "', ";
-
- }
- }
- //////////////// attachments ////////////////
- // attachment during this upload
- $attachment = validate_attachment($attachment_error);
- if ($attachment_error AND $attachment_error != 5) {
- // attachment error type
- if ($attachment_error == 1) {
- $attachment_no_big = 1;
- } elseif ($attachment_error == 2) {
- $attachment_no_filetype = 1;
- } else {
- $attachment_no = 1;
- }
- $stop = 1;
- }
- //////////////// error / create ticket ////////////////
- if ($stop) {
- $_REQUEST['do'] = "start";
- } else {
- $_REQUEST['do'] = "create";
- }
- }
- ############################### CREATE NEW TICKET ###############################
- if ($_REQUEST['do'] == "start") {
- if (!$email) {
- $email = $user[email];
- }
- // sort emails
- if ($session[userid]) {
- $email_array[] = $user[email];
- $db->query("SELECT email FROM user_email WHERE userid = '$session[userid]' AND validated");
- while ($result = $db->row_array()) {
- $email_array[] = $result[email];
- }
- if (count($email_array) > 1) {
- $email_html = form_select('email', $email_array, $email, '', 1);
- }
- } else {
- $email_html = form_input('email', $email);
- }
- // categories
- $categories = user_category_array('new');
- if (is_array($categories)) {
- $category_html = form_select('category', $categories, $_REQUEST[category]);
- }
- // priorities
- $priorities = user_priority_array('new');
- if (is_array($priorities)) {
- $priority_html = form_select('priority', $priorities, $_REQUEST[priority]);
- }
-
- // get rest of ticket data
- $db->query(
- "SELECT * from ticket_def
- WHERE ticket_start
- ORDER by displayorder"
- );
- $custom = array();
- while ($result = $db->row_array()) {
- unset($name, $description, $error, $html);
- $tmp = unserialize($result[display_name]);
- $name = $tmp[$session[language]];
- $tmp = unserialize($result[description]);
- $description = $tmp[$session[language]];
- if ($stop OR $redo) {
- $html = field_def($result, 'redo', $_REQUEST[custom_fields][$result[name]], $_REQUEST[custom_fields]["extra" . $result[name]]);
- } else {
- $html = field_def($result);
- }
- $tmp = unserialize($result[error_message]);
- $error = $tmp[$session[language]];
- // two arrays, one for loop and one to allow custom form design
- $custom[] = array(
- 'name' => $name,
- 'description' => $description,
- 'html' => $html,
- 'error' => iff(@in_array($result[name], $custom_errors), $error, '')
- );
- ${$result[name]} = array(
- 'name' => $name,
- 'description' => $description,
- 'html' => $html,
- 'error' => iff(@in_array($result[name], $custom_errors), $error, '')
- );
- }
- $attach = array();
-
- $db->query(" SELECT * FROM ticket_attachments
- WHERE temporaryid = '" . mysql_escape_string($tempticketid) . "'
- AND temporaryid
- ");
-
- while ($result = $db->row_array()) {
- $attach[] = array(
- 'id' => $result[id],
- 'name' => $result[filename],
- 'filesize' => filesize_display($result[filesize])
- );
- }
- eval(makeeval('header', 'HF_header'));
- eval(makeeval('footer', 'HF_footer'));
- eval(makeeval('echo', 'NEW_newticket'));
- }
- ############################### ADD TICKET TO DATABASE AND PROCESS ################################
- if ($_REQUEST['do'] == "create") {
- /////////////////////// CHECK / CREATE USER ///////////////////////
- // logged in
- if (!$session[userid]) {
- // old user who is not logged in
- if ($user[id] = userid_from_email($email)) {
-
- $loggedout_user = 1;
- $user = $db->query_return("SELECT * FROM user WHERE id = '$user[id]'");
- } else {
-
- // create user
- $new_user = 1;
- // passwords
- $password = make_pass(8);
- $password_cookie = md5($session[sessionid] . $password . uniqid(rand(),1));
- $password_cookie = substr($password_cookie, 0, 8);
- $password_url = md5($password . uniqid(rand(),1) . $session[sessionid]);
- $password_url = substr($password_url, 0, 8);
- // create username and check valid
- $username = make_username($email);
-
- // validate number
- $validate_number = substr(md5(time()),0,6);
-
- $db->query("
- INSERT into user SET
- password = '" . mysql_escape_string($password) . "',
- password_cookie = '" . mysql_escape_string($password_cookie) . "',
- password_url = '" . mysql_escape_string($password_url) . "',
- email = '" . mysql_escape_string($email) . "',
- name = '" . mysql_escape_string($_REQUEST[name]) . "',
- date_registered = '" . mktime() . "',
- validate_key = '" . addslashes($validate_number) . "',
- username = '" . mysql_escape_string($username) . "'" .
- iff($settings['manual_validation'], ", awaiting_manual_validation = '1'") .
- iff($settings['validate_email'], ", awaiting_validation = '1'") . "
- ");
- $user[id] = $db->last_id();
- $user = $db->query_return("SELECT * FROM user WHERE id = '$user[id]'");
- $user_details = $user; // needed for email to admins
- update_session('user', $user[id]);
- dp_setcookie('dp_user_userid', $user[id], 'ever');
- dp_setcookie('dp_user_password', $password_cookie, 'ever');
- $send_emails = explode(',', $settings[email_registeradmin]);
- if (is_array($send_emails)) {
- foreach ($send_emails AS $key => $var) {
- $var = trim($var);
- if (validate_email($var)) {
- $toemail = $var;
- eval(makeemaileval('message', 'TECHBODY_newuser', $subject));
- dp_mail($var, $subject, $message);
- }
- }
- }
- }
- } else {
- $loggedin_user = 1;
- }
- /////////////////////// CREATE TICKET ///////////////////////
-
- $ticketref = make_ticket_ref();
- $authcode = substr(md5(rand(0,100000) . mktime()), 0, 8);
- // start the sql statment (including custom fields
- $query = "
- INSERT into ticket SET
- $query2
- subject = '" . mysql_escape_string($_REQUEST[subject]) . "',
- language = '$session[language]',
- category = '" . mysql_escape_string($_REQUEST[category]) . "',
- priority = '" . mysql_escape_string($_REQUEST[priority]) . "',
- userid = '$user[id]',
- is_open = '1',
- awaiting_tech = '1',
- date_awaiting_toggled = '" . mktime() . "',
- date_opened = '" . mktime() . "',
- authcode = '$authcode',
- ref = '$ticketref'
- ";
- // set ticket to hidden if the user is awaiting a form of validation
- // i) new users and and settings to validate
- // ii) already awaiting a form of validation
- // tech must validate themselves
- if ($user[awaiting_manual_validation] OR ($settings[manual_validation] AND $new_user)) {
- $query .= ", nodisplay = '3'";
- } elseif ($user[awaiting_validation] OR ($settings[validate_email] AND $new_user)) {
- // user must validate themselves
- $query .= ", nodisplay = '2'";
- }
- // user e-mail
- if ($_REQUEST['email']) {
- $query .= ", email = '". mysql_escape_string($_REQUEST['email']) . "'";
- }
-
- // auto assignemnt (category overrides priority)
- if ($pri_data[auto_assign_tech]) {
- $query .= ", tech = '$pri_data[auto_assign_tech]'";
- } elseif ($cat_data[auto_assign_tech]) {
- $query .= ", tech = '$cat_data[auto_assign_tech]'";
- }
- // add new thread to database and get back id
- $db->query($query);
- $id = $db->last_id();
- ticketlog($id, 'created');
- /////////////////////// ATTACHMENTS ///////////////////////
- if ($attachment) {
- $attach = add_attachment();
-
- $db->query("
- INSERT INTO ticket_attachments SET
- blobid = '$attach[blobid]',
- filename = '" . mysql_escape_string($attach[name]) . "',
- filesize = '" . mysql_escape_string($attach[size]) . "',
- extension = '" . mysql_escape_string($attach[extension]) . "',
- ticketid = '" . mysql_escape_string($id) . "',
- userid = '$user[id]',
- timestamp = '" . mktime() . "'
- ");
- $attachment_id = $db->last_id();
- // attachments to send
- $attach['id'] = $attachment_id;
- // to link to an attachment for those techs that don't want to download it
- $email_attachment[] = $attach;
- }
- // any previous attachments
- if ($_REQUEST[tempticketid]) {
- $db->query("SELECT blobs.blobdata, ticket_attachments.id, filename, extension
- FROM ticket_attachments
- LEFT JOIN blobs ON (blobs.id = ticket_attachments.blobid)
- ");
- // get the attachments to email
- while ($result = $db->row_array()) {
- $email_attachments[] = array(
- 'data' => $result[blobdata],
- 'name' => $result[filename],
- 'extension' => $result[extension]
- );
- }
- $db->query("UPDATE ticket_attachments SET
- ticketid = '" . mysql_escape_string($id) . "',
- userid = '$user[id]',
- temporaryid = ''
- WHERE temporaryid = '" . mysql_escape_string($_REQUEST[tempticketid]) . "'
- ");
- }
- /////////////////////// ADD REPLY TO DATABASE ///////////////////////
-
- // add the new post to database
- $db->query("
- INSERT into ticket_message SET
- message = '" . mysql_escape_string($_REQUEST[message]) . "',
- ticketid = '" . intval($id) . "',
- userid = '$user[id]',
- date = '" . mktime() . "',
- ipaddress = '" . mysql_escape_string($ipaddress) . "'
- ");
- /////////////////////// CLEAN VARIABLES TO USE IN EMAIL ///////////////////////
- $ticket = $db->query_return("
- SELECT ticket.*
- FROM ticket
- WHERE ticket.id = '" . intval($id) . "'
- ");
- // logged in
- if (!$user) {
- global $user;
- $user = array(
- 'id' => $user[id],
- 'password' => $password,
- 'password_cookie' => $password_cookie,
- 'password_url' => $password_url,
- 'subject' => $subject,
- 'email' => $email,
- 'username' => $username
- );
- }
- $message = $_REQUEST['message'];
- $ticket['body'] = $message;
- /////////////////////// SEND EMAILS ///////////////////////
- notify_user('new_user', $ticket, $user, $_REQUEST[message], $email_attachment);
- notify_technicians('new', $ticket, $user, $_REQUEST[message], $email_attachment);
- /////////////////////// JUMP DEPENDING ON USER STATUS ///////////////////////
- if ($loggedin_user) {
- jump("view.php?ticketref=$ticketref", 'redirect_new_ticket');
- } elseif ($loggedout_user) {
- jump("view.php?ticketref=$ticketref&newticket=1", 'redirect_new_ticket');
- } elseif ($new_user) {
- jump("view.php?ticketref=$ticketref", 'redirect_new_ticket');
- }
- }
- ?>