/
kFile.class.php
574 lines (484 loc) · 14.2 KB
/
kFile.class.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
/**
* @package infra
* @subpackage Storage
*/
class kFile extends kFileBase
{
const MO_PATTERN = "GNU message catalog";
const TEXT = "text";
const COPY_FAILED_CODE = "COPY_FAILED";
const RENAME_FAILED_CODE = "RENAME_FAILED";
/**
* Returns directory $path contents as an array of :
* array[0] = name
* array[1] = type (dir/file)
* array[2] = filesize
* @param string $path
* @param string $pathPrefix
* @return array
*/
public static function listDir($path, $pathPrefix = '')
{
$fileList = array();
$path = str_ireplace(DIRECTORY_SEPARATOR, '/', $path);
$handle = opendir($path);
if ($handle)
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
$fullPath = $path.'/'.$file;
$tmpPrefix = $pathPrefix.$file;
if (is_dir($fullPath))
{
$tmpPrefix = $tmpPrefix.'/';
$fileList[] = array($tmpPrefix, 'dir', self::fileSize($fullPath));
$fileList = array_merge($fileList, self::listDir($fullPath, $tmpPrefix));
}
else
{
$fileList[] = array($tmpPrefix, 'file', self::fileSize($fullPath));
}
}
}
closedir($handle);
}
return $fileList;
}
// TODO - implement recursion
static public function dirList($directory, $return_directory_as_prefix = true, $should_recurse = false)
{
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = @opendir($directory);
if(! $handler)
KalturaLog::info("dirList $directory does not exist");
// keep going until all files in directory have been read
while($file = readdir($handler))
{
// if $file isn't this directory or its parent,
// add it to the results array
if($file != '.' && $file != '..')
{
$results[] = ($return_directory_as_prefix ? $directory . "/" : "") . $file;
}
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
/*
* Be sure to limit the search with $max_results if not all files are required
*/
static public function recursiveDirList($directory, $return_directory_as_prefix = true, $should_recurse = false, $file_pattern = NULL, $depth = 0, $max_results = -1)
{
if($depth > 10)
{
// exceeded the recursion depth
return NULL;
}
$depth ++;
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = @opendir($directory);
if(! $handler)
return NULL;
// echo ( "directory: " .$directory . "<br>" );
$current_path = pathinfo($directory, PATHINFO_DIRNAME) . "/" . pathinfo($directory, PATHINFO_BASENAME) . "/";
// keep going until all files in directory have been read
while(($file = readdir($handler)) != NULL)
{
// if $file isn't this directory or its parent,
// add it to the results array
if($file != '.' && $file != '..')
{
$match = 1;
if($file_pattern != NULL)
{
$match = preg_match($file_pattern, $file);
}
if($match > 0)
{
$results[] = ($return_directory_as_prefix ? $directory . "/" : "") . $file;
if($max_results > 1 && count($results) > $max_results)
return $results;
}
if($should_recurse && is_dir($current_path . $file))
{
// echo "Recursing... [$should_recurse] [$current_path $file]<br>";
$child_dir_results = self::recursiveDirList($current_path . $file, $return_directory_as_prefix, $should_recurse, $file_pattern, $depth);
if($child_dir_results)
{
$results = kArray::append($results, $child_dir_results);
}
}
}
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
/**
* the result is a list of tuples - files_name , files_size
*/
// TODO - implement recursion
static public function dirListExtended($directory, $return_directory_as_prefix = true, $should_recurse = false, $file_pattern = NULL, $depth = 0, $fetch_content = false)
{
if($depth > 10)
{
// exceeded the recursion depth
return NULL;
}
// create an array to hold directory list
$results = array();
// create a handler for the directory
$handler = @opendir($directory);
if(! $handler)
return NULL;
// echo ( "directory: " .$directory . "<br>" );
$current_path = pathinfo($directory, PATHINFO_DIRNAME) . "/" . pathinfo($directory, PATHINFO_BASENAME) . "/";
// keep going until all files in directory have been read
while(($file = readdir($handler)) != NULL)
{
// if $file isn't this directory or its parent,
// add it to the results array
if($file != '.' && $file != '..')
{
if(! $file_pattern)
$match = 1;
else
$match = preg_match($file_pattern, $file);
if($match > 0)
{
$file_full_path = $directory . "/" . $file;
$result = array();
// first - name (with or without the full path)
$result[] = ($return_directory_as_prefix ? $directory . "/" : "") . $file;
// second - size
$result[] = self::fileSize($file_full_path);
// third - time
$result[] = filemtime($file_full_path);
// forth - content (only if requested
if($fetch_content)
$result[] = file_get_contents($file_full_path);
$results[] = $result;
}
if($should_recurse && is_dir($current_path . $file))
{
// echo "Recursing... [$should_recurse] [$current_path $file]<br>";
$child_dir_results = self::dirListExtended($current_path . $file, $return_directory_as_prefix, $should_recurse, $file_pattern, ++ $depth);
if($child_dir_results)
{
$results = kArray::append($results, $child_dir_results);
}
}
}
}
// tidy up: close the handler
closedir($handler);
// done!
return $results;
}
/**
* copies src to destination.
* Doesn't support non-flat directories!
* One can't use rename because rename isn't supported between partitions.
*/
private static function copyRecursively($src, $dest, $deleteSrc = false) {
if (is_dir($src)) {
// Generate target directory
if (file_exists ($dest)) {
if (! is_dir($dest)) {
KalturaLog::err("Can't override a file with a directory [$dest]");
return false;
}
} else {
if (! mkdir($dest)) {
KalturaLog::err("Failed to create directory [$dest]");
return false;
}
}
// Copy files
$dir = dir($src);
while ( false !== $entry = $dir->read () ) {
if ($entry == '.' || $entry == '..') {
continue;
}
$newSrc = $src . DIRECTORY_SEPARATOR . $entry;
if(is_dir($newSrc)) {
KalturaLog::err("Copying of non-flat directroeis is illegal");
return false;
}
$res = self::copySingleFile ($newSrc, $dest . DIRECTORY_SEPARATOR . $entry , $deleteSrc);
if (! $res)
return false;
}
// Delete source
if ($deleteSrc && (! rmdir($src))) {
KalturaLog::err("Failed to delete source directory : [$src]");
return false;
}
} else {
self::copySingleFile($src, $dest, $deleteSrc);
}
return true;
}
private static function copySingleFile($src, $dest, $deleteSrc) {
if($deleteSrc)
{
// In case of move, first try to move the file before copy & unlink.
$startTime = microtime(true);
$renameSucceeded = rename($src, $dest);
$timeTook = microtime(true) - $startTime;
KalturaMonitorClient::monitorFileSystemAccess('RENAME', $timeTook, $renameSucceeded ? null : self::RENAME_FAILED_CODE);
if($renameSucceeded)
{
KalturaLog::log("rename took : $timeTook [$src] to [$dest] size: ".filesize($dest));
return true;
}
KalturaLog::err("Failed to rename file : [$src] to [$dest]");
}
$startTime = microtime(true);
$copySucceeded = copy($src,$dest);
$timeTook = microtime(true) - $startTime;
KalturaMonitorClient::monitorFileSystemAccess('COPY', $timeTook, $copySucceeded ? null : self::COPY_FAILED_CODE);
if (!$copySucceeded)
{
KalturaLog::err("Failed to copy file : [$src] to [$dest]");
return false;
}
if ($deleteSrc && (!unlink($src)))
{
KalturaLog::err("Failed to delete source file : [$src]");
return false;
}
return true;
}
public static function moveFile($from, $to, $override_if_exists = false, $copy = false)
{
$from = str_replace("\\", "/", $from);
$to = str_replace("\\", "/", $to);
// Validation
if(!file_exists($from))
{
KalturaLog::err("Source doesn't exist [$from]");
return false;
}
if(strpos($to,'\"') !== false)
{
KalturaLog::err("Illegal destination file [$to]");
return false;
}
// Preperation
if($override_if_exists && is_file($to))
{
self::deleteFile($to);
}
if(! is_dir(dirname($to)))
{
self::fullMkdir($to);
}
// Copy
return self::copyRecursively($from,$to, !$copy);
}
public static function linkFile($from, $to, $overrideIfExists = false, $copyIfLinkFailed = true)
{
$from = str_replace("\\", "/", $from);
$to = str_replace("\\", "/", $to);
if($overrideIfExists && (is_file($to) || is_link($to)))
{
self::deleteFile($to);
}
if(! is_dir(dirname($to)))
{
self::fullMkdir($to);
}
if(!file_exists($from))
{
KalturaLog::err("Source file doesn't exist [$from]");
return false;
}
if(strpos($to,'\"') !== false)
{
KalturaLog::err("Illegal destination file [$to]");
return false;
}
if(symlink($from, $to))
return true;
$out_arr = array();
$rv = 0;
exec("ln -s \"$from\" \"$to\"", $out_arr, $rv);
// echo "RV($rv)\n";
if($rv==0)
return true;
if(!$copyIfLinkFailed)
return false;
return self::moveFile($from, $to, $overrideIfExists, true);
}
//
// downloadHeader - 1 only body, 2 - only header, 3 - both body and header
//
static public function downloadUrlToString($sourceUrl, $downloadHeader = 1, $extraHeaders = null)
{
// create a new curl resource
// TODO - remove this hack !!!
// I added it only because for some reason or other I couldn't get hold of the http_get
/*
if ( function_exists ('http_get'))
{
return http_get($sourceUrl, array('redirect' => 5));
}
else
*/
{
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $sourceUrl);
curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.11.1");
curl_setopt($ch, CURLOPT_HEADER, ($downloadHeader & 2) ? 1 : 0);
curl_setopt($ch, CURLOPT_NOBODY, ($downloadHeader & 1) ? 0 : 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if($extraHeaders)
curl_setopt($ch, CURLOPT_HTTPHEADER, $extraHeaders);
// grab URL and pass it to the browser
$content = curl_exec($ch);
// close curl resource, and free up system resources
curl_close($ch);
}
return $content;
}
public static function getFileData($file_full_path)
{
return new kFileData($file_full_path);
}
public static function getFileDataWithContent($file_full_path)
{
$add_content = (strpos($file_full_path, ".txt") !== false || strpos($file_full_path, ".log") !== false);
return new kFileData($file_full_path, $add_content);
}
public static function read_header($ch, $string)
{
$length = strlen($string);
// we shouldnt return a chunked encoded header as we read the whole response and echo it after curl extracts it
if (stripos($string, "Transfer-Encoding: chunked") === FALSE)
{
header($string);
}
return $length;
}
public static function read_body($ch, $string)
{
$length = strlen($string);
echo $string;
return $length;
}
public static function getRequestHeaders()
{
if(function_exists('apache_request_headers'))
return apache_request_headers();
foreach($_SERVER as $key => $value)
{
if(substr($key, 0, 5) == "HTTP_")
{
$key = str_replace(" ", "-", ucwords(strtolower(str_replace("_", " ", substr($key, 5)))));
$out[$key] = $value;
}
}
return $out;
}
public static function cacheRedirect($url)
{
if (function_exists('apc_store'))
{
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? "https" : "http";
apc_store("redirect-".$protocol.$_SERVER["REQUEST_URI"], $url, 60);
}
}
public static function closeDbConnections()
{
// close all opened db connetion while we end an action with a long executing operation such as dumping a file.
// this will limit the number of concurrent db connections as dumping a file make take a long time
try
{
Propel::close();
}
catch(Exception $e)
{
KalturaLog::err("closeDbConnections: error closing db $e");
}
}
/**
* Check if the file is executable
* @param string $path
*
* @return string
*/
public static function getMediaInfoFormat($path)
{
$mediaInfoParser = new KMediaInfoMediaParser($path);
$mediaInfo = $mediaInfoParser->getMediaInfo();
return $mediaInfo->containerFormat;
}
/**
* Try to find the file type by running the file cmd and match the output to a pattern
* It will return empty string if no pattern was matched
*/
public static function findFileTypeByFileCmd($filePath)
{
$fileType = '';
$realPath = realpath($filePath);
if($realPath)
{
$fileBrief = shell_exec('file -b ' . $realPath);
if(kString::beginsWith($fileBrief,self::MO_PATTERN))
$fileType = 'application/mo';
}
return $fileType;
}
public static function getFileDescription($realPath)
{
return shell_exec('file -b '.$realPath);
}
}
/**
* @package infra
* @subpackage Storage
*/
class kFileData
{
public $exists;
public $full_path = NULL;
public $name = NULL;
public $size = NULL;
public $timestamp = NULL;
public $ext = NULL;
public $content = NULL;
public $raw_timestamp = NULL;
public $last_modified = NULL;
public function __construct($full_file_path, $add_content = false)
{
//debugUtils::st();
$this->full_path = realpath($full_file_path);
$this->exists = file_exists($full_file_path);
$this->name = pathinfo($full_file_path, PATHINFO_BASENAME);
$this->ext = pathinfo($full_file_path, PATHINFO_EXTENSION);
if($this->exists)
{
$this->size = kFile::fileSize($full_file_path);
$this->raw_timestamp = filectime($full_file_path);
$this->timestamp = date("Y-m-d H:i:s.", $this->raw_timestamp);
$this->last_modified = filemtime($full_file_path);
if($add_content)
{
$this->content = file_get_contents($full_file_path);
}
}
}
}