Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • PHP
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cLayoutHandler
  • cLayoutSynchronizer
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * This file contains the layout handler class.
  4:  *
  5:  * @package Core
  6:  * @subpackage LayoutHandler
  7:  * @author Rusmir Jusufovic
  8:  * @copyright four for business AG <www.4fb.de>
  9:  * @license http://www.contenido.org/license/LIZENZ.txt
 10:  * @link http://www.4fb.de
 11:  * @link http://www.contenido.org
 12:  */
 13: 
 14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 15: 
 16: /**
 17:  * This class controls all layouts in filesystem.
 18:  *
 19:  * @package Core
 20:  * @subpackage LayoutHandler
 21:  */
 22: class cLayoutHandler {
 23: 
 24:     /**
 25:      * The ID of the layout
 26:      *
 27:      * @var int
 28:      */
 29:     protected $_layoutId = 0;
 30: 
 31:     /**
 32:      * The code of the layout
 33:      *
 34:      * @var string
 35:      */
 36:     protected $_layoutCode = '';
 37: 
 38:     /**
 39:      *
 40:      * @var cDb
 41:      */
 42:     protected $_db = NULL;
 43: 
 44:     /**
 45:      * Layout name
 46:      *
 47:      * @var string
 48:      */
 49:     protected $_layoutName = '';
 50: 
 51:     /**
 52:      * The contenido cfg
 53:      *
 54:      * @var array
 55:      */
 56:     protected $_cfg = array();
 57: 
 58:     /**
 59:      * Encoding of the page
 60:      *
 61:      * @var string
 62:      */
 63:     protected $_encoding;
 64: 
 65:     /**
 66:      * Layout path
 67:      * [layout_path].layoutName/
 68:      *
 69:      * @var string
 70:      */
 71:     protected $_layoutPath = '';
 72: 
 73:     /**
 74:      * Main path of layouts.
 75:      * [layout_path].layouts
 76:      *
 77:      * @var string
 78:      */
 79:     protected $_layoutMainPath = '';
 80: 
 81:     /**
 82:      * File name of the layout ([layoutname].html
 83:      *
 84:      * @var string
 85:      */
 86:     protected $_fileName = '';
 87: 
 88:     /**
 89:      * Constructor to create an instance of this class.
 90:      *
 91:      * @param int    $layoutId
 92:      * @param string $layoutCode [optional]
 93:      * @param array  $cfg        [optional]
 94:      * @param int    $lang       [optional]
 95:      * @param cDb    $db         [optional]
 96:      *                           CONTENIDO database object
 97:      *
 98:      * @throws cDbException
 99:      * @throws cInvalidArgumentException
100:      */
101:     public function __construct($layoutId = 0, $layoutCode = '', array $cfg = array(), $lang = 0, cDb $db = NULL) {
102:         if ($db === NULL) {
103:             $db = cRegistry::getDb();
104:         }
105: 
106:         $this->_layoutId = $layoutId;
107:         $this->_db = $db;
108:         $this->init($layoutId, $layoutCode, $cfg, $lang);
109:     }
110: 
111:     /**
112:      * Get method for Layout path
113:      *
114:      * @return string
115:      */
116:     public function _getLayoutPath() {
117:         return $this->_layoutPath;
118:     }
119: 
120:     /**
121:      * Get method for Filename
122:      *
123:      * @return string
124:      */
125:     public function _getFileName() {
126:         return $this->_fileName;
127:     }
128: 
129:     /**
130:      * Look in layout directory if layout [$layoutAlias] directory exists
131:      *
132:      * @param string $layoutAlias
133:      * @param array $cfgClient
134:      * @param int $client
135:      * @return bool
136:      *         true if file exist
137:      */
138:     static function existLayout($layoutAlias, $cfgClient, $client) {
139:         $file = $cfgClient[$client]['layout']['path'] . $layoutAlias . '/';
140:         return cFileHandler::exists($file);
141:     }
142: 
143:     /**
144:      * Init all vars for the class
145:      *
146:      * @param int    $layoutId
147:      * @param string $layoutCode
148:      * @param array  $cfg
149:      * @param int    $language
150:      *
151:      * @throws cDbException
152:      * @throws cInvalidArgumentException
153:      */
154:     public function init($layoutId, $layoutCode, $cfg, $language) {
155:         $this->_layoutCode = $layoutCode;
156:         $this->_cfg = $cfg;
157: 
158:         // set encoding
159:         $this->_setEncoding($language);
160: 
161:         if ((int) $layoutId == 0) {
162:             return;
163:         }
164: 
165:         global $cfgClient, $client;
166: 
167:         $cApiLayout = new cApiLayout($layoutId);
168: 
169:         if (true === $cApiLayout->isLoaded() && is_array($cfgClient) && (int) $client > 0) {
170:             $this->_layoutName = $cApiLayout->get('alias');
171:             $this->_layoutMainPath = $cfgClient[$client]['layout']['path'];
172:             $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . '/';
173:             $this->_fileName = $this->_layoutName . '.html';
174: 
175:             // make directoryies for layout
176:             $this->_makeDirectories();
177:         }
178:     }
179: 
180:     /**
181:      * Get the layout name
182:      *
183:      * @return string
184:      *         layoutname
185:      */
186:     public function getLayoutName() {
187:         return $this->_layoutName;
188:     }
189: 
190:     /**
191:      * Init class vars with values, only use for setup or upgrade
192:      *
193:      * @param cDb $dbObject
194:      *         CONTENIDO database object
195:      * 
196:      * @throws cInvalidArgumentException
197:      */
198:     public function initWithDbObject($dbObject) {
199:         global $cfgClient;
200: 
201:         $this->_layoutCode = $dbObject->f('code');
202:         $this->_layoutName = $dbObject->f('alias');
203:         $this->_layoutMainPath = $cfgClient[$dbObject->f('idclient')]['layout']['path'];
204:         $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . '/';
205:         $this->_fileName = $this->_layoutName . '.html';
206: 
207:         // make directories for layout
208:         $this->_makeDirectories();
209:     }
210: 
211:     /**
212:      * Make all directories for layout.
213:      * Main directory and Layout directory
214:      *
215:      * @return bool
216:      *         true if successfully
217:      * 
218:      * @throws cInvalidArgumentException
219:      */
220:     private function _makeDirectories() {
221:         if ($this->_makeDirectory($this->_layoutMainPath)) {
222:             if ($this->_makeDirectory($this->_layoutPath)) {
223:                 return true;
224:             }
225:         }
226: 
227:         return false;
228:     }
229: 
230:     /**
231:      * Make directory
232:      *
233:      * @param string $directory
234:      *
235:      * @return bool
236:      *         true if succssesfully
237:      * 
238:      * @throws cInvalidArgumentException
239:      */
240:     private function _makeDirectory($directory) {
241:         if (is_dir($directory)) {
242:             $success = true;
243:         } else {
244:             $success = mkdir($directory);
245:             if ($success) {
246:                 cDirHandler::setDefaultPermissions($directory);
247:             }
248:         }
249: 
250:         return $success;
251:     }
252: 
253:     /**
254:      * Save encoding from language.
255:      *
256:      * @param int $lang
257:      *
258:      * @throws cDbException
259:      */
260:     private function _setEncoding($lang) {
261:         if ((int) $lang == 0) {
262:             $clientId = cRegistry::getClientId();
263: 
264:             $clientsLangColl = new cApiClientLanguageCollection();
265:             $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
266:             sort($clientLanguages);
267: 
268:             if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
269:                 $languageId = $clientLanguages[0];
270:             }
271:         } else {
272:             $languageId = $lang;
273:         }
274: 
275:         $cApiLanguage = new cApiLanguage($languageId);
276:         $encoding = $cApiLanguage->get('encoding');
277: 
278:         $this->_encoding = $encoding;
279:     }
280: 
281:     /**
282:      * Can write/create a file
283:      *
284:      * @param string $fileName
285:      *         file name
286:      * @param string $directory
287:      *         directory where is the file
288:      * @return bool
289:      *         true on success else false
290:      */
291:     public function isWritable($fileName, $directory) {
292:         if (cFileHandler::exists($fileName)) {
293:             if (!is_writable($fileName)) {
294:                 return false;
295:             }
296:         } else {
297:             if (!is_writable($directory)) {
298:                 return false;
299:             }
300:         }
301: 
302:         return true;
303:     }
304: 
305:     /**
306:      * Save Layout
307:      *
308:      * @param string $layoutCode [optional]
309:      *                           
310:      * @return bool
311:      * 
312:      * @throws cInvalidArgumentException
313:      */
314:     public function saveLayout($layoutCode = '') {
315:         $fileName = $this->_layoutPath . $this->_fileName;
316: 
317:         if (!$this->isWritable($fileName, $this->_layoutPath)) {
318:             return false;
319:         }
320: 
321:         return $this->_save($layoutCode);
322:     }
323: 
324:     /**
325:      * Save the layout only if layout doesn't exist in filesystem!
326:      * Use it for upgrade!
327:      *
328:      * @param string $layoutCode [optional]
329:      *                           
330:      * @return bool
331:      * 
332:      * @throws cInvalidArgumentException
333:      */
334:     public function saveLayoutByUpgrade($layoutCode = '') {
335:         // if file exist dont overwirte it
336:         if (cFileHandler::exists($this->_layoutPath . $this->_fileName)) {
337:             return true;
338:         }
339: 
340:         return $this->_save($layoutCode);
341:     }
342: 
343:     /**
344:      *
345:      * @param string $layoutCode [optional]
346:      *                           
347:      * @return bool
348:      * 
349:      * @throws cInvalidArgumentException
350:      */
351:     private function _save($layoutCode = '') {
352:         if ($layoutCode == '') {
353:             $layoutCode = $this->_layoutCode;
354:         }
355: 
356:         // exist layout path
357:         if (!is_dir($this->_layoutPath)) {
358:             return false;
359:         }
360: 
361:         // convert
362:         $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
363:         $layoutCode = cString::recodeString($layoutCode, $this->_encoding, $fileEncoding);
364: 
365:         $save = cFileHandler::write($this->_layoutPath . $this->_fileName, $layoutCode);
366: 
367:         return (cString::getStringLength($layoutCode) == 0 && $save == 0) || $save > 0;
368:     }
369: 
370:     /**
371:      * Removes this layout from the filesystem.
372:      * Also deletes the version files.
373:      *
374:      * @return bool
375:      *         true on success or false on failure
376:      * 
377:      * @throws cInvalidArgumentException
378:      */
379:     public function eraseLayout() {
380:         global $area, $frame;
381:         $cfg = cRegistry::getConfig();
382:         $cfgClient = cRegistry::getClientConfig();
383:         $db = cRegistry::getDb();
384:         $client = cRegistry::getClientId();
385: 
386:         $layoutVersion = new cVersionLayout($this->_layoutId, $cfg, $cfgClient, $db, $client, $area, $frame);
387:         $success = true;
388:         if (count($layoutVersion->getRevisionFiles()) > 0 && !$layoutVersion->deleteFile()) {
389:             $success = false;
390:         }
391: 
392:         return $success && cDirHandler::recursiveRmdir($this->_layoutPath);
393:     }
394: 
395:     /**
396:      * Rename the Layout directory and layout file
397:      *
398:      * @param string $old
399:      * @param string $new
400:      * @return bool
401:      */
402:     public function rename($old, $new) {
403:         // try to rename the dir
404:         $newPath = $this->_layoutMainPath . $new . '/';
405: 
406:         $newFileName = $new . '.html';
407: 
408:         if (rename($this->_layoutPath, $newPath) == FALSE) {
409:             return false;
410:         }
411: 
412:         // if file input exist rename it
413:         if (!cFileHandler::exists($newPath . $this->_fileName)) {
414:             return false;
415:         }
416: 
417:         if (!rename($newPath . $this->_fileName, $newPath . $newFileName)) {
418:             return false;
419:         }
420: 
421:         $this->_layoutName = $new;
422:         $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . '/';
423:         $this->_fileName = $this->_layoutName . '.html';
424: 
425:         return true;
426:     }
427: 
428:     /**
429:      * Get the contents of the file
430:      *
431:      * @return string|bool
432:      *         content or false
433:      * 
434:      * @throws cInvalidArgumentException
435:      */
436:     public function getLayoutCode() {
437:         // cant read it dont exist file
438:         if (!is_readable($this->_layoutPath . $this->_fileName)) {
439:             return false;
440:         }
441: 
442:         if (($content = cFileHandler::read($this->_layoutPath . $this->_fileName)) === FALSE) {
443:             return false;
444:         } else {
445:             // convert
446:             $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
447:             $content = iconv($fileEncoding, $this->_encoding . '//IGNORE', $content);
448:             return $content;
449:         }
450:     }
451: 
452:     /**
453:      * Save all layout in file system.
454:      * Use it for upgrade.
455:      *
456:      * @param cDb $adb
457:      *         CONTENIDO database object
458:      * @param array $cfg
459:      *         CONTENIDO config array
460:      * @param int $clientId
461:      * 
462:      * @throws cException if the layout could not be saved
463:      */
464:     public static function upgrade($adb, $cfg, $clientId) {
465:         // get name of layout and frontendpath
466:         if (!$adb->query("SELECT * FROM `%s` WHERE idclient='%s'", $cfg['tab']['lay'], $clientId)) {
467:             return;
468:         }
469: 
470:         while ($adb->nextRecord()) {
471:             // init class var for save
472:             $layout = new cLayoutHandler();
473:             $layout->initWithDbObject($adb);
474:             if ($layout->saveLayoutByUpgrade($adb->f('code')) == false) {
475:                 throw new cException('Can not save layout.' . print_r($layout, true));
476:             }
477:         }
478: 
479:         // all layouts are saved, so remove the code field from _lay
480:         $sql = sprintf("UPDATE %s SET code = '' WHERE idclient='%s'", $cfg['tab']['lay'], $clientId);
481:         $adb->query($sql);
482:     }
483: }
484: 
CMS CONTENIDO 4.10.0 API documentation generated by ApiGen 2.8.0