PageRenderTime 106ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/html/AppCode/expressionengine/controllers/cp/design.php

https://github.com/w3bg/www.hsifin.com
PHP | 4632 lines | 3155 code | 906 blank | 571 comment | 575 complexity | 54567a43fef52bd10171d50df890d34b MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine CP Home Page Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Control Panel
  19. * @category Control Panel
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class Design extends Controller {
  24. var $sub_breadcrumbs = array();
  25. // Reserved Template names
  26. var $reserved_names = array('act', 'css');
  27. // Reserved Global Variable names
  28. var $reserved_vars = array(
  29. 'lang',
  30. 'charset',
  31. 'homepage',
  32. 'debug_mode',
  33. 'gzip_mode',
  34. 'version',
  35. 'elapsed_time',
  36. 'hits',
  37. 'total_queries',
  38. 'XID_HASH'
  39. );
  40. /**
  41. * Constructor
  42. *
  43. * @access public
  44. */
  45. function Design()
  46. {
  47. parent::Controller();
  48. if ( ! $this->cp->allowed_group('can_access_design'))
  49. {
  50. show_error($this->lang->line('unauthorized_access'));
  51. }
  52. $this->load->model('template_model');
  53. $this->lang->loadfile('design');
  54. $this->javascript->compile();
  55. if ($this->cp->allowed_group('can_admin_templates'))
  56. {
  57. $this->sub_breadcrumbs = array(
  58. 'global_variables' => BASE.AMP.'C=design'.AMP.'M=global_variables',
  59. 'snippets' => BASE.AMP.'C=design'.AMP.'M=snippets',
  60. 'sync_templates' => BASE.AMP.'C=design'.AMP.'M=sync_templates',
  61. 'global_template_preferences' => BASE.AMP.'C=design'.AMP.'M=global_template_preferences',
  62. 'template_preferences_manager' => BASE.AMP.'C=design'.AMP.'M=template_preferences_manager'
  63. );
  64. }
  65. $this->cp->set_variable('wiki_installed', (bool) $this->db->table_exists('wikis'));
  66. $this->cp->set_variable('forum_installed', (bool) $this->db->table_exists('forums'));
  67. }
  68. // --------------------------------------------------------------------
  69. /**
  70. * Index function
  71. *
  72. * @access public
  73. * @return void
  74. */
  75. function index()
  76. {
  77. if ( ! $this->cp->allowed_group('can_access_design'))
  78. {
  79. show_error($this->lang->line('unauthorized_access'));
  80. }
  81. $this->cp->set_variable('cp_page_title', $this->lang->line('design'));
  82. $this->javascript->output($this->javascript->slidedown("#adminTemplatesSubmenu"));
  83. $this->javascript->compile();
  84. $this->load->vars(array('controller'=>'design'));
  85. $this->load->view('_shared/overview');
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * New Template
  90. *
  91. * Create a new template
  92. *
  93. * @access public
  94. * @return type
  95. */
  96. function template_group_pick($edit = FALSE)
  97. {
  98. if ( ! $this->cp->allowed_group('can_access_design'))
  99. {
  100. show_error($this->lang->line('unauthorized_access'));
  101. }
  102. $group_id = $this->input->get_post('id');
  103. if ($group_id != '')
  104. {
  105. $this->new_template('', $group_id);
  106. }
  107. $this->load->model('template_model');
  108. $this->lang->loadfile('admin_content');
  109. $this->cp->set_variable('cp_page_title', $this->lang->line('new_template_form'));
  110. $this->javascript->compile();
  111. $template_groups_query = $this->template_model->get_template_groups();
  112. $vars['template_groups'] = $template_groups_query->result_array();
  113. $vars['link_to_method'] = ($edit) ? 'edit_template_group' : 'new_template';
  114. // if this isn't an admin, then unset any template
  115. // groups they aren't allowed to admin
  116. if ($this->session->userdata['group_id'] != 1)
  117. {
  118. foreach($vars['template_groups'] as $index=>$group)
  119. {
  120. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  121. {
  122. unset($vars['template_groups'][$index]);
  123. }
  124. }
  125. }
  126. $this->load->view('design/new_template_group_pick', $vars);
  127. }
  128. // --------------------------------------------------------------------
  129. /**
  130. * Delete Template Group
  131. *
  132. * Create a new template
  133. *
  134. * @access public
  135. * @return type
  136. */
  137. function delete_template_group_pick()
  138. {
  139. if ( ! $this->cp->allowed_group('can_access_design'))
  140. {
  141. show_error($this->lang->line('unauthorized_access'));
  142. }
  143. $group_id = $this->input->get_post('id');
  144. if ($group_id != '')
  145. {
  146. $this->manager();
  147. }
  148. $this->load->model('template_model');
  149. $this->lang->loadfile('admin_content');
  150. $this->cp->set_variable('cp_page_title', $this->lang->line('new_template_form'));
  151. $template_groups_query = $this->template_model->get_template_groups();
  152. $vars['template_groups'] = $template_groups_query->result_array();
  153. // if this isn't an admin, then unset any template
  154. // groups they aren't allowed to admin
  155. if ($this->session->userdata['group_id'] != 1)
  156. {
  157. foreach($vars['template_groups'] as $index=>$group)
  158. {
  159. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  160. {
  161. unset($vars['template_groups'][$index]);
  162. }
  163. }
  164. }
  165. $this->javascript->compile();
  166. $this->load->view('design/delete_template_group', $vars);
  167. }
  168. // --------------------------------------------------------------------
  169. /**
  170. * Template Delete Confirm
  171. */
  172. function template_group_delete_confirm()
  173. {
  174. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  175. {
  176. show_error($this->lang->line('unauthorized_access'));
  177. }
  178. $group_id = $this->input->get_post('group_id');
  179. if ($group_id == '')
  180. {
  181. $this->manager();
  182. }
  183. if ( ! is_numeric($group_id))
  184. {
  185. show_error('id_not_found');
  186. }
  187. $this->load->model('template_model');
  188. $this->load->helper('form');
  189. $query = $this->template_model->get_group_info($group_id);
  190. $group_id = $query->row('group_id') ;
  191. $vars['template_group_name'] = $query->row('group_name') ;
  192. if ( ! $this->cp->allowed_group('can_admin_templates'))
  193. {
  194. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  195. {
  196. show_error($this->lang->line('unauthorized_access'));
  197. }
  198. }
  199. $vars['file_folder'] = FALSE;
  200. // Check for associated group folder
  201. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  202. {
  203. $basepath = $this->config->slash_item('tmpl_file_basepath');
  204. $basepath .= $this->config->item('site_short_name').'/'.$vars['template_group_name'].'.group/';
  205. $vars['file_folder'] = is_dir($basepath);
  206. }
  207. $vars['damned'] = array($group_id);
  208. $vars['cp_page_title'] = $this->lang->line('delete_template_group');
  209. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  210. $vars['form_hidden']['group_id'] = $group_id;
  211. $this->javascript->compile();
  212. $this->load->view('design/template_group_delete_confirm', $vars);
  213. }
  214. // --------------------------------------------------------------------
  215. /** -------------------------------
  216. /** Delete Template Group
  217. /** -------------------------------*/
  218. function template_group_delete()
  219. {
  220. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  221. {
  222. show_error($this->lang->line('unauthorized_access'));
  223. }
  224. // if the hidden group_id field is not set, they might be here by accident.
  225. if ( ! $this->input->post('group_id'))
  226. {
  227. show_error($this->lang->line('unauthorized_access'));
  228. }
  229. $group_id = $this->input->get_post('group_id');
  230. if ($group_id == '' OR ! is_numeric($group_id))
  231. {
  232. show_error($this->lang->line('unauthorized_access'));
  233. }
  234. // Delete the group folder if it exists
  235. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  236. {
  237. $this->db->select('group_name');
  238. $result = $this->db->get_where('template_groups', array('group_id' => $group_id));
  239. $basepath = $this->config->slash_item('tmpl_file_basepath');
  240. $basepath .= $this->config->item('site_short_name').'/'.$result->row('group_name').'.group/';
  241. $this->load->helper('file');
  242. delete_files($basepath, TRUE);
  243. @rmdir($basepath);
  244. }
  245. // We need to delete all the saved template data in the versioning table
  246. $this->db->select('template_id');
  247. $this->db->where('group_id', $group_id);
  248. $query = $this->db->get('templates');
  249. if ($query->num_rows() > 0)
  250. {
  251. $sql = "DELETE FROM exp_revision_tracker WHERE ";
  252. $sqlb = '';
  253. foreach ($query->result_array() as $row)
  254. {
  255. $sqlb .= " item_id = '".$row['template_id']."' OR";
  256. }
  257. $sqlb = substr($sqlb, 0, -2);
  258. $this->db->query($sql.$sqlb);
  259. $this->db->query("DELETE FROM exp_template_no_access WHERE ".str_replace('item_id', 'template_id', $sqlb));
  260. $this->db->query("DELETE FROM exp_templates WHERE group_id = '$group_id'");
  261. }
  262. $this->db->query("DELETE FROM exp_template_groups WHERE group_id = '$group_id'");
  263. $this->session->set_flashdata('message_success', $this->lang->line('template_group_deleted'));
  264. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=manager');
  265. }
  266. // --------------------------------------------------------------------
  267. /**
  268. * New Template
  269. *
  270. * Create a new template
  271. *
  272. * @access public
  273. * @return type
  274. */
  275. function new_template($message = '', $group_id = '')
  276. {
  277. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  278. {
  279. show_error($this->lang->line('unauthorized_access'));
  280. }
  281. if ($group_id == '')
  282. {
  283. $group_id = $this->input->get_post('group_id');
  284. }
  285. // if its still blank, make them choose a template
  286. if ($group_id == '')
  287. {
  288. return $this->template_group_pick();
  289. }
  290. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  291. {
  292. show_error($this->lang->line('unauthorized_access'));
  293. }
  294. $this->load->model('template_model');
  295. $this->load->helper('form');
  296. $this->load->library('table');
  297. $templates = $this->template_model->get_templates($this->config->item('site_id'));
  298. $vars['templates'] = array();
  299. foreach($templates->result() as $template)
  300. {
  301. $vars['templates'][$template->group_name][$template->template_id] = $template->template_name;
  302. }
  303. $vars['form_hidden']['group_id'] = $group_id;
  304. //create_new_template
  305. $this->cp->set_variable('cp_page_title', $this->lang->line('new_template_form'));
  306. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  307. $templates = $this->template_model->get_templates($this->config->item('site_id'));
  308. $vars['templates'] = array();
  309. foreach($templates->result() as $template)
  310. {
  311. $vars['templates'][$template->group_name][$template->template_id] = $template->template_name;
  312. }
  313. $this->javascript->compile();
  314. $this->load->view('design/new_template', $vars);
  315. }
  316. // --------------------------------------------------------------------
  317. /**
  318. * New Template Group
  319. *
  320. * Create a new template group
  321. *
  322. * @access public
  323. * @return type
  324. */
  325. function new_template_group()
  326. {
  327. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  328. {
  329. show_error($this->lang->line('unauthorized_access'));
  330. }
  331. $this->load->helper('form');
  332. $this->cp->set_variable('cp_page_title', $this->lang->line('create_new_template_group'));
  333. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  334. $this->load->model('template_model');
  335. $this->lang->loadfile('admin_content');
  336. $this->load->library('form_validation');
  337. $this->load->library('table');
  338. $this->form_validation->set_rules('group_name', 'lang:group_name', 'required|callback__group_name_checks');
  339. $this->form_validation->set_rules('duplicate_group', 'lang:duplicate_group', '');
  340. $this->form_validation->set_rules('is_site_default', 'lang:is_site_default', '');
  341. $this->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
  342. $template_groups_query = $this->template_model->get_template_groups();
  343. $template_groups = $template_groups_query->result_array();
  344. // if this isn't an admin, then unset any template
  345. // groups they aren't allowed to admin
  346. if ($this->session->userdata['group_id'] != 1)
  347. {
  348. foreach($template_groups as $index=>$group)
  349. {
  350. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  351. {
  352. unset($template_groups[$index]);
  353. }
  354. }
  355. }
  356. // now that the groups are filtered, built the group output
  357. $vars['template_groups'] = array('false'=>$this->lang->line('do_not_duplicate_group'));
  358. foreach($template_groups as $group)
  359. {
  360. $vars['template_groups'][$group['group_id']] = $group['group_name'];
  361. }
  362. if ($this->form_validation->run() === TRUE)
  363. {
  364. $this->update_template_group();
  365. }
  366. else
  367. {
  368. $this->javascript->compile();
  369. $this->load->view('design/new_template_group', $vars);
  370. }
  371. }
  372. // --------------------------------------------------------------------
  373. /**
  374. * Global Template Preferences
  375. *
  376. * @access public
  377. * @return type
  378. */
  379. function global_template_preferences()
  380. {
  381. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  382. {
  383. show_error($this->lang->line('unauthorized_access'));
  384. }
  385. $this->load->model('template_model');
  386. $this->load->model('admin_model');
  387. $this->load->helper('form');
  388. $this->load->library('table');
  389. $this->jquery->tablesorter('.mainTable', '{
  390. headers: {2: {sorter: false}},
  391. widgets: ["zebra"]
  392. }');
  393. $this->cp->set_variable('cp_page_title', $this->lang->line('global_template_preferences'));
  394. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  395. $vars['template_data'] = array('' => lang('none'));
  396. $templates = $this->template_model->get_templates();
  397. foreach ($templates->result() as $template)
  398. {
  399. $group_name = $template->group_name.'/'.$template->template_name;
  400. $vars['template_data'][$group_name] = $group_name;
  401. }
  402. $f_data = $this->admin_model->get_config_fields('template_cfg');
  403. foreach ($f_data as $conf => $val)
  404. {
  405. $vars[$conf] = $this->config->item($conf);
  406. }
  407. $vars['save_tmpl_revisions_options'] = array(
  408. 'n' => lang('no'),
  409. 'y' => lang('yes')
  410. );
  411. $vars['save_tmpl_files_options'] = array(
  412. 'n' => lang('no'),
  413. 'y' => lang('yes')
  414. );
  415. $vars['save_tmpl_files_n'] = TRUE;
  416. $vars['save_tmpl_files_y'] = FALSE;
  417. $vars['save_tmpl_revisions_n'] = TRUE;
  418. $vars['save_tmpl_revisions_y'] = FALSE;
  419. $vars['strict_urls_options'] = array(
  420. 'n' => lang('no'),
  421. 'y' => lang('yes')
  422. );
  423. if ($vars['save_tmpl_files'] && $vars['save_tmpl_files'] == 'y')
  424. {
  425. $vars['save_tmpl_files_n'] = FALSE;
  426. $vars['save_tmpl_files_y'] = TRUE;
  427. }
  428. if ($vars['save_tmpl_revisions'] && $vars['save_tmpl_revisions'] == 'y')
  429. {
  430. $vars['save_tmpl_revisions_n'] = FALSE;
  431. $vars['save_tmpl_revisions_y'] = TRUE;
  432. }
  433. $this->javascript->compile();
  434. $this->load->view('design/global_template_preferences', $vars);
  435. }
  436. function update_global_template_prefs()
  437. {
  438. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  439. {
  440. show_error($this->lang->line('unauthorized_access'));
  441. }
  442. //Just to be careful, let's strip out everything not a template conf
  443. $this->load->model('admin_model');
  444. $template_vars = array_keys($this->admin_model->get_config_fields('template_cfg'));
  445. foreach ($_POST as $key => $val)
  446. {
  447. if ( ! in_array($key, $template_vars))
  448. {
  449. unset($_POST[$key]);
  450. }
  451. }
  452. $this->config->update_site_prefs($_POST);
  453. $this->session->set_flashdata('message_success', $this->lang->line('preferences_updated'));
  454. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=global_template_preferences');
  455. }
  456. // --------------------------------------------------------------------
  457. /**
  458. * Snippets
  459. *
  460. * Early-parsed variables for dynamic content
  461. *
  462. * @access public
  463. * @param string
  464. * @return void
  465. */
  466. function snippets()
  467. {
  468. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  469. {
  470. show_error($this->lang->line('unauthorized_access'));
  471. }
  472. $this->load->model('template_model');
  473. $this->load->helper('form');
  474. $this->load->library('table');
  475. $this->jquery->tablesorter('.mainTable', '{
  476. headers: {2: {sorter: false}},
  477. widgets: ["zebra"]
  478. }');
  479. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  480. $this->cp->set_variable('cp_page_title', $this->lang->line('snippets'));
  481. $vars['snippets'] = $this->template_model->get_snippets();
  482. $vars['snippets_count'] = $vars['snippets']->num_rows();
  483. $vars['message'] = ($this->input->get_post('delete') !== FALSE) ? lang('variable_deleted') : FALSE;
  484. $vars['message'] = ($this->input->get_post('update') !== FALSE) ? lang('snippet_updated') : FALSE;
  485. $this->javascript->compile();
  486. $this->cp->set_right_nav(array(
  487. 'create_new_snippet' => BASE.AMP.'C=design'.AMP.'M=snippets_edit')
  488. );
  489. $this->load->view('design/snippets', $vars);
  490. }
  491. // --------------------------------------------------------------------
  492. /**
  493. * Edit/Create Snippets
  494. *
  495. * Displays the form for the creation/editing of Snippets
  496. *
  497. * @access public
  498. * @return void
  499. */
  500. function snippets_edit()
  501. {
  502. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  503. {
  504. show_error($this->lang->line('unauthorized_access'));
  505. }
  506. $this->load->model('template_model');
  507. $this->load->helper('form');
  508. // form defaults
  509. $vars = array(
  510. 'msm' => FALSE,
  511. 'update' => ($this->input->get_post('update') == 1),
  512. 'site_id' => $this->config->item('site_id'),
  513. 'all_sites' => FALSE,
  514. 'snippet_id' => NULL,
  515. 'snippet_name' => '',
  516. 'snippet_contents' => '',
  517. 'create_edit' => $this->lang->line('snippet_create')
  518. );
  519. if ($this->config->item('multiple_sites_enabled') == 'y')
  520. {
  521. $vars['msm'] = TRUE;
  522. }
  523. if ($this->input->get_post('snippet') !== FALSE)
  524. {
  525. if (($snippet = $this->template_model->get_snippet($this->input->get_post('snippet'), TRUE)) !== FALSE)
  526. {
  527. $snippet['snippet_site_id'] = $snippet['site_id'];
  528. unset($snippet['site_id']);
  529. $vars = array_merge($vars, $snippet);
  530. $vars['orig_name'] = $vars['snippet_name'];
  531. $vars['create_edit'] = sprintf($this->lang->line('snippet_edit'), $vars['snippet_name']);
  532. $vars['all_sites'] = ($snippet['snippet_site_id'] == 0) ? TRUE : FALSE;
  533. }
  534. }
  535. $this->cp->set_variable('cp_page_title', $vars['create_edit']);
  536. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  537. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=snippets', $this->lang->line('snippets'));
  538. $this->javascript->compile();
  539. $this->load->view('design/snippets_edit', $vars);
  540. }
  541. // --------------------------------------------------------------------
  542. /**
  543. * Snippets Update
  544. *
  545. * Handles creating/updating of Snippets
  546. *
  547. * @access public
  548. * @return void
  549. */
  550. function snippets_update()
  551. {
  552. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  553. {
  554. show_error($this->lang->line('unauthorized_access'));
  555. }
  556. $this->load->model('template_model');
  557. $this->load->library('api');
  558. foreach (array('snippet_id', 'site_id', 'snippet_name', 'snippet_contents') as $var)
  559. {
  560. ${$var} = $this->input->get_post($var);
  561. }
  562. $update = FALSE;
  563. // is this an update?
  564. if ($snippet_id !== FALSE && ($snippet = $this->template_model->get_snippet($snippet_id)) !== FALSE)
  565. {
  566. $update = TRUE;
  567. }
  568. // validate name and contents
  569. if ($snippet_name == '' OR $snippet_contents == '' OR $site_id === FALSE)
  570. {
  571. show_error($this->lang->line('all_fields_required'));
  572. }
  573. elseif ($this->api->is_url_safe($snippet_name) === FALSE)
  574. {
  575. show_error($this->lang->line('illegal_characters'));
  576. }
  577. elseif (in_array($snippet_name, $this->cp->invalid_custom_field_names()))
  578. {
  579. show_error($this->lang->line('reserved_name'));
  580. }
  581. // validate site_id
  582. if ($site_id != $this->config->item('site_id') AND $site_id != 0)
  583. {
  584. $site_id = $this->config->item('site_id');
  585. }
  586. // looks okay!
  587. $data = array(
  588. 'snippet_name' => $snippet_name,
  589. 'snippet_contents' => $snippet_contents,
  590. 'site_id' => $site_id
  591. );
  592. if ($update === TRUE)
  593. {
  594. // if the var name is changing, make sure it's unique
  595. if ($snippet['snippet_name'] != $data['snippet_name'] && $this->template_model->unique_snippet_name($data['snippet_name']) !== TRUE)
  596. {
  597. show_error($this->lang->line('duplicate_snippet_name'));
  598. }
  599. $this->db->update('snippets', $data, array('snippet_id' => $snippet_id));
  600. $cp_message = $this->lang->line('snippet_updated');
  601. }
  602. else
  603. {
  604. // double check for uniqueness please. Note that since a variable might change from being for
  605. // one site to all sites at any time, we have to have strict uniqueness for all variables at all times.
  606. if ($this->template_model->unique_snippet_name($data['snippet_name']) !== TRUE)
  607. {
  608. show_error($this->lang->line('duplicate_snippet_name'));
  609. }
  610. $this->db->insert('snippets', $data);
  611. $cp_message = $this->lang->line('snippet_created');
  612. }
  613. // Clear caches- db and template cache my result in update not being reflected
  614. $this->functions->clear_caching('all');
  615. $this->session->set_flashdata('message_success', $cp_message);
  616. if ($this->input->get_post('update_and_return') !== FALSE)
  617. {
  618. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=snippets'.AMP.'update=1');
  619. }
  620. else
  621. {
  622. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=snippets_edit'.AMP.'snippet='.$snippet_name.AMP.'update=1');
  623. }
  624. }
  625. // --------------------------------------------------------------------
  626. /**
  627. * Delete Snippets
  628. *
  629. * Delete Delete I Eat Meat
  630. *
  631. * @access public
  632. * @return void
  633. */
  634. function snippets_delete()
  635. {
  636. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  637. {
  638. show_error($this->lang->line('unauthorized_access'));
  639. }
  640. $this->load->model('template_model');
  641. $this->load->helper('form');
  642. if (($snippet_id = $this->input->get_post('snippet_id')) === FALSE)
  643. {
  644. show_error($this->lang->line('unauthorized_access'));
  645. }
  646. if (($snippet = $this->template_model->get_snippet($snippet_id)) === FALSE)
  647. {
  648. show_error($this->lang->line('unauthorized_access'));
  649. }
  650. // offer up confirmation first
  651. if ($this->input->get_post('delete_confirm') == TRUE)
  652. {
  653. $this->template_model->delete_snippet($snippet_id);
  654. $this->session->set_flashdata('message_success', $this->lang->line('snippet_deleted'));
  655. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=snippets'.AMP.'delete=1');
  656. }
  657. else
  658. {
  659. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  660. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=snippets', $this->lang->line('snippets'));
  661. $this->cp->set_variable('cp_page_title', $this->lang->line('delete_snippet'));
  662. $this->load->view('design/snippets_delete', $snippet);
  663. }
  664. }
  665. // --------------------------------------------------------------------
  666. /**
  667. * Global Variables
  668. *
  669. * @access public
  670. * @param string
  671. * @return type
  672. */
  673. function global_variables()
  674. {
  675. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  676. {
  677. show_error($this->lang->line('unauthorized_access'));
  678. }
  679. $this->load->model('template_model');
  680. $this->load->helper('form');
  681. $this->load->library('table');
  682. $this->cp->set_variable('cp_page_title', $this->lang->line('global_variables'));
  683. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  684. $this->jquery->tablesorter('.mainTable', '{
  685. headers: {2: {sorter: false}},
  686. widgets: ["zebra"]
  687. }');
  688. $vars['global_variables'] = $this->template_model->get_global_variables();
  689. $vars['global_variables_count'] = $vars['global_variables']->num_rows();
  690. $this->javascript->compile();
  691. $this->cp->set_right_nav(array(
  692. 'create_new_global_variable' => BASE.AMP.'C=design'.AMP.'M=global_variables_create'
  693. ));
  694. $this->load->view('design/global_variables', $vars);
  695. }
  696. // --------------------------------------------------------------------
  697. /**
  698. * Update Global Variables
  699. *
  700. * Processes the updating of Global Variables
  701. *
  702. * @access public
  703. * @return type
  704. */
  705. function global_variables_update()
  706. {
  707. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  708. {
  709. show_error($this->lang->line('unauthorized_access'));
  710. }
  711. $this->load->model('template_model');
  712. $this->load->library('table');
  713. $variable_id = $this->input->get_post('variable_id');
  714. $variable_name = $this->input->get_post('variable_name');
  715. $variable_data = $this->input->get_post('variable_data');
  716. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  717. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=global_variables', $this->lang->line('global_variables'));
  718. if ($variable_name != '')
  719. {
  720. if ($variable_name == '' OR $variable_data == '')
  721. {
  722. show_error($this->lang->line('all_fields_required'));
  723. }
  724. if ( ! preg_match("#^[a-zA-Z0-9_\-/]+$#i",$variable_name))
  725. {
  726. show_error($this->lang->line('illegal_characters'));
  727. }
  728. if (in_array($_POST['variable_name'], $this->reserved_vars))
  729. {
  730. show_error($this->lang->line('reserved_name'));
  731. }
  732. $this->template_model->update_global_variable($variable_id, $variable_name, $variable_data);
  733. // Clear caches- db and template cache my result in update not being reflected
  734. $this->functions->clear_caching('all');
  735. // Send success message and move user back to global vars page
  736. $this->session->set_flashdata('message_success', $this->lang->line('global_var_updated'));
  737. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=global_variables');
  738. }
  739. else
  740. {
  741. $global_variable = $this->template_model->get_global_variable($variable_id);
  742. if ($global_variable->num_rows() < 1)
  743. {
  744. // They shouldn't be this far
  745. show_error('variable_does_not_exist');
  746. }
  747. $this->load->helper('form');
  748. $global_variable_info = $global_variable->row(); // PHP 5 can do this in one step...
  749. $vars['variable_id'] = $global_variable_info->variable_id;
  750. $vars['variable_name'] = $global_variable_info->variable_name;
  751. $vars['variable_data'] = $global_variable_info->variable_data;
  752. $this->cp->set_variable('cp_page_title', $this->lang->line('global_var_update'));
  753. $this->javascript->compile();
  754. $this->load->view('design/global_variables_update', $vars);
  755. }
  756. }
  757. // --------------------------------------------------------------------
  758. /**
  759. * Create Global Variables
  760. *
  761. * Processes the creation of Global Variables
  762. *
  763. * @access public
  764. * @return type
  765. */
  766. function global_variables_create()
  767. {
  768. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  769. {
  770. show_error($this->lang->line('unauthorized_access'));
  771. }
  772. $this->load->library('table');
  773. $variable_name = $this->input->get_post('variable_name');
  774. $variable_data = $this->input->get_post('variable_data');
  775. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  776. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=global_variables', $this->lang->line('global_variables'));
  777. // Existing variables, will have an id
  778. if ($variable_name != '')
  779. {
  780. if ($variable_name == '' OR $variable_data == '')
  781. {
  782. show_error($this->lang->line('all_fields_required'));
  783. }
  784. if ( ! preg_match("#^[a-zA-Z0-9_\-/]+$#i",$variable_name))
  785. {
  786. show_error($this->lang->line('illegal_characters'));
  787. }
  788. if (in_array($variable_name, $this->reserved_vars))
  789. {
  790. show_error($this->lang->line('reserved_name'));
  791. }
  792. if ($this->template_model->check_duplicate_global_variable_name($variable_name) === FALSE)
  793. {
  794. show_error($this->lang->line('duplicate_var_name'));
  795. }
  796. $this->template_model->create_global_variable($variable_name, $variable_data);
  797. // Clear caches- db and template cache my result in update not being reflected
  798. $this->functions->clear_caching('all');
  799. // Send success message and move user back to global vars page
  800. $this->global_variables($this->lang->line('global_var_created'));
  801. }
  802. else
  803. {
  804. $this->load->helper('form');
  805. $this->cp->set_variable('cp_page_title', $this->lang->line('create_new_global_variable'));
  806. $this->javascript->compile();
  807. $this->load->view('design/global_variables_create');
  808. }
  809. }
  810. // --------------------------------------------------------------------
  811. /**
  812. * Delete Global Variables
  813. *
  814. * @access public
  815. * @return type
  816. */
  817. function global_variables_delete()
  818. {
  819. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  820. {
  821. show_error($this->lang->line('unauthorized_access'));
  822. }
  823. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  824. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=global_variables', $this->lang->line('global_variables'));
  825. $this->load->helper('form');
  826. $variable_id = $this->input->get_post('variable_id');
  827. if ($variable_id == '')
  828. {
  829. // They shouldn't be this far
  830. show_error($this->lang->line('variable_does_not_exist'));
  831. }
  832. $global_variable = $this->template_model->get_global_variable($variable_id);
  833. if ($global_variable->num_rows() < 1)
  834. {
  835. // They shouldn't be this far
  836. show_error('variable_does_not_exist');
  837. }
  838. // offer up confirmation first
  839. // This is a hidden form value, and === isn't an appropriate check
  840. if ($this->input->get_post('delete_confirm') == TRUE)
  841. {
  842. $this->template_model->delete_global_variable($variable_id);
  843. // Send success message and move user back to global vars page
  844. $this->global_variables($this->lang->line('variable_deleted'));
  845. }
  846. else
  847. {
  848. $this->cp->set_variable('cp_page_title', $this->lang->line('delete_global_variable'));
  849. $global_variable_info = $global_variable->row(); // PHP 5 can do this in one step...
  850. $vars['variable_id'] = $global_variable_info->variable_id;
  851. $vars['variable_name'] = $global_variable_info->variable_name;
  852. $this->javascript->compile();
  853. $this->load->view('design/global_variables_delete', $vars);
  854. }
  855. }
  856. // --------------------------------------------------------------------
  857. /**
  858. * Template Preferences Manager
  859. *
  860. * @access public
  861. * @return type
  862. */
  863. function template_preferences_manager($message = '')
  864. {
  865. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  866. {
  867. show_error($this->lang->line('unauthorized_access'));
  868. }
  869. if ($this->input->get_post('id') !== '')
  870. {
  871. $group_id = $this->input->get_post('id');
  872. }
  873. $vars['message'] = $message;
  874. $vars['show_template_manager'] = TRUE; // in an error condition, this will go false
  875. if ($this->session->userdata['group_id'] != 1 && (count($this->session->userdata['assigned_template_groups']) == 0 OR $this->cp->allowed_group('can_admin_templates') == FALSE))
  876. {
  877. $vars['message'] = $this->lang->line('no_templates_assigned');
  878. $vars['show_template_manager'] = FALSE;
  879. return $this->load->view('design/template_preferences_manager', $vars);
  880. }
  881. $this->load->helper('form');
  882. $this->load->library('table');
  883. $this->javascript->output('
  884. // select all options for template access restrictions
  885. $("input.select_all").click(function(){
  886. $("input[class="+$(this).val()+"]").each(function() {
  887. this.checked = true;
  888. });
  889. });
  890. var the_templates = $(\'div[id^="template_group_div_"]\');
  891. $("#template_groups").change(function() {
  892. the_templates.hide();
  893. var openDivs = $(this).val().toString()
  894. var ids = new Array();
  895. ids = openDivs.split(",");
  896. for(i=0;i<ids.length;i++)
  897. {
  898. $("#template_group_div_"+ids[i]).show();
  899. }
  900. return false;
  901. });
  902. ');
  903. // Retrieve Valid Template Groups and Templates
  904. $this->db->from('template_groups tg, templates t');
  905. $this->db->select('tg.group_id, tg.group_name, t.template_id, t.template_name');
  906. $this->db->where('tg.group_id = t.group_id');
  907. $this->db->where('tg.site_id', $this->config->item('site_id'));
  908. if ($this->session->userdata['group_id'] != 1)
  909. {
  910. $this->db->where_in('t.group_id', array_keys($this->session->userdata['assigned_template_groups']));
  911. }
  912. $this->db->order_by('tg.group_order, t.group_id, t.template_name');
  913. $query = $this->db->get();
  914. if ($query->num_rows() == 0)
  915. {
  916. $vars['message'] = $this->lang->line('no_templates_available');
  917. $vars['show_template_manager'] = FALSE;
  918. return $this->load->view('design/template_preferences_manager', $vars);
  919. }
  920. // Create MultiSelect Lists
  921. $current_group = 0;
  922. $groups = array();
  923. $tmpl = array();
  924. $vars['templates'] = array();
  925. foreach ($query->result_array() as $i => $row)
  926. {
  927. if ($row['group_id'] != $current_group)
  928. {
  929. $groups[$row['group_id']] = form_prep($row['group_name']);
  930. if ($current_group != 0)
  931. {
  932. $vars['templates']['template_group_div_'.$current_group]['select'] = form_multiselect('template_group_'.$row['group_id'].'[]', $tmpl, '', "size='8' class='multiselect' style='width:45%'");
  933. $vars['templates']['template_group_div_'.$current_group]['active'] = ($current_group == $group_id) ? TRUE : FALSE;
  934. $tmpl = array();
  935. }
  936. }
  937. $tmpl[$row['template_id']] = form_prep($row['template_name']);
  938. $current_group = $row['group_id'];
  939. }
  940. $groups = form_multiselect('template_groups', $groups, $group_id, "id='template_groups' size='10' class='multiselect' style='width:160px'");
  941. $vars['templates']['template_group_div_'.$current_group]['select'] = form_multiselect('template_group_'.$row['group_id'].'[]', $tmpl, '', "size='8' class='multiselect' style='width:45%'");
  942. $vars['templates']['template_group_div_'.$current_group]['active'] = ($current_group == $group_id) ? TRUE : FALSE;
  943. $vars['groups'] = $groups;
  944. if ($this->input->get_post('U'))
  945. {
  946. $vars['message'] = $this->lang->line('preferences_updated');
  947. }
  948. // Template Preference Headings
  949. $headings = array(
  950. array('template_type', $this->lang->line('type')),
  951. array('cache', $this->lang->line('cache_enable')),
  952. array('refresh', $this->lang->line('refresh_interval').' <small>('.$this->lang->line('refresh_in_minutes').')</small>')
  953. );
  954. if ($this->session->userdata['group_id'] == 1)
  955. {
  956. $headings[] = array('allow_php', $this->lang->line('enable_php').' <span class="notice">*</span>');
  957. $headings[] = array('php_parse_location', $this->lang->line('parse_stage'));
  958. }
  959. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  960. {
  961. $headings[] = array('save_template_file', $this->lang->line('save_template_file'));
  962. }
  963. $headings[] = array('hits', $this->lang->line('hit_counter'));
  964. $vars['headings'] = $headings;
  965. // Template Preference Options
  966. $vars['template_prefs'] = array();
  967. $template_type_options = array(
  968. 'null' => $this->lang->line('do_not_change'),
  969. 'css' => $this->lang->line('css_stylesheet'),
  970. 'js' => $this->lang->line('js'),
  971. 'feed' => $this->lang->line('rss'),
  972. 'static' => $this->lang->line('static'),
  973. 'webpage' => $this->lang->line('webpage'),
  974. 'xml' => $this->lang->line('xml')
  975. );
  976. $vars['template_prefs']['template_type'] = form_dropdown('template_type', $template_type_options, 'null', 'id="template_type"');
  977. $yes_no_options = array(
  978. 'null' => $this->lang->line('do_not_change'),
  979. 'y' => $this->lang->line('yes'),
  980. 'n' => $this->lang->line('no')
  981. );
  982. $vars['template_prefs']['cache'] = form_dropdown('cache', $yes_no_options, 'null', 'id="cache"');
  983. $vars['template_prefs']['refresh'] = form_input(array('name'=>'refresh', 'value'=>'0', 'size'=>5));
  984. if ($this->session->userdata['group_id'] == 1)
  985. {
  986. $php_i_o_options = array(
  987. 'null' => $this->lang->line('do_not_change'),
  988. 'i' => $this->lang->line('input'),
  989. 'o' => $this->lang->line('output')
  990. );
  991. $vars['template_prefs']['allow_php'] = form_dropdown('allow_php', $yes_no_options, 'null', 'id="allow_php"');
  992. $vars['template_prefs']['php_parse_location'] = form_dropdown('php_parse_location', $php_i_o_options, 'null', 'id="php_parse_location"');
  993. }
  994. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  995. {
  996. $vars['template_prefs']['save_template_file'] = form_dropdown('save_template_file', $yes_no_options, 'null', 'id="save_template_file"');
  997. }
  998. $vars['template_prefs']['hits'] = form_input(array('name'=>'hits', 'value'=>'', 'size'=>5));
  999. // Template Access Restrictions
  1000. $this->db->select('group_id, group_title');
  1001. $this->db->where('site_id', $this->config->item('site_id'));
  1002. $this->db->where('group_id !=', '1');
  1003. $this->db->order_by('group_title');
  1004. $query = $this->db->get('member_groups');
  1005. $vars['template_access'] = array();
  1006. foreach ($query->result() as $row)
  1007. {
  1008. $vars['template_access'][$row->group_id][] = $row->group_title;
  1009. $radio_options = '';
  1010. foreach ($yes_no_options as $key => $lang)
  1011. {
  1012. $radio_options .= '<label>'.form_radio('access_'.$row->group_id, $key, '', 'class="access_'.$key.'"').NBS.$lang.'</label>'.NBS.NBS.NBS.NBS.NBS.NBS.NBS;
  1013. }
  1014. $vars['template_access'][$row->group_id][] = $radio_options;
  1015. }
  1016. $vars['template_access']['select_all'][] = $this->lang->line('select_all');
  1017. $select_all_radios = '<label>'.form_radio('select_all', 'access_null', '', 'class="select_all"').NBS.$this->lang->line('do_not_change').'</label>'.NBS.NBS.NBS.NBS.NBS.NBS.NBS;
  1018. $select_all_radios .= '<label>'.form_radio('select_all', 'access_y', '', 'class="select_all"').NBS.$this->lang->line('yes').'</label>'.NBS.NBS.NBS.NBS.NBS.NBS.NBS;
  1019. $select_all_radios .= '<label>'.form_radio('select_all', 'access_n', '', 'class="select_all"').NBS.$this->lang->line('no').'</label>';
  1020. $vars['template_access']['select_all'][] = $select_all_radios;
  1021. $this->db->select('template_groups.group_name, templates.template_name, templates.template_id');
  1022. $this->db->where('template_groups.group_id = '.$this->db->dbprefix('templates.group_id'));
  1023. $this->db->where('template_groups.site_id', $this->config->item('site_id'));
  1024. $this->db->order_by('template_groups.group_name, templates.template_name');
  1025. $query = $this->db->get(array('template_groups', 'templates'));
  1026. $vars['no_auth_bounce_options']['null'] = $this->lang->line('do_not_change');
  1027. foreach ($query->result() as $row)
  1028. {
  1029. $vars['no_auth_bounce_options'][$row->template_id] = $row->group_name.'/'.$row->template_name;
  1030. }
  1031. $vars['enable_http_auth_options'] = $yes_no_options;
  1032. $this->cp->set_variable('cp_page_title', $this->lang->line('template_preferences_manager'));
  1033. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  1034. $this->javascript->compile();
  1035. $this->load->view('design/template_preferences_manager', $vars);
  1036. }
  1037. // --------------------------------------------------------------------
  1038. /**
  1039. * Update Preferences Manager
  1040. *
  1041. * @access public
  1042. * @return type
  1043. */
  1044. function update_manager_prefs()
  1045. {
  1046. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  1047. {
  1048. show_error($this->lang->line('unauthorized_access'));
  1049. }
  1050. // Determine Valid Template Groups and Templates
  1051. if ($this->session->userdata['group_id'] != 1 && (count($this->session->userdata['assigned_template_groups']) == 0 OR $this->cp->allowed_group('can_admin_templates') == FALSE))
  1052. {
  1053. show_error($this->lang->line('unauthorized_access'));
  1054. }
  1055. $this->db->from('template_groups tg, templates t');
  1056. $this->db->select('t.template_id, t.group_id, tg.group_name');
  1057. $this->db->where('tg.group_id = t.group_id');
  1058. $this->db->where('tg.site_id', $this->config->item('site_id'));
  1059. if ($this->session->userdata['group_id'] != 1)
  1060. {
  1061. $this->db->where_in('t.group_id', array_keys($this->session->userdata['assigned_template_groups']));
  1062. }
  1063. $query = $this->db->get();
  1064. if ($query->num_rows() == 0)
  1065. {
  1066. show_error($this->lang->line('unauthorized_access'));
  1067. }
  1068. $delete = array();
  1069. foreach ($query->result_array() as $row)
  1070. {
  1071. $delete[$row['template_id']] = $row['group_name'];
  1072. }
  1073. $templates = array();
  1074. foreach($_POST as $key => $value)
  1075. {
  1076. if (substr($key, 0, strlen('template_group_')) == 'template_group_' && is_array($value))
  1077. {
  1078. foreach($value as $template)
  1079. {
  1080. $templates[] = $this->db->escape_str($template);
  1081. }
  1082. }
  1083. }
  1084. if (count($templates) == 0)
  1085. {
  1086. show_error($this->lang->line('no_templates_selected'));
  1087. }
  1088. // Template Preferences
  1089. $data = array();
  1090. if (in_array($_POST['template_type'], array('css', 'js', 'feed', 'static', 'webpage', 'xml')))
  1091. {
  1092. $data['template_type'] = $_POST['template_type'];
  1093. }
  1094. if ($_POST['cache'] == 'y' OR $_POST['cache'] == 'n')
  1095. {
  1096. $data['cache'] = $_POST['cache'];
  1097. if ($_POST['refresh'] != '' && is_numeric($_POST['refresh']))
  1098. {
  1099. $data['refresh'] = $_POST['refresh'];
  1100. }
  1101. }
  1102. if ($this->session->userdata['group_id'] == 1)
  1103. {
  1104. if ($_POST['allow_php'] == 'y' OR $_POST['allow_php'] == 'n')
  1105. {
  1106. $data['allow_php'] = $_POST['allow_php'];
  1107. if ($_POST['php_parse_location'] == 'i' OR $_POST['php_parse_location'] == 'o')
  1108. {
  1109. $data['php_parse_location'] = $_POST['php_parse_location'];
  1110. }
  1111. }
  1112. }
  1113. if ($_POST['hits'] != '' && is_numeric($_POST['hits']))
  1114. {
  1115. $data['hits'] = $_POST['hits'];
  1116. }
  1117. if ($_POST['enable_http_auth'] == 'y' OR $_POST['enable_http_auth'] == 'n')
  1118. {
  1119. $data['enable_http_auth'] = $_POST['enable_http_auth'];
  1120. }
  1121. if ($_POST['no_auth_bounce'] != 'null')
  1122. {
  1123. $data['no_auth_bounce'] = $_POST['no_auth_bounce'];
  1124. }
  1125. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '')
  1126. {
  1127. if ($this->input->post('save_template_file') != FALSE && $this->input->post('save_template_file') != 'null')
  1128. {
  1129. $data['save_template_file'] = $this->input->post('save_template_file');
  1130. }
  1131. }
  1132. if (count($data) > 0)
  1133. {
  1134. // If we switched 'save' to no, we need to delete files.
  1135. $short_name = $this->config->item('site_short_name');
  1136. if ($this->input->post('save_template_file') == 'n')
  1137. {
  1138. $this->db->from('templates');
  1139. $this->db->select('template_name, template_type, template_id');
  1140. $this->db->where('save_template_file', 'y');
  1141. $this->db->where_in('template_id', $templates);
  1142. $query = $this->db->get();
  1143. if ($query->num_rows() > 0)
  1144. {
  1145. foreach ($query->result_array() as $row)
  1146. {
  1147. $tdata = array(
  1148. 'template_id' => $row['template_id'],
  1149. 'site_short_name' => $short_name,
  1150. 'template_group' => $delete[$row['template_id']],
  1151. 'template_name' => $row['template_name'],
  1152. 'template_type' => $row['template_type']
  1153. );
  1154. $this->_delete_template_file($tdata);
  1155. }
  1156. }
  1157. }
  1158. $this->db->query($this->db->update_string('exp_templates', $data, "template_id IN ('".implode("','", $templates)."')"));
  1159. }
  1160. // Template Access
  1161. $yes = array();
  1162. $no = array();
  1163. $this->db->select('group_id');
  1164. $this->db->where('site_id', $this->config->item('site_id'));
  1165. $this->db->where('group_id !=', '1');
  1166. $this->db->order_by('group_title');
  1167. $query = $this->db->get('member_groups');
  1168. if ($query->num_rows() > 0)
  1169. {
  1170. foreach($query->result_array() as $row)
  1171. {
  1172. if ( isset($_POST['access_'.$row['group_id']]))
  1173. {
  1174. if ($_POST['access_'.$row['group_id']] == 'y')
  1175. {
  1176. $yes[] = $row['group_id'];
  1177. }
  1178. elseif($_POST['access_'.$row['group_id']] == 'n')
  1179. {
  1180. $no[] = $row['group_id'];
  1181. }
  1182. }
  1183. }
  1184. }
  1185. if ( ! empty($yes) OR ! empty($no))
  1186. {
  1187. $access = array();
  1188. if (count($no) > 0)
  1189. {
  1190. foreach($templates as $template)
  1191. {
  1192. $access[$template] = $no;
  1193. }
  1194. }
  1195. $this->db->where_in('template_id', $templates);
  1196. $query = $this->db->get('template_no_access');
  1197. if ($query->num_rows() > 0)
  1198. {
  1199. foreach($query->result_array() as $row)
  1200. {
  1201. if ( ! in_array($row['member_group'], $yes) && ! in_array($row['member_group'], $no))
  1202. {
  1203. $access[$row['template_id']][] = $row['member_group'];
  1204. }
  1205. }
  1206. }
  1207. $this->db->where_in('template_id', $templates);
  1208. $this->db->delete('template_no_access');
  1209. foreach($access as $template => $groups)
  1210. {
  1211. if ( empty($groups)) continue;
  1212. foreach($groups as $group)
  1213. {
  1214. $this->db->query($this->db->insert_string('exp_template_no_access', array('template_id' => $template, 'member_group' => $group)));
  1215. }
  1216. }
  1217. }
  1218. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=template_preferences_manager'.AMP.'U=1');
  1219. }
  1220. // --------------------------------------------------------------------
  1221. /**
  1222. * Create New Template
  1223. *
  1224. * @access public
  1225. * @return type
  1226. */
  1227. function create_new_template()
  1228. {
  1229. if ( ! $this->cp->allowed_group('can_access_design'))
  1230. {
  1231. show_error($this->lang->line('unauthorized_access'));
  1232. }
  1233. $template_name = $this->input->post('template_name');
  1234. $group_id = $this->input->post('group_id');
  1235. if ($group_id == '')
  1236. {
  1237. show_error($this->lang->line('unauthorized_access'));
  1238. }
  1239. if ($template_name == '')
  1240. {
  1241. show_error($this->lang->line('you_must_submit_a_name'));
  1242. }
  1243. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  1244. {
  1245. show_error($this->lang->line('unauthorized_access'));
  1246. }
  1247. if ( ! preg_match("#^[a-zA-Z0-9_\.-]+$#i", $template_name))
  1248. {
  1249. show_error($this->lang->line('illegal_characters'));
  1250. }
  1251. if (in_array($template_name, $this->reserved_names))
  1252. {
  1253. show_error($this->lang->line('reserved_name'));
  1254. }
  1255. $this->db->where('group_id', $_POST['group_id']);
  1256. $this->db->where('template_name', $_POST['template_name']);
  1257. if ($this->db->count_all_results('templates'))
  1258. {
  1259. show_error($this->lang->line('template_name_taken'));
  1260. }
  1261. $template_data = '';
  1262. $template_type = $this->input->post('template_type');
  1263. if ($_POST['template_data'] == 'existing_template')
  1264. {
  1265. $this->db->from('templates t, template_groups tg');
  1266. $this->db->select('tg.group_name, template_name, template_data, template_type, template_notes, cache, refresh, no_auth_bounce, allow_php, php_parse_location, save_template_file');
  1267. $this->db->where('t.template_id', $_POST['existing_template']);
  1268. $this->db->where('tg.group_id = t.group_id');
  1269. $query = $this->db->get();
  1270. if ($this->config->item('save_tmpl_files') == 'y' && $this->config->item('tmpl_file_basepath') != '' && $query->row('save_template_file') == 'y')
  1271. {
  1272. $basepath = $this->config->item('tmpl_file_basepath');
  1273. if (substr($basepath, -1) != '/')
  1274. {
  1275. $basepath .= '/';
  1276. }
  1277. $basepath .= $query->row('group_name') .'/'.$query->row('template_name') .'.php';
  1278. if ($fp = @fopen($basepath, FOPEN_READ))
  1279. {
  1280. flock($fp, LOCK_SH);
  1281. $query->set_row('template_data', (filesize($basepath) == 0) ? '' : fread($fp, filesize($basepath)));
  1282. flock($fp, LOCK_UN);
  1283. fclose($fp);
  1284. }
  1285. }
  1286. $template_data = $query->row('template_data') ;
  1287. if ($template_type != $query->row('template_type') )
  1288. {
  1289. $template_type = $query->row('template_type') ;
  1290. }
  1291. $data = array(
  1292. 'group_id' => $_POST['group_id'],
  1293. 'template_name' => $_POST['template_name'],
  1294. 'template_notes' => $query->row('template_notes') ,
  1295. 'cache' => $query->row('cache') ,
  1296. 'refresh' => $query->row('refresh') ,
  1297. 'no_auth_bounce' => $query->row('no_auth_bounce') ,
  1298. 'php_parse_location' => $query->row('php_parse_location') ,
  1299. 'allow_php' => ($this->session->userdata['group_id'] == 1) ? $query->row('allow_php') : 'n',
  1300. 'template_type' => $template_type,
  1301. 'template_data' => $template_data,
  1302. 'edit_date' => $this->localize->now,
  1303. 'site_id' => $this->config->item('site_id'),
  1304. 'last_author_id' => 0
  1305. );
  1306. $template_id = $this->template_model->create_template($data);
  1307. }
  1308. else
  1309. {
  1310. $data = array(
  1311. 'group_id' => $_POST['group_id'],
  1312. 'template_name' => $_POST['template_name'],
  1313. 'template_type' => $template_type,
  1314. 'template_data' => '',
  1315. 'edit_date' => $this->localize->now,
  1316. 'site_id' => $this->config->item('site_id'),
  1317. 'last_author_id' => $this->session->userdata['member_id']
  1318. );
  1319. $template_id = $this->template_model->create_template($data);
  1320. }
  1321. if (isset($_POST['create']))
  1322. {
  1323. $this->manager($this->lang->line('template_created'));
  1324. }
  1325. else
  1326. {
  1327. $this->edit_template($template_id, $this->lang->line('template_created'));
  1328. }
  1329. }
  1330. // --------------------------------------------------------------------
  1331. /**
  1332. * Edit Template
  1333. *
  1334. * The main template editor
  1335. *
  1336. * @access public
  1337. * @return void
  1338. */
  1339. function edit_template($template_id = '', $message = '', $warnings = array())
  1340. {
  1341. if ( ! $this->cp->allowed_group('can_access_design'))
  1342. {
  1343. show_error($this->lang->line('unauthorized_access'));
  1344. }
  1345. if ($template_id == '')
  1346. {
  1347. $template_id = $this->input->get_post('id');
  1348. if ($template_id == '')
  1349. {
  1350. show_error($this->lang->line('id_not_found'));
  1351. }
  1352. }
  1353. if ( ! is_numeric($template_id))
  1354. {
  1355. show_error($this->lang->line('id_not_found'));
  1356. }
  1357. $this->load->library('api');
  1358. $this->api->instantiate('template_structure');
  1359. $this->load->model('design_model');
  1360. $this->load->helper('file');
  1361. $this->load->helper('form');
  1362. $vars['can_admin_templates'] = $this->cp->allowed_group('can_admin_templates');
  1363. $query = $this->template_model->get_template_info($template_id);
  1364. if ($query->num_rows() == 0)
  1365. {
  1366. show_error($this->lang->line('id_not_found'));
  1367. }
  1368. $group_id = $query->row('group_id');
  1369. $vars['template_type'] = $query->row('template_type') ;
  1370. $this->db->select('group_name');
  1371. $result = $this->db->get_where('template_groups', array('group_id' => $group_id));
  1372. $vars['template_group'] = $result->row('group_name') ;
  1373. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  1374. {
  1375. show_error($this->lang->line('unauthorized_access'));
  1376. }
  1377. $vars['last_file_edit'] = '';
  1378. $vars['file_synced'] = TRUE;
  1379. $vars['template_id'] = $template_id;
  1380. $vars['group_id'] = $group_id;
  1381. $vars['template_data'] = $query->row('template_data') ;
  1382. $vars['template_name'] = $query->row('template_name') ;
  1383. $vars['template_notes'] = $query->row('template_notes') ;
  1384. $vars['save_template_file'] = ($query->row('save_template_file') != 'y') ? FALSE : TRUE ;
  1385. $vars['no_auth_bounce'] = $query->row('no_auth_bounce');
  1386. $vars['enable_http_auth'] = $query->row('enable_http_auth');
  1387. foreach(array('template_type', 'cache', 'refresh', 'allow_php', 'php_parse_location', 'hits') as $pref)
  1388. {
  1389. $vars['prefs'][$pref] = $query->row($pref);
  1390. }
  1391. $vars['prefs']['template_size'] = $this->session->userdata('template_size');
  1392. // now that we have the info, we can set the breadcrumb and page titles
  1393. $this->cp->set_variable('cp_page_title', $this->lang->line('edit_template').' ('.$vars['template_group'].' / '.$vars['template_name'].')');
  1394. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  1395. $date_fmt = ($this->session->userdata('time_format') != '') ? $this->session->userdata('time_format') : $this->config->item('time_format');
  1396. if ($date_fmt == 'us')
  1397. {
  1398. $datestr = '%m/%d/%y %h:%i %a';
  1399. }
  1400. else
  1401. {
  1402. $datestr = '%Y-%m-%d %H:%i';
  1403. }
  1404. $vars['edit_date'] = $this->localize->decode_date($datestr, $query->row('edit_date'), TRUE);
  1405. $mquery = $this->db->query("SELECT screen_name FROM exp_members WHERE member_id = ".$query->row('last_author_id'));
  1406. $vars['last_author'] = ($mquery->num_rows() == 0) ? '' : $mquery->row('screen_name');
  1407. /* -------------------------------------
  1408. /* 'edit_template_start' hook.
  1409. /* - Allows complete takeover of the template editor
  1410. /* - Added 1.6.0
  1411. */
  1412. $edata = $this->extensions->call('edit_template_start', $query, $template_id, $message);
  1413. if ($this->extensions->end_script === TRUE) return;
  1414. /*
  1415. /* -------------------------------------*/
  1416. // Clear old revisions
  1417. if ($this->config->item('save_tmpl_revisions') == 'y')
  1418. {
  1419. $maxrev = $this->config->item('max_tmpl_revisions');
  1420. if ($maxrev != '' AND is_numeric($maxrev) AND $maxrev > 0)
  1421. {
  1422. $res = $this->db->query("SELECT tracker_id FROM exp_revision_tracker WHERE item_id = '$template_id' AND item_table = 'exp_templates' AND item_field ='template_data' ORDER BY tracker_id DESC");
  1423. if ($res->num_rows() > 0 AND $res->num_rows() > $maxrev)
  1424. {
  1425. $flag = '';
  1426. $ct = 1;
  1427. foreach ($res->result_array() as $row)
  1428. {
  1429. if ($ct >= $maxrev)
  1430. {
  1431. $flag = $row['tracker_id'];
  1432. break;
  1433. }
  1434. $ct++;
  1435. }
  1436. if ($flag != '')
  1437. {
  1438. $this->db->query("DELETE FROM exp_revision_tracker WHERE tracker_id < $flag AND item_id = '".$this->db->escape_str($template_id)."' AND item_table = 'exp_templates' AND item_field ='template_data'");
  1439. }
  1440. }
  1441. }
  1442. }
  1443. if ($this->config->item('save_tmpl_files') == 'y' AND $this->config->item('tmpl_file_basepath') != '' AND $vars['save_template_file'] == TRUE)
  1444. {
  1445. $this->load->helper('file');
  1446. $basepath = $this->config->slash_item('tmpl_file_basepath');
  1447. $basepath .= $this->config->item('site_short_name').'/'.$vars['template_group'].'.group/'.$query->row('template_name').$this->api_template_structure->file_extensions($query->row('template_type'));
  1448. if (($file = read_file($basepath)) !== FALSE)
  1449. {
  1450. // Get the file edit date
  1451. $file_date = get_file_info($basepath, 'date');
  1452. if ($file_date !== FALSE)
  1453. {
  1454. $vars['last_file_edit'] = $this->localize->decode_date($datestr, $file_date['date'], TRUE);
  1455. if ($query->row('edit_date') < $file_date['date'])
  1456. {
  1457. $vars['file_synced'] = FALSE;
  1458. $vars['template_data'] = $file;
  1459. }
  1460. else
  1461. {
  1462. $vars['file_synced'] = TRUE;
  1463. }
  1464. }
  1465. }
  1466. }
  1467. $vars['view_path'] = $this->functions->fetch_site_index(0, 0).QUERY_MARKER.'URL='.$this->functions->fetch_site_index();
  1468. $vars['view_path'] = rtrim($vars['view_path'], '/').'/';
  1469. if ($vars['template_type'] == 'css')
  1470. {
  1471. $vars['view_path'] .= QUERY_MARKER.'css='.$vars['template_group'].'/'.$vars['template_name'];
  1472. }
  1473. else
  1474. {
  1475. $vars['view_path'] .= $vars['template_group'].(($vars['template_name'] == 'index') ? '' : '/'.$vars['template_name']);
  1476. }
  1477. $vars['revisions_js'] = ''; //"class='select' onchange='flipButtonText(this.options[this.selectedIndex].value);'>";
  1478. $vars['revision_options'][] = $this->lang->line('revision_history');
  1479. $query = $this->db->query("SELECT tracker_id, item_date, screen_name FROM exp_revision_tracker LEFT JOIN exp_members ON exp_members.member_id = exp_revision_tracker.item_author_id WHERE item_table = 'exp_templates' AND item_field = 'template_data' AND item_id = '".$this->db->escape_str($template_id)."' ORDER BY tracker_id DESC");
  1480. if ($query->num_rows() > 0)
  1481. {
  1482. foreach ($query->result_array() as $row)
  1483. {
  1484. $vars['revision_options'][$row['tracker_id']] = $this->localize->set_human_time($row['item_date']).' ('.$row['screen_name'].')';
  1485. }
  1486. $vars['revision_options']['clear'] = $this->lang->line('clear_revision_history');
  1487. }
  1488. $vars['message'] = $message;
  1489. $vars['save_template_revision'] = ($this->config->item('save_tmpl_revisions') == 'y') ? 1 : '';
  1490. $vars['can_save_file'] = ($this->config->item('save_tmpl_files') == 'y' && $this->config->item('tmpl_file_basepath') != '') ? TRUE : FALSE;
  1491. $this->cp->add_js_script(array(
  1492. 'plugin' => 'markitup',
  1493. 'file' => array(
  1494. 'ee_txtarea',
  1495. 'cp/template_editor',
  1496. 'cp/manager'
  1497. )
  1498. )
  1499. );
  1500. $markItUp = array(
  1501. 'nameSpace' => "html",
  1502. 'onShiftEnter' => array('keepDefault' => FALSE, 'replaceWith' => "<br />\n"),
  1503. 'onCtrlEnter' => array('keepDefault' => FALSE, 'openWith' => "\n<p>", 'closeWith' => "</p>\n")
  1504. );
  1505. /* -------------------------------------------
  1506. /* Hidden Configuration Variable
  1507. /* - allow_textarea_tabs => Preserve tabs in all textareas or disable completely
  1508. /* -------------------------------------------*/
  1509. if($this->config->item('allow_textarea_tabs') != 'n')
  1510. {
  1511. $markItUp['onTab'] = array('keepDefault' => FALSE, 'replaceWith' => "\t");
  1512. }
  1513. $this->javascript->set_global('template.markitup', $markItUp);
  1514. $this->javascript->set_global('template.url',
  1515. str_replace(AMP, '&', BASE).'&C=design&M=template_revision_history&template='.$template_id.'&revision_id=');
  1516. $vars['table_template'] = array(
  1517. 'table_open' => '<table class="templateTable templateEditorTable" border="0" cellspacing="0" cellpadding="0">'
  1518. );
  1519. // member group query
  1520. $this->db->select('group_id, group_title');
  1521. $this->db->where('site_id', $this->config->item('site_id'));
  1522. $this->db->where('group_id !=', '1');
  1523. $this->db->order_by('group_title');
  1524. $m_groups = $this->db->get('member_groups');
  1525. $vars['member_groups'] = array();
  1526. foreach($m_groups->result() as $m_group)
  1527. {
  1528. $vars['member_groups'][$m_group->group_id] = $m_group;
  1529. }
  1530. // template access restrictions query
  1531. $denied_groups = $this->design_model->template_access_restrictions();
  1532. $vars['access'] = array();
  1533. foreach($vars['member_groups'] as $mgroup_id => $group)
  1534. {
  1535. $vars['access'][$mgroup_id] = isset($denied_groups[$template_id][$mgroup_id]) ? FALSE : TRUE;
  1536. }
  1537. $vars['no_auth_bounce_options'] = array();
  1538. if ($this->cp->allowed_group('can_admin_templates'))
  1539. {
  1540. $query = $this->template_model->get_templates();
  1541. foreach ($query->result_array() as $row)
  1542. {
  1543. $vars['no_auth_bounce_options'][$row['template_id']] = $row['group_name'].'/'.$row['template_name'];
  1544. }
  1545. }
  1546. $vars['warnings'] = $warnings;
  1547. $this->javascript->compile();
  1548. $this->cp->set_right_nav(array(
  1549. 'view_rendered_template' => $vars['view_path']
  1550. ));
  1551. $this->load->view('design/edit_template', $vars);
  1552. }
  1553. // --------------------------------------------------------------------
  1554. /**
  1555. * Update template
  1556. *
  1557. * @access public
  1558. * @return void
  1559. */
  1560. function update_template()
  1561. {
  1562. if ( ! $this->cp->allowed_group('can_access_design'))
  1563. {
  1564. show_error($this->lang->line('unauthorized_access'));
  1565. }
  1566. if ( ! $template_id = $this->input->post('template_id'))
  1567. {
  1568. return false;
  1569. }
  1570. if ( ! is_numeric($template_id))
  1571. {
  1572. return false;
  1573. }
  1574. if ( ! $this->_template_access_privs(array('template_id' => $template_id)))
  1575. {
  1576. show_error($this->lang->line('unauthorized_access'));
  1577. }
  1578. $save_result = FALSE;
  1579. $delete_template_file = FALSE;
  1580. $save_template_file = ($this->input->post('save_template_file') == 'y') ? 'y' : 'n';
  1581. /** -------------------------------
  1582. /** Save template as file
  1583. /** -------------------------------*/
  1584. // Depending on how things are set up we might save the template data in a text file
  1585. if ($this->config->item('tmpl_file_basepath') != '' && $this->config->item('save_tmpl_files') == 'y')
  1586. {
  1587. $query = $this->db->query("SELECT exp_templates.template_name, exp_templates.template_type, exp_templates.save_template_file, exp_template_groups.group_name
  1588. FROM exp_templates
  1589. LEFT JOIN exp_template_groups ON exp_templates.group_id = exp_template_groups.group_id
  1590. WHERE template_id = '".$this->db->escape_str($template_id)."'");
  1591. if ($save_template_file == 'y')
  1592. {
  1593. $tdata = array(
  1594. 'site_short_name' => $this->config->item('site_short_name'),
  1595. 'template_id' => $template_id,
  1596. 'template_group' => $query->row('group_name') ,
  1597. 'template_name' => $query->row('template_name'),
  1598. 'template_type' => $query->row('template_type'),
  1599. 'template_data' => $_POST['template_data'],
  1600. 'edit_date' => $this->localize->now,
  1601. 'last_author_id' => $this->session->userdata['member_id']
  1602. );
  1603. $save_result = $this->update_template_file($tdata);
  1604. }
  1605. else
  1606. {
  1607. // If the template was previously saved as a text file,
  1608. // but the checkbox was not selected this time we'll
  1609. // delete the file
  1610. if ($query->row('save_template_file') == 'y')
  1611. {
  1612. $delete_template_file = TRUE;
  1613. $tdata = array(
  1614. 'template_id' => $template_id,
  1615. 'site_short_name' => $this->config->item('site_short_name'),
  1616. 'template_group' => $query->row('group_name') ,
  1617. 'template_name' => $query->row('template_name'),
  1618. 'template_type' => $query->row('template_type')
  1619. );
  1620. $template_file_result = $this->_delete_template_file($tdata);
  1621. }
  1622. }
  1623. }
  1624. /** -------------------------------
  1625. /** Save revision cache
  1626. /** -------------------------------*/
  1627. if ($this->input->post('save_template_revision') == 'y')
  1628. {
  1629. $data = array(
  1630. 'item_id' => $template_id,
  1631. 'item_table' => 'exp_templates',
  1632. 'item_field' => 'template_data',
  1633. 'item_data' => $_POST['template_data'],
  1634. 'item_date' => $this->localize->now,
  1635. 'item_author_id' => $this->session->userdata['member_id']
  1636. );
  1637. $this->db->query($this->db->insert_string('exp_revision_tracker', $data));
  1638. }
  1639. /** -------------------------------
  1640. /** Save Template
  1641. /** -------------------------------*/
  1642. $this->db->query($this->db->update_string('exp_templates', array('template_data' => $_POST['template_data'], 'edit_date' => $this->localize->now, 'last_author_id' => $this->session->userdata['member_id'], 'save_template_file' => $save_template_file, 'template_notes' => $_POST['template_notes']), "template_id = '$template_id'"));
  1643. // Clear cache files
  1644. $this->functions->clear_caching('all');
  1645. $message = $this->lang->line('template_updated');
  1646. $cp_message['message_success'] = $this->lang->line('template_updated');
  1647. if ($save_template_file == 'y' AND $save_result == FALSE)
  1648. {
  1649. $cp_message['message_failure'] = $this->lang->line('template_not_saved');
  1650. $message .= BR.$this->lang->line('template_not_saved');
  1651. }
  1652. elseif ($delete_template_file == TRUE && $template_file_result == FALSE)
  1653. {
  1654. $cp_message['message_failure'] = $this->lang->line('template_file_not_deleted');
  1655. $message .= BR.$this->lang->line('template_file_not_deleted');
  1656. }
  1657. /* -------------------------------------
  1658. /* 'update_template_end' hook.
  1659. /* - Add more things to do for template
  1660. /* - Added 1.6.0
  1661. */
  1662. $edata = $this->extensions->call('update_template_end', $template_id, $message);
  1663. if ($this->extensions->end_script === TRUE) return;
  1664. /*
  1665. /* -------------------------------------*/
  1666. // Check submitted tags (valid modules / plugins)
  1667. $this->_validate_tags();
  1668. if (isset($_POST['update_and_return']) && ( ! count($this->warnings) OR $this->input->post('warnings')))
  1669. {
  1670. $this->session->set_flashdata($cp_message);
  1671. $this->db->select('group_id');
  1672. $this->db->where('template_id', $template_id);
  1673. $query = $this->db->get('templates');
  1674. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$query->row('group_id'));
  1675. }
  1676. elseif (count($this->warnings))
  1677. {
  1678. $this->edit_template($template_id, $message, $this->warnings);
  1679. }
  1680. else
  1681. {
  1682. //$this->edit_template($template_id, $message);
  1683. $this->session->set_flashdata($cp_message);
  1684. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=edit_template'.AMP.'id='.$template_id);
  1685. }
  1686. }
  1687. // --------------------------------------------------------------------
  1688. /**
  1689. * Validate tags
  1690. *
  1691. * @access private
  1692. * @return void
  1693. */
  1694. function _validate_tags()
  1695. {
  1696. $this->warnings = array();
  1697. $str = $_POST['template_data'];
  1698. // Don't trigger inside EE comments
  1699. $str = preg_replace('/{!--(.*?)--}/is', '', $str);
  1700. if (strpos($str, '{exp:') === FALSE)
  1701. {
  1702. return;
  1703. }
  1704. $tags = $this->functions->assign_variables($str);
  1705. $this->load->library('template');
  1706. $this->load->model('addons_model');
  1707. $this->template->fetch_addons();
  1708. $modules = $this->template->modules;
  1709. $plugins = $this->template->plugins;
  1710. unset($this->template);
  1711. $this->db->select('module_name');
  1712. $this->db->order_by('module_name');
  1713. $query = $this->db->get('modules');
  1714. $installed = array_map('array_pop', $query->result_array());
  1715. $installed = array_map('strtolower', $installed);
  1716. $this->info = array_merge($modules, $plugins);
  1717. // Go through the single variables and check if they match installed plugins
  1718. foreach($tags['var_single'] as $tag)
  1719. {
  1720. if (strncmp($tag, 'exp:', 4) === 0)
  1721. {
  1722. $name = substr($tag, 4, strcspn($tag, ': ', 4));
  1723. if ( ! in_array($name, $plugins))
  1724. {
  1725. if (in_array($name, $modules))
  1726. {
  1727. $this->_add_warning($name, $tag, 'no_closing_tag');
  1728. $this->_add_warning($name, $tag, 'docs_link');
  1729. }
  1730. else
  1731. {
  1732. $this->_add_warning($name, $tag, 'class');
  1733. }
  1734. }
  1735. }
  1736. }
  1737. // And now the variable pairs
  1738. foreach($tags['var_pair'] as $tag => $inner)
  1739. {
  1740. if (strncmp($tag, 'exp:', 4) === 0)
  1741. {
  1742. $name = substr($tag, 4, strcspn($tag, ': ', 4)); // :<space>, leave the space in there!
  1743. if ( ! in_array($name, $installed) && ! in_array($name, $plugins))
  1744. {
  1745. if (in_array($name, $modules))
  1746. {
  1747. $this->_add_warning($name, $tag, 'install');
  1748. }
  1749. else
  1750. {
  1751. $this->_add_warning($name, $tag, 'class');
  1752. }
  1753. }
  1754. }
  1755. }
  1756. }
  1757. // --------------------------------------------------------------------
  1758. /**
  1759. * Tag suggestion
  1760. *
  1761. * Takes a tag's class name and finds the closest matching tag using
  1762. * a character swap count (up to 3 changes).
  1763. *
  1764. * @access private
  1765. * @param string tag class name
  1766. * @return void
  1767. */
  1768. function _tag_suggestion($tag_name)
  1769. {
  1770. $weight = 3;
  1771. $suggestion = '';
  1772. if ($tag_name == 'weblog')
  1773. {
  1774. return 'channel';
  1775. }
  1776. foreach($this->info as $name)
  1777. {
  1778. $new_weight = levenshtein($name, $tag_name);
  1779. if ($new_weight != -1 && $new_weight < $weight)
  1780. {
  1781. $suggestion = $name;
  1782. $weight = $new_weight;
  1783. }
  1784. }
  1785. return $suggestion;
  1786. }
  1787. // --------------------------------------------------------------------
  1788. /**
  1789. * Add Warning
  1790. *
  1791. * Utility method used by _validate_tags to build an array of warnings
  1792. *
  1793. * @access private
  1794. * @return void
  1795. */
  1796. function _add_warning($name, $tag, $type)
  1797. {
  1798. if ( ! isset($this->warnings[$name]))
  1799. {
  1800. $this->warnings[$name] = array(
  1801. 'suggestion' => ($type == 'class') ? $this->_tag_suggestion($name) : '',
  1802. 'errors' => array('tag_'.$type.'_error'),
  1803. 'full_tags' => array($tag)
  1804. );
  1805. }
  1806. else
  1807. {
  1808. $this->warnings[$name]['errors'][] = 'tag_'.$type.'_error';
  1809. if ( ! in_array($tag, $this->warnings[$name]['full_tags']))
  1810. {
  1811. $this->warnings[$name]['full_tags'][] = $tag;
  1812. }
  1813. }
  1814. }
  1815. // --------------------------------------------------------------------
  1816. /**
  1817. * Update Template File
  1818. *
  1819. * Saves / updates template saved as a file
  1820. *
  1821. * @access public
  1822. * @param array
  1823. * @return bool
  1824. */
  1825. function update_template_file($data)
  1826. {
  1827. if ( ! $this->cp->allowed_group('can_access_design'))
  1828. {
  1829. show_error($this->lang->line('unauthorized_access'));
  1830. }
  1831. if ( ! isset($data['template_id']) OR ! $this->_template_access_privs(array('template_id' => $data['template_id'])))
  1832. {
  1833. return FALSE;
  1834. }
  1835. if ($this->config->item('save_tmpl_files') == 'n' OR $this->config->item('tmpl_file_basepath') == '')
  1836. {
  1837. return FALSE;
  1838. }
  1839. // check the main template path
  1840. $basepath = $this->config->slash_item('tmpl_file_basepath');
  1841. if ( ! @is_dir($basepath) OR ! is_really_writable($basepath))
  1842. {
  1843. return FALSE;
  1844. }
  1845. $this->load->library('api');
  1846. $this->api->instantiate('template_structure');
  1847. // add a site short name folder, in case MSM uses the same template path, and repeat
  1848. $basepath .= $this->config->item('site_short_name');
  1849. if ( ! @is_dir($basepath))
  1850. {
  1851. if ( ! @mkdir($basepath, DIR_WRITE_MODE))
  1852. {
  1853. return FALSE;
  1854. }
  1855. @chmod($basepath, DIR_WRITE_MODE);
  1856. }
  1857. // and finally with our template group
  1858. $basepath .= '/'.$data['template_group'].'.group';
  1859. if ( ! is_dir($basepath))
  1860. {
  1861. if ( ! @mkdir($basepath, DIR_WRITE_MODE))
  1862. {
  1863. return FALSE;
  1864. }
  1865. @chmod($basepath, DIR_WRITE_MODE);
  1866. }
  1867. $filename = $data['template_name'].$this->api_template_structure->file_extensions($data['template_type']);
  1868. if ( ! $fp = @fopen($basepath.'/'.$filename, FOPEN_WRITE_CREATE_DESTRUCTIVE))
  1869. {
  1870. return FALSE;
  1871. }
  1872. else
  1873. {
  1874. flock($fp, LOCK_EX);
  1875. fwrite($fp, $data['template_data']);
  1876. flock($fp, LOCK_UN);
  1877. fclose($fp);
  1878. @chmod($basepath.'/'.$filename, FILE_WRITE_MODE);
  1879. }
  1880. return TRUE;
  1881. }
  1882. function _delete_template_file($data)
  1883. {
  1884. if ( ! isset($data['template_id']) OR ! $this->_template_access_privs(array('template_id' => $data['template_id'])))
  1885. {
  1886. return FALSE;
  1887. }
  1888. $this->load->library('api');
  1889. $this->api->instantiate('template_structure');
  1890. $basepath = $this->config->slash_item('tmpl_file_basepath');
  1891. $basepath .= $data['site_short_name'].'/'.$data['template_group'].'.group/'.$data['template_name'].$this->api_template_structure->file_extensions($data['template_type']);
  1892. if ( ! @unlink($basepath))
  1893. {
  1894. return FALSE;
  1895. }
  1896. return TRUE;
  1897. }
  1898. // --------------------------------------------------------------------
  1899. /**
  1900. * View Template Revision
  1901. */
  1902. function template_revision_history()
  1903. {
  1904. if ($this->config->item('save_tmpl_revisions') == 'n')
  1905. {
  1906. // Revisions are off, but they are here anyway
  1907. // It's confusing to simply show a white screen, so
  1908. // give some feedback.
  1909. show_error($this->lang->line('tmpl_revisions_not_enabled'));
  1910. }
  1911. if ( ! $this->cp->allowed_group('can_access_design'))
  1912. {
  1913. show_error($this->lang->line('unauthorized_access'));
  1914. }
  1915. if ( ! $id = $this->input->get_post('revision_id'))
  1916. {
  1917. show_error($this->lang->line('unauthorized_access'));
  1918. }
  1919. $item_id = $this->input->get_post('template');
  1920. $this->load->helper('form');
  1921. $this->load->library('api');
  1922. $this->api->instantiate('template_structure');
  1923. $vars = array();
  1924. $this->javascript->output('$(window).focus();');
  1925. $this->javascript->compile();
  1926. if ($id != 'clear')
  1927. {
  1928. $vars['cp_page_title'] = $this->lang->line('revision_history');
  1929. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  1930. $this->db->select('item_id, item_data, item_date');
  1931. $this->db->where('tracker_id', $id);
  1932. $this->db->where('item_table', 'exp_templates');
  1933. $this->db->where('item_field', 'template_data');
  1934. $query = $this->db->get('revision_tracker');
  1935. $item_id = $query->row('item_id');
  1936. $vars['revision_data'] = $query->row('item_data');
  1937. $vars['type'] = 'revision';
  1938. if ($query->num_rows() == 0)
  1939. {
  1940. return false;
  1941. }
  1942. $date_fmt = ($this->session->userdata('time_format') != '') ? $this->session->userdata('time_format') : $this->config->item('time_format');
  1943. if ($date_fmt == 'us')
  1944. {
  1945. $datestr = '%m/%d/%y %h:%i %a';
  1946. }
  1947. else
  1948. {
  1949. $datestr = '%Y-%m-%d %H:%i';
  1950. }
  1951. $vars['revision_date'] = $this->localize->decode_date($datestr, $query->row('item_date'), TRUE);
  1952. }
  1953. else
  1954. {
  1955. $vars['cp_page_title'] = $this->lang->line('clear_revision_history');
  1956. $vars['revision_data'] = '';
  1957. $vars['type'] = 'clear';
  1958. $vars['form_hidden'] = array('template_id' => $item_id);
  1959. $vars['template_name'] = '';
  1960. $vars['revision_date'] = '';
  1961. }
  1962. if ( ! $this->_template_access_privs(array('template_id' => $item_id)))
  1963. {
  1964. show_error($this->lang->line('unauthorized_access'));
  1965. }
  1966. $query = $this->template_model->get_template_info($item_id);
  1967. if ($query->num_rows() == 0)
  1968. {
  1969. show_error($this->lang->line('id_not_found'));
  1970. }
  1971. $group_id = $query->row('group_id');
  1972. $this->db->select('group_name');
  1973. $result = $this->db->get_where('template_groups', array('group_id' => $group_id));
  1974. $vars['template_group'] = $result->row('group_name') ;
  1975. $vars['template_name'] = $query->row('template_name') ;
  1976. //$EE_view_disable = TRUE;
  1977. // $vars['EE_view_disable'] = TRUE;
  1978. $this->javascript->compile();
  1979. $this->load->view('design/revision_history', $vars);
  1980. }
  1981. // --------------------------------------------------------------------
  1982. /**
  1983. * Clear Revision History
  1984. */
  1985. function clear_revision_history()
  1986. {
  1987. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  1988. {
  1989. show_error($this->lang->line('unauthorized_access'));
  1990. }
  1991. if ( ! $id = $this->input->post('template_id'))
  1992. {
  1993. return false;
  1994. }
  1995. $this->db->where('item_id', $id);
  1996. $this->db->where('item_table', 'exp_templates');
  1997. $this->db->where('item_field', 'template_data');
  1998. $this->db->delete('revision_tracker');
  1999. $vars['cp_page_title'] = $this->lang->line('history_cleared');
  2000. $vars['revision_data'] = '';
  2001. $vars['type'] = 'cleared';
  2002. $vars['form_hidden'] = array();
  2003. $vars['template_name'] = '';
  2004. $vars['revision_date'] = '';
  2005. $this->javascript->compile();
  2006. $this->load->view('design/revision_history', $vars);
  2007. }
  2008. // --------------------------------------------------------------------
  2009. /**
  2010. * Template Delete Confirm
  2011. */
  2012. function template_delete_confirm()
  2013. {
  2014. if ( ! $this->cp->allowed_group('can_access_design'))
  2015. {
  2016. show_error($this->lang->line('unauthorized_access'));
  2017. }
  2018. $template_id = $this->input->get_post('template_id');
  2019. if ($template_id == '')
  2020. {
  2021. return $this->manager();
  2022. }
  2023. if ( ! is_numeric($template_id))
  2024. {
  2025. show_error($this->lang->line('template_id_not_found'));
  2026. }
  2027. $this->load->helper('form');
  2028. $this->load->library('api');
  2029. $this->api->instantiate('template_structure');
  2030. $query = $this->template_model->get_template_info($template_id, array('group_id', 'template_name', 'template_type'));
  2031. // You can't delete the index template
  2032. if ($query->row('template_name') == 'index')
  2033. {
  2034. show_error($this->lang->line('index_delete_disallowed'));
  2035. }
  2036. $group_id = $query->row('group_id') ;
  2037. $vars['template_name'] = $query->row('template_name') ;
  2038. if ( ! $this->cp->allowed_group('can_admin_templates'))
  2039. {
  2040. if ( ! $this->_template_access_privs(array('group_id' => $group_id)))
  2041. {
  2042. show_error($this->lang->line('unauthorized_access'));
  2043. }
  2044. }
  2045. $this->db->select('group_name');
  2046. $result = $this->db->get_where('template_groups', array('group_id' => $group_id));
  2047. $vars['template_group'] = $result->row('group_name');
  2048. $file = FALSE;
  2049. if ($this->config->item('save_tmpl_files') == 'y' && $this->config->item('tmpl_file_basepath') != '')
  2050. {
  2051. $basepath = $this->config->slash_item('tmpl_file_basepath');
  2052. $basepath .= $this->config->item('site_short_name').'/'.$vars['template_group'].'.group/'.$query->row('template_name').$this->api_template_structure->file_extensions($query->row('template_type'));
  2053. if (($file = read_file($basepath)) !== FALSE)
  2054. {
  2055. $file = $basepath;
  2056. }
  2057. }
  2058. $vars['file'] = $file;
  2059. $vars['damned'] = array($template_id);
  2060. $vars['group_id'] = $group_id;
  2061. $vars['cp_page_title'] = $this->lang->line('template_del_conf');
  2062. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  2063. $vars['form_hidden']['template_id'] = $template_id;
  2064. $this->javascript->compile();
  2065. $this->load->view('design/template_delete_confirm', $vars);
  2066. }
  2067. // --------------------------------------------------------------------
  2068. /**
  2069. * Delete Template
  2070. */
  2071. function template_delete()
  2072. {
  2073. if ( ! $this->cp->allowed_group('can_access_design'))
  2074. {
  2075. show_error($this->lang->line('unauthorized_access'));
  2076. }
  2077. $template_id = $this->input->get_post('template_id');
  2078. if ($template_id == '')
  2079. {
  2080. $this->manager();
  2081. }
  2082. if ( ! is_numeric($template_id))
  2083. {
  2084. show_error('id_not_found');
  2085. }
  2086. $path = FALSE;
  2087. $query = $this->template_model->get_template_info($template_id, array('group_id', 'template_type', 'template_name'));
  2088. $group_id = $query->row('group_id');
  2089. if ( ! $this->cp->allowed_group('can_admin_templates'))
  2090. {
  2091. if ( ! $this->template_access_privs(array('group_id' => $group_id)))
  2092. {
  2093. show_error($this->lang->line('unauthorized_access'));
  2094. }
  2095. }
  2096. if ($this->config->item('save_tmpl_files') == 'y' && $this->config->item('tmpl_file_basepath') != '')
  2097. {
  2098. $this->load->library('api');
  2099. $this->api->instantiate('template_structure');
  2100. $this->db->select('group_name');
  2101. $result = $this->db->get_where('template_groups', array('group_id' => $group_id));
  2102. $this->load->helper('file');
  2103. $basepath = $this->config->slash_item('tmpl_file_basepath');
  2104. $basepath .= $this->config->item('site_short_name').'/'.$result->row('group_name').'.group/'.$query->row('template_name').$this->api_template_structure->file_extensions($query->row('template_type'));
  2105. if (($file = read_file($basepath)) !== FALSE)
  2106. {
  2107. $path = $basepath;
  2108. }
  2109. }
  2110. $out = $this->template_model->delete_template($template_id, $path);
  2111. $message = ($out === TRUE) ? $this->lang->line('template_deleted') : $this->lang->line('error_deleting_template');
  2112. $this->manager($message);
  2113. }
  2114. // --------------------------------------------------------------------
  2115. /**
  2116. * Template Access Privs
  2117. *
  2118. * Verifies access privileges to edit a template
  2119. *
  2120. * @access private
  2121. * @param mixed
  2122. * @return bool
  2123. */
  2124. function _template_access_privs($data = '')
  2125. {
  2126. // If the user is a Super Admin, return true
  2127. if ($this->session->userdata['group_id'] == 1)
  2128. {
  2129. return TRUE;
  2130. }
  2131. $template_id = '';
  2132. $group_id = '';
  2133. if (is_array($data))
  2134. {
  2135. if (isset($data['template_id']))
  2136. {
  2137. $template_id = $data['template_id'];
  2138. }
  2139. if (isset($data['group_id']))
  2140. {
  2141. $group_id = $data['group_id'];
  2142. }
  2143. }
  2144. if ($group_id == '')
  2145. {
  2146. if ($template_id == '')
  2147. {
  2148. return FALSE;
  2149. }
  2150. else
  2151. {
  2152. $query = $this->db->query("SELECT group_id, template_name FROM exp_templates WHERE template_id = '".$this->db->escape_str($template_id)."'");
  2153. $group_id = $query->row('group_id') ;
  2154. }
  2155. }
  2156. $access = FALSE;
  2157. foreach ($this->session->userdata['assigned_template_groups'] as $key => $val)
  2158. {
  2159. if ($group_id == $key)
  2160. {
  2161. $access = TRUE;
  2162. break;
  2163. }
  2164. }
  2165. if ($access == FALSE)
  2166. {
  2167. return FALSE;
  2168. }
  2169. return TRUE;
  2170. }
  2171. // --------------------------------------------------------------------
  2172. /**
  2173. * User Message
  2174. *
  2175. * Processes new template
  2176. *
  2177. * @access public
  2178. * @param string
  2179. * @return type
  2180. */
  2181. function user_message($message = '')
  2182. {
  2183. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_design'))
  2184. {
  2185. show_error($this->lang->line('unauthorized_access'));
  2186. }
  2187. $template_id = $this->input->get_post('template_id');
  2188. $template_data = $this->input->get_post('template_data');
  2189. $this->jquery->plugin(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'plugin=markitup', TRUE);
  2190. $markItUp = array(
  2191. 'nameSpace' => "html",
  2192. 'onShiftEnter' => array('keepDefault' => FALSE, 'replaceWith' => "<br />\n"),
  2193. 'onCtrlEnter' => array('keepDefault' => FALSE, 'openWith' => "\n<p>", 'closeWith' => "</p>\n")
  2194. );
  2195. /* -------------------------------------------
  2196. /* Hidden Configuration Variable
  2197. /* - allow_textarea_tabs => Preserve tabs in all textareas or disable completely
  2198. /* -------------------------------------------*/
  2199. if($this->config->item('allow_textarea_tabs') != 'n') {
  2200. $markItUp['onTab'] = array('keepDefault' => FALSE, 'replaceWith' => "\t");
  2201. }
  2202. $this->javascript->output('
  2203. $("#template_data").markItUp('.$this->javascript->generate_json($markItUp).');
  2204. ');
  2205. // check what the message is also, as this method could throw itself
  2206. // into an infinite loop if we aren't careful here.
  2207. if ($template_id)
  2208. {
  2209. $this->template_model->update_specialty_template($template_id, $template_data);
  2210. $this->session->set_flashdata('message_success', $this->lang->line('template_updated'));
  2211. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=user_message');
  2212. }
  2213. else
  2214. {
  2215. $this->load->helper('form');
  2216. $this->lang->loadfile('specialty_tmp');
  2217. $this->cp->set_variable('cp_page_title', $this->lang->line('user_message'));
  2218. $template = $this->template_model->get_specialty_template('message_template');
  2219. $template_data = $template->row();
  2220. $vars = array(
  2221. 'template_data' => $template_data->template_data,
  2222. 'template_id' => $template_data->template_id,
  2223. 'message' => $message
  2224. );
  2225. $this->javascript->compile();
  2226. $this->load->view('design/user_message', $vars);
  2227. }
  2228. }
  2229. // --------------------------------------------------------------------
  2230. /**
  2231. * System Offline
  2232. *
  2233. * The system offline template
  2234. *
  2235. * @access public
  2236. * @param string
  2237. * @return type
  2238. */
  2239. function system_offline()
  2240. {
  2241. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_design'))
  2242. {
  2243. show_error($this->lang->line('unauthorized_access'));
  2244. }
  2245. $this->javascript->compile();
  2246. $template_id = $this->input->get_post('template_id');
  2247. $template_data = $this->input->get_post('template_data');
  2248. $this->jquery->plugin(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'plugin=markitup', TRUE);
  2249. $markItUp = array(
  2250. 'nameSpace' => "html",
  2251. 'onShiftEnter' => array('keepDefault' => FALSE, 'replaceWith' => "<br />\n"),
  2252. 'onCtrlEnter' => array('keepDefault' => FALSE, 'openWith' => "\n<p>", 'closeWith' => "</p>\n")
  2253. );
  2254. /* -------------------------------------------
  2255. /* Hidden Configuration Variable
  2256. /* - allow_textarea_tabs => Preserve tabs in all textareas or disable completely
  2257. /* -------------------------------------------*/
  2258. if($this->config->item('allow_textarea_tabs') != 'n') {
  2259. $markItUp['onTab'] = array('keepDefault' => FALSE, 'replaceWith' => "\t");
  2260. }
  2261. $this->javascript->output('
  2262. $("#template_data").markItUp('.$this->javascript->generate_json($markItUp).');
  2263. ');
  2264. if ($template_id)
  2265. {
  2266. $this->template_model->update_specialty_template($template_id, $template_data);
  2267. $this->session->set_flashdata('message_success', $this->lang->line('template_updated'));
  2268. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=system_offline');
  2269. }
  2270. else
  2271. {
  2272. $this->load->helper('form');
  2273. $this->lang->loadfile('specialty_tmp');
  2274. $this->cp->set_variable('cp_page_title', $this->lang->line('offline_template'));
  2275. $template = $this->template_model->get_specialty_template('offline_template');
  2276. $template_data = $template->row();
  2277. $vars = array(
  2278. 'template_data' => $template_data->template_data,
  2279. 'template_id' => $template_data->template_id,
  2280. );
  2281. $this->javascript->compile();
  2282. $this->load->view('design/system_offline', $vars);
  2283. }
  2284. }
  2285. // --------------------------------------------------------------------
  2286. /**
  2287. * Email Notification
  2288. *
  2289. * @access public
  2290. * @return void
  2291. */
  2292. function email_notification()
  2293. {
  2294. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_design'))
  2295. {
  2296. show_error($this->lang->line('unauthorized_access'));
  2297. }
  2298. $this->lang->loadfile('specialty_tmp');
  2299. $this->cp->set_variable('cp_page_title', $this->lang->line('email_notification_template'));
  2300. $vars['specialty_email_templates_summary'] = $this->template_model->get_specialty_email_templates_summary();
  2301. $this->javascript->compile();
  2302. $this->load->view('design/email_notification', $vars);
  2303. }
  2304. // --------------------------------------------------------------------
  2305. /**
  2306. * Edit Email Template
  2307. *
  2308. * @access public
  2309. * @return void
  2310. */
  2311. function edit_email_notification()
  2312. {
  2313. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_design'))
  2314. {
  2315. show_error($this->lang->line('unauthorized_access'));
  2316. }
  2317. $this->lang->loadfile('specialty_tmp');
  2318. $template = $this->input->get_post('template');
  2319. $template_query = $this->template_model->get_specialty_template($template);
  2320. if ($template_query->num_rows() == 0)
  2321. {
  2322. show_error($this->lang->line('unauthorized_access'));
  2323. }
  2324. $this->cp->set_variable('cp_page_title', $this->lang->line('edit_template'));
  2325. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=email_notification', $this->lang->line('email_notification_template'));
  2326. $this->load->helper('form');
  2327. $this->cp->add_js_script(array('plugin' => 'markitup'));
  2328. $markItUp = array(
  2329. 'nameSpace' => "html",
  2330. 'onShiftEnter' => array('keepDefault' => FALSE, 'replaceWith' => "<br />\n"),
  2331. 'onCtrlEnter' => array('keepDefault' => FALSE, 'openWith' => "\n<p>", 'closeWith' => "</p>\n")
  2332. );
  2333. /* -------------------------------------------
  2334. /* Hidden Configuration Variable
  2335. /* - allow_textarea_tabs => Preserve tabs in all textareas or disable completely
  2336. /* -------------------------------------------*/
  2337. if($this->config->item('allow_textarea_tabs') != 'n')
  2338. {
  2339. $markItUp['onTab'] = array('keepDefault' => FALSE, 'replaceWith' => "\t");
  2340. }
  2341. $this->javascript->output('
  2342. $("#template_data").markItUp('.$this->javascript->generate_json($markItUp).');
  2343. ');
  2344. $vars = array(
  2345. 'vars' => $this->template_model->get_specialty_template_vars($template),
  2346. 'template' => $template,
  2347. 'template_data' => $template_query->row('template_data'),
  2348. 'template_title' => $template_query->row('data_title'),
  2349. 'template_id' => $template_query->row('template_id'),
  2350. 'template_name' => ($this->lang->line($template) == FALSE) ? $template : $this->lang->line($template),
  2351. 'enable_template' => $template_query->row('enable_template')
  2352. );
  2353. $this->javascript->compile();
  2354. $this->load->view('design/edit_email_notification', $vars);
  2355. }
  2356. // --------------------------------------------------------------------
  2357. /**
  2358. * Update Email Notification
  2359. *
  2360. * @access public
  2361. * @return void
  2362. */
  2363. function update_email_notification()
  2364. {
  2365. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_design'))
  2366. {
  2367. show_error($this->lang->line('unauthorized_access'));
  2368. }
  2369. $template_name = $this->input->post('template');
  2370. $template_id = $this->input->post('template_id');
  2371. $template_data = $this->input->post('template_data');
  2372. $enable_template = ($this->input->post('enable_template')) ? 'y' : 'n';
  2373. $template_title = $this->input->post('template_title');
  2374. $query = $this->template_model->get_specialty_template($template_name);
  2375. if ($query->num_rows() != 1 OR $query->row('template_id') != $template_id)
  2376. {
  2377. show_error($this->lang->line('unauthorized_access'));
  2378. }
  2379. $this->template_model->update_specialty_template($template_id, $template_data,
  2380. $enable_template, $template_title);
  2381. // Clear cache files
  2382. $this->functions->clear_caching('all');
  2383. $this->session->set_flashdata('message_success', $this->lang->line('template_updated'));
  2384. if ($this->input->get_post('update_and_return') !== FALSE)
  2385. {
  2386. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=email_notification');
  2387. }
  2388. // go back to the edit page for this template
  2389. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=edit_email_notification'.AMP.'template='.$template_name);
  2390. }
  2391. // --------------------------------------------------------------------
  2392. /**
  2393. * Member Profile Templates
  2394. */
  2395. function member_profile_templates()
  2396. {
  2397. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_mbr_templates'))
  2398. {
  2399. show_error($this->lang->line('unauthorized_access'));
  2400. }
  2401. $this->load->model('member_model');
  2402. $this->load->helper('string');
  2403. $vars['profiles'] = $this->member_model->get_profile_templates();
  2404. $this->cp->set_variable('cp_page_title', $this->lang->line('member_profile_templates'));
  2405. $this->javascript->compile();
  2406. $this->load->view('design/member_profile_templates', $vars);
  2407. }
  2408. // --------------------------------------------------------------------
  2409. /** -----------------------------
  2410. /** List Templates within a set
  2411. /** -----------------------------*/
  2412. function list_profile_templates()
  2413. {
  2414. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_mbr_templates'))
  2415. {
  2416. show_error($this->lang->line('unauthorized_access'));
  2417. }
  2418. $this->load->library('security');
  2419. $path = PATH_MBR_THEMES.$this->security->sanitize_filename($this->input->get_post('name'));
  2420. if ( ! is_dir($path))
  2421. {
  2422. show_error($this->lang->line('unable_to_find_templates'));
  2423. }
  2424. $this->load->helper('directory');
  2425. $files = directory_map($path, TRUE);
  2426. $vars = array();
  2427. $vars['theme_name'] = $this->input->get_post('name');
  2428. $vars['theme_display_name'] = ucfirst(str_replace("_", " ", $vars['theme_name']));
  2429. $vars['templates'] = array();
  2430. foreach ($files as $val)
  2431. {
  2432. if (strpos($val, '.') === FALSE)
  2433. {
  2434. continue;
  2435. }
  2436. $human = substr($val, 0, -strlen(strrchr($val, '.')));
  2437. $vars['templates'][$val] = ($this->lang->line($human) == FALSE) ? $human : $this->lang->line($human);
  2438. }
  2439. asort($vars['templates']);
  2440. $this->cp->set_variable('cp_page_title', $this->lang->line('member_profile_templates'));
  2441. $this->javascript->compile();
  2442. $this->load->view('design/member_profile_templates_list', $vars);
  2443. }
  2444. // --------------------------------------------------------------------
  2445. /** -----------------------------
  2446. /** Edit Profile Template
  2447. /** -----------------------------*/
  2448. function edit_profile_template($theme = '', $name = '', $template_data = '')
  2449. {
  2450. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_mbr_templates'))
  2451. {
  2452. show_error($this->lang->line('unauthorized_access'));
  2453. }
  2454. $this->load->library('security');
  2455. $update = ($theme != '' AND $name != '') ? TRUE : FALSE;
  2456. if ($theme == '')
  2457. {
  2458. $theme = $this->input->get_post('theme');
  2459. }
  2460. if ($name == '')
  2461. {
  2462. $name = $this->input->get_post('name');
  2463. }
  2464. $path = PATH_MBR_THEMES.$this->security->sanitize_filename($theme).'/'.$name;
  2465. if ( ! file_exists($path))
  2466. {
  2467. show_error($this->lang->line('unable_to_find_template_file'));
  2468. }
  2469. $human = substr($name, 0, -strlen(strrchr($name, '.')));
  2470. $vars['template_name'] = ($this->lang->line($human) == FALSE) ? $human : $this->lang->line($human);
  2471. $vars['theme'] = $theme;
  2472. $vars['theme_display_name'] = ucfirst(str_replace("_", " ", $vars['theme']));
  2473. $vars['template_data'] = ($update === FALSE) ? file_get_contents($path) : $template_data;
  2474. $vars['name'] = $name;
  2475. $vars['not_writable'] = ! is_really_writable($path);
  2476. $vars['message'] = ($update === TRUE) ? $this->lang->line('template_updated') : '';
  2477. $vars['type'] = 'profile';
  2478. $this->load->helper('form');
  2479. $this->cp->set_variable('cp_page_title', $this->lang->line('member_profile_templates'));
  2480. $this->jquery->plugin(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'plugin=markitup', TRUE);
  2481. $markItUp = array(
  2482. 'nameSpace' => "html",
  2483. 'onShiftEnter' => array('keepDefault' => FALSE, 'replaceWith' => "<br />\n"),
  2484. 'onCtrlEnter' => array('keepDefault' => FALSE, 'openWith' => "\n<p>", 'closeWith' => "</p>\n")
  2485. );
  2486. /* -------------------------------------------
  2487. /* Hidden Configuration Variable
  2488. /* - allow_textarea_tabs => Preserve tabs in all textareas or disable completely
  2489. /* -------------------------------------------*/
  2490. if($this->config->item('allow_textarea_tabs') != 'n') {
  2491. $markItUp['onTab'] = array('keepDefault' => FALSE, 'replaceWith' => "\t");
  2492. }
  2493. $this->javascript->output('
  2494. $("#template_data").markItUp('.$this->javascript->generate_json($markItUp).');
  2495. ');
  2496. $this->javascript->compile();
  2497. $this->load->view('design/edit_theme_template', $vars);
  2498. }
  2499. /** -----------------------------
  2500. /** Save Profile Template
  2501. /** -----------------------------*/
  2502. function update_theme_template()
  2503. {
  2504. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_mbr_templates'))
  2505. {
  2506. show_error($this->lang->line('unauthorized_access'));
  2507. }
  2508. $this->load->library('security');
  2509. $theme = $this->input->get_post('theme');
  2510. $name = $this->input->get_post('name');
  2511. $template_data = $this->input->get_post('template_data');
  2512. switch ($type = $this->input->get_post('type'))
  2513. {
  2514. case 'profile':
  2515. default:
  2516. $path = PATH_MBR_THEMES.$this->security->sanitize_filename($theme).'/'.$this->security->sanitize_filename($name);
  2517. }
  2518. if ( ! file_exists($path))
  2519. {
  2520. show_error($this->lang->line('unable_to_find_template_file'));
  2521. }
  2522. $this->load->helper('file');
  2523. if ( ! write_file($path, $template_data))
  2524. {
  2525. show_error($this->lang->line('error_opening_template'));
  2526. }
  2527. // Clear cache files
  2528. $this->functions->clear_caching('all');
  2529. $this->session->set_flashdata('message_success',$this->lang->line('template_updated'));
  2530. if ($this->input->get_post('update_and_return') !== FALSE)
  2531. {
  2532. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=list_'.$type.'_templates'.AMP.'name='.$theme);
  2533. }
  2534. // go back to the edit page for the appropriate area
  2535. $function = "edit_{$type}_template";
  2536. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M='.$function.AMP.'theme='.$theme.AMP.'name='.$name);
  2537. }
  2538. // --------------------------------------------------------------------
  2539. /**
  2540. * Manager
  2541. *
  2542. * Template Manager
  2543. *
  2544. * @access public
  2545. * @return type
  2546. */
  2547. function manager($message = '')
  2548. {
  2549. if ( ! $this->cp->allowed_group('can_access_design'))
  2550. {
  2551. show_error($this->lang->line('unauthorized_access'));
  2552. }
  2553. $this->load->model('design_model');
  2554. $this->cp->set_variable('cp_page_title', $this->lang->line('template_manager'));
  2555. $this->load->library('table');
  2556. $vars['can_admin_templates'] = $this->cp->allowed_group('can_admin_templates');
  2557. $this->jquery->tablesorter('.templateTable', '{
  2558. headers: {
  2559. 0: {sorter: false},
  2560. 1: {sorter: false},
  2561. 2: {sorter: false},
  2562. 3: {sorter: false},
  2563. 4: {sorter: false},
  2564. 5: {sorter: false}
  2565. },
  2566. widgets: ["zebra"]
  2567. }');
  2568. $this->jquery->plugin(BASE.AMP.'C=javascript'.AMP.'M=load'.AMP.'file=cp/manager', TRUE);
  2569. // auto scan for new templates
  2570. if ($this->config->item('save_tmpl_files') == 'y' && $this->config->item('tmpl_file_basepath') != '')
  2571. {
  2572. $this->_sync_from_files();
  2573. }
  2574. // template group query
  2575. // This query runs before the javascript so that we can determine
  2576. // the first template group that will be listed
  2577. $template_groups_query = $this->template_model->get_template_groups();
  2578. $vars['template_groups'] = $template_groups_query->result_array();
  2579. // if this isn't an admin, then unset any template
  2580. // groups they aren't allowed to admin
  2581. if ($this->session->userdata['group_id'] != 1)
  2582. {
  2583. foreach($vars['template_groups'] as $index => $group)
  2584. {
  2585. if ( ! array_key_exists($group['group_id'], $this->session->userdata['assigned_template_groups']))
  2586. {
  2587. unset($vars['template_groups'][$index]);
  2588. }
  2589. }
  2590. }
  2591. $this->javascript->set_global(array(
  2592. 'lang' => array('search_template' => $this->lang->line('search_template')))
  2593. );
  2594. $vars['message'] = $message;
  2595. $vars['default_group'] = '';
  2596. $vars['search_terms'] = FALSE;
  2597. $this->javascript->output('
  2598. // messages are hidden because they push the table out of the way with empty paragraphs
  2599. // but if there is a message, we need to show it.
  2600. if ("'.$message.'" != "")
  2601. {
  2602. $(".notice").show();
  2603. }
  2604. $("table").trigger("applyWidgets");
  2605. $(".groupList ul li a").each(function(){
  2606. var id = $(this).parent("li").attr("id");
  2607. // var group_id = id.replace(/template_group_/, ""); // different then id, used for just the number
  2608. // add edit group link into the list item
  2609. // $(this).parent("li").append("<a class=\"editTemplateGroup\" href=\"'.BASE.AMP.'C=design'.AMP.'M=edit_template_group'.AMP.'group_id="+group_id+" \">'.$this->lang->line('edit_template_group').'</a>");
  2610. // enable group switching
  2611. $(this).click(function() {
  2612. // change appearance in side bar
  2613. $(this).parent("li").addClass("selected").siblings("li").removeClass("selected");
  2614. $("#" + id + "_templates").show().siblings(":not(.linkBar)").hide();
  2615. // re-jig (yes, rejig is a word) the create new template link for the new selected group
  2616. // $("#new_template_create").attr("href", EE.BASE + "&C=design&M=new_template&group_id=" + group_id);
  2617. $("table").show().trigger("applyWidgets");
  2618. // Update the export group link
  2619. $("div.exportTemplateGroup a#export_group").attr("href", EE.BASE+"&C=design&M=export_templates&group_id="+id);
  2620. // do not follow any links
  2621. return false;
  2622. });
  2623. });
  2624. EE.template_edit_url = EE.BASE + "&C=design&M=template_edit_ajax";
  2625. EE.access_edit_url = EE.BASE + "&C=design&M=access_edit_ajax";
  2626. $(".show_prefs_link").click(function() {
  2627. id = $(this).attr("id").replace("show_prefs_link_","");
  2628. EE.manager.showPrefsRow(EE.pref_json[id], this);
  2629. return false;
  2630. });
  2631. $(".show_access_link").click(function() {
  2632. id = $(this).attr("id").replace("show_access_link_","");
  2633. EE.manager.showAccessRow(id, EE.pref_json[id], this);
  2634. return false;
  2635. });
  2636. ');
  2637. // reordering of template groups
  2638. $this->javascript->output('
  2639. $("#sortable_template_groups").sortable({
  2640. axis: "y",
  2641. update: function() {
  2642. $.ajax({
  2643. type: "POST",
  2644. url: EE.BASE + "&C=design&M=reorder_template_groups",
  2645. data: "is_ajax=true&XID="+ EE.XID + "&" + $("#sortable_template_groups").sortable("serialize")
  2646. });
  2647. }
  2648. });
  2649. ');
  2650. // load up the names too
  2651. foreach ($vars['template_groups'] as $groups)
  2652. {
  2653. $vars['groups'][$groups['group_id']] = $groups['group_name'];
  2654. // default group name
  2655. if ($groups['is_site_default'] == 'y')
  2656. {
  2657. $vars['default_group'] = $groups['group_name'];
  2658. }
  2659. }
  2660. // member group query
  2661. $this->db->select('group_id, group_title');
  2662. $this->db->where('site_id', $this->config->item('site_id'));
  2663. $this->db->where('group_id !=', '1');
  2664. $this->db->order_by('group_title');
  2665. $m_groups = $this->db->get('member_groups');
  2666. $vars['member_groups'] = array();
  2667. foreach($m_groups->result() as $m_group)
  2668. {
  2669. $vars['member_groups'][$m_group->group_id] = $m_group;
  2670. }
  2671. $hidden_indicator = ($this->config->item('hidden_template_indicator') != '') ? $this->config->item('hidden_template_indicator') : '.';
  2672. $hidden_indicator_length = strlen($hidden_indicator);
  2673. $query = $this->design_model->fetch_templates();
  2674. if ($query->num_rows() == 0)
  2675. {
  2676. if ($vars['search_terms'] != FALSE)
  2677. {
  2678. $vars['no_results'] = $this->lang->line('no_results');
  2679. }
  2680. else
  2681. {
  2682. $vars['no_results'] = $this->lang->line('no_templates_available');
  2683. }
  2684. }
  2685. // template access restrictions query
  2686. $denied_groups = $this->design_model->template_access_restrictions();
  2687. $vars['templates'] = array();
  2688. $displayed_groups = array();
  2689. foreach ($query->result_array() as $row)
  2690. {
  2691. $displayed_groups[$row['group_id']] = $row['group_id'];
  2692. $vars['templates'][$row['group_id']][$row['template_id']] = $row;
  2693. $vars['templates'][$row['group_id']][$row['template_id']]['hidden'] = (strncmp($row['template_name'], $hidden_indicator, $hidden_indicator_length) == 0) ? TRUE : FALSE;
  2694. if ($row['template_name'] == 'index')
  2695. {
  2696. $vars['templates'][$row['group_id']][$row['template_id']]['class'] = 'index';
  2697. }
  2698. elseif ($vars['templates'][$row['group_id']][$row['template_id']]['hidden'])
  2699. {
  2700. $vars['templates'][$row['group_id']][$row['template_id']]['class'] = 'hiddenTemplate '.$row['template_type'];
  2701. }
  2702. else
  2703. {
  2704. $vars['templates'][$row['group_id']][$row['template_id']]['class'] = $row['template_type'];
  2705. }
  2706. $vars['templates'][$row['group_id']][$row['template_id']]['view_path'] = $this->functions->fetch_site_index(0, 0).QUERY_MARKER.'URL='.$this->functions->fetch_site_index();
  2707. $vars['templates'][$row['group_id']][$row['template_id']]['view_path'] = rtrim($vars['templates'][$row['group_id']][$row['template_id']]['view_path'], '/').'/';
  2708. if ($vars['templates'][$row['group_id']][$row['template_id']]['template_type'] == 'css')
  2709. {
  2710. $vars['templates'][$row['group_id']][$row['template_id']]['view_path'] .= QUERY_MARKER.'css='.$vars['groups'][$row['group_id']].'/'.$vars['templates'][$row['group_id']][$row['template_id']]['template_name'];
  2711. }
  2712. else
  2713. {
  2714. $vars['templates'][$row['group_id']][$row['template_id']]['view_path'] .= $vars['groups'][$row['group_id']].(($vars['templates'][$row['group_id']][$row['template_id']]['template_name'] == 'index') ? '' : '/'.$vars['templates'][$row['group_id']][$row['template_id']]['template_name']);
  2715. }
  2716. // Access
  2717. foreach($vars['member_groups'] as $group_id => $group)
  2718. {
  2719. $vars['templates'][$row['group_id']][$row['template_id']]['access'][$group_id] = array(
  2720. 'id' => $group->group_id,
  2721. 'group_name' => $group->group_title,
  2722. 'access' => isset($denied_groups[$row['template_id']][$group_id]) ? FALSE : TRUE
  2723. );
  2724. }
  2725. }
  2726. // remove any template groups that aren't being displayed, as may be the case when a search was performed
  2727. foreach ($vars['template_groups'] as $index => $group)
  2728. {
  2729. if ( ! array_key_exists($group['group_id'], $displayed_groups))
  2730. {
  2731. unset($vars['template_groups'][$index]);
  2732. }
  2733. }
  2734. $vars['no_auth_bounce_options'] = array();
  2735. if ($this->cp->allowed_group('can_admin_templates'))
  2736. {
  2737. $query = $this->template_model->get_templates();
  2738. foreach ($query->result_array() as $row)
  2739. {
  2740. $vars['no_auth_bounce_options'][$row['template_id']] = $row['group_name'].'/'.$row['template_name'];
  2741. }
  2742. }
  2743. //$first_template = reset($vars['template_groups']->result_array());
  2744. $first_template = reset($vars['template_groups']);
  2745. $vars['first_template'] = $first_template['group_id'];
  2746. if ($this->input->get('tgpref'))
  2747. {
  2748. $vars['first_template'] = $this->input->get('tgpref');
  2749. }
  2750. $this->javascript->output('$("#template_group_'.$vars['first_template'].'").addClass("selected");');
  2751. $this->javascript->output('$("#template_group_'.$vars['first_template'].'_templates").show();');
  2752. $this->javascript->output(
  2753. '$("div.exportTemplateGroup a#export_group").attr("href", EE.BASE+"&C=design&M=export_templates&group_id=template_group_'.$vars['first_template'].'");
  2754. ');
  2755. $vars['table_template'] = array(
  2756. 'table_open' => '<table class="templateTable" border="0" cellspacing="0" cellpadding="0">'
  2757. );
  2758. $this->cp->set_right_nav($this->sub_breadcrumbs);
  2759. $this->javascript->compile();
  2760. $this->load->view('design/manager', $vars);
  2761. }
  2762. // --------------------------------------------------------------------
  2763. /**
  2764. * Export Template Group
  2765. *
  2766. * Export Template Group as a ZIP file
  2767. *
  2768. * @access public
  2769. * @return void
  2770. */
  2771. function export_templates()
  2772. {
  2773. if ( ! $this->cp->allowed_group('can_access_design') && ! $this->cp->allowed_group('can_admin_templates') )
  2774. {
  2775. show_error($this->lang->line('unauthorized_access'));
  2776. }
  2777. // Load the design model
  2778. $this->load->model('design_model');
  2779. $templates = $this->design_model->export_tmpl_group($this->input->get_post('group_id'));
  2780. $this->load->library('zip');
  2781. $site_name = $this->config->item('site_short_name');
  2782. foreach ($templates as $template)
  2783. {
  2784. // Create appropriate file extensions for each template
  2785. switch ($template['template_type'])
  2786. {
  2787. case 'xml':
  2788. $tmpl_ext = '.xml';
  2789. break;
  2790. case 'feed':
  2791. $tmpl_ext = '.xml';
  2792. break;
  2793. case 'css':
  2794. $tmpl_ext = '.css';
  2795. break;
  2796. case 'js':
  2797. $tmpl_ext = '.js';
  2798. break;
  2799. default:
  2800. $tmpl_ext = '.html';
  2801. }
  2802. $template_name = $site_name.'/'.$template['group_name'].'/'.$template['template_name'].$tmpl_ext;
  2803. $this->zip->add_data($template_name, $template['template_data']);
  2804. }
  2805. if ($this->input->get_post('group_id'))
  2806. {
  2807. $this->zip->download($site_name.'_'.$template['group_name'].'.zip');
  2808. }
  2809. else
  2810. {
  2811. $this->zip->download($site_name.'.zip');
  2812. }
  2813. $this->zip->clear_data();
  2814. exit();
  2815. }
  2816. // --------------------------------------------------------------------
  2817. /**
  2818. * Edit Template Ajax
  2819. *
  2820. * Used for inline editing of template prefs
  2821. *
  2822. * @access public
  2823. * @return type
  2824. */
  2825. function template_edit_ajax()
  2826. {
  2827. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  2828. {
  2829. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2830. }
  2831. $template_id = $this->input->get_post('template_id');
  2832. // check access privs
  2833. if ( ! $this->_template_access_privs(array('template_id' => $template_id)))
  2834. {
  2835. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2836. }
  2837. // Do we just have alpha num + . ?
  2838. if ($this->input->get_post('template_name'))
  2839. {
  2840. if ( ! preg_match("#^[a-zA-Z0-9_\.-]+$#i", $this->input->get_post('template_name')))
  2841. {
  2842. $this->output->send_ajax_response($this->lang->line('illegal_characters'), TRUE);
  2843. }
  2844. }
  2845. $this->output->enable_profiler(FALSE);
  2846. $data = array(
  2847. 'template_name' => $this->input->get_post('template_name'),
  2848. 'template_type' => ($this->input->get_post('template_type') == '') ? 'webpage' : $this->input->get_post('template_type'),
  2849. 'cache' => ($this->input->get_post('cache') == 'y') ? 'y' : 'n',
  2850. 'refresh' => ($this->input->get_post('refresh') == '') ? 0 : $this->input->get_post('refresh'),
  2851. 'allow_php' => ($this->input->get_post('allow_php') == 'y') ? 'y' : 'n',
  2852. 'php_parse_location' => ($this->input->get_post('php_parse_location') == 'i') ? 'i' : 'o',
  2853. 'hits' => $this->input->get_post('hits')
  2854. );
  2855. $this->db->select('template_name, template_type, save_template_file, group_name, templates.group_id');
  2856. $this->db->join('template_groups', 'template_groups.group_id = templates.group_id');
  2857. $this->db->where('template_id', $template_id);
  2858. $this->db->where('templates.site_id', $this->config->item('site_id'));
  2859. $query = $this->db->get('templates');
  2860. $template_info = $query->row_array();
  2861. // safety
  2862. if (count($template_info) == 0)
  2863. {
  2864. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2865. }
  2866. $rename_file = FALSE;
  2867. if ($data['template_name'] != $template_info['template_name'])
  2868. {
  2869. if ($template_info['template_name'] == 'index')
  2870. {
  2871. $this->output->send_ajax_response($this->lang->line('index_delete_disallowed'), TRUE);
  2872. }
  2873. $this->db->where('group_id', $template_info['group_id']);
  2874. $this->db->where('template_name', $data['template_name']);
  2875. // unique?
  2876. if ($this->db->count_all_results('templates'))
  2877. {
  2878. $this->output->send_ajax_response($this->lang->line('template_name_taken'), TRUE);
  2879. }
  2880. // reserved?
  2881. if (in_array($data['template_name'], $this->reserved_names))
  2882. {
  2883. $this->output->send_ajax_response($this->lang->line('reserved_name'), TRUE);
  2884. }
  2885. if ($template_info['save_template_file'] == 'y')
  2886. {
  2887. $rename_file = TRUE;
  2888. }
  2889. }
  2890. $trigger_preference_notice = FALSE;
  2891. // Update the template size?
  2892. if (is_numeric($this->input->get_post('template_size')))
  2893. {
  2894. if ($this->session->userdata['template_size'] != $this->input->get_post('template_size'))
  2895. {
  2896. $this->load->model('member_model');
  2897. $this->member_model->update_member($this->session->userdata('member_id'), array('template_size'=>$this->input->get_post('template_size')));
  2898. $this->session->userdata['template_size'] = $this->input->get_post('template_size');
  2899. $trigger_preference_notice = TRUE;
  2900. }
  2901. }
  2902. if ($this->template_model->update_template_ajax($template_id, $data) OR $trigger_preference_notice)
  2903. {
  2904. if ($rename_file === TRUE)
  2905. {
  2906. if ($this->template_model->rename_template_file($template_info['group_name'], $template_info['template_type'], $template_info['template_name'], $data['template_name']) == FALSE)
  2907. {
  2908. $this->output->send_ajax_response($this->lang->line('template_file_not_renamed'), TRUE);
  2909. }
  2910. }
  2911. $this->output->send_ajax_response($this->lang->line('preferences_updated'));
  2912. }
  2913. }
  2914. // --------------------------------------------------------------------
  2915. /**
  2916. * Access Template Ajax
  2917. *
  2918. * Used for inline editing of template access prefs
  2919. *
  2920. * @access public
  2921. * @return type
  2922. */
  2923. function access_edit_ajax()
  2924. {
  2925. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  2926. {
  2927. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2928. }
  2929. $template_id = $this->input->get_post("template_id");
  2930. if ( ! $this->_template_access_privs(array('template_id' => $template_id)))
  2931. {
  2932. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2933. }
  2934. $this->output->enable_profiler(FALSE);
  2935. if ($member_group = $this->input->get_post('member_group_id'))
  2936. {
  2937. $new_status = $this->input->get_post('new_status');
  2938. $no_auth_bounce = $this->input->get_post('no_auth_bounce');
  2939. if (($new_status != 'y' && $new_status != 'n') OR ! ctype_digit($no_auth_bounce))
  2940. {
  2941. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2942. }
  2943. $this->template_model->update_access_ajax($template_id, $member_group, $new_status);
  2944. $this->template_model->update_template_ajax($template_id, array('no_auth_bounce' => $no_auth_bounce));
  2945. }
  2946. elseif ($enable_http_auth = $this->input->get_post('enable_http_auth'))
  2947. {
  2948. if ($enable_http_auth != 'y' && $enable_http_auth != 'n')
  2949. {
  2950. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2951. }
  2952. $this->template_model->update_template_ajax($template_id, array('enable_http_auth' => $enable_http_auth));
  2953. }
  2954. elseif ($no_auth_bounce = $this->input->get_post('no_auth_bounce'))
  2955. {
  2956. if ( ! ctype_digit($no_auth_bounce))
  2957. {
  2958. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2959. }
  2960. $this->template_model->update_template_ajax($template_id, array('no_auth_bounce' => $no_auth_bounce));
  2961. }
  2962. else
  2963. {
  2964. $this->output->send_ajax_response($this->lang->line('unauthorized_access'), TRUE);
  2965. }
  2966. $this->output->send_ajax_response($this->lang->line('preferences_updated'));
  2967. }
  2968. // --------------------------------------------------------------------
  2969. /**
  2970. * Edit Template Group
  2971. *
  2972. * Edit a template group
  2973. *
  2974. * @access public
  2975. * @return type
  2976. */
  2977. function edit_template_group()
  2978. {
  2979. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  2980. {
  2981. show_error($this->lang->line('unauthorized_access'));
  2982. }
  2983. $group_id = $this->input->get_post("group_id");
  2984. if ($group_id == '')
  2985. {
  2986. return $this->template_group_pick(TRUE);
  2987. // $this->manager();
  2988. }
  2989. $this->load->helper('form');
  2990. $this->load->library('form_validation');
  2991. $this->load->library('table');
  2992. $group_info = $this->template_model->get_group_info($group_id);
  2993. $vars['group_name'] = $group_info->row('group_name');
  2994. $vars['is_default'] = ($group_info->row('is_site_default') == 'y') ? TRUE : FALSE;
  2995. $this->form_validation->set_rules('group_name', 'lang:group_name', 'required|callback__group_name_checks');
  2996. $this->form_validation->set_rules('is_site_default', 'lang:is_site_default', '');
  2997. $this->form_validation->set_error_delimiters('<br /><span class="notice">', '</span>');
  2998. if ($this->form_validation->run() === FALSE)
  2999. {
  3000. $this->cp->set_variable('cp_page_title', $this->lang->line('edit_template_group_form'));
  3001. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id, $this->lang->line('template_manager'));
  3002. $vars['form_hidden'] = array(
  3003. 'group_id' => $group_id,
  3004. 'old_name' => $vars['group_name']
  3005. );
  3006. $this->javascript->compile();
  3007. $this->load->view('design/edit_template_group', $vars);
  3008. }
  3009. else
  3010. {
  3011. $this->update_template_group();
  3012. }
  3013. }
  3014. // --------------------------------------------------------------------
  3015. /**
  3016. * Check Template Group Name
  3017. */
  3018. function _group_name_checks($str)
  3019. {
  3020. if ( ! preg_match("#^[a-zA-Z0-9_\-/]+$#i", $str))
  3021. {
  3022. $this->form_validation->set_message('_group_name_checks', $this->lang->line('illegal_characters'));
  3023. return FALSE;
  3024. }
  3025. if (in_array($str, $this->reserved_names))
  3026. {
  3027. $this->form_validation->set_message('_group_name_checks', $this->lang->line('reserved_name'));
  3028. return FALSE;
  3029. }
  3030. $this->db->select('count(*) as count');
  3031. $this->db->where('site_id', $this->config->item('site_id'));
  3032. $this->db->where('group_name', $str);
  3033. $query = $this->db->get('template_groups');
  3034. if ((strtolower($this->input->post('old_name')) != strtolower($str)) AND $query->row('count') > 0)
  3035. {
  3036. $this->form_validation->set_message('_group_name_checks', $this->lang->line('template_group_taken'));
  3037. return FALSE;
  3038. }
  3039. elseif ($query->row('count') > 1)
  3040. {
  3041. $this->form_validation->set_message('_group_name_checks', $this->lang->line('template_group_taken'));
  3042. return FALSE;
  3043. }
  3044. return TRUE;
  3045. }
  3046. /**
  3047. * Create/Update Template Group
  3048. */
  3049. function update_template_group()
  3050. {
  3051. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  3052. {
  3053. show_error($this->lang->line('unauthorized_access'));
  3054. }
  3055. $this->load->model('template_model');
  3056. $group_id = $this->input->get_post('group_id');
  3057. $group_name = $this->input->post('group_name');
  3058. $is_site_default = ($this->input->post('is_site_default') == 'y' ) ? 'y' : 'n';
  3059. if ($is_site_default == 'y')
  3060. {
  3061. $this->db->where('group_id !=', $group_id);
  3062. $this->db->where('site_id', $this->config->item('site_id'));
  3063. $this->db->set('is_site_default', 'n');
  3064. $this->db->update('exp_template_groups');
  3065. }
  3066. if ( ! $group_id)
  3067. {
  3068. $data = array(
  3069. 'group_name' => $group_name,
  3070. 'is_site_default' => $is_site_default,
  3071. 'site_id' => $this->config->item('site_id')
  3072. );
  3073. $group_id = $this->template_model->create_group($data);
  3074. $duplicate = FALSE;
  3075. if (is_numeric($_POST['duplicate_group']))
  3076. {
  3077. $query = $this->db->query("SELECT template_name, save_template_file, template_data, template_type, template_notes, cache, refresh, no_auth_bounce, allow_php, php_parse_location FROM exp_templates WHERE group_id = '".$this->db->escape_str($_POST['duplicate_group'])."'");
  3078. if ($query->num_rows() > 0)
  3079. {
  3080. $duplicate = TRUE;
  3081. }
  3082. }
  3083. if ( ! $duplicate)
  3084. {
  3085. $data = array(
  3086. 'group_id' => $group_id,
  3087. 'template_name' => 'index',
  3088. 'template_data' => '',
  3089. 'last_author_id' => 0,
  3090. 'edit_date' => $this->localize->now,
  3091. 'site_id' => $this->config->item('site_id')
  3092. );
  3093. $this->template_model->create_template($data);
  3094. }
  3095. else
  3096. {
  3097. foreach ($query->result_array() as $row)
  3098. {
  3099. $data = array(
  3100. 'group_id' => $group_id,
  3101. 'template_name' => $row['template_name'],
  3102. 'save_template_file' => $row['save_template_file'],
  3103. 'template_notes' => $row['template_notes'],
  3104. 'cache' => $row['cache'],
  3105. 'refresh' => $row['refresh'],
  3106. 'no_auth_bounce' => $row['no_auth_bounce'],
  3107. 'php_parse_location' => $row['php_parse_location'],
  3108. 'allow_php' => ($this->session->userdata['group_id'] == 1) ? $row['allow_php'] : 'n',
  3109. 'template_type' => $row['template_type'],
  3110. 'template_data' => $row['template_data'],
  3111. 'edit_date' => $this->localize->now,
  3112. 'last_author_id' => 0,
  3113. 'site_id' => $this->config->item('site_id')
  3114. );
  3115. $this->template_model->create_template($data);
  3116. }
  3117. }
  3118. $this->session->set_flashdata('message_success', $this->lang->line('template_group_created'));
  3119. }
  3120. else
  3121. {
  3122. // If the group name changed, check for templates saved as files
  3123. $old_name = $this->input->post('old_name');
  3124. if ($old_name != FALSE && $old_name != $group_name && $this->config->item('save_tmpl_files') == 'y')
  3125. {
  3126. $basepath = $this->config->slash_item('tmpl_file_basepath').'/'.$this->config->item('site_short_name').'/';
  3127. $old_dir = $basepath.$old_name.'.group/';
  3128. $new_dir = $basepath.$group_name.'.group/';
  3129. if (is_dir($old_dir) === TRUE && is_dir($new_dir) === FALSE)
  3130. {
  3131. rename($old_dir, $new_dir);
  3132. }
  3133. }
  3134. $fields = array(
  3135. 'group_name' => $group_name,
  3136. 'is_site_default' => $is_site_default,
  3137. 'group_id' => $group_id,
  3138. 'site_id' => $this->config->item('site_id')
  3139. );
  3140. $this->template_model->update_template_group($group_id, $fields);
  3141. $this->session->set_flashdata('message_success', $this->lang->line('template_group_updated'));
  3142. }
  3143. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=manager'.AMP.'tgpref='.$group_id);
  3144. }
  3145. // --------------------------------------------------------------------
  3146. /**
  3147. * Edit Template Group Order
  3148. *
  3149. * Create a new template gropu
  3150. *
  3151. * @access public
  3152. * @return type
  3153. */
  3154. function edit_template_group_order($message = '')
  3155. {
  3156. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  3157. {
  3158. show_error($this->lang->line('unauthorized_access'));
  3159. }
  3160. $this->load->helper('form');
  3161. $this->cp->set_variable('cp_page_title', $this->lang->line('edit_template_group_order'));
  3162. $this->javascript->output('
  3163. $("form label").css("cursor", "move");
  3164. $("form").sortable({
  3165. tolerance: "intersect",
  3166. items: "p",
  3167. axis: "y",
  3168. stop: function(event, ui) {
  3169. $("form p input[type=text]").each(function(i) {
  3170. $(this).val(i+1);
  3171. });
  3172. }
  3173. });
  3174. ');
  3175. $vars['message'] = $message;
  3176. $vars['form_hidden'] = array();
  3177. $vars['template_groups'] = $this->template_model->get_template_groups();
  3178. $this->javascript->compile();
  3179. $this->load->view('design/edit_template_group_order', $vars);
  3180. }
  3181. // --------------------------------------------------------------------
  3182. /**
  3183. * Reorder Template Groups
  3184. *
  3185. * Called over Ajax, where it re-orders the template groups
  3186. * behind the scenes
  3187. *
  3188. * @access public
  3189. * @return void
  3190. */
  3191. function reorder_template_groups()
  3192. {
  3193. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  3194. {
  3195. show_error($this->lang->line('unauthorized_access'));
  3196. }
  3197. $template_groups = $this->input->post('template_group');
  3198. if ($this->input->get_post('is_ajax') == 'true')
  3199. {
  3200. // Ajax request, no need to send them anywhere
  3201. foreach ($template_groups as $order=>$group)
  3202. {
  3203. $this->template_model->update_template_group($group, array('group_order' => $order));
  3204. }
  3205. return TRUE;
  3206. }
  3207. else
  3208. {
  3209. // "old fashioned" request, show them the template order page
  3210. $auto_order = 1;
  3211. foreach ($template_groups as $group=>$order)
  3212. {
  3213. $order = ( ! is_numeric($order)) ? $auto_order++ : $order;
  3214. $this->template_model->update_template_group($group, array('group_order' => $order));
  3215. }
  3216. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=edit_template_group_order');
  3217. }
  3218. }
  3219. /**
  3220. * Sync from files confirmation
  3221. *
  3222. * Confirm updating the database to match template files
  3223. *
  3224. * @access public
  3225. * @return void
  3226. */
  3227. function sync_templates($message = '')
  3228. {
  3229. if ( ! $this->cp->allowed_group('can_access_design') OR ! $this->cp->allowed_group('can_admin_templates'))
  3230. {
  3231. show_error($this->lang->line('unauthorized_access'));
  3232. }
  3233. $this->load->helper('form');
  3234. $this->load->library('table');
  3235. if ($this->config->item('save_tmpl_files') != 'y' OR $this->config->item('tmpl_file_basepath') == '')
  3236. {
  3237. $message = $this->lang->line('sync_not_allowed_1');
  3238. $message .= '<a href="'.str_replace('&amp;', '&', BASE).'&C=design&M=global_template_preferences">'.$this->lang->line('sync_not_allowed_2').'</a>';
  3239. }
  3240. $vars['table_template'] = array('table_open' => '<table id="entries" class="templateTable" border="0" cellspacing="0" cellpadding="0">',
  3241. 'row_start' => '<tr class="odd">',
  3242. 'row_end' => '</tr>',
  3243. 'cell_start' => '<td>',
  3244. 'cell_end' => '</td>',
  3245. 'row_alt_start' => '<tr>',
  3246. 'row_alt_end' => '</tr>',
  3247. 'cell_alt_start' => '<td>',
  3248. 'cell_alt_end' => '</td>',
  3249. );
  3250. if ( ! $this->cp->allowed_group('can_admin_templates'))
  3251. {
  3252. show_error($this->lang->line('unauthorized_access'));
  3253. }
  3254. // Add in new files
  3255. $this->_sync_from_files();
  3256. $vars['cp_page_title'] = $this->lang->line('sync_templates');
  3257. $this->cp->set_breadcrumb(BASE.AMP.'C=design'.AMP.'M=manager', $this->lang->line('template_manager'));
  3258. $this->javascript->output(array(
  3259. '$(".toggle_all").toggle(
  3260. function(){
  3261. $("input.toggle").each(function() {
  3262. this.checked = true;
  3263. });
  3264. }, function (){
  3265. var checked_status = this.checked;
  3266. $("input.toggle").each(function() {
  3267. this.checked = false;
  3268. });
  3269. }
  3270. );')
  3271. );
  3272. $vars['templates'] = array();
  3273. $vars['form_hidden']['confirm'] = 'confirm';
  3274. $this->load->library('api');
  3275. $this->api->instantiate('template_structure');
  3276. $this->load->helper('file');
  3277. $this->db->select(array('group_name', 'templates.group_id', 'template_name', 'template_type', 'template_id', 'edit_date'));
  3278. $this->db->join('template_groups', 'template_groups.group_id = templates.group_id');
  3279. $this->db->where('templates.site_id', $this->config->item('site_id'));
  3280. $this->db->where('save_template_file', 'y');
  3281. $this->db->order_by('group_name, template_name', 'ASC');
  3282. $query = $this->db->get('templates');
  3283. $date_fmt = ($this->session->userdata('time_format') != '') ? $this->session->userdata('time_format') : $this->config->item('time_format');
  3284. if ($date_fmt == 'us')
  3285. {
  3286. $datestr = '%m/%d/%y %h:%i %a';
  3287. }
  3288. else
  3289. {
  3290. $datestr = '%Y-%m-%d %H:%i';
  3291. }
  3292. $existing = array();
  3293. if ($query->num_rows() > 0)
  3294. {
  3295. foreach ($query->result() as $row)
  3296. {
  3297. // Skip groups they do not have access to
  3298. if ( ! $this->_template_access_privs(array('group_id' => $row->group_id)))
  3299. {
  3300. continue;
  3301. }
  3302. $edit_date = $this->localize->decode_date($datestr, $row->edit_date, TRUE);
  3303. $existing[$row->group_name][$row->template_name.$this->api_template_structure->file_extensions($row->template_type)] =
  3304. array('template_id' => $row->template_id,
  3305. 'edit_date' => $edit_date,
  3306. 'raw_edit_date' => $row->edit_date,
  3307. 'template_name' => $row->template_name,
  3308. 'file_name' => $row->template_name.$this->api_template_structure->file_extensions($row->template_type),
  3309. 'type' => $row->template_type,
  3310. 'file_edit' => '',
  3311. 'file_synced' => FALSE,
  3312. 'file_exists' => FALSE,
  3313. 'toggle' => form_checkbox('toggle[]', 'cf-'.$row->template_id, '', ' class="toggle" id="sync_box_'.$row->template_id.'"'));
  3314. }
  3315. }
  3316. $basepath = $this->config->slash_item('tmpl_file_basepath');
  3317. $basepath .= '/'.$this->config->item('site_short_name');
  3318. $this->load->helper('directory');
  3319. $files = directory_map($basepath, 0, 1);
  3320. if ($files !== FALSE)
  3321. {
  3322. foreach ($files as $group => $templates)
  3323. {
  3324. if (substr($group, -6) != '.group')
  3325. {
  3326. continue;
  3327. }
  3328. $group_name = substr($group, 0, -6); // remove .group
  3329. foreach ($templates as $template)
  3330. {
  3331. if (is_array($template))
  3332. {
  3333. continue;
  3334. }
  3335. $file_date = get_file_info($basepath.'/'.$group.'/'.$template);
  3336. $file_date = ($file_date === FALSE) ? $file_date : $file_date['date'];
  3337. if (isset($existing[$group_name][$template]))
  3338. {
  3339. $existing[$group_name][$template]['file_exists'] = TRUE;
  3340. if ($existing[$group_name][$template]['raw_edit_date'] >= $file_date)
  3341. {
  3342. $existing[$group_name][$template]['file_synced'] = TRUE;
  3343. $existing[$group_name][$template]['toggle'] = '';
  3344. }
  3345. $existing[$group_name][$template]['file_edit'] = $this->localize->decode_date($datestr, $file_date, TRUE);
  3346. $existing[$group_name][$template]['file_name'] = $template;
  3347. $existing[$group_name][$template]['toggle'] = form_checkbox('toggle[]', $existing[$group_name][$template]['template_id'], '', ' class="toggle" id="sync_box_'.$existing[$group_name][$template]['template_id'].'"');
  3348. }
  3349. }
  3350. }
  3351. }
  3352. if ($message == '' && count($existing) == 0)
  3353. {
  3354. $message = $this->lang->line('no_valid_templates_sync');
  3355. }
  3356. $vars['message'] = $message;
  3357. $vars['templates'] = $existing;
  3358. $this->javascript->compile();
  3359. $this->load->view('design/sync_confirm', $vars);
  3360. }
  3361. /**
  3362. * Sync data from files
  3363. *
  3364. * Update database to match current template files
  3365. *
  3366. * @access public
  3367. * @return void
  3368. */
  3369. function sync_run()
  3370. {
  3371. if ( ! $this->cp->allowed_group('can_access_design'))
  3372. {
  3373. show_error($this->lang->line('unauthorized_access'));
  3374. }
  3375. $message = '';
  3376. if ($this->config->item('save_tmpl_files') != 'y' OR $this->config->item('tmpl_file_basepath') == '')
  3377. {
  3378. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=sync_templates');
  3379. }
  3380. if ( ! $this->cp->allowed_group('can_admin_templates'))
  3381. {
  3382. show_error($this->lang->line('unauthorized_access'));
  3383. }
  3384. if ( ! $confirmed = $this->input->get_post('confirm') OR $confirmed != 'confirm')
  3385. {
  3386. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=sync_templates');
  3387. }
  3388. if ( ! $this->input->post('toggle') OR ! is_array($this->input->post('toggle')))
  3389. {
  3390. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=sync_templates');
  3391. }
  3392. $damned = array();
  3393. $create_files = array();
  3394. foreach ($_POST['toggle'] as $key => $val)
  3395. {
  3396. if (strncmp($val, 'cf-', 3) == 0)
  3397. {
  3398. $create_files[] = substr($val, 3);
  3399. $damned[] = substr($val, 3);
  3400. }
  3401. else
  3402. {
  3403. $damned[] = $val;
  3404. }
  3405. }
  3406. $save_result = FALSE;
  3407. // If we need to create files, we do it now.
  3408. if (count($create_files) > 0)
  3409. {
  3410. $this->db->select(array('group_name', 'template_name', 'template_type', 'template_id', 'edit_date', 'template_data'));
  3411. $this->db->join('template_groups', 'template_groups.group_id = templates.group_id');
  3412. $this->db->where('templates.site_id', $this->config->item('site_id'));
  3413. $this->db->where('save_template_file', 'y');
  3414. $this->db->where_in('template_id', $create_files);
  3415. $this->db->order_by('group_name, template_name', 'ASC');
  3416. $query = $this->db->get('templates');
  3417. if ($query->num_rows() > 0)
  3418. {
  3419. foreach ($query->result() as $row)
  3420. {
  3421. $tdata = array(
  3422. 'site_short_name' => $this->config->item('site_short_name'),
  3423. 'template_id' => $row->template_id,
  3424. 'template_group' => $row->group_name,
  3425. 'template_name' => $row->template_name,
  3426. 'template_type' => $row->template_type,
  3427. 'template_data' => $row->template_data,
  3428. 'edit_date' => $this->localize->now,
  3429. 'last_author_id' => $this->session->userdata['member_id']
  3430. );
  3431. $save_result = $this->update_template_file($tdata);
  3432. if ($save_result == FALSE)
  3433. {
  3434. show_error($this->lang->line('template_not_saved'));
  3435. }
  3436. }
  3437. }
  3438. // Annoying. This would cut down on overhead and eliminate need to include these in the following processing.
  3439. // UPDATE exp_templates SET edit_date = $this->localize->now WHERE template_id IN ($create_files)
  3440. }
  3441. $this->load->library('api');
  3442. $this->api->instantiate('template_structure');
  3443. $this->load->helper('file');
  3444. $this->db->select(array('group_name', 'templates.group_id', 'template_name', 'template_type', 'template_id', 'edit_date'));
  3445. $this->db->join('template_groups', 'template_groups.group_id = templates.group_id');
  3446. $this->db->where('templates.site_id', $this->config->item('site_id'));
  3447. $this->db->where('save_template_file', 'y');
  3448. $this->db->where_in('template_id', $damned);
  3449. $this->db->order_by('group_name, template_name', 'ASC');
  3450. $query = $this->db->get('templates');
  3451. $existing = array();
  3452. if ($query->num_rows() > 0)
  3453. {
  3454. foreach ($query->result() as $row)
  3455. {
  3456. // Skip groups they do not have access to
  3457. if ( ! $this->_template_access_privs(array('group_id' => $row->group_id)))
  3458. {
  3459. continue;
  3460. }
  3461. $existing[$row->group_name.'.group'][$row->template_name.$this->api_template_structure->file_extensions($row->template_type)] =
  3462. array($row->group_id,
  3463. $row->template_id,
  3464. $row->edit_date,
  3465. $row->template_name,
  3466. $row->template_type
  3467. );
  3468. }
  3469. }
  3470. $query->free_result();
  3471. $basepath = $this->config->slash_item('tmpl_file_basepath');
  3472. $basepath .= '/'.$this->config->item('site_short_name');
  3473. $this->load->helper('directory');
  3474. $files = directory_map($basepath, 0, 1);
  3475. $save_revisions = $this->config->item('save_tmpl_revisions');
  3476. $maxrev = $this->config->item('max_tmpl_revisions');
  3477. if ($files !== FALSE)
  3478. {
  3479. foreach ($files as $group => $templates)
  3480. {
  3481. if (substr($group, -6) != '.group')
  3482. {
  3483. continue;
  3484. }
  3485. $group_name = substr($group, 0, -6); // remove .group
  3486. // update existing templates
  3487. foreach ($templates as $template)
  3488. {
  3489. if (is_array($template))
  3490. {
  3491. continue;
  3492. }
  3493. if ( isset($existing[$group][$template]))
  3494. {
  3495. $edit_date = $existing[$group][$template]['2'];
  3496. $file_date = get_file_info($basepath.'/'.$group.'/'.$template);
  3497. if (($file_date !== FALSE) && ($file_date['date'] < $edit_date))
  3498. {
  3499. continue;
  3500. }
  3501. $contents = file_get_contents($basepath.'/'.$group.'/'.$template);
  3502. if ($contents !== FALSE)
  3503. {
  3504. $data = array(
  3505. 'group_id' => $existing[$group][$template]['0'],
  3506. 'template_name' => $existing[$group][$template]['3'],
  3507. 'template_type' => $existing[$group][$template]['4'],
  3508. 'template_data' => $contents,
  3509. 'edit_date' => $this->localize->now,
  3510. 'save_template_file' => 'y',
  3511. 'last_author_id' => $this->session->userdata['member_id'],
  3512. 'site_id' => $this->config->item('site_id')
  3513. );
  3514. $this->db->where('template_id', $existing[$group][$template]['1']);
  3515. $this->db->update('templates', $data);
  3516. // Revision tracking
  3517. if ($save_revisions == 'y')
  3518. {
  3519. $data = array(
  3520. 'item_id' => $existing[$group][$template]['1'],
  3521. 'item_table' => 'exp_templates',
  3522. 'item_field' => 'template_data',
  3523. 'item_data' => $contents,
  3524. 'item_date' => $this->localize->now,
  3525. 'item_author_id' => $this->session->userdata['member_id']
  3526. );
  3527. $this->db->insert('revision_tracker', $data);
  3528. // Cull revisions
  3529. if ($maxrev != '' AND is_numeric($maxrev) AND $maxrev > 0)
  3530. {
  3531. $this->db->select('tracker_id');
  3532. $this->db->where('item_id', $existing[$group][$template]['1']);
  3533. $this->db->where('item_table', 'exp_templates');
  3534. $this->db->where('item_field', 'template_data');
  3535. $this->db->order_by("tracker_id", "desc");
  3536. $res = $this->db->get('revision_tracker');
  3537. if ($res->num_rows() > 0 AND $res->num_rows() > $maxrev)
  3538. {
  3539. $flag = '';
  3540. $ct = 1;
  3541. foreach ($res->result_array() as $row)
  3542. {
  3543. if ($ct >= $maxrev)
  3544. {
  3545. $flag = $row['tracker_id'];
  3546. break;
  3547. }
  3548. $ct++;
  3549. }
  3550. if ($flag != '')
  3551. {
  3552. $this->db->where('tracker_id <', $flag);
  3553. $this->db->where('item_id', $existing[$group][$template]['1']);
  3554. $this->db->where('item_table', 'exp_templates');
  3555. $this->db->where('item_field', 'template_data');
  3556. $this->db->delete('revision_tracker');
  3557. }
  3558. }
  3559. }
  3560. }
  3561. }
  3562. unset($existing[$group][$template]);
  3563. }
  3564. }
  3565. }
  3566. }
  3567. $this->functions->clear_caching('all');
  3568. $message = $this->lang->line('sync_completed');
  3569. $this->session->set_flashdata('message_success', $message);
  3570. $this->functions->redirect(BASE.AMP.'C=design'.AMP.'M=sync_templates');
  3571. }
  3572. /**
  3573. * Sync from files
  3574. *
  3575. * Reads the template file directory and
  3576. * automatically creates new groups and templates as necessary
  3577. *
  3578. * @access public
  3579. * @return void
  3580. */
  3581. function _sync_from_files()
  3582. {
  3583. if ($this->config->item('save_tmpl_files') != 'y' OR $this->config->item('tmpl_file_basepath') == '')
  3584. {
  3585. return FALSE;
  3586. }
  3587. $this->load->library('api');
  3588. $this->api->instantiate('template_structure');
  3589. $this->db->select(array('group_name', 'template_name', 'template_type', 'save_template_file'));
  3590. $this->db->join('template_groups', 'template_groups.group_id = templates.group_id');
  3591. $this->db->where('templates.site_id', $this->config->item('site_id'));
  3592. $this->db->order_by('group_name, template_name', 'ASC');
  3593. $query = $this->db->get('templates');
  3594. $existing = array();
  3595. if ($query->num_rows() > 0)
  3596. {
  3597. foreach ($query->result() as $row)
  3598. {
  3599. $existing[$row->group_name.'.group'][$row->template_name] = $row->save_template_file;
  3600. }
  3601. }
  3602. $basepath = $this->config->slash_item('tmpl_file_basepath');
  3603. $basepath .= '/'.$this->config->item('site_short_name');
  3604. $this->load->helper('directory');
  3605. $files = directory_map($basepath, 0, 1);
  3606. if ($files !== FALSE)
  3607. {
  3608. foreach ($files as $group => $templates)
  3609. {
  3610. if (substr($group, -6) != '.group')
  3611. {
  3612. continue;
  3613. }
  3614. $group_name = substr($group, 0, -6); // remove .group
  3615. $group_id = '';
  3616. if ( ! preg_match("#^[a-zA-Z0-9_\-]+$#i", $group_name))
  3617. {
  3618. continue;
  3619. }
  3620. // if the template group doesn't exist, make it!
  3621. if ( ! isset($existing[$group]))
  3622. {
  3623. if ( ! $this->api->is_url_safe($group_name))
  3624. {
  3625. show_error($this->lang->line('illegal_characters').NBS.NBS.'a'.htmlentities($group_name));
  3626. }
  3627. if (in_array($group_name, $this->reserved_names))
  3628. {
  3629. show_error($this->lang->line('reserved_name').NBS.NBS.htmlentities($group_name));
  3630. }
  3631. $data = array(
  3632. 'group_name' => $group_name,
  3633. 'is_site_default' => 'n',
  3634. 'site_id' => $this->config->item('site_id')
  3635. );
  3636. $group_id = $this->template_model->create_group($data);
  3637. }
  3638. // Grab group_id if we still don't have it.
  3639. if ($group_id == '')
  3640. {
  3641. $this->db->select('group_id');
  3642. $this->db->where('group_name', $group_name);
  3643. $this->db->where('site_id', $this->config->item('site_id'));
  3644. $query = $this->db->get('template_groups');
  3645. $group_id = $query->row('group_id');
  3646. }
  3647. // if the templates don't exist, make 'em!
  3648. foreach ($templates as $template)
  3649. {
  3650. // Skip subdirectories (such as those created by svn)
  3651. if (is_array($template))
  3652. {
  3653. continue;
  3654. }
  3655. // If the last occurance is the first position? We skip that too.
  3656. if (strrpos($template, '.') == FALSE)
  3657. {
  3658. continue;
  3659. }
  3660. $ext = strtolower(ltrim(strrchr($template, '.'), '.'));
  3661. if ( ! in_array('.'.$ext, $this->api_template_structure->file_extensions))
  3662. {
  3663. continue;
  3664. }
  3665. $ext_length = strlen($ext)+1;
  3666. $template_name = substr($template, 0, -$ext_length);
  3667. $template_type = array_search('.'.$ext, $this->api_template_structure->file_extensions);
  3668. if (isset($existing[$group][$template_name]))
  3669. {
  3670. continue;
  3671. }
  3672. if ( ! $this->api->is_url_safe($template_name))
  3673. {
  3674. show_error($this->lang->line('illegal_characters').NBS.NBS.htmlentities($template_name));
  3675. }
  3676. $data = array(
  3677. 'group_id' => $group_id,
  3678. 'template_name' => $template_name,
  3679. 'template_type' => $template_type,
  3680. 'template_data' => file_get_contents($basepath.'/'.$group.'/'.$template),
  3681. 'edit_date' => $this->localize->now,
  3682. 'save_template_file' => 'y',
  3683. 'last_author_id' => $this->session->userdata['member_id'],
  3684. 'site_id' => $this->config->item('site_id')
  3685. );
  3686. // do it!
  3687. $this->template_model->create_template($data);
  3688. // add to existing array so we don't try to create this template again
  3689. $existing[$group][$template_name] = 'y';
  3690. }
  3691. // An index template is required- so we create it if necessary
  3692. if ( ! isset($existing[$group]['index']))
  3693. {
  3694. $data = array(
  3695. 'group_id' => $group_id,
  3696. 'template_name' => 'index',
  3697. 'template_data' => '',
  3698. 'edit_date' => $this->localize->now,
  3699. 'save_template_file' => 'y',
  3700. 'last_author_id' => $this->session->userdata['member_id'],
  3701. 'site_id' => $this->config->item('site_id')
  3702. );
  3703. $this->template_model->create_template($data);
  3704. }
  3705. unset($existing[$group]);
  3706. }
  3707. }
  3708. }
  3709. }
  3710. /* End of file design.php */
  3711. /* Location: ./system/expressionengine/controllers/cp/design.php */