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