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 synchronizer 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: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 14: 
 15: /**
 16:  * This class synchronizes layouts from filesystem to database table.
 17:  *
 18:  * @package Core
 19:  * @subpackage LayoutHandler
 20:  */
 21: class cLayoutSynchronizer {
 22: 
 23:     /**
 24:      *
 25:      * @var array
 26:      */
 27:     protected $_cfg;
 28: 
 29:     /**
 30:      *
 31:      * @var array
 32:      */
 33:     protected $_cfgClient;
 34: 
 35:     /**
 36:      *
 37:      * @var int
 38:      */
 39:     protected $_lang;
 40: 
 41:     /**
 42:      *
 43:      * @var int
 44:      */
 45:     protected $_client;
 46: 
 47:     /**
 48:      *
 49:      * @var array
 50:      */
 51:     private $_outputMessage = array();
 52: 
 53:     /**
 54:      * Constructor to create an instance of this class.
 55:      *
 56:      * @param array $cfg
 57:      * @param array $cfgClient
 58:      * @param int $lang
 59:      * @param int $client
 60:      */
 61:     public function __construct($cfg, $cfgClient, $lang, $client) {
 62:         $this->_cfg = $cfg;
 63:         $this->_cfgClient = $cfgClient;
 64:         $this->_lang = $lang;
 65:         $this->_client = $client;
 66:     }
 67: 
 68:     /**
 69:      * Add a Layout to table or update a layout
 70:      *
 71:      * @param string $dir
 72:      * @param string $oldLayoutName
 73:      * @param string $newLayoutName
 74:      * @param string $idclient
 75:      */
 76:     private function _addOrUpdateLayout($dir, $oldLayoutName, $newLayoutName, $idclient) {
 77:         // if layout dont exist in the $cfg["tab"]["lay"] table.
 78:         if ($this->_isExistInTable($oldLayoutName, $idclient) == false) {
 79:             // add new Layout in db-table
 80:             $layoutCollection = new cApiLayoutCollection();
 81:             $layoutCollection->create($newLayoutName, $idclient, $newLayoutName);
 82: 
 83:             // make a layout file if not exist
 84:             if (!cFileHandler::exists($dir . $newLayoutName . '/' . $newLayoutName . '.html')) {
 85:                 cFileHandler::write($dir . $newLayoutName . '/' . $newLayoutName . '.html', '');
 86:             }
 87: 
 88:             // set output message
 89:             $this->_outputMessage['info'][] = sprintf(i18n("Layout synchronization successful: %s"), $newLayoutName);
 90:         } else {
 91:             // update the name of the layout
 92:             if ($oldLayoutName != $newLayoutName) {
 93:                 $this->_updateModulnameInDb($oldLayoutName, $newLayoutName, $idclient);
 94:             }
 95:         }
 96:     }
 97: 
 98:     /**
 99:      * Update the name of layout (if the name not allowes)
100:      *
101:      * @param string $oldName
102:      *         old name
103:      * @param string $newName
104:      *         new module name
105:      * @param int $idclient
106:      *         id of client
107:      */
108:     private function _updateModulnameInDb($oldName, $newName, $idclient) {
109:         $oLayColl = new cApiLayoutCollection();
110:         $oLayColl->select("alias='" . $oLayColl->escape($oldName) . "' AND idclient=" . (int) $idclient);
111:         if (false !== $oLay = $oLayColl->next()) {
112:             $oLay->set('alias', $newName);
113:             $oLay->store();
114:         }
115:     }
116: 
117:     /**
118:      * Rename the directory and files
119:      *
120:      * @param string $dir
121:      * @param string $dirNameOld
122:      * @param string $dirNameNew
123:      * @param int $client
124:      * @return bool
125:      */
126:     private function _renameFileAndDir($dir, $dirNameOld, $dirNameNew, $client) {
127:         if (rename($dir . $dirNameOld, $dir . $dirNameNew) == false) {
128:             return false;
129:         }
130: 
131:         $this->_renameFiles($dir, $dirNameOld, $dirNameNew);
132: 
133:         return true;
134:     }
135: 
136:     /**
137:      * Exist the layout in db-table
138:      *
139:      * @param string $alias
140:      *         layout name
141:      * @param int $idclient
142:      *         client id
143:      * @return bool
144:      */
145:     private function _isExistInTable($alias, $idclient) {
146:         // Select depending from idclient all moduls wiht the name $name
147:         $oLayColl = new cApiLayoutCollection();
148:         $ids = $oLayColl->getIdsByWhereClause("alias='" . $oLayColl->escape($alias) . "' AND idclient=" . (int) $idclient);
149:         return (count($ids) > 0) ? true : false;
150:     }
151: 
152:     /**
153:      * Rename the Layout
154:      *
155:      * @param string $dir
156:      *         path to client layout-direcotry $dir
157:      * @param string $oldLayoutName
158:      *         layout name in file directory
159:      * @param string $newLayoutName
160:      *         clear layout name
161:      */
162:     private function _renameFiles($dir, $oldLayoutName, $newLayoutName) {
163:         if (cFileHandler::exists($dir . $newLayoutName . '/' . $oldLayoutName . '.html') == true) {
164:             rename($dir . $newLayoutName . '/' . $oldLayoutName . '.html', $dir . $newLayoutName . '/' . $newLayoutName . '.html');
165:         }
166:     }
167: 
168:     /**
169:      * Update the con_mod, the field lastmodified
170:      *
171:      * @param int $timestamp
172:      *         timestamp of last modification
173:      * @param int $idlay
174:      *         Id of layout
175:      */
176:     public function setLastModified($timestamp, $idlay) {
177:         $oLay = new cApiLayout((int) $idlay);
178:         if ($oLay->isLoaded()) {
179:             $oLay->set('lastmodified', date('Y-m-d H:i:s', $timestamp));
180:             $oLay->store();
181:         }
182:     }
183: 
184:     /**
185:      * Compare file change timestamp and the timestamp in ["tab"]["lay"].
186:      * If file had changed make new code :conGenerateCodeForAllArtsUsingMod
187:      */
188:     private function _compareFileAndLayoutTimestamp() {
189:         // get all layouts from client
190:         $sql = sprintf("SELECT UNIX_TIMESTAMP(lastmodified) AS lastmodified, alias, name, description, idlay FROM %s WHERE idclient=%s", $this->_cfg['tab']['lay'], $this->_client);
191:         $notification = new cGuiNotification();
192:         $dir = $this->_cfgClient[$this->_client]['layout']['path'];
193: 
194:         $db = cRegistry::getDb();
195:         $db->query($sql);
196:         $retIdMod = 0;
197:         while ($db->nextRecord()) {
198:             $lastmodified = $db->f('lastmodified');
199: 
200:             // exist layout directory
201:             if (is_dir($dir . $db->f('alias') . '/')) {
202:                 if (cFileHandler::exists($dir . $db->f('alias') . '/' . $db->f('alias') . '.html')) {
203:                     $lastmodifiedLayout = filemtime($dir . $db->f('alias') . '/' . $db->f('alias') . '.html');
204: 
205:                     // update layout data
206:                     if ($lastmodified < $lastmodifiedLayout) {
207:                         // update field lastmodified in table lay
208:                         $this->setLastModified($lastmodifiedLayout, $db->f('idlay'));
209:                         $layout = new cLayoutHandler($db->f('idlay'), ' ', $this->_cfg, $this->_lang);
210:                         // Update CODE table
211:                         conGenerateCodeForAllartsUsingLayout($db->f('idlay'));
212:                         $this->_outputMessage['info'][] = i18n("Layout synchronization successful: ") . $db->f('name');
213:                     }
214:                 }
215:             } else {
216:                 $oLayout = new cApiLayout($db->f('idlay'));
217: 
218:                 $layout = new cLayoutHandler($db->f('idlay'), '', $this->_cfg, $this->_lang);
219:                 // is layout in use
220:                 if ($oLayout->isInUse()) {
221:                     // make layout file
222:                     $layout->saveLayout('');
223:                     $this->_outputMessage['info'][] = i18n("Layout synchronization successful, created: ") . $db->f('name');
224:                 } else {
225:                     // if not in use delete layout
226:                     if ($layout->eraseLayout()) {
227:                         layDeleteLayout($db->f('idlay'));
228:                         $this->_outputMessage['info'][] = i18n("Layout synchronization successful, deleted: ") . $db->f('name');
229:                     } else {
230:                         $this->_outputMessage['error'][] = i18n("Synchronization failed could not delete layout: ") . $db->f('name');
231:                     }
232:                 }
233:             }
234:         }
235:     }
236: 
237:     /**
238:      */
239:     private function _showOutputMessage() {
240:         $emptyMessage = true;
241:         $notification = new cGuiNotification();
242:         foreach ($this->_outputMessage as $typ) {
243:             foreach ($typ as $message) {
244:                 $emptyMessage = false;
245:                 // show display massage
246:                 $notification->displayNotification($typ, $message);
247:             }
248:         }
249:         if ($emptyMessage) {
250:             $notification->displayNotification('info', i18n("Synchronization successful!"));
251:         }
252:     }
253: 
254:     /**
255:      * Synchronize the Layout directory with the lay-table und the lay-table
256:      * with directory.
257:      *
258:      * @return bool
259:      */
260:     public function synchronize() {
261:         // update file and layout
262:         $this->_compareFileAndLayoutTimestamp();
263: 
264:         // get the path to clients layouts
265:         $dir = $this->_cfgClient[$this->_client]['layout']['path'];
266: 
267:         // is/exist directory
268:         if (!is_dir($dir)) {
269:             return false;
270:         }
271: 
272:         if (false !== ($handle = cDirHandler::read($dir))) {
273:             foreach ($handle as $file) {
274:                 // skip dirs to exclude
275:                 // @todo should use setting for dirs to exclude
276:                 if (cFileHandler::fileNameBeginsWithDot($file)) {
277:                     continue;
278:                 }
279: 
280:                 // skip entries that are no directories
281:                 if (false === is_dir($dir . $file . "/")) {
282:                     continue;
283:                 }
284: 
285:                 $newFile = strtolower(cString::cleanURLCharacters($file));
286: 
287:                 if ($newFile == $file) {
288:                     // dir is ok
289:                     $this->_addOrUpdateLayout($dir, $file, $newFile, $this->_client);
290:                 } else {
291:                     // dir not ok (with not allowed characters)
292:                     if (is_dir($dir . $newFile) && strtolower($file) != $newFile) {
293:                         // exist the new dir name after clean?
294:                         // make new dirname
295:                         $newDirName = $newFile . substr(md5(time() . rand(0, time())), 0, 4);
296:                         // rename
297:                         if ($this->_renameFileAndDir($dir, $file, $newDirName, $this->_client) != false) {
298:                             $this->_addOrUpdateLayout($dir, $file, $newDirName, $this->_client);
299:                         }
300:                     } else {
301:                         // $newFile (dir) not exist
302:                         // rename dir old
303:                         if ($this->_renameFileAndDir($dir, $file, $newFile, $this->_client) != false) {
304:                             $this->_addOrUpdateLayout($dir, $file, $newFile, $this->_client);
305:                         }
306:                     }
307:                 }
308:             }
309:         }
310: 
311:         $this->_showOutputMessage();
312:     }
313: }
314: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0