Overview

Packages

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