浏览代码

Upload files to 'public_html/scripts'

david.ortiz11 5 年前
父节点
当前提交
8ab49ce1f9

+ 23
- 0
public_html/scripts/config.php 查看文件

@@ -0,0 +1,23 @@
1
+<?php
2
+/*  Authors         :   Carlos C. Corrada-Bravo
3
+                        David J. Ortiz-Rivera
4
+
5
+    Organization    :   Centro de Desarrollo y Consultoria Computacional
6
+    Project         :   OPASO Material Registry   
7
+    File            :   config.php
8
+    Description     :   Initialize database connection. */
9
+
10
+    /* Initiate values */ 
11
+	$server = 'localhost';
12
+	$username = 'david.ortiz11';
13
+	$password = 'John2815/';
14
+	$name = 'OPASO';
15
+
16
+	/* Create connection */
17
+	$db = new mysqli($server,$username,$password,$name);
18
+	/* Check connection */
19
+	if ($db->connect_error){
20
+	    die('Connection failed: ' . $db->connect_error);
21
+	}
22
+	// echo 'Connected successfully <br>' ;
23
+?>

+ 175
- 0
public_html/scripts/extract.php 查看文件

@@ -0,0 +1,175 @@
1
+<?php
2
+/*  Authors         :   Carlos C. Corrada-Bravo
3
+                        David J. Ortiz-Rivera
4
+
5
+    Organization    :   Centro de Desarrollo y Consultoria Computacional
6
+    Project         :   OPASO Material Registry   
7
+    File            :   extract.php
8
+    Description     :   Extract and insert data from tsv. */
9
+
10
+	/* Import database connection & display errors */
11
+	include_once "config.php";
12
+	error_reporting(E_ALL);
13
+	ini_set("display_errors",1);
14
+	/* For each file: open and explode each row by tabs */
15
+	foreach (glob("../ndata/*.txt") as $file){
16
+		/* Initiate laboratory/personnel/register dictionaries. */ 
17
+		$laboratory = array();
18
+		$pi = array();
19
+		$cho = array();
20
+		$register = array();
21
+		/* Initiate some values. */ 
22
+		$success = 0;
23
+		$null = "n/a";
24
+		$row = 1;	/* Start at row 1. */	
25
+		/* Parse tsv file */
26
+		if(($fhandle = fopen($file,"r")) !== FALSE){
27
+	  		while(($data = fgetcsv($fhandle,1000,"\t")) !== FALSE){
28
+				/* Extract department and personnel in columns B(1) and I(8) */ 
29
+				if($row > 6 and $row < 12){
30
+					/* Avoid null keys. */
31
+					if($data[0]){
32
+						$laboratory[$data[0]] = $data[1];
33
+					}
34
+					if($row < 10){
35
+						$pi[$data[7]] = $data[8];
36
+					}
37
+					else{
38
+						$cho[$data[7]] = $data[8];
39
+					}
40
+				}
41
+				/* Extract cho phone. */ 
42
+				elseif($row == 12){
43
+					$cho[$data[7]] = $data[8];
44
+				}
45
+				/* Insert laboratory/personnel info and generate register keys. */ 
46
+				elseif($row == 14){
47
+					for($index=0; $index < count($data); $index++){
48
+						/* Initiate keys with null values. */
49
+						if($data[$index]){
50
+							$register[$data[$index]] = "";
51
+						}
52
+					}
53
+					foreach($pi as $key=>&$value){
54
+						if(!$value){
55
+							$value = $null;
56
+						}
57
+					}
58
+					foreach($cho as $key=>&$value){
59
+						if(!$value){
60
+							$value = $null;
61
+						}
62
+					}
63
+					/* person entries. 
64
+					   Bind and insert pi data. */
65
+					$person = $db->stmt_init();
66
+					if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
67
+						$person->bind_param("sssss",$pi['pi'],$pi['piemail'],$null,$pi['piphone'],$null);
68
+						if($person->execute()){
69
+							$pi = $db->insert_id;	/* Extract generated pid. */
70
+						}
71
+						/* In case of duplicate entry. */ 
72
+						else{
73
+							$email = $pi['piemail'];
74
+							/* Extract id by email. */
75
+							if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
76
+							    /* Bind selector and execute. */
77
+							    $person->bind_param("s",$email);
78
+							    if($person->execute()){
79
+				    				/* Bind result variables and fetch. */
80
+								    $person->bind_result($pi);
81
+								    $person->fetch();
82
+							    }
83
+							}
84
+						}
85
+					}
86
+					/* Bind and insert cho data. */
87
+					$person = $db->stmt_init();
88
+					if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
89
+						$person->bind_param("sssss",$cho['cho'],$cho['choemail'],$null,$cho['chophone'],$null);
90
+						if($person->execute()){
91
+							$cho = $db->insert_id;	/* Extract generated pid. */
92
+						}
93
+						/* In case of duplicate entry. */ 
94
+						else{
95
+							$email = $cho['choemail'];
96
+							/* Extract id by email. */
97
+							if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
98
+							    /* Bind selector and execute. */
99
+							    $person->bind_param("s",$email);
100
+							    if($person->execute()){
101
+				    				/* Bind result variables and fetch. */
102
+								    $person->bind_result($cho);
103
+								    $person->fetch();
104
+							    }
105
+							}
106
+						}
107
+					}
108
+					$person->close();
109
+					/* lab entry
110
+					   Bind and insert lab data. */ 
111
+					$lab = $db->stmt_init();
112
+					if($lab = $db->prepare("INSERT INTO Laboratory(lname,lab,department,building,extension,pi,cho) VALUES (?,?,?,?,?,?,?)")){
113
+						$lab->bind_param("sssssii",$laboratory['lname'],$laboratory['lab'],$laboratory['department'],$laboratory['building'],$laboratory['extension'],$pi,$cho);
114
+						$lab->execute();
115
+						$lab->close();
116
+						$lab = $db->insert_id;	/* Extract generated lid. */
117
+					}
118
+				}
119
+				/* Extract register data by row. */ 
120
+				elseif($row > 15){
121
+					$d = 0;	/* Reset index. */
122
+					foreach($register as $key=>&$value){
123
+						/* Replace null entries with identifiable string. */ 
124
+						if($data[$d]){
125
+							$value = $data[$d];
126
+						}
127
+						else{
128
+							$value = $null;
129
+						}
130
+						$d++;
131
+						/* Avoid segmentation fault. */ 
132
+						if($d >= count($register)){
133
+							break;	
134
+						}
135
+					}
136
+					/* inventory entries
137
+					   Bind and insert inventory data. */ 
138
+					$inventory = $db->stmt_init();
139
+					if($register['chemical'] != "null entry"){
140
+						if($inventory = $db->prepare("INSERT INTO Inventory(chemical,manufacturer,sds,cas,state,hazard,type,amount,quantity,total,location,ghs,lid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")){
141
+					 		$inventory->bind_param("ssssssssssssi",$register['chemical'],$register['manufacturer'],$register['sds'],$register['cas'],$register['state'],$register['hazard'],$register['type'],$register['amount'],$register['quantity'],$register['total'],$register['location'],$register['ghs'],$lab);
142
+							/* Query failed, display error. */ 
143
+							if(!($inventory->execute())){
144
+								echo "Entry error:";
145
+								echo "<br>";
146
+					 			print_r($inventory);
147
+					 			echo "<br>";
148
+					 			print_r($register);
149
+					 			echo "<br>";
150
+								echo "PI id: " . $pi . "<br>";
151
+								echo "CHO id: " . $cho . "<br>";
152
+								echo "LAB id: " . $lab . "<br>";
153
+							}
154
+							/* Track number of successful entries. */ 
155
+							else{
156
+								$success++;
157
+								$inventory->close();
158
+							}
159
+						}
160
+					}
161
+				}
162
+		    	$row++;		/* Update row. */ 
163
+		  	}
164
+		  	/* Close all remaining connections/streams. */ 
165
+		  	fclose($fhandle);
166
+			echo "<br>FILE: " . $file . "<br>";
167
+			echo "PI id: " . $pi . "<br>";
168
+			echo "CHO id: " . $cho . "<br>";
169
+			echo "LAB id: " . $lab . "<br>";
170
+			echo $success . " successful entries." . "<br><br>";
171
+		}
172
+	}
173
+	/* Close db connection. */ 
174
+	$db->close();
175
+?>

+ 70
- 0
public_html/scripts/main.js 查看文件

@@ -0,0 +1,70 @@
1
+/*  Authors         :   Carlos C. Corrada-Bravo
2
+                        David J. Ortiz-Rivera
3
+                        José A. Quiñones-Flores
4
+
5
+    Organization    :   Centro de Desarrollo y Consultoria Computacional
6
+    Project         :   OPASO Material Registry   
7
+    File            :   main.js
8
+    Description     :   Compile mainly used functions */
9
+
10
+/* current() - highlight current page in navbar */ 
11
+function current(){
12
+    var current = window.location.pathname;
13
+    $(".link").each(function(){
14
+        if($(this).attr("href") == current){
15
+            $(this).addClass("current");
16
+        }
17
+    });
18
+}
19
+/* get_arg(arg: string) - fetches argument from url if set, else returns null */ 
20
+function get_arg(arg){
21
+    /* Get arg by id */ 
22
+    var url = new URL(window.location.href);
23
+    arg = url.searchParams.get(arg);
24
+    return arg;
25
+}
26
+/* loading_screen(flag: bool) - hide/show loading screen */
27
+function loading_screen(flag){
28
+    if(flag){   /* show */
29
+        $(".content").css("overflow","hidden");
30
+        $(".loading-screen").show();
31
+    }
32
+    else{       /* hide */
33
+        $(".loading-screen").hide();
34
+        $(".content").css("overflow","auto");
35
+    }
36
+}
37
+/* create_button(text: string,icon: object,cname: string,id: string,val: string) - generates button */ 
38
+function create_button(text,icon,cname,id,val){
39
+    var button = document.createElement("button");
40
+    /* set attributes */ 
41
+    button.setAttribute("class",cname);
42
+    button.setAttribute("id",id);
43
+    if(is_defined(val)){
44
+        button.setAttribute("val",val);
45
+    }
46
+    /* set content */ 
47
+    button.textContent = text;
48
+    button.append(create_element("i",icon));
49
+    return  button;
50
+}
51
+/* create_element(type: string,cname: string,content: string) - generates an html element */ 
52
+function create_element(type,cname,content){
53
+    var element = document.createElement(type);
54
+    /* set class */ 
55
+    element.setAttribute("class",cname);
56
+    /* set content */
57
+    if(is_defined(content)){
58
+        element.textContent = content;
59
+    }
60
+    return element;
61
+}
62
+/* display_error(error: string) - display error to user */ 
63
+function display_error(message){
64
+    var error = create_element("div","error",message);
65
+    $(".main").html(error);
66
+}
67
+/* is_defined(arg: any) - return true if an argument is defined */
68
+function is_defined(arg){
69
+    return typeof arg != undefined;
70
+} 

+ 5
- 0
public_html/scripts/materials.js 查看文件

@@ -0,0 +1,5 @@
1
+let user = "david.ortiz";
2
+let q = "all";
3
+$.post("/scripts/materials.php",{user:user,type:type},function(response){
4
+	console.log(response);
5
+});

+ 164
- 0
public_html/scripts/opaso.php 查看文件

@@ -0,0 +1,164 @@
1
+<?php
2
+/*  Authors         :   Carlos C. Corrada-Bravo
3
+                        David J. Ortiz-Rivera
4
+                        José A. Quiñones-Flores
5
+
6
+    Organization    :   Centro de Desarrollo y Consultoria Computacional
7
+    Project         :   OPASO Material Registry   
8
+    File            :   opaso.php
9
+    Description     :   Handle API requests for OPASO */
10
+	require "config.php";
11
+	error_reporting(E_ALL);
12
+	ini_set("display_errors",1);
13
+	
14
+
15
+	/* extract api call */
16
+	if(isset($_POST["query"])){
17
+		$query = $_POST["query"];
18
+		$response = array();	/* response array */
19
+		$error = false;
20
+		/* handle query */ 
21
+		switch($query){
22
+			case 0:		/* Register user */
23
+				break;
24
+			case 1:		/* Login user */
25
+				break;
26
+			case 2:		/* Get laboratories */
27
+				break;
28
+			case 3:		/* Get materials */
29
+				break;
30
+			case 4:		/* Get inventory by lab */
31
+				/* extract args */ 
32
+				if(isset($_POST["laboratory"])){
33
+					$laboratory = $_POST["laboratory"];
34
+					if($lab_name = $db->prepare("SELECT lname FROM Laboratory WHERE lid=?")){
35
+						$lab_name->bind_param("i",$laboratory);		/* bind laboratory to query and execute */
36
+						if($lab_name->execute()){
37
+							$lab_name->bind_result($lname);
38
+							$lab_name->store_result();
39
+							if($lab_name->num_rows > 0){
40
+								while($lab_name->fetch()){
41
+									$response["laboratory"] = $lname;
42
+								}
43
+							}
44
+							else{
45
+								$error = true;
46
+								$message = "Lab not found.";
47
+							}
48
+						}
49
+						else{
50
+							$error = true;
51
+							$message = "Something went wrong.";
52
+						}
53
+					}
54
+					else{
55
+						$error = true;
56
+						$message = "Something went wrong.";
57
+					}
58
+					/* prepare query */ 
59
+					if($inventory = $db->prepare("SELECT eid,chemical,cas,manufacturer,sds,ghs,hazard,state,type,amount,quantity,total,uom,location FROM Inventory WHERE lid=?")){
60
+						$inventory->bind_param("i",$laboratory);		/* bind laboratory to query and execute */
61
+						if($inventory->execute()){
62
+							$inventory->bind_result($eid,$chemical,$cas,$manufacturer,$sds,$ghs,$hazard,$state,$type,$amount,$quantity,$total,$uom,$location);
63
+							$inventory->store_result();
64
+							if($inventory->num_rows > 0){
65
+								$response["status"] = "success";
66
+								$response["inventory"] = [];
67
+								$options = [
68
+								    "cost" => 8,
69
+								];
70
+								while($inventory->fetch()){
71
+									$response["inventory"][password_hash($eid,PASSWORD_DEFAULT,$options)] = ["chemical" => $chemical,"cas" => $cas,"manufacturer" => $manufacturer,"sds" => $sds,"ghs" => $ghs,"hazard" => $hazard,"state" => $state,"type" => $type,"amount" => $amount,"quantity" => $quantity,"total" => $total,"uom" => $uom,"location" => $location];
72
+								}
73
+							}
74
+							else{
75
+								$error = true;
76
+								$message = "No results found.";
77
+							}
78
+							$inventory->close();
79
+						}
80
+						else{
81
+							$error = true;
82
+							$message = "Something went wrong.";
83
+						}
84
+					}
85
+					else{
86
+						$error = true;
87
+						$message = "Something went wrong.";
88
+					}
89
+				}
90
+				/* missing args */ 
91
+				else{
92
+					$error = true;
93
+					$message = "One ore more arguments missing.";
94
+				}
95
+				break;
96
+			case 5:		/* */
97
+				break;
98
+			case 6:		/* edit row */
99
+				/* extract args */
100
+				if(isset($_POST["eid"]) and isset($_POST["laboratory"]) and isset($_POST["fields"])){
101
+					$eid = $_POST["eid"];
102
+					$laboratory = $_POST["laboratories"];
103
+					$fields = $_POST["fields"];
104
+					/* match row with authorized rows */ 
105
+					$uid = "";
106
+					$labs = array();
107
+					for($l=0; $l < sizeof($labs); $l++){
108
+						if($rows = $db->prepare("SELECT eid FROM Laboratory WHERE lid=?")){
109
+							$rows->bind_param("i",$labs[$l]);		/* bind laboratory to query and execute */
110
+							if($rows->execute()){
111
+								$rows->bind_result($rid);
112
+								$rows->store_result();
113
+								if($rows->num_rows > 0){
114
+									while($rows->fetch()){
115
+										/* if ids match, update row */ 
116
+										if(password_verify($rid,$_POST["eid"])){
117
+											if($update_row = $db->prepare("UPDATE Inventory SET chemical=?,manufacturer=?,sds=?,cas=?,state=?,hazard=?,type=?,amount=?,quantity=?,total=?,location=?,ghs=?,uom=? WHERE eid=?")){
118
+												$update_row->bind_param("sssssssssssssi",$fields["chemical"],$fields["manufacturer"],$fields["sds"],$fields["cas"],$fields["state"],$fields["hazard"],$fields["type"],$fields["amount"],$fields["quantity"],$fields["total"],$fields["ghs"],$fields["uom"],$rid);
119
+												if($update_row->execute()){
120
+													$response["status"] = "success";
121
+												}
122
+											}
123
+										}	
124
+									}
125
+								}
126
+
127
+								else{
128
+									$error = true;
129
+									$message = "No results found.";
130
+								}
131
+							}
132
+						}
133
+					}
134
+				}
135
+				/* missing args */ 
136
+				else{
137
+					$error = true;
138
+					$message = "One ore more arguments missing.";
139
+				}
140
+				break;
141
+			case 7: 	/* edit inventory row */
142
+				print_r($_POST);
143
+				break;
144
+			case 8: 	/* copy inventory row */
145
+				print_r($_POST);
146
+				break;
147
+			default:	/* non defined requests */
148
+				print_r($_POST);
149
+
150
+				echo "request not defined";
151
+				break;
152
+		}
153
+		if($error){
154
+			$response = array();
155
+			$response["status"] = "error";
156
+			$response["error"] = $message;
157
+		}
158
+		echo json_encode($response);
159
+	}
160
+	/* missing api call */ 
161
+	else{
162
+		echo "one or more arguments are missing";
163
+	}
164
+?>