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
@ -34,7 +38,8 @@ class API_Model extends CI_Model {
$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');
@ -42,14 +47,16 @@ class API_Model extends CI_Model {
return $query->result_array()[0]; return $query->result_array()[0];
} }
function key_userid($key) { function key_userid($key)
{
$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]['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),
@ -58,17 +65,18 @@ class API_Model extends CI_Model {
$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)
@ -83,10 +91,10 @@ class API_Model extends CI_Model {
$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,18 +116,20 @@ class API_Model extends CI_Model {
} }
} }
function authorize($key) { function authorize($key)
{
$r = $this->access($key); $r = $this->access($key);
if($r == "rw") { if ($r == "rw") {
return 2; return 2;
} else if($r == "r") { } else if ($r == "r") {
return 1; return 1;
} else { } else {
return 0; return 0;
} }
} }
function update_last_used($key) { function update_last_used($key)
{
$this->db->set('last_used', 'NOW()', FALSE); $this->db->set('last_used', 'NOW()', FALSE);
$this->db->where('key', xss_clean($key)); $this->db->where('key', xss_clean($key));
$this->db->update('api'); $this->db->update('api');
@ -129,12 +139,9 @@ class API_Model extends CI_Model {
// 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' => ''),
); );
} }
?>