Example #1
0
 public function put_index($siteId)
 {
     $put = json_decode(Input::put()['model'], true);
     if ($this->isExist($siteId)) {
         try {
             DB::transaction_start();
             DB::commit();
         } catch (Exception $e) {
             DB::rollback();
         }
     } else {
     }
 }
Example #2
0
	/**
	 * update member's ngroups
	 *
	 * @param array   $ngroups array of integers
	 */
	public function update_ngroups(array $ngroups) {

		DB::transaction_start();

		$sql = "SELECT ngroup FROM member_ngroup WHERE member=".intval($this->id);
		$existing_ngroups = DB::fetchfieldarray($sql);

		$insert_ngroups = array_diff($ngroups, $existing_ngroups);
		if ($insert_ngroups) {
			$sql_groups = array();
			foreach ($insert_ngroups as $insert_ngroup) {
				$sql_groups[] = "(".intval($this->id).", ".intval($insert_ngroup).")";
			}
			DB::query("INSERT INTO member_ngroup (member, ngroup) VALUES ".join(", ", $sql_groups));
		}

		$delete_ngroups = array_diff($existing_ngroups, $ngroups);
		if ($delete_ngroups) {
			$sql = "DELETE FROM member_ngroup
				WHERE member=".intval($this->id)."
					AND ngroup IN (".join(", ", array_map("intval", $delete_ngroups)).")";
			DB::query($sql);
		}

		DB::transaction_commit();

	}
/**
 * avoid more than one execution of cron() at the same time
 *
 * @return boolean
 */
function cron_lock() {

	$pid = getmypid();
	$ps = explode(PHP_EOL, `ps -e | awk '{print $1}'`);

	DB::transaction_start();

	$result = DB::query("SELECT pid FROM cron_lock");
	if ( $row = DB::fetch_assoc($result) ) {
		// check if process is still running
		if (in_array($row['pid'], $ps)) {
			DB::transaction_commit();
			return false;
		}
		// remove lock for no longer running process
		cron_unlock();
	}

	DB::query("INSERT INTO cron_lock (pid) VALUES (".intval($pid).")");
	DB::transaction_commit();

	return true;
}
Example #4
0
	/**
	 * move the proposal to a different issue
	 *
	 * @param integer $new_issue_id
	 */
	public function move_to_issue($new_issue_id) {

		DB::transaction_start();
		$this->read();

		if ( !$this->allowed_move_to_issue() ) {
			DB::transaction_rollback();
			warning(_("Moving this proposal is not allowed anymore."));
			redirect();
		};

		$options = $this->options_move_to_issue();
		if (!isset($options[$new_issue_id])) {
			DB::transaction_rollback();
			warning(_("The selected option is not available."));
			redirect();
		}

		$old_issue = $this->issue();

		if ($new_issue_id) {
			$new_issue = new Issue($new_issue_id);
			if (!$new_issue->id) {
				DB::transaction_rollback();
				warning(_("The issue does not exist."));
				redirect();
			}
		} else {
			// create a new empty issue
			$new_issue = new Issue;
			$new_issue->area   = $old_issue->area;
			$new_issue->period = $old_issue->period;
			$new_issue->state  = $old_issue->state;
			// If the old issue reached offline voting, the new issue gets offline voting unseen the number of offline voting demanders.
			$new_issue->votingmode_reached = $old_issue->votingmode_reached;
			$new_issue->debate_started = $old_issue->debate_started;
			$new_issue->create();
		}

		$this->issue = $new_issue->id;

		if ( ! $this->update(['issue']) ) {
			DB::transaction_rollback();
			return;
		}

		DB::transaction_commit();

		// cancel empty issue
		if ( ! $old_issue->proposals() ) $old_issue->cancel();

		// send notification
		$notification = new Notification("proposal_moved");
		$notification->issue_old = $old_issue;
		$notification->issue     = $new_issue;
		$notification->proposal  = $this;
		// votingmode voters of both issues
		$sql = "SELECT DISTINCT member FROM votingmode_token WHERE issue=".intval($old_issue->id)." OR issue=".intval($new_issue->id);
		$recipients = DB::fetchfieldarray($sql);
		// supporters and proponents of the proposal
		$sql = "SELECT DISTINCT member FROM supporter WHERE proposal=".intval($this->id);
		$recipients = array_unique(array_merge($recipients, DB::fetchfieldarray($sql)));
		$notification->send($recipients);

	}
Example #5
0
	/**
	 * start online voting
	 *
	 * @param array   $issues
	 */
	public function start_voting(array $issues) {

		// entitled members of the ngroup
		$sql = "SELECT member.* FROM member
			JOIN member_ngroup ON member.id = member_ngroup.member AND member_ngroup.ngroup=".intval($this->ngroup)."
			WHERE activated IS NOT NULL AND eligible=TRUE AND verified=TRUE";
		$members = DB::fetchobjectarray($sql, "Member");

		$personal_tokens = array();
		$all_tokens      = array();
		foreach ($issues as $issue) {
			/** @var $issue Issue */

			// generate vote tokens
			$all_tokens[$issue->id] = array();
			foreach ( $members as $member ) {
				DB::transaction_start();
				do {
					$token = Login::generate_token(8);
					$sql = "SELECT token FROM vote_token WHERE token=".DB::esc($token);
				} while ( DB::numrows($sql) );
				$sql = "INSERT INTO vote_token (member, issue, token) VALUES (".intval($member->id).", ".intval($issue->id).", ".DB::esc($token).")";
				DB::query($sql);
				DB::transaction_commit();
				$personal_tokens[$member->id][$issue->id] = $token;
				$all_tokens[$issue->id][]                 = $token;
			}

			$issue->state = "voting";
			$issue->update(["state"], 'voting_started=now()');

		}

		// notification mails
		$subject = sprintf(_("Voting started in period %d"), $this->id);
		$body_top = _("Group").": ".$this->ngroup()->name."\n\n"
			._("Online voting has started on the following proposals").":\n";
		$body_lists = "\n"._("Voting end").": ".datetimeformat($this->counting)
			."\n\n===== "._("Lists of all vote tokens")." =====\n";
		$issues_blocks = array();
		foreach ( $issues as $issue ) {
			$body_lists .= "\n"
				._("Issue")." ".$issue->id.":\n"
				.join(", ", $all_tokens[$issue->id])."\n";
			$issues_blocks[$issue->id] = "\n"._("Issue")." ".$issue->id."\n";
			foreach ( $issue->proposals(true) as $proposal ) {
				$issues_blocks[$issue->id] .= _("Proposal")." ".$proposal->id.": ".$proposal->title."\n"
					.BASE_URL."proposal.php?id=".$proposal->id."\n";
			}
		}
		foreach ( $members as $member ) {
			if (!$member->mail) continue;
			$body = $body_top;
			foreach ( $issues as $issue ) {
				$body .= $issues_blocks[$issue->id]
					._("Vote").": ".BASE_URL."vote.php?issue=".$issue->id."\n"
					._("Your vote token").": ".$personal_tokens[$member->id][$issue->id]."\n";
			}
			$body .= $body_lists;
			send_mail($member->mail, $subject, $body, array(), $member->fingerprint);
		}

	}
Example #6
0
	/**
	 * save vote for this issue
	 *
	 * @param string  $token
	 * @param array   $vote
	 */
	public function vote($token, array $vote) {

		// example for one single proposal:
		// array( 123 => array('acceptance' => 0) )
		// example for two proposals:
		// array( 123 => array('acceptance' => 1, 'score' => 2), 456 => array('acceptance' => -1, 'score' => 0) )

		// convert strings to integers
		foreach ( $vote as &$value ) {
			$value = array_map('intval', $value);
		}
		unset($value);

		DB::transaction_start();

		$sql = "INSERT INTO vote_vote (token, vote) VALUES (".DB::esc($token).", ".DB::esc(serialize($vote)).") RETURNING votetime";
		if ( $result = DB::query($sql) ) {
			list($votetime) = pg_fetch_row($result);

			if (!Login::$member->mail) {
				notice(_("Your vote has been saved, but the email receipt could not be sent, because you have no confirmed email address!"));
			} else {

				// Since the subject can not be encrypted, we don't show which issue.
				$subject = _("Vote receipt");

				$body = _("Group").": ".$this->area()->ngroup()->name."\n\n";

				$body .= sprintf(_("Vote receipt for your vote on issue %d:"), $this->id)."\n\n";
				foreach ( $vote as $proposal_id => $vote_proposal ) {
					$proposal = new Proposal($proposal_id);
					$body .= mb_wordwrap(_("Proposal")." ".$proposal_id.": ".$proposal->title)."\n"
						.BASE_URL."proposal.php?id=".$proposal->id."\n"
						._("Acceptance").": ".acceptance($vote_proposal['acceptance']);
					if (isset($vote_proposal['score'])) $body .= ", "._("Score").": ".score($vote_proposal['score']);
					$body .= "\n\n";
				}
				$body .= _("Your username").": ".Login::$member->username."\n"
					._("Your user ID").": ".Login::$member->id."\n"
					._("Your vote token").": ".$token."\n"
					._("Voting time").": ".date(VOTETIME_FORMAT, strtotime($votetime))."\n\n"
					._("You can change your vote by voting again on:")."\n"
					.BASE_URL."vote.php?issue=".$this->id."\n";

				if ( send_mail(Login::$member->mail, $subject, $body, array(), Login::$member->fingerprint) ) {
					success(_("Your vote has been saved and an email receipt has been sent to you."));
				} else {
					warning(_("Your vote has been saved, but the email receipt could not be sent!"));
				}

			}

			DB::transaction_commit();

		} else {
			warning(_("Your vote could not be saved!"));
			DB::transaction_rollback();
		}

	}
			} else {
				// create group that does not yet exist
				$ngroup = new Ngroup;
				$ngroup->name   = $name;
				$ngroup->parent = $parent;
				$ngroup->create(['name', 'parent']);
				echo "Created group '$name'\n";
			}
			$ngroup_map[$name] = $ngroup->id;
		}
		$ngroups[] = $ngroup_map[$name];
		$parent    = $ngroup_map[$name];
	}

	$sql = "SELECT * FROM member WHERE invite=".DB::esc($invite);
	DB::transaction_start();
	$result = DB::query($sql);
	if ( $member = DB::fetch_object($result, "Member") ) {
		$member->eligible = (bool) $data[1];
		$member->verified = (bool) $data[2];
		$member->update(['eligible', 'verified']);
	} else {
		$member = new Member;
		$member->invite = $invite;
		$member->eligible = (bool) $data[1];
		$member->verified = (bool) $data[2];
		$member->create(['invite', 'eligible', 'verified'], ['invite_expiry'=>"now() + ".DB::esc(INVITE_EXPIRY)]);
		++$inserted;
	}
	DB::transaction_commit();