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: static function existLayout($layoutAlias, $cfgClient, $client) {
136: $file = $cfgClient[$client]['layout']['path'] . $layoutAlias . '/';
137: return cFileHandler::exists($file);
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147:
148: public function init($layoutId, $layoutCode, $cfg, $language) {
149: $this->_layoutCode = $layoutCode;
150: $this->_cfg = $cfg;
151:
152:
153: $this->_setEncoding($language);
154:
155: if ((int) $layoutId == 0) {
156: return;
157: }
158:
159: global $cfgClient, $client;
160:
161: $cApiLayout = new cApiLayout($layoutId);
162:
163: if ($cApiLayout->virgin == false && is_array($cfgClient) && (int) $client > 0) {
164: $this->_layoutName = $cApiLayout->get('alias');
165: $this->_layoutMainPath = $cfgClient[$client]['layout']['path'];
166: $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
167: $this->_fileName = $this->_layoutName . ".html";
168:
169:
170: $this->_makeDirectories();
171: }
172: }
173:
174: 175: 176: 177: 178:
179: public function getLayoutName() {
180: return $this->_layoutName;
181: }
182:
183: 184: 185: 186: 187:
188: public function initWithDbObject($dbObject) {
189: global $cfgClient, $client;
190:
191: $this->_layoutCode = $dbObject->f("code");
192: $this->_layoutName = $dbObject->f("alias");
193: $this->_layoutMainPath = $cfgClient[$dbObject->f("idclient")]['layout']['path'];
194: $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
195: $this->_fileName = $this->_layoutName . ".html";
196:
197:
198: $this->_makeDirectories();
199: }
200:
201: 202: 203: 204: 205: 206:
207: private function _makeDirectories() {
208: if ($this->_makeDirectory($this->_layoutMainPath)) {
209: if ($this->_makeDirectory($this->_layoutPath)) {
210: return true;
211: }
212: }
213:
214: return false;
215: }
216:
217: 218: 219: 220: 221: 222:
223: private function _makeDirectory($directory) {
224: if (is_dir($directory)) {
225: $success = true;
226: } else {
227: $success = mkdir($directory);
228: if ($success) {
229: cFileHandler::setDefaultDirPerms($directory);
230: }
231: }
232:
233: return $success;
234: }
235:
236: 237: 238: 239: 240:
241: private function _setEncoding($lang) {
242: if ((int) $lang == 0) {
243: $clientId = cRegistry::getClientId();
244:
245: $clientsLangColl = new cApiClientLanguageCollection();
246: $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
247: sort($clientLanguages);
248:
249: if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
250: $languageId = $clientLanguages[0];
251: }
252: } else {
253: $languageId = $lang;
254: }
255:
256: $cApiLanguage = new cApiLanguage($languageId);
257: $encoding = $cApiLanguage->get('encoding');
258:
259: $this->_encoding = $encoding;
260: }
261:
262: 263: 264: 265: 266: 267: 268:
269: public function isWritable($fileName, $directory) {
270: if (cFileHandler::exists($fileName)) {
271: if (!is_writable($fileName)) {
272: return false;
273: }
274: } else {
275: if (!is_writable($directory)) {
276: return false;
277: }
278: }
279:
280: return true;
281: }
282:
283: 284: 285: 286: 287: 288: 289:
290: public function saveLayout($layoutCode = '') {
291: $fileName = $this->_layoutPath . $this->_fileName;
292:
293: if (!$this->isWritable($fileName, $this->_layoutPath)) {
294: return false;
295: }
296:
297: return $this->_save($layoutCode);
298: }
299:
300: 301: 302: 303: 304: 305: 306:
307: public function saveLayoutByUpgrade($layoutCode = '') {
308:
309: if (cFileHandler::exists($this->_layoutPath . $this->_fileName)) {
310: return true;
311: }
312:
313: return $this->_save($layoutCode);
314: }
315:
316: 317: 318: 319: 320:
321: private function _save($layoutCode = '') {
322: if ($layoutCode == '') {
323: $layoutCode = $this->_layoutCode;
324: }
325:
326:
327: if (!is_dir($this->_layoutPath)) {
328: return false;
329: }
330:
331:
332: $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
333: $layoutCode = iconv($this->_encoding, $fileEncoding, $layoutCode);
334:
335: $save = cFileHandler::write($this->_layoutPath . $this->_fileName, $layoutCode);
336:
337: return (strlen($layoutCode) == 0 && $save == 0) || $save > 0;
338: }
339:
340: 341: 342: 343: 344: 345:
346: public function eraseLayout() {
347: global $area, $frame;
348: $cfg = cRegistry::getConfig();
349: $cfgClient = cRegistry::getClientConfig();
350: $db = cRegistry::getDb();
351: $client = cRegistry::getClientId();
352:
353: $layoutVersion = new cVersionLayout($this->_layoutId, $cfg, $cfgClient, $db, $client, $area, $frame);
354: $success = true;
355: if (count($layoutVersion->getRevisionFiles()) > 0 && !$layoutVersion->deleteFile()) {
356: $success = false;
357: }
358:
359: return $success && cFileHandler::recursiveRmdir($this->_layoutPath);
360: }
361:
362: 363: 364: 365: 366: 367: 368:
369: public function rename($old, $new) {
370:
371: $newPath = $this->_layoutMainPath . $new . "/";
372:
373: $newFileName = $new . ".html";
374:
375: if (rename($this->_layoutPath, $newPath) == FALSE) {
376: return false;
377: }
378:
379:
380: if (!cFileHandler::exists($newPath . $this->_fileName)) {
381: return false;
382: }
383:
384: if (!rename($newPath . $this->_fileName, $newPath . $newFileName)) {
385: return false;
386: }
387:
388: $this->_layoutName = $new;
389: $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . "/";
390: $this->_fileName = $this->_layoutName . ".html";
391:
392: return true;
393: }
394:
395: 396: 397: 398: 399:
400: public function getLayoutCode() {
401:
402: if (!is_readable($this->_layoutPath . $this->_fileName)) {
403: return false;
404: }
405:
406: if (($content = cFileHandler::read($this->_layoutPath . $this->_fileName)) === FALSE) {
407: return false;
408: } else {
409:
410: $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
411: $content = iconv($fileEncoding, $this->_encoding . "//IGNORE", $content);
412: return $content;
413: }
414: }
415:
416: 417: 418: 419: 420: 421: 422: 423: 424:
425: public static function upgrade($adb, $cfg, $clientId) {
426:
427: if (!$adb->query("SELECT * FROM `%s` WHERE idclient='%s'", $cfg['tab']['lay'], $clientId)) {
428: return;
429: }
430:
431: while ($adb->nextRecord()) {
432:
433: $layout = new cLayoutHandler();
434: $layout->initWithDbObject($adb);
435: if ($layout->saveLayoutByUpgrade($adb->f('code')) == false) {
436: throw new cException('Can not save layout.' . print_r($layout, true));
437: }
438: }
439:
440:
441: $sql = sprintf("UPDATE %s SET code = '' WHERE idclient='%s'", $cfg['tab']['lay'], $clientId);
442: $adb->query($sql);
443: }
444: }
445: