27d26b509030f64d9d74b76b57cb0d227c5b9d59
[atutor.git] / mods / wiki / plugins / action / diff_gnu_safemode.php
1 <?php
2
3  # This plugin is a small modification of the original diff_gnu.php plugin.
4  # It allows use with safemode enabled. 
5  # However it requires the directory where diff is located to be in the php ini
6  # This way you can still control what programs are executed by PHP and the programs 
7  # can have root as owner.
8  # Of course its not smart to put the normal bin in your php settings, cause that
9  # would effectively disable safemode, just copying the diff utility works best.
10  
11  # Example phpsettings entry:
12  #   safe_mode_exec_dir = /usr/local/bin/ewiki
13  
14  # With safemode disabled it should work without any special settings.
15  
16  # Original plugin comment:
17  
18  # This diff plugin utilizes the external GNU diff program to show up
19  # differences between two versions of a WikiPage.
20  # The diff utility is commonly not part of the graphical OS simulators
21  # from Redmond, but you could install the Cygwin environment to make
22  # it available.
23
24
25
26  $ewiki_plugins["action"]["diff"] = "ewiki_page_gnu_diff_safemode";
27  $ewiki_config["action_links"]["info"]["diff"] = "diff";
28
29
30
31  function ewiki_page_gnu_diff_safemode($id, &$data, $action) {
32
33     #-- different operation modes of GNU diff:
34    $OPTIONS = " -B -u -U 50 ";
35 #   $OPTIONS = " -B ";
36 #   $OPTIONS = " -c ";
37 #   $OPTIONS = " --side-by-side ";
38
39     #-- fetch old wiki source
40     if (($old_ver = ($new_ver = $data["version"]) - 1) > 0)
41     $data0 = ewiki_db::GET($id, $old_ver);
42
43     $o = ewiki_make_title($id, "Differences between version $new_ver and $old_ver of »{$id}«");
44
45     #-- create temporary files from wikipages
46     $file0 = tempnam(EWIKI_TMP, "ewiki.diff.gnu.");
47     $f = fopen($file0, "w");
48     fwrite($f, $data0["content"]);
49     fclose($f);
50     $file1 = tempnam(EWIKI_TMP, "ewiki.diff.gnu.");
51     $f = fopen($file1, "w");
52     fwrite($f, $data["content"]);
53     fclose($f);
54
55     #-- parse thru GNU diff util
56     $fn = addslashes($id);
57     $OPTIONS .= " --label='$fn (version $old_ver)' --label='$fn (version $new_ver)' ";
58     //following lines replace the original 
59     //$diff = shell_exec("diff $OPTIONS $file0 $file1");
60     $dfd = popen("diff $OPTIONS $file0 $file1", "r");
61     $diff = "";
62     while (!feof($dfd)) {
63         $diff .= fread($dfd, 5000);
64     }
65     pclose($dfd);
66     //end of moderation
67
68     #-- remove temporary files
69     unlink($file0);
70     unlink($file1);
71
72     #-- encolor diff output
73     foreach (explode("\n", $diff) as $dl) {
74
75        $str = substr($dl, 1);
76
77        switch (substr($dl, 0, 1)) {
78           case "<":
79           case "-":
80              $o .= "<b>-</b><font color=\"#990000\"> <tt>$str</tt></font><br />\n";
81              break;
82           case ">":
83           case "+":
84              $o .= "<b>+</b><font color=\"#009900\"> <tt>$str</tt></font><br />\n";
85              break;
86           case "*":
87           case "-":
88              break;
89           default:
90              $o .= "<small><tt>$dl</tt></small><br />";
91        }
92
93     }
94
95     return($o);
96  }
97
98
99 ?>