forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductRegistrationService.php
92 lines (79 loc) · 2.91 KB
/
ProductRegistrationService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* ProductRegistrationService
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Matthew Vita <[email protected]>
* @author Victor Kofia <[email protected]>
* @copyright Copyright (c) 2017 Matthew Vita <[email protected]>
* @copyright Copyright (c) 2017 Victor Kofia <[email protected]>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
require_once($GLOBALS['fileroot'] . "/interface/product_registration/exceptions/generic_product_registration_exception.php");
class ProductRegistrationService
{
/**
* Default constructor.
*/
public function __construct()
{
}
public function getProductStatus()
{
$row = sqlQuery("SELECT * FROM `product_registration`");
if (!empty($row)) {
$email = $row['email'];
$optOut = $row['opt_out'];
}
if (empty($row)) {
$row = ['statusAsString' => 'UNREGISTERED'];
} elseif (!empty($email)) {
$row['statusAsString'] = 'REGISTERED';
} elseif (!empty($optOut) && $optOut == 1) {
$row['statusAsString'] = 'OPT_OUT';
}
return $row;
}
public function registerProduct($email)
{
if (!$email || $email == 'false') {
$this->optOutStrategy();
return null;
} else {
return $this->optInStrategy($email);
}
}
private function optInStrategy($email)
{
// build the information array
$info = ['email' => $email, 'version' => (new VersionService())->asString()];
if (!empty(getenv('OPENEMR_DOCKER_ENV_TAG', true))) {
// this will add standard package information if it exists
$info['distribution'] = getenv('OPENEMR_DOCKER_ENV_TAG', true);
}
$curl = curl_init('https://reg.open-emr.org/api/registration');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($info));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$responseBodyRaw = curl_exec($curl);
$responseCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
switch ($responseCode) {
case 201:
sqlStatement("INSERT INTO `product_registration` (`email`, `opt_out`) VALUES (?, 0)", [$email]);
return $email;
break;
default:
throw new \GenericProductRegistrationException(xl("Server error: try again later"));
}
}
// void... don't bother checking for success/failure.
private function optOutStrategy()
{
sqlStatement("INSERT INTO `product_registration` (`email`, `opt_out`) VALUES (null, 1)");
}
}