-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTsdrApi.php
436 lines (374 loc) · 12.1 KB
/
TsdrApi.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
/**
* This API will allow users to create an AJAX form
* that takes a serial or registation number of a trademark and
* get important information about it
*
* @version 0.3 Beta
*/
include_once 'urlupload.php';
class TsdrApi
{
const NO_INFO_FOUND = "No Information found.";
const STATUS_ABANDONED = "Abandoned";
const STATUS_CANCELLED = "Registration cancelled";
const REGISTRATION_NUMBER = "rn";
const SERIAL_NUMBER = "sn";
/**
* This variable stores all the "important" data of the mark
*
* @var Array
*/
private $_data;
/**
* Name of the directory to store xml files
*
* @var String
*/
private $_dir;
/**
* SimpleXMLElement Object that represents the entire trademark
* with all of its information
*
* @var Object
*/
private $_trademark;
/**
* returns Api Form
*/
public static function getApiForm()
{
$constant = 'constant';
$reloadGif = 'reload.gif';
$phpself = "Response.php";
$form = <<<APIFORM
<h1>Trademark API test environment</h1>
<p>Welcome to the Trademark API Test Environment!</p>
<p>This form will dynamically retrieve relative information about any existing trademark on the fly!</p>
<p>Please enter a serial or registration number.</p>
<i>If you don't have have one, use this for an example: serial no 85129597</i>
<form method="post" action="$phpself">
<!-- Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br> -->
APIFORM;
$form .= 'Type: <input type="radio" name="type" value="' . self::SERIAL_NUMBER . '" checked>Serial ';
$form .= '<input type="radio" name="type" value="' . self::REGISTRATION_NUMBER . '">Registration<br>';
$form .= <<<APIFORM
Number: <input type="text" name="number">
<br><br>
<input type="button" name="submit" value="Submit" onclick="javascript:submitNumber(event)"> <!-- AJAX CALL -->
<div id='loadingmessage' style='display:none'>
<img src=$reloadGif>
</div>
</form>
<div id="trademark"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type=text/javascript>
$("input").keypress(function(event) {
if (event.which == 13) {
event.preventDefault();
submitNumber();
}
});
function submitNumber()
{
$('#loadingmessage').show();
if($('input[name=number]').val() == "")
{
$( ".response-form" ).remove();
$( ".error" ).remove();
$('#trademark').append('<div class="error">The field cannot be empty. </br> Please insert a valid serial or registration number</div>');
$('#loadingmessage').hide();
}
else
{
$( ".error" ).remove();
$.ajax({
type: "POST",
data: {
'number': $('input[name=number]').val(),'type': $('input[name=type]:checked').val()
},
url: "$phpself",
dataType: "html",
success: function(data) {
$('#loadingmessage').hide();
var result = null;
result = data;
$('#trademark').html(result);
}
});
}
}
</script>
APIFORM;
echo $form;
}
/**
* Return response form
*
* @TODO Do I need to handle errored responses from here?
* @param Object $data
* @param Int $number
* @return HTML form
*/
public function responseForm($data, $number)
{
$sn = $data['SerialNumber']; // parameter $number might be registration number - png is sn.png
// see if an image was returned in the zip file
if(file_exists("status_archive/$sn.png"))
{
$image = <<<IMAGE
<img src="status_archive/$sn.png" style="width: 170px;" alt="trademark image">
IMAGE;
}
else
{
$image = "no image available";
}
$respForm = <<<RESPONSEFORM
<div class="response-form" id="$number">
<dl class="trademark-info">
<dt>Trademark</dt>
<dd>$image</dd>
<dt>Serial Number</dt>
<dd>{$data['SerialNumber']}</dd>
<dt>Application Filing Date</dt>
<dd>{$data['ApplicationFilingDate']}</dd>
<dt>Registration Number</dt>
<dd>{$data['RegistrationNumber']}{$data['RegistrationCertificate']}</dd>
<dt>Registration Date</dt>
<dd>{$data['RegistrationDate']}</dd>
<dt>Mark Type</dt>
<dd>{$data['MarkType']}</dd>
<dt>Status</dt>
<dd>{$data['Status']}</dd>
<dt>Renewal Date</dt>
<dd>{$data['RenewalDate']}</dd>
<dt>Trademark Literal Elements</dt>
<dd>{$data['MarkLiteralElements']}</dd>
<dt>Standard Character Claim</dt>
<dd>{$data['StandardCharacterClaim']}</dd>
RESPONSEFORM;
for($i = 0; $i < count($data['ClassNumber']); $i++)
{
$respForm .= <<<RESPONSEFORM
<dt>Classification Number and Description</dt>
<dd>{$data['ClassNumber'][$i]}:   {$data['ClassDescription'][$i]}</dd>
<!-- <dt>Classification Description</dt> -->
<dd></dd>
RESPONSEFORM;
}
$respForm .= <<<RESPONSEFORM
</dl>
</div>
RESPONSEFORM;
echo $respForm;
}
/**
* Retrieves Trademark information
*
* @param String $number serial or registration number
* @param String $type sn or rn for serial number or registration number
* @return Array $data|Bool false
*/
public function getTrademarkData($number, $type)
{
// Check if status archive exists
$this->_makeStatusDir();
// Clean number input
$number = $this->_cleanInput($number);
// Check if exception was thrown
if(!$this->_getTrademark($number, $type))
{
return False;
}
$this->_mapImportantData($this->_trademark);
return $this->_data;
}
/**
* Removes all files in archive directory
* This method should be called at EOD or 12 AM
*
* This method should be called from crontab
*
* @param String $dir name of directory
*/
public function flushArchive($dir = 'status_archive')
{
// Recursively remove all files if subdirectories exist
$path = getcwd() . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
foreach (glob("{$path}*") as $file)
{
if(is_dir($file))
{
if(is_dir_empty($file))
{
rmdir($file);
}
else
{
flushArchive($file);
}
}
else
{
unlink($file);
}
}
}
/*=================== UTILITIES ======================*/
/**
* Generates trademark Object via Serial or Registration number
* If an xml file already exists with the corresponding serial number,
* it will parse it from disk rather than obtain it from TSDR system
*
* @param String $number serial or registration number
* @param String $type sn or rn for serial number or registration number
* @param String $dir directory name to store xml
* @return Bool False if exception caught
*/
private function _getTrademark($number, $type)
{
// Check if status file exists
if($type === self::REGISTRATION_NUMBER || !file_exists($this->_dir. DIRECTORY_SEPARATOR ."{$number}_status_st96.xml"))
{
// @TODO Make abstraction for $url
$url = "https://tsdrsec.uspto.gov/ts/cd/casedocs/{$type}{$number}/zip-bundle-download?case=true&docs=&assignments=&prosecutionHistory=";
try
{
// @TODO decouple urluploader class and use dependency injection principles
$upload = new UrlUpload($url, $this->_dir, $number);
$upload->uploadFromUrl();
// might have to change $number registration number search returns searial.zip
$number = $upload->getLocalname();
}
catch (Exception $e)
{
// Development
// echo "ERROR: ". $e->getMessage()."</br>";
// echo "<pre>";
// echo print_r($e->getFile())."\n";
// echo "Line: ".$e->getLine();
// echo "</pre>";
// Production
$label = ($type == self::SERIAL_NUMBER ? "serial" : "registration");
echo "<div class=\"error\">Sorry, but the provided $label number was not found </br> Please make sure you inserted the correct serial or registration number</div>";
return false;
}
}
$xml = simplexml_load_file($this->_dir. DIRECTORY_SEPARATOR . "{$number}_status_st96.xml", NULL, NULL, 'ns2', True);
$trademark = $xml->TrademarkTransactionBody->TransactionContentBag->TransactionData->TrademarkBag->Trademark;
$this->_trademark = $trademark;
return true;
}
/**
* Maps specific data from Trademark object
* for ease of use
*
* @param Object $trademark
*/
private function _mapImportantData($trademark)
{
$counter = 0;
$data = array();
$data['ApplicationFilingDate'] = $trademark->ApplicationDate
? $trademark->ApplicationDate: self::NO_INFO_FOUND;
$ns1 = $trademark->children('ns1', true);
$data['SerialNumber'] = $ns1->ApplicationNumber->ApplicationNumberText ? $ns1->ApplicationNumber->ApplicationNumberText: self::NO_INFO_FOUND;
$data['RegistrationDate'] = $ns1->RegistrationDate
? $ns1->RegistrationDate: self::NO_INFO_FOUND;
$data['RegistrationNumber'] = $ns1->RegistrationNumber
? $ns1->RegistrationNumber: self::NO_INFO_FOUND;
$data['RegistrationCertificate'] = $ns1->RegistrationNumber
? ' <a target="_blank" href="https://tsdrsec.uspto.gov/ts/cd/casedocs/bundle.pdf?rn=' . $ns1->RegistrationNumber . '&category=RC">Registration Certificate</a>': '';
$data['MarkType'] = $trademark->MarkCategory
? $trademark->MarkCategory: self::NO_INFO_FOUND;
$data['Status'] = $trademark->NationalTrademarkInformation->MarkCurrentStatusExternalDescriptionText
? $trademark->NationalTrademarkInformation->MarkCurrentStatusExternalDescriptionText: self::NO_INFO_FOUND;
// Special logic for status
$data['Status'] = $this->_cleanUpStatus($data['Status']);
$data['RenewalDate'] = $trademark->NationalTrademarkInformation->RenewalDate
? $trademark->NationalTrademarkInformation->RenewalDate: self::NO_INFO_FOUND;
$data['MarkLiteralElements'] = $trademark->MarkRepresentation->MarkReproduction->WordMarkSpecification->MarkVerbalElementText
? $trademark->MarkRepresentation->MarkReproduction->WordMarkSpecification->MarkVerbalElementText: self::NO_INFO_FOUND;
$data['StandardCharacterClaim'] = $trademark->MarkRepresentation->MarkReproduction->WordMarkSpecification->MarkStandardCharacterIndicator
? $trademark->MarkRepresentation->MarkReproduction->WordMarkSpecification->MarkStandardCharacterIndicator: self::NO_INFO_FOUND;
// Accounts for multiple classes and descriptions in a trademark
foreach ($trademark->GoodsServicesBag->GoodsServices as $GoodsServices)
{
$data['ClassNumber'][$counter] = $GoodsServices[$counter]->ClassDescriptionBag->ClassDescription[0]->ClassNumber
? $GoodsServices[$counter]->ClassDescriptionBag->ClassDescription[0]->ClassNumber: self::NO_INFO_FOUND;
$data['ClassDescription'][$counter] = $GoodsServices[$counter]->ClassDescriptionBag->ClassDescription[0]->GoodsServicesDescriptionText
? $GoodsServices[$counter]->ClassDescriptionBag->ClassDescription[0]->GoodsServicesDescriptionText: self::NO_INFO_FOUND;
$counter++;
}
$this->_data = $data;
}
/**
* Returns directory name and creates
* a path if it doesn't exist
*
*/
private function _makeStatusDir()
{
$dir = 'status_archive';
if (!file_exists($dir) && !is_dir($dir))
{
mkdir($dir);
}
$this->_dir = $dir;
}
/**
* Clean up user input for security purposes
*
* @param String $data
* @return String $data cleaned up user input
*/
private function _cleanInput($userInput) {
$userInput = trim($userInput);
$userInput = stripslashes($userInput);
$userInput = htmlspecialchars($userInput);
$userInput = str_replace(",", "", $userInput);
return $userInput;
}
/**
* Removes indication of TSDR website for applications
*
* @param String $status
*/
private function _cleanUpStatus($status)
{
// @TODO find a more elegent solution to substitute typecast
$status = (string)$status;
if($status === self::NO_INFO_FOUND)
{
return $status; // No modifications on status
}
// Check for status
$arr = explode(' ',trim($status));
if ($arr[0] == 'Registration')
{
$arr = $arr[0]." ".$arr[1];
}
else
{
$arr = $arr[0];
}
switch ($arr)
{
case self::STATUS_ABANDONED:
$status = explode(". ", $status);
$status = $status[0];
return $status;
case self::STATUS_CANCELLED:
$status = explode(". ", $status);
$status = $status[0];
return $status;
default:
return $status; // No modifications on status
}
}
}
?>