-
Notifications
You must be signed in to change notification settings - Fork 0
/
itmanage.php
167 lines (133 loc) · 2.81 KB
/
itmanage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
if (session_status()==PHP_SESSION_NONE)
session_start();
include_once "config.php";
include_once "generic.php";
function callback_is_file($var)
{
return is_file(SOURCEDIR . $var);
}
function loadIT()
{
$itList = array_filter(scandir(SOURCEDIR), 'callback_is_file');
return $itList;
}
function withoutTag($var)
{
$tag = strstr($var, '-');
$tag = substr($tag, 1);
$tag = strstr($tag, '-', true);
if ($tag === false OR !in_array($tag, TAGS))
{
return $var;
}
else
{
//might cause problems if $var contains multiple tags
return str_replace('-' . $tag, '', $var);
}
}
//returns the filename WITH RELATIVE PATH
//takes the file without tag as a param
function findFile($itname, $dir = SOURCEDIR, $ext = '.pdf')
{
if (is_array($ext))
{
foreach ($ext as $elem)
{
$rep = look_for_file($itname, $dir, $elem);
if ($rep!==false)
return $rep;
}
}
else
{
$rep = look_for_file($itname, $dir, $ext);
if ($rep!=false)
return $rep;
}
//if we got to this point it means
//we didn't find it in the normal folder.
//now to look in the archives
if (strstr($dir, ARCHIVES)===false)
{
$rep = findFile($itname, $dir . ARCHIVES, $ext);
if ($rep===false)
{
//echo an error
}
else
{
//put in the log file that a file from the archives was loaded
//the smaller vars ($logged, $user etc...) might not be set yet
$userstr = isset($_SESSION['user'])?($_SESSION['user'] . ' permission: ' . $_SESSION['usrlvl'] . ' at '):'' . $_SERVER['REMOTE_ADDR'];
addLog(logmsg("The file $itname ($rep) that is in the archived folder has been accessed"), $userstr);
}
return $rep;
}
return false;
}
//this function is just to not copy paste in findFile
function look_for_file($itname, $dir, $ext)
{
$resname = $itname . $ext;
$filename = $resname;
if (is_file($dir . $filename))
{
return $dir . $filename;
}
foreach (TAGS as $elem)
{
$position = strpos($resname, '-');
$filename = substr_replace($resname, '-' . $elem, $position, 0);
if (is_file($dir . $filename))
{
return $dir . $filename;
}
}
return false;
}
//sorts ITs by batch
function sortIT($itList)
{
$reptab = array();
foreach($itList as $elem)
{
$separated = explode('-', pathinfo(withoutTag($elem), PATHINFO_FILENAME));
$key;
if ($separated[0]==='IT')
{
$key = $separated[1];
}
elseif (isset($separated[1]) AND $separated[1]==='IT')
{
$key = $separated[2];
}
else
{
//it's a special/generic IT
$key = 'GENERIC';
}
if (in_array($key, GENERIC))
{
$key = 'GENERIC';
}
$reptab[$key][] = $elem;
}
ksort($reptab);
return $reptab;
}
//returns an array of IT that contain that certain string
function search($ITarray, $needle)
{
$rep = array();
foreach($ITarray as $elem)
{
if (strpos($elem, $needle)!==false)
{
$rep[] = $elem;
}
}
return $rep;
}
?>