]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/savepage.php
Patch by Gary Benson <gary@inauspicious.org>:
[SourceForge/phpwiki.git] / lib / savepage.php
1 <?php rcs_id('$Id: savepage.php,v 1.7.2.2 2001-09-21 19:59:13 dairiki Exp $');
2
3 /*
4    All page saving events take place here.
5    All page info is also taken care of here.
6    This is klugey. But it works. There's probably a slicker way of
7    coding it.
8 */
9
10    function UpdateRecentChanges($dbi, $pagename, $isnewpage)
11    {
12       global $remoteuser; // this is set in the config
13       global $dateformat;
14       global $WikiPageStore;
15
16       $recentchanges = RetrievePage($dbi, gettext ("RecentChanges"), $WikiPageStore);
17
18       // this shouldn't be necessary, since PhpWiki loads 
19       // default pages if this is a new baby Wiki
20       if ($recentchanges == -1) {
21          $recentchanges = array(); 
22       }
23
24       $now = time();
25       $today = date($dateformat, $now);
26
27       if (date($dateformat, $recentchanges['lastmodified']) != $today) {
28          $isNewDay = TRUE;
29          $recentchanges['lastmodified'] = $now;
30       } else {
31          $isNewDay = FALSE;
32       }
33
34       $numlines = sizeof($recentchanges['content']);
35       $newpage = array();
36       $k = 0;
37
38       // scroll through the page to the first date and break
39       // dates are marked with "____" at the beginning of the line
40       for ($i = 0; $i < $numlines; $i++) {
41          if (preg_match("/^____/",
42                         $recentchanges['content'][$i])) {
43             break;
44          } else {
45             $newpage[$k++] = $recentchanges['content'][$i];
46          }
47       }
48
49       // if it's a new date, insert it
50       $newpage[$k++] = $isNewDay ? "____$today\r"
51                                  : $recentchanges['content'][$i++];
52
53       // add the updated page's name to the array
54       if($isnewpage) {
55          $newpage[$k++] = "* [$pagename] (new) ..... $remoteuser\r";
56       } else {
57          $diffurl = "phpwiki:?diff=" . rawurlencode($pagename);
58          $newpage[$k++] = "* [$pagename] ([diff|$diffurl]) ..... $remoteuser\r";
59       }
60       if ($isNewDay)
61          $newpage[$k++] = "\r";
62
63       // copy the rest of the page into the new array
64       // and skip previous entry for $pagename
65       $pagename = preg_quote($pagename);
66       for (; $i < $numlines; $i++) {
67          if (!preg_match("|\[$pagename\]|", $recentchanges['content'][$i])) {
68             $newpage[$k++] = $recentchanges['content'][$i];
69          }
70       }
71
72       // copy the new page back into recentchanges, skipping empty days
73       $numlines = sizeof($newpage);
74       $recentchanges['content'] = array();
75       $k = 0;
76       for ($i = 0; $i < $numlines; $i++) {
77          if ($i != $numlines-1 &&
78              preg_match("/^____/", $newpage[$i]) &&
79              preg_match("/^[\r\n]*$/", $newpage[$i+1])) {
80             $i++;
81          } else {
82             $recentchanges['content'][$k++] = $newpage[$i];
83          }
84       }
85
86       InsertPage($dbi, gettext ("RecentChanges"), $recentchanges);
87    }
88
89
90    function ConcurrentUpdates($pagename)
91    {
92       /* xgettext only knows about c/c++ line-continuation strings
93         is does not know about php's dot operator.
94         We want to translate this entire paragraph as one string, of course.
95       */
96       $html = "<P>";
97       $html .= gettext ("PhpWiki is unable to save your changes, because another user edited and saved the page while you were editing the page too. If saving proceeded now changes from the previous author would be lost.");
98       $html .= "</P>\n<P>";
99       $html .= gettext ("In order to recover from this situation follow these steps:");
100       $html .= "\n<OL><LI>";
101       $html .= gettext ("Use your browser's <b>Back</b> button to go back to the edit page.");
102       $html .= "\n<LI>";
103       $html .= gettext ("Copy your changes to the clipboard or to another temporary place (e.g. text editor).");
104       $html .= "\n<LI>";
105       $html .= gettext ("<b>Reload</b> the page. You should now see the most current version of the page. Your changes are no longer there.");
106       $html .= "\n<LI>";
107       $html .= gettext ("Make changes to the file again. Paste your additions from the clipboard (or text editor).");
108       $html .= "\n<LI>";
109       $html .= gettext ("Press <b>Save</b> again.");
110       $html .= "</OL>\n<P>";
111       $html .= gettext ("Sorry for the inconvenience.");
112       $html .= "</P>";
113
114       GeneratePage('MESSAGE', $html,
115         sprintf (gettext ("Problem while updating %s"), $pagename), 0);
116       exit;
117    }
118
119
120
121    $pagename = rawurldecode($post);
122    $pagehash = RetrievePage($dbi, $pagename, $WikiPageStore);
123
124    // if this page doesn't exist yet, now's the time!
125    if (! is_array($pagehash)) {
126       $pagehash = array();
127       $pagehash['version'] = 0;
128       $pagehash['created'] = time();
129       $pagehash['flags'] = 0;
130       $newpage = 1;
131    } else {
132       if (($pagehash['flags'] & FLAG_PAGE_LOCKED) && !defined('WIKI_ADMIN')) {
133          $html = "<p>" . gettext ("This page has been locked by the administrator and cannot be edited.");
134          $html .= "\n<p>" . gettext ("Sorry for the inconvenience.");
135          GeneratePage('MESSAGE', $html, sprintf (gettext ("Problem while editing %s"), $pagename), 0);
136          ExitWiki ("");
137       }
138
139       if(isset($editversion) && ($editversion != $pagehash['version'])) {
140          ConcurrentUpdates($pagename);
141       }
142
143       // archive it if it's a new author
144       if ($pagehash['author'] != $remoteuser) {
145          SaveCopyToArchive($dbi, $pagename, $pagehash);
146       }
147       $newpage = 0;
148    }
149
150    // set new pageinfo
151    $pagehash['lastmodified'] = time();
152    $pagehash['version']++;
153    $pagehash['author'] = $remoteuser;
154
155    // create page header
156    $enc_url = rawurlencode($pagename);
157    $enc_name = htmlspecialchars($pagename);
158    $html = sprintf(gettext("Thank you for editing %s."),
159                    "<a href=\"$ScriptUrl?$enc_url\">$enc_name</a>");
160    $html .= "<br>\n";
161
162    if (! empty($content)) {
163       // patch from Grant Morgan <grant@ryuuguu.com> for magic_quotes_gpc
164       if (get_magic_quotes_gpc())
165          $content = stripslashes($content);
166
167       $pagehash['content'] = preg_split('/[ \t\r]*\n/', chop($content));
168
169       // convert spaces to tabs at user request
170       if (isset($convert)) {
171          $pagehash['content'] = CookSpaces($pagehash['content']);
172       }
173    }
174
175    for ($i = 1; $i <= NUM_LINKS; $i++) {
176         if (! empty(${'r'.$i})) {
177            if (preg_match("#^($AllowedProtocols):#", ${'r'.$i}))
178               $pagehash['refs'][$i] = ${'r'.$i};
179            else
180               $html .= "<P>Link [$i]: <B>unknown protocol</B>" .
181                    " - use one of $AllowedProtocols - link discarded.</P>\n";
182         }
183    }
184
185    InsertPage($dbi, $pagename, $pagehash);
186    UpdateRecentChanges($dbi, $pagename, $newpage);
187
188    $html .= gettext ("Your careful attention to detail is much appreciated.");
189    $html .= "\n";
190
191    // fixme: no test for flat file db system
192    if (isset($DBMdir) && preg_match('@^/tmp\b@', $DBMdir)) {
193       $html .= "<P><B>Warning: the Wiki DB files still live in the " .
194                 "/tmp directory. Please read the INSTALL file and move " .
195                 "the DBM file to a permanent location or risk losing " .
196                 "all the pages!</B>\n";
197    }
198
199    if (!empty($SignatureImg))
200       $html .= "<P><img src=\"$SignatureImg\"></P>\n";
201
202    $html .= "<hr noshade><P>";
203    include('lib/transform.php');
204
205    GeneratePage('BROWSE', $html, $pagename, $pagehash);
206 ?>