示例#1
0
 function layers()
 {
     $this->template->content = new View('admin/layers');
     $this->template->content->title = Kohana::lang('ui_admin.layers');
     // setup and initialize form field names
     $form = array('action' => '', 'layer_id' => '', 'layer_name' => '', 'layer_url' => '', 'layer_file' => '', 'layer_color' => '');
     // copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $parents_array = array();
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = Validation::factory(array_merge($_POST, $_FILES));
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         if ($post->action == 'a') {
             // Add some rules, the input field, followed by a list of checks, carried out in order
             $post->add_rules('layer_name', 'required', 'length[3,80]');
             $post->add_rules('layer_color', 'required', 'length[6,6]');
             $post->add_rules('layer_url', 'url');
             $post->add_rules('layer_file', 'upload::valid', 'upload::type[kml,kmz]');
             if (empty($_POST['layer_url']) && empty($_FILES['layer_file']['name']) && empty($_POST['layer_file_old'])) {
                 $post->add_error('layer_url', 'atleast');
             }
             if (!empty($_POST['layer_url']) && (!empty($_FILES['layer_file']['name']) || !empty($_POST['layer_file_old']))) {
                 $post->add_error('layer_url', 'both');
             }
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             $layer_id = $post->layer_id;
             $layer = new Layer_Model($layer_id);
             if ($post->action == 'd') {
                 // Delete Action
                 // Delete KMZ file if any
                 $layer_file = $layer->layer_file;
                 if (!empty($layer_file) && file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                 }
                 $layer->delete($layer_id);
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             } else {
                 if ($post->action == 'v') {
                     // Show/Hide Action
                     if ($layer->loaded == true) {
                         if ($layer->layer_visible == 1) {
                             $layer->layer_visible = 0;
                         } else {
                             $layer->layer_visible = 1;
                         }
                         $layer->save();
                         $form_saved = TRUE;
                         $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                     }
                 } else {
                     if ($post->action == 'i') {
                         // Delete KMZ/KML Action
                         if ($layer->loaded == true) {
                             $layer_file = $layer->layer_file;
                             if (!empty($layer_file) && file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                                 unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                             }
                             $layer->layer_file = null;
                             $layer->save();
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                         }
                     } else {
                         if ($post->action == 'a') {
                             // Save Action
                             $layer->layer_name = $post->layer_name;
                             $layer->layer_url = $post->layer_url;
                             $layer->layer_color = $post->layer_color;
                             $layer->save();
                             // Upload KMZ/KML
                             $path_info = upload::save("layer_file");
                             if ($path_info) {
                                 $path_parts = pathinfo($path_info);
                                 $file_name = $path_parts['filename'];
                                 $file_ext = $path_parts['extension'];
                                 if (strtolower($file_ext) == "kmz") {
                                     // This is a KMZ Zip Archive, so extract
                                     $archive = new Pclzip($path_info);
                                     if (TRUE == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                                         foreach ($archive_files as $file) {
                                             $ext_file_name = $file['filename'];
                                         }
                                     }
                                     if ($ext_file_name && $archive->extract(PCLZIP_OPT_PATH, Kohana::config('upload.directory')) == TRUE) {
                                         // Okay, so we have an extracted KML - Rename it and delete KMZ file
                                         rename($path_parts['dirname'] . "/" . $ext_file_name, $path_parts['dirname'] . "/" . $file_name . ".kml");
                                         $file_ext = "kml";
                                         unlink($path_info);
                                     }
                                 }
                                 $layer->layer_file = $file_name . "." . $file_ext;
                                 $layer->save();
                             }
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
                         }
                     }
                 }
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('layer'));
             $form_error = TRUE;
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('layer')->count_all()));
     $layers = ORM::factory('layer')->orderby('layer_name', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->layers = $layers;
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/layers_js');
 }
示例#2
0
 /**
  * Unzip the file.
  * 
  * @param String zip_file-- the zip file to be extracted.
  * @param String destdir-- destination directory
  */
 public function unzip_ushahidi($zip_file, $destdir)
 {
     $archive = new Pclzip($zip_file);
     $this->log[] = sprintf("Unpacking %s ", $zip_file);
     if (@$archive->extract(PCLZIP_OPT_PATH, $destdir) == 0) {
         $this->errors[] = sprintf(Kohana::lang('libraries.upgrade_extracting_error'), $archive->errorInfo(true));
         return false;
     }
     $this->log[] = sprintf("Unpacking went successful");
     $this->success = true;
     return true;
 }
示例#3
0
 /**
  * Add Edit Layers (KML, KMZ, GeoRSS)
  */
 public function layers()
 {
     $this->template->content = new View('admin/layers');
     $this->template->content->title = Kohana::lang('ui_admin.layers');
     // Setup and initialize form field names
     $form = array('action' => '', 'layer_id' => '', 'layer_name' => '', 'layer_url' => '', 'layer_file' => '', 'layer_color' => '');
     // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $parents_array = array();
     // Check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Fetch the submitted data
         $post_data = array_merge($_POST, $_FILES);
         // Layer instance for the actions
         $layer = (isset($post_data['layer_id']) and Layer_Model::is_valid_layer($post_data['layer_id'])) ? new Layer_Model($post_data['layer_id']) : new Layer_Model();
         // Check for action
         if ($post_data['action'] == 'a') {
             // Manually extract the primary layer data
             $layer_data = arr::extract($post_data, 'layer_name', 'layer_color', 'layer_url', 'layer_file_old');
             // Grab the layer file to be uploaded
             $layer_data['layer_file'] = isset($post_data['layer_file']['name']) ? $post_data['layer_file']['name'] : NULL;
             // Extract the layer file for upload validation
             $other_data = arr::extract($post_data, 'layer_file');
             // Set up validation for the layer file
             $post = Validation::factory($other_data)->pre_filter('trim', TRUE)->add_rules('layer_file', 'upload::valid', 'upload::type[kml,kmz]');
             // Test to see if validation has passed
             if ($layer->validate($layer_data) and $post->validate()) {
                 // Success! SAVE
                 $layer->save();
                 $path_info = upload::save("layer_file");
                 if ($path_info) {
                     $path_parts = pathinfo($path_info);
                     $file_name = $path_parts['filename'];
                     $file_ext = $path_parts['extension'];
                     if (strtolower($file_ext) == "kmz") {
                         // This is a KMZ Zip Archive, so extract
                         $archive = new Pclzip($path_info);
                         if (TRUE == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                             foreach ($archive_files as $file) {
                                 $ext_file_name = $file['filename'];
                             }
                         }
                         if ($ext_file_name and $archive->extract(PCLZIP_OPT_PATH, Kohana::config('upload.directory')) == TRUE) {
                             // Okay, so we have an extracted KML - Rename it and delete KMZ file
                             rename($path_parts['dirname'] . "/" . $ext_file_name, $path_parts['dirname'] . "/" . $file_name . ".kml");
                             $file_ext = "kml";
                             unlink($path_info);
                         }
                     }
                     $layer->layer_file = $file_name . "." . $file_ext;
                     $layer->save();
                 }
                 $form_saved = TRUE;
                 array_fill_keys($form, '');
                 $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
             } else {
                 // Validation failed
                 // Repopulate the form fields
                 $form = arr::overwrite($form, array_merge($layer_data->as_array(), $post->as_array()));
                 // Ropulate the error fields, if any
                 $errors = arr::overwrite($errors, array_merge($layer_data->errors('layer'), $post->errors('layer')));
                 $form_error = TRUE;
             }
         } elseif ($post_data['action'] == 'd') {
             // Delete action
             if ($layer->loaded) {
                 // Delete KMZ file if any
                 $layer_file = $layer->layer_file;
                 if (!empty($layer_file) and file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                 }
                 $layer->delete();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             }
         } elseif ($post_data['action'] == 'v') {
             // Show/Hide Action
             if ($layer->loaded == TRUE) {
                 $layer->layer_visible = $layer->layer_visible == 1 ? 0 : 1;
                 $layer->save();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
             }
         } elseif ($post_data['action'] == 'i') {
             // Delete KML/KMZ action
             if ($layer->loaded == TRUE) {
                 $layer_file = $layer->layer_file;
                 if (!empty($layer_file) and file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                 }
                 $layer->layer_file = null;
                 $layer->save();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
             }
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('layer')->count_all()));
     $layers = ORM::factory('layer')->orderby('layer_name', 'asc')->find_all($this->items_per_page, $pagination->sql_offset);
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->layers = $layers;
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/layers_js');
 }
 /**
  * Add Edit geometrys (KML, KMZ, GeoRSS)
  */
 public function index()
 {
     $this->template->content = new View('densitymap/settings');
     $this->template->content->title = Kohana::lang('densitymap.densitymap');
     // Setup and initialize form field names
     $form = array('action' => '', 'geometry_id' => '', 'geometry_name' => '', 'kml_file' => '', 'label_lat' => '', 'label_long' => '');
     // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $parents_array = array();
     // Check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Fetch the submitted data
         $post_data = array_merge($_POST, $_FILES);
         // geometry instance for the actions
         $geometry = (isset($post_data['geometry_id']) and Densitymap_geometry_Model::is_valid_geometry($post_data['geometry_id'])) ? new Densitymap_geometry_Model($post_data['geometry_id']) : new Densitymap_geometry_Model();
         // Check for action
         if ($post_data['dm_action'] == 'a') {
             // Manually extract the primary geometry data
             $geometry_data = arr::extract($post_data, 'category_id', 'kml_file_old', 'label_lat', 'label_lon');
             // Grab the geometry file to be uploaded
             $geometry_data['kml_file'] = isset($post_data['kml_file']['name']) ? $post_data['kml_file']['name'] : NULL;
             // Extract the geometry file for upload validation
             $other_data = arr::extract($post_data, 'kml_file');
             // Set up validation for the geometry file
             $post = Validation::factory($other_data)->pre_filter('trim', TRUE)->add_rules('kml_file', 'upload::valid', 'upload::type[kml,kmz]');
             $old_file = $geometry->kml_file;
             // Test to see if validation has passed
             if ($geometry->validate($geometry_data) and $post->validate(false)) {
                 $geometry->kml_file = $old_file;
                 $geometry->category_id = $geometry_data["category_id"];
                 $geometry->label_lat = $geometry_data["label_lat"];
                 $geometry->label_lon = $geometry_data["label_lon"];
                 // Success! SAVE
                 $geometry->save();
                 $path_info = upload::save("kml_file");
                 if ($path_info) {
                     $path_parts = pathinfo($path_info);
                     $file_name = $path_parts['filename'];
                     $file_ext = $path_parts['extension'];
                     if (strtolower($file_ext) == "kmz") {
                         // This is a KMZ Zip Archive, so extract
                         $archive = new Pclzip($path_info);
                         if (TRUE == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                             foreach ($archive_files as $file) {
                                 $ext_file_name = $file['filename'];
                             }
                         }
                         if ($ext_file_name and $archive->extract(PCLZIP_OPT_PATH, Kohana::config('upload.directory')) == TRUE) {
                             // Okay, so we have an extracted KML - Rename it and delete KMZ file
                             rename($path_parts['dirname'] . "/" . $ext_file_name, $path_parts['dirname'] . "/" . $file_name . ".kml");
                             $file_ext = "kml";
                             unlink($path_info);
                         }
                     }
                     $json_file_name = $this->parse_kml($file_name, $file_ext);
                     //delete the KML file
                     unlink(Kohana::config('upload.directory', TRUE) . $file_name . "." . $file_ext);
                     $geometry->kml_file = $json_file_name;
                     $geometry->save();
                     //delete old file
                     if (!empty($old_file) and file_exists(Kohana::config('upload.directory', TRUE) . $old_file)) {
                         unlink(Kohana::config('upload.directory', TRUE) . $old_file);
                     }
                 }
                 $form_saved = TRUE;
                 array_fill_keys($form, '');
                 $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
             } else {
                 // Validation failed
                 // Repopulate the form fields
                 $form = arr::overwrite($form, array_merge($geometry_data->as_array(), $post->as_array()));
                 // Ropulate the error fields, if any
                 $errors = arr::overwrite($errors, array_merge($geometry_data->errors('geometry'), $post->errors('geometry')));
                 $form_error = TRUE;
             }
         } elseif ($post_data['dm_action'] == 'd') {
             // Delete action
             if ($geometry->loaded) {
                 // Delete KMZ file if any
                 $kml_file = $geometry->kml_file;
                 if (!empty($kml_file) and file_exists(Kohana::config('upload.directory', TRUE) . $kml_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $kml_file);
                 }
                 $geometry->delete();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             }
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('densitymap_geometry')->count_all()));
     $geometrys = ORM::factory('densitymap_geometry')->orderby('id', 'asc')->find_all($this->items_per_page, $pagination->sql_offset);
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->geometrys = $geometrys;
     //get array of categories
     $categories = ORM::factory("category")->where("category_visible", "1")->find_all();
     $cat_array = array();
     foreach ($categories as $category) {
         $cat_array[$category->id] = $category->category_title;
     }
     $this->template->content->cat_array = $cat_array;
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('densitymap/settings_js');
 }
示例#5
0
function PMBP_save_FTP($files, $packed = false)
{
    global $CONF;
    global $PMBP_SYS_VAR;
    $out = FALSE;
    // try to connect to server using username and passwort
    if (!$CONF['ftp_server']) {
        $out .= "<div class=\"red\">" . C_WRONG_FTP . "</div>";
    } elseif (!($conn_id = @ftp_connect($CONF['ftp_server'], $CONF['ftp_port'], $PMBP_SYS_VAR['ftp_timeout']))) {
        $out .= "<div class=\"red\">" . F_FTP_1 . " '" . $CONF['ftp_server'] . "'!</div>";
    } else {
        if (!($login_result = @ftp_login($conn_id, $CONF['ftp_user'], $CONF['ftp_passwd']))) {
            $out .= "<div class=\"red\">" . F_FTP_2 . " '" . $CONF['ftp_user'] . "'.</div>";
        } else {
            // succesfully connected -> set passive and change to the right path
            if ($CONF['ftp_pasv']) {
                ftp_pasv($conn_id, TRUE);
            } else {
                ftp_pasv($conn_id, FALSE);
            }
            if (!$CONF['ftp_path']) {
                $path = ".";
            } else {
                $path = $CONF['ftp_path'];
            }
            @ftp_chdir($conn_id, $path);
            // backup as one ZIP file
            if ($packed) {
                include_once "pclzip.lib.php";
                $filename = $CONF['sitename'] . "." . time() . ".zip";
                $pclzip = new Pclzip(PMBP_EXPORT_DIR . $filename);
                $pclzip->create($files);
                // try three times to upload zip files
                $check = FALSE;
                for ($i = 0; $i < 3; $i++) {
                    if (!$check) {
                        $check = ftp_put($conn_id, $filename, PMBP_EXPORT_DIR . $filename, FTP_BINARY);
                    }
                }
                if ($check) {
                    // adjust file permissions on ftp server
                    //ftp_chmod($conn_id,substr(sprintf('%o', fileperms(PMBP_EXPORT_DIR.$filename)), -4),$filename);
                    $out .= "<div class=\"green\">" . F_FTP_4 . " '" . $filename . "'.</div>\n";
                } else {
                    $out .= "<div class=\"red\">" . F_FTP_3 . ".</div>\n";
                }
                @unlink(PMBP_EXPORT_DIR . $filename);
                // backup each file
            } else {
                // create all missing folders
                foreach ($files as $filepath) {
                    if ($filepath = trim($filepath)) {
                        $folders = explode("/", $filepath);
                        $filename = array_pop($folders);
                        $deep = 0;
                        $all_folders = "";
                        $all_folders_local = "";
                        foreach ($folders as $folder) {
                            $all_folders_local .= $folder . "/";
                            if ($folder != "." && $folder != "..") {
                                if (!@ftp_chdir($conn_id, $folder)) {
                                    @ftp_mkdir($conn_id, $folder);
                                    @ftp_chdir($conn_id, $folder);
                                }
                                // adjust directory permissions
                                //ftp_chmod($conn_id,substr(sprintf('%o', fileperms("../".$folder)), -4),"../".$folder);
                                $all_folders .= $folder . "/";
                                $deep++;
                            }
                        }
                        // change back to $path
                        $rel_path = "";
                        for ($i = 0; $i < $deep; $i++) {
                            $rel_path .= "../";
                        }
                        @ftp_chdir($conn_id, $rel_path);
                        // define the source and destination pathes
                        $dest_file = $all_folders . $filename;
                        $source_file = "./" . $filepath;
                        // try three times to upload
                        $check = FALSE;
                        for ($i = 0; $i < 3; $i++) {
                            if (!$check) {
                                $check = @ftp_put($conn_id, $dest_file, $source_file, FTP_BINARY);
                            }
                        }
                        if ($check) {
                            // adjust file permissions on ftp server
                            //ftp_chmod($conn_id,substr(sprintf('%o', fileperms($source_file)), -4),$dest_file);
                            $out .= "<div class=\"green\">" . F_FTP_4 . " '" . $dest_file . "'.</div>\n";
                        } else {
                            $out .= "<div class=\"red\">" . F_FTP_3 . ": '" . $source_file . "' -> '" . $dest_file . "'.</div>\n";
                        }
                    }
                }
            }
            // close the FTP connection
            if (@function_exists("ftp_close")) {
                @ftp_close($conn_id);
            }
        }
    }
    return $out;
}
示例#6
0
 /**
  * Unzip the file.
  * 
  * @param String zip_file-- the zip file to be extracted.
  * @param String destdir-- destination directory
  */
 public function unzip_ushahidi($zip_file, $destdir)
 {
     $archive = new Pclzip($zip_file);
     $this->log[] = sprintf("Unpacking %s ", $zip_file);
     if (@$archive->extract(PCLZIP_OPT_PATH, $destdir) == 0) {
         $this->errors[] = sprintf('Error while extracting: <code>%s</code>', $archive->errorInfo(true));
         return false;
     }
     $this->log[] = sprintf("Unpacking went successful");
     $this->success = true;
     return true;
 }