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
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • 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:      */
 97:     public function __construct($layoutId = 0, $layoutCode = "", array $cfg = array(), $lang = 0, cDb $db = NULL) {
 98:         if ($db === NULL) {
 99:             $db = cRegistry::getDb();
100:         }
101: 
102:         $this->_layoutId = $layoutId;
103:         $this->_db = $db;
104:         $this->init($layoutId, $layoutCode, $cfg, $lang);
105:     }
106: 
107:     /**
108:      * Get method for Layout path
109:      *
110:      * @return string
111:      */
112:     public function _getLayoutPath() {
113:         return $this->_layoutPath;
114:     }
115: 
116:     /**
117:      * Get method for Filename
118:      *
119:      * @return string
120:      */
121:     public function _getFileName() {
122:         return $this->_fileName;
123:     }
124: 
125:     /**
126:      * Look in layout directory if layout [$layoutAlias] directory exists
127:      *
128:      * @param string $layoutAlias
129:      * @param array $cfgClient
130:      * @param int $client
131:      * @return bool
132:      *         true if file exist
133:      */
134:     static function existLayout($layoutAlias, $cfgClient, $client) {
135:         $file = $cfgClient[$client]['layout']['path'] . $layoutAlias . '/';
136:         return cFileHandler::exists($file);
137:     }
138: 
139:     /**
140:      * Init all vars for the class
141:      *
142:      * @param int $layoutId
143:      * @param string $layoutCode
144:      * @param array $cfg
145:      * @param int $language
146:      */
147:     public function init($layoutId, $layoutCode, $cfg, $language) {
148:         $this->_layoutCode = $layoutCode;
149:         $this->_cfg = $cfg;
150: 
151:         // set encoding
152:         $this->_setEncoding($language);
153: 
154:         if ((int) $layoutId == 0) {
155:             return;
156:         }
157: 
158:         global $cfgClient, $client;
159: 
160:         $cApiLayout = new cApiLayout($layoutId);
161: 
162:         if (true === $cApiLayout->isLoaded() && is_array($cfgClient) && (int) $client > 0) {
163:             $this->_layoutName = $cApiLayout->get('alias');
164:             $this->_layoutMainPath = $cfgClient[$client]['layout']['path'];
165:             $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
166:             $this->_fileName = $this->_layoutName . ".html";
167: 
168:             // make directoryies for layout
169:             $this->_makeDirectories();
170:         }
171:     }
172: 
173:     /**
174:      * Get the layout name
175:      *
176:      * @return string
177:      *         layoutname
178:      */
179:     public function getLayoutName() {
180:         return $this->_layoutName;
181:     }
182: 
183:     /**
184:      * Init class vars with values, only use for setup or upgrade
185:      *
186:      * @param cDb $dbObject
187:      */
188:     public function initWithDbObject($dbObject) {
189:         global $cfgClient, $client;
190: 
191:         $this->_layoutCode = $dbObject->f("code");
192:         $this->_layoutName = $dbObject->f("alias");
193:         $this->_layoutMainPath = $cfgClient[$dbObject->f("idclient")]['layout']['path'];
194:         $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
195:         $this->_fileName = $this->_layoutName . ".html";
196: 
197:         // make directories for layout
198:         $this->_makeDirectories();
199:     }
200: 
201:     /**
202:      * Make all directories for layout.
203:      * Main directory and Layout directory
204:      *
205:      * @return bool
206:      *         true if successfully
207:      */
208:     private function _makeDirectories() {
209:         if ($this->_makeDirectory($this->_layoutMainPath)) {
210:             if ($this->_makeDirectory($this->_layoutPath)) {
211:                 return true;
212:             }
213:         }
214: 
215:         return false;
216:     }
217: 
218:     /**
219:      * Make directory
220:      *
221:      * @param string $directory
222:      * @return bool
223:      *         true if succssesfully
224:      */
225:     private function _makeDirectory($directory) {
226:         if (is_dir($directory)) {
227:             $success = true;
228:         } else {
229:             $success = mkdir($directory);
230:             if ($success) {
231:                 cDirHandler::setDefaultDirPerms($directory);
232:             }
233:         }
234: 
235:         return $success;
236:     }
237: 
238:     /**
239:      * Save encoding from language.
240:      *
241:      * @param int $lang
242:      */
243:     private function _setEncoding($lang) {
244:         if ((int) $lang == 0) {
245:             $clientId = cRegistry::getClientId();
246: 
247:             $clientsLangColl = new cApiClientLanguageCollection();
248:             $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
249:             sort($clientLanguages);
250: 
251:             if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
252:                 $languageId = $clientLanguages[0];
253:             }
254:         } else {
255:             $languageId = $lang;
256:         }
257: 
258:         $cApiLanguage = new cApiLanguage($languageId);
259:         $encoding = $cApiLanguage->get('encoding');
260: 
261:         $this->_encoding = $encoding;
262:     }
263: 
264:     /**
265:      * Can write/create a file
266:      *
267:      * @param string $fileName
268:      *         file name
269:      * @param string $directory
270:      *         directory where is the file
271:      * @return bool
272:      *         true on success else false
273:      */
274:     public function isWritable($fileName, $directory) {
275:         if (cFileHandler::exists($fileName)) {
276:             if (!is_writable($fileName)) {
277:                 return false;
278:             }
279:         } else {
280:             if (!is_writable($directory)) {
281:                 return false;
282:             }
283:         }
284: 
285:         return true;
286:     }
287: 
288:     /**
289:      * Save Layout
290:      *
291:      * @param string $layoutCode [optional]
292:      * @return bool
293:      */
294:     public function saveLayout($layoutCode = '') {
295:         $fileName = $this->_layoutPath . $this->_fileName;
296: 
297:         if (!$this->isWritable($fileName, $this->_layoutPath)) {
298:             return false;
299:         }
300: 
301:         return $this->_save($layoutCode);
302:     }
303: 
304:     /**
305:      * Save the layout only if layout doesn't exist in filesystem!
306:      * Use it for upgrade!
307:      *
308:      * @param string $layoutCode [optional]
309:      * @return bool
310:      */
311:     public function saveLayoutByUpgrade($layoutCode = '') {
312:         // if file exist dont overwirte it
313:         if (cFileHandler::exists($this->_layoutPath . $this->_fileName)) {
314:             return true;
315:         }
316: 
317:         return $this->_save($layoutCode);
318:     }
319: 
320:     /**
321:      *
322:      * @param string $layoutCode [optional]
323:      * @return bool
324:      */
325:     private function _save($layoutCode = '') {
326:         if ($layoutCode == '') {
327:             $layoutCode = $this->_layoutCode;
328:         }
329: 
330:         // exist layout path
331:         if (!is_dir($this->_layoutPath)) {
332:             return false;
333:         }
334: 
335:         // convert
336:         $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
337:         $layoutCode = cString::recodeString($layoutCode, $this->_encoding, $fileEncoding);
338: 
339:         $save = cFileHandler::write($this->_layoutPath . $this->_fileName, $layoutCode);
340: 
341:         return (strlen($layoutCode) == 0 && $save == 0) || $save > 0;
342:     }
343: 
344:     /**
345:      * Removes this layout from the filesystem.
346:      * Also deletes the version files.
347:      *
348:      * @return bool
349:      *         true on success or false on failure
350:      */
351:     public function eraseLayout() {
352:         global $area, $frame;
353:         $cfg = cRegistry::getConfig();
354:         $cfgClient = cRegistry::getClientConfig();
355:         $db = cRegistry::getDb();
356:         $client = cRegistry::getClientId();
357: 
358:         $layoutVersion = new cVersionLayout($this->_layoutId, $cfg, $cfgClient, $db, $client, $area, $frame);
359:         $success = true;
360:         if (count($layoutVersion->getRevisionFiles()) > 0 && !$layoutVersion->deleteFile()) {
361:             $success = false;
362:         }
363: 
364:         return $success && cDirHandler::recursiveRmdir($this->_layoutPath);
365:     }
366: 
367:     /**
368:      * Rename the Layout directory and layout file
369:      *
370:      * @param string $old
371:      * @param string $new
372:      * @return bool
373:      */
374:     public function rename($old, $new) {
375:         // try to rename the dir
376:         $newPath = $this->_layoutMainPath . $new . "/";
377: 
378:         $newFileName = $new . ".html";
379: 
380:         if (rename($this->_layoutPath, $newPath) == FALSE) {
381:             return false;
382:         }
383: 
384:         // if file input exist rename it
385:         if (!cFileHandler::exists($newPath . $this->_fileName)) {
386:             return false;
387:         }
388: 
389:         if (!rename($newPath . $this->_fileName, $newPath . $newFileName)) {
390:             return false;
391:         }
392: 
393:         $this->_layoutName = $new;
394:         $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
395:         $this->_fileName = $this->_layoutName . ".html";
396: 
397:         return true;
398:     }
399: 
400:     /**
401:      * Get the contents of the file
402:      *
403:      * @return string|bool
404:      *         content or false
405:      */
406:     public function getLayoutCode() {
407:         // cant read it dont exist file
408:         if (!is_readable($this->_layoutPath . $this->_fileName)) {
409:             return false;
410:         }
411: 
412:         if (($content = cFileHandler::read($this->_layoutPath . $this->_fileName)) === FALSE) {
413:             return false;
414:         } else {
415:             // convert
416:             $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
417:             $content = iconv($fileEncoding, $this->_encoding . "//IGNORE", $content);
418:             return $content;
419:         }
420:     }
421: 
422:     /**
423:      * Save all layout in file system.
424:      * Use it for upgrade.
425:      *
426:      * @throws cException if the layout could not be saved
427:      * @param cDb $adb
428:      *         database object
429:      * @param array $cfg
430:      *         CONTENIDO config array
431:      * @param int $clientId
432:      */
433:     public static function upgrade($adb, $cfg, $clientId) {
434:         // get name of layout and frontendpath
435:         if (!$adb->query("SELECT * FROM `%s` WHERE idclient='%s'", $cfg['tab']['lay'], $clientId)) {
436:             return;
437:         }
438: 
439:         while ($adb->nextRecord()) {
440:             // init class var for save
441:             $layout = new cLayoutHandler();
442:             $layout->initWithDbObject($adb);
443:             if ($layout->saveLayoutByUpgrade($adb->f('code')) == false) {
444:                 throw new cException('Can not save layout.' . print_r($layout, true));
445:             }
446:         }
447: 
448:         // all layouts are saved, so remove the code field from _lay
449:         $sql = sprintf("UPDATE %s SET code = '' WHERE idclient='%s'", $cfg['tab']['lay'], $clientId);
450:         $adb->query($sql);
451:     }
452: }
453: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0