/**
  * 
  * Display gitolite admin page
  * @return void
  */
 function index()
 {
     $this->wireframe->actions->add('need_help', lang('Need Help?'), Router::assemble('need_help_path'), array('onclick' => new FlyoutFormCallback('repository_created'), 'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE)));
     $gitoliteadminpath = GitoliteAdmin::get_admin_path();
     $settings = GitoliteAdmin::get_admin_settings();
     $gitoliteadminpath = $settings['gitoliteadminpath'] == "" ? "" : $settings['gitoliteadminpath'] . "gitolite-admin/";
     $domain_name = GitoliteAdmin::get_server_name();
     $server_name = $settings['gitoliteserveradd'] == "" ? $domain_name : $settings['gitoliteserveradd'];
     $git_server_location = $settings['git_server_location'] == "" ? "local" : $settings['git_server_location'];
     $git_ssh_port = !isset($settings['git_ssh_port']) || $settings['git_ssh_port'] == "" ? 22 : $settings['git_ssh_port'];
     //$is_auto = ($settings['initialize_repo'] == "") ? "No" : $settings['initialize_repo'];
     if ($settings['gitoliteuser'] == "") {
         $gitoliteuser = "";
         $is_enable = FALSE;
     } else {
         $gitoliteuser = $settings['gitoliteuser'];
         $is_enable = TRUE;
     }
     $setup_script = GitoliteAdmin::get_setup_path();
     $empty_repositories = GitoliteAdmin::get_empty_repositories();
     if (is_array($empty_repositories) && count($empty_repositories) > 0) {
         $i = 0;
         foreach ($empty_repositories as $key => $value) {
             $srcobj = new ProjectSourceRepository($value['obj_id']);
             $empty_repositories[$i]["view_url"] = $srcobj->getViewUrl();
             $i++;
         }
     }
     $delete_url = Router::assemble('delele_repo_url');
     $this->response->assign(array('settings' => $settings, 'empty_repositories' => $empty_repositories, 'setup_script' => $setup_script, 'gitoliteuser' => $gitoliteuser, 'gitoliteadminpath' => $gitoliteadminpath, 'server_name' => $server_name, 'git_server_location' => $git_server_location, 'delete_url' => $delete_url, 'git_ssh_port' => $git_ssh_port, 'is_enable' => $is_enable));
 }
 /**
  * List public keys in inline tabs
  * @return void
  */
 function getpublickeys()
 {
     $active_user = $this->active_user;
     $user_public_keys = GitoliteAc::fetch_keys($active_user->getId());
     $is_gitolite = GitoliteAdmin::is_gitolite();
     $this->smarty->assign(array('user_public_keys' => $user_public_keys, 'icon' => AngieApplication::getImageUrl('layout/button-add.png', ENVIRONMENT_FRAMEWORK, AngieApplication::getPreferedInterface()), 'delete_icon' => AngieApplication::getImageUrl('icons/12x12/delete.png', ENVIRONMENT_FRAMEWORK, AngieApplication::getPreferedInterface()), 'add_url' => Router::assemble('add_public_keys', array('company_id' => $active_user->getCompanyId(), 'user_id' => $active_user->getId())), 'del_url' => $this->active_user->getViewUrl(), 'is_gitolite' => $is_gitolite));
 }
function smarty_function_notifications($params, &$smarty)
{
    $user = array_required_var($params, 'user', true, 'IUser');
    $activity_logs = NotificationsActivityLogs::findRecent($user);
    $interface = array_var($params, 'interface', AngieApplication::getPreferedInterface(), true);
    $id = isset($params['id']) && $params['id'] ? $params['id'] : HTML::uniqueId('activity_log');
    $wrapper = '<div id="' . $id . '" class="quick_view_item">';
    $wrapper .= '</div>';
    list($authors, $subjects, $targets) = ActivityLogs::loadRelatedDataFromActivities($activity_logs, $user);
    // Load related data, so we can pass it to callbacks
    return $wrapper .= '<script type="text/javascript">$("#' . $id . '").activityLog(' . JSON::encode(array('entries' => $activity_logs, 'authors' => $authors, 'subjects' => $subjects, 'targets' => $targets, 'callbacks' => ActivityLogs::getCallbacks(), 'decorator' => ActivityLogs::getDecorator($interface), 'interface' => $interface)) . ');</script>';
}
 /**
  * 
  * Add custom buttons on Admin Source page
  * @return void
  */
 function index()
 {
     parent::index();
     $this->wireframe->actions->add('add_source_gitolite_repository', lang('Add Gitolite Repository'), Router::assemble('add_source_gitolite_repository'), array('onclick' => new FlyoutFormCallback('repository_created', array('width' => 'narrow')), 'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE)));
     $this->wireframe->actions->add('clone_source_git', lang('Clone Remote Repository'), Router::assemble('clone_source_git_repository'), array('onclick' => new FlyoutFormCallback('repository_created', array('width' => '900')), 'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE)));
 }
 /**
  * Check repository access and add repository help on history page
  * @return void
  */
 function history()
 {
     //ProjectGitolite::delete_commits($this->active_repository->getId());
     /*         * $this->wireframe->actions->add('branches', lang('Branches'), '#', array(
               'subitems' => self::ac_gitolite_get_branches(),
               'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE),
               'id'=> 'branches_list'
               ));
     
               $this->wireframe->actions->add('tags', lang('Tags'), '#', array(
               'subitems' => self::ac_gitolite_get_tags(),
               'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE),
               'id'=> 'tags_list'
               ));
              */
     $repo_id = array_var($_GET, 'project_source_repository_id');
     //project objects id
     $project = $this->active_project;
     $repository = $this->active_repository;
     $repo_details = ProjectGitolite::get_repo_details($repo_id);
     if (is_array($repo_details) && count($repo_details) > 0) {
         //if ( $this->logged_user->isAdministrator () || $this->logged_user->isProjectManager () || $project->isLeader ( $this->logged_user ) ) {
         $this->wireframe->actions->add('manage_access', lang('Manage Access'), Router::assemble('edit_git_repository', array('project_slug' => $project->getSlug(), 'project_source_repository_id' => $repo_id)), array('id' => 'update_access_levels', 'onclick' => new FlyoutFormCallback("access_updated", array('width' => 'narrow')), 'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE)));
         //} //if
     }
     /*  Commented FTP section
          $this->wireframe->actions->add('add_ftp', 'FTP Connectiions', Router::assemble('add_ftp_conn',array('project_slug' => $this->active_project->getSlug(),
          'project_source_repository_id' => $repo_id)), array(
          'onclick' => new FlyoutFormCallback('ftps_updated', array('width' => '1100')),
          'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE))
          );
         */
     $this->wireframe->actions->add('add_hooks', 'Hooks', Router::assemble('add_hooks_git', array('project_slug' => $this->active_project->getSlug(), 'project_source_repository_id' => $repo_id)), array('onclick' => new FlyoutFormCallback('urls_updated', array('width' => '900')), 'icon' => AngieApplication::getPreferedInterface() == AngieApplication::INTERFACE_DEFAULT ? AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE) : AngieApplication::getImageUrl('icons/16X16-git.png', AC_GITOLITE_MODULE, AngieApplication::INTERFACE_PHONE)));
     $repo_path = $repository->getRepositoryPathUrl();
     $repo_fk = $repository->getId();
     $chk_gitolite = ProjectGitolite::is_gitolite_repo($repo_fk);
     if (is_array($chk_gitolite) && sizeof($chk_gitolite) > 0 && $chk_gitolite['chk_gitolite'] > 0) {
         $settings = GitoliteAdmin::get_admin_settings();
         $clone_url = $settings['git_clone_url'] . $this->active_repository->getName() . ".git";
         $permissions = @unserialize($chk_gitolite['permissions']);
         if ($permissions !== false || $permissions === 'b:0;') {
             $permissions_array = $permissions;
         } else {
             $permissions_array = array();
         }
         if (array_key_exists($this->logged_user->getId(), $permissions_array) && $permissions_array[$this->logged_user->getId()] > 1 || $this->logged_user->isAdministrator() || $this->logged_user->isProjectManager() || $this->active_project->isLeader($this->logged_user)) {
             $body = "<h2>Git Global Setup</h2>";
             $body .= "<code>";
             $body .= "git config --global user.name '" . $this->logged_user->getDisplayName() . "'" . "<br>";
             $body .= "git config --global user.email '" . $this->logged_user->getEmail() . "'" . "<br>";
             $body .= "</code>";
             $body .= "<h2>Create Repository:</h2>";
             $body .= "<code>";
             $body .= "mkdir " . $this->active_repository->getName() . "<br>";
             $body .= "cd " . $this->active_repository->getName() . "<br>";
             $body .= "git init" . "<br>";
             $body .= "touch README" . "<br>";
             $body .= "git add README" . "<br>";
             $body .= "git commit -m 'first commit'" . "<br>";
             $body .= "git remote add origin " . $clone_url . "<br>";
             $body .= "git push -u origin master" . "<br>";
             $body .= "</code>";
             $body .= "<h2>Existing Git Repo?</h2>";
             $body .= "<code>";
             $body .= "cd existing_git_repo" . "<br>";
             $body .= "git remote add origin " . $clone_url . "<br>";
             $body .= "git push -u origin master";
             $body .= "</code>";
             $body_text = $body;
         } else {
             $this->response->forbidden();
         }
     } else {
         $body_text = $repository->getFieldValue("repository_path_url");
     }
     parent::history();
     $this->response->assign(array('body_text' => $body_text, 'repo_path' => $repo_path, 'clone_url' => $clone_url));
 }
 function &logUserIn(User $user, $settings = null)
 {
     $existing_session_id = isset($settings['existing_session_id']) && $settings['existing_session_id'] ? $settings['existing_session_id'] : null;
     try {
         DB::beginWork('Logging user in @ ' . __CLASS__);
         $users_table = TABLE_PREFIX . 'users';
         $user_sessions_table = TABLE_PREFIX . 'user_sessions';
         $remember = (bool) array_var($settings, 'remember', false);
         $new_visit = (bool) array_var($settings, 'new_visit', false);
         // Some initial data
         $session_id = null;
         $new_expires_on = $remember ? time() + 1209600 : time() + 1800;
         // 30 minutes or 2 weeks?
         // Existing session
         if ($existing_session_id) {
             $existing_session_data = DB::executeFirstRow("SELECT remember, session_key, interface FROM {$user_sessions_table} WHERE id = ?", $existing_session_id);
             if ($existing_session_data && isset($existing_session_data['remember']) && isset($existing_session_data['session_key'])) {
                 if ($existing_session_data['remember']) {
                     $new_expires_on = time() + 1209600;
                 }
                 // if
                 $session_key = $existing_session_data['session_key'];
                 DB::execute("UPDATE {$user_sessions_table} SET user_ip = ?, user_agent = ?, last_activity_on = UTC_TIMESTAMP(), expires_on = ?, visits = visits + 1 WHERE id = ?", AngieApplication::getVisitorIp(), AngieApplication::getVisitorUserAgent(), date(DATETIME_MYSQL, $new_expires_on), $existing_session_id);
                 $session_id = $existing_session_id;
                 AngieApplication::setPreferedInterface($existing_session_data['interface']);
             }
             // if
         }
         // if
         // New session?
         if ($session_id === null) {
             AngieApplication::setPreferedInterface(array_var($settings, 'interface'));
             do {
                 $session_key = make_string(40);
             } while (DB::executeFirstCell("SELECT COUNT(id) AS 'row_count' FROM {$user_sessions_table} WHERE session_key = ?", $session_key) > 0);
             DB::execute("INSERT INTO {$user_sessions_table} (user_id, user_ip, user_agent, visits, remember, interface, created_on, last_activity_on, expires_on, session_key) VALUES (?, ?, ?, ?, ?, ?, UTC_TIMESTAMP(), ?, ?, ?)", $user->getId(), AngieApplication::getVisitorIp(), AngieApplication::getVisitorUserAgent(), 1, (int) $remember, AngieApplication::getPreferedInterface(), date(DATETIME_MYSQL), date(DATETIME_MYSQL, $new_expires_on), $session_key);
             $session_id = DB::lastInsertId();
         }
         // if
         // Update last visit time
         if ($new_visit) {
             DB::execute("UPDATE {$users_table} SET last_visit_on = last_login_on, last_login_on = ?, last_activity_on = ? WHERE id = ?", date(DATETIME_MYSQL), date(DATETIME_MYSQL), $user->getId());
         } else {
             DB::execute("UPDATE {$users_table} SET last_activity_on = ? WHERE id = ?", date(DATETIME_MYSQL), $user->getId());
         }
         // if
         DB::commit('User logged in @ ' . __CLASS__);
         $this->session_id = $session_id;
         // remember it, for logout
         Cookies::setVariable($this->session_id_var_name, "{$session_id}/{$session_key}/" . date(DATETIME_MYSQL));
         return parent::logUserIn($user);
     } catch (Exception $e) {
         DB::rollback('Failed to log user in @ ' . __CLASS__);
         throw $e;
     }
     // try
 }