Cleaned up formatting of api model

这个提交包含在:
Peter Goodhall 2024-11-21 16:30:12 +00:00
父节点 1729aaaa69
当前提交 6bffd4a1c1

查看文件

@ -6,21 +6,25 @@
* *
*/ */
class API_Model extends CI_Model { class API_Model extends CI_Model
{
// GET API Keys // GET API Keys
function keys() { function keys()
{
$this->db->where('user_id', $this->session->userdata('user_id')); $this->db->where('user_id', $this->session->userdata('user_id'));
return $this->db->get('api'); return $this->db->get('api');
} }
function CountKeysWithNoUserID() { function CountKeysWithNoUserID()
{
$this->db->where('user_id =', NULL); $this->db->where('user_id =', NULL);
$query = $this->db->get('api'); $query = $this->db->get('api');
return $query->num_rows(); return $query->num_rows();
} }
function ClaimAllAPIKeys($id = NULL) { function ClaimAllAPIKeys($id = NULL)
{
// if $id is empty then use session user_id // if $id is empty then use session user_id
if (empty($id)) { if (empty($id)) {
// Get the first USER ID from user table in the database // Get the first USER ID from user table in the database
@ -28,65 +32,69 @@ class API_Model extends CI_Model {
} }
$data = array( $data = array(
'user_id' => $id, 'user_id' => $id,
); );
$this->db->update('api', $data); $this->db->update('api', $data);
} }
function key_description($key) { function key_description($key)
{
$this->db->where('user_id', $this->session->userdata('user_id')); $this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->where('key', $key); $this->db->where('key', $key);
$query = $this->db->get('api'); $query = $this->db->get('api');
return $query->result_array()[0]; return $query->result_array()[0];
} }
function key_userid($key) { function key_userid($key)
$this->db->where('key', $key); {
$query = $this->db->get('api'); $this->db->where('key', $key);
$query = $this->db->get('api');
return $query->result_array()[0]['user_id']; return $query->result_array()[0]['user_id'];
} }
function update_key_description($key, $description) { function update_key_description($key, $description)
{
$data = array( $data = array(
'description' => xss_clean($description), 'description' => xss_clean($description),
); );
$this->db->where('key', xss_clean($key)); $this->db->where('key', xss_clean($key));
$this->db->where('user_id', $this->session->userdata('user_id')); $this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->update('api', xss_clean($data)); $this->db->update('api', xss_clean($data));
}
}
function delete_key($key) { function delete_key($key)
{
$this->db->where('user_id', $this->session->userdata('user_id')); $this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->where('key', xss_clean($key)); $this->db->where('key', xss_clean($key));
$this->db->delete('api'); $this->db->delete('api');
} }
// Generate API Key // Generate API Key
function generate_key($rights) { function generate_key($rights)
{
// Expects either rw (Read, Write) or r (read only) // Expects either rw (Read, Write) or r (read only)
// Generate Unique Key // Generate Unique Key
$data['key'] = uniqid("cl"); $data['key'] = uniqid("cl");
$data['rights'] = $rights; $data['rights'] = $rights;
// Set API key to active // Set API key to active
$data['status'] = "active"; $data['status'] = "active";
$data['user_id'] = $this->session->userdata('user_id'); $data['user_id'] = $this->session->userdata('user_id');
$this->db->insert('api', $data); $this->db->insert('api', $data);
}
} function access($key)
{
function access($key) {
// No key = no access // No key = no access
if (!$key) { if (!$key) {
return "No Key Found"; return "No Key Found";
@ -108,33 +116,32 @@ class API_Model extends CI_Model {
} }
} }
function authorize($key) { function authorize($key)
$r = $this->access($key); {
if($r == "rw") { $r = $this->access($key);
return 2; if ($r == "rw") {
} else if($r == "r") { return 2;
return 1; } else if ($r == "r") {
} else { return 1;
return 0; } else {
} return 0;
} }
}
function update_last_used($key) { function update_last_used($key)
$this->db->set('last_used', 'NOW()', FALSE); {
$this->db->where('key', xss_clean($key)); $this->db->set('last_used', 'NOW()', FALSE);
$this->db->update('api'); $this->db->where('key', xss_clean($key));
} $this->db->update('api');
}
// FUNCTION: string name(string $column) // FUNCTION: string name(string $column)
// Converts a MySQL column name to a more friendly name // Converts a MySQL column name to a more friendly name
function name($col) function name($col)
{ {
if($this->_columnName[$col]) if ($this->_columnName[$col]) {
{
return $this->_columnName[$col]['Name']; return $this->_columnName[$col]['Name'];
} } else {
else
{
return 0; return 0;
} }
} }
@ -143,19 +150,13 @@ class API_Model extends CI_Model {
// Returns the description for a MySQL column name // Returns the description for a MySQL column name
function description($col) function description($col)
{ {
if($this->_columnName[$col]) if ($this->_columnName[$col]) {
{ if ($this->_columnName[$col]['Description'] != "") {
if($this->_columnName[$col]['Description'] != "")
{
return $this->_columnName[$col]['Description']; return $this->_columnName[$col]['Description'];
} } else {
else
{
return "No description available"; return "No description available";
} }
} } else {
else
{
return 0; return 0;
} }
} }
@ -164,10 +165,8 @@ class API_Model extends CI_Model {
// Converts a friendly name to a MySQL column name // Converts a friendly name to a MySQL column name
function column($name) function column($name)
{ {
while ($column = current($this->_columnName)) while ($column = current($this->_columnName)) {
{ if ($this->_columnName[key($this->_columnName)]['Name'] == $name) {
if($this->_columnName[key($this->_columnName)]['Name'] == $name)
{
$a = key($this->_columnName); $a = key($this->_columnName);
reset($this->_columnName); reset($this->_columnName);
return $a; return $a;
@ -303,7 +302,4 @@ class API_Model extends CI_Model {
'COL_CREDIT_GRANTED' => array('Name' => 'UNK_CREDIT_GRANTED', 'Description' => '', 'Type' => ''), 'COL_CREDIT_GRANTED' => array('Name' => 'UNK_CREDIT_GRANTED', 'Description' => '', 'Type' => ''),
'COL_CREDIT_SUBMITTED' => array('Name' => 'UNK_CREDIT_SUBMITTED', 'Description' => '', 'Type' => ''), 'COL_CREDIT_SUBMITTED' => array('Name' => 'UNK_CREDIT_SUBMITTED', 'Description' => '', 'Type' => ''),
); );
} }
?>