1+ <?php
2+
3+ /**
4+ * FOSSBilling.
5+ *
6+ * @copyright FOSSBilling (https://www.fossbilling.org)
7+ * @license Apache-2.0
8+ *
9+ * Copyright FOSSBilling 2022
10+ * This software may contain code previously used in the BoxBilling project.
11+ * Copyright BoxBilling, Inc 2011-2021
12+ *
13+ * This source file is subject to the Apache-2.0 License that is bundled
14+ * with this source code in the file LICENSE
15+ */
16+
17+ namespace Box \Mod \Contact \Controller ;
18+
19+ class Client implements \FOSSBilling \InjectionAwareInterface
20+ {
21+ protected $ di ;
22+
23+ public function setDi (\Pimple \Container |null $ di ): void
24+ {
25+ $ this ->di = $ di ;
26+ }
27+
28+ public function getDi (): ?\Pimple \Container
29+ {
30+ return $ this ->di ;
31+ }
32+
33+ /**
34+ * Methods maps client areas urls to corresponding methods
35+ * Always use your module prefix to avoid conflicts with other modules
36+ * in future.
37+ *
38+ * @param \Box_App $app - returned by reference
39+ */
40+ public function register (\Box_App &$ app ): void
41+ {
42+ $ app ->get ('/contact ' , 'get_index ' , [], static ::class);
43+ $ app ->post ('/contact ' , 'get_index ' , [], static ::class);
44+ }
45+
46+ public function get_index (\Box_App $ app )
47+ {
48+ // Initialize variables
49+ $ error = '' ;
50+ $ success = '' ;
51+
52+ // Capture GET parameter and sanitize the domain
53+ $ domain = filter_input (INPUT_GET , 'domain ' , FILTER_SANITIZE_FULL_SPECIAL_CHARS );
54+
55+ // Process form submission if it's a POST request
56+ if ($ _SERVER ['REQUEST_METHOD ' ] === 'POST ' ) {
57+ // Capture and sanitize form data
58+ $ name = filter_input (INPUT_POST , 'name ' , FILTER_SANITIZE_FULL_SPECIAL_CHARS );
59+ $ email = filter_input (INPUT_POST , 'email ' , FILTER_VALIDATE_EMAIL );
60+ $ message = filter_input (INPUT_POST , 'message ' , FILTER_SANITIZE_FULL_SPECIAL_CHARS );
61+
62+ // Validate form data
63+ if ($ name && $ email && $ message ) {
64+ // Define sender and recipient
65+ $ sender = [
66+ 'email ' => $ email ,
67+ 'name ' => $ name ,
68+ ];
69+ $ recipient = [
70+ 'email ' => 'registrant@example.com ' , // Replace with actual registrant's email
71+ 'name ' => 'Domain Registrant ' ,
72+ ];
73+
74+ // Get email settings
75+ $ mod = $ this ->di ['mod ' ]('email ' );
76+ $ settings = $ mod ->getConfig ();
77+ $ logEnabled = isset ($ settings ['log_enabled ' ]) && $ settings ['log_enabled ' ];
78+ $ transport = $ settings ['mailer ' ] ?? 'sendmail ' ;
79+
80+ // Prepare the email content and transport
81+ $ mail = new \FOSSBilling \Mail ($ sender , $ recipient , "Contact Domain Registrant: " . $ domain , $ message , $ transport , $ settings ['custom_dsn ' ] ?? null );
82+
83+ try {
84+ // Attempt to send the email
85+ $ mail ->send ($ settings );
86+
87+ // Log activity if enabled
88+ if ($ logEnabled ) {
89+ $ activityService = $ this ->di ['mod_service ' ]('activity ' );
90+ $ activityService ->logEmail ("Contact Domain Registrant: " . $ domain , null , $ email , 'registrant@example.com ' , $ message );
91+ }
92+ $ success = 'Your message has been sent successfully. ' ;
93+ } catch (\Exception $ e ) {
94+ $ error = 'Failed to send the message. Please try again later. ' ;
95+ $ this ->di ['logger ' ]->setChannel ('email ' )->err ($ e ->getMessage ());
96+ }
97+ } else {
98+ $ error = 'Please fill in all fields with valid information. ' ;
99+ }
100+ } else {
101+ // Existing GET logic for domain validation
102+ if ($ domain ) {
103+ $ parts = explode ('. ' , $ domain );
104+ $ sld = '' ;
105+ $ tld = '' ;
106+
107+ if (count ($ parts ) >= 3 ) {
108+ $ tld = implode ('. ' , array_slice ($ parts , -2 ));
109+ $ sld = $ parts [count ($ parts ) - 3 ];
110+ } elseif (count ($ parts ) === 2 ) {
111+ $ sld = $ parts [0 ];
112+ $ tld = $ parts [1 ];
113+ } else {
114+ $ error = "Error: Invalid domain format. " ;
115+ }
116+
117+ if ($ sld && $ tld ) {
118+ $ domainExists = $ this ->di ['db ' ]->getAll (
119+ 'SELECT * FROM service_domain WHERE sld = :sld AND tld = :tld ' ,
120+ ['sld ' => $ sld , 'tld ' => '. ' . $ tld ]
121+ );
122+
123+ if (!$ domainExists ) {
124+ $ error = "Error: The specified domain does not exist. " ;
125+ }
126+ } else {
127+ $ error = "Error: Unable to extract SLD and TLD from the domain. " ;
128+ }
129+ } else {
130+ $ error = "Error: You must specify a domain. " ;
131+ }
132+ }
133+
134+ return $ app ->render ('mod_contact_index ' , [
135+ 'domain ' => $ domain ,
136+ 'error ' => $ error ,
137+ 'success ' => $ success ,
138+ ]);
139+ }
140+
141+ }
0 commit comments