diff options
Diffstat (limited to 'ratatoeskr/backend.php')
-rw-r--r-- | ratatoeskr/backend.php | 319 |
1 files changed, 167 insertions, 152 deletions
diff --git a/ratatoeskr/backend.php b/ratatoeskr/backend.php index ae72195..2db82d7 100644 --- a/ratatoeskr/backend.php +++ b/ratatoeskr/backend.php @@ -12,6 +12,7 @@ require_once(dirname(__FILE__) . "/sys/models.php"); require_once(dirname(__FILE__) . "/sys/pwhash.php"); require_once(dirname(__FILE__) . "/sys/textprocessors.php"); +require_once(dirname(__FILE__) . "/sys/plugin_api.php"); require_once(dirname(__FILE__) . "/languages.php"); $admin_grp = NULL; @@ -38,25 +39,6 @@ function maketags($tagnames, $lang) return $rv; } -/* Generates Yes/No form / checks it. */ -function askyesno($ste, $callback, $question, $yes=NULL, $no=NULL, $moredetails="") -{ - if(isset($_POST["yes"])) - return True; - if(isset($_POST["no"])) - return False; - - $ste->vars["callback"] = $callback; - $ste->vars["question"] = $question; - if($yes !== NULL) - $ste->vars["yestext"] = $yes; - if($no !== NULL) - $ste->vars["notext"] = $no; - if($moredetails !== NULL) - $ste->vars["moredetails"] = $moredetails; - return $ste->exectemplate("/systemtemplates/areyousure.html"); -} - $backend_subactions = NULL; function build_backend_subactions() @@ -116,6 +98,7 @@ $backend_subactions = url_action_subactions(array( } } load_language(); + /* If we are here, user is not logged in... */ $url_next = array("login"); }, @@ -160,7 +143,7 @@ $backend_subactions = url_action_subactions(array( "content" => url_action_subactions(array( "write" => function(&$data, $url_now, &$url_next) { - global $ste, $translation, $textprocessors, $ratatoeskr_settings, $languages; + global $ste, $translation, $textprocessors, $ratatoeskr_settings, $languages, $articleeditor_plugins; list($article, $editlang) = array_slice($url_next, 0); if(!isset($editlang)) @@ -192,11 +175,11 @@ $backend_subactions = url_action_subactions(array( if(isset($_POST["save_article"])) { - if(!preg_match('/^[a-zA-Z0-9-_]+$/', @$_POST["urlname"])) + if(!Article::test_urlname($_POST["urlname"])) $fail_reasons[] = $translation["invalid_urlname"]; else $inputs["urlname"] = $_POST["urlname"]; - if((@$_POST["article_status"] < 0) or (@$_POST["article_status"] > 3)) + if(!Article::test_status(@$_POST["article_status"])) $fail_reasons[] = $translation["invalid_article_status"]; else $inputs["article_status"] = (int) $_POST["article_status"]; @@ -235,14 +218,21 @@ $backend_subactions = url_action_subactions(array( if(isset($_POST["saveaslang"])) $editlang = $_POST["saveaslang"]; } + else + { + /* Call articleeditor plugins */ + $article = empty($article) ? NULL : Article::by_urlname($article); + foreach($articleeditor_plugins as $plugin) + call_user_func($plugin["fx"], $article, False); + } function fill_article(&$article, $inputs, $editlang) { $article->urlname = $inputs["urlname"]; $article->status = $inputs["article_status"]; $article->timestamp = $inputs["date"]; - $article->title [$editlang] = new Translation($inputs["title"], "" ); - $article->text [$editlang] = new Translation($inputs["content"], $inputs["content_txtproc"]); + $article->title[$editlang] = new Translation($inputs["title"], ""); + $article->text[$editlang] = new Translation($inputs["content"], $inputs["content_txtproc"]); $article->excerpt[$editlang] = new Translation($inputs["excerpt"], $inputs["excerpt_txtproc"]); $article->set_tags(maketags($inputs["tags"], $editlang)); $article->set_section($inputs["article_section"]); @@ -257,15 +247,32 @@ $backend_subactions = url_action_subactions(array( { $article = Article::create($inputs["urlname"]); fill_article($article, $inputs, $editlang); - try + + /* Calling articleeditor plugins */ + $call_after_save = array(); + foreach($articleeditor_plugins as $plugin) { - $article->save(); - $ste->vars["article_editurl"] = urlencode($article->urlname) . "/" . urlencode($editlang); - $ste->vars["success"] = htmlesc($translation["article_save_success"]); + $result = call_user_func($plugin["fx"], $article, True); + if(is_string($result)) + $fail_reasons[] = $result; + elseif($result !== NULL) + $call_after_save[] = $result; } - catch(AlreadyExistsError $e) + + if(empty($fail_reasons)) { - $fail_reasons[] = $translation["article_name_already_in_use"]; + try + { + $article->save(); + foreach($call_after_save as $cb) + call_user_func($cb, $article); + $ste->vars["article_editurl"] = urlencode($article->urlname) . "/" . urlencode($editlang); + $ste->vars["success"] = htmlesc($translation["article_save_success"]); + } + catch(AlreadyExistsError $e) + { + $fail_reasons[] = $translation["article_name_already_in_use"]; + } } } } @@ -273,7 +280,8 @@ $backend_subactions = url_action_subactions(array( { try { - $article = Article::by_urlname($article); + if(!($article instanceof Article)) + $article = Article::by_urlname($article); } catch(DoesNotExistError $e) { @@ -283,15 +291,32 @@ $backend_subactions = url_action_subactions(array( if(empty($fail_reasons) and isset($_POST["save_article"])) { fill_article($article, $inputs, $editlang); - try + + /* Calling articleeditor plugins */ + $call_after_save = array(); + foreach($articleeditor_plugins as $plugin) { - $article->save(); - $ste->vars["article_editurl"] = urlencode($article->urlname) . "/" . urlencode($editlang); - $ste->vars["success"] = htmlesc($translation["article_save_success"]); + $result = call_user_func($plugin["fx"], $article, True); + if(is_string($result)) + $fail_reasons[] = $result; + elseif($result !== NULL) + $call_after_save[] = $result; } - catch(AlreadyExistsError $e) + + if(empty($fail_reasons)) { - $fail_reasons[] = $translation["article_name_already_in_use"]; + try + { + $article->save(); + foreach($call_after_save as $cb) + call_user_func($cb, $article); + $ste->vars["article_editurl"] = urlencode($article->urlname) . "/" . urlencode($editlang); + $ste->vars["success"] = htmlesc($translation["article_save_success"]); + } + catch(AlreadyExistsError $e) + { + $fail_reasons[] = $translation["article_name_already_in_use"]; + } } } @@ -367,6 +392,9 @@ $backend_subactions = url_action_subactions(array( $ste->vars[$k_out] = $inputs[$k_in]; } + /* Displaying article editor plugins */ + $ste->vars["displayed_plugins"] = array_map(function($x) { return array("label" => $x["label"], "template" => $x["template"]); }, array_filter($articleeditor_plugins, function($x) { return $x["display"]; })); + if(!empty($fail_reasons)) $ste->vars["failed"] = $fail_reasons; @@ -375,149 +403,136 @@ $backend_subactions = url_action_subactions(array( "tags" => function(&$data, $url_now, &$url_next) { global $translation, $languages, $ste, $rel_path_to_root; - $ste->vars["section"] = "content"; - $ste->vars["submenu"] = "tags"; - list($tagname, $tagaction) = $url_next; $url_next = array(); - if(isset($tagname)) + $ste->vars["section"] = "content"; + $ste->vars["submenu"] = "tags"; + $ste->vars["pagetitle"] = $translation["tags_overview"]; + + if(isset($_POST["delete"]) and ($_POST["really_delete"] == "yes")) { - try + foreach($_POST["tag_multiselect"] as $tagid) { - $tag = Tag::by_name($tagname); + try + { + $tag = Tag::by_id($tagid); + $tag->delete(); + } + catch(DoesNotExistError $e) + { + continue; + } } - catch(DoesNotExistError $e) + + $ste->vars["success"] = $translation["tags_successfully_deleted"]; + } + + if(isset($_POST["save_changes"])) + { + $newlang = (!empty($_POST["new_language"])) ? $_POST["new_language"] : NULL; + $newtag = NULL; + + if(!empty($_POST["newtagname"])) { - throw new NotFoundError(); + if(!Tag::test_name(@$_POST["newtagname"])) + $ste->vars["error"] = $translation["invalid_tag_name"]; + else + $newtag = $_POST["newtagname"]; } - if(isset($tagaction)) + if(($newlang !== NULL) and (!isset($languages[$newlang]))) + $newlang = NULL; + if($newtag !== NULL) { - switch($tagaction) + try { - case "delete": - $ste->vars["pagetitle"] = str_replace("[[TAGNAME]]", $tag->name, $translation["delete_tag_pagetitle"]); - $yesnoresp = askyesno($ste, "$rel_path_to_root/backend/content/tags/{$tag->name}/delete", $translation["delete_comment_question"]); - if(is_string($yesnoresp)) - { - echo $yesnoresp; - return; - } - - if($yesnoresp) - { - $tag->delete(); - echo $ste->exectemplate("/systemtemplates/tag_deleted.html"); - } - else - goto backend_content_tags_overview; /* Hopefully no dinosaur will attack me: http://xkcd.com/292/ :-) */ - break; - case "addtranslation": - $ste->vars["pagetitle"] = $translation["tag_add_lang"]; - $ste->vars["tagname"] = $tag->name; - if(isset($_POST["addtranslation"])) - { - $errors = array(); - if(!isset($languages[@$_POST["language"]])) - $errors[] = $translation["language_unknown"]; - if(empty($_POST["translation"])) - $errors[] = $translation["no_translation_text_given"]; - if(empty($errors)) - { - $tag->title[$_POST["language"]] = new Translation($_POST["translation"], ""); - $tag->save(); - $ste->vars["success"] = $translation["tag_translation_added"]; - goto backend_content_tags_overview; - } - else - $ste->vars["errors"] = $errors; - } - echo $ste->exectemplate("/systemtemplates/tag_addtranslation.html"); - break; + $newtag = Tag::create($newtag); + } + catch(AlreadyExistsError $e) + { + $newtag = NULL; } } - } - else - { - backend_content_tags_overview: - if(isset($_POST["create_new_tag"])) + $translations = array(); + foreach($_POST as $k => $v) { - if((strpos(@$_POST["new_tag_name"], ",") !== False) or (strpos(@$_POST["new_tag_name"], " ") !== False) or (strlen(@$_POST["new_tag_name"]) == 0)) - $ste->vars["error"] = $translation["invalid_tag_name"]; - else + if(preg_match('/tagtrans_(NEW|[a-z]{2})_(.*)/', $k, $matches) == 1) { - try - { - $tag = Tag::create($_POST["new_tag_name"]); - $tag->title[$data["user"]->language] = new Translation($_POST["new_tag_name"], ""); - $tag->save(); - $ste->vars["success"] = $translation["tag_created_successfully"]; - } - catch(AlreadyExistsError $e) - { - $ste->vars["error"] = $translation["tag_name_already_in_use"]; - } + $lang = ($matches[1] == "NEW") ? $newlang : $matches[1]; + $tag = $matches[2]; + if($lang === NULL) + continue; + $translations[$tag][$lang] = $v; } } - if(isset($_POST["edit_translations"])) + foreach($translations as $tag => $langmap) { - $tagbuffer = array(); - foreach($_POST as $k => $v) + if($tag == "NEW") + { + if($newtag === NULL) + continue; + $tag = $newtag; + } + else { - if(preg_match("/^tagtrans_(.*?)_(.*)$/", $k, $matches)) + try { - if(!isset($languages[$matches[1]])) - continue; - - if(!isset($tagbuffer[$matches[2]])) - { - try - { - $tagbuffer[$matches[2]] = Tag::by_name($matches[2]); - } - catch(DoesNotExistError $e) - { - continue; - } - } - - if(empty($v) and isset($tagbuffer[$matches[2]]->title[$matches[1]])) - unset($tagbuffer[$matches[2]]->title[$matches[1]]); - elseif(empty($v)) - continue; - else - $tagbuffer[$matches[2]]->title[$matches[1]] = new Translation($v, ""); + $tag = Tag::by_id($tag); + } + catch(DoesNotExistError $e) + { + continue; } } - foreach($tagbuffer as $tag) - $tag->save(); + foreach($langmap as $l => $text) + { + if(empty($text)) + unset($tag->title[$l]); + else + $tag->title[$l] = new Translation($text, ""); + } - $ste->vars["success"] = $translation["tag_titles_edited_successfully"]; + $tag->save(); } - $ste->vars["pagetitle"] = $translation["tags_overview"]; - - $alltags = Tag::all(); - usort($alltags, function($a, $b) { return strcmp($a->name, $b->name); }); - $ste->vars["all_tag_langs"] = array(); - $ste->vars["alltags"] = array(); - foreach($alltags as $tag) - { - $tag_pre = array("name" => $tag->name, "translations" => array()); - foreach($tag->title as $langcode => $translation_obj) - { - $tag_pre["translations"][$langcode] = $translation_obj->text; - if(!isset($ste->vars["all_tag_langs"][$langcode])) - $ste->vars["all_tag_langs"][$langcode] = $languages[$langcode]["language"]; - } - $ste->vars["alltags"][] = $tag_pre; + $ste->vars["success"] = $translation["tags_successfully_edited"]; + } + + $ste->vars["alltags"] = array(); + $taglangs = array(); + + $alltags = Tag::all(); + foreach($alltags as $tag) + { + $transls = array(); + foreach($tag->title as $l => $t) + { + if(!in_array($l, $taglangs)) + $taglangs[] = $l; + $transls[$l] = $t->text; } - echo $ste->exectemplate("/systemtemplates/tags_overview.html"); + + $ste->vars["alltags"][] = array( + "id" => $tag->get_id(), + "name" => $tag->name, + "translations" => $transls + ); } + + $unused_langs = array_diff(array_keys($languages), $taglangs); + + $ste->vars["all_tag_langs"] = array(); + foreach($taglangs as $l) + $ste->vars["all_tag_langs"][$l] = $languages[$l]["language"]; + $ste->vars["unused_languages"] = array(); + foreach($unused_langs as $l) + $ste->vars["unused_languages"][$l] = $languages[$l]["language"]; + + echo $ste->exectemplate("/systemtemplates/tags_overview.html"); }, "articles" => function(&$data, $url_now, &$url_next) { @@ -1045,7 +1060,7 @@ $backend_subactions = url_action_subactions(array( /* A write request? */ if(isset($_POST["save_style"])) { - if(preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $_POST["style_name"]) == 1) + if(Style::test_name($_POST["style_name"])) { $ste->vars["style_name"] = $_POST["style_name"]; $ste->vars["style_code"] = $_POST["style_code"]; @@ -1098,7 +1113,7 @@ $backend_subactions = url_action_subactions(array( { if((preg_match("/^[a-zA-Z0-9\\-_\\.]+$/", $_POST["template"]) == 0) or (!is_file(SITE_BASE_PATH . "/ratatoeskr/templates/src/usertemplates/{$_POST['template']}"))) $ste->vars["error"] = $translation["unknown_template"]; - else if(preg_match("/^[a-zA-Z0-9\\-_]+$/", $_POST["section_name"]) == 0) + else if(!Section::test_name($_POST["section_name"])) $ste->vars["error"] = $translation["invalid_section_name"]; else { |