-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilenamereport.inc.php
185 lines (153 loc) · 6.51 KB
/
filenamereport.inc.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
// Copyright (c) Claus Tondering. E-mail: [email protected].
//
// This code is distributed under an MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// This file contains files to handle the photo naming scheme:
// [PLACE#]_[PERIOD#]_[PICTURE#]_[RESOLUTION].[FILETYPE]
require_once 'database.inc.php';
// Find legal place numbers
$places = array();
$res = exec_sql("SELECT intval FROM {$db_prefix}categories,{$db_prefix}catval "
. "WHERE {$db_prefix}categories.name='Place' "
. "AND {$db_prefix}categories.id={$db_prefix}catval.category");
while ($row = mysqli_fetch_object($res))
$places[] = $row->intval;
// Find legal culture numbers
$cultures = array();
$res = exec_sql("SELECT intval FROM {$db_prefix}categories,{$db_prefix}catval "
. "WHERE {$db_prefix}categories.name='Culture' "
. "AND {$db_prefix}categories.id={$db_prefix}catval.category");
while ($row = mysqli_fetch_object($res))
$cultures[] = $row->intval;
// Find legal period numbers
$periods = array();
$res = exec_sql("SELECT intval FROM {$db_prefix}categories,{$db_prefix}catval "
. "WHERE {$db_prefix}categories.name='Period' "
. "AND {$db_prefix}categories.id={$db_prefix}catval.category");
while ($row = mysqli_fetch_object($res))
$periods[] = $row->intval;
// Find legal resolution strings
$resolutions = array();
$res = exec_sql("SELECT stringval FROM {$db_prefix}categories,{$db_prefix}catval "
. "WHERE {$db_prefix}categories.name='Resolution type' "
. "AND {$db_prefix}categories.id={$db_prefix}catval.category");
while ($row = mysqli_fetch_object($res))
$resolutions[] = $row->stringval;
class FileNameReport {
public $directory;
public $old_filename;
public $new_filename;
public $renamed;
public $errors;
public $fatal_error;
public $categories;
public $picno;
private $place;
private $culture;
private $period;
private $resolution;
function __construct($dir, $f) {
global $db_prefix;
$this->directory = $dir;
$this->old_filename = $f;
$this->errors = array();
$this->fatal_error = null;
$ext = strrchr($f,'.');
if ($ext!='.jpg' && $ext!='.JPG' && $ext!='.png' && $ext!='.PNG') {
$this->fatal_error = 'Illegal file extension';
return;
}
// Rename?
if ($ext==".JPG") {
$this->new_filename = substr_replace($f,'jpg',-3);
$this->renamed = true;
}
elseif ($ext==".PNG") {
$this->new_filename = substr_replace($f,'png',-3);
$this->renamed = true;
}
else {
$this->new_filename = $f;
$this->renamed = false;
}
$head = substr($f, 0, -4);
$parts = explode('_', $head);
if (count($parts)!=4) {
$this->fatal_error = 'File name does not contain 3 underscores';
return;
}
$cat = $this->fillcategories();
if (!is_numeric($this->picno)) {
$this->fatal_error = "Picture number ($this->picno) is not numeric";
return;
}
foreach ($this->place as $c) {
if (!is_numeric($c)) {
$this->fatal_error = "Place/culture number is not numeric";
return;
}
if (!isset($places[$c]))
$this->errors[] = "Illegal place number";
}
foreach ($this->culture as $c) {
if (!isset($cultures[$c]))
$this->errors[] = "Illegal culture number";
}
foreach ($this->period as $c) {
if (!is_numeric($c)) {
$this->fatal_error = "Period number is not numeric";
return;
}
if (!isset($periods[$c]))
$this->errors[] = "Illegal period number";
}
if (!isset($this->resolution) ||
!isset($resolutions[$this->resolution]))
$this->errors[] = "Illegal resolution type";
}
private function fillcategories() {
// This code assumes that the file extension is always 4 characters long
$this->place = array();
$this->culture = array();
$this->period = array();
$parts = explode('_', substr($this->new_filename,0,-4));
// $parts[0] contains the place if the value is <2200 or in the range 5000..5499, otherwise
// the value specifies culture
$subparts = explode('.', $parts[0]);
foreach ($subparts as $sp) {
if (is_numeric($sp)) {
if ($sp<2200 || ($sp>=5000 && $sp<5500))
$this->place[] = $sp;
else
$this->culture[] = $sp;
}
}
// $parts[1] is the period
$subparts = explode('.', $parts[1]);
foreach ($subparts as $sp)
$this->period[] = $sp;
// $parts[2] is the picture number
$this->picno = $parts[2];
// $parts[3] is the resolution
$this->resolution = $parts[3];
$this->categories = array( 'Place' => $this->place,
'Culture' => $this->culture,
'Period' => $this->period,
'Resolution type' => array($this->resolution) );
}
}