Browse Source

Add support for downloading reports from mobile app, add HTML export format

Skylar Ittner 1 year ago
parent
commit
879bd8d408
7 changed files with 125 additions and 8 deletions
  1. BIN
      database.mwb
  2. 34
    0
      database.sql
  3. 1
    0
      lang/en_us.php
  4. 58
    3
      lib/reports.php
  5. 8
    3
      pages/export.php
  6. 15
    1
      required.php
  7. 9
    1
      settings.template.php

BIN
database.mwb View File


+ 34
- 0
database.sql View File

@@ -0,0 +1,34 @@
1
+-- MySQL Script generated by MySQL Workbench
2
+-- Wed 11 Oct 2017 12:49:43 PM MDT
3
+-- Model: New Model    Version: 1.0
4
+-- MySQL Workbench Forward Engineering
5
+
6
+SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
7
+SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
8
+SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
9
+
10
+-- -----------------------------------------------------
11
+-- Schema managepanel
12
+-- -----------------------------------------------------
13
+
14
+-- -----------------------------------------------------
15
+-- Schema managepanel
16
+-- -----------------------------------------------------
17
+CREATE SCHEMA IF NOT EXISTS `managepanel` DEFAULT CHARACTER SET utf8 ;
18
+USE `managepanel` ;
19
+
20
+-- -----------------------------------------------------
21
+-- Table `managepanel`.`report_access_codes`
22
+-- -----------------------------------------------------
23
+CREATE TABLE IF NOT EXISTS `managepanel`.`report_access_codes` (
24
+  `id` INT NOT NULL AUTO_INCREMENT,
25
+  `code` VARCHAR(45) NULL,
26
+  `expires` DATETIME NULL,
27
+  PRIMARY KEY (`id`),
28
+  UNIQUE INDEX `id_UNIQUE` (`id` ASC))
29
+ENGINE = MEMORY;
30
+
31
+
32
+SET SQL_MODE=@OLD_SQL_MODE;
33
+SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
34
+SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

+ 1
- 0
lang/en_us.php View File

@@ -101,6 +101,7 @@ define("STRINGS", [
101 101
     "choose an option" => "Choose an option",
102 102
     "csv file" => "CSV text file",
103 103
     "ods file" => "ODS spreadsheet",
104
+    "html file" => "HTML web page",
104 105
     "uid" => "User ID",
105 106
     "manager name" => "Manager",
106 107
     "manager username" => "Mgr. Username",

+ 58
- 3
lib/reports.php View File

@@ -1,8 +1,16 @@
1 1
 <?php
2 2
 
3
+// Detect if loaded by the user or by PHP
4
+if (count(get_included_files()) == 1) {
5
+    define("LOADED", true);
6
+} else {
7
+    define("LOADED", false);
8
+}
9
+
3 10
 require_once __DIR__ . "/../required.php";
4 11
 
5 12
 use League\Csv\Writer;
13
+use League\Csv\HTMLConverter;
6 14
 use odsPhpGenerator\ods;
7 15
 use odsPhpGenerator\odsTable;
8 16
 use odsPhpGenerator\odsTableRow;
@@ -11,7 +19,27 @@ use odsPhpGenerator\odsTableCellString;
11 19
 use odsPhpGenerator\odsStyleTableColumn;
12 20
 use odsPhpGenerator\odsStyleTableCell;
13 21
 
14
-dieifnotloggedin();
22
+// Allow access with a one-time code, for mobile app and stuff
23
+if (isset($VARS['code']) && LOADED) {
24
+    $date = date("Y-m-d H:i:s");
25
+    if ($database2->has('report_access_codes', ["AND" => ['code' => $VARS['code'], 'expires[>]' => $date]])) {
26
+        $database2->delete('report_access_codes', ["OR" => ['code' => $VARS['code'], 'expires[<=]' => $date]]);
27
+    } else {
28
+        dieifnotloggedin();
29
+    }
30
+} else {
31
+    dieifnotloggedin();
32
+}
33
+
34
+if (LOADED) {
35
+    if (isset($VARS['type']) && isset($VARS['format'])) {
36
+        generateReport($VARS['type'], $VARS['format']);
37
+        die();
38
+    } else {
39
+        lang("invalid parameters");
40
+        die();
41
+    }
42
+}
15 43
 
16 44
 function getUserReport() {
17 45
     global $database;
@@ -144,7 +172,7 @@ function dataToCSV($data, $name = "report") {
144 172
     $csv = Writer::createFromString('');
145 173
     $csv->insertAll($data);
146 174
     header('Content-type: text/csv');
147
-    header('Content-Disposition: attachment; filename="' . $name . "_" . date("Y-m-d_Hm") . ".csv" . '"');
175
+    header('Content-Disposition: attachment; filename="' . $name . "_" . date("Y-m-d_Hi") . ".csv" . '"');
148 176
     echo $csv;
149 177
     die();
150 178
 }
@@ -175,7 +203,31 @@ function dataToODS($data, $name = "report") {
175 203
         $rowid++;
176 204
     }
177 205
     $ods->addTable($table);
178
-    $ods->downloadOdsFile($name . "_" . date("Y-m-d_Hm") . ".ods");
206
+    $ods->downloadOdsFile($name . "_" . date("Y-m-d_Hi") . ".ods");
207
+}
208
+
209
+function dataToHTML($data, $name = "report") {
210
+    header('Content-type: text/html');
211
+    $converter = new HTMLConverter();
212
+    $out = "<!DOCTYPE html>\n"
213
+            . "<meta charset=\"utf-8\">\n"
214
+            . "<meta name=\"viewport\" content=\"width=device-width\">\n"
215
+            . "<title>" . $name . "_" . date("Y-m-d_Hi") . "</title>\n"
216
+            . <<<STYLE
217
+<style>
218
+    .table-csv-data {
219
+        border-collapse: collapse;
220
+    }
221
+    .table-csv-data tr:first-child {
222
+        font-weight: bold;
223
+    }
224
+    .table-csv-data tr td {
225
+        border: 1px solid black;
226
+    }
227
+</style>
228
+STYLE
229
+            . $converter->convert($data);
230
+    echo $out;
179 231
 }
180 232
 
181 233
 function generateReport($type, $format) {
@@ -184,6 +236,9 @@ function generateReport($type, $format) {
184 236
         case "ods":
185 237
             dataToODS($data, $type);
186 238
             break;
239
+        case "html":
240
+            dataToHTML($data, $type);
241
+            break;
187 242
         case "csv":
188 243
         default:
189 244
             echo dataToCSV($data, $type);

+ 8
- 3
pages/export.php View File

@@ -4,12 +4,11 @@ require_once __DIR__ . '/../required.php';
4 4
 redirectifnotloggedin();
5 5
 ?>
6 6
 
7
-<form action="action.php" method="POST" target="_BLANK">
7
+<form action="lib/reports.php" method="GET" target="_BLANK">
8 8
     <div class="row">
9 9
         <div class="col-xs-12 col-sm-6">
10 10
             <label for="type"><?php lang("report type"); ?></label>
11 11
             <select name="type" class="form-control" required>
12
-                <option selected><?php lang("choose an option") ?></option>
13 12
                 <option value="users"><?php lang("users") ?></option>
14 13
                 <option value="managers"><?php lang("managers") ?></option>
15 14
                 <option value="permissions"><?php lang("permissions") ?></option>
@@ -21,12 +20,18 @@ redirectifnotloggedin();
21 20
             <select name="format" class="form-control" required>
22 21
                 <option value="csv"><?php lang("csv file") ?></option>
23 22
                 <option value="ods"><?php lang("ods file") ?></option>
23
+                <option value="html"><?php lang("html file") ?></option>
24 24
             </select>
25 25
         </div>
26 26
     </div>
27 27
     <br />
28 28
     <input type="hidden" name="action" value="export" />
29 29
     <input type="hidden" name="source" value="export" />
30
+    <?php
31
+    $code = uniqid(rand(10000000,99999999), true);
32
+    $database2->insert('report_access_codes', ['code' => $code, 'expires' => date("Y-m-d H:i:s", strtotime("+5 minutes"))]);
33
+    ?>
34
+    <input type="hidden" name="code" value="<?php echo $code; ?>" />
30 35
     
31
-    <button type="submit" class="btn btn-success"><i class="fa fa-download"></i> <?php lang("generate report"); ?></button>
36
+    <button type="submit" class="btn btn-success" onclick="setTimeout(function () {window.location.reload();}, 1000)"><i class="fa fa-download"></i> <?php lang("generate report"); ?></button>
32 37
 </form>

+ 15
- 1
required.php View File

@@ -53,9 +53,23 @@ try {
53 53
     ]);
54 54
 } catch (Exception $ex) {
55 55
     //header('HTTP/1.1 500 Internal Server Error');
56
-    sendError("Database error.  Try again later.  $ex");
56
+    sendError("Database error 1.  Try again later.  $ex");
57 57
 }
58 58
 
59
+$database2;
60
+try {
61
+    $database2 = new Medoo([
62
+        'database_type' => DB2_TYPE,
63
+        'database_name' => DB2_NAME,
64
+        'server' => DB2_SERVER,
65
+        'username' => DB2_USER,
66
+        'password' => DB2_PASS,
67
+        'charset' => DB2_CHARSET
68
+    ]);
69
+} catch (Exception $ex) {
70
+    //header('HTTP/1.1 500 Internal Server Error');
71
+    sendError("Database error 2.  Try again later.  $ex");
72
+}
59 73
 
60 74
 if (!DEBUG) {
61 75
     error_reporting(0);

+ 9
- 1
settings.template.php View File

@@ -4,7 +4,7 @@
4 4
 // DO NOT SET TO TRUE IN PRODUCTION!!!
5 5
 define("DEBUG", false);
6 6
 
7
-// Portal database connection settings
7
+// AccountHub database connection settings
8 8
 // See http://medoo.in/api/new for info
9 9
 define("DB_TYPE", "mysql");
10 10
 define("DB_NAME", "sso");
@@ -13,6 +13,14 @@ define("DB_USER", "sso");
13 13
 define("DB_PASS", "");
14 14
 define("DB_CHARSET", "utf8");
15 15
 
16
+// ManagePanel DB connection
17
+define("DB2_TYPE", "mysql");
18
+define("DB2_NAME", "managepanel");
19
+define("DB2_SERVER", "localhost");
20
+define("DB2_USER", "managepanel");
21
+define("DB2_PASS", "");
22
+define("DB2_CHARSET", "utf8");
23
+
16 24
 // Name of the app.
17 25
 define("SITE_TITLE", "ManagePanel");
18 26
 

Loading…
Cancel
Save