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
    • NavigationMain
    • 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 unknown_type
 28:      */
 29:     protected $_cfg;
 30: 
 31:     /**
 32:      *
 33:      * @var unknown_type
 34:      */
 35:     protected $_lang;
 36: 
 37:     /**
 38:      *
 39:      * @var unknown_type
 40:      */
 41:     protected $_client;
 42: 
 43:     /**
 44:      *
 45:      * @var unknown_type
 46:      */
 47:     protected $_cfgClient;
 48: 
 49:     /**
 50:      *
 51:      * @var unknown_type
 52:      */
 53:     private $_outputMessage = array();
 54: 
 55:     /**
 56:      *
 57:      * @param unknown_type $cfg
 58:      * @param unknown_type $cfgClient
 59:      * @param unknown_type $lang
 60:      * @param unknown_type $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:      */
123:     private function _renameFileAndDir($dir, $dirNameOld, $dirNameNew, $client) {
124:         if (rename($dir . $dirNameOld, $dir . $dirNameNew) == false) {
125:             return false;
126:         }
127: 
128:         $this->_renameFiles($dir, $dirNameOld, $dirNameNew);
129: 
130:         return true;
131:     }
132: 
133:     /**
134:      * Exist the layout in db-table
135:      *
136:      * @param string $alias layout name
137:      * @param int $idclient client id
138:      */
139:     private function _isExistInTable($alias, $idclient) {
140:         // Select depending from idclient all moduls wiht the name $name
141:         $oLayColl = new cApiLayoutCollection();
142:         $ids = $oLayColl->getIdsByWhereClause("alias='" . $oLayColl->escape($alias) . "' AND idclient=" . (int) $idclient);
143:         return (count($ids) > 0)? true : false;
144:     }
145: 
146:     /**
147:      * Rename the Layout
148:      *
149:      * @param string path to client layout-direcotry $dir
150:      * @param string $oldLayoutName layout name in file directory
151:      * @param string $newLayoutName clear layout name
152:      */
153:     private function _renameFiles($dir, $oldLayoutName, $newLayoutName) {
154:         if (cFileHandler::exists($dir . $newLayoutName . '/' . $oldLayoutName . '.html') == true) {
155:             rename($dir . $newLayoutName . '/' . $oldLayoutName . '.html', $dir . $newLayoutName . '/' . $newLayoutName . '.html');
156:         }
157:     }
158: 
159:     /**
160:      * Update the con_mod, the field lastmodified
161:      *
162:      * @param int $timestamp timestamp of last modification
163:      * @param int $idlay Id of layout
164:      */
165:     public function setLastModified($timestamp, $idlay) {
166:         $oLay = new cApiLayout((int) $idlay);
167:         if ($oLay->isLoaded()) {
168:             $oLay->set('lastmodified', date('Y-m-d H:i:s', $timestamp));
169:             $oLay->store();
170:         }
171:     }
172: 
173:     /**
174:      * Compare file change timestamp and the timestamp in ["tab"]["lay"].
175:      * If file had changed make new code :conGenerateCodeForAllArtsUsingMod
176:      */
177:     private function _compareFileAndLayoutTimestamp() {
178:         // get all layouts from client
179:         $sql = sprintf("SELECT UNIX_TIMESTAMP(lastmodified) AS lastmodified, alias, name, description, idlay FROM %s WHERE idclient=%s", $this->_cfg['tab']['lay'], $this->_client);
180:         $notification = new cGuiNotification();
181:         $dir = $this->_cfgClient[$this->_client]['layout']['path'];
182: 
183:         $db = cRegistry::getDb();
184:         $db->query($sql);
185:         $retIdMod = 0;
186:         while ($db->nextRecord()) {
187:             $lastmodified = $db->f('lastmodified');
188: 
189:             // exist layout directory
190:             if (is_dir($dir . $db->f('alias') . '/')) {
191:                 if (cFileHandler::exists($dir . $db->f('alias') . '/' . $db->f('alias') . '.html')) {
192:                     $lastmodifiedLayout = filemtime($dir . $db->f('alias') . '/' . $db->f('alias') . '.html');
193: 
194:                     // update layout data
195:                     if ($lastmodified < $lastmodifiedLayout) {
196:                         // update field lastmodified in table lay
197:                         $this->setLastModified($lastmodifiedLayout, $db->f('idlay'));
198:                         $layout = new cLayoutHandler($db->f('idlay'), ' ', $this->_cfg, $this->_lang);
199:                         // Update CODE table
200:                         conGenerateCodeForAllartsUsingLayout($db->f('idlay'));
201:                         $this->_outputMessage['info'][] = i18n("Synchronization successfully layout name: ") . $db->f('name');
202:                     }
203:                 }
204:             } else {
205:                 $oLayout = new cApiLayout($db->f('idlay'));
206: 
207:                 $layout = new cLayoutHandler($db->f('idlay'), '', $this->_cfg, $this->_lang);
208:                 // is layout in use
209:                 if ($oLayout->isInUse()) {
210:                     // make layout file
211:                     $layout->saveLayout('');
212:                     $this->_outputMessage['info'][] = i18n("Synchronization successfully layout name made: ") . $db->f('name');
213:                 } else {
214:                     // if not in use delete layout
215:                     if ($layout->eraseLayout()) {
216:                         layDeleteLayout($db->f('idlay'));
217:                         $this->_outputMessage['info'][] = i18n("Synchronization successfully layout deleted: ") . $db->f('name');
218:                     } else {
219:                         $this->_outputMessage['error'][] = i18n("Synchronization faild cold not delate layout: ") . $db->f('name');
220:                     }
221:                 }
222:             }
223:         }
224:     }
225: 
226:     private function _showOutputMessage() {
227:         $emptyMessage = true;
228:         $notification = new cGuiNotification();
229:         foreach ($this->_outputMessage as $typ) {
230:             foreach ($typ as $message) {
231:                 $emptyMessage = false;
232:                 // show display massage
233:                 $notification->displayNotification($typ, $message);
234:             }
235:         }
236:         if ($emptyMessage) {
237:             $notification->displayNotification('info', i18n("Synchronization successfully!"));
238:         }
239:     }
240: 
241:     /**
242:      * If the first char a '.' return false else true
243:      *
244:      * @param string $file
245:      * @return boolean true if the first char !='.' else false
246:      */
247:     private function _isValidFirstChar($file) {
248:         return (!(substr($file, 0, 1) == '.'));
249:     }
250: 
251:     /**
252:      * Synchronize the Layout directory with the lay-table und the lay-table
253:      * with directory.
254:      */
255:     public function synchronize() {
256:         // update file and layout
257:         $this->_compareFileAndLayoutTimestamp();
258: 
259:         // get the path to clients layouts
260:         $dir = $this->_cfgClient[$this->_client]['layout']['path'];
261: 
262:         // is/exist directory
263:         if (!is_dir($dir)) {
264:             return false;
265:         }
266: 
267:         if (false !== $dh = opendir($dir)) {
268:             while (false !== $file = readdir($dh)) {
269: 
270:                 // skip dirs to exclude
271:                 // @todo should use setting for dirs to exclude
272:                 if (!$this->_isValidFirstChar($file)) {
273:                     continue;
274:                 }
275: 
276:                 // skip entries that are no directories
277:                 if (false === is_dir($dir . $file . "/")) {
278:                     continue;
279:                 }
280: 
281:                 $newFile = strtolower(cApiStrCleanURLCharacters($file));
282: 
283:                 if ($newFile == $file) {
284:                     // dir is ok
285:                     $this->_addOrUpdateLayout($dir, $file, $newFile, $this->_client);
286:                 } else {
287:                     // dir not ok (with not allowed characters)
288:                     if (is_dir($dir . $newFile) && strtolower($file) != $newFile) {
289:                         // exist the new dir name after clean?
290:                         // make new dirname
291:                         $newDirName = $newFile . substr(md5(time() . rand(0, time())), 0, 4);
292:                         // rename
293:                         if ($this->_renameFileAndDir($dir, $file, $newDirName, $this->_client) != false) {
294:                             $this->_addOrUpdateLayout($dir, $file, $newDirName, $this->_client);
295:                         }
296:                     } else {
297:                         // $newFile (dir) not exist
298:                         // rename dir old
299:                         if ($this->_renameFileAndDir($dir, $file, $newFile, $this->_client) != false) {
300:                             $this->_addOrUpdateLayout($dir, $file, $newFile, $this->_client);
301:                         }
302:                     }
303:                 }
304:             }
305: 
306:             // close dir
307:             closedir($dh);
308:         }
309: 
310:         $this->_showOutputMessage();
311:     }
312: 
313: }
314: 
315: ?>
CMS CONTENIDO 4.9.0 API documentation generated by ApiGen 2.8.0