1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
15:
16: 17: 18: 19: 20: 21:
22: class cLayoutHandler {
23:
24: 25: 26: 27: 28:
29: protected $_layoutId = 0;
30:
31: 32: 33: 34: 35:
36: protected $_layoutCode = '';
37:
38: 39: 40: 41:
42: protected $_db = NULL;
43:
44: 45: 46: 47: 48:
49: protected $_layoutName = '';
50:
51: 52: 53: 54: 55:
56: protected $_cfg = array();
57:
58: 59: 60: 61: 62:
63: protected $_encoding;
64:
65: 66: 67: 68: 69: 70:
71: protected $_layoutPath = '';
72:
73: 74: 75: 76: 77: 78:
79: protected $_layoutMainPath = '';
80:
81: 82: 83: 84: 85:
86: protected $_fileName = '';
87:
88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100:
101: public function __construct($layoutId = 0, $layoutCode = '', array $cfg = array(), $lang = 0, cDb $db = NULL) {
102: if ($db === NULL) {
103: $db = cRegistry::getDb();
104: }
105:
106: $this->_layoutId = $layoutId;
107: $this->_db = $db;
108: $this->init($layoutId, $layoutCode, $cfg, $lang);
109: }
110:
111: 112: 113: 114: 115:
116: public function _getLayoutPath() {
117: return $this->_layoutPath;
118: }
119:
120: 121: 122: 123: 124:
125: public function _getFileName() {
126: return $this->_fileName;
127: }
128:
129: 130: 131: 132: 133: 134: 135: 136: 137:
138: static function existLayout($layoutAlias, $cfgClient, $client) {
139: $file = $cfgClient[$client]['layout']['path'] . $layoutAlias . '/';
140: return cFileHandler::exists($file);
141: }
142:
143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153:
154: public function init($layoutId, $layoutCode, $cfg, $language) {
155: $this->_layoutCode = $layoutCode;
156: $this->_cfg = $cfg;
157:
158:
159: $this->_setEncoding($language);
160:
161: if ((int) $layoutId == 0) {
162: return;
163: }
164:
165: global $cfgClient, $client;
166:
167: $cApiLayout = new cApiLayout($layoutId);
168:
169: if (true === $cApiLayout->isLoaded() && is_array($cfgClient) && (int) $client > 0) {
170: $this->_layoutName = $cApiLayout->get('alias');
171: $this->_layoutMainPath = $cfgClient[$client]['layout']['path'];
172: $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . '/';
173: $this->_fileName = $this->_layoutName . '.html';
174:
175:
176: $this->_makeDirectories();
177: }
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function getLayoutName() {
187: return $this->_layoutName;
188: }
189:
190: 191: 192: 193: 194: 195: 196: 197:
198: public function initWithDbObject($dbObject) {
199: global $cfgClient;
200:
201: $this->_layoutCode = $dbObject->f('code');
202: $this->_layoutName = $dbObject->f('alias');
203: $this->_layoutMainPath = $cfgClient[$dbObject->f('idclient')]['layout']['path'];
204: $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . '/';
205: $this->_fileName = $this->_layoutName . '.html';
206:
207:
208: $this->_makeDirectories();
209: }
210:
211: 212: 213: 214: 215: 216: 217: 218: 219:
220: private function _makeDirectories() {
221: if ($this->_makeDirectory($this->_layoutMainPath)) {
222: if ($this->_makeDirectory($this->_layoutPath)) {
223: return true;
224: }
225: }
226:
227: return false;
228: }
229:
230: 231: 232: 233: 234: 235: 236: 237: 238: 239:
240: private function _makeDirectory($directory) {
241: if (is_dir($directory)) {
242: $success = true;
243: } else {
244: $success = mkdir($directory);
245: if ($success) {
246: cDirHandler::setDefaultPermissions($directory);
247: }
248: }
249:
250: return $success;
251: }
252:
253: 254: 255: 256: 257: 258: 259:
260: private function _setEncoding($lang) {
261: if ((int) $lang == 0) {
262: $clientId = cRegistry::getClientId();
263:
264: $clientsLangColl = new cApiClientLanguageCollection();
265: $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
266: sort($clientLanguages);
267:
268: if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
269: $languageId = $clientLanguages[0];
270: }
271: } else {
272: $languageId = $lang;
273: }
274:
275: $cApiLanguage = new cApiLanguage($languageId);
276: $encoding = $cApiLanguage->get('encoding');
277:
278: $this->_encoding = $encoding;
279: }
280:
281: 282: 283: 284: 285: 286: 287: 288: 289: 290:
291: public function isWritable($fileName, $directory) {
292: if (cFileHandler::exists($fileName)) {
293: if (!is_writable($fileName)) {
294: return false;
295: }
296: } else {
297: if (!is_writable($directory)) {
298: return false;
299: }
300: }
301:
302: return true;
303: }
304:
305: 306: 307: 308: 309: 310: 311: 312: 313:
314: public function saveLayout($layoutCode = '') {
315: $fileName = $this->_layoutPath . $this->_fileName;
316:
317: if (!$this->isWritable($fileName, $this->_layoutPath)) {
318: return false;
319: }
320:
321: return $this->_save($layoutCode);
322: }
323:
324: 325: 326: 327: 328: 329: 330: 331: 332: 333:
334: public function saveLayoutByUpgrade($layoutCode = '') {
335:
336: if (cFileHandler::exists($this->_layoutPath . $this->_fileName)) {
337: return true;
338: }
339:
340: return $this->_save($layoutCode);
341: }
342:
343: 344: 345: 346: 347: 348: 349: 350:
351: private function _save($layoutCode = '') {
352: if ($layoutCode == '') {
353: $layoutCode = $this->_layoutCode;
354: }
355:
356:
357: if (!is_dir($this->_layoutPath)) {
358: return false;
359: }
360:
361:
362: $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
363: $layoutCode = cString::recodeString($layoutCode, $this->_encoding, $fileEncoding);
364:
365: $save = cFileHandler::write($this->_layoutPath . $this->_fileName, $layoutCode);
366:
367: return (cString::getStringLength($layoutCode) == 0 && $save == 0) || $save > 0;
368: }
369:
370: 371: 372: 373: 374: 375: 376: 377: 378:
379: public function eraseLayout() {
380: global $area, $frame;
381: $cfg = cRegistry::getConfig();
382: $cfgClient = cRegistry::getClientConfig();
383: $db = cRegistry::getDb();
384: $client = cRegistry::getClientId();
385:
386: $layoutVersion = new cVersionLayout($this->_layoutId, $cfg, $cfgClient, $db, $client, $area, $frame);
387: $success = true;
388: if (count($layoutVersion->getRevisionFiles()) > 0 && !$layoutVersion->deleteFile()) {
389: $success = false;
390: }
391:
392: return $success && cDirHandler::recursiveRmdir($this->_layoutPath);
393: }
394:
395: 396: 397: 398: 399: 400: 401:
402: public function rename($old, $new) {
403:
404: $newPath = $this->_layoutMainPath . $new . '/';
405:
406: $newFileName = $new . '.html';
407:
408: if (rename($this->_layoutPath, $newPath) == FALSE) {
409: return false;
410: }
411:
412:
413: if (!cFileHandler::exists($newPath . $this->_fileName)) {
414: return false;
415: }
416:
417: if (!rename($newPath . $this->_fileName, $newPath . $newFileName)) {
418: return false;
419: }
420:
421: $this->_layoutName = $new;
422: $this->_layoutPath = $this->_layoutMainPath . $this->_layoutName . '/';
423: $this->_fileName = $this->_layoutName . '.html';
424:
425: return true;
426: }
427:
428: 429: 430: 431: 432: 433: 434: 435:
436: public function getLayoutCode() {
437:
438: if (!is_readable($this->_layoutPath . $this->_fileName)) {
439: return false;
440: }
441:
442: if (($content = cFileHandler::read($this->_layoutPath . $this->_fileName)) === FALSE) {
443: return false;
444: } else {
445:
446: $fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
447: $content = iconv($fileEncoding, $this->_encoding . '//IGNORE', $content);
448: return $content;
449: }
450: }
451:
452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463:
464: public static function upgrade($adb, $cfg, $clientId) {
465:
466: if (!$adb->query("SELECT * FROM `%s` WHERE idclient='%s'", $cfg['tab']['lay'], $clientId)) {
467: return;
468: }
469:
470: while ($adb->nextRecord()) {
471:
472: $layout = new cLayoutHandler();
473: $layout->initWithDbObject($adb);
474: if ($layout->saveLayoutByUpgrade($adb->f('code')) == false) {
475: throw new cException('Can not save layout.' . print_r($layout, true));
476: }
477: }
478:
479:
480: $sql = sprintf("UPDATE %s SET code = '' WHERE idclient='%s'", $cfg['tab']['lay'], $clientId);
481: $adb->query($sql);
482: }
483: }
484: