create_ticket.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /* Copyright (C) 2013-2016 Jean-François FERRY <hello@librethic.io>
  3. * Copyright (C) 2016 Christophe Battarel <christophe@altairis.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/public/ticket/create_ticket.php
  20. * \ingroup ticket
  21. * \brief Display public form to add new ticket
  22. */
  23. if (!defined('NOREQUIREUSER')) {
  24. define('NOREQUIREUSER', '1');
  25. }
  26. if (!defined('NOTOKENRENEWAL')) {
  27. define('NOTOKENRENEWAL', '1');
  28. }
  29. if (!defined('NOREQUIREMENU')) {
  30. define('NOREQUIREMENU', '1');
  31. }
  32. if (!defined('NOREQUIREHTML')) {
  33. define('NOREQUIREHTML', '1');
  34. }
  35. if (!defined('NOLOGIN')) {
  36. define("NOLOGIN", 1); // This means this output page does not require to be logged.
  37. }
  38. if (!defined('NOCSRFCHECK')) {
  39. define("NOCSRFCHECK", 1); // We accept to go on this page from external web site.
  40. }
  41. if (!defined('NOIPCHECK')) {
  42. define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
  43. }
  44. if (!defined('NOBROWSERNOTIF')) {
  45. define('NOBROWSERNOTIF', '1');
  46. }
  47. require '../../main.inc.php';
  48. require_once DOL_DOCUMENT_ROOT.'/ticket/class/actions_ticket.class.php';
  49. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formticket.class.php';
  50. require_once DOL_DOCUMENT_ROOT.'/core/lib/ticket.lib.php';
  51. require_once DOL_DOCUMENT_ROOT.'/core/lib/security.lib.php';
  52. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  53. require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
  54. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  55. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  56. // Load translation files required by the page
  57. $langs->loadLangs(array('companies', 'other', 'mails', 'ticket'));
  58. // Get parameters
  59. $id = GETPOST('id', 'int');
  60. $msg_id = GETPOST('msg_id', 'int');
  61. $action = GETPOST('action', 'aZ09');
  62. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  63. $hookmanager->initHooks(array('publicnewticketcard', 'globalcard'));
  64. $object = new Ticket($db);
  65. $extrafields = new ExtraFields($db);
  66. $extrafields->fetch_name_optionals_label($object->table_element);
  67. if (empty($conf->ticket->enabled)) {
  68. accessforbidden('', 0, 0, 1);
  69. }
  70. /*
  71. * Actions
  72. */
  73. $parameters = array(
  74. 'id' => $id,
  75. );
  76. // Note that $action and $object may have been modified by some hooks
  77. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action);
  78. if ($reshook < 0) {
  79. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  80. }
  81. // Add file in email form
  82. if (empty($reshook) && GETPOST('addfile', 'alpha') && !GETPOST('save', 'alpha')) {
  83. ////$res = $object->fetch('','',GETPOST('track_id'));
  84. ////if($res > 0)
  85. ////{
  86. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  87. // Set tmp directory TODO Use a dedicated directory for temp mails files
  88. $vardir = $conf->ticket->dir_output;
  89. $upload_dir_tmp = $vardir.'/temp/'.session_id();
  90. if (!dol_is_dir($upload_dir_tmp)) {
  91. dol_mkdir($upload_dir_tmp);
  92. }
  93. dol_add_file_process($upload_dir_tmp, 0, 0, 'addedfile', '', null, '', 0);
  94. $action = 'create_ticket';
  95. ////}
  96. }
  97. // Remove file
  98. if (empty($reshook) && GETPOST('removedfile', 'alpha') && !GETPOST('save', 'alpha')) {
  99. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  100. // Set tmp directory
  101. $vardir = $conf->ticket->dir_output.'/';
  102. $upload_dir_tmp = $vardir.'/temp/'.session_id();
  103. // TODO Delete only files that was uploaded from email form
  104. dol_remove_file_process($_POST['removedfile'], 0, 0);
  105. $action = 'create_ticket';
  106. }
  107. if (empty($reshook) && $action == 'create_ticket' && GETPOST('save', 'alpha')) {
  108. $error = 0;
  109. $origin_email = GETPOST('email', 'alpha');
  110. if (empty($origin_email)) {
  111. $error++;
  112. array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Email")));
  113. $action = '';
  114. } else {
  115. // Search company saved with email
  116. $searched_companies = $object->searchSocidByEmail($origin_email, '0');
  117. // Chercher un contact existant avec cette adresse email
  118. // Le premier contact trouvé est utilisé pour déterminer le contact suivi
  119. $contacts = $object->searchContactByEmail($origin_email);
  120. // Option to require email exists to create ticket
  121. if (!empty($conf->global->TICKET_EMAIL_MUST_EXISTS) && !$contacts[0]->socid) {
  122. $error++;
  123. array_push($object->errors, $langs->trans("ErrorEmailMustExistToCreateTicket"));
  124. $action = '';
  125. }
  126. }
  127. if (!GETPOST("subject", "restricthtml")) {
  128. $error++;
  129. array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Subject")));
  130. $action = '';
  131. } elseif (!GETPOST("message", "restricthtml")) {
  132. $error++;
  133. array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("message")));
  134. $action = '';
  135. }
  136. // Check email address
  137. if (!isValidEmail($origin_email)) {
  138. $error++;
  139. array_push($object->errors, $langs->trans("ErrorBadEmailAddress", $langs->transnoentities("email")));
  140. $action = '';
  141. }
  142. // Check Captcha code if is enabled
  143. if (!empty($conf->global->MAIN_SECURITY_ENABLECAPTCHA)) {
  144. $sessionkey = 'dol_antispam_value';
  145. $ok = (array_key_exists($sessionkey, $_SESSION) === true && (strtolower($_SESSION[$sessionkey]) === strtolower(GETPOST('code', 'none'))));
  146. if (!$ok) {
  147. $error++;
  148. array_push($object->errors, $langs->trans("ErrorBadValueForCode"));
  149. $action = '';
  150. }
  151. }
  152. if (!$error) {
  153. $object->db->begin();
  154. $object->track_id = generate_random_id(16);
  155. $object->subject = GETPOST("subject", "restricthtml");
  156. $object->message = GETPOST("message", "restricthtml");
  157. $object->origin_email = $origin_email;
  158. $object->type_code = GETPOST("type_code", 'aZ09');
  159. $object->category_code = GETPOST("category_code", 'aZ09');
  160. $object->severity_code = GETPOST("severity_code", 'aZ09');
  161. if (is_array($searched_companies)) {
  162. $object->fk_soc = $searched_companies[0]->id;
  163. }
  164. if (is_array($contacts) and count($contacts) > 0) {
  165. $object->fk_soc = $contacts[0]->socid;
  166. $usertoassign = $contacts[0]->id;
  167. }
  168. $ret = $extrafields->setOptionalsFromPost(null, $object);
  169. // Generate new ref
  170. $object->ref = $object->getDefaultRef();
  171. if (!is_object($user)) {
  172. $user = new User($db);
  173. }
  174. $object->context['disableticketemail'] = 1; // Disable emails sent by ticket trigger when creation is done from this page, emails are already sent later
  175. $id = $object->create($user);
  176. if ($id <= 0) {
  177. $error++;
  178. $errors = ($object->error ? array($object->error) : $object->errors);
  179. array_push($object->errors, $object->error ? array($object->error) : $object->errors);
  180. $action = 'create_ticket';
  181. }
  182. if (!$error && $id > 0) {
  183. if ($usertoassign > 0) {
  184. $object->add_contact($usertoassign, "SUPPORTCLI", 'external', 0);
  185. }
  186. }
  187. if (!$error) {
  188. $object->db->commit();
  189. $action = "infos_success";
  190. } else {
  191. $object->db->rollback();
  192. setEventMessages($object->error, $object->errors, 'errors');
  193. $action = 'create_ticket';
  194. }
  195. if (!$error) {
  196. $res = $object->fetch($id);
  197. if ($res) {
  198. // Create form object
  199. include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
  200. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  201. $formmail = new FormMail($db);
  202. // Init to avoid errors
  203. $filepath = array();
  204. $filename = array();
  205. $mimetype = array();
  206. $attachedfiles = $formmail->get_attached_files();
  207. $filepath = $attachedfiles['paths'];
  208. $filename = $attachedfiles['names'];
  209. $mimetype = $attachedfiles['mimes'];
  210. // Send email to customer
  211. $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketNewEmailSubject', $object->ref, $object->track_id);
  212. $message = ($conf->global->TICKET_MESSAGE_MAIL_NEW ? $conf->global->TICKET_MESSAGE_MAIL_NEW : $langs->transnoentities('TicketNewEmailBody')).'<br><br>';
  213. $message .= $langs->transnoentities('TicketNewEmailBodyInfosTicket').'<br>';
  214. $url_public_ticket = ($conf->global->TICKET_URL_PUBLIC_INTERFACE ? $conf->global->TICKET_URL_PUBLIC_INTERFACE.'/' : dol_buildpath('/public/ticket/view.php', 2)).'?track_id='.$object->track_id;
  215. $infos_new_ticket = $langs->transnoentities('TicketNewEmailBodyInfosTrackId', '<a href="'.$url_public_ticket.'" rel="nofollow noopener">'.$object->track_id.'</a>').'<br>';
  216. $infos_new_ticket .= $langs->transnoentities('TicketNewEmailBodyInfosTrackUrl').'<br><br>';
  217. $message .= $infos_new_ticket;
  218. $message .= $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE ? $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE : $langs->transnoentities('TicketMessageMailSignatureText');
  219. $sendto = GETPOST('email', 'alpha');
  220. $from = $conf->global->MAIN_INFO_SOCIETE_NOM.' <'.$conf->global->TICKET_NOTIFICATION_EMAIL_FROM.'>';
  221. $replyto = $from;
  222. $sendtocc = '';
  223. $deliveryreceipt = 0;
  224. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  225. $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  226. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  227. }
  228. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  229. $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1, '', '', 'tic'.$object->id, '', 'ticket');
  230. if ($mailfile->error || $mailfile->errors) {
  231. setEventMessages($mailfile->error, $mailfile->errors, 'errors');
  232. } else {
  233. $result = $mailfile->sendfile();
  234. }
  235. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  236. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  237. }
  238. // Send email to TICKET_NOTIFICATION_EMAIL_TO
  239. $sendto = $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
  240. if ($sendto) {
  241. $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketNewEmailSubjectAdmin', $object->ref, $object->track_id);
  242. $message_admin = $langs->transnoentities('TicketNewEmailBodyAdmin', $object->track_id).'<br><br>';
  243. $message_admin .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
  244. $message_admin .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';
  245. $message_admin .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';
  246. $message_admin .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';
  247. $message_admin .= '<li>'.$langs->trans('From').' : '.$object->origin_email.'</li>';
  248. // Extrafields
  249. $extrafields->fetch_name_optionals_label($object->table_element);
  250. if (is_array($object->array_options) && count($object->array_options) > 0) {
  251. foreach ($object->array_options as $key => $value) {
  252. $key = substr($key, 8); // remove "options_"
  253. $message_admin .= '<li>'.$langs->trans($extrafields->attributes[$object->element]['label'][$key]).' : '.$extrafields->showOutputField($key, $value).'</li>';
  254. }
  255. }
  256. $message_admin .= '</ul>';
  257. $message_admin .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
  258. $message_admin .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'" rel="nofollow noopener">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';
  259. $from = $conf->global->MAIN_INFO_SOCIETE_NOM.' <'.$conf->global->TICKET_NOTIFICATION_EMAIL_FROM.'>';
  260. $replyto = $from;
  261. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  262. $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
  263. $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
  264. }
  265. include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  266. $mailfile = new CMailFile($subject, $sendto, $from, $message_admin, $filepath, $mimetype, $filename, $sendtocc, '', $deliveryreceipt, -1, '', '', 'tic'.$object->id, '', 'ticket');
  267. if ($mailfile->error || $mailfile->errors) {
  268. setEventMessages($mailfile->error, $mailfile->errors, 'errors');
  269. } else {
  270. $result = $mailfile->sendfile();
  271. }
  272. if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
  273. $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
  274. }
  275. }
  276. }
  277. // Copy files into ticket directory
  278. $destdir = $conf->ticket->dir_output.'/'.$object->ref;
  279. if (!dol_is_dir($destdir)) {
  280. dol_mkdir($destdir);
  281. }
  282. foreach ($filename as $i => $val) {
  283. dol_move($filepath[$i], $destdir.'/'.$filename[$i], 0, 1);
  284. $formmail->remove_attached_files($i);
  285. }
  286. //setEventMessages($langs->trans('YourTicketSuccessfullySaved'), null, 'mesgs');
  287. // Make a redirect to avoid to have ticket submitted twice if we make back
  288. $messagetoshow = $langs->trans('MesgInfosPublicTicketCreatedWithTrackId', '{s1}', '{s2}');
  289. $messagetoshow = str_replace(array('{s1}', '{s2}'), array('<strong>'.$object->track_id.'</strong>', '<strong>'.$object->ref.'</strong>'), $messagetoshow);
  290. setEventMessages($messagetoshow, null, 'warnings');
  291. setEventMessages($langs->trans('PleaseRememberThisId'), null, 'warnings');
  292. header("Location: index.php");
  293. exit;
  294. }
  295. } else {
  296. setEventMessages($object->error, $object->errors, 'errors');
  297. }
  298. }
  299. /*
  300. * View
  301. */
  302. $form = new Form($db);
  303. $formticket = new FormTicket($db);
  304. if (!$conf->global->TICKET_ENABLE_PUBLIC_INTERFACE) {
  305. print '<div class="error">'.$langs->trans('TicketPublicInterfaceForbidden').'</div>';
  306. $db->close();
  307. exit();
  308. }
  309. $arrayofjs = array();
  310. $arrayofcss = array('/opensurvey/css/style.css', '/ticket/css/styles.css.php');
  311. llxHeaderTicket($langs->trans("CreateTicket"), "", 0, 0, $arrayofjs, $arrayofcss);
  312. print '<div class="ticketpublicarea">';
  313. if ($action != "infos_success") {
  314. $formticket->withfromsocid = isset($socid) ? $socid : $user->socid;
  315. $formticket->withtitletopic = 1;
  316. $formticket->withcompany = 0;
  317. $formticket->withusercreate = 1;
  318. $formticket->fk_user_create = 0;
  319. $formticket->withemail = 1;
  320. $formticket->ispublic = 1;
  321. $formticket->withfile = 2;
  322. $formticket->action = 'create_ticket';
  323. $formticket->param = array('returnurl' => $_SERVER['PHP_SELF'].($conf->entity > 1 ? '?entity='.$conf->entity : ''));
  324. print load_fiche_titre($langs->trans('NewTicket'), '', '', 0, 0, 'marginleftonly');
  325. if (empty($conf->global->TICKET_NOTIFICATION_EMAIL_FROM)) {
  326. $langs->load("errors");
  327. print '<div class="error">';
  328. print $langs->trans("ErrorFieldRequired", $langs->transnoentities("TicketEmailNotificationFrom")).'<br>';
  329. print $langs->trans("ErrorModuleSetupNotComplete", $langs->transnoentities("Ticket"));
  330. print '</div>';
  331. } else {
  332. print '<div class="info marginleftonly marginrightonly">'.$langs->trans('TicketPublicInfoCreateTicket').'</div>';
  333. $formticket->showForm(0, 'edit', 1);
  334. }
  335. }
  336. print '</div>';
  337. // End of page
  338. htmlPrintOnlinePaymentFooter($mysoc, $langs, 1, $suffix, $object);
  339. llxFooter('', 'public');
  340. $db->close();