6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
32 function main($id, $mode)
34 global $db, $user, $auth, $template, $cache;
35 global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
37 // Hardcoded template bitfield to add for new templates
38 $bitfield = new bitfield();
48 define('TEMPLATE_BITFIELD', $bitfield->get_base64());
51 $user->add_lang('acp/styles');
53 $this->tpl_name = 'acp_styles';
54 $this->page_title = 'ACP_CAT_STYLES';
56 $action = request_var('action', '');
57 $action = (isset($_POST['add'])) ? 'add' : $action;
58 $style_id = request_var('id', 0);
60 // Fill the configuration variables
61 $this->style_cfg = $this->template_cfg = $this->theme_cfg = $this->imageset_cfg = '
63 # phpBB {MODE} configuration file
66 # @copyright (c) 2005 phpBB Group
67 # @license http://opensource.org/licenses/gpl-license.php GNU Public License
70 # At the left is the name, please do not change this
71 # At the right the value is entered
72 # For on/off options the valid values are on, off, 1, 0, true and false
74 # Values get trimmed, if you want to add a space in front or at the end of
75 # the value, then enclose the value with single or double quotes.
76 # Single and double quotes do not need to be escaped.
80 # General Information about this {MODE}
82 copyright = {COPYRIGHT}
87 # Some configuration options
90 # You have to turn this option on if you want to use the
91 # path template variables ({T_IMAGESET_PATH} for example) within
93 # This is mostly the case if you want to use language specific
94 # images within your css file.
96 parse_css_file = {PARSE_CSS_FILE}
99 $this->template_cfg .= '
100 # Some configuration options
103 # You can use this function to inherit templates from another template.
104 # The template of the given name has to be installed.
105 # Templates cannot inherit from inheriting templates.
108 $this->imageset_keys = array(
113 'icon_back_top', 'icon_contact_aim', 'icon_contact_email', 'icon_contact_icq', 'icon_contact_jabber', 'icon_contact_msnm', 'icon_contact_pm', 'icon_contact_yahoo', 'icon_contact_www', 'icon_post_delete', 'icon_post_edit', 'icon_post_info', 'icon_post_quote', 'icon_post_report', 'icon_user_online', 'icon_user_offline', 'icon_user_profile', 'icon_user_search', 'icon_user_warn', 'button_pm_forward', 'button_pm_new', 'button_pm_reply', 'button_topic_locked', 'button_topic_new', 'button_topic_reply',
116 'icon_post_target', 'icon_post_target_unread', 'icon_topic_attach', 'icon_topic_latest', 'icon_topic_newest', 'icon_topic_reported', 'icon_topic_unapproved', 'icon_friend', 'icon_foe',
119 'forum_link', 'forum_read', 'forum_read_locked', 'forum_read_subforum', 'forum_unread', 'forum_unread_locked', 'forum_unread_subforum', 'subforum_read', 'subforum_unread'
122 'topic_moved', 'topic_read', 'topic_read_mine', 'topic_read_hot', 'topic_read_hot_mine', 'topic_read_locked', 'topic_read_locked_mine', 'topic_unread', 'topic_unread_mine', 'topic_unread_hot', 'topic_unread_hot_mine', 'topic_unread_locked', 'topic_unread_locked_mine', 'sticky_read', 'sticky_read_mine', 'sticky_read_locked', 'sticky_read_locked_mine', 'sticky_unread', 'sticky_unread_mine', 'sticky_unread_locked', 'sticky_unread_locked_mine', 'announce_read', 'announce_read_mine', 'announce_read_locked', 'announce_read_locked_mine', 'announce_unread', 'announce_unread_mine', 'announce_unread_locked', 'announce_unread_locked_mine', 'global_read', 'global_read_mine', 'global_read_locked', 'global_read_locked_mine', 'global_unread', 'global_unread_mine', 'global_unread_locked', 'global_unread_locked_mine', 'pm_read', 'pm_unread',
125 'poll_left', 'poll_center', 'poll_right',
131 'user_icon1', 'user_icon2', 'user_icon3', 'user_icon4', 'user_icon5', 'user_icon6', 'user_icon7', 'user_icon8', 'user_icon9', 'user_icon10',
135 // Execute overall actions
141 $this->remove($mode, $style_id);
149 $this->export($mode, $style_id);
155 $this->install($mode);
167 $this->details($mode, $style_id);
178 return $this->edit_imageset($style_id);
180 return $this->edit_template($style_id);
182 return $this->edit_theme($style_id);
193 return $this->template_cache($style_id);
208 if ($style_id == $config['default_style'])
210 trigger_error($user->lang['DEACTIVATE_DEFAULT'] . adm_back_link($this->u_action), E_USER_WARNING);
213 if (($action == 'deactivate' && confirm_box(true)) || $action == 'activate')
215 $sql = 'UPDATE ' . STYLES_TABLE . '
216 SET style_active = ' . (($action == 'activate') ? 1 : 0) . '
217 WHERE style_id = ' . $style_id;
218 $db->sql_query($sql);
220 // Set style to default for any member using deactivated style
221 if ($action == 'deactivate')
223 $sql = 'UPDATE ' . USERS_TABLE . '
224 SET user_style = ' . $config['default_style'] . "
225 WHERE user_style = $style_id";
226 $db->sql_query($sql);
228 $sql = 'UPDATE ' . FORUMS_TABLE . '
230 WHERE forum_style = ' . $style_id;
231 $db->sql_query($sql);
234 else if ($action == 'deactivate')
236 $s_hidden_fields = array(
240 'style_id' => $style_id,
242 confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields($s_hidden_fields));
247 $this->frontend('style', array('details'), array('export', 'delete'));
254 // Refresh template data stored in db and clear cache
258 FROM ' . STYLES_TEMPLATE_TABLE . "
259 WHERE template_id = $style_id";
260 $result = $db->sql_query($sql);
261 $template_row = $db->sql_fetchrow($result);
262 $db->sql_freeresult($result);
266 trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
269 if (confirm_box(true))
271 $template_refreshed = '';
273 // Only refresh database if the template is stored in the database
274 if ($template_row['template_storedb'] && file_exists("{$phpbb_root_path}styles/{$template_row['template_path']}/template/"))
276 $filelist = array('' => array());
278 $sql = 'SELECT template_filename, template_mtime
279 FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
280 WHERE template_id = $style_id";
281 $result = $db->sql_query($sql);
283 while ($row = $db->sql_fetchrow($result))
285 // if (@filemtime("{$phpbb_root_path}styles/{$template_row['template_path']}/template/" . $row['template_filename']) > $row['template_mtime'])
287 // get folder info from the filename
288 if (($slash_pos = strrpos($row['template_filename'], '/')) === false)
290 $filelist[''][] = $row['template_filename'];
294 $filelist[substr($row['template_filename'], 0, $slash_pos + 1)][] = substr($row['template_filename'], $slash_pos + 1, strlen($row['template_filename']) - $slash_pos - 1);
298 $db->sql_freeresult($result);
300 $this->store_templates('update', $style_id, $template_row['template_path'], $filelist);
303 $template_refreshed = $user->lang['TEMPLATE_REFRESHED'] . '<br />';
304 add_log('admin', 'LOG_TEMPLATE_REFRESHED', $template_row['template_name']);
307 $this->clear_template_cache($template_row);
309 trigger_error($template_refreshed . $user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action));
313 confirm_box(false, ($template_row['template_storedb']) ? $user->lang['CONFIRM_TEMPLATE_REFRESH'] : $user->lang['CONFIRM_TEMPLATE_CLEAR_CACHE'], build_hidden_fields(array(
324 $this->frontend('template', array('edit', 'cache', 'details'), array('refresh', 'export', 'delete'));
331 // Refresh theme data stored in the database
335 FROM ' . STYLES_THEME_TABLE . "
336 WHERE theme_id = $style_id";
337 $result = $db->sql_query($sql);
338 $theme_row = $db->sql_fetchrow($result);
339 $db->sql_freeresult($result);
343 trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
346 if (!$theme_row['theme_storedb'])
348 trigger_error($user->lang['THEME_ERR_REFRESH_FS'] . adm_back_link($this->u_action), E_USER_WARNING);
351 if (confirm_box(true))
353 if ($theme_row['theme_storedb'] && file_exists("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"))
357 'theme_mtime' => (int) filemtime("{$phpbb_root_path}styles/{$theme_row['theme_path']}/theme/stylesheet.css"),
358 'theme_data' => $this->db_theme_data($theme_row)
361 $sql = 'UPDATE ' . STYLES_THEME_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
362 WHERE theme_id = $style_id";
363 $db->sql_query($sql);
365 $cache->destroy('sql', STYLES_THEME_TABLE);
367 add_log('admin', 'LOG_THEME_REFRESHED', $theme_row['theme_name']);
368 trigger_error($user->lang['THEME_REFRESHED'] . adm_back_link($this->u_action));
373 confirm_box(false, $user->lang['CONFIRM_THEME_REFRESH'], build_hidden_fields(array(
383 $this->frontend('theme', array('edit', 'details'), array('refresh', 'export', 'delete'));
393 FROM ' . STYLES_IMAGESET_TABLE . "
394 WHERE imageset_id = $style_id";
395 $result = $db->sql_query($sql);
396 $imageset_row = $db->sql_fetchrow($result);
397 $db->sql_freeresult($result);
401 trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
404 if (confirm_box(true))
408 $imageset_definitions = array();
409 foreach ($this->imageset_keys as $topic => $key_array)
411 $imageset_definitions = array_merge($imageset_definitions, $key_array);
414 $cfg_data_imageset = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/imageset.cfg");
416 $db->sql_transaction('begin');
418 $sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . '
419 WHERE imageset_id = ' . $style_id;
420 $result = $db->sql_query($sql);
422 foreach ($cfg_data_imageset as $image_name => $value)
424 if (strpos($value, '*') !== false)
426 if (substr($value, -1, 1) === '*')
428 list($image_filename, $image_height) = explode('*', $value);
433 list($image_filename, $image_height, $image_width) = explode('*', $value);
438 $image_filename = $value;
439 $image_height = $image_width = 0;
442 if (strpos($image_name, 'img_') === 0 && $image_filename)
444 $image_name = substr($image_name, 4);
445 if (in_array($image_name, $imageset_definitions))
448 'image_name' => (string) $image_name,
449 'image_filename' => (string) $image_filename,
450 'image_height' => (int) $image_height,
451 'image_width' => (int) $image_width,
452 'imageset_id' => (int) $style_id,
459 $sql = 'SELECT lang_dir
461 $result = $db->sql_query($sql);
463 while ($row = $db->sql_fetchrow($result))
465 if (@file_exists("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg"))
467 $cfg_data_imageset_data = parse_cfg_file("{$phpbb_root_path}styles/{$imageset_row['imageset_path']}/imageset/{$row['lang_dir']}/imageset.cfg");
468 foreach ($cfg_data_imageset_data as $image_name => $value)
470 if (strpos($value, '*') !== false)
472 if (substr($value, -1, 1) === '*')
474 list($image_filename, $image_height) = explode('*', $value);
479 list($image_filename, $image_height, $image_width) = explode('*', $value);
484 $image_filename = $value;
485 $image_height = $image_width = 0;
488 if (strpos($image_name, 'img_') === 0 && $image_filename)
490 $image_name = substr($image_name, 4);
491 if (in_array($image_name, $imageset_definitions))
494 'image_name' => (string) $image_name,
495 'image_filename' => (string) $image_filename,
496 'image_height' => (int) $image_height,
497 'image_width' => (int) $image_width,
498 'imageset_id' => (int) $style_id,
499 'image_lang' => (string) $row['lang_dir'],
506 $db->sql_freeresult($result);
508 $db->sql_multi_insert(STYLES_IMAGESET_DATA_TABLE, $sql_ary);
510 $db->sql_transaction('commit');
512 $cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
513 $cache->destroy('imageset_site_logo_md5');
515 add_log('admin', 'LOG_IMAGESET_REFRESHED', $imageset_row['imageset_name']);
516 trigger_error($user->lang['IMAGESET_REFRESHED'] . adm_back_link($this->u_action));
520 confirm_box(false, $user->lang['CONFIRM_IMAGESET_REFRESH'], build_hidden_fields(array(
530 $this->frontend('imageset', array('edit', 'details'), array('refresh', 'export', 'delete'));
536 * Build Frontend with supplied options
538 function frontend($mode, $options, $actions)
540 global $user, $template, $db, $config, $phpbb_root_path, $phpEx;
543 $style_count = array();
548 $sql_from = STYLES_TABLE;
550 $sql = 'SELECT user_style, COUNT(user_style) AS style_count
551 FROM ' . USERS_TABLE . '
552 GROUP BY user_style';
553 $result = $db->sql_query($sql);
555 while ($row = $db->sql_fetchrow($result))
557 $style_count[$row['user_style']] = $row['style_count'];
559 $db->sql_freeresult($result);
564 $sql_from = STYLES_TEMPLATE_TABLE;
568 $sql_from = STYLES_THEME_TABLE;
572 $sql_from = STYLES_IMAGESET_TABLE;
576 $l_prefix = strtoupper($mode);
578 $this->page_title = 'ACP_' . $l_prefix . 'S';
580 $template->assign_vars(array(
581 'S_FRONTEND' => true,
582 'S_STYLE' => ($mode == 'style') ? true : false,
584 'L_TITLE' => $user->lang[$this->page_title],
585 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
586 'L_NAME' => $user->lang[$l_prefix . '_NAME'],
587 'L_INSTALLED' => $user->lang['INSTALLED_' . $l_prefix],
588 'L_UNINSTALLED' => $user->lang['UNINSTALLED_' . $l_prefix],
589 'L_NO_UNINSTALLED' => $user->lang['NO_UNINSTALLED_' . $l_prefix],
590 'L_CREATE' => $user->lang['CREATE_' . $l_prefix],
592 'U_ACTION' => $this->u_action,
598 $result = $db->sql_query($sql);
600 $installed = array();
602 $basis_options = '<option class="sep" value="">' . $user->lang['OPTIONAL_BASIS'] . '</option>';
603 while ($row = $db->sql_fetchrow($result))
605 $installed[] = $row[$mode . '_name'];
606 $basis_options .= '<option value="' . $row[$mode . '_id'] . '">' . $row[$mode . '_name'] . '</option>';
608 $stylevis = ($mode == 'style' && !$row['style_active']) ? 'activate' : 'deactivate';
610 $s_options = array();
611 foreach ($options as $option)
613 $s_options[] = '<a href="' . $this->u_action . "&action=$option&id=" . $row[$mode . '_id'] . '">' . $user->lang[strtoupper($option)] . '</a>';
616 $s_actions = array();
617 foreach ($actions as $option)
619 $s_actions[] = '<a href="' . $this->u_action . "&action=$option&id=" . $row[$mode . '_id'] . '">' . $user->lang[strtoupper($option)] . '</a>';
622 $template->assign_block_vars('installed', array(
623 'S_DEFAULT_STYLE' => ($mode == 'style' && $row['style_id'] == $config['default_style']) ? true : false,
624 'U_EDIT' => $this->u_action . '&action=' . (($mode == 'style') ? 'details' : 'edit') . '&id=' . $row[$mode . '_id'],
625 'U_STYLE_ACT_DEACT' => $this->u_action . '&action=' . $stylevis . '&id=' . $row[$mode . '_id'],
626 'L_STYLE_ACT_DEACT' => $user->lang['STYLE_' . strtoupper($stylevis)],
627 'S_OPTIONS' => implode(' | ', $s_options),
628 'S_ACTIONS' => implode(' | ', $s_actions),
629 'U_PREVIEW' => ($mode == 'style') ? append_sid("{$phpbb_root_path}index.$phpEx", "$mode=" . $row[$mode . '_id']) : '',
631 'NAME' => $row[$mode . '_name'],
632 'STYLE_COUNT' => ($mode == 'style' && isset($style_count[$row['style_id']])) ? $style_count[$row['style_id']] : 0,
636 $db->sql_freeresult($result);
638 // Grab uninstalled items
639 $new_ary = $cfg = array();
641 $dp = @opendir("{$phpbb_root_path}styles");
645 while (($file = readdir($dp)) !== false)
647 if ($file[0] == '.' || !is_dir($phpbb_root_path . 'styles/' . $file))
652 $subpath = ($mode != 'style') ? "$mode/" : '';
653 if (file_exists("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
655 if ($cfg = file("{$phpbb_root_path}styles/$file/$subpath$mode.cfg"))
657 $items = parse_cfg_file('', $cfg);
658 $name = (isset($items['name'])) ? trim($items['name']) : false;
660 if ($name && !in_array($name, $installed))
665 'copyright' => $items['copyright'],
676 if (sizeof($new_ary))
678 foreach ($new_ary as $cfg)
680 $template->assign_block_vars('uninstalled', array(
681 'NAME' => $cfg['name'],
682 'COPYRIGHT' => $cfg['copyright'],
683 'U_INSTALL' => $this->u_action . '&action=install&path=' . urlencode($cfg['path']))
689 $template->assign_vars(array(
690 'S_BASIS_OPTIONS' => $basis_options)
696 * Provides a template editor which allows saving changes to template files on the filesystem or in the database.
698 * @param int $template_id specifies which template set is being edited
700 function edit_template($template_id)
702 global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
704 if (defined('PHPBB_DISABLE_ACP_EDITOR'))
706 trigger_error($user->lang['EDITOR_DISABLED'] . adm_back_link($this->u_action));
709 $this->page_title = 'EDIT_TEMPLATE';
711 $filelist = $filelist_cats = array();
713 $template_data = utf8_normalize_nfc(request_var('template_data', '', true));
714 $template_data = htmlspecialchars_decode($template_data);
715 $template_file = utf8_normalize_nfc(request_var('template_file', '', true));
716 $text_rows = max(5, min(999, request_var('text_rows', 20)));
717 $save_changes = (isset($_POST['save'])) ? true : false;
719 // make sure template_file path doesn't go upwards
720 $template_file = preg_replace('#\.{2,}#', '.', $template_file);
722 // Retrieve some information about the template
723 $sql = 'SELECT template_storedb, template_path, template_name
724 FROM ' . STYLES_TEMPLATE_TABLE . "
725 WHERE template_id = $template_id";
726 $result = $db->sql_query($sql);
727 $template_info = $db->sql_fetchrow($result);
728 $db->sql_freeresult($result);
732 trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
735 if ($save_changes && !check_form_key('acp_styles'))
737 trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
739 else if (!$save_changes)
741 add_form_key('acp_styles');
744 // save changes to the template if the user submitted any
745 if ($save_changes && $template_file)
747 // Get the filesystem location of the current file
748 $file = "{$phpbb_root_path}styles/{$template_info['template_path']}/template/$template_file";
751 // If the template is stored on the filesystem try to write the file else store it in the database
752 if (!$safe_mode && !$template_info['template_storedb'] && file_exists($file) && phpbb_is_writable($file))
754 if (!($fp = @fopen($file, 'wb')))
756 // File exists and is writeable, but still not able to be written to
757 trigger_error(sprintf($user->lang['TEMPLATE_FILE_NOT_WRITABLE'], htmlspecialchars($template_file)) . adm_back_link($this->u_action), E_USER_WARNING);
759 fwrite($fp, $template_data);
764 $db->sql_transaction('begin');
766 // If it's not stored in the db yet, then update the template setting and store all template files in the db
767 if (!$template_info['template_storedb'])
769 if ($super = $this->get_super('template', $template_id))
771 $this->store_in_db('template', $super['template_id']);
775 $this->store_in_db('template', $template_id);
778 add_log('admin', 'LOG_TEMPLATE_EDIT_DETAILS', $template_info['template_name']);
779 $additional .= '<br />' . $user->lang['EDIT_TEMPLATE_STORED_DB'];
782 // Update the template_data table entry for this template file
783 $sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . "
784 SET template_data = '" . $db->sql_escape($template_data) . "', template_mtime = " . time() . "
785 WHERE template_id = $template_id
786 AND template_filename = '" . $db->sql_escape($template_file) . "'";
787 $db->sql_query($sql);
789 $db->sql_transaction('commit');
792 // destroy the cached version of the template (filename without extension)
793 $this->clear_template_cache($template_info, array(substr($template_file, 0, -5)));
795 $cache->destroy('sql', STYLES_TABLE);
797 add_log('admin', 'LOG_TEMPLATE_EDIT', $template_info['template_name'], $template_file);
798 trigger_error($user->lang['TEMPLATE_FILE_UPDATED'] . $additional . adm_back_link($this->u_action . "&action=edit&id=$template_id&text_rows=$text_rows&template_file=$template_file"));
801 // Generate a category array containing template filenames
802 if (!$template_info['template_storedb'])
804 $template_path = "{$phpbb_root_path}styles/{$template_info['template_path']}/template";
806 $filelist = filelist($template_path, '', 'html');
807 $filelist[''] = array_diff($filelist[''], array('bbcode.html'));
811 if (!file_exists($template_path . "/$template_file") || !($template_data = file_get_contents($template_path . "/$template_file")))
813 trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
820 FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
821 WHERE template_id = $template_id";
822 $result = $db->sql_query($sql);
824 $filelist = array('' => array());
825 while ($row = $db->sql_fetchrow($result))
827 $file_info = pathinfo($row['template_filename']);
829 if (($file_info['basename'] != 'bbcode') && ($file_info['extension'] == 'html'))
831 if (($file_info['dirname'] == '.') || empty($file_info['dirname']))
833 $filelist[''][] = $row['template_filename'];
837 $filelist[$file_info['dirname'] . '/'][] = $file_info['basename'];
841 if ($row['template_filename'] == $template_file)
843 $template_data = $row['template_data'];
846 $db->sql_freeresult($result);
850 if (empty($filelist['']))
852 trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
855 // Now create the categories
856 $filelist_cats[''] = array();
857 foreach ($filelist as $pathfile => $file_ary)
859 // Use the directory name as category name
860 if (!empty($pathfile))
862 $filelist_cats[$pathfile] = array();
863 foreach ($file_ary as $file)
865 $filelist_cats[$pathfile][$pathfile . $file] = $file;
868 // or if it's in the main category use the word before the first underscore to group files
872 foreach ($file_ary as $file)
874 $cats[] = substr($file, 0, strpos($file, '_'));
875 $filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
878 $cats = array_values(array_unique($cats));
880 // we don't need any single element categories so put them into the misc '' category
881 for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
883 if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
885 $filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
886 unset($filelist_cats[$cats[$i]]);
894 // Generate list of categorised template files
896 ksort($filelist_cats);
897 foreach ($filelist_cats as $category => $tpl_ary)
901 if (!empty($category))
903 $tpl_options .= '<option class="sep" value="">' . $category . '</option>';
906 foreach ($tpl_ary as $filename => $file)
908 $selected = ($template_file == $filename) ? ' selected="selected"' : '';
909 $tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
913 $template->assign_vars(array(
914 'S_EDIT_TEMPLATE' => true,
915 'S_HIDDEN_FIELDS' => build_hidden_fields(array('template_file' => $template_file)),
916 'S_TEMPLATES' => $tpl_options,
918 'U_ACTION' => $this->u_action . "&action=edit&id=$template_id&text_rows=$text_rows",
919 'U_BACK' => $this->u_action,
921 'L_EDIT' => $user->lang['EDIT_TEMPLATE'],
922 'L_EDIT_EXPLAIN' => $user->lang['EDIT_TEMPLATE_EXPLAIN'],
923 'L_EDITOR' => $user->lang['TEMPLATE_EDITOR'],
924 'L_EDITOR_HEIGHT' => $user->lang['TEMPLATE_EDITOR_HEIGHT'],
925 'L_FILE' => $user->lang['TEMPLATE_FILE'],
926 'L_SELECT' => $user->lang['SELECT_TEMPLATE'],
927 'L_SELECTED' => $user->lang['SELECTED_TEMPLATE'],
928 'L_SELECTED_FILE' => $user->lang['SELECTED_TEMPLATE_FILE'],
930 'SELECTED_TEMPLATE' => $template_info['template_name'],
931 'TEMPLATE_FILE' => $template_file,
932 'TEMPLATE_DATA' => utf8_htmlspecialchars($template_data),
933 'TEXT_ROWS' => $text_rows)
938 * Allows the admin to view cached versions of template files and clear single template cache files
940 * @param int $template_id specifies which template's cache is shown
942 function template_cache($template_id)
944 global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
946 $source = str_replace('/', '.', request_var('source', ''));
947 $file_ary = array_diff(request_var('delete', array('')), array(''));
948 $submit = isset($_POST['submit']) ? true : false;
951 FROM ' . STYLES_TEMPLATE_TABLE . "
952 WHERE template_id = $template_id";
953 $result = $db->sql_query($sql);
954 $template_row = $db->sql_fetchrow($result);
955 $db->sql_freeresult($result);
959 trigger_error($user->lang['NO_TEMPLATE'] . adm_back_link($this->u_action), E_USER_WARNING);
962 // User wants to delete one or more files ...
963 if ($submit && $file_ary)
965 $this->clear_template_cache($template_row, $file_ary);
966 trigger_error($user->lang['TEMPLATE_CACHE_CLEARED'] . adm_back_link($this->u_action . "&action=cache&id=$template_id"));
969 $cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
971 // Someone wants to see the cached source ... so we'll highlight it,
972 // add line numbers and indent it appropriately. This could be nasty
973 // on larger source files ...
974 if ($source && file_exists("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"))
976 adm_page_header($user->lang['TEMPLATE_CACHE']);
978 $template->set_filenames(array(
979 'body' => 'viewsource.html')
982 $template->assign_vars(array(
983 'FILENAME' => str_replace('.', '/', $source) . '.html')
986 $code = str_replace(array("\r\n", "\r"), array("\n", "\n"), file_get_contents("{$phpbb_root_path}cache/{$cache_prefix}_$source.html.$phpEx"));
988 $conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string');
989 foreach ($conf as $ini_var)
991 @ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var));
994 $marker = 'MARKER' . time();
995 $code = highlight_string(str_replace("\n", $marker, $code), true);
996 $code = str_replace($marker, "\n", $code);
997 $str_from = array('<span style="color: ', '<font color="syntax', '</font>', '<code>', '</code>','[', ']', '.', ':');
998 $str_to = array('<span class="', '<span class="syntax', '</span>', '', '', '[', ']', '.', ':');
1000 $code = str_replace($str_from, $str_to, $code);
1001 $code = preg_replace('#^(<span class="[a-z_]+">)\n?(.*?)\n?(</span>)$#ism', '$1$2$3', $code);
1002 $code = substr($code, strlen('<span class="syntaxhtml">'));
1003 $code = substr($code, 0, -1 * strlen('</ span>'));
1004 $code = explode("\n", $code);
1006 foreach ($code as $key => $line)
1008 $template->assign_block_vars('source', array(
1009 'LINENUM' => $key + 1,
1010 'LINE' => preg_replace('#([^ ;]) ([^ &])#', '$1 $2', $line))
1018 $filemtime = array();
1019 if ($template_row['template_storedb'])
1022 if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
1024 $ids[] = $template_row['template_inherits_id'];
1026 $ids[] = $template_row['template_id'];
1028 $filemtime = array();
1029 $file_template_db = array();
1031 foreach ($ids as $id)
1033 $sql = 'SELECT template_filename, template_mtime
1034 FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
1035 WHERE template_id = $id";
1036 $result = $db->sql_query($sql);
1038 while ($row = $db->sql_fetchrow($result))
1040 $filemtime[$row['template_filename']] = $row['template_mtime'];
1041 $file_template_db[$row['template_filename']] = $id;
1043 $db->sql_freeresult($result);
1047 // Get a list of cached template files and then retrieve additional information about them
1048 $file_ary = $this->template_cache_filelist($template_row['template_path']);
1050 foreach ($file_ary as $file)
1052 $file = str_replace('/', '.', $file);
1054 // perform some dirty guessing to get the path right.
1055 // We assume that three dots in a row were '../'
1056 $tpl_file = str_replace('.', '/', $file);
1057 $tpl_file = str_replace('///', '../', $tpl_file);
1059 $filename = "{$cache_prefix}_$file.html.$phpEx";
1061 if (!file_exists("{$phpbb_root_path}cache/$filename"))
1066 $file_tpl = "{$phpbb_root_path}styles/{$template_row['template_path']}/template/$tpl_file.html";
1069 if (isset($template_row['template_inherits_id']) && $template_row['template_inherits_id'])
1071 if (!$template_row['template_storedb'])
1073 if (!file_exists($file_tpl))
1075 $file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
1081 if ($file_template_db[$file . '.html'] == $template_row['template_inherits_id'])
1083 $file_tpl = "{$phpbb_root_path}styles/{$template_row['template_inherit_path']}/template/$tpl_file.html";
1089 // Correct the filename if it is stored in database and the file is in a subfolder.
1090 if ($template_row['template_storedb'])
1092 $file = str_replace('.', '/', $file);
1095 $template->assign_block_vars('file', array(
1096 'U_VIEWSOURCE' => $this->u_action . "&action=cache&id=$template_id&source=$file",
1098 'CACHED' => $user->format_date(filemtime("{$phpbb_root_path}cache/$filename")),
1099 'FILENAME' => $file,
1100 'FILENAME_PATH' => $file_tpl,
1101 'FILESIZE' => get_formatted_filesize(filesize("{$phpbb_root_path}cache/$filename")),
1102 'MODIFIED' => $user->format_date((!$template_row['template_storedb']) ? filemtime($file_tpl) : $filemtime[$file . '.html']))
1107 $template->assign_vars(array(
1109 'S_TEMPLATE' => true,
1111 'U_ACTION' => $this->u_action . "&action=cache&id=$template_id",
1112 'U_BACK' => $this->u_action)
1117 * Provides a css editor and a basic easier to use stylesheet editing tool for less experienced (or lazy) users
1119 * @param int $theme_id specifies which theme is being edited
1121 function edit_theme($theme_id)
1123 global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template, $safe_mode;
1125 $this->page_title = 'EDIT_THEME';
1127 $filelist = $filelist_cats = array();
1129 $theme_data = utf8_normalize_nfc(request_var('template_data', '', true));
1130 $theme_data = htmlspecialchars_decode($theme_data);
1131 $theme_file = utf8_normalize_nfc(request_var('template_file', '', true));
1132 $text_rows = max(5, min(999, request_var('text_rows', 20)));
1133 $save_changes = (isset($_POST['save'])) ? true : false;
1135 // make sure theme_file path doesn't go upwards
1136 $theme_file = str_replace('..', '.', $theme_file);
1138 // Retrieve some information about the theme
1139 $sql = 'SELECT theme_storedb, theme_path, theme_name, theme_data
1140 FROM ' . STYLES_THEME_TABLE . "
1141 WHERE theme_id = $theme_id";
1142 $result = $db->sql_query($sql);
1144 if (!($theme_info = $db->sql_fetchrow($result)))
1146 trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
1148 $db->sql_freeresult($result);
1150 // save changes to the theme if the user submitted any
1153 // Get the filesystem location of the current file
1154 $file = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme/$theme_file";
1156 $message = $user->lang['THEME_UPDATED'];
1158 // If the theme is stored on the filesystem try to write the file else store it in the database
1159 if (!$safe_mode && !$theme_info['theme_storedb'] && file_exists($file) && phpbb_is_writable($file))
1161 if (!($fp = @fopen($file, 'wb')))
1163 trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
1165 fwrite($fp, $theme_data);
1170 // Write stylesheet to db
1172 'theme_mtime' => time(),
1173 'theme_storedb' => 1,
1174 'theme_data' => $this->db_theme_data($theme_info, $theme_data),
1176 $sql = 'UPDATE ' . STYLES_THEME_TABLE . '
1177 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1178 WHERE theme_id = ' . $theme_id;
1179 $db->sql_query($sql);
1181 $cache->destroy('sql', STYLES_THEME_TABLE);
1183 // notify the user if the theme was not stored in the db before his modification
1184 if (!$theme_info['theme_storedb'])
1186 add_log('admin', 'LOG_THEME_EDIT_DETAILS', $theme_info['theme_name']);
1187 $message .= '<br />' . $user->lang['EDIT_THEME_STORED_DB'];
1190 $cache->destroy('sql', STYLES_THEME_TABLE);
1191 add_log('admin', (!$theme_info['theme_storedb']) ? 'LOG_THEME_EDIT_FILE' : 'LOG_THEME_EDIT', $theme_info['theme_name'], (!$theme_info['theme_storedb']) ? $theme_file : '');
1193 trigger_error($message . adm_back_link($this->u_action . "&action=edit&id=$theme_id&template_file=$theme_file&text_rows=$text_rows"));
1196 // Generate a category array containing theme filenames
1197 if (!$theme_info['theme_storedb'])
1199 $theme_path = "{$phpbb_root_path}styles/{$theme_info['theme_path']}/theme";
1201 $filelist = filelist($theme_path, '', 'css');
1205 if (!file_exists($theme_path . "/$theme_file") || !($theme_data = file_get_contents($theme_path . "/$theme_file")))
1207 trigger_error($user->lang['NO_THEME'] . adm_back_link($this->u_action), E_USER_WARNING);
1213 $theme_data = &$theme_info['theme_data'];
1216 // Now create the categories
1217 $filelist_cats[''] = array();
1218 foreach ($filelist as $pathfile => $file_ary)
1220 // Use the directory name as category name
1221 if (!empty($pathfile))
1223 $filelist_cats[$pathfile] = array();
1224 foreach ($file_ary as $file)
1226 $filelist_cats[$pathfile][$pathfile . $file] = $file;
1229 // or if it's in the main category use the word before the first underscore to group files
1233 foreach ($file_ary as $file)
1235 $cats[] = substr($file, 0, strpos($file, '_'));
1236 $filelist_cats[substr($file, 0, strpos($file, '_'))][$file] = $file;
1239 $cats = array_values(array_unique($cats));
1241 // we don't need any single element categories so put them into the misc '' category
1242 for ($i = 0, $n = sizeof($cats); $i < $n; $i++)
1244 if (sizeof($filelist_cats[$cats[$i]]) == 1 && $cats[$i] !== '')
1246 $filelist_cats[''][key($filelist_cats[$cats[$i]])] = current($filelist_cats[$cats[$i]]);
1247 unset($filelist_cats[$cats[$i]]);
1255 // Generate list of categorised theme files
1257 ksort($filelist_cats);
1258 foreach ($filelist_cats as $category => $tpl_ary)
1262 if (!empty($category))
1264 $tpl_options .= '<option class="sep" value="">' . $category . '</option>';
1267 foreach ($tpl_ary as $filename => $file)
1269 $selected = ($theme_file == $filename) ? ' selected="selected"' : '';
1270 $tpl_options .= '<option value="' . $filename . '"' . $selected . '>' . $file . '</option>';
1274 $template->assign_vars(array(
1275 'S_EDIT_THEME' => true,
1276 'S_HIDDEN_FIELDS' => build_hidden_fields(array('template_file' => $theme_file)),
1277 'S_THEME_IN_DB' => $theme_info['theme_storedb'],
1278 'S_TEMPLATES' => $tpl_options,
1280 'U_ACTION' => $this->u_action . "&action=edit&id=$theme_id&text_rows=$text_rows",
1281 'U_BACK' => $this->u_action,
1283 'L_EDIT' => $user->lang['EDIT_THEME'],
1284 'L_EDIT_EXPLAIN' => $user->lang['EDIT_THEME_EXPLAIN'],
1285 'L_EDITOR' => $user->lang['THEME_EDITOR'],
1286 'L_EDITOR_HEIGHT' => $user->lang['THEME_EDITOR_HEIGHT'],
1287 'L_FILE' => $user->lang['THEME_FILE'],
1288 'L_SELECT' => $user->lang['SELECT_THEME'],
1289 'L_SELECTED' => $user->lang['SELECTED_THEME'],
1290 'L_SELECTED_FILE' => $user->lang['SELECTED_THEME_FILE'],
1292 'SELECTED_TEMPLATE' => $theme_info['theme_name'],
1293 'TEMPLATE_FILE' => $theme_file,
1294 'TEMPLATE_DATA' => utf8_htmlspecialchars($theme_data),
1295 'TEXT_ROWS' => $text_rows)
1302 * @param int $imageset_id specifies which imageset is being edited
1304 function edit_imageset($imageset_id)
1306 global $db, $user, $phpbb_root_path, $cache, $template;
1308 $this->page_title = 'EDIT_IMAGESET';
1312 trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
1315 $update = (isset($_POST['update'])) ? true : false;
1317 $imgname = request_var('imgname', 'site_logo');
1318 $imgname = preg_replace('#[^a-z0-9\-+_]#i', '', $imgname);
1319 $sql_extra = $imgnamelang = '';
1321 $sql = 'SELECT imageset_path, imageset_name
1322 FROM ' . STYLES_IMAGESET_TABLE . "
1323 WHERE imageset_id = $imageset_id";
1324 $result = $db->sql_query($sql);
1325 $imageset_row = $db->sql_fetchrow($result);
1326 $db->sql_freeresult($result);
1330 trigger_error($user->lang['NO_IMAGESET'] . adm_back_link($this->u_action), E_USER_WARNING);
1333 $imageset_path = $imageset_row['imageset_path'];
1334 $imageset_name = $imageset_row['imageset_name'];
1336 if (strpos($imgname, '-') !== false)
1338 list($imgname, $imgnamelang) = explode('-', $imgname);
1339 $sql_extra = " AND image_lang IN ('" . $db->sql_escape($imgnamelang) . "', '')";
1342 $sql = 'SELECT image_filename, image_width, image_height, image_lang, image_id
1343 FROM ' . STYLES_IMAGESET_DATA_TABLE . "
1344 WHERE imageset_id = $imageset_id
1345 AND image_name = '" . $db->sql_escape($imgname) . "'$sql_extra";
1346 $result = $db->sql_query($sql);
1347 $imageset_data_row = $db->sql_fetchrow($result);
1348 $db->sql_freeresult($result);
1350 $image_filename = $imageset_data_row['image_filename'];
1351 $image_width = $imageset_data_row['image_width'];
1352 $image_height = $imageset_data_row['image_height'];
1353 $image_lang = $imageset_data_row['image_lang'];
1354 $image_id = $imageset_data_row['image_id'];
1355 $imgsize = ($imageset_data_row['image_width'] && $imageset_data_row['image_height']) ? 1 : 0;
1357 // Check to see whether the selected image exists in the table
1358 $valid_name = ($update) ? false : true;
1360 foreach ($this->imageset_keys as $category => $img_ary)
1362 if (in_array($imgname, $img_ary))
1369 if ($update && isset($_POST['imgpath']) && $valid_name)
1371 // If imgwidth and imgheight are non-zero grab the actual size
1372 // from the image itself ... we ignore width settings for the poll center image
1373 $imgwidth = request_var('imgwidth', 0);
1374 $imgheight = request_var('imgheight', 0);
1375 $imgsize = request_var('imgsize', 0);
1376 $imgpath = request_var('imgpath', '');
1377 $imgpath = str_replace('..', '.', $imgpath);
1379 // If no dimensions selected, we reset width and height to 0 ;)
1382 $imgwidth = $imgheight = 0;
1387 if ($imgpath && !file_exists("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath"))
1389 trigger_error($user->lang['NO_IMAGE_ERROR'] . adm_back_link($this->u_action), E_USER_WARNING);
1392 // Determine width/height. If dimensions included and no width/height given, we detect them automatically...
1393 if ($imgsize && $imgpath)
1395 if (!$imgwidth || !$imgheight)
1397 list($imgwidth_file, $imgheight_file) = getimagesize("{$phpbb_root_path}styles/$imageset_path/imageset/$imgpath");
1398 $imgwidth = ($imgwidth) ? $imgwidth : $imgwidth_file;
1399 $imgheight = ($imgheight) ? $imgheight : $imgheight_file;
1401 $imgwidth = ($imgname != 'poll_center') ? (int) $imgwidth : 0;
1402 $imgheight = (int) $imgheight;
1405 if (strpos($imgpath, '/') !== false)
1407 list($imglang, $imgfilename) = explode('/', $imgpath);
1411 $imgfilename = $imgpath;
1415 'image_filename' => (string) $imgfilename,
1416 'image_width' => (int) $imgwidth,
1417 'image_height' => (int) $imgheight,
1418 'image_lang' => (string) $imglang,
1422 if ($imageset_data_row)
1424 $sql = 'UPDATE ' . STYLES_IMAGESET_DATA_TABLE . '
1425 SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
1426 WHERE image_id = $image_id";
1427 $db->sql_query($sql);
1430 else if (!$imageset_data_row)
1432 $sql_ary['image_name'] = $imgname;
1433 $sql_ary['imageset_id'] = (int) $imageset_id;
1434 $db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
1437 $cache->destroy('sql', STYLES_IMAGESET_DATA_TABLE);
1439 add_log('admin', 'LOG_IMAGESET_EDIT', $imageset_name);
1441 $template->assign_var('SUCCESS', true);
1443 $image_filename = $imgfilename;
1444 $image_width = $imgwidth;
1445 $image_height = $imgheight;
1446 $image_lang = $imglang;
1450 $imagesetlist = array('nolang' => array(), 'lang' => array());
1453 $dir = "{$phpbb_root_path}styles/$imageset_path/imageset";
1454 $dp = @opendir($dir);
1458 while (($file = readdir($dp)) !== false)
1460 if ($file[0] != '.' && strtoupper($file) != 'CVS' && !is_file($dir . '/' . $file) && !is_link($dir . '/' . $file))
1464 else if (preg_match('#\.(?:gif|jpg|png)$#', $file))
1466 $imagesetlist['nolang'][] = $file;
1472 $dp2 = @opendir("$dir/$imgnamelang");
1476 while (($file2 = readdir($dp2)) !== false)
1478 if (preg_match('#\.(?:gif|jpg|png)$#', $file2))
1480 $imagesetlist['lang'][] = "$imgnamelang/$file2";
1489 // Generate list of image options
1491 foreach ($this->imageset_keys as $category => $img_ary)
1493 $template->assign_block_vars('category', array(
1494 'NAME' => $user->lang['IMG_CAT_' . strtoupper($category)]
1497 foreach ($img_ary as $img)
1499 if ($category == 'buttons')
1501 foreach ($langs as $language)
1503 $template->assign_block_vars('category.images', array(
1504 'SELECTED' => ($img == $imgname && $language == $imgnamelang),
1505 'VALUE' => $img . '-' . $language,
1506 'TEXT' => $user->lang['IMG_' . strtoupper($img)] . ' [ ' . $language . ' ]'
1512 $template->assign_block_vars('category.images', array(
1513 'SELECTED' => ($img == $imgname),
1515 'TEXT' => (($category == 'custom') ? $img : $user->lang['IMG_' . strtoupper($img)])
1521 // Make sure the list of possible images is sorted alphabetically
1522 sort($imagesetlist['lang']);
1523 sort($imagesetlist['nolang']);
1525 $image_found = false;
1527 foreach ($imagesetlist as $type => $img_ary)
1529 if ($type !== 'lang' || $sql_extra)
1531 $template->assign_block_vars('imagesetlist', array(
1532 'TYPE' => ($type == 'lang')
1536 foreach ($img_ary as $img)
1538 $imgtext = preg_replace('/^([^\/]+\/)/', '', $img);
1539 $selected = (!empty($imgname) && strpos($image_filename, $imgtext) !== false);
1542 $image_found = true;
1543 $img_val = htmlspecialchars($img);
1545 $template->assign_block_vars('imagesetlist.images', array(
1546 'SELECTED' => $selected,
1548 'VALUE' => htmlspecialchars($img)
1553 $imgsize_bool = (!empty($imgname) && $image_width && $image_height) ? true : false;
1554 $image_request = '../styles/' . $imageset_path . '/imageset/' . ($image_lang ? $imgnamelang . '/' : '') . $image_filename;
1556 $template->assign_vars(array(
1557 'S_EDIT_IMAGESET' => true,
1558 'L_TITLE' => $user->lang[$this->page_title],
1559 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
1560 'IMAGE_OPTIONS' => $img_options,
1561 'IMAGE_SIZE' => $image_width,
1562 'IMAGE_HEIGHT' => $image_height,
1563 'IMAGE_REQUEST' => (empty($image_filename)) ? 'images/no_image.png' : $image_request,
1564 'U_ACTION' => $this->u_action . "&action=edit&id=$imageset_id",
1565 'U_BACK' => $this->u_action,
1566 'NAME' => $imageset_name,
1567 'A_NAME' => addslashes($imageset_name),
1568 'PATH' => $imageset_path,
1569 'A_PATH' => addslashes($imageset_path),
1570 'ERROR' => !$valid_name,
1571 'IMG_SRC' => ($image_found) ? '../styles/' . $imageset_path . '/imageset/' . $img_val : 'images/no_image.png',
1572 'IMAGE_SELECT' => $image_found
1577 * Remove style/template/theme/imageset
1579 function remove($mode, $style_id)
1581 global $db, $template, $user, $phpbb_root_path, $cache, $config;
1583 $new_id = request_var('new_id', 0);
1584 $update = (isset($_POST['update'])) ? true : false;
1590 $sql_from = STYLES_TABLE;
1591 $sql_select = 'style_id, style_name, template_id, theme_id, imageset_id';
1592 $sql_where = 'AND style_active = 1';
1596 $sql_from = STYLES_TEMPLATE_TABLE;
1597 $sql_select = 'template_id, template_name, template_path, template_storedb';
1601 $sql_from = STYLES_THEME_TABLE;
1602 $sql_select = 'theme_id, theme_name, theme_path, theme_storedb';
1606 $sql_from = STYLES_IMAGESET_TABLE;
1607 $sql_select = 'imageset_id, imageset_name, imageset_path';
1611 if ($mode === 'template' && ($conflicts = $this->check_inheritance($mode, $style_id)))
1613 $l_type = strtoupper($mode);
1614 $msg = $user->lang[$l_type . '_DELETE_DEPENDENT'];
1615 foreach ($conflicts as $id => $values)
1617 $msg .= '<br />' . $values['template_name'];
1620 trigger_error($msg . adm_back_link($this->u_action), E_USER_WARNING);
1623 $l_prefix = strtoupper($mode);
1625 $sql = "SELECT $sql_select
1627 WHERE {$mode}_id = $style_id";
1628 $result = $db->sql_query($sql);
1629 $style_row = $db->sql_fetchrow($result);
1630 $db->sql_freeresult($result);
1634 trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
1637 $s_only_component = $this->display_component_options($mode, $style_row[$mode . '_id'], $style_row);
1639 if ($s_only_component)
1641 trigger_error($user->lang['ONLY_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
1646 if ($mode == 'style')
1648 $sql = "DELETE FROM $sql_from
1649 WHERE {$mode}_id = $style_id";
1650 $db->sql_query($sql);
1652 $sql = 'UPDATE ' . USERS_TABLE . "
1653 SET user_style = $new_id
1654 WHERE user_style = $style_id";
1655 $db->sql_query($sql);
1657 $sql = 'UPDATE ' . FORUMS_TABLE . "
1658 SET forum_style = $new_id
1659 WHERE forum_style = $style_id";
1660 $db->sql_query($sql);
1662 if ($style_id == $config['default_style'])
1664 set_config('default_style', $new_id);
1667 // Remove the components
1668 $components = array('template', 'theme', 'imageset');
1669 foreach ($components as $component)
1671 $new_id = request_var('new_' . $component . '_id', 0);
1672 $component_id = $style_row[$component . '_id'];
1673 $this->remove_component($component, $component_id, $new_id, $style_id);
1678 $this->remove_component($mode, $style_id, $new_id);
1681 $cache->destroy('sql', STYLES_TABLE);
1683 add_log('admin', 'LOG_' . $l_prefix . '_DELETE', $style_row[$mode . '_name']);
1684 $message = ($mode != 'style') ? $l_prefix . '_DELETED_FS' : $l_prefix . '_DELETED';
1685 trigger_error($user->lang[$message] . adm_back_link($this->u_action));
1688 $this->page_title = 'DELETE_' . $l_prefix;
1690 $template->assign_vars(array(
1693 'L_TITLE' => $user->lang[$this->page_title],
1694 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
1695 'L_NAME' => $user->lang[$l_prefix . '_NAME'],
1696 'L_REPLACE' => $user->lang['REPLACE_' . $l_prefix],
1697 'L_REPLACE_EXPLAIN' => $user->lang['REPLACE_' . $l_prefix . '_EXPLAIN'],
1699 'U_ACTION' => $this->u_action . "&action=delete&id=$style_id",
1700 'U_BACK' => $this->u_action,
1702 'NAME' => $style_row[$mode . '_name'],
1706 if ($mode == 'style')
1708 $template->assign_vars(array(
1709 'S_DELETE_STYLE' => true,
1715 * Remove template/theme/imageset entry from the database
1717 function remove_component($component, $component_id, $new_id, $style_id = false)
1721 if (($new_id == 0) || ($component === 'template' && ($conflicts = $this->check_inheritance($component, $component_id))))
1723 // We can not delete the template, as the user wants to keep the component or an other template is inheriting from this one.
1727 $component_in_use = array();
1728 if ($component != 'style')
1730 $component_in_use = $this->component_in_use($component, $component_id, $style_id);
1733 if (($new_id == -1) && !empty($component_in_use))
1735 // We can not delete the component, as it is still in use
1739 if ($component == 'imageset')
1741 $sql = 'DELETE FROM ' . STYLES_IMAGESET_DATA_TABLE . "
1742 WHERE imageset_id = $component_id";
1743 $db->sql_query($sql);
1749 $sql_from = STYLES_TEMPLATE_TABLE;
1753 $sql_from = STYLES_THEME_TABLE;
1757 $sql_from = STYLES_IMAGESET_TABLE;;
1761 $sql = "DELETE FROM $sql_from
1762 WHERE {$component}_id = $component_id";
1763 $db->sql_query($sql);
1765 $sql = 'UPDATE ' . STYLES_TABLE . "
1766 SET {$component}_id = $new_id
1767 WHERE {$component}_id = $component_id";
1768 $db->sql_query($sql);
1772 * Display the options which can be used to replace a style/template/theme/imageset
1774 * @return boolean Returns true if the component is the only component and can not be deleted.
1776 function display_component_options($component, $component_id, $style_row = false, $style_id = false)
1778 global $db, $template, $user;
1780 $is_only_component = true;
1781 $component_in_use = array();
1782 if ($component != 'style')
1784 $component_in_use = $this->component_in_use($component, $component_id, $style_id);
1791 $sql_from = STYLES_TABLE;
1792 $sql_where = 'WHERE style_active = 1';
1796 $sql_from = STYLES_TEMPLATE_TABLE;
1797 $sql_where = 'WHERE template_inherits_id <> ' . $component_id;
1801 $sql_from = STYLES_THEME_TABLE;
1805 $sql_from = STYLES_IMAGESET_TABLE;
1810 if (($component != 'style') && empty($component_in_use))
1812 // If it is not in use, there must be another component
1813 $is_only_component = false;
1815 $sql = "SELECT {$component}_id, {$component}_name
1817 WHERE {$component}_id = {$component_id}";
1818 $result = $db->sql_query($sql);
1819 $row = $db->sql_fetchrow($result);
1820 $db->sql_freeresult($result);
1822 $s_options .= '<option value="-1" selected="selected">' . $user->lang['DELETE_' . strtoupper($component)] . '</option>';
1823 $s_options .= '<option value="0">' . sprintf($user->lang['KEEP_' . strtoupper($component)], $row[$component . '_name']) . '</option>';
1827 $sql = "SELECT {$component}_id, {$component}_name
1830 ORDER BY {$component}_name ASC";
1831 $result = $db->sql_query($sql);
1833 $s_keep_option = $s_options = '';
1834 while ($row = $db->sql_fetchrow($result))
1836 if ($row[$component . '_id'] != $component_id)
1838 $is_only_component = false;
1839 $s_options .= '<option value="' . $row[$component . '_id'] . '">' . sprintf($user->lang['REPLACE_WITH_OPTION'], $row[$component . '_name']) . '</option>';
1841 else if ($component != 'style')
1843 $s_keep_option = '<option value="0" selected="selected">' . sprintf($user->lang['KEEP_' . strtoupper($component)], $row[$component . '_name']) . '</option>';
1846 $db->sql_freeresult($result);
1847 $s_options = $s_keep_option . $s_options;
1852 $template->assign_var('S_REPLACE_' . strtoupper($component) . '_OPTIONS', $s_options);
1856 $template->assign_var('S_REPLACE_OPTIONS', $s_options);
1857 if ($component == 'style')
1859 $components = array('template', 'theme', 'imageset');
1860 foreach ($components as $component)
1862 $this->display_component_options($component, $style_row[$component . '_id'], false, $component_id, true);
1867 return $is_only_component;
1871 * Check whether the component is still used by another style or component
1873 function component_in_use($component, $component_id, $style_id = false)
1877 $component_in_use = array();
1881 $sql = 'SELECT style_id, style_name
1882 FROM ' . STYLES_TABLE . "
1883 WHERE {$component}_id = {$component_id}
1884 AND style_id <> {$style_id}
1885 ORDER BY style_name ASC";
1889 $sql = 'SELECT style_id, style_name
1890 FROM ' . STYLES_TABLE . "
1891 WHERE {$component}_id = {$component_id}
1892 ORDER BY style_name ASC";
1894 $result = $db->sql_query($sql);
1895 while ($row = $db->sql_fetchrow($result))
1897 $component_in_use[] = $row['style_name'];
1899 $db->sql_freeresult($result);
1901 if ($component === 'template' && ($conflicts = $this->check_inheritance($component, $component_id)))
1903 foreach ($conflicts as $temp_id => $conflict_data)
1905 $component_in_use[] = $conflict_data['template_name'];
1909 return $component_in_use;
1913 * Export style or style elements
1915 function export($mode, $style_id)
1917 global $db, $template, $user, $phpbb_root_path, $cache, $phpEx, $config;
1919 $update = (isset($_POST['update'])) ? true : false;
1921 $inc_template = request_var('inc_template', 0);
1922 $inc_theme = request_var('inc_theme', 0);
1923 $inc_imageset = request_var('inc_imageset', 0);
1924 $store = request_var('store', 0);
1925 $format = request_var('format', '');
1928 $methods = array('tar');
1930 $available_methods = array('tar.gz' => 'zlib', 'tar.bz2' => 'bz2', 'zip' => 'zlib');
1931 foreach ($available_methods as $type => $module)
1933 if (!@extension_loaded($module))
1941 if (!in_array($format, $methods))
1949 if ($update && ($inc_template + $inc_theme + $inc_imageset) < 1)
1951 $error[] = $user->lang['STYLE_ERR_MORE_ELEMENTS'];
1954 $name = 'style_name';
1956 $sql_select = 's.style_id, s.style_name, s.style_copyright';
1957 $sql_select .= ($inc_template) ? ', t.*' : ', t.template_name';
1958 $sql_select .= ($inc_theme) ? ', c.*' : ', c.theme_name';
1959 $sql_select .= ($inc_imageset) ? ', i.*' : ', i.imageset_name';
1960 $sql_from = STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . ' i';
1961 $sql_where = "s.style_id = $style_id AND t.template_id = s.template_id AND c.theme_id = s.theme_id AND i.imageset_id = s.imageset_id";
1963 $l_prefix = 'STYLE';
1967 $name = 'template_name';
1970 $sql_from = STYLES_TEMPLATE_TABLE;
1971 $sql_where = "template_id = $style_id";
1973 $l_prefix = 'TEMPLATE';
1977 $name = 'theme_name';
1980 $sql_from = STYLES_THEME_TABLE;
1981 $sql_where = "theme_id = $style_id";
1983 $l_prefix = 'THEME';
1987 $name = 'imageset_name';
1990 $sql_from = STYLES_IMAGESET_TABLE;
1991 $sql_where = "imageset_id = $style_id";
1993 $l_prefix = 'IMAGESET';
1997 if ($update && !sizeof($error))
1999 $sql = "SELECT $sql_select
2002 $result = $db->sql_query($sql);
2003 $style_row = $db->sql_fetchrow($result);
2004 $db->sql_freeresult($result);
2008 trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
2011 $var_ary = array('style_id', 'style_name', 'style_copyright', 'template_id', 'template_name', 'template_path', 'template_copyright', 'template_storedb', 'template_inherits_id', 'bbcode_bitfield', 'theme_id', 'theme_name', 'theme_path', 'theme_copyright', 'theme_storedb', 'theme_mtime', 'theme_data', 'imageset_id', 'imageset_name', 'imageset_path', 'imageset_copyright');
2013 foreach ($var_ary as $var)
2015 if (!isset($style_row[$var]))
2017 $style_row[$var] = '';
2021 $files = $data = array();
2023 if ($mode == 'style')
2025 $style_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['style_name'], $style_row['style_copyright'], $config['version']), $this->style_cfg);
2027 $style_cfg .= (!$inc_template) ? "\nrequired_template = {$style_row['template_name']}" : '';
2028 $style_cfg .= (!$inc_theme) ? "\nrequired_theme = {$style_row['theme_name']}" : '';
2029 $style_cfg .= (!$inc_imageset) ? "\nrequired_imageset = {$style_row['imageset_name']}" : '';
2032 'src' => $style_cfg,
2033 'prefix' => 'style.cfg'
2039 // Export template core code
2040 if ($mode == 'template' || $inc_template)
2042 $template_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['template_name'], $style_row['template_copyright'], $config['version']), $this->template_cfg);
2044 $use_template_name = '';
2046 // Add the inherit from variable, depending on it's use...
2047 if ($style_row['template_inherits_id'])
2049 // Get the template name
2050 $sql = 'SELECT template_name
2051 FROM ' . STYLES_TEMPLATE_TABLE . '
2052 WHERE template_id = ' . (int) $style_row['template_inherits_id'];
2053 $result = $db->sql_query($sql);
2054 $use_template_name = (string) $db->sql_fetchfield('template_name');
2055 $db->sql_freeresult($result);
2058 $template_cfg .= ($use_template_name) ? "\ninherit_from = $use_template_name" : "\n#inherit_from = ";
2059 $template_cfg .= "\n\nbbcode_bitfield = {$style_row['bbcode_bitfield']}";
2062 'src' => $template_cfg,
2063 'prefix' => 'template/template.cfg'
2066 // This is potentially nasty memory-wise ...
2067 if (!$style_row['template_storedb'])
2070 'src' => "styles/{$style_row['template_path']}/template/",
2071 'prefix-' => "styles/{$style_row['template_path']}/",
2073 'exclude' => 'template.cfg'
2078 $sql = 'SELECT template_filename, template_data
2079 FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
2080 WHERE template_id = {$style_row['template_id']}";
2081 $result = $db->sql_query($sql);
2083 while ($row = $db->sql_fetchrow($result))
2086 'src' => $row['template_data'],
2087 'prefix' => 'template/' . $row['template_filename']
2090 $db->sql_freeresult($result);
2092 unset($template_cfg);
2095 // Export theme core code
2096 if ($mode == 'theme' || $inc_theme)
2098 $theme_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['theme_name'], $style_row['theme_copyright'], $config['version']), $this->theme_cfg);
2100 // Read old cfg file
2101 $items = $cache->obtain_cfg_items($style_row);
2102 $items = $items['theme'];
2104 if (!isset($items['parse_css_file']))
2106 $items['parse_css_file'] = 'off';
2109 $theme_cfg = str_replace(array('{PARSE_CSS_FILE}'), array($items['parse_css_file']), $theme_cfg);
2112 'src' => "styles/{$style_row['theme_path']}/theme/",
2113 'prefix-' => "styles/{$style_row['theme_path']}/",
2115 'exclude' => ($style_row['theme_storedb']) ? 'stylesheet.css,theme.cfg' : 'theme.cfg'
2119 'src' => $theme_cfg,
2120 'prefix' => 'theme/theme.cfg'
2123 if ($style_row['theme_storedb'])
2126 'src' => $style_row['theme_data'],
2127 'prefix' => 'theme/stylesheet.css'
2131 unset($items, $theme_cfg);
2134 // Export imageset core code
2135 if ($mode == 'imageset' || $inc_imageset)
2137 $imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
2139 $imageset_main = array();
2141 $sql = 'SELECT image_filename, image_name, image_height, image_width
2142 FROM ' . STYLES_IMAGESET_DATA_TABLE . "
2143 WHERE imageset_id = $style_id
2144 AND image_lang = ''";
2145 $result = $db->sql_query($sql);
2146 while ($row = $db->sql_fetchrow($result))
2148 $imageset_main[$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
2150 $db->sql_freeresult($result);
2152 foreach ($this->imageset_keys as $topic => $key_array)
2154 foreach ($key_array as $key)
2156 if (isset($imageset_main[$key]))
2158 $imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_main[$key]);
2164 'src' => "styles/{$style_row['imageset_path']}/imageset/",
2165 'prefix-' => "styles/{$style_row['imageset_path']}/",
2167 'exclude' => 'imageset.cfg'
2171 'src' => trim($imageset_cfg),
2172 'prefix' => 'imageset/imageset.cfg'
2177 $imageset_root = "{$phpbb_root_path}styles/{$style_row['imageset_path']}/imageset/";
2179 if ($dh = @opendir($imageset_root))
2181 while (($fname = readdir($dh)) !== false)
2183 if ($fname[0] != '.' && $fname != 'CVS' && is_dir("$imageset_root$fname"))
2185 $files[key($files)]['exclude'] .= ',' . $fname . '/imageset.cfg';
2191 $imageset_lang = array();
2193 $sql = 'SELECT image_filename, image_name, image_height, image_width, image_lang
2194 FROM ' . STYLES_IMAGESET_DATA_TABLE . "
2195 WHERE imageset_id = $style_id
2196 AND image_lang <> ''";
2197 $result = $db->sql_query($sql);
2198 while ($row = $db->sql_fetchrow($result))
2200 $imageset_lang[$row['image_lang']][$row['image_name']] = $row['image_filename'] . ($row['image_height'] ? '*' . $row['image_height']: '') . ($row['image_width'] ? '*' . $row['image_width']: '');
2202 $db->sql_freeresult($result);
2204 foreach ($imageset_lang as $lang => $imageset_localized)
2206 $imageset_cfg = str_replace(array('{MODE}', '{NAME}', '{COPYRIGHT}', '{VERSION}'), array($mode, $style_row['imageset_name'], $style_row['imageset_copyright'], $config['version']), $this->imageset_cfg);
2208 foreach ($this->imageset_keys as $topic => $key_array)
2210 foreach ($key_array as $key)
2212 if (isset($imageset_localized[$key]))
2214 $imageset_cfg .= "\nimg_" . $key . ' = ' . str_replace("styles/{$style_row['imageset_path']}/imageset/", '{PATH}', $imageset_localized[$key]);
2220 'src' => trim($imageset_cfg),
2221 'prefix' => 'imageset/' . $lang . '/imageset.cfg'
2225 unset($imageset_cfg);
2247 $error[] = $user->lang[$l_prefix . '_ERR_ARCHIVE'];
2250 if (!sizeof($error))
2252 include($phpbb_root_path . 'includes/functions_compress.' . $phpEx);
2254 if ($mode == 'style')
2256 $path = preg_replace('#[^\w-]+#', '_', $style_row['style_name']);
2260 $path = $style_row[$mode . '_path'];
2263 if ($format == 'zip')
2265 $compress = new compress_zip('w', $phpbb_root_path . "store/$path$ext");
2269 $compress = new compress_tar('w', $phpbb_root_path . "store/$path$ext", $ext);
2274 foreach ($files as $file_ary)
2276 $compress->add_file($file_ary['src'], $file_ary['prefix-'], $file_ary['prefix+'], $file_ary['exclude']);
2282 foreach ($data as $data_ary)
2284 $compress->add_data($data_ary['src'], $data_ary['prefix']);
2290 add_log('admin', 'LOG_' . $l_prefix . '_EXPORT', $style_row[$mode . '_name']);
2294 $compress->download($path);
2295 @unlink("{$phpbb_root_path}store/$path$ext");
2299 trigger_error(sprintf($user->lang[$l_prefix . '_EXPORTED'], "store/$path$ext") . adm_back_link($this->u_action));
2303 $sql = "SELECT {$mode}_id, {$mode}_name
2304 FROM " . (($mode == 'style') ? STYLES_TABLE : $sql_from) . "
2305 WHERE {$mode}_id = $style_id";
2306 $result = $db->sql_query($sql);
2307 $style_row = $db->sql_fetchrow($result);
2308 $db->sql_freeresult($result);
2312 trigger_error($user->lang['NO_' . $l_prefix] . adm_back_link($this->u_action), E_USER_WARNING);
2315 $this->page_title = $l_prefix . '_EXPORT';
2317 $format_buttons = '';
2318 foreach ($methods as $method)
2320 $format_buttons .= '<label><input type="radio"' . ((!$format_buttons) ? ' id="format"' : '') . ' class="radio" value="' . $method . '" name="format"' . (($method == $format) ? ' checked="checked"' : '') . ' /> ' . $method . '</label>';
2323 $template->assign_vars(array(
2325 'S_ERROR_MSG' => (sizeof($error)) ? true : false,
2326 'S_STYLE' => ($mode == 'style') ? true : false,
2328 'L_TITLE' => $user->lang[$this->page_title],
2329 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
2330 'L_NAME' => $user->lang[$l_prefix . '_NAME'],
2332 'U_ACTION' => $this->u_action . '&action=export&id=' . $style_id,
2333 'U_BACK' => $this->u_action,
2335 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
2336 'NAME' => $style_row[$mode . '_name'],
2337 'FORMAT_BUTTONS' => $format_buttons)
2344 function details($mode, $style_id)
2346 global $template, $db, $config, $user, $safe_mode, $cache, $phpbb_root_path;
2348 $update = (isset($_POST['update'])) ? true : false;
2349 $l_type = strtoupper($mode);
2352 $element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
2357 $sql_from = STYLES_TABLE;
2361 $sql_from = STYLES_TEMPLATE_TABLE;
2365 $sql_from = STYLES_THEME_TABLE;
2369 $sql_from = STYLES_IMAGESET_TABLE;
2375 WHERE {$mode}_id = $style_id";
2376 $result = $db->sql_query($sql);
2377 $style_row = $db->sql_fetchrow($result);
2378 $db->sql_freeresult($result);
2382 trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
2385 $style_row['style_default'] = ($mode == 'style' && $config['default_style'] == $style_id) ? 1 : 0;
2389 $name = utf8_normalize_nfc(request_var('name', '', true));
2390 $copyright = utf8_normalize_nfc(request_var('copyright', '', true));
2392 $template_id = request_var('template_id', 0);
2393 $theme_id = request_var('theme_id', 0);
2394 $imageset_id = request_var('imageset_id', 0);
2396 $style_active = request_var('style_active', 0);
2397 $style_default = request_var('style_default', 0);
2398 $store_db = request_var('store_db', 0);
2400 // If the admin selected the style to be the default style, but forgot to activate it... we will do it for him
2406 $sql = "SELECT {$mode}_id, {$mode}_name
2408 WHERE {$mode}_id <> $style_id
2409 AND LOWER({$mode}_name) = '" . $db->sql_escape(strtolower($name)) . "'";
2410 $result = $db->sql_query($sql);
2411 $conflict = $db->sql_fetchrow($result);
2412 $db->sql_freeresult($result);
2414 if ($mode == 'style' && (!$template_id || !$theme_id || !$imageset_id))
2416 $error[] = $user->lang['STYLE_ERR_NO_IDS'];
2419 if ($mode == 'style' && $style_row['style_active'] && !$style_active && $config['default_style'] == $style_id)
2421 $error[] = $user->lang['DEACTIVATE_DEFAULT'];
2424 if (!$name || $conflict)
2426 $error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
2429 if ($mode === 'theme' || $mode === 'template')
2431 // a rather elaborate check we have to do here once to avoid trouble later
2432 $check = "{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . (($mode === 'theme') ? '/theme/stylesheet.css' : '/template');
2433 if (($style_row["{$mode}_storedb"] != $store_db) && !$store_db && ($safe_mode || !phpbb_is_writable($check)))
2435 $error[] = $user->lang['EDIT_' . strtoupper($mode) . '_STORED_DB'];
2439 // themes which have to be parsed have to go into db
2440 if ($mode == 'theme')
2442 $cfg = parse_cfg_file("{$phpbb_root_path}styles/" . $style_row["{$mode}_path"] . "/theme/theme.cfg");
2444 if (isset($cfg['parse_css_file']) && $cfg['parse_css_file'] && !$store_db)
2446 $error[] = $user->lang['EDIT_THEME_STORE_PARSED'];
2452 if (!sizeof($error))
2454 // Check length settings
2455 if (utf8_strlen($name) > 30)
2457 $error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
2460 if (utf8_strlen($copyright) > 60)
2462 $error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
2467 if ($update && sizeof($error))
2469 $style_row = array_merge($style_row, array(
2470 'template_id' => $template_id,
2471 'theme_id' => $theme_id,
2472 'imageset_id' => $imageset_id,
2473 'style_active' => $style_active,
2474 $mode . '_storedb' => $store_db,
2475 $mode . '_name' => $name,
2476 $mode . '_copyright' => $copyright)
2480 // User has submitted form and no errors have occurred
2481 if ($update && !sizeof($error))
2484 $mode . '_name' => $name,
2485 $mode . '_copyright' => $copyright
2493 'template_id' => (int) $template_id,
2494 'theme_id' => (int) $theme_id,
2495 'imageset_id' => (int) $imageset_id,
2496 'style_active' => (int) $style_active,
2505 if ($style_row['theme_storedb'] != $store_db)
2509 if (!$style_row['theme_storedb'])
2511 $theme_data = $this->db_theme_data($style_row);
2513 else if (!$store_db && !$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css"))
2516 $theme_data = $style_row['theme_data'];
2518 if ($fp = @fopen("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css", 'wb'))
2520 $store_db = (@fwrite($fp, str_replace("styles/{$style_row['theme_path']}/theme/", './', $theme_data))) ? 0 : 1;
2526 'theme_mtime' => ($store_db) ? filemtime("{$phpbb_root_path}styles/{$style_row['theme_path']}/theme/stylesheet.css") : 0,
2527 'theme_storedb' => $store_db,
2528 'theme_data' => ($store_db) ? $theme_data : '',
2535 if ($style_row['template_storedb'] != $store_db)
2537 if ($super = $this->get_super($mode, $style_row['template_id']))
2539 $error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
2544 if (!$store_db && !$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$style_row['template_path']}/template"))
2546 $err = $this->store_in_fs('template', $style_row['template_id']);
2554 $this->store_in_db('template', $style_row['template_id']);
2558 // We no longer store within the db, but are also not able to update the file structure
2559 // Since the admin want to switch this, we adhere to his decision. But we also need to remove the cache
2560 $sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
2561 WHERE template_id = $style_id";
2562 $db->sql_query($sql);
2566 'template_storedb' => $store_db,
2573 if (sizeof($sql_ary))
2575 $sql = "UPDATE $sql_from
2576 SET " . $db->sql_build_array('UPDATE', $sql_ary) . "
2577 WHERE {$mode}_id = $style_id";
2578 $db->sql_query($sql);
2580 // Making this the default style?
2581 if ($mode == 'style' && $style_default)
2583 set_config('default_style', $style_id);
2587 $cache->destroy('sql', STYLES_TABLE);
2589 add_log('admin', 'LOG_' . $l_type . '_EDIT_DETAILS', $name);
2592 trigger_error(implode('<br />', $error) . adm_back_link($this->u_action), E_USER_WARNING);
2596 trigger_error($user->lang[$l_type . '_DETAILS_UPDATED'] . adm_back_link($this->u_action));
2600 if ($mode == 'style')
2602 foreach ($element_ary as $element => $table)
2604 $sql = "SELECT {$element}_id, {$element}_name
2606 ORDER BY {$element}_id ASC";
2607 $result = $db->sql_query($sql);
2609 ${$element . '_options'} = '';
2610 while ($row = $db->sql_fetchrow($result))
2612 $selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
2613 ${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
2615 $db->sql_freeresult($result);
2619 if ($mode == 'template')
2622 if (isset($style_row[$mode . '_inherits_id']) && $style_row['template_inherits_id'])
2624 $super = $this->get_super($mode, $style_row['template_id']);
2628 $this->page_title = 'EDIT_DETAILS_' . $l_type;
2630 $template->assign_vars(array(
2631 'S_DETAILS' => true,
2632 'S_ERROR_MSG' => (sizeof($error)) ? true : false,
2633 'S_STYLE' => ($mode == 'style') ? true : false,
2634 'S_TEMPLATE' => ($mode == 'template') ? true : false,
2635 'S_THEME' => ($mode == 'theme') ? true : false,
2636 'S_IMAGESET' => ($mode == 'imageset') ? true : false,
2637 'S_STORE_DB' => (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
2638 'S_STORE_DB_DISABLED' => (isset($style_row[$mode . '_inherits_id'])) ? $style_row[$mode . '_inherits_id'] : 0,
2639 'S_STYLE_ACTIVE' => (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
2640 'S_STYLE_DEFAULT' => (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
2641 'S_SUPERTEMPLATE' => (isset($style_row[$mode . '_inherits_id']) && $style_row[$mode . '_inherits_id']) ? $super['template_name'] : 0,
2643 'S_TEMPLATE_OPTIONS' => ($mode == 'style') ? $template_options : '',
2644 'S_THEME_OPTIONS' => ($mode == 'style') ? $theme_options : '',
2645 'S_IMAGESET_OPTIONS' => ($mode == 'style') ? $imageset_options : '',
2647 'U_ACTION' => $this->u_action . '&action=details&id=' . $style_id,
2648 'U_BACK' => $this->u_action,
2650 'L_TITLE' => $user->lang[$this->page_title],
2651 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
2652 'L_NAME' => $user->lang[$l_type . '_NAME'],
2653 'L_LOCATION' => ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
2654 'L_LOCATION_EXPLAIN' => ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
2656 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
2657 'NAME' => $style_row[$mode . '_name'],
2658 'COPYRIGHT' => $style_row[$mode . '_copyright'],
2664 * Load css file contents
2666 function load_css_file($path, $filename)
2668 global $phpbb_root_path;
2670 $file = "{$phpbb_root_path}styles/$path/theme/$filename";
2672 if (file_exists($file) && ($content = file_get_contents($file)))
2674 $content = trim($content);
2680 if (defined('DEBUG'))
2682 $content = "/* BEGIN @include $filename */ \n $content \n /* END @include $filename */ \n";
2689 * Returns a string containing the value that should be used for the theme_data column in the theme database table.
2690 * Includes contents of files loaded via @import
2692 * @param array $theme_row is an associative array containing the theme's current database entry
2693 * @param mixed $stylesheet can either be the new content for the stylesheet or false to load from the standard file
2694 * @param string $root_path should only be used in case you want to use a different root path than "{$phpbb_root_path}styles/{$theme_row['theme_path']}"
2696 * @return string Stylesheet data for theme_data column in the theme table
2698 function db_theme_data($theme_row, $stylesheet = false, $root_path = '')
2700 global $phpbb_root_path;
2704 $root_path = $phpbb_root_path . 'styles/' . $theme_row['theme_path'];
2710 if (file_exists($root_path . '/theme/stylesheet.css'))
2712 $stylesheet = file_get_contents($root_path . '/theme/stylesheet.css');
2716 // Match CSS imports
2718 preg_match_all('/@import url\((["\'])(.*)\1\);/i', $stylesheet, $matches);
2720 // remove commented stylesheets (very simple parser, allows only whitespace
2721 // around an @import statement)
2722 preg_match_all('#/\*\s*@import url\((["\'])(.*)\1\);\s\*/#i', $stylesheet, $commented);
2723 $matches[2] = array_diff($matches[2], $commented[2]);
2725 if (sizeof($matches))
2727 foreach ($matches[0] as $idx => $match)
2729 if (isset($matches[2][$idx]))
2731 $stylesheet = str_replace($match, acp_styles::load_css_file($theme_row['theme_path'], $matches[2][$idx]), $stylesheet);
2737 return str_replace('./', 'styles/' . $theme_row['theme_path'] . '/theme/', $stylesheet);
2741 * Store template files into db
2743 function store_templates($mode, $style_id, $template_path, $filelist)
2745 global $phpbb_root_path, $phpEx, $db;
2747 $template_path = $template_path . '/template/';
2748 $includes = array();
2749 foreach ($filelist as $pathfile => $file_ary)
2751 foreach ($file_ary as $file)
2753 if (!($fp = @fopen("{$phpbb_root_path}styles/$template_path$pathfile$file", 'r')))
2755 trigger_error("Could not open {$phpbb_root_path}styles/$template_path$pathfile$file", E_USER_ERROR);
2758 $filesize = filesize("{$phpbb_root_path}styles/$template_path$pathfile$file");
2762 $template_data = fread($fp, $filesize);
2773 if (preg_match_all('#<!-- INCLUDE (.*?\.html) -->#is', $template_data, $matches))
2775 foreach ($matches[1] as $match)
2777 $includes[trim($match)][] = $file;
2783 foreach ($filelist as $pathfile => $file_ary)
2785 foreach ($file_ary as $file)
2788 if (strpos($file, 'index.') === 0)
2793 // We could do this using extended inserts ... but that could be one
2794 // heck of a lot of data ...
2796 'template_id' => (int) $style_id,
2797 'template_filename' => "$pathfile$file",
2798 'template_included' => (isset($includes[$file])) ? implode(':', $includes[$file]) . ':' : '',
2799 'template_mtime' => (int) filemtime("{$phpbb_root_path}styles/$template_path$pathfile$file"),
2800 'template_data' => (string) file_get_contents("{$phpbb_root_path}styles/$template_path$pathfile$file"),
2803 if ($mode == 'insert')
2805 $sql = 'INSERT INTO ' . STYLES_TEMPLATE_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
2809 $sql = 'UPDATE ' . STYLES_TEMPLATE_DATA_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
2810 WHERE template_id = $style_id
2811 AND template_filename = '" . $db->sql_escape("$pathfile$file") . "'";
2813 $db->sql_query($sql);
2819 * Returns an array containing all template filenames for one template that are currently cached.
2821 * @param string $template_path contains the name of the template's folder in /styles/
2823 * @return array of filenames that exist in /styles/$template_path/template/ (without extension!)
2825 function template_cache_filelist($template_path)
2827 global $phpbb_root_path, $phpEx, $user;
2829 $cache_prefix = 'tpl_' . str_replace('_', '-', $template_path);
2831 if (!($dp = @opendir("{$phpbb_root_path}cache")))
2833 trigger_error($user->lang['TEMPLATE_ERR_CACHE_READ'] . adm_back_link($this->u_action), E_USER_WARNING);
2836 $file_ary = array();
2837 while ($file = readdir($dp))
2839 if ($file[0] == '.')
2844 if (is_file($phpbb_root_path . 'cache/' . $file) && (strpos($file, $cache_prefix) === 0))
2846 $file_ary[] = str_replace('.', '/', preg_replace('#^' . preg_quote($cache_prefix, '#') . '_(.*?)\.html\.' . $phpEx . '$#i', '\1', $file));
2855 * Destroys cached versions of template files
2857 * @param array $template_row contains the template's row in the STYLES_TEMPLATE_TABLE database table
2858 * @param mixed $file_ary is optional and may contain an array of template file names which should be refreshed in the cache.
2859 * The file names should be the original template file names and not the cache file names.
2861 function clear_template_cache($template_row, $file_ary = false)
2863 global $phpbb_root_path, $phpEx, $user;
2865 $cache_prefix = 'tpl_' . str_replace('_', '-', $template_row['template_path']);
2867 if (!$file_ary || !is_array($file_ary))
2869 $file_ary = $this->template_cache_filelist($template_row['template_path']);
2870 $log_file_list = $user->lang['ALL_FILES'];
2874 $log_file_list = implode(', ', $file_ary);
2877 foreach ($file_ary as $file)
2879 $file = str_replace('/', '.', $file);
2881 $file = "{$phpbb_root_path}cache/{$cache_prefix}_$file.html.$phpEx";
2882 if (file_exists($file) && is_file($file))
2889 add_log('admin', 'LOG_TEMPLATE_CACHE_CLEARED', $template_row['template_name'], $log_file_list);
2893 * Install Style/Template/Theme/Imageset
2895 function install($mode)
2897 global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
2899 $l_type = strtoupper($mode);
2901 $error = $installcfg = $style_row = array();
2902 $root_path = $cfg_file = '';
2903 $element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
2905 $install_path = request_var('path', '');
2906 $update = (isset($_POST['update'])) ? true : false;
2908 // Installing, obtain cfg file contents
2911 $root_path = $phpbb_root_path . 'styles/' . $install_path . '/';
2912 $cfg_file = ($mode == 'style') ? "$root_path$mode.cfg" : "$root_path$mode/$mode.cfg";
2914 if (!file_exists($cfg_file))
2916 $error[] = $user->lang[$l_type . '_ERR_NOT_' . $l_type];
2920 $installcfg = parse_cfg_file($cfg_file);
2925 if (sizeof($installcfg))
2927 $name = $installcfg['name'];
2928 $copyright = $installcfg['copyright'];
2929 $version = $installcfg['version'];
2933 $mode . '_name' => '',
2934 $mode . '_copyright' => ''
2943 'style_name' => $installcfg['name'],
2944 'style_copyright' => $installcfg['copyright']
2947 $reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
2948 $reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
2949 $reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
2951 // Check to see if each element is already installed, if it is grab the id
2952 foreach ($element_ary as $element => $table)
2954 $style_row = array_merge($style_row, array(
2955 $element . '_id' => 0,
2956 $element . '_name' => '',
2957 $element . '_copyright' => '')
2960 $this->test_installed($element, $error, (${'reqd_' . $element}) ? $phpbb_root_path . 'styles/' . $reqd_template . '/' : $root_path, ${'reqd_' . $element}, $style_row[$element . '_id'], $style_row[$element . '_name'], $style_row[$element . '_copyright']);
2962 if (!$style_row[$element . '_name'])
2964 $style_row[$element . '_name'] = $reqd_template;
2967 // Merge other information to installcfg... if present
2968 $cfg_file = $phpbb_root_path . 'styles/' . $install_path . '/' . $element . '/' . $element . '.cfg';
2970 if (file_exists($cfg_file))
2972 $cfg_contents = parse_cfg_file($cfg_file);
2974 // Merge only specific things. We may need them later.
2975 foreach (array('inherit_from', 'parse_css_file') as $key)
2977 if (!empty($cfg_contents[$key]) && !isset($installcfg[$key]))
2979 $installcfg[$key] = $cfg_contents[$key];
2988 $this->test_installed('template', $error, $root_path, false, $style_row['template_id'], $style_row['template_name'], $style_row['template_copyright']);
2992 $this->test_installed('theme', $error, $root_path, false, $style_row['theme_id'], $style_row['theme_name'], $style_row['theme_copyright']);
2996 $this->test_installed('imageset', $error, $root_path, false, $style_row['imageset_id'], $style_row['imageset_name'], $style_row['imageset_copyright']);
3002 trigger_error($user->lang['NO_' . $l_type] . adm_back_link($this->u_action), E_USER_WARNING);
3005 $style_row['store_db'] = request_var('store_db', 0);
3006 $style_row['style_active'] = request_var('style_active', 1);
3007 $style_row['style_default'] = request_var('style_default', 0);
3009 // User has submitted form and no errors have occurred
3010 if ($update && !sizeof($error))
3012 if ($mode == 'style')
3014 foreach ($element_ary as $element => $table)
3016 ${$element . '_root_path'} = (${'reqd_' . $element}) ? $phpbb_root_path . 'styles/' . ${'reqd_' . $element} . '/' : false;
3017 ${$element . '_path'} = (${'reqd_' . $element}) ? ${'reqd_' . $element} : false;
3019 $this->install_style($error, 'install', $root_path, $style_row['style_id'], $style_row['style_name'], $install_path, $style_row['style_copyright'], $style_row['style_active'], $style_row['style_default'], $style_row, $template_root_path, $template_path, $theme_root_path, $theme_path, $imageset_root_path, $imageset_path);
3023 $style_row['store_db'] = $this->install_element($mode, $error, 'install', $root_path, $style_row[$mode . '_id'], $style_row[$mode . '_name'], $install_path, $style_row[$mode . '_copyright'], $style_row['store_db']);
3026 if (!sizeof($error))
3028 $cache->destroy('sql', STYLES_TABLE);
3030 $message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
3031 trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
3035 $this->page_title = 'INSTALL_' . $l_type;
3037 $template->assign_vars(array(
3038 'S_DETAILS' => true,
3039 'S_INSTALL' => true,
3040 'S_ERROR_MSG' => (sizeof($error)) ? true : false,
3041 'S_LOCATION' => (isset($installcfg['inherit_from']) && $installcfg['inherit_from']) ? false : true,
3042 'S_STYLE' => ($mode == 'style') ? true : false,
3043 'S_TEMPLATE' => ($mode == 'template') ? true : false,
3044 'S_SUPERTEMPLATE' => (isset($installcfg['inherit_from'])) ? $installcfg['inherit_from'] : '',
3045 'S_THEME' => ($mode == 'theme') ? true : false,
3047 'S_STORE_DB' => (isset($style_row[$mode . '_storedb'])) ? $style_row[$mode . '_storedb'] : 0,
3048 'S_STYLE_ACTIVE' => (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
3049 'S_STYLE_DEFAULT' => (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
3051 'U_ACTION' => $this->u_action . "&action=install&path=" . urlencode($install_path),
3052 'U_BACK' => $this->u_action,
3054 'L_TITLE' => $user->lang[$this->page_title],
3055 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
3056 'L_NAME' => $user->lang[$l_type . '_NAME'],
3057 'L_LOCATION' => ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
3058 'L_LOCATION_EXPLAIN' => ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
3060 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
3061 'NAME' => $style_row[$mode . '_name'],
3062 'COPYRIGHT' => $style_row[$mode . '_copyright'],
3063 'TEMPLATE_NAME' => ($mode == 'style') ? $style_row['template_name'] : '',
3064 'THEME_NAME' => ($mode == 'style') ? $style_row['theme_name'] : '',
3065 'IMAGESET_NAME' => ($mode == 'style') ? $style_row['imageset_name'] : '')
3074 global $phpbb_root_path, $phpEx, $config, $db, $cache, $user, $template;
3076 $l_type = strtoupper($mode);
3077 $element_ary = array('template' => STYLES_TEMPLATE_TABLE, 'theme' => STYLES_THEME_TABLE, 'imageset' => STYLES_IMAGESET_TABLE);
3081 $mode . '_name' => utf8_normalize_nfc(request_var('name', '', true)),
3082 $mode . '_copyright' => utf8_normalize_nfc(request_var('copyright', '', true)),
3086 'store_db' => request_var('store_db', 0),
3087 'style_active' => request_var('style_active', 1),
3088 'style_default' => request_var('style_default', 0),
3091 $basis = request_var('basis', 0);
3092 $update = (isset($_POST['update'])) ? true : false;
3099 $sql_select = 'template_id, theme_id, imageset_id';
3100 $sql_from = STYLES_TABLE;
3104 $sql_select = 'template_id';
3105 $sql_from = STYLES_TEMPLATE_TABLE;
3109 $sql_select = 'theme_id';
3110 $sql_from = STYLES_THEME_TABLE;
3114 $sql_select = 'imageset_id';
3115 $sql_from = STYLES_IMAGESET_TABLE;
3119 $sql = "SELECT $sql_select
3121 WHERE {$mode}_id = $basis";
3122 $result = $db->sql_query($sql);
3123 $row = $db->sql_fetchrow($result);
3124 $db->sql_freeresult($result);
3128 $error[] = $user->lang['NO_' . $l_type];
3131 if (!sizeof($error))
3133 $style_row['template_id'] = (isset($row['template_id'])) ? $row['template_id'] : $style_row['template_id'];
3134 $style_row['theme_id'] = (isset($row['theme_id'])) ? $row['theme_id'] : $style_row['theme_id'];
3135 $style_row['imageset_id'] = (isset($row['imageset_id'])) ? $row['imageset_id'] : $style_row['imageset_id'];
3141 $style_row['template_id'] = request_var('template_id', $style_row['template_id']);
3142 $style_row['theme_id'] = request_var('theme_id', $style_row['theme_id']);
3143 $style_row['imageset_id'] = request_var('imageset_id', $style_row['imageset_id']);
3145 if ($mode == 'style' && (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id']))
3147 $error[] = $user->lang['STYLE_ERR_NO_IDS'];
3151 // User has submitted form and no errors have occurred
3152 if ($update && !sizeof($error))
3154 if ($mode == 'style')
3156 $style_row['style_id'] = 0;
3158 $this->install_style($error, 'add', '', $style_row['style_id'], $style_row['style_name'], '', $style_row['style_copyright'], $style_row['style_active'], $style_row['style_default'], $style_row);
3161 if (!sizeof($error))
3163 $cache->destroy('sql', STYLES_TABLE);
3165 $message = ($style_row['store_db']) ? '_ADDED_DB' : '_ADDED';
3166 trigger_error($user->lang[$l_type . $message] . adm_back_link($this->u_action));
3170 if ($mode == 'style')
3172 foreach ($element_ary as $element => $table)
3174 $sql = "SELECT {$element}_id, {$element}_name
3176 ORDER BY {$element}_id ASC";
3177 $result = $db->sql_query($sql);
3179 ${$element . '_options'} = '';
3180 while ($row = $db->sql_fetchrow($result))
3182 $selected = ($row[$element . '_id'] == $style_row[$element . '_id']) ? ' selected="selected"' : '';
3183 ${$element . '_options'} .= '<option value="' . $row[$element . '_id'] . '"' . $selected . '>' . $row[$element . '_name'] . '</option>';
3185 $db->sql_freeresult($result);
3189 $this->page_title = 'ADD_' . $l_type;
3191 $template->assign_vars(array(
3192 'S_DETAILS' => true,
3194 'S_ERROR_MSG' => (sizeof($error)) ? true : false,
3195 'S_STYLE' => ($mode == 'style') ? true : false,
3196 'S_TEMPLATE' => ($mode == 'template') ? true : false,
3197 'S_THEME' => ($mode == 'theme') ? true : false,
3198 'S_BASIS' => ($basis) ? true : false,
3200 'S_STORE_DB' => (isset($style_row['storedb'])) ? $style_row['storedb'] : 0,
3201 'S_STYLE_ACTIVE' => (isset($style_row['style_active'])) ? $style_row['style_active'] : 0,
3202 'S_STYLE_DEFAULT' => (isset($style_row['style_default'])) ? $style_row['style_default'] : 0,
3203 'S_TEMPLATE_OPTIONS' => ($mode == 'style') ? $template_options : '',
3204 'S_THEME_OPTIONS' => ($mode == 'style') ? $theme_options : '',
3205 'S_IMAGESET_OPTIONS' => ($mode == 'style') ? $imageset_options : '',
3207 'U_ACTION' => $this->u_action . '&action=add&basis=' . $basis,
3208 'U_BACK' => $this->u_action,
3210 'L_TITLE' => $user->lang[$this->page_title],
3211 'L_EXPLAIN' => $user->lang[$this->page_title . '_EXPLAIN'],
3212 'L_NAME' => $user->lang[$l_type . '_NAME'],
3213 'L_LOCATION' => ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION'] : '',
3214 'L_LOCATION_EXPLAIN' => ($mode == 'template' || $mode == 'theme') ? $user->lang[$l_type . '_LOCATION_EXPLAIN'] : '',
3216 'ERROR_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
3217 'NAME' => $style_row[$mode . '_name'],
3218 'COPYRIGHT' => $style_row[$mode . '_copyright'])
3225 $reqd_template = (isset($installcfg['required_template'])) ? $installcfg['required_template'] : false;
3226 $reqd_theme = (isset($installcfg['required_theme'])) ? $installcfg['required_theme'] : false;
3227 $reqd_imageset = (isset($installcfg['required_imageset'])) ? $installcfg['required_imageset'] : false;
3229 // Check to see if each element is already installed, if it is grab the id
3230 foreach ($element_ary as $element => $table)
3232 $style_row = array_merge($style_row, array(
3233 $element . '_id' => 0,
3234 $element . '_name' => '',
3235 $element . '_copyright' => '')
3238 $this->test_installed($element, $error, $root_path, ${'reqd_' . $element}, $style_row[$element . '_id'], $style_row[$element . '_name'], $style_row[$element . '_copyright']);
3239 * Is this element installed? If not, grab its cfg details
3241 function test_installed($element, &$error, $root_path, $reqd_name, &$id, &$name, &$copyright)
3248 $sql_from = STYLES_TEMPLATE_TABLE;
3252 $sql_from = STYLES_THEME_TABLE;
3256 $sql_from = STYLES_IMAGESET_TABLE;
3260 $l_element = strtoupper($element);
3262 $chk_name = ($reqd_name !== false) ? $reqd_name : $name;
3264 $sql = "SELECT {$element}_id, {$element}_name
3266 WHERE {$element}_name = '" . $db->sql_escape($chk_name) . "'";
3267 $result = $db->sql_query($sql);
3269 if ($row = $db->sql_fetchrow($result))
3271 $name = $row[$element . '_name'];
3272 $id = $row[$element . '_id'];
3276 if (!($cfg = @file("$root_path$element/$element.cfg")))
3278 $error[] = sprintf($user->lang['REQUIRES_' . $l_element], $reqd_name);
3282 $cfg = parse_cfg_file("$root_path$element/$element.cfg", $cfg);
3284 $name = $cfg['name'];
3285 $copyright = $cfg['copyright'];
3290 $db->sql_freeresult($result);
3296 function install_style(&$error, $action, $root_path, &$id, $name, $path, $copyright, $active, $default, &$style_row, $template_root_path = false, $template_path = false, $theme_root_path = false, $theme_path = false, $imageset_root_path = false, $imageset_path = false)
3298 global $config, $db, $user;
3300 $element_ary = array('template', 'theme', 'imageset');
3304 $error[] = $user->lang['STYLE_ERR_STYLE_NAME'];
3307 // Check length settings
3308 if (utf8_strlen($name) > 30)
3310 $error[] = $user->lang['STYLE_ERR_NAME_LONG'];
3313 if (utf8_strlen($copyright) > 60)
3315 $error[] = $user->lang['STYLE_ERR_COPY_LONG'];
3318 // Check if the name already exist
3319 $sql = 'SELECT style_id
3320 FROM ' . STYLES_TABLE . "
3321 WHERE style_name = '" . $db->sql_escape($name) . "'";
3322 $result = $db->sql_query($sql);
3323 $row = $db->sql_fetchrow($result);
3324 $db->sql_freeresult($result);
3328 $error[] = $user->lang['STYLE_ERR_NAME_EXIST'];
3336 foreach ($element_ary as $element)
3338 // Zero id value ... need to install element ... run usual checks
3339 // and do the install if necessary
3340 if (!$style_row[$element . '_id'])
3342 $this->install_element($element, $error, $action, (${$element . '_root_path'}) ? ${$element . '_root_path'} : $root_path, $style_row[$element . '_id'], $style_row[$element . '_name'], (${$element . '_path'}) ? ${$element . '_path'} : $path, $style_row[$element . '_copyright']);
3346 if (!$style_row['template_id'] || !$style_row['theme_id'] || !$style_row['imageset_id'])
3348 $error[] = $user->lang['STYLE_ERR_NO_IDS'];
3356 $db->sql_transaction('begin');
3359 'style_name' => $name,
3360 'style_copyright' => $copyright,
3361 'style_active' => (int) $active,
3362 'template_id' => (int) $style_row['template_id'],
3363 'theme_id' => (int) $style_row['theme_id'],
3364 'imageset_id' => (int) $style_row['imageset_id'],
3367 $sql = 'INSERT INTO ' . STYLES_TABLE . '
3368 ' . $db->sql_build_array('INSERT', $sql_ary);
3369 $db->sql_query($sql);
3371 $id = $db->sql_nextid();
3375 $sql = 'UPDATE ' . USERS_TABLE . "
3376 SET user_style = $id
3377 WHERE user_style = " . $config['default_style'];
3378 $db->sql_query($sql);
3380 set_config('default_style', $id);
3383 $db->sql_transaction('commit');
3385 add_log('admin', 'LOG_STYLE_ADD', $name);
3389 * Install/add an element, doing various checks as we go
3391 function install_element($mode, &$error, $action, $root_path, &$id, $name, $path, $copyright, $store_db = 0)
3393 global $phpbb_root_path, $db, $user;
3395 // we parse the cfg here (again)
3396 $cfg_data = parse_cfg_file("$root_path$mode/$mode.cfg");
3401 $sql_from = STYLES_TEMPLATE_TABLE;
3405 $sql_from = STYLES_THEME_TABLE;
3409 $sql_from = STYLES_IMAGESET_TABLE;
3413 $l_type = strtoupper($mode);
3417 $error[] = $user->lang[$l_type . '_ERR_STYLE_NAME'];
3420 // Check length settings
3421 if (utf8_strlen($name) > 30)
3423 $error[] = $user->lang[$l_type . '_ERR_NAME_LONG'];
3426 if (utf8_strlen($copyright) > 60)
3428 $error[] = $user->lang[$l_type . '_ERR_COPY_LONG'];
3431 // Check if the name already exist
3432 $sql = "SELECT {$mode}_id
3434 WHERE {$mode}_name = '" . $db->sql_escape($name) . "'";
3435 $result = $db->sql_query($sql);
3436 $row = $db->sql_fetchrow($result);
3437 $db->sql_freeresult($result);
3441 // If it exist, we just use the style on installation
3442 if ($action == 'install')
3444 $id = $row[$mode . '_id'];
3448 $error[] = $user->lang[$l_type . '_ERR_NAME_EXIST'];
3451 if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
3453 if ($mode === 'template')
3455 $select_bf = ', bbcode_bitfield';
3462 $sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path, {$mode}_storedb $select_bf
3464 WHERE {$mode}_name = '" . $db->sql_escape($cfg_data['inherit_from']) . "'
3465 AND {$mode}_inherits_id = 0";
3466 $result = $db->sql_query($sql);
3467 $row = $db->sql_fetchrow($result);
3468 $db->sql_freeresult($result);
3471 $error[] = sprintf($user->lang[$l_type . '_ERR_REQUIRED_OR_INCOMPLETE'], $cfg_data['inherit_from']);
3475 $inherit_id = $row["{$mode}_id"];
3476 $inherit_path = $row["{$mode}_path"];
3477 $inherit_bf = ($mode === 'template') ? $row["bbcode_bitfield"] : false;
3478 $cfg_data['store_db'] = $row["{$mode}_storedb"];
3479 $store_db = $row["{$mode}_storedb"];
3486 $inherit_bf = false;
3495 $mode . '_name' => $name,
3496 $mode . '_copyright' => $copyright,
3497 $mode . '_path' => $path,
3503 // We check if the template author defined a different bitfield
3504 if (!empty($cfg_data['template_bitfield']))
3506 $sql_ary['bbcode_bitfield'] = $cfg_data['template_bitfield'];
3508 else if ($inherit_bf)
3510 $sql_ary['bbcode_bitfield'] = $inherit_bf;
3514 $sql_ary['bbcode_bitfield'] = TEMPLATE_BITFIELD;
3517 // We set a pre-defined bitfield here which we may use further in 3.2
3519 'template_storedb' => $store_db,
3521 if (isset($cfg_data['inherit_from']) && $cfg_data['inherit_from'])
3524 'template_inherits_id' => $inherit_id,
3525 'template_inherit_path' => $inherit_path,
3531 // We are only interested in the theme configuration for now
3533 if (isset($cfg_data['parse_css_file']) && $cfg_data['parse_css_file'])
3539 'theme_storedb' => $store_db,
3540 'theme_data' => ($store_db) ? $this->db_theme_data($sql_ary, false, $root_path) : '',
3541 'theme_mtime' => (int) filemtime("{$phpbb_root_path}styles/$path/theme/stylesheet.css")
3545 // all the heavy lifting is done later
3550 $db->sql_transaction('begin');
3552 $sql = "INSERT INTO $sql_from
3553 " . $db->sql_build_array('INSERT', $sql_ary);
3554 $db->sql_query($sql);
3556 $id = $db->sql_nextid();
3558 if ($mode == 'template' && $store_db)
3560 $filelist = filelist("{$root_path}template", '', 'html');
3561 $this->store_templates('insert', $id, $path, $filelist);
3563 else if ($mode == 'imageset')
3565 $cfg_data = parse_cfg_file("$root_path$mode/imageset.cfg");
3567 $imageset_definitions = array();
3568 foreach ($this->imageset_keys as $topic => $key_array)
3570 $imageset_definitions = array_merge($imageset_definitions, $key_array);
3573 foreach ($cfg_data as $key => $value)
3575 if (strpos($value, '*') !== false)
3577 if (substr($value, -1, 1) === '*')
3579 list($image_filename, $image_height) = explode('*', $value);
3584 list($image_filename, $image_height, $image_width) = explode('*', $value);
3589 $image_filename = $value;
3590 $image_height = $image_width = 0;
3593 if (strpos($key, 'img_') === 0 && $image_filename)
3595 $key = substr($key, 4);
3596 if (in_array($key, $imageset_definitions))
3599 'image_name' => $key,
3600 'image_filename' => str_replace('{PATH}', "styles/$path/imageset/", trim($image_filename)),
3601 'image_height' => (int) $image_height,
3602 'image_width' => (int) $image_width,
3603 'imageset_id' => (int) $id,
3606 $db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
3612 $sql = 'SELECT lang_dir
3613 FROM ' . LANG_TABLE;
3614 $result = $db->sql_query($sql);
3616 while ($row = $db->sql_fetchrow($result))
3618 if (@file_exists("$root_path$mode/{$row['lang_dir']}/imageset.cfg"))
3620 $cfg_data_imageset_data = parse_cfg_file("$root_path$mode/{$row['lang_dir']}/imageset.cfg");
3621 foreach ($cfg_data_imageset_data as $image_name => $value)
3623 if (strpos($value, '*') !== false)
3625 if (substr($value, -1, 1) === '*')
3627 list($image_filename, $image_height) = explode('*', $value);
3632 list($image_filename, $image_height, $image_width) = explode('*', $value);
3637 $image_filename = $value;
3638 $image_height = $image_width = 0;
3641 if (strpos($image_name, 'img_') === 0 && $image_filename)
3643 $image_name = substr($image_name, 4);
3644 if (in_array($image_name, $imageset_definitions))
3647 'image_name' => $image_name,
3648 'image_filename' => $image_filename,
3649 'image_height' => (int) $image_height,
3650 'image_width' => (int) $image_width,
3651 'imageset_id' => (int) $id,
3652 'image_lang' => $row['lang_dir'],
3654 $db->sql_query('INSERT INTO ' . STYLES_IMAGESET_DATA_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
3658 unset($cfg_data_imageset_data);
3661 $db->sql_freeresult($result);
3664 $db->sql_transaction('commit');
3666 $log = ($store_db) ? 'LOG_' . $l_type . '_ADD_DB' : 'LOG_' . $l_type . '_ADD_FS';
3667 add_log('admin', $log, $name);
3669 // Return store_db in case it had to be altered
3674 * Checks downwards dependencies
3677 * @param string $mode The element type to check - only template is supported
3678 * @param int $id The template id
3679 * @returns false if no component inherits, array with name, path and id for each subtemplate otherwise
3681 function check_inheritance($mode, $id)
3685 $l_type = strtoupper($mode);
3690 $sql_from = STYLES_TEMPLATE_TABLE;
3694 $sql_from = STYLES_THEME_TABLE;
3698 $sql_from = STYLES_IMAGESET_TABLE;
3702 $sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
3704 WHERE {$mode}_inherits_id = " . (int) $id;
3705 $result = $db->sql_query($sql);
3708 while ($row = $db->sql_fetchrow($result))
3711 $names[$row["{$mode}_id"]] = array(
3712 "{$mode}_id" => $row["{$mode}_id"],
3713 "{$mode}_name" => $row["{$mode}_name"],
3714 "{$mode}_path" => $row["{$mode}_path"],
3717 $db->sql_freeresult($result);
3730 * Checks upwards dependencies
3733 * @param string $mode The element type to check - only template is supported
3734 * @param int $id The template id
3735 * @returns false if the component does not inherit, array with name, path and id otherwise
3737 function get_super($mode, $id)
3741 $l_type = strtoupper($mode);
3746 $sql_from = STYLES_TEMPLATE_TABLE;
3750 $sql_from = STYLES_THEME_TABLE;
3754 $sql_from = STYLES_IMAGESET_TABLE;
3758 $sql = "SELECT {$mode}_inherits_id
3760 WHERE {$mode}_id = " . (int) $id;
3761 $result = $db->sql_query_limit($sql, 1);
3763 if ($row = $db->sql_fetchrow($result))
3765 $db->sql_freeresult($result);
3772 $super_id = $row["{$mode}_inherits_id"];
3774 $sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
3776 WHERE {$mode}_id = " . (int) $super_id;
3778 $result = $db->sql_query_limit($sql, 1);
3779 if ($row = $db->sql_fetchrow($result))
3781 $db->sql_freeresult($result);
3789 * Moves a template set and its subtemplates to the database
3792 * @param string $mode The component to move - only template is supported
3793 * @param int $id The template id
3795 function store_in_db($mode, $id)
3800 $l_type = strtoupper($mode);
3801 if ($super = $this->get_super($mode, $id))
3803 $error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
3807 $sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
3808 FROM " . STYLES_TEMPLATE_TABLE . '
3809 WHERE template_id = ' . (int) $id;
3811 $result = $db->sql_query_limit($sql, 1);
3812 if ($row = $db->sql_fetchrow($result))
3814 $db->sql_freeresult($result);
3815 $subs = $this->check_inheritance($mode, $id);
3817 $this->_store_in_db($mode, $id, $row["{$mode}_path"]);
3818 if ($subs && sizeof($subs))
3820 foreach ($subs as $sub_id => $sub)
3822 if ($err = $this->_store_in_db($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]))
3838 * Moves a template set to the database
3841 * @param string $mode The component to move - only template is supported
3842 * @param int $id The template id
3843 * @param string $path TThe path to the template files
3845 function _store_in_db($mode, $id, $path)
3847 global $phpbb_root_path, $db;
3849 $filelist = filelist("{$phpbb_root_path}styles/{$path}/template", '', 'html');
3850 $this->store_templates('insert', $id, $path, $filelist);
3852 // Okay, we do the query here -shouldn't be triggered often.
3853 $sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
3854 SET template_storedb = 1
3855 WHERE template_id = ' . $id;
3856 $db->sql_query($sql);
3860 * Moves a template set and its subtemplates to the filesystem
3863 * @param string $mode The component to move - only template is supported
3864 * @param int $id The template id
3866 function store_in_fs($mode, $id)
3871 $l_type = strtoupper($mode);
3872 if ($super = $this->get_super($mode, $id))
3874 $error[] = (sprintf($user->lang["{$l_type}_INHERITS"], $super['template_name']));
3878 $sql = "SELECT {$mode}_id, {$mode}_name, {$mode}_path
3879 FROM " . STYLES_TEMPLATE_TABLE . '
3880 WHERE template_id = ' . (int) $id;
3882 $result = $db->sql_query_limit($sql, 1);
3883 if ($row = $db->sql_fetchrow($result))
3885 $db->sql_freeresult($result);
3886 if (!sizeof($error))
3888 $subs = $this->check_inheritance($mode, $id);
3890 $this->_store_in_fs($mode, $id, $row["{$mode}_path"]);
3892 if ($subs && sizeof($subs))
3894 foreach ($subs as $sub_id => $sub)
3896 $this->_store_in_fs($mode, $sub["{$mode}_id"], $sub["{$mode}_path"]);
3902 $this->store_in_db($id, $mode);
3910 * Moves a template set to the filesystem
3913 * @param string $mode The component to move - only template is supported
3914 * @param int $id The template id
3915 * @param string $path The path to the template
3917 function _store_in_fs($mode, $id, $path)
3919 global $phpbb_root_path, $db, $user, $safe_mode;
3923 if (!$safe_mode && phpbb_is_writable("{$phpbb_root_path}styles/{$path}/template"))
3926 FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
3927 WHERE template_id = $id";
3928 $result = $db->sql_query($sql);
3930 while ($row = $db->sql_fetchrow($result))
3932 if (!($fp = @fopen("{$phpbb_root_path}styles/{$path}/template/" . $row['template_filename'], 'wb')))
3935 $error[] = $user->lang['EDIT_TEMPLATE_STORED_DB'];
3939 fwrite($fp, $row['template_data']);
3942 $db->sql_freeresult($result);
3946 $sql = 'DELETE FROM ' . STYLES_TEMPLATE_DATA_TABLE . "
3947 WHERE template_id = $id";
3948 $db->sql_query($sql);
3955 $sql = 'UPDATE ' . STYLES_TEMPLATE_TABLE . '
3956 SET template_storedb = 0
3957 WHERE template_id = ' . $id;
3958 $db->sql_query($sql);