A number of minor fixes
[PHPCredLocker.git] / lib / API.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/** API call Handler
*
* Copyright (C) 2012 B Tasker
* Released under GNU GPL V2
* See LICENSE
*
*/
defined('_CREDLOCK') or die;
 
ob_start();
 
require_once 'lib/lang.php';
require_once 'lib/auth.class.php';
require_once 'lib/db/loggingdb.class.php';
require_once 'lib/plugins.php';
require_once 'lib/crypto.php';
 
$plg = new Plugins;
$crypt = new Crypto;
 
/**
 Implemented so that we can treat the divider as a key and reduce the likelihood/effectiveness 
 of a known plaintext attack by changing it occasionally throughout the session. Just need to 
 work out a good mechanism for doing so first!
*/
$opDivider = "|..|";
 
 
 
 // See if the user has an active session (must have to continue)
    if (BTMain::getsessVar('Session')){
    $auth = new ProgAuth;
    $auth->SetUserDets(BTMain::getsessVar('Session'));
    }
 
 
   if (empty(BTMain::getUser()->name)){
    
    ob_end_flush();
    $op = BTMain::getip().$opDivider."0".$opDivider."Access Denied".$opDivider;
 
 
    if (!BTMain::getConnTypeSSL()){
    $op = base64_encode($crypt->xorestring(base64_encode($op),$tlskey));
    }
 
    echo $op;
    die;
    }
   
 
 
echo "1".$opDivider;
 
// Decrypt the request
 
$option = BTMain::getVar('option');
 
 
 
 
        if (!BTMain::getConnTypeSSL()){
            $tlskey = BTMain::getsessVar('tls');
            $option = base64_decode($crypt->xordstring(base64_decode($option),$tlskey));
         }
 
 
$option = explode($opDivider,$option);
 
$terms = BTMain::getSessVar('apiterms');
 
$option = $terms[$option[1]];
 
 
switch($option){
 
 
case 'retCred':
    require_once 'lib/db/Credentials.php';
 
    $db = new CredDB;
    $cred = $db->FetchCredential(BTMain::getVar('id'));
 
    
    $crypt->safety = 0;
 
    $key = 'Cre'.$cred->CredType;
 
    // Build the response
    $pass = htmlspecialchars($crypt->decrypt($cred->Hash,$key));
    $address = htmlspecialchars($crypt->decrypt($cred->Address,$key));
    $uname = htmlspecialchars($crypt->decrypt($cred->UName,$key));
 
      if ($cred->Clicky){
          $pass = "<a href='$pass' target=_blank title='Click to Open'>$pass</a>";
      }
 
 
    echo $pass.$opDivider."<a href='$address' target=_blank>".$address."</a>" .$opDivider. 
         $uname . $opDivider;
 
 
    // Call any configured plugins
     $data->cred = $cred;
     $data->cred->id = BTMain::getVar('id');
     $data->action = 'display';
 
    
    echo $plg->loadPlugins("Creds",$data)->plgOutput;
 
   
    break;
 
 
 
case 'checkSess':
    echo "OK";
    break;
 
 
case 'delCred':
    require_once 'lib/db/Credentials.php';
    $db = new CredDB;
    if ( $db->DelCredential(BTMain::getVar('id'))){
        echo "1$opDivider\n";
        }else{
        echo "0$opDivider\n";
        }
    break;
 
 
 
case 'delUser':
    BTMain::checkSuperAdmin();
    $db = new AuthDB;
      if ( $db->DelUser(BTMain::getVar('id'))){
          echo "1$opDivider\n";
      }else{
          echo "0$opDivider\n";
      }
    break;
 
 
 
case 'delCredType':
      BTMain::checkSuperAdmin();
      require_once 'lib/db/Credentials.php';
      $db = new CredDB;
            if ( $db->DelCredentialType(BTMain::getVar('id'))) {
                $data->id = BTMain::getVar('id');
                $data->action = 'del';
                echo $plg->loadPlugins("CredTypes",$data)->plgOutput;
 
                echo "1$opDivider\n";
            }else{
                echo "0$opDivider|\n";
            }
      break;
 
 
 
 
case 'delCust':
    require_once 'lib/db/Customer.php';
    $db = new CustDB;
      if ( $db->DelCust(BTMain::getVar('id'))){
          echo "1$opDivider\n";
      }else{
          echo "0$opDivider\n";
      }
    break;
 
 
 
 
case 'delGroup':
    BTMain::checkSuperAdmin();
    $auth = new AuthDB;
 
        if($auth->delGroup(BTMain::getVar('id'))){
           echo "1$opDivider\n";
        }else{  
          echo "0$opDivider\n";
        }
    break;
 
 
 
default:
  ob_clean();
  echo "2".$opDivider;
  break;
 
}
 
 
// Encrypt the output and send back
$padding = $crypt->genXorPadding();
$endpadding = $crypt->genXorPadding();
 
$op = $padding.$opDivider.ob_get_clean().$opDivider.$endpadding;
 
if (!BTMain::getConnTypeSSL()){
$op = base64_encode($crypt->xorestring(base64_encode($op),$tlskey));
}
 
echo $op;
 
ob_end_flush();
?>