/** * set the user-defined code files * * @param array $args arguments * key 'page' is the page (i.e. page.rev) or false the global code * key 'head' is the content of the head file * key 'body' is the content of the body file * @return array response * true if successful */ function user_code_set_code($args) { if (!isset($args['page']) || $args['page'] !== false && !page_exists($args['page'])) { return response('Required argument "page" missing or invalid', 400); } if (!isset($args['head'])) { return response('Required argument "head" missing', 400); } if (!isset($args['body'])) { return response('Required argument "body" missing', 400); } if ($args['page'] === false) { drop_cache('page'); foreach (array('head', 'body') as $x) { if (empty($args[$x])) { @unlink(CONTENT_DIR . '/user' . $x); } else { $m = umask(0111); if (!@file_put_contents(CONTENT_DIR . '/user' . $x, $args[$x])) { umask($m); return response('Error saving user ' . $x, 500); } else { umask($m); } } } return response(true); } else { drop_cache('page', $args['page']); load_modules('glue'); foreach (array('head', 'body') as $x) { if (empty($args[$x])) { delete_object(array('name' => $args['page'] . '.user' . $x)); } else { update_object(array('name' => $args['page'] . '.user' . $x, 'type' => 'user' . $x, 'module' => 'user_code', 'content' => $args[$x])); } } return response(true); } }
/** * set the user-defined css file * * @param array $args arguments * key 'page' is the page (i.e. page.rev) or false the global css * key 'css' is the content of the css file * @return array response * true if successful */ function user_css_set_css($args) { if (!isset($args['page']) || $args['page'] !== false && !page_exists($args['page'])) { return response('Required argument "page" missing or invalid', 400); } if (!isset($args['css'])) { return response('Required argument "css" missing', 400); } if ($args['page'] === false) { // TODO (later): drop_cache() if (empty($args['css'])) { // empty stylesheet @unlink(CONTENT_DIR . '/usercss'); return response(true); } else { $m = umask(0111); if (!@file_put_contents(CONTENT_DIR . '/usercss', $args['css'])) { umask($m); return response('Error saving stylesheet', 500); } else { umask($m); return response(true); } } } else { drop_cache('page', $args['page']); load_modules('glue'); if (empty($args['css'])) { delete_object(array('name' => $args['page'] . '.usercss')); return response(true); } else { return update_object(array('name' => $args['page'] . '.usercss', 'type' => 'usercss', 'module' => 'user_css', 'content' => $args['css'])); } } }
/** * save an object to the content directory * * use update_object() whenever possible as we want to preserve any object * metadata that is stored in as attributes. * @param array $args arguments * key 'name' is the object name (i.e. page.rev.obj) * key 'content' is the object's content * all other key/value pairs are treated as attributes * @return array response */ function save_object($args) { if (empty($args['name'])) { return response('Required argument "name" missing or empty', 400); } // open file for writing $m = umask(0111); if (($f = @fopen(CONTENT_DIR . '/' . str_replace('.', '/', $args['name']), 'wb')) === false) { umask($m); return response('Error opening ' . quot($args['name']) . ' for writing', 500); } umask($m); // save attributes foreach ($args as $key => $val) { if ($key == 'name' || $key == 'content') { continue; } // check for delimiter character in key if (strpos($key, ':') !== false) { log_msg('warn', 'save_object: skipping attribute ' . quot($key) . ' in object ' . quot($args['name'])); continue; } // filter newlines from value $val = str_replace("\r\n", '', $val); $val = str_replace("\n", '', $val); $val = str_replace("\r", '', $val); fwrite($f, $key . ':' . $val . "\n"); } // save content if (isset($args['content'])) { fwrite($f, "\n"); fwrite($f, $args['content']); } fclose($f); // drop all pages from cache (since the object could be symlinked) drop_cache('page'); return response(true); }