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