diff --git a/web_client/application/controllers/g3_client.php b/web_client/application/controllers/g3_client.php index 8caf86cb..a8c6e91b 100644 --- a/web_client/application/controllers/g3_client.php +++ b/web_client/application/controllers/g3_client.php @@ -43,9 +43,9 @@ class G3_Client_Controller extends Template_Controller { try { $token = G3Remote::instance()->get_access_token($post["user"], $post["password"]); Session::instance()->set("g3_client_access_token", $token); - $resource = G3Remote::instance()->get_resource("gallery"); + $response = G3Remote::instance()->get_resource("gallery"); $valid = true; - $content = $this->_get_main_view($resource); + $content = $this->_get_main_view($response->resource); } catch (Exception $e) { Kohana_Log::add("error", Kohana_Exception::text($e)); $valid = false; diff --git a/web_client/application/controllers/g3_handlers.php b/web_client/application/controllers/g3_handlers.php index 4e6569ea..f3f034b2 100644 --- a/web_client/application/controllers/g3_handlers.php +++ b/web_client/application/controllers/g3_handlers.php @@ -22,7 +22,7 @@ class G3_Handlers_Controller extends Controller { $path = $this->input->get("path"); if ($_POST) { try { - unset($_POST["submit"]); + unset($_POST["do_edit"]); $result = G3Remote::instance()->update_resource("gallery/$path", $_POST); if ($result->status == "OK") { $form = null; @@ -53,12 +53,25 @@ class G3_Handlers_Controller extends Controller { public function add($type) { $path = $this->input->get("path"); if ($_POST) { - unset($_POST["submit"]); - $_POST["name"] = empty($_POST["name"]) ? $_POST["title"] : $_POST["name"]; - $_POST["name"] = g3_client::sanitize_filename($_POST["name"]); - $_POST["slug"] = empty($_POST["slug"]) ? $_POST["title"] : $_POST["slug"]; - $_POST["slug"] = g3_client::sanitize_slug($_POST["slug"]); - $result = G3Remote::instance()->add_resource("gallery/$path/{$_POST['slug']}", $_POST); + try { + unset($_POST["do_edit"]); + $data = array( + "title" => $_POST["title"], + "name" => g3_client::sanitize_title($_POST["name"], $_POST["title"]), + "slug" => g3_client::sanitize_title($_POST["slug"], $_POST["title"]), + "description" => $_POST["description"]); + + if ($_FILES) { + if (empty($_FILES["image"]["error"])) { + unset($_FILES["image"]["error"]); + $data["image"] = (object)$_FILES["image"]; + } else { + throw new Exception("File upload failed for reason: {$_FILES['image']['error']}"); + } + } + + $path = !empty($path) ? $path . "/" : $path; + $result = G3Remote::instance()->add_resource("gallery/$path{$data['slug']}", $data); if ($result->status == "OK") { $form = null; $result = "success"; @@ -71,6 +84,12 @@ class G3_Handlers_Controller extends Controller { } $result = "display"; } + } catch (Exception $e) { + Kohana_Log::add("error", (string)$e); + $form = g3_client::get_form($type, true, $path, (object)$_POST); + $form->errors["form_error"] = $e->getMessage(); + $result = "error"; + } } else { $form = g3_client::get_form($type, true, $path); $result = "display"; diff --git a/web_client/application/helpers/g3_client.php b/web_client/application/helpers/g3_client.php index 40941d38..5cd0aceb 100644 --- a/web_client/application/helpers/g3_client.php +++ b/web_client/application/helpers/g3_client.php @@ -28,8 +28,8 @@ class g3_client_Core { // @todo add sort column sort order fields $form->errors = array("title" => "", "name" => "", "description" => "", "slug" => ""); if ($type != "album" && $add_form) { - $form->form["image_file"] = (object)array("value" => "", "label" => "Image File"); - $form->errors["image_file"] = ""; + $form->form["image"] = (object)array("value" => "", "label" => "Image File"); + $form->errors["image"] = ""; } if (empty($path) && !$add_form) { @@ -52,6 +52,7 @@ class g3_client_Core { $json->form = new View("edit.html"); $json->form->title = ($form->adding ? "Add " : "Update ") . ucwords($type); $json->form->url = $form->adding ? "add" : "edit"; + $json->form->button_text = $form->adding ? "Add" : "Update"; $json->form->path = $path; $json->form->type = $type; $json->form->form = (object)$form->form; @@ -70,19 +71,8 @@ class g3_client_Core { * @param string $filename * @return string sanitized filename */ - static function sanitize_filename($filename) { - $filename = empty($filename) ? $title : $filename; - $filename = strtr($filename, " ", "_"); - $filename = preg_replace("/\..*?$/", "", $filename); - return preg_replace("/ +/", " ", $filename); - } - - /** - * Santize the input url into something safe we can us as the url component. - * @param string $filename - */ - static function sanitize_slug($uri) { - $result = preg_replace("/[^A-Za-z0-9-_]+/", "-", $uri); + static function sanitize_title($field, $title) { + $result = preg_replace("/[^A-Za-z0-9-_]+/", "-", empty($field) ? $title : $field); return trim($result, "-"); } } diff --git a/web_client/application/libraries/G3Remote.php b/web_client/application/libraries/G3Remote.php index 98b964ea..1d4854ac 100644 --- a/web_client/application/libraries/G3Remote.php +++ b/web_client/application/libraries/G3Remote.php @@ -19,20 +19,66 @@ */ class url_connection { static function post($url, $post_data_array, $extra_headers=array()) { - $_data_raw = self::_encode_data($post_data_array, $extra_headers); + //$_data_raw = self::_encode_data($post_data_array, $extra_headers); + $boundary = str_repeat("-", 9) . md5(microtime()); + $boundary_length = strlen($boundary); - $extra_headers['Content-Type'] = 'application/x-www-form-urlencoded'; - $extra_headers['Content-Length'] = strlen($_data_raw); + $extra_headers['Content-Type'] = "multipart/form-data; boundary=$boundary"; + $length = 0; + $fields = array(); + foreach ($post_data_array as $field => $value) { + $fields[$field] = array(); + if (is_string($value)) { + $fields[$field]["disposition"] = "Content-Disposition: form-data; name=\"$field\"\r\n\r\n"; + $fields[$field]["type"] = ""; + $fields[$field]["value"] = "$value\r\n"; + } else { + $fields[$field]["disposition"] = + "Content-Disposition: form-data; name=\"$field\"; filename=\"{$value->name}\"\r\n"; + $fields[$field]["type"] = "Content-Type: {$value->type}\r\n\r\n"; + $fields[$field]["value"] = "\r\n"; + $fields[$field]["local_file"] = $value->tmp_name; + $length += $value->size; + } + $length += strlen($fields[$field]["disposition"]) + strlen($fields[$field]["value"]) + + strlen($fields[$field]["type"]) + $boundary_length + 4; + } + $length += $boundary_length + 6; // boundary terminator and last crlf + $extra_headers['Content-Length'] = $length; + + $socket = url_connection::_open_socket($url, 'POST', $extra_headers); + + $sent_length = 0; + foreach ($fields as $field => $value) { + $sent_length += fwrite($socket, "--$boundary\r\n"); + $sent_length += fwrite($socket, $value["disposition"]); + if (!empty($value["type"])) { + $sent_length += fwrite($socket, $value["type"]); + $file = fopen($value["local_file"], "rb"); + while (!feof($file)) { + $buffer = fread($file, 8192); + $sent_length += fwrite($socket, $buffer); + fflush($socket); + } + } + $sent_length += fwrite($socket, $value["value"]); + fflush($socket); + } + + $sent_length += fwrite($socket, "--$boundary--\r\n"); + fflush($socket); /* Read the web page into a buffer */ - return self::do_request($url, 'POST', $extra_headers, $_data_raw); + return url_connection::_get_response($socket); } static function get($url, $_data_array=array(), $extra_headers=array()) { $_data_raw = self::_encode_data($_data_array, $extra_headers); + $handle = url_connection::_open_socket("{$url}?$_data_raw", "GET", $extra_headers); + /* Read the web page into a buffer */ - return self::do_request("{$url}?$_data_raw", "GET", $extra_headers); + return url_connection::_get_response($handle); } static function success($response_status) { @@ -60,12 +106,9 @@ class url_connection { } /** - * A single request, without following redirects - * - * @todo: Handle redirects? If so, only for GET (i.e. not for POST), and use G2's - * WebHelper_simple::_parseLocation logic. + * Open the socket to server */ - static function do_request($url, $method='GET', $headers=array(), $body='') { + static function _open_socket($url, $method='GET', $headers=array()) { /* Convert illegal characters */ $url = str_replace(' ', '%20', $url); @@ -81,18 +124,19 @@ class url_connection { $header_lines[] = $key . ': ' . $value; } - $success = fwrite($handle, sprintf("%s %s HTTP/1.0\r\n%s\r\n\r\n%s", + $success = fwrite($handle, sprintf("%s %s HTTP/1.0\r\n%s\r\n\r\n", $method, $url_components['uri'], - implode("\r\n", $header_lines), - $body)); - if (!$success) { - // Zero bytes written or false was returned - // log "fwrite failed in requestWebPage($url)" . ($success === false ? ' - false' : '' - return array(null, null, null); - } + implode("\r\n", $header_lines))); fflush($handle); + return $handle; + } + + /** + * Read the http response + */ + static function _get_response($handle) { /* * Read the status line. fgets stops after newlines. The first line is the protocol * version followed by a numeric status code and its associated textual phrase. diff --git a/web_client/application/views/add.html.php b/web_client/application/views/add.html.php index 77185656..462cad39 100644 --- a/web_client/application/views/add.html.php +++ b/web_client/application/views/add.html.php @@ -26,9 +26,9 @@
  • -
    - - {$errors["image_file"]}" ?> +
    + + {$errors["image"]}" ?>
  • diff --git a/web_client/application/views/edit.html.php b/web_client/application/views/edit.html.php index c89a18e9..e94dc0e9 100644 --- a/web_client/application/views/edit.html.php +++ b/web_client/application/views/edit.html.php @@ -27,11 +27,11 @@ empty($form->slug->readonly) ? "" : "readonly={$form->slug->readonly}") ?> slug) ? "" : "{$errors->slug}" ?>
  • - image_file)): ?> + image)): ?>
  • - image_file->label}:") ?>
    - - image_file) ? "" : "{$errors->image_file}" ?> + image->label}:") ?>
    + + image) ? "" : "{$errors->image}" ?>
  • form_error)): ?> @@ -40,7 +40,7 @@
  • - + 'reset','name'=>'reset'), "Reset") ?>
  • diff --git a/web_client/js/g3_client.js b/web_client/js/g3_client.js index af1c3531..2c612977 100644 --- a/web_client/js/g3_client.js +++ b/web_client/js/g3_client.js @@ -192,12 +192,13 @@ $.getJSON("/g3_client/index.php/" + dialog, {path: resource_path}, function(data) { $("#g-dialog").html(data.form); - $("#g-dialog").dialog("open"); if ($("#g-dialog fieldset legend").length) { $("#g-dialog").dialog('option', 'title', $("#g-dialog fieldset legend:eq(0)").html()); } _ajaxifyDialog(); + $("#g-dialog").dialog("open"); + }); } @@ -206,7 +207,7 @@ $("#g-dialog form").ajaxForm({ dataType: "json", beforeSubmit: function(formData, form, options) { - form.find(":submit") + form.find(":submit, :reset") .addClass("ui-state-disabled") .attr("disabled", "disabled"); return true;