-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonFunctions.inc.php
798 lines (667 loc) · 23.5 KB
/
commonFunctions.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
<?php
/**
* Update the plugin specified URL / Branch
*
* @param $gitURL
* @param string $branch
* @param $pluginName
*/
function updatePluginFromGitHub($gitURL, $branch = "master", $pluginName)
{
global $settings;
logEntry("updating plugin: " . $pluginName);
logEntry("settings: " . $settings['pluginDirectory']);
//create update script
//$gitUpdateCMD = "sudo cd ".$settings['pluginDirectory']."/".$pluginName."/; sudo /usr/bin/git git pull ".$gitURL." ".$branch;
$pluginUpdateCMD = "/opt/fpp/scripts/update_plugin " . $pluginName;
logEntry("update command: " . $pluginUpdateCMD);
exec($pluginUpdateCMD, $updateResult);
//logEntry("update result: ".print_r($updateResult));
//loop through result
return;// ($updateResult);
}
/**
* Returns a list of API servers from the api_servers.json file
*/
function getApiServers($env)
{
global $settings, $pluginName;
$api_server_json_path = $settings['pluginDirectory'] . "/" . $pluginName . "/api_servers.json";
$api_server_json_contents_arr = array();
if (file_exists($api_server_json_path)) {
$api_server_json_contents = file_get_contents($api_server_json_path);
if (isset($api_server_json_contents) && !empty($api_server_json_contents)) {
$api_server_json_contents_arr = json_decode($api_server_json_contents, true);
//if a environment has been supplied, then try to return the server for that environment
if (isset($env) && !empty($env)) {
foreach ($api_server_json_contents_arr as $id => $server) {
//if the server id matches the supplied environment, break and return only that environment
if (strtolower($server['_id']) == strtolower($env)) {
return $server;
break;
}
}
}
return $api_server_json_contents_arr;
}
}
return $api_server_json_contents_arr;
}
/**
* Helper function to write to the plugins logfile
*
* @param $data
*/
function logEntry($data)
{
global $logFile, $myPid, $settings, $pluginName;
if (!isset($logFile) && empty($logFile)) {
$logFile = $settings['logDirectory'] . "/" . $pluginName . ".log";
}
$data = $_SERVER['PHP_SELF'] . " : [" . $myPid . "] " . $data;
$logWrite = fopen($logFile, "a") or die("Unable to open file!");
fwrite($logWrite, date('Y-m-d h:i:s A', time()) . ": " . $data . "\n");
fclose($logWrite);
}
/**
* Fork PHP script into its own process
* This "unhooks" it from the FPPD call and lets by itself
*
* Extracted from rdsToMatrix plugin
*
* @param $argv
* @return string
*/
function fork($argv)
{
global $DEBUG;
$safe_arg = escapeshellarg($argv[4]);
//$safe_arg["arg_2"] = escapeshellarg($arg_2);
$pid = pcntl_fork();
if ($pid == -1) {
// Fork failed
if ($DEBUG)
logEntry("ERROR :: fork failed");
exit(1);
} else if ($pid) {
// We are the parent
if ($DEBUG) {
logEntry("------------");
logEntry("fork parent");
logEntry("------------");
}
return "Parent";
// Can no longer use $db because it will be closed by the child
// Instead, make a new MySQL connection for ourselves to work with
} else {
if ($DEBUG) {
logEntry("------------");
logEntry("fork child");
logEntry("------------");
}
//logEntry("sleeping 5 seconds, processing, thensleeping agin");
//Process the callback
processCallback($argv);
return "Child";
}
}
/**
* Process the FPPD callback (via fork)
*
* modified from from rdsToMatrix plugin
*
* @param $argv
*/
function processCallback($argv)
{
global $DEBUG, $pluginName;
// $SEPARATOR = urldecode(ReadSettingFromFile("SEPARATOR", $pluginName));
//if($DEBUG)
//print_r($argv);
//argv0 = program
//argv2 should equal our registration // need to process all the registrations we may have, array??
//argv3 should be --data
//argv4 should be json data
$registrationType = $argv[2];
if ($DEBUG) {
logEntry("registration type: " . $registrationType);
}
//Extract the data from position 4
$data = $argv[4];
logEntry("PROCESSING CALLBACK");
//Switch block to handle
switch ($registrationType) {
case "media":
if ($argv[3] == "--data") {
$data = trim($data);
logEntry("CALLBACK DATA: " . $data);
//Decode the json to an array so we can work on it
$obj = json_decode($data);
$type = $obj->{'type'};
switch ($type) {
case "sequence":
logEntry("WARNING :: SEQUENCE ENTRY: !!NOT!! SUBMITTING SEQUENCE NAME");
//Sequence only
$sequenceName = $obj->{'Sequence'};
//Process now playing item
// processSequenceName($obj->{'Sequence'});
if (isset($sequenceName) && !empty($sequenceName)) {
// logEntry("SENDING NOW PLAYING: (sequence) " . $sequenceName);
// submitNowPlaying($sequenceName, null, 60);
}
break;
case "media":
logEntry("MEDIA ENTRY: EXTRACTING TITLE AND ARTIST");
$songTitle = $obj->{'title'};
$songArtist = $obj->{'artist'};
$songLength = $obj->{'length'};
$songTitleFull = "";
//check to see if the title and artist are set (incase they for whatever reason have not been set in the tags
if (!empty($songTitle) && !empty($songArtist)) {
//Build to the song title
$songTitleFull = $songTitle . " - " . $songArtist;
}
//Process now playing item if we have a song title
if (isset($songTitleFull) && !empty($songTitleFull)) {
//Print log output
logEntry("SENDING NOW PLAYING: " . $songTitleFull);
submitNowPlaying(null, $songTitleFull, $songLength);
} else {
//log message to say we couldn't get the title and artist
//Print log output
logEntry("ERROR :: Could not extract Media Title or Artist please check your MP3 tags: " . $obj->{'Media'});
}
break;
case "both":
logEntry("MEDIA ENTRY: EXTRACTING TITLE AND ARTIST");
$songTitle = $obj->{'title'};
$songArtist = $obj->{'artist'};
$songLength = $obj->{'length'};
$songTitleFull = "";
//Build to the song title
if (!empty($songTitle) && !empty($songArtist)) {
//Build to the song title
$songTitleFull = $songTitle . " - " . $songArtist;
}
//Process now playing item
if (isset($songTitleFull) && !empty($songTitleFull)) {
logEntry("SENDING NOW PLAYING: " . $songTitleFull);
submitNowPlaying(null, $songTitleFull, $songLength);
} else {
//log message to say we couldn't get the title and artist
//Print log output
logEntry("ERROR :: Could not extract Media Title or Artist please check your MP3 tags: " . $obj->{'Media'});
}
break;
default:
logEntry("WARNING :: We do not understand: type: " . $obj->{'type'} . " at this time");
exit(0);
break;
}
}
break;
exit(0);
default:
exit(0);
}
}
/**
* Stop FPPD
* Pulled from fppxml.php
*
*/
function StopFPPD()
{
global $SUDO;
SendCommand('d'); // Ignore return and just kill if 'd' doesn't work...
$status = exec($SUDO . " " . dirname(dirname(__FILE__)) . "/scripts/fppd_stop");
// EchoStatusXML('true');
}
/**
* START FPPD
* Pulled from fppxml.php
*
*/
function StartFPPD()
{
global $settingsFile, $SUDO;
$status = exec("if ps cax | grep -q fppd; then echo \"true\"; else echo \"false\"; fi");
if ($status == 'false') {
$status = exec($SUDO . " " . dirname(dirname(__FILE__)) . "/scripts/fppd_start");
}
// EchoStatusXML($status);
}
/**
* Restart FPD
*
* Pulled from fppxml.php
*
*/
function FPP_RestartFPPD()
{
global $SUDO, $FPC_FPPD_START, $FPC_FPPD_STOP;
if (!isset($FPC_FPPD_START)) {
$FPC_FPPD_START = "/usr/bin/sudo /opt/fpp/scripts/fppd_start";
}
if (!isset($FPC_FPPD_STOP)) {
$FPC_FPPD_STOP = "/usr/bin/sudo /opt/fpp/scripts/fppd_stop";
}
//Stop FPPD
exec($FPC_FPPD_STOP);
logEntry("FPP_RestartFPPD: !!WARNING FPPD RESTART!! Stopping FPPD.. wait 2 seconds");
//sleep 2 seconds
sleep(2);
//Check it's not runing & start it again
$status = exec("if ps cax | grep -q fppd; then echo \"true\"; else echo \"false\"; fi");
if ($status == 'false') {
logEntry("FPP_RestartFPPD: !!WARNING FPPD RESTART!! FPPD has stopped, attempting FPP Daemon start");
//start FPPD now, we could do another status check but seems excessive
$status = exec($FPC_FPPD_START);
}
}
/**
* called by vote_check to work out our current status when performing functions
* Pulled from fppjson.php
*
* //{
* // "fppd": "running",
* // "mode": 2,
* // "mode_name": "player",
* // "status": 0,
* // "status_name": "idle",
* // "volume": 41,
* // "time": "Sun Oct 22 13:08:20 AEST 2017",
* // "current_playlist": {
* // "playlist": "",
* // "type": "",
* // "index": "0",
* // "count": "0"
* // },
* // "current_sequence": "",
* // "current_song": "",
* // "seconds_played": "0",
* // "seconds_remaining": "0",
* // "time_elapsed": "00:00",
* // "time_remaining": "00:00",
* // "next_playlist": {
* // "playlist": "2016_ITS_PIX_TREE",
* // "start_time": "Saturday @ 00:00:00 - (Weekends)\n"
* // },
* // "repeat_mode": 0
* //}
*
* @param $status String status from fpp CLI client
* @return array
*/
function FPP_parseStatus($status)
{
$modes = [
0 => 'unknown',
1 => 'bridge',
2 => 'player',
6 => 'master',
8 => 'remote'
];
$statuses = [
0 => 'idle',
1 => 'playing',
2 => 'stopping gracefully'
];
$status = explode(',', $status, 14);
$mode = (int)$status[0];
$fppStatus = (int)$status[1];
if ($mode == 1) {
return [
'fppd' => 'running',
'mode' => $mode,
'mode_name' => $modes[$mode],
'status' => $fppStatus,
];
}
$baseData = [
'fppd' => 'running',
'mode' => $mode,
'mode_name' => $modes[$mode],
'status' => $fppStatus,
'status_name' => $statuses[$fppStatus],
'volume' => (int)$status[2],
'time' => exec('date'),
];
if ($mode == 8) {
$data = [
'playlist' => $status[3],
'sequence_filename' => $status[3],
'media_filename' => $status[4],
'seconds_elapsed' => $status[5],
'seconds_remaining' => $status[6],
'time_elapsed' => FPP_parseTimeFromSeconds((int)$status[5]),
'time_remaining' => FPP_parseTimeFromSeconds((int)$status[6]),
];
} else {
if ($fppStatus == 0) {
$data = [
'next_playlist' => [
'playlist' => $status[3],
'start_time' => $status[4]
],
'repeat_mode' => 0,
];
} else {
$data = [
'current_playlist' => [
'playlist' => pathinfo($status[3])['filename'],
'type' => $status[4],
'index' => $status[7],
'count' => $status[8]
],
'current_sequence' => $status[5],
'current_song' => $status[6],
'seconds_played' => $status[9],
'seconds_remaining' => $status[10],
'time_elapsed' => FPP_parseTimeFromSeconds((int)$status[9]),
'time_remaining' => FPP_parseTimeFromSeconds((int)$status[10]),
'next_playlist' => [
'playlist' => $status[11],
'start_time' => $status[12]
],
'repeat_mode' => (int)$status[13],
];
}
}
return array_merge($baseData, $data);
}
/**
* Converts seconds to minute:seconds format
*
* @param $seconds
* @return string|void
*/
function FPP_parseTimeFromSeconds($seconds) {
if(!is_numeric($seconds)) {
return;
}
$minutes = (int) floor($seconds/60);
$seconds = (int) $seconds % 60;
return sprintf('%s:%s', str_pad($minutes, 2, 0, STR_PAD_LEFT), str_pad($seconds, 2, 0, STR_PAD_LEFT));
}
/**
* Creates the Event related to this plugin, to be used in playlists or other areas
*/
function createCheckVotingApi_Event()
{
global $DEBUG, $settings, $eventDirectory, $pluginName;
//check that the eventDirectory exists.. just in case
if (file_exists($eventDirectory)) {
logEntry("createCheckVotingApi_Event: Event directory exists!!");
//Find if the event exists already, returned value is true/false
$EVENT_FILE_CHECK = checkEventFilesForKey("VOTE_CHECK_API");
//If no event
if (!$EVENT_FILE_CHECK) {
logEntry("createCheckVotingApi_Event: Event doesn't exist Creating!!");
//Get the next avail event id's
$nextEventFilename = getNextEventFilename();
// $newIndex = $MAJOR_INDEX . "_" . $MINOR_INDEX . ".fevt";
//remove ext, explode and get the major and miner index
$tmp_event_filename = str_ireplace(".fevt", "", $nextEventFilename);
$tmp_event_filename = explode("_", $tmp_event_filename);
if (array_key_exists(0, $tmp_event_filename) && array_key_exists(1, $tmp_event_filename)) {
$MAJOR = $tmp_event_filename[0];
$MINOR = $tmp_event_filename[1];
} else {
$MAJOR = substr($nextEventFilename, 0, 2);
$MINOR = substr($nextEventFilename, 3, 2);
}
//build the event file contents
$eventData = "";
$eventData = "majorID=" . (int)$MAJOR . "\n";
$eventData .= "minorID=" . (int)$MINOR . "\n";
$eventData .= "name='VOTE_CHECK_API'\n";
$eventData .= "effect=''\n";
$eventData .= "startChannel=\n";
$eventData .= "script='voteCheckAPI.sh'\n";
// echo "eventData: ".$eventData."<br/>\n";
logEntry("createCheckVotingApi_Event: EventData: " . json_encode($eventData));
//write out the event file
file_put_contents($eventDirectory . "/" . $nextEventFilename, $eventData);
//Create the script file that points to our vote_check script
$scriptCMD = $settings['pluginDirectory'] . "/" . $pluginName . "/" . "vote_check.php";
logEntry("createCheckVotingApi_Event: ScriptCMD: " . json_encode($scriptCMD));
createScriptFile("voteCheckAPI.sh", $scriptCMD);
} else {
logEntry("Existing event file present");
}
}
//echo "$key => $val\n";
}
/**
* Creates shell script file to run the specified php file
*
* Extracted from rdsToMatrix plugin
*
* @param $scriptFilename
* @param $scriptCMD
*/
function createScriptFile($scriptFilename, $scriptCMD)
{
global $scriptDirectory, $pluginName;
$scriptFilename = $scriptDirectory . "/" . $scriptFilename;
logEntry("Creating script: " . $scriptFilename);
$ext = pathinfo($scriptFilename, PATHINFO_EXTENSION);
$data = "";
$data .= "#!/bin/sh\n";
$data .= "\n";
$data .= "#Script to run " . $pluginName . "\n";
$data .= "#Created by " . $pluginName . "\n";
$data .= "#\n";
$data .= "/usr/bin/php " . $scriptCMD . "\n";
logEntry($data);
$fs = fopen($scriptFilename, "w");
fputs($fs, $data);
fclose($fs);
}
/**
* Get the next available event filename
* return the next event file available for use
*
* Extracted from rdsToMatrix plugin
*
* @return string
*/
function getNextEventFilename()
{
$MAX_MAJOR_DIGITS = 2;
$MAX_MINOR_DIGITS = 2;
global $eventDirectory;
//echo "Event Directory: ".$eventDirectory."<br/> \n";
$MAJOR = array();
$MINOR = array();
$MAJOR_INDEX = 0;
$MINOR_INDEX = 0;
$EVENT_FILES = directoryToArray($eventDirectory, false);
//print_r($EVENT_FILES);
foreach ($EVENT_FILES as $eventFile) {
$eventFileParts = explode("_", $eventFile);
$MAJOR[] = (int)basename($eventFileParts[0]);
//$MAJOR = $eventFileParts[0];
$minorTmp = explode(".fevt", $eventFileParts[1]);
$MINOR[] = (int)$minorTmp[0];
//echo "MAJOR: ".$MAJOR." MINOR: ".$MINOR."\n";
//print_r($MAJOR);
//print_r($MINOR);
}
$MAJOR_INDEX = max(array_values($MAJOR));
$MINOR_INDEX = max(array_values($MINOR));
//echo "Major max: ".$MAJOR_INDEX." MINOR MAX: ".$MINOR_INDEX."\n";
if ($MAJOR_INDEX <= 0) {
$MAJOR_INDEX = 1;
}
if ($MINOR_INDEX <= 0) {
$MINOR_INDEX = 1;
} else {
$MINOR_INDEX++;
}
$MAJOR_INDEX = str_pad($MAJOR_INDEX, $MAX_MAJOR_DIGITS, '0', STR_PAD_LEFT);
$MINOR_INDEX = str_pad($MINOR_INDEX, $MAX_MINOR_DIGITS, '0', STR_PAD_LEFT);
//for now just return the next MINOR index up and keep the same Major
$newIndex = $MAJOR_INDEX . "_" . $MINOR_INDEX . ".fevt";
//echo "new index: ".$newIndex."\n";
return $newIndex;
}
/**
* Returns the file contents of the supplied directory
*
* Extracted from rdsToMatrix plugin
*
* @param $directory
* @param $recursive
* @return array
*/
function directoryToArray($directory, $recursive)
{
$array_items = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($directory . "/" . $file)) {
if ($recursive) {
$array_items = array_merge($array_items, directoryToArray($directory . "/" . $file, $recursive));
}
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
} else {
$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);
}
}
}
closedir($handle);
}
return $array_items;
}
/**
* check all the event files for a string matching this and return true/false if exist
*
* Extracted from rdsToMatrix plugin
*
* @param $keyCheckString
* @return bool
*/
function checkEventFilesForKey($keyCheckString)
{
global $eventDirectory;
$keyExist = false;
$eventFiles = array();
$eventFiles = directoryToArray($eventDirectory, false);
foreach ($eventFiles as $eventFile) {
if (strpos(file_get_contents($eventFile), $keyCheckString) !== false) {
// do stuff
$keyExist = true;
break;
// return $keyExist;
}
}
return $keyExist;
}
/**
* check all the event files for a string matching this and returns the filename of the associated event
*
* @param $keyCheckString
* @return bool
*/
function getEventFileNameForKey($keyCheckString)
{
global $eventDirectory;
$keyExist = false;
$eventFiles = array();
$event_file_found = null;
$eventFiles = directoryToArray($eventDirectory, false);
foreach ($eventFiles as $eventFile) {
//look into the file and try to find the check string
if (strpos(file_get_contents($eventFile), $keyCheckString) !== false) {
//get the event filename, it should be after the last forward slash /
$event_file_bits = explode("/", $eventFile);
$event_file_bits_length = count($event_file_bits) - 1;
//
if (array_key_exists($event_file_bits_length, $event_file_bits)) {
$event_file_found = $event_file_bits[$event_file_bits_length];
$keyExist = true;
break;
}
}
}
return $event_file_found;
}
/**
* Looks in a directory and reads file contents of files within it
* @param $directory String directory to search in
* @param $return_data Boolean switch to include file data
* @return array Array of file names and respective data
*/
function read_directory_files($directory, $return_data = true)
{
$file_list = array();
$file_data = false;
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
// do something with the file
// note that '.' and '..' is returned even
// if file isn't this directory or its parent, add it to the results
if ($file != "." && $file != "..") {
// collect the filenames & data
if ($return_data == true) {
$file_data = explode("\n", file_get_contents($directory . '/' . $file));
}
$file_list[$file] = $file_data;
}
}
closedir($handle);
}
return $file_list;
}
/**
* Tail a log file
*
* @param $filename
* @param int $lines
* @param int $buffer
* @return string
*/
function tail($filename, $lines = 10, $buffer = 4096)
{
$output = '';
if (file_exists($filename)) {
// Open the file
$f = fopen($filename, "rb");
// Jump to last character
fseek($f, -1, SEEK_END);
// Read it and adjust line number if necessary
// (Otherwise the result would be wrong if file doesn't end with a blank line)
if (fread($f, 1) != "\n") $lines -= 1;
// Start reading
$output = '';
$chunk = '';
// While we would like more
while (ftell($f) > 0 && $lines >= 0) {
// Figure out how far back we should jump
$seek = min(ftell($f), $buffer);
// Do the jump (backwards, relative to where we are)
fseek($f, -$seek, SEEK_CUR);
// Read a chunk and prepend it to our output
$output = ($chunk = fread($f, $seek)) . $output;
// Jump back to where we started reading
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
// Decrease our line counter
$lines -= substr_count($chunk, "\n");
}
// While we have too many lines
// (Because of buffer size we might have read too many)
while ($lines++ < 0) {
// Find first newline and remove all text before that
$output = substr($output, strpos($output, "\n") + 1);
}
// Close file and return
fclose($f);
}
return $output;
}
?>