]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/savepage.php
2004-12-19 01:02 rurban
[SourceForge/phpwiki.git] / lib / savepage.php
1 <?php rcs_id('$Id: savepage.php,v 1.7.2.3.2.3 2005-01-07 14:02:28 rurban 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    if (get_magic_quotes_gpc()) {
121       $post = stripslashes($post);
122    }
123    $pagename = rawurldecode($post);
124    $pagehash = RetrievePage($dbi, $pagename, $WikiPageStore);
125
126    // if this page doesn't exist yet, now's the time!
127    if (! is_array($pagehash)) {
128       $pagehash = array();
129       $pagehash['version'] = 0;
130       $pagehash['created'] = time();
131       $pagehash['flags'] = 0;
132       $newpage = 1;
133    } else {
134       if (($pagehash['flags'] & FLAG_PAGE_LOCKED) && !defined('WIKI_ADMIN')) {
135          $html = "<p>" . gettext ("This page has been locked by the administrator and cannot be edited.");
136          $html .= "\n<p>" . gettext ("Sorry for the inconvenience.");
137          GeneratePage('MESSAGE', $html, sprintf (gettext ("Problem while editing %s"), $pagename), 0);
138          ExitWiki ("");
139       }
140
141       if(isset($editversion) && ($editversion != $pagehash['version'])) {
142          ConcurrentUpdates($pagename);
143       }
144
145       // archive it if it's a new author
146       if ($pagehash['author'] != $remoteuser) {
147          SaveCopyToArchive($dbi, $pagename, $pagehash);
148       }
149       $newpage = 0;
150    }
151
152    // set new pageinfo
153    $pagehash['lastmodified'] = time();
154    $pagehash['version']++;
155    $pagehash['author'] = $remoteuser;
156
157    // create page header
158    $enc_url = rawurlencode($pagename);
159    $enc_name = htmlspecialchars($pagename);
160    $html = sprintf(gettext("Thank you for editing %s."),
161                    "<a href=\"$ScriptUrl?$enc_url\">$enc_name</a>");
162    $html .= "<br>\n";
163
164    if (! empty($content)) {
165       // patch from Grant Morgan <grant@ryuuguu.com> for magic_quotes_gpc
166       if (get_magic_quotes_gpc())
167          $content = stripslashes($content);
168
169       $pagehash['content'] = preg_split('/[ \t\r]*\n/', chop($content));
170
171       // convert spaces to tabs at user request
172       if (isset($convert)) {
173          $pagehash['content'] = CookSpaces($pagehash['content']);
174       }
175    }
176
177    for ($i = 1; $i <= NUM_LINKS; $i++) {
178         if (! empty(${'r'.$i})) {
179            if (preg_match("#^($AllowedProtocols):#", ${'r'.$i}))
180               $pagehash['refs'][$i] = ${'r'.$i};
181            else
182               $html .= "<P>Link [$i]: <B>unknown protocol</B>" .
183                    " - use one of $AllowedProtocols - link discarded.</P>\n";
184         }
185    }
186
187    InsertPage($dbi, $pagename, $pagehash);
188    UpdateRecentChanges($dbi, $pagename, $newpage);
189
190    $html .= gettext ("Your careful attention to detail is much appreciated.");
191    $html .= "\n";
192
193    // fixme: no test for flat file db system
194    if (isset($DBMdir) && preg_match('@^/tmp\b@', $DBMdir)) {
195       $html .= "<P><B>Warning: the Wiki DB files still live in the " .
196                 "/tmp directory. Please read the INSTALL file and move " .
197                 "the DB files to a permanent location or risk losing " .
198                 "all the pages!</B>\n";
199    }
200
201    if (!empty($SignatureImg))
202       $html .= "<P><img src=\"$SignatureImg\"></P>\n";
203
204    $html .= "<hr noshade><P>";
205    include('lib/transform.php');
206
207    GeneratePage('BROWSE', $html, $pagename, $pagehash);
208 ?>