00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 @require_once('config.inc.php');
00014 require_once('common.inc.php');
00015 require_once('controller.inc.php');
00016 require_once('html.inc.php');
00017 require_once('modules.inc.php');
00018 require_once('util.inc.php');
00019
00020
00021
00022
00023
00024
00025 function controller_user_code_stylesheet($args)
00026 {
00027 if ($args[0][1] == 'code') {
00028
00029 $page = $args[0][0];
00030 page_canonical($page);
00031 if (!page_exists($page)) {
00032 hotglue_error(404);
00033 }
00034 } else {
00035
00036 $page = false;
00037 }
00038
00039 default_html(true);
00040 html_add_js_var('$.glue.page', $page);
00041 html_add_css(base_url().'modules/user_code/user_code.css');
00042 if (USE_MIN_FILES) {
00043 html_add_js(base_url().'modules/user_code/user_code.min.js');
00044 } else {
00045 html_add_js(base_url().'modules/user_code/user_code.js');
00046 }
00047 $bdy = &body();
00048
00049 $code = array('head'=>'','body'=>'');
00050 elem_attr($bdy, 'id', 'user_code');
00051 if ($page === false) {
00052 body_append('<h1>Global code</h1>'.nl());
00053
00054 foreach ($code as $x => $v) {
00055 $code[$x] = @file_get_contents(CONTENT_DIR.'/user'.$x);
00056 if ($code[$x] === false) {
00057 $code[$x] = '';
00058 }
00059 }
00060 } else {
00061 body_append('<h1>"'.htmlspecialchars(substr($page, 0, strpos($page, '.')), ENT_NOQUOTES, 'UTF-8').'" page code</h1>'.nl());
00062 load_modules('glue');
00063 foreach ($code as $x => $v) {
00064 $obj = load_object(array('name'=>$page.'.user'.$x));
00065 if ($obj['#error']) {
00066 $code[$x] = '';
00067 } else {
00068 $code[$x] = $obj['#data']['content'];
00069 }
00070 }
00071 }
00072 foreach ($code as $k => $v) {
00073
00074 $v = htmlspecialchars($v, ENT_NOQUOTES, 'UTF-8');
00075
00076
00077 $v = str_replace("\r\n", ' ', $v);
00078 $v = str_replace("\n", ' ', $v);
00079
00080 $v = str_replace("\t", '	', $v);
00081 $code[$k] = $v;
00082 }
00083 body_append('<div id=\'text\'>add your custom code to <head> and <body> sections of this '.($page ? 'page.' : 'site.').nl());
00084 body_append('<br>'.nl());
00085 body_append('be cautious - errors in the code below may render the whole '.($page ? 'page' : 'site').' unusable.</div>'.nl());
00086 body_append('<br>'.nl());
00087 body_append('<div id=\'fake_tags\'><head></div>'.nl());
00088 body_append('<textarea id="user_head_text" placeholder="enter code here">'.$code['head'].'</textarea>'.nl());
00089 body_append('<br>'.nl());
00090 body_append('<div id=\'fake_tags\'></head><br>'.nl());
00091 body_append('<body></div>'.nl());
00092 body_append('<textarea id="user_body_text" placeholder="enter code here">'.$code['body'].'</textarea>'.nl());
00093 body_append('<div id=\'fake_tags\'></body></div><br>'.nl());
00094 body_append('<input id="user_code_save" type="button" value="save">'.nl());
00095 echo html_finalize();
00096 }
00097
00098 register_controller('code', '', 'controller_user_code_stylesheet', array('auth'=>true));
00099 register_controller('*', 'code', 'controller_user_code_stylesheet', array('auth'=>true));
00100
00101
00102 function user_code_render_object($args)
00103 {
00104 $obj = $args['obj'];
00105 if (!isset($obj['type']) || !($obj['type'] == 'userhead' or $obj['type'] == 'userbody')) {
00106 return false;
00107 }
00108 if (!empty($obj['content'])) {
00109 if ($obj['type'] == 'userhead') {
00110 html_add_head_inline($obj['content'], 5);
00111 } else {
00112 html_add_body_inline($obj['content'], 5);
00113 }
00114 }
00115 return '';
00116 }
00117
00118
00119 function user_code_render_page_early($args)
00120 {
00121
00122 foreach (array('head','body') as $x) {
00123 if (@is_file(CONTENT_DIR.'/user'.$x)) {
00124 $func = 'html_add_'.$x.'_inline';
00125 $func(@file_get_contents(CONTENT_DIR.'/user'.$x), 5);
00126 }
00127 }
00128 if ($args['edit']) {
00129 if (USE_MIN_FILES) {
00130 html_add_js(base_url().'modules/user_code/user_code-edit.min.js');
00131 } else {
00132 html_add_js(base_url().'modules/user_code/user_code-edit.js');
00133 }
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 function user_code_set_code($args)
00149 {
00150 if (!isset($args['page']) || ($args['page'] !== false && !page_exists($args['page']))) {
00151 return response('Required argument "page" missing or invalid', 400);
00152 }
00153 if (!isset($args['head'])) {
00154 return response('Required argument "head" missing', 400);
00155 }
00156 if (!isset($args['body'])) {
00157 return response('Required argument "body" missing', 400);
00158 }
00159
00160 if ($args['page'] === false) {
00161 drop_cache('page');
00162 foreach (array('head','body') as $x) {
00163 if (empty($args[$x])) {
00164 @unlink(CONTENT_DIR.'/user'.$x);
00165 } else {
00166 $m = umask(0111);
00167 if (!@file_put_contents(CONTENT_DIR.'/user'.$x, $args[$x])) {
00168 umask($m);
00169 return response('Error saving user '.$x, 500);
00170 } else {
00171 umask($m);
00172 }
00173 }
00174 }
00175 return response(true);
00176 } else {
00177 drop_cache('page', $args['page']);
00178 load_modules('glue');
00179 foreach (array('head','body') as $x) {
00180 if (empty($args[$x])) {
00181 delete_object(array('name'=>$args['page'].'.user'.$x));
00182
00183 } else {
00184 update_object(array('name'=>$args['page'].'.user'.$x, 'type'=>'user'.$x, 'module'=>'user_code', 'content'=>$args[$x]));
00185 }
00186 }
00187 return response(true);
00188 }
00189 }
00190
00191 register_service('user_code.set_code', 'user_code_set_code', array('auth'=>true));