PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/layout/layout.admin.controller.php

https://github.com/prologos/xe-core
PHP | 1003 lines | 678 code | 145 blank | 180 comment | 104 complexity | caeb4c4a5538f7ceea2d015798bc8b39 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* Copyright (C) NAVER <http://www.navercorp.com> */
  3. /**
  4. * @class layoutAdminController
  5. * @author NAVER (developers@xpressengine.com)
  6. * admin controller class of the layout module
  7. */
  8. class layoutAdminController extends layout
  9. {
  10. /**
  11. * Initialization
  12. * @return void
  13. */
  14. function init()
  15. {
  16. }
  17. /**
  18. * Create a new layout
  19. * Insert a title into "layouts" table in order to create a layout
  20. * @deprecated
  21. * @return void|Object (void : success, Object : fail)
  22. */
  23. function procLayoutAdminInsert()
  24. {
  25. if(Context::get('layout') == 'faceoff') return $this->stop('not supported');
  26. // Get information to create a layout
  27. $site_module_info = Context::get('site_module_info');
  28. $args->site_srl = (int)$site_module_info->site_srl;
  29. $args->layout_srl = getNextSequence();
  30. $args->layout = Context::get('layout');
  31. $args->title = Context::get('title');
  32. $args->layout_type = Context::get('_layout_type');
  33. if(!$args->layout_type) $args->layout_type = "P";
  34. // Insert into the DB
  35. $output = $this->insertLayout($args);
  36. if(!$output->toBool()) return $output;
  37. // initiate if it is faceoff layout
  38. $this->initLayout($args->layout_srl, $args->layout);
  39. // update layout info
  40. Context::set('layout_srl', $args->layout_srl);
  41. $output = $this->procLayoutAdminUpdate();
  42. if (!$output->toBool()) return $output;
  43. return $this->setRedirectUrl(Context::get('success_return_url'), $output);
  44. }
  45. /**
  46. * Insert layout information into the DB
  47. * @param object $args layout information
  48. * @return Object
  49. */
  50. function insertLayout($args)
  51. {
  52. $output = executeQuery("layout.insertLayout", $args);
  53. return $output;
  54. }
  55. /**
  56. * Initiate if it is faceoff layout
  57. * @param int $layout_srl
  58. * @param string $layout_name
  59. * @return void
  60. */
  61. function initLayout($layout_srl, $layout_name)
  62. {
  63. $oLayoutModel = getModel('layout');
  64. // Import a sample layout if it is faceoff
  65. if($oLayoutModel->useDefaultLayout($layout_name))
  66. {
  67. $this->importLayout($layout_srl, $this->module_path.'tpl/faceOff_sample.tar');
  68. // Remove a directory
  69. }
  70. else
  71. {
  72. FileHandler::removeDir($oLayoutModel->getUserLayoutPath($layout_srl));
  73. }
  74. }
  75. /**
  76. * Update layout information
  77. * Apply a title of the new layout and extra vars
  78. * @return Object
  79. */
  80. function procLayoutAdminUpdate()
  81. {
  82. // Consider the rest of items as extra vars, except module, act, layout_srl, layout, and title .. Some gurida ..
  83. $extra_vars = Context::getRequestVars();
  84. unset($extra_vars->module);
  85. unset($extra_vars->act);
  86. unset($extra_vars->layout_srl);
  87. unset($extra_vars->layout);
  88. unset($extra_vars->title);
  89. unset($extra_vars->apply_layout);
  90. unset($extra_vars->apply_mobile_view);
  91. $is_sitemap = $extra_vars->is_sitemap;
  92. unset($extra_vars->is_sitemap);
  93. $args = Context::gets('layout_srl','title');
  94. // Get layout information
  95. $oLayoutModel = getModel('layout');
  96. $oMenuAdminModel = getAdminModel('menu');
  97. $layout_info = $oLayoutModel->getLayout($args->layout_srl);
  98. if($layout_info->menu)
  99. {
  100. $menus = get_object_vars($layout_info->menu);
  101. }
  102. if(count($menus))
  103. {
  104. foreach($menus as $menu_id => $val)
  105. {
  106. $menu_srl = Context::get($menu_id);
  107. if(!$menu_srl) continue;
  108. // if menu is -1, get default menu in site
  109. if($menu_srl == -1)
  110. {
  111. $oModuleModel = getModel('module');
  112. $start_module = $oModuleModel->getSiteInfo(0, $columnList);
  113. $tmpArgs->url = $start_module->mid;
  114. $tmpArgs->site_srl = 0;
  115. $output = executeQuery('menu.getMenuItemByUrl', $tmpArgs);
  116. if(!$output->toBool())
  117. {
  118. return new Object(-1, 'fail_to_update');
  119. }
  120. $menu_srl = $output->data->menu_srl;
  121. }
  122. $output = $oMenuAdminModel->getMenu($menu_srl);
  123. $menu_srl_list[] = $menu_srl;
  124. $menu_name_list[$menu_srl] = $output->title;
  125. $apply_layout = Context::get('apply_layout');
  126. $apply_mobile_view = Context::get('apply_mobile_view');
  127. if($apply_layout=='Y' || $apply_mobile_view=='Y')
  128. {
  129. $menu_args = new stdClass();
  130. $menu_args->menu_srl = $menu_srl;
  131. $menu_args->site_srl = $layout_info->site_srl;
  132. $output = executeQueryArray('layout.getLayoutModules', $menu_args);
  133. if($output->data)
  134. {
  135. $modules = array();
  136. for($i=0;$i<count($output->data);$i++)
  137. {
  138. $modules[] = $output->data[$i]->module_srl;
  139. }
  140. if(count($modules))
  141. {
  142. $update_args = new stdClass();
  143. $update_args->module_srls = implode(',',$modules);
  144. if($apply_layout == "Y")
  145. {
  146. $update_args->layout_srl = $args->layout_srl;
  147. }
  148. if($layout_info->layout_type == "M")
  149. {
  150. if(Context::get('apply_mobile_view') == "Y")
  151. {
  152. $update_args->use_mobile = "Y";
  153. }
  154. $output = executeQuery('layout.updateModuleMLayout', $update_args);
  155. }
  156. else
  157. {
  158. $output = executeQuery('layout.updateModuleLayout', $update_args);
  159. }
  160. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  161. if($oCacheHandler->isSupport())
  162. {
  163. $oCacheHandler->invalidateGroupKey('site_and_module');
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. $tmpDir = sprintf('./files/attach/images/%s/tmp', $args->layout_srl);
  171. // Separately handle if a type of extra_vars is an image
  172. if($layout_info->extra_var)
  173. {
  174. foreach($layout_info->extra_var as $name => $vars)
  175. {
  176. if($vars->type!='image') continue;
  177. $fileName = $extra_vars->{$name};
  178. if($vars->value == $fileName)
  179. {
  180. continue;
  181. }
  182. FileHandler::removeFile($vars->value);
  183. if(!$fileName)
  184. {
  185. continue;
  186. }
  187. $pathInfo = pathinfo($fileName);
  188. $tmpFileName = sprintf('%s/tmp/%s', $pathInfo['dirname'], $pathInfo['basename']);
  189. if(!FileHandler::moveFile($tmpFileName, $fileName))
  190. {
  191. unset($extra_vars->{$name});
  192. }
  193. }
  194. }
  195. // Save header script into "config" of layout module
  196. $oModuleModel = getModel('module');
  197. $oModuleController = getController('module');
  198. $layout_config = new stdClass();
  199. $layout_config->header_script = Context::get('header_script');
  200. $oModuleController->insertModulePartConfig('layout',$args->layout_srl,$layout_config);
  201. // Save a title of the menu
  202. $extra_vars->menu_name_list = $menu_name_list;
  203. // Variable setting for DB insert
  204. $args->extra_vars = serialize($extra_vars);
  205. $output = $this->updateLayout($args);
  206. if(!$output->toBool())
  207. {
  208. return $output;
  209. }
  210. FileHandler::removeDir($tmpDir);
  211. if(!$is_sitemap)
  212. {
  213. return $this->setRedirectUrl(Context::get('error_return_url'), $output);
  214. }
  215. else
  216. {
  217. $context = Context::getInstance();
  218. $context->setRequestMethod('JSON');
  219. $this->setMessage('success');
  220. }
  221. }
  222. /**
  223. * Update layout information into the DB
  224. * @param object $args
  225. * @return Object
  226. */
  227. function updateLayout($args) {
  228. $output = executeQuery('layout.updateLayout', $args);
  229. if($output->toBool())
  230. {
  231. $oLayoutModel = getModel('layout');
  232. $cache_file = $oLayoutModel->getUserLayoutCache($args->layout_srl, Context::getLangType());
  233. FileHandler::removeFile($cache_file);
  234. }
  235. return $output;
  236. }
  237. /**
  238. * Delete Layout
  239. * Delete xml cache file too when deleting a layout
  240. * @return Object
  241. */
  242. function procLayoutAdminDelete()
  243. {
  244. $layout_srl = Context::get('layout_srl');
  245. $this->setRedirectUrl(Context::get('error_return_url'));
  246. return $this->deleteLayout($layout_srl);
  247. }
  248. /**
  249. * Delete layout xml cache file
  250. * @param int $layout_srl
  251. * @return Object
  252. */
  253. function deleteLayout($layout_srl, $force = FALSE)
  254. {
  255. $oLayoutModel = getModel('layout');
  256. if(!$force)
  257. {
  258. $layoutInfo = $oLayoutModel->getLayout($layout_srl);
  259. if($layoutInfo)
  260. {
  261. $layoutList = $oLayoutModel->getLayoutInstanceList($layoutInfo->site_srl, $layoutInfo->layout_type, $layoutInfo->layout, array('layout_srl', 'layout'));
  262. if(count($layoutList) <= 1)
  263. {
  264. // uninstall package
  265. $path = $layoutInfo->path;
  266. $oAutoinstallModel = getModel('autoinstall');
  267. $packageSrl = $oAutoinstallModel->getPackageSrlByPath($path);
  268. $oAutoinstallAdminController = getAdminController('autoinstall');
  269. if($packageSrl)
  270. {
  271. $output = $oAutoinstallAdminController->uninstallPackageByPackageSrl($packageSrl);
  272. }
  273. else
  274. {
  275. $output = $oAutoinstallAdminController->uninstallPackageByPath($path);
  276. }
  277. if(!$output->toBool())
  278. {
  279. return new Object(-1, $output->message);
  280. }
  281. }
  282. }
  283. }
  284. $path = $oLayoutModel->getUserLayoutPath($layout_srl);
  285. FileHandler::removeDir($path);
  286. $layout_file = $oLayoutModel->getUserLayoutHtml($layout_srl);
  287. FileHandler::removeFile($layout_file);
  288. // Delete Layout
  289. $args = new stdClass();
  290. $args->layout_srl = $layout_srl;
  291. $output = executeQuery("layout.deleteLayout", $args);
  292. if(!$output->toBool()) return $output;
  293. return new Object(0,'success_deleted');
  294. }
  295. /**
  296. * Adding Layout Code
  297. * @return void|Object (void : success, Object : fail)
  298. */
  299. function procLayoutAdminCodeUpdate()
  300. {
  301. $mode = Context::get('mode');
  302. if ($mode == 'reset')
  303. {
  304. return $this->procLayoutAdminCodeReset();
  305. }
  306. $layout_srl = Context::get('layout_srl');
  307. $code = Context::get('code');
  308. $code_css = Context::get('code_css');
  309. $is_post = ($_SERVER['REQUEST_METHOD'] == 'POST');
  310. if(!$layout_srl || !$code || !$is_post)
  311. {
  312. return new Object(-1, 'msg_invalid_request');
  313. }
  314. $oLayoutModel = getModel('layout');
  315. $layout_file = $oLayoutModel->getUserLayoutHtml($layout_srl);
  316. FileHandler::writeFile($layout_file, $code);
  317. $layout_css_file = $oLayoutModel->getUserLayoutCss($layout_srl);
  318. FileHandler::writeFile($layout_css_file, $code_css);
  319. $this->setRedirectUrl(Context::get('error_return_url'));
  320. $this->setMessage('success_updated');
  321. }
  322. /**
  323. * Reset layout code
  324. * @return void|Object (void : success, Object : fail)
  325. */
  326. function procLayoutAdminCodeReset()
  327. {
  328. $layout_srl = Context::get('layout_srl');
  329. if(!$layout_srl) return new Object(-1, 'msg_invalid_request');
  330. // delete user layout file
  331. $oLayoutModel = getModel('layout');
  332. $layout_file = $oLayoutModel->getUserLayoutHtml($layout_srl);
  333. FileHandler::removeFile($layout_file);
  334. $info = $oLayoutModel->getLayout($layout_srl);
  335. // if face off delete, tmp file
  336. if($oLayoutModel->useDefaultLayout($info->layout))
  337. {
  338. $this->deleteUserLayoutTempFile($layout_srl);
  339. $faceoff_css = $oLayoutModel->getUserLayoutFaceOffCss($layout_srl);
  340. FileHandler::removeFile($faceoff_css);
  341. }
  342. $this->initLayout($layout_srl, $info->layout);
  343. $this->setMessage('success_reset');
  344. $this->setRedirectUrl(Context::get('error_return_url'));
  345. }
  346. /**
  347. * Layout setting page -> Upload an image
  348. * @return void
  349. */
  350. function procLayoutAdminUserImageUpload()
  351. {
  352. if(!Context::isUploaded()) exit();
  353. $image = Context::get('user_layout_image');
  354. $layout_srl = Context::get('layout_srl');
  355. if(!is_uploaded_file($image['tmp_name'])) exit();
  356. $this->insertUserLayoutImage($layout_srl, $image);
  357. $this->setTemplatePath($this->module_path.'tpl');
  358. $this->setTemplateFile("top_refresh.html");
  359. $this->setRedirectUrl(Context::get('error_return_url'));
  360. }
  361. /**
  362. * insert image into user layout
  363. * @param int $layout_srl
  364. * @param object $source file data
  365. * @return boolean (true : success, false : fail)
  366. */
  367. function insertUserLayoutImage($layout_srl,$source)
  368. {
  369. $oLayoutModel = getModel('layout');
  370. $path = $oLayoutModel->getUserLayoutImagePath($layout_srl);
  371. if(!is_dir($path)) FileHandler::makeDir($path);
  372. $filename = strtolower($source['name']);
  373. if($filename != urlencode($filename))
  374. {
  375. $ext = substr(strrchr($filename,'.'),1);
  376. $filename = sprintf('%s.%s', md5($filename), $ext);
  377. }
  378. // Check uploaded file
  379. if(!checkUploadedFile($source['tmp_name'])) return false;
  380. if(file_exists($path .'/'. $filename)) @unlink($path . $filename);
  381. if(!move_uploaded_file($source['tmp_name'], $path . $filename )) return false;
  382. return true;
  383. }
  384. /**
  385. * Layout setting page -> Delete an image
  386. * @return void
  387. */
  388. function procLayoutAdminUserImageDelete()
  389. {
  390. $filename = Context::get('filename');
  391. $layout_srl = Context::get('layout_srl');
  392. $this->removeUserLayoutImage($layout_srl,$filename);
  393. $this->setMessage('success_deleted');
  394. $this->setRedirectUrl(Context::get('error_return_url'));
  395. }
  396. /**
  397. * delete image into user layout
  398. * @param int $layout_srl
  399. * @param string $filename
  400. * @return void
  401. */
  402. function removeUserLayoutImage($layout_srl,$filename)
  403. {
  404. $oLayoutModel = getModel('layout');
  405. $path = $oLayoutModel->getUserLayoutImagePath($layout_srl);
  406. @unlink($path . $filename);
  407. }
  408. // deprecated
  409. /**
  410. * Save layout configuration
  411. * save in "ini" format for faceoff
  412. * @deprecated
  413. * @return void|Object (void : success, Object : fail)
  414. */
  415. function procLayoutAdminUserValueInsert()
  416. {
  417. $oModuleModel = getModel('module');
  418. $mid = Context::get('mid');
  419. if(!$mid) return new Object(-1, 'msg_invalid_request');
  420. $site_module_info = Context::get('site_module_info');
  421. $columnList = array('layout_srl');
  422. $module_info = $oModuleModel->getModuleInfoByMid($mid, $site_module_info->site_srl, $columnList);
  423. $layout_srl = $module_info->layout_srl;
  424. if(!$layout_srl) return new Object(-1, 'msg_invalid_request');
  425. $oLayoutModel = getModel('layout');
  426. // save tmp?
  427. $temp = Context::get('saveTemp');
  428. if($temp =='Y')
  429. {
  430. $oLayoutModel->setUseUserLayoutTemp();
  431. }
  432. else
  433. {
  434. // delete temp files
  435. $this->deleteUserLayoutTempFile($layout_srl);
  436. }
  437. $this->add('saveTemp',$temp);
  438. // write user layout
  439. $extension_obj = Context::gets('e1','e2','neck','knee');
  440. $file = $oLayoutModel->getUserLayoutHtml($layout_srl);
  441. $content = FileHandler::readFile($file);
  442. $content = $this->addExtension($layout_srl,$extension_obj,$content);
  443. FileHandler::writeFile($file,$content);
  444. // write faceoff.css
  445. $css = stripslashes(Context::get('css'));
  446. $css_file = $oLayoutModel->getUserLayoutFaceOffCss($layout_srl);
  447. FileHandler::writeFile($css_file,$css);
  448. // write ini
  449. $obj = Context::gets('type','align','column');
  450. $obj = (array)$obj;
  451. $src = $oLayoutModel->getUserLayoutIniConfig($layout_srl);
  452. foreach($obj as $key => $val) $src[$key] = $val;
  453. $this->insertUserLayoutValue($layout_srl,$src);
  454. }
  455. /**
  456. * Layout setting, save "ini"
  457. * @param int $layout_srl
  458. * @param object $arr layout ini
  459. * @return void
  460. */
  461. function insertUserLayoutValue($layout_srl,$arr)
  462. {
  463. $oLayoutModel = getModel('layout');
  464. $file = $oLayoutModel->getUserLayoutIni($layout_srl);
  465. FileHandler::writeIniFile($file, $arr);
  466. }
  467. /**
  468. * Add the widget code for faceoff into user layout file
  469. * @param int $layout_srl
  470. * @param object $arg
  471. * @param string $content
  472. * @return string
  473. */
  474. function addExtension($layout_srl,$arg,$content)
  475. {
  476. $oLayoutModel = getModel('layout');
  477. $reg = '/(<\!\-\- start\-e1 \-\->)(.*)(<\!\-\- end\-e1 \-\->)/i';
  478. $extension_content = '\1' .stripslashes($arg->e1) . '\3';
  479. $content = preg_replace($reg,$extension_content,$content);
  480. $reg = '/(<\!\-\- start\-e2 \-\->)(.*)(<\!\-\- end\-e2 \-\->)/i';
  481. $extension_content = '\1' .stripslashes($arg->e2) . '\3';
  482. $content = preg_replace($reg,$extension_content,$content);
  483. $reg = '/(<\!\-\- start\-neck \-\->)(.*)(<\!\-\- end\-neck \-\->)/i';
  484. $extension_content = '\1' .stripslashes($arg->neck) . '\3';
  485. $content = preg_replace($reg,$extension_content,$content);
  486. $reg = '/(<\!\-\- start\-knee \-\->)(.*)(<\!\-\- end\-knee \-\->)/i';
  487. $extension_content = '\1' .stripslashes($arg->knee) . '\3';
  488. $content = preg_replace($reg,$extension_content,$content);
  489. return $content;
  490. }
  491. /**
  492. * Delete temp files for faceoff
  493. * @param int $layout_srl
  494. * @return void
  495. */
  496. function deleteUserLayoutTempFile($layout_srl)
  497. {
  498. $oLayoutModel = getModel('layout');
  499. $file_list = $oLayoutModel->getUserLayoutTempFileList($layout_srl);
  500. foreach($file_list as $key => $file)
  501. {
  502. FileHandler::removeFile($file);
  503. }
  504. }
  505. /**
  506. * export user layout
  507. * @return void
  508. */
  509. function procLayoutAdminUserLayoutExport()
  510. {
  511. $layout_srl = Context::get('layout_srl');
  512. if(!$layout_srl) return new Object('-1','msg_invalid_request');
  513. require_once(_XE_PATH_.'libs/tar.class.php');
  514. $oLayoutModel = getModel('layout');
  515. // Copy files to temp path
  516. $file_path = $oLayoutModel->getUserLayoutPath($layout_srl);
  517. $target_path = $oLayoutModel->getUserLayoutPath(0);
  518. FileHandler::copyDir($file_path, $target_path);
  519. // replace path and ini config
  520. $ini_config = $oLayoutModel->getUserLayoutIniConfig(0);
  521. $file_list = $oLayoutModel->getUserLayoutFileList($layout_srl);
  522. unset($file_list[2]);
  523. foreach($file_list as $file)
  524. {
  525. if(strncasecmp('images', $file, 6) === 0) continue;
  526. // replace path
  527. $file = $target_path . $file;
  528. $content = FileHandler::readFile($file);
  529. $pattern = '/(http:\/\/[^ ]+)?(\.\/)?' . str_replace('/', '\/', (str_replace('./', '', $file_path))) . '/';
  530. if(basename($file) == 'faceoff.css' || basename($file) == 'layout.css')
  531. $content = preg_replace($pattern, '../', $content);
  532. else
  533. $content = preg_replace($pattern, './', $content);
  534. // replace ini config
  535. foreach($ini_config as $key => $value)
  536. {
  537. $content = str_replace('{$layout_info->faceoff_ini_config[\'' . $key . '\']}', $value, $content);
  538. }
  539. FileHandler::writeFile($file, $content);
  540. }
  541. // make info.xml
  542. $info_file = $target_path . 'conf/info.xml';
  543. FileHandler::copyFile('./modules/layout/faceoff/conf/info.xml', $info_file);
  544. $content = FileHandler::readFile($info_file);
  545. $content = str_replace('type="faceoff"', '', $content);
  546. FileHandler::writeFile($info_file, $content);
  547. $file_list[] = 'conf/info.xml';
  548. // make css file
  549. $css_file = $target_path . 'css/layout.css';
  550. FileHandler::copyFile('./modules/layout/faceoff/css/layout.css', $css_file);
  551. $content = FileHandler::readFile('./modules/layout/tpl/css/widget.css');
  552. FileHandler::writeFile($css_file, "\n" . $content, 'a');
  553. $content = FileHandler::readFile($target_path . 'faceoff.css');
  554. FileHandler::writeFile($css_file, "\n" . $content, 'a');
  555. $content = FileHandler::readFile($target_path . 'layout.css');
  556. FileHandler::writeFile($css_file, "\n" . $content, 'a');
  557. // css load
  558. $content = FileHandler::readFile($target_path . 'layout.html');
  559. $content = "<load target=\"css/layout.css\" />\n" . $content;
  560. FileHandler::writeFile($target_path . 'layout.html', $content);
  561. unset($file_list[3]);
  562. unset($file_list[1]);
  563. $file_list[] = 'css/layout.css';
  564. // Compress the files
  565. $tar = new tar();
  566. $user_layout_path = FileHandler::getRealPath($oLayoutModel->getUserLayoutPath(0));
  567. chdir($user_layout_path);
  568. foreach($file_list as $key => $file) $tar->addFile($file);
  569. $stream = $tar->toTarStream();
  570. $filename = 'faceoff_' . date('YmdHis') . '.tar';
  571. header("Cache-Control: ");
  572. header("Pragma: ");
  573. header("Content-Type: application/x-compressed");
  574. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  575. // header("Content-Length: " .strlen($stream)); ?? why??
  576. header('Content-Disposition: attachment; filename="'. $filename .'"');
  577. header("Content-Transfer-Encoding: binary\n");
  578. echo $stream;
  579. // Close Context and then exit
  580. Context::close();
  581. // delete temp path
  582. FileHandler::removeDir($target_path);
  583. exit();
  584. }
  585. /**
  586. * faceoff import
  587. * @deprecated
  588. * @return void
  589. */
  590. function procLayoutAdminUserLayoutImport()
  591. {
  592. return $this->stop('not supported');
  593. // check upload
  594. if(!Context::isUploaded()) exit();
  595. $file = Context::get('file');
  596. if(!is_uploaded_file($file['tmp_name']) || !checkUploadedFile($file['tmp_name'])) exit();
  597. if(substr_compare($file['name'], '.tar', -4) !== 0) exit();
  598. $layout_srl = Context::get('layout_srl');
  599. if(!$layout_srl) exit();
  600. $oLayoutModel = getModel('layout');
  601. $user_layout_path = FileHandler::getRealPath($oLayoutModel->getUserLayoutPath($layout_srl));
  602. if(!move_uploaded_file($file['tmp_name'], $user_layout_path . 'faceoff.tar')) exit();
  603. $this->importLayout($layout_srl, $user_layout_path.'faceoff.tar');
  604. $this->setRedirectUrl(Context::get('error_return_url'));
  605. }
  606. /**
  607. * layout copy
  608. * @return void
  609. */
  610. function procLayoutAdminCopyLayout()
  611. {
  612. $sourceArgs = Context::getRequestVars();
  613. if($sourceArgs->layout == 'faceoff')
  614. {
  615. return $this->stop('not supported');
  616. }
  617. if(!$sourceArgs->layout_srl)
  618. {
  619. return $this->stop('msg_empty_origin_layout');
  620. }
  621. $oLayoutModel = getModel('layout');
  622. $layout = $oLayoutModel->getLayout($sourceArgs->layout_srl);
  623. if(!$sourceArgs->title)
  624. {
  625. $sourceArgs->title = array($layout->title.'_'.$this->_makeRandomMid());
  626. }
  627. if(!is_array($sourceArgs->title) || count($sourceArgs->title) == 0)
  628. {
  629. return $this->stop('msg_empty_target_layout');
  630. }
  631. $output = $oLayoutModel->getLayoutRawData($sourceArgs->layout_srl, array('extra_vars'));
  632. $args = new stdClass();
  633. $args->extra_vars = $output->extra_vars;
  634. $extra_vars = unserialize($args->extra_vars);
  635. if($layout->extra_var_count) {
  636. $reg = "/^.\/files\/attach\/images\/([0-9]+)\/(.*)/";
  637. if($extra_vars) foreach($extra_vars as $key => $val) {
  638. if($layout->extra_var->{$key}->type == 'image') {
  639. if(!preg_match($reg,$val,$matches)) continue;
  640. $image_list[$key]->filename = $matches[2];
  641. $image_list[$key]->old_file = $val;
  642. }
  643. }
  644. }
  645. $oModuleController = getController('module');
  646. $layout_config = new stdClass();
  647. $layout_config->header_script = $extra_vars->header_script;
  648. // Get information to create a layout
  649. $args->site_srl = (int)$layout->site_srl;
  650. $args->layout = $layout->layout;
  651. $args->layout_type = $layout->layout_type;
  652. if(!$args->layout_type) $args->layout_type = "P";
  653. $oDB = &DB::getInstance();
  654. $oDB->begin();
  655. if(is_array($sourceArgs->title))
  656. {
  657. foreach($sourceArgs->title AS $key=>$value)
  658. {
  659. if(!trim($value))
  660. {
  661. continue;
  662. }
  663. $args->layout_srl = getNextSequence();
  664. $args->title = $value;
  665. if(is_array($image_list)) {
  666. foreach($image_list as $key=>$val) {
  667. $new_file = sprintf("./files/attach/images/%s/%s", $args->layout_srl,$val->filename);
  668. FileHandler::copyFile($val->old_file, $new_file);
  669. $extra_vars->{$key} = $new_file;
  670. }
  671. $args->extra_vars = serialize($extra_vars);
  672. }
  673. // for header script
  674. $oModuleController->insertModulePartConfig('layout', $args->layout_srl, $layout_config);
  675. // Insert into the DB
  676. $output = $this->insertLayout($args);
  677. if(!$output->toBool())
  678. {
  679. $oDB->rollback();
  680. return $output;
  681. }
  682. // initiate if it is faceoff layout
  683. $this->initLayout($args->layout_srl, $args->layout);
  684. // update layout info
  685. $output = $this->updateLayout($args);
  686. if (!$output->toBool())
  687. {
  688. $oDB->rollback();
  689. return $output;
  690. }
  691. $this->_copyLayoutFile($layout->layout_srl, $args->layout_srl);
  692. }
  693. }
  694. $oDB->commit();
  695. $this->setMessage('success_registed');
  696. if(!in_array(Context::getRequestMethod(),array('XMLRPC','JSON'))) {
  697. global $lang;
  698. htmlHeader();
  699. alertScript($lang->success_registed);
  700. reload(true);
  701. closePopupScript();
  702. htmlFooter();
  703. Context::close();
  704. exit;
  705. }
  706. }
  707. private function _makeRandomMid()
  708. {
  709. $time = $_SERVER['REQUEST_TIME'];
  710. $randomString = "";
  711. for($i=0;$i<4;$i++)
  712. {
  713. $doc = rand()%26+65;
  714. $randomString .= chr($doc);
  715. }
  716. return $randomString.substr($time, -4);
  717. }
  718. /**
  719. * Layout file copy
  720. * @param $sourceLayoutSrl origin layout number
  721. * @param $targetLayoutSrl origin layout number
  722. * @return void
  723. */
  724. function _copyLayoutFile($sourceLayoutSrl, $targetLayoutSrl)
  725. {
  726. $oLayoutModel = getModel('layout');
  727. $sourceLayoutPath = FileHandler::getRealPath($oLayoutModel->getUserLayoutPath($sourceLayoutSrl));
  728. $targetLayoutPath = FileHandler::getRealPath($oLayoutModel->getUserLayoutPath($targetLayoutSrl));
  729. $sourceImagePath = $oLayoutModel->getUserLayoutImagePath($sourceLayoutSrl);
  730. $targetImagePath = $oLayoutModel->getUserLayoutImagePath($targetLayoutSrl);
  731. FileHandler::makeDir($targetImagePath);
  732. $sourceFileList = $oLayoutModel->getUserLayoutFileList($sourceLayoutSrl);
  733. foreach($sourceFileList as $key => $file)
  734. {
  735. if(is_readable($sourceLayoutPath.$file))
  736. {
  737. FileHandler::copyFile($sourceLayoutPath.$file, $targetLayoutPath.$file);
  738. if($file == 'layout.html' || $file == 'layout.css')
  739. {
  740. $this->_changeFilepathInSource($targetLayoutPath.$file, $sourceImagePath, $targetImagePath);
  741. }
  742. }
  743. }
  744. }
  745. /**
  746. * Change resource file path in Layout file
  747. * @param string $file
  748. * @return void
  749. */
  750. function _changeFilepathInSource($file, $source, $target)
  751. {
  752. $content = FileHandler::readFile($file);
  753. $content = str_replace($source, $target, $content);
  754. FileHandler::writeFile($file, $content);
  755. }
  756. /**
  757. * import layout
  758. * @param int $layout_srl
  759. * @param string $source_file path of imported file
  760. * @return void
  761. */
  762. function importLayout($layout_srl, $source_file)
  763. {
  764. $oLayoutModel = getModel('layout');
  765. $user_layout_path = FileHandler::getRealPath($oLayoutModel->getUserLayoutPath($layout_srl));
  766. $file_list = $oLayoutModel->getUserLayoutFileList($layout_srl);
  767. foreach($file_list as $key => $file)
  768. {
  769. FileHandler::removeFile($user_layout_path . $file);
  770. }
  771. require_once(_XE_PATH_.'libs/tar.class.php');
  772. $image_path = $oLayoutModel->getUserLayoutImagePath($layout_srl);
  773. FileHandler::makeDir($image_path);
  774. $tar = new tar();
  775. $tar->openTAR($source_file);
  776. // If layout.ini file does not exist
  777. if(!$tar->getFile('layout.ini')) return;
  778. $replace_path = getNumberingPath($layout_srl,3);
  779. foreach($tar->files as $key => $info)
  780. {
  781. FileHandler::writeFile($user_layout_path . $info['name'],str_replace('__LAYOUT_PATH__',$replace_path,$info['file']));
  782. }
  783. // Remove uploaded file
  784. FileHandler::removeFile($source_file);
  785. }
  786. /**
  787. * Upload config image
  788. */
  789. function procLayoutAdminConfigImageUpload()
  790. {
  791. $layoutSrl = Context::get('layout_srl');
  792. $name = Context::get('name');
  793. $img = Context::get('img');
  794. $this->setTemplatePath($this->module_path.'tpl');
  795. $this->setTemplateFile("after_upload_config_image.html");
  796. if(!$img['tmp_name'] || !is_uploaded_file($img['tmp_name']) || !checkUploadedFile($img['tmp_name']))
  797. {
  798. Context::set('msg', Context::getLang('upload failed'));
  799. return;
  800. }
  801. if(!preg_match('/\.(jpg|jpeg|gif|png|swf)$/i', $img['name']))
  802. {
  803. Context::set('msg', Context::getLang('msg_layout_image_target'));
  804. return;
  805. }
  806. $path = sprintf('./files/attach/images/%s/', $layoutSrl);
  807. $tmpPath = $path . 'tmp/';
  808. if(!FileHandler::makeDir($tmpPath))
  809. {
  810. Context::set('msg', Context::getLang('make directory failed'));
  811. return;
  812. }
  813. $ext = substr(strrchr($img['name'],'.'),1);
  814. $_fileName = md5(crypt(rand(1000000,900000), rand(0,100))).'.'.$ext;
  815. $fileName = $path . $_fileName;
  816. $tmpFileName = $tmpPath . $_fileName;
  817. if(!move_uploaded_file($img['tmp_name'], $tmpFileName))
  818. {
  819. Context::set('msg', Context::getLang('move file failed'));
  820. return;
  821. }
  822. Context::set('name', $name);
  823. Context::set('fileName', $fileName);
  824. Context::set('tmpFileName', $tmpFileName);
  825. }
  826. /**
  827. * Delete config image
  828. */
  829. function procLayoutAdminConfigImageDelete()
  830. {
  831. $layoutSrl = Context::get('layout_srl');
  832. $name = Context::get('name');
  833. $this->setTemplatePath($this->module_path.'tpl');
  834. $this->setTemplateFile("after_delete_config_image.html");
  835. $oModel = getModel('layout');
  836. $layoutInfo = $oModel->getLayout($layoutSrl);
  837. if($layoutInfo->extra_var_count)
  838. {
  839. foreach($layoutInfo->extra_var as $varId => $val)
  840. {
  841. $newLayoutInfo->{$varId} = $val->value;
  842. }
  843. }
  844. unset($newLayoutInfo->{$name});
  845. $args->layout_srl = $layoutSrl;
  846. $args->extra_vars = serialize($newLayoutInfo);
  847. $output = $this->updateLayout($args);
  848. if(!$output->toBool())
  849. {
  850. Context::set('msg', Context::getLang($output->getMessage()));
  851. return $output;
  852. }
  853. FileHandler::removeFile($layoutInfo->extra_var->{$name}->value);
  854. Context::set('name', $name);
  855. }
  856. }
  857. /* End of file layout.admin.controller.php */
  858. /* Location: ./modules/layout/layout.admin.controller.php */