Browse Source

Add comments/documentation

hi
Skylar 3 years ago
parent
commit
eadb83ceb1
11 changed files with 55 additions and 21 deletions
  1. 4
    1
      database.php
  2. 3
    1
      dieifnotloggedin.php
  3. 3
    1
      getitem.php
  4. 3
    1
      getlocation.php
  5. 3
    1
      getmodel.php
  6. 3
    1
      getstatus.php
  7. 4
    1
      login.php
  8. 4
    1
      readfrom.php
  9. 19
    10
      required.php
  10. 4
    1
      search.php
  11. 5
    2
      updateitem.php

+ 4
- 1
database.php View File

@@ -1,5 +1,8 @@
<?php

/**
* Database configuration. If running on the same machine as snipeit,
* you can probably use the same settings here.
*/
$database = new medoo(
[
'database_type' => 'mysql',

+ 3
- 1
dieifnotloggedin.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Simple way to block people that aren't logged in.
*/
require_once 'required.php';

if ($_SESSION['loggedin'] !== true || is_empty($_SESSION['user'])) {

+ 3
- 1
getitem.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Dump a database row for a given asset/accessory/consumable
*/
require 'required.php';
require 'dieifnotloggedin.php';


+ 3
- 1
getlocation.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Get a list of all locations, and the location for a given id if any.
*/
require 'required.php';
//require 'dieifnotloggedin.php';


+ 3
- 1
getmodel.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Get all the models.
*/
require 'required.php';
//require 'dieifnotloggedin.php';


+ 3
- 1
getstatus.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Get all status types.
*/
require 'required.php';
//require 'dieifnotloggedin.php';


+ 4
- 1
login.php View File

@@ -1,7 +1,10 @@
<?php

/**
* Handles login requests, authentication, and session creation.
*/
require 'required.php';

// For lazy browser testing
//$_POST = $_GET;

$user = $_POST['user'];

+ 4
- 1
readfrom.php View File

@@ -1,5 +1,8 @@
<?php

/**
* Filter table info to sane choices. You could add aliases if you like.
* The app uses all of these variations because I'm lazy and just pass label text.
*/
switch ($from) {
case 'accessories':
case 'accessory':

+ 19
- 10
required.php View File

@@ -1,12 +1,16 @@
<?php

ob_start();
ob_start(); // No worries about sending headers before/after content
session_start();
require 'vendor/autoload.php';
require 'database.php';

define('JSON', true);
header('Content-Type: application/json');
require 'vendor/autoload.php'; // Load database stuff from Composer
require 'database.php'; // Load database settings

define('JSON', true); // Don't touch this or Something Bad might happen.
header('Content-Type: application/json'); // Don't touch this either.

// Completely disable CORS stuff, everything is allowed. You could change this
// if you know exactly what domain traffic is coming from.
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Credentials: true');

@@ -39,11 +43,6 @@ function authenticate_user($username, $password) {
global $database;
$qf = 'username';
if (!username_exists($username)) {
// if (!email_exists($username)) {
// return false;
// } else {
// $qf = 'email';
// }
return false;
}
$hash = $database->select('users', ['password'], [$qf => $username])[0]['password'];
@@ -59,6 +58,11 @@ function is_empty($str) {
return (!isset($str) || $str == '' || $str == null);
}

/**
* Send a generic OK message.
* @param string $message Optional message text.
* @param boolean $die End execution after sending message (default true).
*/
function sendOK($message = "", $die = true) {
if (!is_empty($message) && JSON) {
echo '{ "status": "OK", "message": "' . $message . '" }';
@@ -74,6 +78,11 @@ function sendOK($message = "", $die = true) {
}
}

/**
* Send an error message.
* @param string $error Error text.
* @param boolean $die End execution after sending error (default true).
*/
function sendError($error, $die = true) {
if (JSON) {
echo '{ "status": "ERROR", "message": "' . $error . '" }';

+ 4
- 1
search.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Search for a given term (q=) in a given category (from=) and spit out JSON.
*/
require 'required.php';

require 'dieifnotloggedin.php';
@@ -13,6 +15,7 @@ if (is_empty($q)) {
}

$results;
// If you want to search through more/different fields, just add them.
if ($from == 'assets') {
$results = $database->select($from, '*', ['OR' => ['name[~]' => $q, 'asset_tag[~]' => $q, 'serial[~]' => $q, 'order_number[~]' => $q]]);
} else {

+ 5
- 2
updateitem.php View File

@@ -1,5 +1,7 @@
<?php

/**
* Create/update an item.
*/
require 'required.php';
require 'dieifnotloggedin.php';

@@ -12,6 +14,7 @@ if (is_empty($_POST['asset_tag']) && $from == 'assets') {
}

if (is_empty($id)) {
// We need to create an item
if ($from == 'assets') {
$user_id = $database->select('users', 'id', ['username' => $_SESSION['user']])[0];
$database->insert($from, ['name' => $_POST['name'], 'user_id' => $user_id, 'asset_tag' => $_POST['asset_tag'], 'rtd_location_id' => $_POST['location'], 'order_number' => $_POST['order_number'], 'status_id' => $_POST['status'], 'serial' => $_POST['serial'], 'model_id' => $_POST['model'], '#updated_at' => 'NOW()', '#created_at' => 'NOW()', '_snipeit_hard_drive_secure__y_n_' => $_POST['hdd_secure']]);
@@ -19,7 +22,7 @@ if (is_empty($id)) {
$database->insert($from, ['name' => $_POST['name'], 'location_id' => $_POST['location'], 'qty' => $_POST['qty'], 'order_number' => $_POST['order_number'], '#updated_at' => 'NOW()', '#created_at' => 'NOW()']);
}
} else {
// Update an existing item by id
if ($from == 'assets') {
$database->update($from, ['name' => $_POST['name'], 'asset_tag' => $_POST['asset_tag'], 'rtd_location_id' => $_POST['location'], 'order_number' => $_POST['order_number'], 'status_id' => $_POST['status'], 'serial' => $_POST['serial'], 'model_id' => $_POST['model'], '#updated_at' => 'NOW()', '_snipeit_hard_drive_secure__y_n_' => $_POST['hdd_secure']], ['id' => $id]);
} else {

Loading…
Cancel
Save