PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/3.1/modules/themeroller/controllers/admin_themeroller.php

http://github.com/gallery/gallery3-contrib
PHP | 193 lines | 151 code | 20 blank | 22 comment | 16 complexity | f7f5d64021843f0c1a3b56d60b0ced1d MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php defined("SYSPATH") or die("No direct script access.");/**
  2. * Gallery - a web based photo album viewer and editor
  3. * Copyright (C) 2000-2011 Bharat Mediratta
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. class Admin_Themeroller_Controller extends Admin_Controller {
  20. public function form_upload() {
  21. $v = new View("admin_themeroller_upload.html");
  22. list ($v->form, $v->errors) = $this->_get_upload_form();
  23. $v->is_writable = is_writable(THEMEPATH);
  24. $v->action = "admin/themeroller/form_create";
  25. $submit_class = "ui-state-default ui-corner-all submit g-left";
  26. if ($v->not_writable = !is_writable(THEMEPATH)) {
  27. $submit_class .= " ui-state-disabled";
  28. }
  29. $v->submit_class = $submit_class;
  30. $v->script_data = array(
  31. "g3sid" => Session::instance()->id(),
  32. "user_agent" => Input::instance()->server("HTTP_USER_AGENT"),
  33. "csrf" => access::csrf_token());
  34. json::reply(array("html" => (string) $v));
  35. }
  36. public function form_create() {
  37. $theme_name = Session::instance()->get_once("theme_name");
  38. json::reply(array("html" => (string) $this->_get_theme_form($theme_name)));
  39. }
  40. public function upload() {
  41. access::verify_csrf();
  42. $validation = new Validation(array_merge($_POST, $_FILES));
  43. $validation->add_rules("zip_file", "upload::valid", "upload::required", "upload::type[zip]");
  44. $validation->add_rules("is_admin", "chars[0,1]");
  45. $validation->add_callbacks("zip_file", array($this, "_unload_zip"));
  46. if ($validation->validate()) {
  47. $session = Session::instance();
  48. $themeroller_name = $session->get("themeroller_name");
  49. $is_admin = $validation["is_admin"];
  50. $counter = 0;
  51. $theme_name_generated = $theme_name = ($is_admin ? "admin_" : "") . $themeroller_name;
  52. while (file_exists(THEMEPATH . "$theme_name_generated/theme.info")) {
  53. $counter++;
  54. $theme_name_generated = "{$theme_name}_{$counter}";
  55. }
  56. $theme_name = strtolower(strtr($theme_name_generated, " ", "_"));
  57. $session->set("theme_name", $theme_name);
  58. $session->set("themeroller_is_admin", $is_admin);
  59. print "FILEID: {$validation["zip_file"]["tmp_name"]}";
  60. } else {
  61. header("HTTP/1.1 400 Bad Request");
  62. print "ERROR: " . t("Invalid zip archive");
  63. }
  64. }
  65. public function create() {
  66. access::verify_csrf();
  67. $form = $this->_get_theme_form();
  68. if ($form->validate()) {
  69. $session = Session::instance();
  70. $extract_path = $session->get_once("theme_extract_path");
  71. $v = new View("admin_themeroller_progress.html");
  72. $task_def = Task_Definition::factory()
  73. ->callback("themeroller_task::create_theme")
  74. ->description(t("Generate theme from a themeroller archive"))
  75. ->name(t("Generate theme"));
  76. $v->task = task::create($task_def,
  77. array("path" => $extract_path,
  78. "user_name" => SafeString::purify(identity::active_user()->name),
  79. "original_name" => SafeString::purify($form->theme->original->value),
  80. "theme_name" => SafeString::purify($form->theme->theme_name->value),
  81. "display_name" => SafeString::purify($form->theme->display_name->value),
  82. "description" => SafeString::purify($form->theme->description->value),
  83. "author_url" => SafeString::purify($form->theme->author_url->value),
  84. "info_url" => SafeString::purify($form->theme->info_url->value),
  85. "discuss_url" => SafeString::purify($form->theme->discuss_url->value),
  86. "is_admin" => $session->get("themeroller_is_admin")));
  87. json::reply(array("html" => (string) $v));
  88. } else {
  89. json::reply(array("result" => "error", "html" => (string) $form));
  90. }
  91. }
  92. /**
  93. * Run the task of creating the theme
  94. */
  95. static function run($task_id) {
  96. access::verify_csrf();
  97. $task = ORM::factory("task", $task_id);
  98. if (!$task->loaded() || $task->owner_id != identity::active_user()->id) {
  99. access::forbidden();
  100. }
  101. $task = task::run($task_id);
  102. // Prevent the JavaScript code from breaking by forcing a period as
  103. // decimal separator for all locales with sprintf("%F", $value).
  104. json::reply(array("done" => (bool)$task->done,
  105. "status" => (string)$task->status,
  106. "percent_complete" => sprintf("%F", $task->percent_complete)));
  107. }
  108. static function _is_theme_defined($name) {
  109. $theme_name = strtolower(strtr($name->value, " ", "_"));
  110. if (file_exists(THEMEPATH . "$theme_name/theme.info")) {
  111. $name->add_error("conflict", 1);
  112. }
  113. }
  114. public function _unload_zip(Validation $post, $field) {
  115. $zipfile = $post["zip_file"]["tmp_name"];
  116. if (false !== ($extract_path = themeroller::extract_zip_file($zipfile))) {
  117. $theme_name = themeroller::get_theme_name($extract_path);
  118. if (!empty($theme_name)) {
  119. Session::instance()->set("themeroller_name", $theme_name);
  120. } else {
  121. Kohana_Log::add("error", "zip file: css directory not found");
  122. $post->add_error($field, "invalid zipfile");
  123. }
  124. } else {
  125. Kohana_Log::add("error", "zip file: open failed");
  126. $post->add_error($field, "invalid zipfile");
  127. }
  128. if (file_exists($zipfile)) {
  129. unlink($zipfile);
  130. }
  131. }
  132. private function _get_theme_form($theme_name=null) {
  133. $session = Session::instance();
  134. $form = new Forge("admin/themeroller/create", "", "post", array("id" => "g-themeroller-create-form"));
  135. $form_group = $form->group("theme")->label(t("Create theme"));
  136. $original_name = $form_group->hidden("original");
  137. $name_field = $form_group->input("theme_name")->label(t("Theme Name"))->id("g-name")
  138. ->rules("required")
  139. ->callback("Admin_Themeroller_Controller::_is_theme_defined")
  140. ->error_messages("conflict", t("There is already a theme with that name"))
  141. ->error_messages("required", t("You must enter a theme name"));
  142. $display_name = $form_group->input("display_name")->label(t("Display Name"))
  143. ->id("g-display-name")
  144. ->rules("required")
  145. ->error_messages("required", t("You must enter a theme display name"));
  146. if (!empty($theme_name)) {
  147. $name_field->value($theme_name);
  148. $is_admin = $session->get("themeroller_is_admin");
  149. $themeroller_name = $session->get("themeroller_name");
  150. $display_name->value(ucwords($is_admin ? t("%name administration theme",
  151. array("name" => str_replace("-", " ", $themeroller_name))) :
  152. t("%name theme",
  153. array("name" => str_replace("-", " ", $themeroller_name)))));
  154. $original_name->hidden("original")->value(Session::instance()->get("themeroller_name"));
  155. }
  156. $form_group->textarea("description")->label(t("Description"))
  157. ->id("g-description")
  158. ->value(t("A generated theme based on the ui themeroller '%name' styling",
  159. array("name" => str_replace("admin_", "", $theme_name))))
  160. ->rules("required")
  161. ->error_messages("required", t("You must enter a theme description name"));
  162. $form_group->input("author_url")->label(t("Author url"))->id("g-author-url");
  163. $form_group->input("info_url")->label(t("Info url"))->id("g-info-url");
  164. $form_group->input("discuss_url")->label(t("Theme Name"))->id("g-discuss-url");
  165. $form_group->submit("")->value(t("Create"));
  166. return $form;
  167. }
  168. private function _get_upload_form() {
  169. $form = array("zip_file" => "", "is_admin" => "");
  170. $errors = array_fill_keys(array_keys($form), "");
  171. return array($form, $errors);
  172. }
  173. }