aMember 3 Intergration with UAC

This guide should assist some of you who are attempting to integrate a custom PHP script with aMember and to check UAC level.

The first part, is to modify your aMember plugin (custom) to allow the user to enter which products that plugin applies. This is the simplest method. For example purposes, we will use the plugin named “plugin_template”.

 


amember/plugins/protect/plugin_template/config.inc.php

1
2
3
4
5
6
7
add_config_field(
    'protect.plugin_template.products',
    'Product ID\'s',
    'text',
    "Enter the product ID to create access for. For multiple products, use the ; (semicolon). [example: 19;21;22 ]",
    $notebook_page
);

 


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
93
94
95
96
97
98
99
100
101
include("amember/config.inc.php"); // Include the aMember CONFIG file
$plugin    =   $config['protect']['plugin_template'];
$dbconf     =   $config['db']['mysql'];
$sql        =   '';
$expires    =   null;
$products   =   Array();
$member_id  =   0;

// Check if logged in and get aMember user id
if(isset($_SESSION['_admin_login'])) {
    // If user logged in as admin, set aMember user id to 0 --- for use later
    $member_id = -1;
} elseif ( isset($_SESSION['_amember_user']['member_id']) ) {
    // Get member_id from session
    $member_id = $_SESSION['_amember_user']['member_id'];
}

switch($member_id) {
  case -1:
    // User is Admin
    echo "Logged in as <b>" . $_SESSION['_admin_login'] . "</b> which has administrator access.";
    break;

  case 0;
    // Not logged in
    echo sprintf('<a href="%s">Click here to Login</a>', $config['root_surl'] . "/member.php");
    break;

  default:
    // Logged in as a member

    // Get products allowed from aMember Plugin Settings
    if(strpos($plugin['products'],";") > 0) {
      $products   = explode(";", $plugin['products']);
     else {
      $products[] = $plugin['products'];
   

    // Build query to check aMember payments for non-expired subscription
    foreach($products as $product) {
      if($sql <> '')  $sql .= ' OR ';
      $sql .= '`product_id`=' . $product;
   

    // Get expiry date of most recent supported product for member
    $sql = "SELECT `expire_date` FROM `" . $dbconf['prefix'] . "payments` WHERE (`member_id`=" . $member_id . ") AND (" . $sql . ") AND ( `completed` > 0 ) ORDER BY `expire_date` DESC LIMIT 1;";
    $results = $db->query($sql);
    if($results) {
      if($row = mysql_fetch_assoc($results)) {
        $expires = $row['expire_date'];
      }
    }

    // Compare expiry date with current date
    if($expires >= date('Y-m-d')) {
      // Display member content here
      echo "Your membership expires" . DateExpires($expires) . ".";
    } else {
      // Access to paid area has expired or membership never existed
      if($expired . "" <> "") {
        // Membership expired
        echo "Your membership expired" . DateExpired($expires) . ".";
      } else {
        // Membership did not exist
        echo "You have never had a membership.";
      }
    }
    break;
}

function DateExpired($date) {
  $date1 = new DateTime($date);
  $date2 = new DateTime(date('Y-m-d'));
  $interval = $date2->diff($date1);
  $output = '';
  if($interval->d > 0 || $interval->m > 0 || $interval->y > 0 ) {
    $output = $interval->d." days";
    if($interval->m > 0 || $interval->y > 0) { $output = $interval->m." months, " . $output; }
    if($interval->y > 0) { $output = $interval->m." years, " . $output; }
    $output = ' ' . $output . ' ago.';
  } else {
    $output = ' Today';
  }
  return $output;
}

function DateExpires($date) {
  $date1 = new DateTime($date);
  $date2 = new DateTime(date('Y-m-d'));
  $interval = $date1->diff($date2);
  $output = '';
  if($interval->d > 0 || $interval->m > 0 || $interval->y > 0 ) {
    $output = $interval->d." days";
    if($interval->m > 0 || $interval->y > 0) { $output = $interval->m." months, " . $output; }
    if($interval->y > 0) { $output = $interval->m." years, " . $output; }
    $output = ' in ' . $output;
  } else {
    $output = ' Today';
  }
  return $output;
}

Your script to protect the page. Please read inline comments for where to place your code.

 


And thats all there is to it. Administrators of aMember should now be able to add their products by going to aMember CP > Plugins > plugin_template by entering the product_id which shows up on the left most column of every product created. For multiple products, they would use a semicolon ( ; ) between the product id’s (these instructions are in the comment field for the new field).

Now you can focus on coding the application without worrying about the aMember integration side of things by using this code. For more advanced scenarios we are still available for custom solutions.