-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPHPDocFill.php
1646 lines (1447 loc) · 56.8 KB
/
PHPDocFill.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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PHPDocFill - Easily Document your PHP Scripts
* Copyright (C) 2013 Clement Nedelcu
* Version 1.0 BETA 2
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/
* @package PHPDocFill
*/
//////////////////////////////////////
// -CONFIGURATION- //
// Set a password to access the app //
define("APP_USER", "admin");
// Note: 8 characters minimum
define("APP_PASSWORD", "salutpoilu"); //
// Or change this (not recommended) //
define("REQUIRES_PASSWORD", true);
session_start();
// Path of dir to scan without the last slash
if (isset($_POST['dir']) && !empty($_POST['dir'])) {
$_SESSION['project']['dir'] = htmlentities($_POST['dir']);
} elseif(!isset($_SESSION['project']['dir']) ){
$_SESSION['project']['dir'] = __DIR__;
}
define("DIR_TO_SCAN",$_SESSION['project']['dir']);
// URL of dir to scan
if (isset($_POST['url']) && !empty($_POST['url'])) {
$_SESSION['project']['url'] = htmlentities($_POST['url']);
} elseif(!isset($_SESSION['project']['url']) ){
$_SESSION['project']['url'] = '';
}
define("URL_OF_DIR_TO_SCAN",$_SESSION['project']['url']);
// Name of project
if (isset($_POST['name']) && !empty($_POST['name'])) {
$_SESSION['project']['name'] = htmlentities($_POST['name']);
} elseif(!isset($_SESSION['project']['name']) ){
$_SESSION['project']['name'] = "MY PROJECT";
}
define("MY_PROJECT",$_SESSION['project']['name']);
//////////////////////////////////////
///////////////////////
// CODE BEGINS BELOW //
// Block types
define("BLOCK_PAGE", "PAGE");
define("BLOCK_CODE", "CODE");
define("T_SPECIAL_BRACKET",15090134);
/**
* Represents one actual document-block comment
* Static class containing dynamic methods (constructor, outputs...) and static methods (InsertDocBlock, ...)
* Handles all the docblock related work
*/
class DocBlock {
var $valid = false;
var $tags = "";
var $shortDescription = "";
var $longDescription = "";
var $type = 0;
var $comment = null;
var $associatedCode = null;
var $associatedHlCode = null;
var $line = 0;
var $docline = 0;
var $indent = "";
/**
* Builds a docblock object from a raw comment
* @param string $comment The raw comment read from the source file (optional)
* @param string $associatedCode The code line associated to the docblock (optional)
* @param string $blockType The type of block being created (optional)
*/
function __construct($comment="", $associatedCode="", $blockType="") {
if (!$comment && !$associatedCode && !$blockType) return;
// Figure out indent (for later output)
if ( preg_match('@^([\t\040]*).*$@', $associatedCode, $matches) ) {
$this->indent = $matches[1];
}
// Save tokens
$this->comment = trim($comment);
$this->associatedCode = $associatedCode;
$this->associatedHlCode = @str_ireplace("<?php ","",@highlight_string("<?php ".trim($this->associatedCode),true));
$this->type = $blockType;
// What's the source?
$source = $comment; // element 1 is the token source
$source = trim($source);
$source = str_replace("/**","",$source);
$source = str_replace("*/","",$source);
$lines = explode("\n", $source);
$source = array();
foreach($lines as $line) {
$line = trim($line);
$line = str_replace("\r", "", $line);
// If the line doesn't start by *, we aren't interested
if (substr($line,0,1) == "*" ) $source[] = trim(substr($line,1));
}
if (!count($source)) return false;
// Short description?
if (substr($source[0],0,1) != "@") {
$this->shortDescription = $source[0];
}
// Long description?
for($i=1; $i<count($source); $i++) {
if (substr($source[$i],0,1) == "@") break;
$this->longDescription .= $source[$i]."\n";
}
$this->longDescription = trim($this->longDescription);
// Tags?
$this->tags = array();
for($i=count($source)-1; $i>=0; $i--) {
if (trim($source[$i]) == "") continue;
if (substr($source[$i],0,1) != "@") break;
$this->tags[] = $source[$i];
}
$this->tags = array_reverse($this->tags);
$this->tags = implode(PHP_EOL,$this->tags);
// There needs to be at least a short description, or a long description, or a tags section for the block to be valid
if ($this->shortDescription || $this->longDescription || $this->tags) $this->valid = true;
// Convert to UTF8
$this->shortDescription = !isUtf8($this->shortDescription) ? utf8_encode($this->shortDescription) : $this->shortDescription;
$this->longDescription = !isUtf8($this->longDescription) ? utf8_encode($this->longDescription) : $this->longDescription;
$this->tags = !isUtf8($this->tags) ? utf8_encode($this->tags) : $this->tags;
return $this->valid;
}
/**
* Provides an output of the dobclock as array of string
* @param bool $utf8 if true, result will be sent UTF8-encoded; false will be UTF8-decoded or left as is
* @return array An array of strings to be imploded and saved in a file
*/
function Output($utf8=false) {
$out = array();
$out[] = "/**";
if ($this->shortDescription)
$out[] = " * ".str_replace("\r\n"," ",str_replace("\n", " ",$this->shortDescription));
if ($this->longDescription) {
$lines = preg_split("/((\r?\n)|(\n?\r))/", $this->longDescription);
// do we insert a blank line?
//if ($this->shortDescription) // there is already a short description, so, yep
// $out[] = " *";
foreach($lines as $line)
$out[] = " * ".$line;
}
if ($this->tags) {
$lines = preg_split("/((\r?\n)|(\n?\r))/", $this->tags);
foreach($lines as $line)
$out[] = " * ".$line;
}
$out[] = " */";
// do we want UTF8 ?
$result = array();
$indent = $this->indent;
$i = 0;
foreach($out as $o) {
if ($i == 1) $indent .= " ";
$i++;
if (isUtf8($o) && !$utf8) // string is in UTF8, but we DONT want UTF8
$result[] = utf8_decode($indent.trim($o));
elseif (!isUtf8($o) && $utf8) // string is NOT in UTF8, but we WANT UTF8
$result[] = utf8_encode($indent.trim($o));
else
$result[] = $indent.trim($o);
}
return $result;
}
/**
* Outputs the docblock as a trimmed string comment (for use in a textarea or tooltip)
* @return string The docblock as string, including opening and closing tags
*/
function OutputComment() {
$pagecomment = trim($this->shortDescription) ? PrepString($this->shortDescription)."<br />" : "";
$pagecomment .= trim($this->longDescription) ? PrepString($this->longDescription)."<br />" : "";
$pagecomment .= trim($this->tags) ? "<i>".PrepString($this->tags)."</i>" : "";
$pagecomment = $pagecomment ? $pagecomment : "<i>(Undocumented)</i>";
$pagecomment = "<div class='pagecomment'>$pagecomment</div>";
return $pagecomment;
}
/**
* Returns the docblock "name"
* The docblock name is the name of the object being commented.
* For example the name of a function, of a class, of an interface...
* It is mostly used for generating the documentation manual.
* @return string The name of the object being commented
*/
function GetName() {
if ($this->type == BLOCK_PAGE) return "";
$source = "<?php ".trim($this->associatedCode);
$interesting = array( T_FUNCTION, T_STRING, T_VARIABLE, T_CLASS, T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE, T_REQUIRE_ONCE, T_INTERFACE, T_CONSTANT_ENCAPSED_STRING );
$alltokens = token_get_all($source);
$tokens = array();
foreach($alltokens as $token) {
if (!in_array($token[0],$interesting)) continue;
//if ($token[0] == T_STRING && strtolower($token[1]) != "define") continue; // a string, but not "define"... not useful to us
$tokens[] = $token;
}
switch($this->type) {
case "T_FUNCTION": return $tokens[1][1];
case "T_CLASS": return $tokens[1][1];
case "T_INTERFACE": return $tokens[1][1];
case "T_STRING": return $tokens[1][1];
case "T_INCLUDE": case "T_INCLUDE_ONCE": case "T_REQUIRE": case "T_REQUIRE_ONCE": return $tokens[1][1];
}
return $this->type;
}
/**
* Returns the class of the code element concerned by the docblock
* If the docblock is about a class, this function returns "class"
* If the docblock is about a function, this function returns "function"
* Check the code to see the list of possible values
* @return string The type of element associated to the docblock (class, function, ...)
*/
function GetClass() {
switch($this->type) {
case BLOCK_PAGE: return "page";
case "T_FUNCTION": return "function";
case "T_CLASS": return "class";
case "T_INTERFACE": return "interface";
case "T_STRING": return "define";
case "T_INCLUDE": case "T_INCLUDE_ONCE": case "T_REQUIRE": case "T_REQUIRE_ONCE": return "include";
}
return "";
}
/**
* Parses code to build the list of commentable elements and their associated docblocks
* Using the token_get_all function to generate the list of tokens from the given PHP source code,
* this function analyzes your code and attempts to detect all commentable elements as well as their associated docblocks.
* @param string $source The source code to analyze
* @return array An array of DocBlock instances
*/
static function ParseCode($source) {
global $phpTokens;
$alltokens = token_get_all($source);
$tokens = array();
$interesting = array(
T_DOC_COMMENT, T_FUNCTION, T_STRING, T_VARIABLE, T_CLASS, T_INCLUDE, T_INCLUDE_ONCE, T_REQUIRE,
T_REQUIRE_ONCE, T_CONSTANT_ENCAPSED_STRING, T_DNUMBER,T_LNUMBER, T_SPECIAL_BRACKET, T_INTERFACE);
$opentagfound = false;
foreach($alltokens as $token) {
if ($token[0] == T_OPEN_TAG) $opentagfound = true;
if (!is_array($token) && $token != ")") continue;
if ($token == ")") $token = array(T_SPECIAL_BRACKET, ")", 0);
if (!in_array($token[0], $interesting)) continue;
$token[3] = @token_name($token[0]);
$tokens[] = $token;
}
// NO open tag? there is no PHP code at all?
if (!$opentagfound) return null;
// Find Php Comment blocks
$blocks = array();
// Now, we have all interesting & consecutive tokens
$pageDocBlock = null;
$lines = preg_split("/((\r?\n)|(\n?\r))/", $source);
// Check all tokens
for($i=0; $i<count($tokens); $i++) {
$token = @$tokens[$i];
$nextToken = @$tokens[$i+1];
$previousToken = @$tokens[$i-1];
// Trying to detect if there is a Page Docblock
// First doc bloc should be a T_DOC_COMMENT.
// It should contain @package
if ($i == 0 && $token[0] == T_DOC_COMMENT) {
if (stripos($token[1], "@package") !== FALSE) {
// We found the page docblock!
$block = new DocBlock($token[1], "(Page DocBlock)", BLOCK_PAGE);
$block->line = $token[2];
$block->docline = $token[2];
$blocks[] = $block;
$pageDocBlock = $block;
continue;
}
}
// So, what's this comment block for?
if (!in_array($token[0],array(T_FUNCTION,T_CLASS,T_INTERFACE,T_STRING,T_INCLUDE,T_INCLUDE_ONCE,T_REQUIRE,T_REQUIRE_ONCE))) continue; // nothing of interest (bad syntax?) => continue...
if ($token[0] == T_STRING && strtolower($token[1]) != "define") continue; // a string, but not "define"... not useful to us
// Filter out elements we don't want to see
if ($token[0] == T_FUNCTION && !$_POST['Dfunc']) continue;
elseif ($token[0] == T_CLASS && !$_POST['Dclas']) continue;
elseif ($token[0] == T_INTERFACE && !$_POST['Dinte']) continue;
elseif ($token[0] == T_STRING && !$_POST['Dcons']) continue;
elseif (in_array($token[0], array(T_INCLUDE,T_INCLUDE_ONCE,T_REQUIRE,T_REQUIRE_ONCE)) && !$_POST['Dincl']) continue;
// Filter out anonymous functions (non anonymous functions have their name/T_STRING right after the keyword 'function')
if ($token[0] == T_FUNCTION && $nextToken[0] != T_STRING) continue;
// Do we have a docblock for this token?
$relevantDocBlock = null;
if ($previousToken[0] == T_DOC_COMMENT) {
// Is it the page token?
if ($i == 1 && $pageDocBlock) { // yup, it's the page token, no action
//$relevantDocBlock = null;
} else {
// This is a doc block likely meant for the following object
$relevantDocBlock = $previousToken;
}
}
// Create empty docblock
if (!$relevantDocBlock) {
$relevantDocBlock = array(T_DOC_COMMENT,"/**\r\n*\r\n*/",0);
}
// Find out associated tokens, depending on the current token type
$associatedCode = $lines[ $token[2]-1 ];
$type = $token[3];
$block = new DocBlock($relevantDocBlock[1],$associatedCode, $type);
$block->line = $token[2];
$block->docline = $relevantDocBlock[2];
//echo "<pre>"; print_r(htmlentities($lines));die();
$blocks[] = $block;
}
return $blocks;
}
/**
* Calculates the commenting completion index of a file
* @param string $file Path of the file to be analyzed
* @return string An fully formed HTML span tag containing the properly colored percentage
*/
static function AnalyzeFile($file) {
$completion = -1;
$source = @file_get_contents($file);
$complete = 0;
$total = 0;
if ($source) {
$blocks = DocBlock::ParseCode($source);
if ($blocks !== null) {
$pageblock = false;
foreach($blocks as $block) {
$complete += $block->valid ? 1 : 0;
if ($block->type == BLOCK_PAGE) $pageblock = true;
if ($block->type == BLOCK_PAGE && !$_POST['Dpage']) continue;
$total++;
}
if (!$pageblock && $_POST['Dpage']) $total++;
// Statistics
if ($total > 0) {
$completion = round( $complete / $total * 100);
}
}
}
return DocBlock::PrepareBullet($completion,$complete,$total);
}
/**
* Provides an HTML span tag containing the completion index with the proper color
* @param int $completion The completion percentage
* @param int $done The elements that are already documented
* @param int $total The total amount of commentable elements in the source
* @return string Some HTML code to insert before a file name in the menu
*/
static function PrepareBullet($completion,$done,$total) {
$text = "Documentation completion: $done/$total [$completion%]";
if ($completion == -1) { $color = 'black'; $text = "No PHP code found or nothing to document"; }
elseif ($completion == 0) { $color = '#CC0000'; }
elseif ($completion == 100) { $color = 'green'; }
else $color = 'orange';
$completion = $completion == -1 ? "-" : $completion."%";
return "<span title='$text' style='display: inline-block; margin-right: 5px; width: 25px; font-size: 9px; color: $color;'>$completion</span>";
}
/**
* Insert a docblock into a file before the specified code
* @param string $file Path of the concerned file
* @param string $doc DocBlock instance to insert in the file
* @param string $type Type of docblock to insert
* @param string $code The code to verify against the code found at $line
* @param string $line Line that should contain the code of the associated docblock
*/
function InsertDocBlock($file, $doc, $type, $code="", $line=0) {
// Read file...
$source = @file_get_contents($file);
if (!$source) die("ERR3"); // error 3 => can't read file
// Split the code in an array
$lines = preg_split("/((\r?\n)|(\n?\r))/", $source);
// Figure out line number
// Make sure the retained line contains the same code as previously, otherwise we may have a problem
if ($type != BLOCK_PAGE) {
$linenumber = intval($line);
if (trim($lines[$linenumber-1]) != trim($code) ) die("ERR1");
// Figure out indent
if ( preg_match('@^([\t\040]*).*$@', $lines[$linenumber-1], $matches) ) {
$doc->indent = $matches[1];
}
} else {
// For the page block, we have to find the first open tag and insert after
$tokens = token_get_all($source);
$found = false;
foreach($tokens as $token) {
if ($token[0] == T_OPEN_TAG) {
$found = true;
$linenumber = intval($token[2])+1;
}
}
if (!$found) die("ERR4");
}
// Which line breaks does the source use?
$lbtype = "\n";
if (substr_count($source,"\r\n") > 0) $lbtype = "\r\n";
// Insert docblock
array_splice($lines, $linenumber-1, 0, $lbtype.@implode($lbtype,$doc->Output( isUtf8($source) )) );
// final source
$source = implode($lbtype, $lines);
//rename($file,$file.".bak");
file_put_contents($file, $source);
}
/**
* Update an existing docblock
* @param string $file File in which the docblock should be updated
* @param DocBlock $doc DocBlock instance that contains the text to insert
* @param int $line Code line where the existing docblock should be located
*/
static function UpdateDocBlock($file, $doc, $line) {
// Read file...
$source = @file_get_contents($file);
if (!$source) die("ERR3"); // error 3 => can't read file
// Now, we process the file, and make sure the original comment is still at the right location, or it's been moved
$tokens = token_get_all($source);
$found = false;
$oldcomment = "";
foreach($tokens as $token) {
if ($token[0] == T_DOC_COMMENT && $token[2] == $line) {
$found = true;
$oldcomment = $token[1];
}
}
if (!$found) die("ERR4");
// Set indent
$code = $_POST['Code'];
if ( preg_match('@^([\t\040]*).*$@', $code, $matches) ) {
$doc->indent = $matches[1];
}
// Which line breaks does the source use?
$lbtype = "\n";
if (substr_count($source,"\r\n") > 0) $lbtype = "\r\n";
$final = implode($lbtype, $doc->Output(isUtf8($source)));
// Simply search & replace old docblock
$source = str_replace($oldcomment, trim($final), $source);
//rename($file,$file.".bak");
file_put_contents($file, $source);
}
/**
* Scans current folder and subfolders for .php files
*/
static function AJAX_Scan() {
$list = RecursiveScanDir(DIR_TO_SCAN);
usort($list, "strnatcasecmp");
// No files?
if (!$list || !count($list)) die("No .php files located in this<br />folder and/or subfolders");
// For each file we'll present something nice
$out = "";
foreach($list as $file) {
// If Mode is set to 1, we need to parse the file and check the completion
$analysis = "";
if ($_POST['Mode']) {
$analysis = DocBlock::AnalyzeFile($file);
}
$out .= "
$analysis<a href='#' data-filename='".rawurlencode($file)."' class='filelink'>$file</a><br />
";
}
// Ouptut
echo $out;
// Exit "Gracefully"
exit(0);
}
/**
* Analyzes a given file and produces an HTML table of all commentable elements
*/
static function AJAX_Analyze() {
// Which file?
$file = DIR_TO_SCAN.'/'.$_POST['File'];
// Prevent loading illegal files
if ( strpos($file,":") !== FALSE || strpos($file,"..") !== FALSE || strtolower(substr(pathinfo($file,PATHINFO_EXTENSION),0,3)) != "php") die("Invalid file");
// Does this file exist?
if (!file_exists($file)) die("Error: file not found");
// File too big?
if (filesize($file) > 4*1024*1024) die("Error: this file is over 4 megabytes, parsing would take too long.");
// Read file...
$source = @file_get_contents($file);
if (!$source) die("ERR3");
// Display
$blocks = DocBlock::ParseCode($source);
if ($blocks === null) {
echo "This PHP script does not seem to contain any PHP code. It cannot be documented.";
} else {
//echo "<pre>";
$out = "
<table cellspacing=0 cellpadding=0 id='blocklist'>
<thead>
<tr>
<th> </th>
<th>Done</th>
<th>Line</th>
<th>Code</th>
</tr>
</thead>
<tbody>
";
$pageblock = false;
$complete = 0;
$i = 0;
$rows = "";
foreach($blocks as $block) {
if ($block->type == BLOCK_PAGE) $pageblock = true;
if ($block->type == BLOCK_PAGE && !$_POST['Dpage']) continue;
$jsoncomment = str_replace("\n",'',str_replace("\r",'',str_replace('"','"', strip_tags(implode('<br />',$block->Output(true)),"<br>"))));
$state = $block->valid ? "<span class='valid comment' title='Comment' data-comment=\"$jsoncomment\">Yes</span>" : "-<span class='todo'>No</span>-";
$jsonobj = json_encode($block);
$rows .= "
<tr class='".($i%2==1?"odd":"")."'>
<td><button data-index='$i' id='action$i' data-type='{$block->type}' data-obj=\"". str_replace('"','"', $jsonobj)."\" class='action'>Edit</button></td>
<td align=center>$state</td>
<td align=center><a href='#line".$block->line."' style='text-decoration: none;' title='Jump to code (when "Display Source" option is enabled)'>".$block->line."</a></td>
<td>".($block->type != BLOCK_PAGE ? $block->associatedHlCode : "(Page DocBlock)")."</td>
</tr>
";
$complete += $block->valid ? 1 : 0;
$i++;
}
if (!$pageblock && $_POST['Dpage']) $rows = "<tr class='dbmissing'><td><button data-stuff='' class='action'>Edit</button></td><td align=center colspan=3><span class=todo>Page DocBlock is missing</span></td></tr>".$rows;
$out .= $rows."</tbody></table>";
// Statistics
$total = count($blocks);
if (!$pageblock) $total++;
$completion = round( $complete / $total * 100);
$out .= "<br />Current code commenting completion: <b>$completion%</b><br />";
// No rows at all?
if (!$rows) $out = "No code to document.";
}
// Output
echo $out;
// Do we show the source?
if ($_POST['ShowSource']) {
echo "<h2>Source Code</h2>";
$lines = preg_split("/((\r?\n)|(\n?\r))/", $source);
echo highlight_php( isUtf8($source) ? implode(PHP_EOL,$lines) : utf8_encode(implode(PHP_EOL,$lines)),true);
}
die();
}
/**
* Save changes, either inserting a new docblock or updating an existing docblock
*/
static function AJAX_Save() {
// Which file?
$file = DIR_TO_SCAN.'/'.$_POST['File'];
// Prevent loading illegal files
if ( strpos($file,":") !== FALSE || strpos($file,"..") !== FALSE || strtolower(substr(pathinfo($file,PATHINFO_EXTENSION),0,3)) != "php") die("ERR0");
// Does this file exist?
if (!file_exists($file)) die("ERR2");
// File too big?
if (filesize($file) > 4*1024*1024) die("ERR0");
// Prepare docblock
$doc = new DocBlock("","","");
$doc->shortDescription = str_replace("*/", "", trim($_POST['SD']));
$doc->longDescription = str_replace("*/", "", trim($_POST['LD']));
$doc->tags = str_replace("*/", "", trim($_POST['TAGS']));
// What are we doing? UPDATING or INSERTING?
if ($_POST['MODE'] === "0") { // 0 = Inserting a new doc block
// Which block are we inserting? is it the page docblock?
if ($_POST['Line'] === "0") {
DocBlock::InsertDocBlock( $file, $doc, BLOCK_PAGE );
die("OK");
}
// --Not a page docblock--
DocBlock::InsertDocBlock( $file, $doc, BLOCK_CODE, $_POST['Code'], $_POST['Line'] );
die("OK");
}
elseif ($_POST['MODE'] === "1") { // UPDATING a docblock
// Lets see...
DocBlock::UpdateDocBlock($file, $doc, $_POST['DocLine']);
die("OK");
}
}
/**
* Generates HTML manual as PHPDocFill.html file in the same folder
*/
static function AJAX_Manual() {
$tabfile = '';
$tabfunctions = '';
$tabclasses = '';
$tabint = '';
$tabdef = '';
$tabinc = '';
$fileout = '';
$fnout = '';
$classout = '';
$intout = '';
$defout = '';
$incout = '';
// OK, what do we do?
// 1. scan files
$list = RecursiveScanDir(DIR_TO_SCAN);
usort($list,"strnatcasecmp");
// 2. for each file, we'll gather the insides
$allblocks = array();
$files = array();
$bid = 0;
foreach($list as $file) {
$file = DIR_TO_SCAN.'/'.$file;
$source = @file_get_contents($file);
if (!$source) continue;
$fileblocks = DocBlock::ParseCode($source);
if ($fileblocks === null) continue;
foreach($fileblocks as $block) {
$bid++;
$block->filename = $file;
$block->id = $bid;
$blockkey = $block->GetName()." [{$block->filename}] {$block->line}"; // a unique identifier to sort the classes
$allblocks[$block->GetClass()][$blockkey] = $block;
$files[$file][] = $block;
}
}
// 3. generate list of files output
if ($_POST['Dpage']) {
$fileout = "<div id='files'>";
foreach($files as $filename=>$blocks) {
// Find page docblock
$pagecomment = "";
foreach($blocks as $block)
if ($block->type == BLOCK_PAGE)
$pagecomment = nl2br(str_replace("\n\n","\n",trim($block->shortDescription."\n".$block->longDescription."\n<i>".$block->tags."</i>")));
$pagecomment = !isUtf8($pagecomment) ? utf8_encode($pagecomment) : $pagecomment;
$elements = array();
// Prepare list of contained elements
foreach($blocks as $block) {
$class = $block->GetClass();
$name = $block->GetName();
$bid = $block->id;
$documented = $block->valid ? "" : "Undocumented";
if ($name)
$elements[$class][] = "<a href='#$class$bid' title='$documented' class='$documented'><span class='objname'>$name</span></a>";
}
// Prepare for output
$elementsout = "";
foreach($elements as $class=>$blocks) {
$elementsout .= "<span class='$class bold'>$class</span>".implode(", ", $blocks)."<br />";
}
$pagecomment = $pagecomment ? "<div class='pagecomment'>$pagecomment</div>" : "";
$id = dechex(crc32($filename));
$fileout .= "
<div class='file'>
<a name='file$id'><div class='filename'>$filename</div>
$pagecomment
<div class='contains'>$elementsout</div>
</div>
";
}
$fileout .= "</div>";
$countfiles = count($files);
$tabfile = "<li><a href='#files'>Files ($countfiles)</a></li>";
}
// 4. Generate list of classes
if ($_POST['Dclas']) {
$classout = "<div id='classes'>";
if (!isset($allblocks['class']) || !is_array($allblocks['class'])) $allblocks['class'] = array();
uksort($allblocks['class'], "strnatcasecmp" );
foreach($allblocks['class'] as $block) {
// Prepare docs
$pagecomment = $block->OutputComment();
// Prepare information
$bid = $block->id;
$name = $block->GetName();
$classname = $block->GetClass();
$fid = dechex(crc32($block->filename));
$filename = $block->filename;
$line = $block->line;
// Prepare for output
$classout .= "
<div class='file'>
<a name='$classname$bid'></a><div class='classname'>$name</div>
From: <a href='#file$fid'>$filename</a> (line $line)<br />
<pre>$pagecomment</pre>
</div>
";
}
$classout .= "</div>";
$countclasses = count($allblocks['class']);
$tabclasses = "<li><a href='#classes'>Classes ($countclasses)</a></li>";
}
// 5. Generate list of functions
if ($_POST['Dfunc']) {
$fnout = "<div id='functions'>";
if (!isset($allblocks['function']) || !is_array($allblocks['function'])) $allblocks['function'] = array();
uksort($allblocks['function'], "strnatcasecmp" );
foreach($allblocks['function'] as $block) {
// Prepare docs
$pagecomment = $block->OutputComment();
// Prepare information
$bid = $block->id;
$name = $block->GetName();
$classname = $block->GetClass();
$fid = dechex(crc32($block->filename));
$filename = $block->filename;
$code = $block->associatedHlCode;
$line = $block->line;
// Prepare for output
$fnout .= "
<div class='file'>
<a name='$classname$bid'></a><div class='fnname'>$name</div>
From: <a href='#file$fid'>$filename</a> (line $line)<br />
Context: $code<br />
<pre>$pagecomment</pre>
</div>
";
}
$fnout .= "</div>";
$countfunctions = count($allblocks['function']);
$tabfunctions = "<li><a href='#functions'>Functions ($countfunctions)</a></li>";
}
// 6. Generate list of interfaces
if ($_POST['Dinte']) {
$intout = "<div id='interfaces'>";
if (!isset($allblocks['interface']) || !is_array($allblocks['interface'])) $allblocks['interface'] = array();
uksort($allblocks['interface'], "strnatcasecmp" );
foreach($allblocks['interface'] as $block) {
// Prepare docs
$pagecomment = $block->OutputComment();
// Prepare information
$bid = $block->id;
$name = $block->GetName();
$classname = $block->GetClass();
$fid = dechex(crc32($block->filename));
$filename = $block->filename;
$line = $block->line;
// Prepare for output
$intout .= "
<div class='file'>
<a name='$classname$bid'></a><div class='intname'>$name</div>
From: <a href='#file$fid'>$filename</a> (line $line)<br />
<pre>$pagecomment</pre>
</div>
";
}
$intout .= "</div>";
$countint = count($allblocks['interface']);
$tabint = "<li><a href='#interfaces'>Interfaces ($countint)</a></li>";
}
// 7. Generate list of constants
if ($_POST['Dcons']) {
$defout = "<div id='define'>";
if (!is_array($allblocks['define'])) $allblocks['define'] = array();
uksort($allblocks['define'], "strnatcasecmp" );
foreach($allblocks['define'] as $block) {
// Prepare docs
$pagecomment = $block->OutputComment();
// Prepare information
$bid = $block->id;
$name = $block->GetName();
$classname = $block->GetClass();
$fid = dechex(crc32($block->filename));
$filename = $block->filename;
$line = $block->line;
$code = $block->associatedHlCode;
// Prepare for output
$defout .= "
<div class='file'>
<a name='$classname$bid'></a><div class='defname'>$name</div>
From: <a href='#$classname$fid'>$filename</a> (line $line)<br />
Context: $code<br />
<pre>$pagecomment</pre>
</div>
";
}
$defout .= "</div>";
$countdef = count($allblocks['define']);
$tabdef = "<li><a href='#define'>Constants ($countdef)</a></li>";
}
// 8. Generate list of constants
if ($_POST['Dincl']) {
$incout = "<div id='include'>";
if (!is_array($allblocks['include'])) $allblocks['include'] = array();
uksort($allblocks['include'], "strnatcasecmp" );
foreach($allblocks['include'] as $block) {
// Prepare docs
$pagecomment = $block->OutputComment();
// Prepare information
$bid = $block->id;
$name = $block->GetName();
$classname = $block->GetClass();
$fid = dechex(crc32($block->filename));
$filename = $block->filename;
$line = $block->line;
$code = $block->associatedHlCode;
// Prepare for output
$incout .= "
<div class='file'>
<a name='$classname$bid'></a><div class='incname'>$name</div>
From: <a href='#$classname$fid'>$filename</a> (line $line)<br />
Context: $code<br />
<pre>$pagecomment</pre>
</div>
";
}
$incout .= "</div>";
$countinc = count($allblocks['include']);
$tabinc = "<li><a href='#include'>Includes ($countinc)</a></li>";
}
// FINAL STEP: Prepare presentation
$date = date("F j, Y, H:i");
$out = ("<!DOCTYPE html>
<html lang=\"fr\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Documentation manual generated by PHPDocFill</title>
<!-- Include jQuery & jQuery UI 鏩 -->
<meta charset='utf-8' />
<link rel='stylesheet' href='phpdocfill-resources/jquery-ui.css' />
<link rel='stylesheet' href='phpdocfill-resources/fonts.css?family=Oxygen:400,700|PT+Sans+Narrow:700' />
<script type='text/javascript' src='phpdocfill-resources/jquery.min.js'></script>
<script type='text/javascript' src='phpdocfill-resources/jquery-ui.js'></script>
<style>
body{ margin:20px;padding:0;font-size: 12px;font-family: Oxygen, Verdana, Tahoma, Arial;color: black;background: white;}
.file{ font-family: Oxygen, Verdana; margin: 15px; }
.filename { font: bold 150% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: 1px solid #1365ad; }
.classname { font: bold 150% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: 1px solid #ad1365; }
.fnname { font: bold 150% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: 1px solid #13ad65; }
.intname { font: bold 150% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: 1px solid #f4ba3e; }
.defname { font: bold 150% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: 1px solid #d375e3; }
.incname { font: bold 150% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: 1px solid #7c90aa; }
.generated { font-size: 10px; margin: 10px; text-align: center; }
.bold { font-weight: bold; }
.class { display: inline-block; width: 65px; color: #1365ad; !important; }
.function { display: inline-block; width: 65px; color: #1365ad !important; }
.interface { display: inline-block; width: 65px; color: #1365ad !important; }
.include { display: inline-block; width: 65px; color: #1365ad !important; }
.define { display: inline-block; width: 65px; color: #1365ad !important; }
.objname { font-weight: normal !important; }
h1 { font: bold 300% 'PT Sans Narrow', sans-serif; color: #666; letter-spacing: 1.4px; border-bottom: solid 2px #1365ad; height: 45px; text-transform: uppercase; margin-top: -10px; margin-bottom: 15px; }
a { font-family: Oxygen, Verdana; text-decoration: none; }
pre { margin: 0; }
.pagecomment { margin: 2px; color:#922e2e; font-family: Courier New; }
.asterisk { color: red; }
.Undocumented { font-style: italic; color: #777 !important; }
</style>
</head>
<body>
<h1>".MY_PROJECT."</h1>
<div id='tabs'>
<ul>
$tabfile
$tabfunctions
$tabclasses
$tabint
$tabdef
$tabinc
</ul>
$fileout
$fnout
$classout
$intout
$defout
$incout
</div>
<div class='generated'>
Generated by <a href='http://cnedelcu.net/phpdocfill/' style='color:black;'>PHPDocFill</a> - $date
</div>
<script>
jQuery.fn.extend({
scrollToMe: function () {
if (jQuery(this).length < 1) return;
var y = jQuery(this).offset().top;
jQuery('html,body').animate({scrollTop: y}, 100);
}});
var elementToScroll = '';
function DoScrollTo() {
$(elementToScroll).scrollToMe();
}
$(function() {
$('#tabs').tabs();
$('a').click(function(e) {
var link = $(this).attr('href');
var index = 0;
if (link.indexOf('#function') > -1)
index = $('#functions').index();
else if (link.indexOf('#class') > -1)
index = $('#classes').index();
else if (link.indexOf('#interface') > -1)
index = $('#interfaces').index();
else if (link.indexOf('#include') > -1)