~automne-team/automne/4.2

« back to all changes in this revision

Viewing changes to automne/classes/tree/website.php

  • Committer: sebastien
  • Date: 2008-11-26 17:12:36 UTC
  • Revision ID: sebastien_sebastien-20081126171236-16r3kxfuz2kmq2qe
Tags: V4_0_0a0
4.0.0a0 :
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
 
3
// +----------------------------------------------------------------------+
 
4
// | Automne (TM)                                                                                                                 |
 
5
// +----------------------------------------------------------------------+
 
6
// | Copyright (c) 2000-2009 WS Interactive                                                               |
 
7
// +----------------------------------------------------------------------+
 
8
// | Automne is subject to version 2.0 or above of the GPL license.               |
 
9
// | The license text is bundled with this package in the file                    |
 
10
// | LICENSE-GPL, and is available through the world-wide-web at                  |
 
11
// | http://www.gnu.org/copyleft/gpl.html.                                                                |
 
12
// +----------------------------------------------------------------------+
 
13
// | Author: Antoine Pouch <antoine.pouch@ws-interactive.fr> &            |
 
14
// | Author: S�bastien Pauchet <sebastien.pauchet@ws-interactive.fr>      |
 
15
// +----------------------------------------------------------------------+
 
16
//
 
17
// $Id: website.php,v 1.1.1.1 2008/11/26 17:12:06 sebastien Exp $
 
18
 
 
19
/**
 
20
  * Class CMS_website
 
21
  *
 
22
  * represent a website placed on a page in the tree structure. A websites defines mainly a directory
 
23
  * where the pages files will be placed. 
 
24
  * Beware ! Because of the label-to-directory relationship, label should'nt be changeable after the website creation.
 
25
  * This condition is enforced here.
 
26
  *
 
27
  * @package CMS
 
28
  * @subpackage tree
 
29
  * @author Antoine Pouch <antoine.pouch@ws-interactive.fr>
 
30
  */
 
31
 
 
32
class CMS_website extends CMS_grandFather
 
33
{
 
34
        /**
 
35
          * DB id
 
36
          * @var integer
 
37
          * @access private
 
38
          */
 
39
        protected $_id;
 
40
        
 
41
        /**
 
42
          * Label of the website
 
43
          * @var string
 
44
          * @access private
 
45
          */
 
46
        protected $_label;
 
47
 
 
48
        /**
 
49
          * URL of the website (does NOT start with http://)
 
50
          * @var string
 
51
          * @access private
 
52
          */
 
53
        protected $_url;
 
54
 
 
55
        /**
 
56
          * Root page.
 
57
          * @var CMS_page
 
58
          * @access private
 
59
          */
 
60
        protected $_root;
 
61
 
 
62
        /**
 
63
          * Is this website the main website ?
 
64
          * @var boolean
 
65
          * @access private
 
66
          */
 
67
        protected $_isMain = false;
 
68
 
 
69
        /**
 
70
          * Website order
 
71
          * @var integer
 
72
          * @access private
 
73
          */
 
74
        protected $_order;
 
75
 
 
76
        /**
 
77
          * Default Meta values for website
 
78
          * @var boolean
 
79
          * @access private
 
80
          */
 
81
        protected $_meta = array(
 
82
                'keywords' => '',
 
83
                'description' => '',
 
84
                'category' => '',
 
85
                'author' => '',
 
86
                'replyto' => '',
 
87
                'copyright' => '',
 
88
                'language' => '',
 
89
                'robots' => '',
 
90
                'favicon' => '',
 
91
        );
 
92
        
 
93
        /**
 
94
          * Constructor.
 
95
          * initializes the website if the id is given.
 
96
          *
 
97
          * @param integer $id DB id
 
98
          * @return void
 
99
          * @access public
 
100
          */
 
101
        function __construct($id = 0)
 
102
        {
 
103
                static $applicationWebroot;
 
104
                if ($id) {
 
105
                        if (($id == 1 && !is_object($applicationWebroot)) || $id != 1) {
 
106
                                if (!SensitiveIO::isPositiveInteger($id)) {
 
107
                                        $this->raiseError("Id is not a positive integer");
 
108
                                        return;
 
109
                                }
 
110
                                $sql = "
 
111
                                        select
 
112
                                                *
 
113
                                        from
 
114
                                                websites
 
115
                                        where
 
116
                                                id_web='$id'
 
117
                                ";
 
118
                                $q = new CMS_query($sql);
 
119
                                if ($q->getNumRows()) {
 
120
                                        $data = $q->getArray();
 
121
                                        $this->_id = $id;
 
122
                                        $this->_label = $data["label_web"];
 
123
                                        $this->_url = $data["url_web"];
 
124
                                        $this->_root = new CMS_page($data["root_web"]);
 
125
                                        $this->_order = $data["order_web"];
 
126
                                        //the main website has The main page (ID 1) as root
 
127
                                        if ($data["root_web"] == APPLICATION_ROOT_PAGE_ID) {
 
128
                                                $this->_isMain = true;
 
129
                                        }
 
130
                                        $this->_meta['keywords'] = $data["keywords_web"];
 
131
                                        $this->_meta['description'] = $data["description_web"];
 
132
                                        $this->_meta['category'] = $data["category_web"];
 
133
                                        $this->_meta['author'] = $data["author_web"];
 
134
                                        $this->_meta['replyto'] = $data["replyto_web"];
 
135
                                        $this->_meta['copyright'] = $data["copyright_web"];
 
136
                                        $this->_meta['language'] = $data["language_web"];
 
137
                                        $this->_meta['robots'] = $data["robots_web"];
 
138
                                        $this->_meta['favicon'] = $data["favicon_web"];
 
139
                                } else {
 
140
                                        $this->raiseError("Unknown ID :".$id);
 
141
                                }
 
142
                                if ($id == 1) {
 
143
                                        $applicationWebroot = $this;
 
144
                                }
 
145
                        } else {
 
146
                                $this->_id = $id;
 
147
                                $this->_label = $applicationWebroot->_label;
 
148
                                $this->_url = $applicationWebroot->_url;
 
149
                                $this->_root = $applicationWebroot->_root;
 
150
                                $this->_order = $applicationWebroot->_order;
 
151
                                $this->_isMain = $applicationWebroot->_isMain;
 
152
                                $this->_meta = $applicationWebroot->_meta;
 
153
                        }
 
154
                }
 
155
        }
 
156
        
 
157
        /**
 
158
          * Gets the DB ID of the instance.
 
159
          *
 
160
          * @return integer the DB id
 
161
          * @access public
 
162
          */
 
163
        function getID()
 
164
        {
 
165
                return $this->_id;
 
166
        }
 
167
        
 
168
        /**
 
169
          * Get a website meta value
 
170
          *
 
171
          * @param string $meta The meta name to get
 
172
          * @return string the website meta value
 
173
          * @access public
 
174
          */
 
175
        function getMeta($meta) {
 
176
                if (!isset($this->_meta[$meta])) {
 
177
                        $this->raiseError("Unknown meta to get : ".$meta);
 
178
                        return false;
 
179
                }
 
180
                return $this->_meta[$meta];
 
181
        }
 
182
        
 
183
        /**
 
184
          * Set a website meta value
 
185
          *
 
186
          * @param string $meta The meta name to set
 
187
          * @param string $value The meta value to get
 
188
          * @return boolean true on success, false on failure
 
189
          * @access public
 
190
          */
 
191
        function setMeta($meta, $value) {
 
192
                if (!isset($this->_meta[$meta])) {
 
193
                        $this->raiseError("Unknown meta to set : ".$meta);
 
194
                        return false;
 
195
                }
 
196
                $this->_meta[$meta] = $value;
 
197
                return true;
 
198
        }
 
199
        
 
200
        /**
 
201
          * Is this the main website ?
 
202
          *
 
203
          * @return boolean
 
204
          * @access public
 
205
          */
 
206
        function isMain()
 
207
        {
 
208
                return $this->_isMain;
 
209
        }
 
210
        
 
211
        /**
 
212
          * Gets the label
 
213
          *
 
214
          * @return string The label
 
215
          * @access public
 
216
          */
 
217
        function getLabel()
 
218
        {
 
219
                return $this->_label;
 
220
        }
 
221
        
 
222
        /**
 
223
          * Sets the label.
 
224
          *
 
225
          * @param string $label The label to set
 
226
          * @return boolean true on success, false on failure.
 
227
          * @access public
 
228
          */
 
229
        function setLabel($label)
 
230
        {
 
231
                //label should'nt be changed once set
 
232
                if ($this->_id) {
 
233
                        $this->raiseError("Trying to change the label of a website already existing");
 
234
                        return false;
 
235
                }
 
236
                if ($label) {
 
237
                        $old_label = $this->_label;
 
238
                        $this->_label = $label;
 
239
                        
 
240
                        //now test to see if a directory already exists with that name (Because label must _not_ be moveable once set)
 
241
                        if (!$this->_isMain && is_dir($this->getPagesPath(PATH_RELATIVETO_FILESYSTEM))) {
 
242
                                $this->_label = $old_label;
 
243
                                $this->raiseError("Label to set has same directory for pages than a previously set one.");
 
244
                                return false;
 
245
                        } else {
 
246
                                return true;
 
247
                        }
 
248
                } else {
 
249
                        $this->raiseError("Label can't be empty");
 
250
                        return false;
 
251
                }
 
252
        }
 
253
        
 
254
        /**
 
255
          * Gets the url (including http://).
 
256
          *
 
257
          * @return string the URL
 
258
          * @access public
 
259
          */
 
260
        function getURL($includeHTTP = true)
 
261
        {
 
262
                if ($includeHTTP) {
 
263
                        return (substr($this->_url,0,4) != 'http') ? "http://".$this->_url : $this->_url;
 
264
                } else {
 
265
                        return (substr($this->_url,0,4) != 'http') ? $this->_url : substr($this->_url,7);
 
266
                }
 
267
        }
 
268
        
 
269
        /**
 
270
          * Sets the url. Can be empty. Will be riden of http://.
 
271
          *
 
272
          * @param string $url The url to set
 
273
          * @return boolean true on success, false on failure.
 
274
          * @access public
 
275
          */
 
276
        function setURL($url)
 
277
        {
 
278
                if (substr($url, 0, 7) == "http://") {
 
279
                        $url = substr($url, 7);
 
280
                }
 
281
                if ($url) {
 
282
                        if (substr($url, strlen($url) - 1) == "/") {
 
283
                                $url = substr($url, 0, -1);
 
284
                        }
 
285
                        $this->_url = $url;
 
286
                        return true;
 
287
                } else {
 
288
                        return false;
 
289
                }
 
290
        }
 
291
        
 
292
        /**
 
293
          * Gets the root page.
 
294
          *
 
295
          * @return CMS_page The Root page
 
296
          * @access public
 
297
          */
 
298
        function getRoot()
 
299
        {
 
300
                return $this->_root;
 
301
        }
 
302
        
 
303
        /**
 
304
          * Sets the root page.
 
305
          *
 
306
          * @param CMS_page $page The new root page to set.
 
307
          * @return boolean true on success, false on failure
 
308
          * @access public
 
309
          */
 
310
        function setRoot($page)
 
311
        {
 
312
                if (is_a($page, "CMS_page")) {
 
313
                        $ws = CMS_tree::getPageWebsite($page);
 
314
                        if ($ws->getRoot() == $page && $ws->getID() != $this->_id) {
 
315
                                $this->raiseError("Root page to set is already a root page for the website : ".$ws->getLabel());
 
316
                                return false;
 
317
                        } else {
 
318
                                $this->_root = $page;
 
319
                                return true;
 
320
                        }
 
321
                } else {
 
322
                        $this->raiseError("Root page to set is not a page");
 
323
                        return false;
 
324
                }
 
325
        }
 
326
        
 
327
        /**
 
328
          * Gets the pages directory. It's derived from the label
 
329
          *
 
330
          * @param string $relativeTo Can be PATH_RELATIVETO_WEBROOT for relative to website root, or PATH_RELATIVETO_FILESYSTEM for relative to filesystem root
 
331
          * @return string The pages directory.
 
332
          * @access public
 
333
          */
 
334
        function getPagesPath($relativeTo)
 
335
        {
 
336
                if ($this->_label) {
 
337
                        if (SensitiveIO::isInSet($relativeTo, array(PATH_RELATIVETO_WEBROOT, PATH_RELATIVETO_FILESYSTEM))) {
 
338
                                $relative = ($relativeTo == PATH_RELATIVETO_WEBROOT) ? PATH_PAGES_WR : PATH_PAGES_FS;
 
339
                                if ($this->_isMain) {
 
340
                                        return $relative;
 
341
                                } else {
 
342
                                        return $relative."/".SensitiveIO::sanitizeAsciiString($this->_label);
 
343
                                }
 
344
                        } else {
 
345
                                $this->raiseError("Can't give pages path relative to anything other than WR or FS");
 
346
                                return false;
 
347
                        }
 
348
                } else {
 
349
                        return false;
 
350
                }
 
351
        }
 
352
        
 
353
        /**
 
354
          * Gets the pages directory. It's derived from the label
 
355
          *
 
356
          * @param string $relativeTo Can be PATH_RELATIVETO_WEBROOT for relative to website root, or PATH_RELATIVETO_FILESYSTEM for relative to filesystem root
 
357
          * @return string The pages directory.
 
358
          * @access public
 
359
          */
 
360
        function getHTMLPagesPath($relativeTo)
 
361
        {
 
362
                if ($this->_label) {
 
363
                        if (SensitiveIO::isInSet($relativeTo, array(PATH_RELATIVETO_WEBROOT, PATH_RELATIVETO_FILESYSTEM))) {
 
364
                                $relative = ($relativeTo == PATH_RELATIVETO_WEBROOT) ? PATH_PAGES_HTML_WR : PATH_PAGES_HTML_FS;
 
365
                                if ($this->_isMain) {
 
366
                                        return $relative;
 
367
                                } else {
 
368
                                        return $relative."/".SensitiveIO::sanitizeAsciiString($this->_label);
 
369
                                }
 
370
                        } else {
 
371
                                $this->raiseError("Can't give pages path relative to anything other than WR or FS");
 
372
                                return false;
 
373
                        }
 
374
                } else {
 
375
                        return false;
 
376
                }
 
377
        }
 
378
        
 
379
        /**
 
380
          * Totally destroys the website, including its directory
 
381
          * After deletion from database, launch a regen of the whole tree.
 
382
          *
 
383
          * @return void
 
384
          * @access public
 
385
          */
 
386
        function destroy()
 
387
        {
 
388
                if ($this->_id) {
 
389
                        $sql = "
 
390
                                delete
 
391
                                from
 
392
                                        websites
 
393
                                where
 
394
                                        id_web='".$this->_id."'
 
395
                        ";
 
396
                        $q = new CMS_query($sql);
 
397
                        
 
398
                        //deletes the pages and html directory (with all the pages inside)
 
399
                        if (!$this->_isMain) {
 
400
                                $dir = $this->getPagesPath(PATH_RELATIVETO_FILESYSTEM);
 
401
                                if ($opendir = @opendir($dir)) {
 
402
                                        while (false !== ($readdir = readdir($opendir))) {
 
403
                                                if($readdir !== '..' && $readdir !== '.') {
 
404
                                                        $readdir = trim($readdir);
 
405
                                                        if (is_file($dir.'/'.$readdir)) {
 
406
                                                                @unlink($dir.'/'.$readdir);
 
407
                                                        }
 
408
                                                }
 
409
                                        }
 
410
                                        closedir($opendir);
 
411
                                        @rmdir($dir);
 
412
                                }
 
413
                                $dir = $this->getHTMLPagesPath(PATH_RELATIVETO_FILESYSTEM);
 
414
                                if ($opendir = @opendir($dir)) {
 
415
                                        while (false !== ($readdir = readdir($opendir))) {
 
416
                                                if($readdir !== '..' && $readdir !== '.') {
 
417
                                                        $readdir = trim($readdir);
 
418
                                                        if (is_file($dir.'/'.$readdir)) {
 
419
                                                                @unlink($dir.'/'.$readdir);
 
420
                                                        }
 
421
                                                }
 
422
                                        }
 
423
                                        closedir($opendir);
 
424
                                        @rmdir($dir);
 
425
                                }
 
426
                        }
 
427
                        
 
428
                        //regenerates all the pages
 
429
                        CMS_tree::regenerateAllPages(true);
 
430
                }
 
431
                unset($this);
 
432
        }
 
433
        
 
434
        /**
 
435
          * Writes the website into persistence (MySQL for now).
 
436
          *
 
437
          * @return boolean true on success, false on failure
 
438
          * @access public
 
439
          */
 
440
        function writeToPersistence()
 
441
        {
 
442
                if (!sensitiveIO::isPositiveInteger($this->_order)) {
 
443
                        //get max order
 
444
                        $sql = "
 
445
                                select 
 
446
                                        max(order_web) as order_max
 
447
                                from
 
448
                                        websites
 
449
                        ";
 
450
                        $q = new CMS_query($sql);
 
451
                        if ($q->hasError() || !$q->getNumRows()) {
 
452
                                CMS_grandFather::raiseError('Error to get max order from websites table ... ');
 
453
                                return false;
 
454
                        }
 
455
                        $this->_order = ($q->getValue('order_max')+1) ;
 
456
                }
 
457
                $sql_fields = "
 
458
                        label_web='".SensitiveIO::sanitizeSQLString($this->_label)."',
 
459
                        url_web='".SensitiveIO::sanitizeSQLString($this->_url)."',
 
460
                        root_web='".$this->_root->getID()."',
 
461
                        keywords_web='".SensitiveIO::sanitizeSQLString($this->_meta['keywords'])."',
 
462
                        description_web='".SensitiveIO::sanitizeSQLString($this->_meta['description'])."',
 
463
                        category_web='".SensitiveIO::sanitizeSQLString($this->_meta['category'])."',
 
464
                        author_web='".SensitiveIO::sanitizeSQLString($this->_meta['author'])."',
 
465
                        replyto_web='".SensitiveIO::sanitizeSQLString($this->_meta['replyto'])."',
 
466
                        copyright_web='".SensitiveIO::sanitizeSQLString($this->_meta['copyright'])."',
 
467
                        language_web='".SensitiveIO::sanitizeSQLString($this->_meta['language'])."',
 
468
                        robots_web='".SensitiveIO::sanitizeSQLString($this->_meta['robots'])."',
 
469
                        favicon_web='".SensitiveIO::sanitizeSQLString($this->_meta['favicon'])."',
 
470
                        order_web='".SensitiveIO::sanitizeSQLString($this->_order)."'
 
471
                ";
 
472
                if ($this->_id) {
 
473
                        $sql = "
 
474
                                update
 
475
                                        websites
 
476
                                set
 
477
                                        ".$sql_fields."
 
478
                                where
 
479
                                        id_web='".$this->_id."'
 
480
                        ";
 
481
                } else {
 
482
                        $sql = "
 
483
                                insert into
 
484
                                        websites
 
485
                                set
 
486
                                        ".$sql_fields;
 
487
                }
 
488
                $q = new CMS_query($sql);
 
489
                if ($q->hasError()) {
 
490
                        return false;
 
491
                } elseif (!$this->_id) {
 
492
                        $this->_id = $q->getLastInsertedID();
 
493
                }
 
494
                //create the page directory
 
495
                if (!is_dir($this->getPagesPath(PATH_RELATIVETO_FILESYSTEM))) {
 
496
                        @mkdir($this->getPagesPath(PATH_RELATIVETO_FILESYSTEM));
 
497
                        @chmod($this->getPagesPath(PATH_RELATIVETO_FILESYSTEM), octdec(DIRS_CHMOD));
 
498
                }
 
499
                if (!is_dir($this->getHTMLPagesPath(PATH_RELATIVETO_FILESYSTEM))) {
 
500
                        //create the html directory
 
501
                        @mkdir($this->getHTMLPagesPath(PATH_RELATIVETO_FILESYSTEM));
 
502
                        @chmod($this->getHTMLPagesPath(PATH_RELATIVETO_FILESYSTEM), octdec(DIRS_CHMOD));
 
503
                }
 
504
                return true;
 
505
        }
 
506
}
 
507
?>
 
 
b'\\ No newline at end of file'