| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,181 @@ |
| 1 |
+<?php |
|
| 2 |
+/** |
|
| 3 |
+ * Copyright (C) 2021 Double Bastion LLC |
|
| 4 |
+ * |
|
| 5 |
+ * This file is part of Roundpin, which is licensed under the |
|
| 6 |
+ * GNU Affero General Public License Version 3.0. The license terms |
|
| 7 |
+ * are detailed in the "LICENSE.txt" file located in the root directory. |
|
| 8 |
+ */ |
|
| 9 |
+ |
|
| 10 |
+session_start(); |
|
| 11 |
+ |
|
| 12 |
+ // header('Set-Cookie: PHPSESSID= ' . session_id() . '; SameSite=strict; Secure=true; HttpOnly=true;');
|
|
| 13 |
+ |
|
| 14 |
+if (isset($_POST['s_ajax_call']) && ($_POST['s_ajax_call'] == $_SESSION['validate_s_access'])) {
|
|
| 15 |
+ |
|
| 16 |
+ define('ACCESSCONST', TRUE);
|
|
| 17 |
+ |
|
| 18 |
+ require('db-connect.php');
|
|
| 19 |
+ |
|
| 20 |
+ $username = $_POST['username']; |
|
| 21 |
+ $contDataPosted = $_POST['contactsdata']; |
|
| 22 |
+ $contactsData = json_decode($contDataPosted, true); |
|
| 23 |
+ |
|
| 24 |
+ // Get the user id from the 'app_users' table |
|
| 25 |
+ $queryid = $mysqli->prepare("SELECT id, username FROM app_users WHERE BINARY username = ?");
|
|
| 26 |
+ $queryid->bind_param("s", $username);
|
|
| 27 |
+ $queryid->execute(); |
|
| 28 |
+ $queryidres = $queryid->get_result()->fetch_assoc(); |
|
| 29 |
+ $userID = $queryidres['id']; |
|
| 30 |
+ |
|
| 31 |
+ $restrictmsg = ""; |
|
| 32 |
+ $insertcheck = 0; |
|
| 33 |
+ $duplicate = 0; |
|
| 34 |
+ |
|
| 35 |
+ for ($i = 0; $i < count($contactsData); $i++) {
|
|
| 36 |
+ |
|
| 37 |
+ $contactName = $contactsData[$i][0]; |
|
| 38 |
+ if (preg_match('/[^A-Za-z0-9\s\-\.\'\(\)]/', $contactName)) {
|
|
| 39 |
+ $contactNamemsg = "The 'Display Name' field contains characters that are not allowed."; |
|
| 40 |
+ } else { $contactNamemsg = ""; }
|
|
| 41 |
+ $restrictmsg .= $contactNamemsg . " "; |
|
| 42 |
+ |
|
| 43 |
+ $contactDesc = $contactsData[$i][1]; |
|
| 44 |
+ if (preg_match('/[^A-Za-z0-9\s\-\_\.\,\'\"\(\)\!\?\@\%\&\*]/', $contactDesc)) {
|
|
| 45 |
+ $contactDescmsg = "The 'Title / Description' field contains characters that are not allowed."; |
|
| 46 |
+ } else { $contactDescmsg = ""; }
|
|
| 47 |
+ $restrictmsg .= $contactDescmsg . " "; |
|
| 48 |
+ |
|
| 49 |
+ $contactLongDesc = $contactsData[$i][2]; |
|
| 50 |
+ if (preg_match('/[^A-Za-z0-9\s\-\_\.\,\'\"\(\)\!\?\@\%\&\*]/', $contactLongDesc)) {
|
|
| 51 |
+ $contactLongDescmsg = "The 'Long Description' field contains characters that are not allowed."; |
|
| 52 |
+ } else { $contactLongDescmsg = ""; }
|
|
| 53 |
+ $restrictmsg .= $contactLongDescmsg . " "; |
|
| 54 |
+ |
|
| 55 |
+ $addressStreet = $contactsData[$i][3]; |
|
| 56 |
+ if (preg_match('/[^A-Za-z0-9\s\-\_\.\,\'\"\(\)\#\%\&]/', $addressStreet)) {
|
|
| 57 |
+ $addressStreetmsg = "The 'Address - Street' field contains characters that are not allowed."; |
|
| 58 |
+ } else { $addressStreetmsg = ""; }
|
|
| 59 |
+ $restrictmsg .= $addressStreetmsg . " "; |
|
| 60 |
+ |
|
| 61 |
+ $addressZip = $contactsData[$i][4]; |
|
| 62 |
+ if (preg_match('/[^A-Za-z0-9\s\-\_\.\(\)\#\&]/', $addressZip)) {
|
|
| 63 |
+ $addressZipmsg = "The 'Address - Zip' field contains characters that are not allowed."; |
|
| 64 |
+ } else { $addressZipmsg = ""; }
|
|
| 65 |
+ $restrictmsg .= $addressZipmsg . " "; |
|
| 66 |
+ |
|
| 67 |
+ $addressTown = $contactsData[$i][5]; |
|
| 68 |
+ if (preg_match('/[^A-Za-z0-9\s\-\_\.\,\(\)\&]/', $addressTown)) {
|
|
| 69 |
+ $addressTownmsg = "The 'Address - Town' field contains characters that are not allowed."; |
|
| 70 |
+ } else { $addressTownmsg = ""; }
|
|
| 71 |
+ $restrictmsg .= $addressTownmsg . " "; |
|
| 72 |
+ |
|
| 73 |
+ $addressCountry = $contactsData[$i][6]; |
|
| 74 |
+ if (preg_match('/[^A-Za-z0-9\s\-\.\,\(\)\&]/', $addressCountry)) {
|
|
| 75 |
+ $addressCountrymsg = "The 'Address - Country' field contains characters that are not allowed."; |
|
| 76 |
+ } else { $addressCountrymsg = ""; }
|
|
| 77 |
+ $restrictmsg .= $addressCountrymsg . " "; |
|
| 78 |
+ |
|
| 79 |
+ $addressState = $contactsData[$i][7]; |
|
| 80 |
+ if (preg_match('/[^A-Za-z0-9\s\-\.\,\(\)\&]/', $addressState)) {
|
|
| 81 |
+ $addressStatemsg = "The 'Address - State/Province' field contains characters that are not allowed."; |
|
| 82 |
+ } else { $addressStatemsg = ""; }
|
|
| 83 |
+ $restrictmsg .= $addressStatemsg . " "; |
|
| 84 |
+ |
|
| 85 |
+ $extensionNumber = $contactsData[$i][8]; |
|
| 86 |
+ if (preg_match('/[^A-Za-z0-9\*\#]/', $extensionNumber)) {
|
|
| 87 |
+ $extensionNumbermsg = "The 'Extension (Internal)' field contains characters that are not allowed."; |
|
| 88 |
+ } else { $extensionNumbermsg = ""; }
|
|
| 89 |
+ $restrictmsg .= $extensionNumbermsg . " "; |
|
| 90 |
+ |
|
| 91 |
+ $contactMobile = $contactsData[$i][9]; |
|
| 92 |
+ if (preg_match('/[^0-9\s\+\-\.\#]/', $contactMobile)) {
|
|
| 93 |
+ $contactMobilemsg = "The 'Mobile Number' field contains characters that are not allowed."; |
|
| 94 |
+ } else { $contactMobilemsg = ""; }
|
|
| 95 |
+ $restrictmsg .= $contactMobilemsg . " "; |
|
| 96 |
+ |
|
| 97 |
+ $contactNum1 = $contactsData[$i][10]; |
|
| 98 |
+ if (preg_match('/[^0-9\s\+\-\.\#]/', $contactNum1)) {
|
|
| 99 |
+ $contactNum1msg = "The 'Contact Number 1' field contains characters that are not allowed."; |
|
| 100 |
+ } else { $contactNum1msg = ""; }
|
|
| 101 |
+ $restrictmsg .= $contactNum1msg . " "; |
|
| 102 |
+ |
|
| 103 |
+ $contactNum2 = $contactsData[$i][11]; |
|
| 104 |
+ if (preg_match('/[^0-9\s\+\-\.\#]/', $contactNum2)) {
|
|
| 105 |
+ $contactNum2msg = "The 'Contact Number 2' field contains characters that are not allowed."; |
|
| 106 |
+ } else { $contactNum2msg = ""; }
|
|
| 107 |
+ $restrictmsg .= $contactNum2msg . " "; |
|
| 108 |
+ |
|
| 109 |
+ $contact_fax = $contactsData[$i][12]; |
|
| 110 |
+ if (preg_match('/[^0-9\s\+\-\.\#]/', $contact_fax)) {
|
|
| 111 |
+ $contact_faxmsg = "The 'Fax Number' field contains characters that are not allowed."; |
|
| 112 |
+ } else { $contact_faxmsg = ""; }
|
|
| 113 |
+ $restrictmsg .= $contact_faxmsg . " "; |
|
| 114 |
+ |
|
| 115 |
+ $contact_email = $contactsData[$i][13]; |
|
| 116 |
+ if (filter_var($contact_email, FILTER_VALIDATE_EMAIL) || $contact_email == '') {
|
|
| 117 |
+ $contact_emailmsg = ""; |
|
| 118 |
+ } else { $contact_emailmsg = "The email address in the 'Email' field is not valid."; }
|
|
| 119 |
+ $restrictmsg .= $contact_emailmsg . " "; |
|
| 120 |
+ |
|
| 121 |
+ $profilePicture = $contactsData[$i][14]; |
|
| 122 |
+ if (preg_match('/[^A-Za-z0-9\+\/\=\;\:\,]/', $profilePicture)) {
|
|
| 123 |
+ $profilePicturemsg = "The 'Profile Picture' field contains characters that are not allowed."; |
|
| 124 |
+ } else { $profilePicturemsg = ""; }
|
|
| 125 |
+ $restrictmsg .= $profilePicturemsg . " "; |
|
| 126 |
+ |
|
| 127 |
+ $groups = $contactsData[$i][15]; |
|
| 128 |
+ if (preg_match('/[^A-Za-z0-9\s\-\|]/', $groups)) {
|
|
| 129 |
+ $groupsmsg = "The 'Groups' field contains characters that are not allowed."; |
|
| 130 |
+ } else { $groupsmsg = ""; }
|
|
| 131 |
+ $restrictmsg .= $groupsmsg . " "; |
|
| 132 |
+ |
|
| 133 |
+ $dateAdded = date("Y-m-d H:i:s");
|
|
| 134 |
+ $dateModified = $dateAdded; |
|
| 135 |
+ |
|
| 136 |
+ if (ctype_space($restrictmsg)) {
|
|
| 137 |
+ |
|
| 138 |
+ // Check if the contact is already in the 'contacts' table |
|
| 139 |
+ $queryck = $mysqli->prepare("SELECT id, user_id, contact_name FROM contacts WHERE user_id = ? AND contact_name = ?");
|
|
| 140 |
+ $queryck->bind_param("is", $userID, $contactName);
|
|
| 141 |
+ $queryck->execute(); |
|
| 142 |
+ $fetchInfo = $queryck->get_result()->fetch_assoc(); |
|
| 143 |
+ |
|
| 144 |
+ if (!$fetchInfo) {
|
|
| 145 |
+ |
|
| 146 |
+ $querync = $mysqli->prepare("INSERT INTO contacts (user_id, contact_name, contact_desc, contact_long_desc, address_street, address_zip, address_town, address_country,
|
|
| 147 |
+ address_state, extension_number, contact_mobile, contact_num1, contact_num2, contact_fax, contact_email, profile_picture_c, groups, |
|
| 148 |
+ date_added, date_modified) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); |
|
| 149 |
+ $querync->bind_param("issssssssssssssssss", $userID, $contactName, $contactDesc, $contactLongDesc, $addressStreet, $addressZip, $addressTown, $addressCountry,
|
|
| 150 |
+ $addressState, $extensionNumber, $contactMobile, $contactNum1, $contactNum2, $contact_fax, $contact_email, $profilePicture, $groups, $dateAdded, |
|
| 151 |
+ $dateModified); |
|
| 152 |
+ |
|
| 153 |
+ if ($querync->execute()) { /* OK */ } else { $insertcheck++; }
|
|
| 154 |
+ |
|
| 155 |
+ } else { $duplicate++; }
|
|
| 156 |
+ |
|
| 157 |
+ } else { break; }
|
|
| 158 |
+ |
|
| 159 |
+ } |
|
| 160 |
+ |
|
| 161 |
+ // Reindex the 'contacts' table |
|
| 162 |
+ $reindexset = $mysqli->prepare("SET @resetrec = 0");
|
|
| 163 |
+ $reindexup = $mysqli->prepare("UPDATE contacts SET id = @resetrec := @resetrec + 1");
|
|
| 164 |
+ $reindexalt = $mysqli->prepare("ALTER TABLE contacts auto_increment = 1");
|
|
| 165 |
+ if ($reindexset->execute() && $reindexup->execute() && $reindexalt->execute()) { $reindexmsg = ""; } else { $reindexmsg = "The 'contacts' table couldn't be reindexed!"; }
|
|
| 166 |
+ |
|
| 167 |
+ if (ctype_space($restrictmsg)) { $restrictmsgproc = ""; } else { $restrictmsgproc = $restrictmsg; }
|
|
| 168 |
+ |
|
| 169 |
+ if ($insertcheck == 0) { $insertmsg = ""; } else { $insertmsg = "At least one error while trying to insert contacts into the database!"; }
|
|
| 170 |
+ |
|
| 171 |
+ if ($duplicate == 0) { $duplicatemsg = ""; } else { $duplicatemsg = "Some of the contacts that you have tried to import were already in the database."; }
|
|
| 172 |
+ |
|
| 173 |
+ $response = array('restrict' => $restrictmsgproc, 'reindex' => $reindexmsg, 'insert' => $insertmsg, 'duplicate' => $duplicatemsg);
|
|
| 174 |
+ |
|
| 175 |
+ echo json_encode($response); |
|
| 176 |
+ |
|
| 177 |
+} else {
|
|
| 178 |
+ header("Location: ../login.php");
|
|
| 179 |
+} |
|
| 180 |
+ |
|
| 181 |
+?> |