function add_ftp_connections()
 {
     /* $cus = new CustomFields();
               //$cus->initForType("Users",2);
               $flds = $cus->getCustomFieldsByType("Users");
     
               //$cus->initForType("Project",1);
               if(is_array($flds) && count($flds) > 0)
               {
               foreach ($flds as $key => $value) {
               $settings["$key"]["label"] = "Comments";
               $settings["$key"]["is_enabled"] = "1";
               $cus->setCustomFieldsByType("Users", $settings);
     
               }
               }
               print_r($flds);
               die(); */
     //$settings["label"];
     //$cus->setCustomFieldsByType("Project", $settings);
     $repo_branches = $this->active_repository;
     $eng = $repo_branches->getEngine($this->active_project->getId());
     //print_r($eng->getBranches);
     $branches_array = $eng->getBranches();
     $repo_branches_str = implode(",", $branches_array);
     $repo_id = array_var($_GET, 'project_source_repository_id');
     //project objects id
     $repo_obj = new ProjectSourceRepository($repo_id);
     $src_repo_id = $repo_obj->getIntegerField1();
     if ($this->request->isSubmitted()) {
         // check for form submission
         $post_data = $this->request->post();
         //print_r($post_data);
         try {
             $errors = new ValidationErrors();
             $ftpdetials = $post_data["ftpdetials"];
             $fld_cnt = count($ftpdetials["ftp_domain"]);
             //print_r($ftpdetials);
             //die();
             $array_urls = array();
             for ($i = 0; $i < $fld_cnt; $i++) {
                 if ($ftpdetials["ftp_domain"][$i] == "" || $ftpdetials["ftp_port"][$i] == "" || $ftpdetials["ftp_username"][$i] == "" || $ftpdetials["ftp_password"][$i] == "" || $ftpdetials["branches"][$i] == "" || $ftpdetials["ftp_dir"][$i] == "") {
                     $errors->addError("Please fill all connection parameters.");
                 } else {
                     $arra_conn[$i] = array("ftp_domain" => $ftpdetials["ftp_domain"][$i], "ftp_port" => $ftpdetials["ftp_port"][$i], "ftp_username" => $ftpdetials["ftp_username"][$i], "ftp_password" => $ftpdetials["ftp_password"][$i], "branches" => $ftpdetials["branches"][$i], "ftp_dir" => $ftpdetials["ftp_dir"][$i]);
                 }
                 /* if(!filter_var($value, FILTER_VALIDATE_URL) && $value != "")
                    {
                    $errors->addError("$value is not a valid URL.");
                    }
                    else
                    {
                    $array_urls[] = $value;
                    } */
             }
             if ($errors->hasErrors()) {
                 throw $errors;
             }
             DB::beginWork('Add FTP Details @ ' . __CLASS__);
             if (is_array($arra_conn) && count($arra_conn) > 0) {
                 $ftp_details_exists = ProjectGitolite::ftp_connections_exists($src_repo_id);
                 if (is_array($ftp_details_exists) && $ftp_details_exists["ftp_cnt"] > 0) {
                     $ftp_table_name = TABLE_PREFIX . "rt_ftp_connections";
                     DB::execute("DELETE FROM {$ftp_table_name} where repo_fk = '" . $src_repo_id . "'");
                 }
                 for ($i = 0; $i < $fld_cnt; $i++) {
                     $ftp_details_add = ProjectGitolite::add_ftp_details($arra_conn[$i], $src_repo_id, $this->logged_user->getId());
                     if (!$ftp_details_add) {
                         $errors->addError('Problem occured while saving data, please try again.');
                         throw $errors;
                     }
                 }
                 DB::commit('FTP details Added @ ' . __CLASS__);
                 $this->response->ok();
             } else {
                 $errors->addError("Error while saving FTP details.");
                 throw $errors;
             }
         } catch (Exception $e) {
             DB::rollback('Failed to add FTP details @ ' . __CLASS__);
             $this->response->exception($e);
         }
     }
     $ftp_details_exists = ProjectGitolite::get_connection_details($src_repo_id);
     $this->response->assign(array('repo_branches_str' => $repo_branches_str, 'branches_array' => $branches_array, 'ftp_test_url' => Router::assemble('test_ftp_conn', array('project_slug' => $this->active_project->getSlug(), 'project_source_repository_id' => $repo_id)), 'form_action' => Router::assemble('add_ftp_conn', array('project_slug' => $this->active_project->getSlug(), 'project_source_repository_id' => $repo_id)), "ftp_details_exists" => $ftp_details_exists));
 }
 /**
  * Map conf Repositories with activecollab projects
  * @return string message
  */
 function map_conf_repos()
 {
     $repo_array = array();
     if (count($repo_array) == 0) {
         $conf_file_path = GIT_FILES_PATH . "/gitolite/gitolite-admin/conf/gitolite.conf";
         if (file_exists($conf_file_path)) {
             $conf_file_contents = file($conf_file_path);
             foreach ($conf_file_contents as $key => $value) {
                 if (preg_match('/^repo\\s+(.+)/', $value, $matches) && !preg_match('/^repo\\s+(gitolite-admin)/', $value) && !preg_match('/^repo\\s+(@all)/', $value)) {
                     $repo_name = trim($matches[1]);
                     if (!array_key_exists($repo_name, $conf_parsed)) {
                         $repo_array[$repo_name] = array();
                     }
                 } elseif (preg_match('/(.*)=\\s(.*)/', $value, $matches)) {
                     $pub_key = trim($matches[2]);
                     $pub_key_access = trim($matches[1]);
                     $repo_array[$repo_name][] = array("key_name" => $pub_key, "pub_key_access" => $pub_key_access);
                 }
             }
         }
     }
     if (isset($_GET["prj_name"])) {
         $prj_id = $_GET["prj_name"];
         $repo_name = $_GET["repo_name"];
         $user_id = $this->logged_user->getId();
         $project_obj = new Project($prj_id);
         $users_details = $project_obj->users()->describe($this->logged_user, true, true, STATE_VISIBLE);
         $users_array = array();
         if (is_foreachable($users_details)) {
             foreach ($users_details as $key => $value) {
                 $users_array[] = $value['user']['id'];
             }
         }
         $access_array = array();
         try {
             DB::beginWork('Mapping Repositories @ ' . __CLASS__);
             $selected_prj = new Project($prj_id);
             $repository_data = array('name' => $repo_name, 'update_type' => 1, 'visibility' => 0);
             $settings = GitoliteAdmin::get_admin_settings();
             $clone_url = $settings['gitoliteuser'] . "@" . $settings['gitoliteserveradd'] . ":" . $repo_name;
             $dup_cnt = ProjectGitolite::check_remote_duplication($prj_id, $repository_data, $clone_url);
             if ($dup_cnt[1]['dup_name_cnt'] > 0) {
                 die('Remote URL already cloned under this project.');
             }
             /**
              * Create access data
              * 
              */
             /* define('GITOLITE_NOACCESS', '1');
                define('GITOLITE_READACCESS', '2');
                define('GITOLITE_MANAGEACCESS', '3'); */
             if (is_array($repo_array) && count($repo_array) > 0) {
                 if (array_key_exists($repo_name, $repo_array)) {
                     if (is_foreachable($repo_array[$repo_name])) {
                         foreach ($repo_array[$repo_name] as $key => $value) {
                             //echo $value["key_name"]."====".$value["pub_key_access"];
                             $key_details = GitoliteAc::get_key_details($value["key_name"]);
                             if (is_array($key_details) && in_array($key_details["user_id"], $users_array)) {
                                 $access_array[$key_details["user_id"]] = $value["pub_key_access"] == "RW+" ? 3 : 2;
                             }
                         }
                     }
                 }
             }
             /**
              * Add rrepositories
              */
             $actual_git_repo_name = $repo_name;
             $chk_actual_name_exists_cnt = ProjectGitolite::check_actual_name_count_gitolite($actual_git_repo_name);
             if (is_array($chk_actual_name_exists_cnt) && isset($chk_actual_name_exists_cnt["actual_name_cnt"])) {
                 $cnt = $chk_actual_name_exists_cnt["actual_name_cnt"] > 0 ? $chk_actual_name_exists_cnt["actual_name_cnt"] + 1 : "";
                 $folder_append = $cnt != "" ? "-{$cnt}" : "";
             } else {
                 $folder_append = "-1";
             }
             // if git repsitory name is same , we need to change the folder name while cloning the repository
             $folder_name = $actual_git_repo_name . $folder_append;
             $work_git_path = GIT_FILES_PATH . "/" . $folder_name . "/";
             $repository_path_url = array('repository_path_url' => $work_git_path);
             $repository_data = array_merge($repository_data, $repository_path_url);
             $this->active_repository = new GitRepository();
             $this->active_repository->setAttributes($repository_data);
             $this->active_repository->setCreatedBy($this->logged_user);
             $this->active_repository->save();
             $repo_fk = $this->active_repository->getId();
             if ($repo_fk) {
                 $clone_url = $settings['gitoliteuser'] . "@" . $settings['gitoliteserveradd'] . ":" . $repo_name;
                 $body = $clone_url;
                 $prj_obj = new ProjectSourceRepository();
                 $prj_obj->setName($this->active_repository->getName());
                 $prj_obj->setBody($body);
                 $prj_obj->setVisibility($repository_data['visibility']);
                 $prj_obj->setProjectId($prj_id);
                 $prj_obj->setCreatedBy($this->logged_user);
                 $prj_obj->setType("ProjectSourceRepository");
                 $prj_obj->setModule("source");
                 $prj_obj->setState(STATE_VISIBLE);
                 $prj_obj->setParentId($this->active_repository->getId());
                 $prj_obj->save();
                 $repo_id = ProjectGitolite::add_repo_details($repo_fk, $prj_id, $user_id, $work_git_path, $repository_data, $clone_url);
                 if ($repo_id) {
                     $add_access = ProjectGitolite::add_access_levels($repo_id, serialize($access_array), $user_id, 1);
                     if ($add_access) {
                         DB::commit('Repository mapped @ ' . __CLASS__);
                         $git_server = $settings['git_clone_url'];
                         chdir(GIT_FILES_PATH);
                         //cd ".GIT_FILES_PATH." &&
                         $command = "git clone " . $git_server . $repo_name . " {$folder_name}";
                         //$command = "git clone ".$git_server.":".$repo_name;
                         exec($command, $output, $return_var);
                         $out = GitoliteAdmin::update_remote_repo($repo_fk);
                         ini_set('max_execution_time', 500);
                         $pull_all_branches = ProjectGitolite::pull_branches($actual_repo_path);
                         if (!$pull_all_branches) {
                             @ProjectGitolite::remove_directory($work_git_path);
                             die("Error while saving branches.");
                             throw $errors;
                         }
                         die("ok");
                     } else {
                         DB::rollback('Failed to map repository @ ' . __CLASS__);
                         echo "Error while saving access levels.";
                     }
                 } else {
                     DB::rollback('Failed to map repository @ ' . __CLASS__);
                     echo "Error while saving repository";
                 }
             }
         } catch (Exception $e) {
             DB::rollback('Mapping repositories @ ' . __CLASS__);
             die("Cannot map repository, try again.");
         }
         /**/
     }
 }