]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/savepage.php
log
[SourceForge/phpwiki.git] / lib / savepage.php
1 <?php rcs_id('$Id: savepage.php,v 1.11 2001-02-12 01:43:10 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 $user;
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       $userid = $user->id();
54       
55       // add the updated page's name to the array
56       if($isnewpage) {
57          $newpage[$k++] = "* [$pagename] (new) ..... $userid\r";
58       } else {
59          $diffurl = "phpwiki:" . rawurlencode($pagename) . "?action=diff";
60          $newpage[$k++] = "* [$pagename] ([diff|$diffurl]) ..... $userid\r";
61       }
62       if ($isNewDay)
63          $newpage[$k++] = "\r";
64
65       // copy the rest of the page into the new array
66       // and skip previous entry for $pagename
67       $pagename = preg_quote($pagename);
68       for (; $i < $numlines; $i++) {
69          if (!preg_match("|\[$pagename\]|", $recentchanges['content'][$i])) {
70             $newpage[$k++] = $recentchanges['content'][$i];
71          }
72       }
73
74       $recentchanges['content'] = $newpage;
75
76       InsertPage($dbi, gettext ("RecentChanges"), $recentchanges);
77    }
78
79
80    function ConcurrentUpdates($pagename)
81    {
82       /* xgettext only knows about c/c++ line-continuation strings
83         is does not know about php's dot operator.
84         We want to translate this entire paragraph as one string, of course.
85       */
86       $html = "<P>";
87       $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.");
88       $html .= "</P>\n<P>";
89       $html .= gettext ("In order to recover from this situation follow these steps:");
90       $html .= "\n<OL><LI>";
91       $html .= gettext ("Use your browser's <b>Back</b> button to go back to the edit page.");
92       $html .= "\n<LI>";
93       $html .= gettext ("Copy your changes to the clipboard or to another temporary place (e.g. text editor).");
94       $html .= "\n<LI>";
95       $html .= gettext ("<b>Reload</b> the page. You should now see the most current version of the page. Your changes are no longer there.");
96       $html .= "\n<LI>";
97       $html .= gettext ("Make changes to the file again. Paste your additions from the clipboard (or text editor).");
98       $html .= "\n<LI>";
99       $html .= gettext ("Press <b>Save</b> again.");
100       $html .= "</OL>\n<P>";
101       $html .= gettext ("Sorry for the inconvenience.");
102       $html .= "</P>";
103
104       echo GeneratePage('MESSAGE', $html,
105                         sprintf (gettext ("Problem while updating %s"), $pagename), 0);
106       exit;
107    }
108
109
110    $pagehash = RetrievePage($dbi, $pagename, $WikiPageStore);
111
112    // if this page doesn't exist yet, now's the time!
113    if (! is_array($pagehash)) {
114       $pagehash = array();
115       $pagehash['version'] = 0;
116       $pagehash['created'] = time();
117       $pagehash['flags'] = 0;
118       $newpage = 1;
119    } else {
120       if (($pagehash['flags'] & FLAG_PAGE_LOCKED) && ! $user->is_admin()) {
121          $html = "<p>" . gettext ("This page has been locked by the administrator and cannot be edited.");
122          $html .= "\n<p>" . gettext ("Sorry for the inconvenience.");
123          echo GeneratePage('MESSAGE', $html,
124                            sprintf (gettext ("Problem while editing %s"), $pagename), 0);
125          ExitWiki ("");
126       }
127
128       if(isset($editversion) && ($editversion != $pagehash['version'])) {
129          ConcurrentUpdates($pagename);
130       }
131
132       if ($user->id() != $pagehash['author'] && ! $user->is_admin())
133          unset($minor_edit);      // Force archive
134       
135       if (empty($minor_edit))
136          SaveCopyToArchive($dbi, $pagename, $pagehash);
137
138       $newpage = 0;
139    }
140
141    // set new pageinfo
142    $pagehash['lastmodified'] = time();
143    $pagehash['version']++;
144    $pagehash['author'] = $user->id();
145
146    // create page header
147    $html = sprintf(gettext("Thank you for editing %s."),
148                    WikiURL($pagename));
149    $html .= "<br>\n";
150
151    if (! empty($content)) {
152       // patch from Grant Morgan <grant@ryuuguu.com> for magic_quotes_gpc
153       fix_magic_quotes_gpc($content);
154
155       $pagehash['content'] = preg_split('/[ \t\r]*\n/', chop($content));
156
157       // convert spaces to tabs at user request
158       if (isset($convert)) {
159          $pagehash['content'] = CookSpaces($pagehash['content']);
160       }
161    }
162
163    InsertPage($dbi, $pagename, $pagehash);
164    UpdateRecentChanges($dbi, $pagename, $newpage);
165
166    $html .= gettext ("Your careful attention to detail is much appreciated.");
167    $html .= "\n";
168
169    // fixme: no test for flat file db system
170    if (!empty($DBWarning)) {
171       $html .= "<P><B>Warning: $DBWarning" .
172                 "Please read the INSTALL file and move " .
173                 "the DB file to a permanent location or risk losing " .
174                 "all the pages!</B>\n";
175    }
176
177    if (!empty($SignatureImg))
178       $html .= sprintf("<P><img src=\"%s\"></P>\n", DataURL($SignatureImg));
179       
180    $html .= "<hr noshade><P>";
181    include('lib/transform.php');
182
183    echo GeneratePage('BROWSE', $html, $pagename, $pagehash);
184 ?>