function add_source_gitolite_repository()
 {
     $is_gitolite = GitoliteAdmin::is_gitolite();
     $logged_user = $this->logged_user;
     $user_id = $logged_user->getId();
     $no_key_warning = FALSE;
     $view_url = "";
     if (AngieApplication::isModuleLoaded("source") && $this->getControllerName() == 'ac_gitolite_source') {
         $do_continue = true;
     }
     if ($do_continue) {
         // Add Administrator , Leaders and Project Manager in allowed people list
         //$role = new Roles();
         //$admins = $role::findAdministrators();
         $usrobj = new Users();
         $users_details = $usrobj->findAdministrators();
         if (is_foreachable($users_details)) {
             foreach ($users_details as $key => $value) {
                 // check key exists
                 $user_keys = GitoliteAc::check_keys_added($value->getId());
                 if ($user_keys > 0) {
                     $user_detail_permissions[$value->getId()] = array('readaccess' => 0, 'writeaccess' => 0, 'writeaccessplus' => 1, 'user_keys' => $user_keys);
                     $allowed_users[$value->getId()] = $value->getDisplayName();
                 }
             }
         }
         $this->response->assign(array('curr_users' => $allowed_users, 'user_detail_permissions' => $user_detail_permissions, 'form_action' => Router::assemble('add_source_gitolite_repository'), 'noaccess' => GITOLITE_NOACCESS, 'readaccess' => GITOLITE_READACCESS, 'manageaccess' => GITOLITE_MANAGEACCESS, 'is_gitolite' => $is_gitolite, 'no_key_warning' => $no_key_warning, 'view_url' => $view_url));
     } else {
         $this->response->assign(array('add_error' => TRUE));
     }
     if ($this->request->isSubmitted()) {
         // check for form submission
         try {
             /* Check form with validation error */
             $repository_data = $this->request->post('repository');
             if (!isset($repository_data["repo_notification_setting"])) {
                 $repository_data["repo_notification_setting"] = "no";
             }
             $errors = new ValidationErrors();
             $post_data = $this->request->post();
             $settings = GitoliteAdmin::get_admin_settings();
             $is_remote = !isset($settings["git_server_location"]) || $settings["git_server_location"] != "remote" ? false : true;
             if (!$is_remote) {
                 $sever_user_path = GitoliteAdmin::get_server_user_path();
                 if (!$sever_user_path) {
                     $errors->addError('Repository path on server invalid');
                 }
             }
             $repo_name = trim($repository_data['name']);
             $access = $post_data['access'];
             if ($repo_name == "") {
                 $errors->addError('Please enter repository name', 'repo_name');
             }
             if (!is_array($access) && count($access) == 0) {
                 $errors->addError('Select access levels for user', 'access');
             }
             /* Check for duplications repository name and Key */
             if (!$errors->hasErrors()) {
                 if (!preg_match("/^[A-Za-z0-9-]+\$/", $repo_name)) {
                     $errors->addError('Please enter valid repository name.', 'repo_name');
                 }
                 $dup_cnt = ProjectGitolite::check_source_git_dup($repository_data);
                 if (count($dup_cnt) == 0) {
                     $errors->addError('Problem occured while saving data, please try again.');
                 } elseif (count($dup_cnt) > 0) {
                     if ($dup_cnt[0]['dup_name_cnt'] > 0) {
                         $errors->addError('Repository name already used');
                     }
                 }
             }
             // if errors found throw error exception
             if ($errors->hasErrors()) {
                 throw $errors;
             }
             /** save gitolite details in database * */
             // save reponame
             try {
                 DB::beginWork('Creating a new repository @ ' . __CLASS__);
                 /**
                  * if gitolite is setup on remote, change repo path
                  */
                 if (!$is_remote) {
                     $repo_path = $sever_user_path . "/repositories/" . $repository_data['name'] . ".git";
                 } elseif ($is_remote) {
                     $repo_path = GIT_FILES_PATH . "/" . $repo_name;
                 }
                 if (is_array($post_data)) {
                     $repository_path_url = array('repository_path_url' => $repo_path);
                 }
                 $repository_data = array_merge($repository_data, $repository_path_url);
                 $clone_url = $settings['git_clone_url'] . $repo_name;
                 $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) {
                     $repo_id = ProjectGitolite::add_repo_details($repo_fk, 0, $user_id, $repo_path, $repository_data, $clone_url);
                     if ($repo_id) {
                         $add_access = ProjectGitolite::add_access_levels($repo_id, serialize($post_data['access']), $user_id, 1);
                         if ($add_access) {
                             $res = ProjectGitolite::render_conf_file();
                             $dir = $settings['gitoliteadminpath'] . "gitolite-admin";
                             $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push  || echo 'Not found'";
                             exec($command, $output, $return_var);
                             if ($is_remote) {
                                 $git_server = $settings['gitoliteuser'] . "@" . $settings['gitoliteserveradd'];
                                 //$command = "cd ".$settings['gitoliteadminpath']." && git clone ".$git_server.":".$repo_name;
                                 chdir(GIT_FILES_PATH);
                                 $command = "git clone " . $git_server . ":" . $repo_name;
                                 exec($command, $output, $return_var);
                             }
                         } else {
                             $errors->addError('Error while saving access levels.');
                             throw $errors;
                         }
                     } else {
                         $errors->addError('Error while saving repository.');
                         throw $errors;
                     }
                 } else {
                     $errors->addError('Error while saving repository.');
                     throw $errors;
                 }
                 DB::commit('Repository created @ ' . __CLASS__);
                 $this->response->respondWithData($this->active_repository, array('as' => 'repository'));
             } catch (Exception $e) {
                 DB::rollback('Failed to create a repository @ ' . __CLASS__);
                 $this->response->exception($e);
             }
         } catch (Exception $e) {
             DB::rollback('Failed to create a repository @ ' . __CLASS__);
             $this->response->exception($e);
         }
     }
 }
 /**
  * Parse keydir folder and fetch all keys added on gitolite setup
  * @return array keys
  */
 function parse_user_keys()
 {
     $array_pub_keys = array();
     $web_user = GitoliteAdmin::get_web_user();
     $key_dir_path = GIT_FILES_PATH . "/gitolite/gitolite-admin/keydir/";
     chdir($key_dir_path);
     foreach (glob('*.pub') as $i => $key) {
         //echo $key."<br>";
         $key = preg_replace('/(|@[^.]*)\\.pub$/', '', $key);
         $key_dir_file_path = $key_dir_path . $key . ".pub";
         if (file_exists($key_dir_file_path)) {
             $key_content = file_get_contents($key_dir_file_path);
             $fetch_actual_key = explode(" ", $key_content);
             $actual_key = is_array($fetch_actual_key) && count($fetch_actual_key) > 0 ? $fetch_actual_key[1] : $fetch_actual_key[0];
             $key_exists_details = GitoliteAc::check_key_map_exists($actual_key);
         }
         if (!in_array($key, $array_pub_keys) && $key != "" && !is_array($key_exists_details) && $key != $web_user) {
             $array_pub_keys[] = $key;
         } elseif (is_array($key_exists_details) && count($key_exists_details) > 0) {
             $array_pub_keys[$key_exists_details["key_name"]] = self::$ac_users[$key_exists_details["user_id"]];
         }
     }
     return $array_pub_keys;
 }
 /**
  * Edit gitolite repository access levels
  * @throws ValidationErrors
  */
 function edit_git_repo()
 {
     $repo_id = array_var($_GET, 'project_source_repository_id');
     //project objects id
     $is_gitolite = GitoliteAdmin::is_gitolite();
     if (!ProjectSourceRepositories::canAdd($this->logged_user, $this->active_project)) {
         $this->response->forbidden();
     }
     // if
     $project = $this->active_project;
     $project_id = $project->getId();
     $logged_user = $this->logged_user;
     $user_id = $logged_user->getId();
     $no_key_warning = FALSE;
     // to give warning if logged in user has not added his public key
     if (AngieApplication::isModuleLoaded("source") && $this->getControllerName() == 'project_tracking_gitolite') {
         $do_continue = true;
     }
     if ($do_continue) {
         $users_details = $this->active_project->users()->describe($this->logged_user, true, true, STATE_VISIBLE);
         $repo_details = ProjectGitolite::get_repo_details($repo_id);
         /* print_r($repo_details);
            die(); */
         $repository_data = $this->request->post('repository');
         if (!is_array($repository_data)) {
             $repository_data = array('updatetype' => $this->active_repository->getFieldValue("update_type"), 'visibility' => $this->project_object_repository->getVisibility());
         }
         // if
         if (is_array($repo_details) && count($repo_details) > 0) {
             // repository id from integer_field_1 in project_objects , we are saving this id in our tables.
             $git_repo_id = $repo_details['repo_id'];
             $access_array = ProjectGitolite::get_access_levels($git_repo_id);
             //$result_access = DB::execute("SELECT * from $access_table_name where repo_id = '".$repo_details['repo_id']."'");
             if (is_array($access_array) && count($access_array) > 0) {
                 $access = $access_array['permissions'];
                 $permissions = @unserialize($access);
                 if ($permissions !== false || $permissions === 'b:0;') {
                     $permissions_array = $permissions;
                 } else {
                     $permissions_array = array();
                 }
             } else {
                 $this->response->forbidden();
             }
         } else {
             $this->response->forbidden();
         }
         //print_r($permissions_array);
         $user_detail_permissions = array();
         $view_url = false;
         if (is_foreachable($users_details)) {
             foreach ($users_details as $key => $value) {
                 // check key exists
                 $user_keys = GitoliteAc::check_keys_added($value['user']['id']);
                 if ($user_keys > 0) {
                     $user_detail_permissions[$value['user']['id']] = array('readaccess' => $permissions_array[$value['user']['id']] == "2" ? TRUE : FALSE, 'writeaccess' => $permissions_array[$value['user']['id']] == "3" ? TRUE : FALSE, 'writeaccessplus' => $permissions_array[$value['user']['id']] == "3" ? TRUE : FALSE, 'user_keys' => $user_keys);
                     $allowed_users[$value['user']['id']] = $value['user']['name'];
                 }
             }
         }
         if (!isset($user_detail_permissions[$user_id]) && ($this->logged_user->isAdministrator() || $this->logged_user->isProjectManager())) {
             $repoobj = new ProjectSourceRepositories();
             $objuser = new rtmUser($user_id);
             $user_keys = GitoliteAc::check_keys_added($user_id);
             if ($user_keys) {
                 $user_detail_permissions[$user_id] = array('readaccess' => $permissions_array[$user_id] == "2" ? TRUE : $repoobj->canAccess($objuser, $project), 'writeaccess' => $permissions_array[$user_id] == "3" ? TRUE : $repoobj->canAdd($objuser, $project), 'writeaccessplus' => $permissions_array[$user_id] == "3" ? TRUE : $repoobj->canManage($objuser, $project), 'user_keys' => $user_keys);
                 $allowed_users[$user_id] = $logged_user->getName();
             } else {
                 $no_key_warning = TRUE;
                 $view_url = $this->logged_user->getViewUrl();
             }
         }
         $this->response->assign(array('curr_users' => $allowed_users, 'repo_details' => $repo_details, 'user_detail_permissions' => $user_detail_permissions, 'form_action' => Router::assemble('edit_git_repository', array('project_slug' => $project->getSlug(), 'project_source_repository_id' => $repo_id)), 'noaccess' => GITOLITE_NOACCESS, 'readaccess' => GITOLITE_READACCESS, 'manageaccess' => GITOLITE_MANAGEACCESS, 'is_gitolite' => $is_gitolite, 'no_key_warning' => $no_key_warning, 'repository_data' => $repository_data, 'view_url' => $view_url));
     } else {
         $this->response->assign(array('add_error' => TRUE));
     }
     if ($this->request->isSubmitted()) {
         // check for form submission
         try {
             /* Check form with validation error */
             $repository_data = $this->request->post('repository');
             $errors = new ValidationErrors();
             $post_data = $this->request->post();
             $settings = GitoliteAdmin::get_admin_settings();
             $sever_user_path = GitoliteAdmin::get_server_user_path();
             if (!$sever_user_path) {
                 //  $errors->addError ( 'Repository path on server invalid' );
             }
             $repo_path = $sever_user_path . "/repositories/" . $repository_data['name'] . ".git";
             $repo_name = trim($repository_data['name']);
             $access = $post_data['access'];
             if ($repo_name == "") {
                 $errors->addError('Please enter repository name', 'repo_name');
             }
             if (!is_array($access) && count($access) == 0) {
                 $errors->addError('Select access levels for user', 'access');
             }
             /* Check for duplications repository name and Key */
             if (!$errors->hasErrors()) {
                 if (!preg_match("/^[A-Za-z0-9-]+\$/", $repo_name)) {
                     $errors->addError('Please enter valid repository name.', 'repo_name');
                 }
                 /* $dup_cnt = ProjectGitolite::check_duplication($project_id,$repository_data);
                                       if(count($dup_cnt) == 0)
                                       {
                                       $errors->addError('Problem occured while saving data, please try again.');
                                       }
                                       elseif(count($dup_cnt) > 0)
                                       {
                                       if($dup_cnt[0]['dup_name_cnt'] > 0)
                                       {
                                       $errors->addError('You have already added repository with same name.');
                 
                                       }
                 
                                       } */
             }
             // if errors found throw error exception
             if ($errors->hasErrors()) {
                 throw $errors;
             }
             /** save gitolite details in database * */
             // save reponame
             try {
                 DB::beginWork('Update repository @ ' . __CLASS__);
                 $this->active_repository->setAttributes($repository_data);
                 $this->project_object_repository->setVisibility($repository_data['visibility']);
                 $this->project_object_repository->setName($repository_data['name']);
                 $this->active_repository->save();
                 $this->project_object_repository->save();
                 $repo_fk = $this->active_repository->getId();
                 if ($repo_id) {
                     $notif_setting = isset($repository_data["disable_notifications"]) ? "yes" : "no";
                     $repo_table_name = TABLE_PREFIX . "rt_gitolite_repomaster";
                     DB::execute("update {$repo_table_name} set disable_notifications = '" . $notif_setting . "' where repo_fk = '" . $repo_fk . "'");
                     $update_access = ProjectGitolite::update_access_levels($git_repo_id, serialize($post_data['access']));
                     if ($update_access) {
                         $res = ProjectGitolite::render_conf_file();
                         $dir = $settings['gitoliteadminpath'] . "gitolite-admin";
                         $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push  || echo 'Not found'";
                         exec($command, $output, $return_var);
                         /* $git_server = $settings['gitoliteuser']."@".$settings['gitoliteserveradd'];
                            $command = "cd ".$settings['gitoliteadminpath']." && git clone ".$git_server.":".$repo_name;
                            exec($command,$output,$return_var); */
                     } else {
                         $errors->addError('Error while saving access levels.');
                         throw $errors;
                     }
                 } else {
                     $errors->addError('Error while saving repository.');
                     throw $errors;
                 }
                 DB::commit('Repository created @ ' . __CLASS__);
                 $this->response->ok();
             } catch (Exception $e) {
                 DB::rollback('Failed to create a repository @ ' . __CLASS__);
                 $this->response->exception($e);
             }
         } catch (Exception $e) {
             DB::rollback('Failed to create a repository @ ' . __CLASS__);
             $this->response->exception($e);
         }
     }
 }
 /**
  * Remove a specific publick key
  * @return void
  */
 function remove_key()
 {
     $get_data = $this->request->get();
     if (isset($get_data['key_id'])) {
         $key_id = $get_data['key_id'];
         $filename = GitoliteAc::get_filename($key_id);
         if ($filename != "") {
             $delete_keys = GitoliteAc::remove_keys($key_id);
             if (!$delete_keys) {
                 throw new Exception("Can't delete key");
             } else {
                 $settings = GitoliteAdmin::get_admin_settings();
                 $dirpath = $settings['gitoliteadminpath'] . "gitolite-admin/keydir/";
                 $path = $dirpath . $filename . ".pub";
                 @unlink($path);
                 $active_user = $this->active_user;
                 ProjectGitolite::update_repo_conf_on_public_key($active_user->getId());
                 $command = "cd " . $dirpath . " && git add * && git commit -am 'deleted key {$filename}.pub' && git push  || echo 'Not found'";
                 exec($command, $output);
             }
         }
     }
     echo '<script type="text/javascript">window.location.href = "' . $this->active_user->getViewUrl() . '"</script>';
     die(0);
 }