Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/srv/http/onarcade242/admin/templates.php
<?php
/**
* onArcade 2.4.2
* Copyright © 2006-2015 Hans Mäesalu & Eveterm OÜ, All Rights Reserved
**
* ONARCADE IS NOT FREE SOFTWARE!
* http://www.onarcade.com
**
* Template editor pages
**/
require ('./global.php');
// submenu content
$txt['menu'] = array(
array('?a=', $lang['templates']),
);
$template->load_js_file('admin_templates');
switch ($_GET['a']) {
case 'template':
$txt['template'] = $_GET['t'];
if (!file_exists('./templates/'. $_GET['t'] .'/')) {
$template->no_page();
}
$template->header .= '<link rel="stylesheet" type="text/css" href="'. $settings['siteurl'] .'/jscripts/codemirror/codemirror.css" />
<link rel="stylesheet" type="text/css" href="'. $settings['siteurl'] .'/jscripts/codemirror/default.css" />';
$template->load_js_file('codemirror/codemirror');
$template->load_js_file('codemirror/xml');
$template->load_js_file('codemirror/javascript');
$template->load_js_file('codemirror/css');
$template->load_js_file('codemirror/htmlmixed');
// template files
$txt['files'] = array();
if ($template_dir = opendir('./templates/'. $txt['template'] .'/')) {
while ($template_files = readdir($template_dir)) {
$file_type = strtolower(substr($template_files, -3)); // get file type
if ($file_type == 'php' && strpos($template_files, '.template') === false || $file_type == 'css' || $file_type == '.js') {
if ($file_type == '.js') {
$file_type = 'js';
}
$txt['files'][] = array ($template_files, '<img src="images/'. $file_type .'.png" alt="" />', $template_files);
}
}
closedir($template_dir);
}
$txt['files'] = json_encode($txt['files']);
$template->add_js_lang(array('name' => $lang['name']
, 'sure_delete' => $lang['sure_delete']));
$template->set_title($lang['templates']);
$template->full('template');
break;
case 'template_values':
$file_name = './templates/'. $_GET['template'] .'/'. $_GET['file'];
$file_type = strtolower(substr($_GET['file'], -3));
if ($file_type == '.js') {
$file_type = 'js';
}
if (!file_exists($file_name) || !($contents = @file_get_contents($file_name))) {
$template->json_error($lang['no_page_found']);
}
// array returned to user
$return = array('error' => false,
'type' => $file_type,
'code' => ''
);
// process PHP template file
if ($file_type == "php") {
if (!isset($_GET['function'])) {
// get all templates list
$return['functions'] = array();
preg_match_all("#{template\\s([^}]*)}.+?{/template}#s", $contents, $matches);
foreach ($matches[1] AS $match) {
$return['functions'][] = $match;
}
unset($matches);
if (!empty($return['functions'])) {
$function = trim($return['functions'][0]);
}
} else {
$function = trim($_GET['function']);
}
if (preg_match("#{template\\s+". str_replace(array('(', ')', '$'), array('\\(', '\\)', '\\$'), $function) ."\\s*}\n?(.+?)\n?{/template}#s", $contents, $match) > 0) {
$return['code'] = $match[1];
} else {
$template->json_error('Template not found.');
}
} else {
$return['code'] = $contents;
}
$template->json($return);
break;
case 'delete_function':
$function = trim($_POST['function']);
$file_name = './templates/'. $_POST['template'] .'/'. $_POST['file'];
$file_type = strtolower(substr($_POST['file'], -3));
if (empty($function) || $file_type != 'php' || !file_exists($file_name)) {
$template->json_error($lang['no_page_found']);
}
if (!is_writable($file_name)) {
$template->json_error(str_replace('{$x}', $file_name, $lang['file_not_writable']));
}
// open, remove and save
if (!$contents = file_get_contents($file_name)) {
$template->json_error('Could not read template file.');
}
$contents = preg_replace("#{template\\s+". str_replace(array('(', ')', '$'), array('\\(', '\\)', '\\$'), $function) ."\\s*}.+?{/template}\\s*#s", '', $contents);
file_put_contents($file_name, $contents);
$template->json(array('error' => false,
'message' => $lang['deleted']));
break;
case 'submit_template':
$template_name = $_POST['template'];
$file = $_POST['file'];
$file_name = './templates/'. $template_name .'/'. $file;
$file_type = strtolower(substr($_POST['file'], -3));
$new_contents = get_without_slashes('code');
if (empty($template_name) || empty($file) || !file_exists($file_name)) {
$template->json_error($lang['no_page_found']);
}
if (!is_writable($file_name)) {
$template->json_error(str_replace('{$x}', $file_name, $lang['file_not_writable']));
}
if (!$contents = file_get_contents($file_name)) {
$template->json_error('Could not read template file.');
}
// backup
if (is_writable('./templates/'. $template_name .'/')) {
file_put_contents('./templates/'. $template_name .'/~'. $file, $contents);
}
if ($file_type == 'php') {
$function = trim($_POST['function']);
if (empty($function)) {
$template->json_error($lang['no_page_found']);
}
$new_contents = "{template ". $function ."}\n". $new_contents ."\n{/template}";
// check if function already exists and add new contents
if (preg_match("#{template\\s+". str_replace(array('(', ')', '$'), array('\\(', '\\)', '\\$'), $function) ."\\s*}.+?{/template}#s", $contents) > 0) {
$contents = preg_replace("#{template\\s+". str_replace(array('(', ')', '$'), array('\\(', '\\)', '\\$'), $function) ."\\s*}.+?{/template}#s", $new_contents, $contents);
} else {
$contents .= "\n". $new_contents;
}
file_put_contents($file_name, $contents); // save
} else {
file_put_contents($file_name, $new_contents);
}
$template->json(array('error' => false,
'message' => $lang['updated']));
break;
default:
if (isset($_GET['ajax'])) {
switch ($_GET['ajax']) {
case 'set_default':
if (isset($_POST['template']) && file_exists('./templates/'. $_POST['template'] .'/')) {
$settings['template'] = $_POST['template'];
if (update_setting()) {
$template->json(array('error' => false, 'message' => $lang['settings_updated']));
}
}
$template->json_error($lang['couldnt_update']);
break;
}
}
// get templates
$txt['templates'] = array();
if ($templates_dir = opendir('./templates/')) {
while ($template_dir = readdir($templates_dir)) {
if ($template_dir != '.' && $template_dir != '..' && is_dir('./templates/'. $template_dir)) {
$set_default_link = stripos($template_dir, 'mobile') === false ? '<p class="set_default"><a href="">'. $lang['set_default'] .'</a></p>' : '';
$txt['templates'][] = array ($template_dir, '<img src="'. $settings['siteurl'] .'/templates/'. $template_dir .'/preview.jpg" alt="" />'
, '<p class="bold">'. ucwords(str_replace('_', ' ', $template_dir) .'</p><p><a href="templates.php?a=template&amp;t='. $template_dir .'">'. $lang['edit'] .'</a></p>' . $set_default_link));
}
}
closedir($templates_dir);
}
$txt['templates'] = json_encode($txt['templates']);
$template->set_title($lang['templates']);
$template->full('templates');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.