File: Barrel.php

package info (click to toggle)
diogenes 0.9.20-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,976 kB
  • ctags: 3,271
  • sloc: php: 12,285; sh: 828; perl: 258; makefile: 137; sql: 85
file content (456 lines) | stat: -rw-r--r-- 12,205 bytes parent folder | download | duplicates (2)
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
<?php
/*
 * Copyright (C) 2003-2004 Polytechnique.org
 * http://opensource.polytechnique.org/
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

require_once 'Barrel/Page.php';
require_once 'Barrel/Options.php';
require_once 'diogenes/diogenes.flagset.inc.php';
 

/** This class describes a Diogenes Barrel.
 */
class Diogenes_Barrel
{
  /** The barrel's alias. */
  var $alias;

  /** The database table holding the menus */
  var $table_menu;

  /** The database table holding the pages */
  var $table_page;
    
  /** The site's flags. */
  var $flags;
  
  /** The site's options. */
  var $options;
  
  /** Cache of the homepage ID */
  var $homepage;

  /** Handle to the spool */
  var $spool;
  
  /** If the barrel is running on a virtualhost. */
  var $vhost;

  /** Cache of tree */
  var $treeCache;
  
  /** File containing the tree cache. */
  var $treeCacheFile;

  /** Cache of the plugins */
  var $pluginsCache;
    
  /** File containing the plugin cache. */
  var $pluginsCacheFile;
   
  
  /** Construct a Diogenes Barrel.
   *
   * @param $alias
   */ 
  function Diogenes_Barrel($alias)
  {
    global $globals;
    $webdav = '';
    
    // Retrieve site-wide info from database
    $res = $globals->db->query("select alias,vhost,flags from diogenes_site where alias='$alias'");
    if (!list($this->alias,$this->vhost,$flags) = mysql_fetch_row($res)) {
      return;      
    }
    mysql_free_result($res);
    
    $this->table_menu = "{$this->alias}_menu";
    $this->table_page = "{$this->alias}_page";
    $this->treeCacheFile = $globals->spoolroot."/diogenes_c/". $this->alias.".tree";
    $this->pluginsCacheFile = $globals->plugins->cacheFile($this->alias);

    $this->flags = new flagset($flags);    
    $this->options = new Diogenes_Barrel_Options($this->alias);
    $this->spool = new DiogenesSpool($this,$this->alias);
    
    $this->readTree();
  }
  
  
  /** Create a new Diogenes barrel. This creates the database, RCS and
   *  spool entries for the new barrel.
   *
   * @param $alias
   * @param $caller
   */
  function create($alias, &$caller)
  {
    global $globals;

    /* sanity check */
    if (!preg_match("/^[a-zA-Z0-9_]+$/",$alias) or
        in_array($alias, $globals->invalidaliases)) 
    {
      $caller->info("Invalid barrel name!");
      return;
    }

    $res = $globals->db->query("select alias from diogenes_site where alias='$alias'");
    if (mysql_num_rows($res) > 0) {
      $caller->info("Entry '{$alias}' already exists in table 'diogenes_site'!");
      return;
    }

    if (file_exists("{$globals->rcsroot}/$alias")) {
      $caller->info("Directory '{$globals->rcsroot}/$alias' already exists!");
      return;
    }

    if (!is_dir($globals->rcsroot) || !is_writable($globals->rcsroot)) {
      $caller->info("Directory '{$globals->rcsroot}' is not writable!");
      return;
    }

    /* log this event */
    $caller->log("barrel_create","$alias:*");

    /* create DB entry */
    $globals->db->query("insert into diogenes_site set alias='$alias'");

    $globals->db->query("CREATE TABLE {$alias}_menu ("
    . "MID int(10) unsigned NOT NULL auto_increment,"
    . "MIDpere int(10) unsigned NOT NULL,"
    . "ordre int(10) unsigned NOT NULL,"
    . "title tinytext NOT NULL,"
    . "link text NOT NULL,"
    . "PID int(10) unsigned NOT NULL,"
    . "PRIMARY KEY  (MID)"
    . ") TYPE=MyISAM;");

    $globals->db->query("CREATE TABLE {$alias}_page ("
    . "PID int(10) unsigned NOT NULL auto_increment,"
    . "parent INT( 10 ) UNSIGNED NOT NULL default '0',"
    . "location tinytext NOT NULL,"
    . "title tinytext NOT NULL,"
    . "status tinyint(1) unsigned NOT NULL default '0',"
    . "perms enum('public','auth','user','admin','forbidden') NOT NULL default 'public',"
    . "wperms enum('public','auth','user','admin','forbidden') NOT NULL default 'admin',"
    . "template varchar(255) NOT NULL,"
    . "PRIMARY KEY  (PID)"
    . ") TYPE=MyISAM;");
    
    /* set the barrel's title */
    $opt = new Diogenes_Barrel_Options($alias);
    $opt->updateOption('title',$alias);

    /* create entry for the homepage */
    $globals->db->query("insert into {$alias}_page set location='temp'");
    $homepage = mysql_insert_id();
    $globals->db->query("update {$alias}_page set location='',title='Home',perms='public' where PID='$homepage'");

    /* create home page */
    $rcs = new $globals->rcs($caller,$alias,$_SESSION['session']->username,true);
    $rcs->newdir("",$homepage);
    $rcs->commit($homepage,$globals->htmlfile,"");

    /* copy CSS template */
    $def_css = file_get_contents("{$globals->root}/styles/{$globals->barrel_style_sheet}.css");
    $rcs->commit($homepage,$globals->cssfile, $def_css);
  }

  
  /** Destroy a Diogenes barrel. This removes the related database, RCS
   *  and spool entries.
   *
   * @param $alias
   * @param $caller
   */
  function destroy($alias, &$caller) {
    global $globals;

    /** Sanity check */
    if (!$alias) {
      $caller->info("Empty alias supplied!");
      return;
    }

    /* log this event */
    $caller->log("barrel_delete","$alias:*");

    system(escapeshellcmd("rm -rf ".escapeshellarg("{$globals->spoolroot}/$alias")));
    system(escapeshellcmd("rm -rf ".escapeshellarg("{$globals->rcsroot}/$alias")));
    system(escapeshellcmd("rm -f ".escapeshellarg("{$globals->spoolroot}/diogenes_c/$alias.tree")));
    system(escapeshellcmd("rm -f ".escapeshellarg($globals->plugins->cacheFile($alias))));
    $globals->db->query("drop table {$alias}_menu");
    $globals->db->query("drop table {$alias}_page");
    $globals->db->query("delete from diogenes_perm where alias='$alias'");
    $globals->db->query("delete from diogenes_site where alias='$alias'");
    $globals->db->query("delete from diogenes_option where barrel='$alias'");    
    $globals->db->query("delete from diogenes_plugin where barrel='$alias'");
  }
  
    
  /** Return the location corresponding to a given page ID
   *
   * @param $PID
   */  
  function getLocation($PID)
  {
    return array_search($PID, $this->treeCache);
  }
    
  
  
  /** Return all the barrel's pages.
   */
  function getPages()
  {
    global $globals;
    $bpages = array();
    
    $res = $globals->db->query("select * from {$this->table_page}");
    while ($props = mysql_fetch_assoc($res)) {
      $bpages[$props['PID']] = new Diogenes_Barrel_Page($this, $props);
    }
    mysql_free_result($res);
    return $bpages;  
  }
  
      
  /** Return the page ID matching a given directory location
   *
   * @param $dir
   */
  function getPID($dir)
  {
    if (isset($this->treeCache[$dir]))
      return $this->treeCache[$dir];
    else
      return;
  }
  
  
  /** List the plugins that are active in a given context
   *
   * @param $page   
   */
  function getPlugins($page = 0)
  {
    $plugins = array();
    foreach ($this->pluginsCache as $plug)
    {
      if ($plug['page'] == $page) 
      {
        array_push($plugins, $plug);
      }
    }
    return $plugins;    
  }

  
  /** Check whether the barrel has a given flag
   *
   * @param $flag
   */
  function hasFlag($flag)
  {
    return $this->flags->hasFlag($flag);
  }


  /** Create a page with the given path, and return its PID.
   *
   * @param $path
   * @param $caller
   */
  function makePath($path, &$caller)
  {
    $pathbits = split("/", $path);
    $curpath = '';
    $curpid = $this->getPID($curpath);;
    foreach ($pathbits as $pathbit)
    {
      $newpath = ($curpath ? "$curpath/" : "") . $pathbit;
      $newpid = $this->getPID($newpath);
      if (!$newpid)
      {
        $tpage = new Diogenes_Barrel_Page($this, array('parent' => $curpid, 'location' => $pathbit));
        $tpage->toDb(0, $caller);
        $newpid = $this->getPID($newpath);
      }
      $curpath = $newpath;
      $curpid = $newpid;
    }
    return $curpid;
  }
  

  /** Recursively delete a given path.
   *
   * @param $path
   * @param $caller
   */
  function rmPath($path, &$caller)
  {
    global $globals;

    if (!$path) {
      $caller->info("rmPath: will not delete from root!");
      return false;
    }
    $curpid = $this->getPID($path);
    if (!$curpid) {
      $caller->info("rmPath: could not find '$path'");
      return false;
    }

    // lookup children
    $res = $globals->db->query("select PID from {$this->table_page} where parent=$curpid");
    $children = array();
    while (list($child) = mysql_fetch_row($res))
      array_push($children, $child);
    mysql_free_result($res);

    // recurse into children
    foreach ($children as $child)
    {
      $childpath = $this->getLocation($child);
      if ($childpath) {
        if (!$this->rmPath($childpath, $caller)) 
          return false;
      }
    }
    
    // delete this page
    return Diogenes_Barrel_Page::delete($this, $curpid, $caller);
  }

  /** Compile the directory tree
   */
  function compileTree()
  {
    global $globals;
    
    if (!$fp = fopen($this->treeCacheFile, "w")) {
      trigger_error("failed to open '{$this->treeCacheFile}' for writing", E_USER_ERROR);
      return;
    }
    
    // load all the pages
    $res = $globals->db->query("select * from {$this->table_page}");
    $tpages = array();
    while ($props = mysql_fetch_assoc($res))
    {      
      $tpage = new Diogenes_Barrel_Page($this, $props);
      $tpages[$props['PID']] = $tpage;
      if (!strlen($props['location']))
      {
        $homepage = $props['PID'];
      }
    }      
        
    // recursively build the tree, starting at the homepage 
    $str = $this->compilePageTree($tpages, $homepage, '');
    fputs($fp, $str);
    fclose($fp);    
  }
  
  
  /** Compile a single page
   */
  function compilePageTree(&$tpages, $PID, $ploc)
  {
    global $globals;
    
    $cpage = $tpages[$PID];
    $ploc = ($ploc ? "$ploc/" : "") . $cpage->props['location'];
          
    // add this page
    $out = "$ploc\t$PID\t".$cpage->props['parent']."\n";      
    
    // add children
    $res = $globals->db->query("select PID from {$this->table_page} where parent='$PID'");    
    while (list($child) = mysql_fetch_row($res))
    {           
      $out .= $this->compilePageTree($tpages, $child, $ploc);
    }      
    mysql_free_result($res);
    return $out;
  }

  
  /** Load all plugins for the specified page.
   *
   * @param $bpage
   */
  function loadPlugins(&$bpage)
  {
    global $globals;
  
    $plugins = $this->getPlugins($bpage->props['PID']);
    
    $loaded = array();
    foreach ($plugins as $plugentry) 
    {
      $loaded[$plugentry['plugin']] =& $globals->plugins->load($plugentry);
    }
    return $loaded;
  }
  
  
  /** Read the compiled plugin cache
   */
  function readPlugins()
  {
    global $globals;

    $this->pluginsCache = $globals->plugins->readCache($this->pluginsCacheFile, $this->alias);
  }
  
    
  /** Read the compiled directory tree
   */
  function readTree()
  {
    global $globals;
    
    // if the tree cache does not exits, try to init it
    if (!file_exists($this->treeCacheFile)) {
      $this->compileTree();
    }
    
    if (!$fp = fopen($this->treeCacheFile, "r")) {
      trigger_error("failed to open '{$this->treeCacheFile}' for reading", E_USER_ERROR);
      return;
    }
    
    $locations = array();
    while ($line = fgets($fp))
    {
      $line = substr($line, 0, -1);
      $bits = explode("\t", $line);
      list($loc, $pid, $parent) = $bits;       
      $locations[$loc] = $pid;
    }
    fclose($fp);

    $this->treeCache = $locations;    
  }
   
}