1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This is an OrderStatusLog for the downloads |
5
|
|
|
* It shows the download links |
6
|
|
|
* To make it work, you will have to add files. |
7
|
|
|
* |
8
|
|
|
* When it is first written it creates a folder for the downloads |
9
|
|
|
* you can then add files using the AddFiles method. |
10
|
|
|
* |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class ElectronicDelivery_OrderLog extends OrderStatusLog |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Use for debugging |
17
|
|
|
* uses debug::log |
18
|
|
|
* @boolean |
19
|
|
|
*/ |
20
|
|
|
private $debug = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Standard SS variable |
24
|
|
|
*/ |
25
|
|
|
private static $db = array( |
|
|
|
|
26
|
|
|
"FolderName" => "Varchar(255)", |
27
|
|
|
"Completed" => "Boolean", |
28
|
|
|
"NumberOfHoursBeforeDownloadGetsDeleted" => "Float" |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Standard SS variable |
33
|
|
|
*/ |
34
|
|
|
private static $many_many = array( |
|
|
|
|
35
|
|
|
"Files" => "File" |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Standard SS variable |
40
|
|
|
*/ |
41
|
|
|
private static $summary_fields = array( |
|
|
|
|
42
|
|
|
"Created" => "Date", |
43
|
|
|
"Type" => "Type", |
44
|
|
|
"Title" => "Title", |
45
|
|
|
"FolderName" => "Folder" |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Standard SS variable |
50
|
|
|
*/ |
51
|
|
|
private static $defaults = array( |
|
|
|
|
52
|
|
|
"InternalUseOnly" => false, |
53
|
|
|
"Completed" => false |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the default: the files are not ready yet! |
58
|
|
|
* Standard SS method |
59
|
|
|
*/ |
60
|
|
|
public function populateDefaults() |
61
|
|
|
{ |
62
|
|
|
parent::populateDefaults(); |
63
|
|
|
$this->Note = "<p>"._t("OrderLog.NODOWNLOADSAREAVAILABLEYET", "No downloads are available yet.")."</p>"; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @return Boolean |
69
|
|
|
**/ |
70
|
|
|
public function canDelete($member = null) |
71
|
|
|
{ |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
* @return Boolean |
78
|
|
|
*/ |
79
|
|
|
public function canCreate($member = null) |
80
|
|
|
{ |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* |
86
|
|
|
* @return Boolean |
87
|
|
|
**/ |
88
|
|
|
public function canEdit($member = null) |
89
|
|
|
{ |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Standard SS var |
95
|
|
|
* @var Array |
96
|
|
|
*/ |
97
|
|
|
private static $searchable_fields = array( |
|
|
|
|
98
|
|
|
'OrderID' => array( |
99
|
|
|
'field' => 'NumericField', |
100
|
|
|
'title' => 'Order Number' |
101
|
|
|
), |
102
|
|
|
"Title" => "PartialMatchFilter", |
103
|
|
|
"Note" => "PartialMatchFilter", |
104
|
|
|
"FolderName" => "PartialMatchFilter" |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Standard SS var |
110
|
|
|
* @var String |
111
|
|
|
*/ |
112
|
|
|
private static $singular_name = "Electronic Delivery Details for one Order"; |
|
|
|
|
113
|
|
|
public function i18n_singular_name() |
114
|
|
|
{ |
115
|
|
|
return _t("OrderStatusLog.ELECTRONICDELIVERYDETAIL", "Electronic Delivery Details for one Order"); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Standard SS var |
120
|
|
|
* @var String |
121
|
|
|
*/ |
122
|
|
|
private static $plural_name = "Electronic Deliveries Detail for many Orders"; |
|
|
|
|
123
|
|
|
public function i18n_plural_name() |
124
|
|
|
{ |
125
|
|
|
return _t("OrderStatusLog.ELECTRONICDELIVERIESDETAILS", "Electronic Deliveries Detail for many Orders"); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Standard SS var |
130
|
|
|
* @var String |
131
|
|
|
*/ |
132
|
|
|
private static $default_sort = "\"Created\" DESC"; |
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Size of the folder name (recommended to be at least 5+) |
137
|
|
|
* @var Int |
138
|
|
|
*/ |
139
|
|
|
private static $random_folder_name_character_count = 12; |
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* if set to anything except an empty string, |
143
|
|
|
* an .htaccess file will be added to the download folder |
144
|
|
|
* with the content of the variable |
145
|
|
|
* content idea: Options -Indexes (stops directly from listing folders) |
146
|
|
|
* @var String |
147
|
|
|
*/ |
148
|
|
|
private static $htaccess_content = ""; |
|
|
|
|
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* List of files to be ignored when searching for files in the folder |
152
|
|
|
* This may allow you to add "hidden" files or ignore other files. |
153
|
|
|
* Can be added as |
154
|
|
|
* 1 => mypng.png |
155
|
|
|
* 2 => mysecondImage.jpg |
156
|
|
|
* |
157
|
|
|
* @var Array |
158
|
|
|
*/ |
159
|
|
|
private static $files_to_be_excluded = array(); |
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Permissions on download folders |
163
|
|
|
* if not set, it will use: |
164
|
|
|
* Config::inst()->get('Filesystem', 'folder_create_mask') |
165
|
|
|
* @var string |
166
|
|
|
*/ |
167
|
|
|
private static $permissions_on_folder = ""; |
|
|
|
|
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @var String $order_dir - the root folder for the place where the files for the order are saved. |
171
|
|
|
* if the variable is equal to downloads then the downloads URL is www.mysite.com/downloads/ |
172
|
|
|
*/ |
173
|
|
|
private static $order_dir = '_downloads'; |
|
|
|
|
174
|
|
|
|
175
|
|
|
public function getCMSFields() |
176
|
|
|
{ |
177
|
|
|
$fields = parent::getCMSFields(); |
178
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("FilesInFolder", _t("OrderStep.ACTUALFilesInFolder", "Actual files in folder: ").implode(", ", $this->getFilesInFolder()))); |
179
|
|
|
return $fields; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Adds the download files to the Log and makes them available for download. |
184
|
|
|
* @param ArrayList | Null $dosWithFiles - Data Object Set with files |
185
|
|
|
*/ |
186
|
|
|
public function AddFiles($listOfFiles) |
187
|
|
|
{ |
188
|
|
|
//update log fields |
189
|
|
|
$this->Title = _t("OrderStatusLog.DOWNLOADFILES", "Download Files"); |
|
|
|
|
190
|
|
|
$this->Note = "<ul>"; |
|
|
|
|
191
|
|
|
if (!$this->OrderID) { |
|
|
|
|
192
|
|
|
user_error("Tried to add files to an ElectronicDelivery_OrderStatus object without an OrderID"); |
193
|
|
|
} |
194
|
|
|
if ($this->debug) { |
195
|
|
|
debug::log(print_r($listOfFiles, 1)); |
196
|
|
|
debug::log("COUNT: ".$listOfFiles->count()); |
197
|
|
|
} |
198
|
|
|
//are there any files? |
199
|
|
|
if ($listOfFiles && $listOfFiles->count()) { |
200
|
|
|
if ($this->debug) { |
201
|
|
|
debug::log("doing it"); |
202
|
|
|
} |
203
|
|
|
//create folder |
204
|
|
|
$fullFolderPath = $this->getOrderDownloadFolder(true); |
|
|
|
|
205
|
|
|
$folderOnlyPart = $this->getOrderDownloadFolder(false); |
|
|
|
|
206
|
|
|
$existingFiles = $this->Files(); |
|
|
|
|
207
|
|
|
$alreadyCopiedFileNameArray = array(); |
208
|
|
|
|
209
|
|
|
//loop through files |
210
|
|
|
foreach ($listOfFiles as $file) { |
211
|
|
|
if ($file->exists() && file_exists($file->getFullPath())) { |
212
|
|
|
$existingFiles->add($file); |
213
|
|
|
$copyFrom = $file->getFullPath(); |
214
|
|
|
$fileName = $file->Name; |
215
|
|
|
$destinationFile = $fullFolderPath."/".$file->Name; |
216
|
|
|
$destinationURL = Director::absoluteURL("/".$this->getBaseFolder(false)."/".$folderOnlyPart."/".$fileName); |
217
|
|
|
if (!in_array($copyFrom, $alreadyCopiedFileNameArray)) { |
218
|
|
|
$alreadyCopiedFileNameArray[] = $fileName; |
219
|
|
|
if (copy($copyFrom, $destinationFile)) { |
220
|
|
|
if ($this->debug) { |
221
|
|
|
debug::log("\r\n COPYING $copyFrom to $destinationFile \r\n |||".serialize($file)); |
222
|
|
|
} |
223
|
|
|
$this->Note .= '<li><a href="'.$destinationURL.'" target="_blank">'.$file->Title.'</a></li>'; |
|
|
|
|
224
|
|
|
} |
225
|
|
|
} else { |
226
|
|
|
$this->Note .= "<li>"._t("OrderLog.NOTINCLUDEDIS", "no download available: ").$file->Title."</li>"; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} else { |
231
|
|
|
$this->Completed = true; |
|
|
|
|
232
|
|
|
$this->Note .= "<li>"._t("OrderStatusLog.THEREARENODOWNLOADSWITHTHISORDER", "There are no downloads for this order.")."</li>"; |
|
|
|
|
233
|
|
|
} |
234
|
|
|
$this->Note .= "</ul>"; |
|
|
|
|
235
|
|
|
$this->write(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* checks if the download has expired (i.e. too much time has passed) |
241
|
|
|
* @return Boolean |
242
|
|
|
*/ |
243
|
|
|
public function IsExpired() |
244
|
|
|
{ |
245
|
|
|
if ($this->Completed) { |
|
|
|
|
246
|
|
|
return true; |
247
|
|
|
} |
248
|
|
|
if (!$this->Created) { |
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
return (strtotime("Now") - strtotime($this->Created)) > (60 * 60 * $this->NumberOfHoursBeforeDownloadGetsDeleted); |
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Standard SS method |
256
|
|
|
* Creates the folder and files. |
257
|
|
|
*/ |
258
|
|
|
public function onBeforeWrite() |
259
|
|
|
{ |
260
|
|
|
parent::onBeforeWrite(); |
261
|
|
|
if (!$this->IsExpired()) { |
262
|
|
|
$this->FolderName = $this->getOrderDownloadFolder(true); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Standard SS method |
268
|
|
|
* If it has expired, then the folder is deleted... |
269
|
|
|
*/ |
270
|
|
|
public function onAfterWrite() |
271
|
|
|
{ |
272
|
|
|
parent::onAfterWrite(); |
273
|
|
|
$this->deleteFolderIfExpired(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* making sure we dont end up in an infinite loop |
278
|
|
|
* @var int |
279
|
|
|
*/ |
280
|
|
|
private $loopEscape = 0; |
281
|
|
|
|
282
|
|
|
public function deleteFolderIfExpired() |
283
|
|
|
{ |
284
|
|
|
if ($this->FolderName) { |
|
|
|
|
285
|
|
|
if ($this->Completed) { |
|
|
|
|
286
|
|
|
//do nothing ... |
287
|
|
|
} else { |
288
|
|
|
if ($this->IsExpired() && $this->loopEscape > 0) { |
289
|
|
|
$this->loopEscape++; |
290
|
|
|
$this->Note = "<p>"._t("OrderStatusLog.DOWNLOADSHAVEEXPIRED", "Downloads have expired.")."</p>"; |
|
|
|
|
291
|
|
|
$this->Completed = $this->deleteFolderContents(); |
|
|
|
|
292
|
|
|
$this->write(); |
293
|
|
|
} elseif ($this->loopEscape == 10) { |
294
|
|
|
user_error("Tried to deleted ".$this->FolderName." 10 times without success", E_USER_NOTICE); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
} else { |
|
|
|
|
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Standard SS method |
303
|
|
|
* Deletes the files in the download folder, |
304
|
|
|
* and the actual download folder itself. |
305
|
|
|
*/ |
306
|
|
|
public function onBeforeDelete() |
307
|
|
|
{ |
308
|
|
|
parent::onBeforeDelete(); |
309
|
|
|
if ($this->FolderName && !$this->Completed) { |
|
|
|
|
310
|
|
|
$this->deleteFolderContents(); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* returns the list of files that are in the current folder |
316
|
|
|
* @return Array |
317
|
|
|
*/ |
318
|
|
|
protected function getFilesInFolder() |
319
|
|
|
{ |
320
|
|
|
if ($this->FolderName && file_exists($this->FolderName)) { |
|
|
|
|
321
|
|
|
return $this->getDirectoryContents($this->FolderName, $showFiles = 1, $showFolders = 0); |
|
|
|
|
322
|
|
|
} else { |
323
|
|
|
return array(_t("OrderStatus.NOFOLDER", "No folder is associated with this download entry.")); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* creates a folder and returns the full folder path |
329
|
|
|
* if the folder is already created it still returns the folder path, |
330
|
|
|
* but it does not create the folder. |
331
|
|
|
* |
332
|
|
|
* @param Boolean $absolutePath |
333
|
|
|
* |
334
|
|
|
* @return NULL | String |
335
|
|
|
*/ |
336
|
|
|
protected function getOrderDownloadFolder($absolutePath = true) |
337
|
|
|
{ |
338
|
|
|
//already exists - do nothing |
339
|
|
|
if ($this->FolderName) { |
|
|
|
|
340
|
|
|
$fullFolderName = $this->FolderName; |
|
|
|
|
341
|
|
|
} elseif ($baseFolder = $this->getBaseFolder(true)) { |
|
|
|
|
342
|
|
|
//create folder.... |
343
|
|
|
$randomFolderName = substr(md5(time()+rand(1, 999)), 0, $this->Config()->get("random_folder_name_character_count"))."_".$this->OrderID; |
|
|
|
|
344
|
|
|
$fullFolderName = $baseFolder."/".$randomFolderName; |
345
|
|
|
if (file_exists($fullFolderName)) { |
346
|
|
|
$allOk = true; |
347
|
|
|
} else { |
348
|
|
|
$allOk = mkdir($fullFolderName, $this->getFolderPermissions()); |
349
|
|
|
} |
350
|
|
|
if (!file_exists($fullFolderName)) { |
351
|
|
|
user_error("Can not create folder: ".$fullFolderName); |
352
|
|
|
return; |
353
|
|
|
} |
354
|
|
|
if ($allOk) { |
355
|
|
|
$this->FolderName = $fullFolderName; |
|
|
|
|
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
if ($absolutePath) { |
359
|
|
|
return $fullFolderName; |
|
|
|
|
360
|
|
|
} else { |
361
|
|
|
//TO DO: test |
362
|
|
|
return str_replace($this->getBaseFolder(true)."/", "", $fullFolderName); |
363
|
|
|
} |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* returns the folder in which all the downloads are kept |
368
|
|
|
* (each order has an individual folder within this base folder) |
369
|
|
|
* returns location of base folder. |
370
|
|
|
* |
371
|
|
|
* @param Boolean $absolutePath - absolute folder path (set to false to get relative path) |
372
|
|
|
* |
373
|
|
|
* @return NULL | String |
374
|
|
|
*/ |
375
|
|
|
protected function getBaseFolder($absolutePath = true) |
376
|
|
|
{ |
377
|
|
|
$baseFolderRelative = $this->Config()->get("order_dir"); |
378
|
|
|
$baseFolderAbsolute = Director::baseFolder()."/".$baseFolderRelative; |
379
|
|
|
if (!file_exists($baseFolderAbsolute)) { |
380
|
|
|
mkdir($baseFolderAbsolute, $this->getFolderPermissions()); |
381
|
|
|
} |
382
|
|
|
if (!file_exists($baseFolderAbsolute)) { |
383
|
|
|
user_error("Can not create folder: ".$baseFolderAbsolute); |
384
|
|
|
return; |
385
|
|
|
} |
386
|
|
|
$manifestExcludeFile = $baseFolderAbsolute."/"."_manifest_exclude"; |
387
|
|
View Code Duplication |
if (!file_exists($manifestExcludeFile)) { |
|
|
|
|
388
|
|
|
$manifestExcludeFileHandle = fopen($manifestExcludeFile, 'w') or user_error("Can not create ".$manifestExcludeFile); |
|
|
|
|
389
|
|
|
fwrite($manifestExcludeFileHandle, "Please do not delete this file"); |
390
|
|
|
fclose($manifestExcludeFileHandle); |
391
|
|
|
} |
392
|
|
|
if ($htAccessContent = $this->Config()->get("htaccess_content")) { |
393
|
|
|
$htAccessFile = $baseFolderAbsolute."/".".htaccess"; |
394
|
|
View Code Duplication |
if (!file_exists($htAccessFile)) { |
|
|
|
|
395
|
|
|
$htAccessFileHandle = fopen($htaccessfile, 'w') or user_error("Can not create ".$htAccessFile); |
|
|
|
|
396
|
|
|
fwrite($htAccessFileHandle, $htAccessContent); |
397
|
|
|
fclose($htAccessFileHandle); |
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
if ($absolutePath) { |
401
|
|
|
return $baseFolderAbsolute; |
402
|
|
|
} else { |
403
|
|
|
return $baseFolderRelative; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* returns the permissions for the folder to be created. |
409
|
|
|
* @return String |
410
|
|
|
*/ |
411
|
|
|
protected function getFolderPermissions() |
412
|
|
|
{ |
413
|
|
|
return $this->Config()->get("permissions_on_folder") ? $this->Config()->get("permissions_on_folder") : Config::inst()->get('Filesystem', 'folder_create_mask'); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* get folder contents |
418
|
|
|
* |
419
|
|
|
* @param String $fullPath (e.g. /var/www/mysite.co.nz/downloads) |
420
|
|
|
* @param Boolean $showFiles - list the files in the directory? |
421
|
|
|
* @param Boolean $showFolders - list the folders in the directory? |
422
|
|
|
* |
423
|
|
|
* @return array |
424
|
|
|
*/ |
425
|
|
|
protected function getDirectoryContents($fullPath, $showFiles = false, $showFolders = false) |
426
|
|
|
{ |
427
|
|
|
$files = array(); |
428
|
|
|
if (file_exists($fullPath)) { |
429
|
|
|
if ($directoryHandle = opendir($fullPath)) { |
430
|
|
|
while (($file = readdir($directoryHandle)) !== false) { |
431
|
|
|
/* no links ! */ |
432
|
|
|
$fullFileName = $fullPath."/".$file; |
433
|
|
|
if (substr($file, strlen($file) - 1) != ".") { |
434
|
|
|
if ((!is_dir($fullFileName) && $showFiles) || ($showFolders && is_dir($fullFileName))) { |
435
|
|
|
if (!in_array($file, $this->Config()->get("files_to_be_excluded"))) { |
436
|
|
|
array_push($files, $fullFileName); |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
} |
441
|
|
|
closedir($directoryHandle); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
return $files; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* remove all the folder contents and remove the folder itself |
449
|
|
|
* as well... Returns true on success. |
450
|
|
|
* Assumes that there are no folders in the folder... |
451
|
|
|
* |
452
|
|
|
* @return Boolean |
453
|
|
|
*/ |
454
|
|
|
protected function deleteFolderContents() |
455
|
|
|
{ |
456
|
|
|
if ($this->FolderName) { |
|
|
|
|
457
|
|
|
if (file_exists($this->FolderName)) { |
|
|
|
|
458
|
|
|
$files = $this->getDirectoryContents($this->FolderName, $showFiles = 1, $showFolders = 0); |
|
|
|
|
459
|
|
|
if ($files) { |
|
|
|
|
460
|
|
|
foreach ($files as $file) { |
461
|
|
|
unlink($file); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
return rmdir($this->FolderName); |
|
|
|
|
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
return true; |
468
|
|
|
} |
469
|
|
|
} |
470
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.