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