1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22: 23:
24: class cLayoutHandler {
25:
26: 27: 28: 29: 30:
31: protected $_layoutId = 0;
32:
33: 34: 35: 36: 37:
38: protected $_layoutCode = "";
39:
40: 41: 42: 43:
44: protected $_db = NULL;
45:
46: 47: 48: 49: 50:
51: protected $_layoutName = "";
52:
53: 54: 55: 56: 57:
58: protected $_cfg = array();
59:
60: 61: 62: 63: 64:
65: protected $_encoding;
66:
67: 68: 69: 70: 71: 72:
73: protected $_layoutPath = "";
74:
75: 76: 77: 78: 79: 80:
81: protected $_layoutMainPath = "";
82:
83: 84: 85: 86: 87:
88: protected $_fileName = "";
89:
90: 91: 92: 93: 94: 95: 96: 97: 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: 111: 112: 113:
114: public function _getLayoutPath() {
115: return $this->_layoutPath;
116: }
117:
118: 119: 120: 121: 122:
123: public function _getFileName() {
124: return $this->_fileName;
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134: 135:
136: static function existLayout($layoutAlias, $cfgClient, $client) {
137: $file = $cfgClient[$client]['layout']['path'] . $layoutAlias . '/';
138: return cFileHandler::exists($file);
139: }
140:
141: 142: 143: 144: 145: 146: 147: 148:
149: public function init($layoutId, $layoutCode, $cfg, $language) {
150: $this->_layoutCode = $layoutCode;
151: $this->_cfg = $cfg;
152:
153:
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:
171: $this->_makeDirectories();
172: }
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function getLayoutName() {
182: return $this->_layoutName;
183: }
184:
185: 186: 187: 188: 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:
200: $this->_makeDirectories();
201: }
202:
203: 204: 205: 206: 207: 208: 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: 222: 223: 224: 225: 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: 242: 243: 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: 268: 269: 270: 271: 272: 273: 274: 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: 292: 293: 294: 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: 308: 309: 310: 311: 312:
313: public function saveLayoutByUpgrade($layoutCode = '') {
314:
315: if (cFileHandler::exists($this->_layoutPath . $this->_fileName)) {
316: return true;
317: }
318:
319: return $this->_save($layoutCode);
320: }
321:
322: 323: 324: 325: 326:
327: private function _save($layoutCode = '') {
328: if ($layoutCode == '') {
329: $layoutCode = $this->_layoutCode;
330: }
331:
332:
333: if (!is_dir($this->_layoutPath)) {
334: return false;
335: }
336:
337:
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: 348: 349: 350: 351: 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: 371: 372: 373: 374: 375:
376: public function rename($old, $new) {
377:
378: $newPath = $this->_layoutMainPath . $new . "/";
379:
380: $newFileName = $new . ".html";
381:
382: if (rename($this->_layoutPath, $newPath) == FALSE) {
383: return false;
384: }
385:
386:
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: 404: 405: 406: 407:
408: public function getLayoutCode() {
409:
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:
418: $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
419: $content = iconv($fileEncoding, $this->_encoding . "//IGNORE", $content);
420: return $content;
421: }
422: }
423:
424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434:
435: public static function upgrade($adb, $cfg, $clientId) {
436:
437: if (!$adb->query("SELECT * FROM `%s` WHERE idclient='%s'", $cfg['tab']['lay'], $clientId)) {
438: return;
439: }
440:
441: while ($adb->nextRecord()) {
442:
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:
451: $sql = sprintf("UPDATE %s SET code = '' WHERE idclient='%s'", $cfg['tab']['lay'], $clientId);
452: $adb->query($sql);
453: }
454: }
455: