/lib/applicationlib.php
PHP | 6486 lines | 4237 code | 1169 blank | 1080 comment | 521 complexity | fbe1054208d01d13503510492a4b7040 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /* vim: set expandtab tabstop=4 shiftwidth=4: */
- /* ====================================================================
- * GNU Lesser General Public License
- * Version 2.1, February 1999
- *
- * <one line to give the library's name and a brief idea of what it does.>
- *
- * Copyright (C) 2001~2012 Adam Preston
- *
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- * $Id:$
- */
- /*
- Gets the possible outcomes for a match
- */
- function mysqli_result($res, $row, $field=0) {
- $res->data_seek($row);
- $datarow = $res->fetch_array();
- return $datarow[$field];
- }
- function getMatchScores($reservationid){
- $query = "SELECT gameswon,gameslost
- FROM tblMatchScore
- INNER JOIN tblCourts ON tblMatchScore.courttypeid = tblCourts.courttypeid
- INNER JOIN tblReservations ON tblCourts.courtid = tblReservations.courtid
- WHERE tblReservations.reservationid = $reservationid";
- return db_query($query);
- }
- function get_site_password($siteid){
-
- $sitePasswordQuery = "SELECT sites.password FROM tblClubSites sites WHERE sites.siteid = $siteid";
- $sitePasswordResult = db_query($sitePasswordQuery);
- $sitePasswordResultArray = mysqli_fetch_array($sitePasswordResult);
-
- return $sitePasswordResultArray[0];
- }
- /*
- Use send grid to send out emails from the admin
- */
- function sendgrid_clubmail($subject, $to_emails, $content, $category ){
-
- if (isDebugEnabled(1)) {
- logMessage("applicationlib.sendgrid_clubmail: sending email with subject $subject with a size " . count($to_emails) );
- }
- if( count($to_emails) == 0){
- if (isDebugEnabled(1)) {
- logMessage("applicationlib.sendgrid_clubmail: there is a problem with sending mail for $subject, exiting..." );
- }
- return;
- }
-
- $apiKey = $_SESSION["CFG"]["sendgrid_api"];
- $sendgrid = new SendGrid($apiKey);
- $mail = new SendGrid\Mail();
- foreach ($to_emails as $k=>$v){
-
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: sending email to: $k with subject $subject and category $category" );
-
- $personalization = new SendGrid\Personalization();
- $email = new SendGrid\Email($v['name'], $k);
- $personalization->addTo($email);
- $personalization->addSubstitution("%firstname%", $v['name']);
- if ( array_key_exists('url', $v) ){
- $personalization->addSubstitution("%signupurl%", $v['url']);
- }
- else {
- $personalization->addSubstitution("%signupurl%","");
- }
- $mail->addPersonalization($personalization);
- }
- $file_contents = file_get_contents($_SESSION["CFG"]["templatedir"]."/email/blank.email.html");
- $template = str_replace("%sitecode%", get_sitecode(), $file_contents);
- $template = str_replace("%content%", $content->line1, $template);
- $template = str_replace("%dns%", $_SESSION["CFG"]["dns"], $template);
- $template = str_replace("%app_root%", $_SESSION["CFG"]["wwwroot"], $template);
-
- $html_content = new SendGrid\Content("text/html", $template);
- $from_email = new SendGrid\Email(get_userfullname(), get_email() );
-
-
- $mail->setFrom($from_email);
- $mail->setSubject($subject);
- $mail->addCategory("Club Email");
- $mail->addCategory($content->clubname);
- $mail->addContent($html_content);
- try {
- $response = $sendgrid->client->mail()->send()->post($mail);
- } catch (Exception $e) {
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: Caught exception: " );
- }
-
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: mail was sent with status ". $response->statusCode() ." number of emails sent: ". count($personalization->getTos()) );
- }
- /*
- Uses sendgrid to send out system generated emails. Check out Sendgrid.com
- */
- function sendgrid_email($subject, $to_emails, $content, $category){
-
- if (isDebugEnabled(1)) {
- logMessage("applicationlib.sendgrid_email: sending email with subject $subject with a size " . count($to_emails) );
- }
-
- if( count($to_emails) == 0){
- if (isDebugEnabled(1)) {
- logMessage("applicationlib.sendgrid_email: there is a problem with sending mail for $subject, exiting..." );
- }
- return;
- }
-
- $apiKey = $_SESSION["CFG"]["sendgrid_api"];
- $sendgrid = new SendGrid($apiKey);
- $file_contents = file_get_contents($_SESSION["CFG"]["templatedir"]."/email/standard.email.html");
- $template = str_replace("%clubname%", $content->clubname, $file_contents);
- $template = str_replace("%sitecode%", get_sitecode(), $template);
- $template = str_replace("%content%", $content->line1, $template);
- $template = str_replace("%dns%", $_SESSION["CFG"]["dns"], $template);
- $template = str_replace("%app_root%", $_SESSION["CFG"]["wwwroot"], $template);
- $mail = new SendGrid\Mail();
- foreach ($to_emails as $k=>$v){
-
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: sending email to: $k with subject $subject and category $category and ".$v['name'] );
-
- $personalization = new SendGrid\Personalization();
- $email = new SendGrid\Email($v['name'], $k);
- $personalization->addTo($email);
- $personalization->addSubstitution("%firstname%", $v['name']);
- if ( array_key_exists('url', $v) ){
- $personalization->addSubstitution("%signupurl%", $v['url']);
- }
- else {
- $personalization->addSubstitution("%signupurl%","");
- }
- $mail->addPersonalization($personalization);
- }
- $from_email = new SendGrid\Email("Sportsynergy", $_SESSION["CFG"]["mailer.email"]);
- $html_content = new SendGrid\Content("text/html", $template);
-
- $mail->setFrom($from_email);
- $mail->setSubject($subject);
- $mail->addCategory($category);
- $mail->addCategory($content->clubname);
- $mail->addContent($html_content);
- try {
- $response = $sendgrid->client->mail()->send()->post($mail);
- } catch (Exception $e) {
- echo 'Caught exception: ', $e->getMessage(), "\n";
- }
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: mail was sent with status ". $response->statusCode() );
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: number of emails sent: ". count($personalization->getTos()) );
- if (isDebugEnabled(1)) logMessage("applicationlib.sendgrid_email: response body: ". $response->body() );
-
- }
- /**
- * Calls the PostageApp
- *
- * @param $subject
- * @param $to_email
- * @param $to_name
- * @param $content an array of line1, line2, line3
- */
- function send_email($subject, $to_emails, $from_email, $content, $template) {
-
- if (isDebugEnabled(1)) {
- logMessage("applicationlib.send_email: sending email with subject $subject with a size " . count($to_emails) . " from $from_email");
- }
- $variables = array(
- 'line1' => $content->line1,
- 'clubname' => $content->clubname
- );
- // Setup some headers
- $header = array(
- 'From' => $from_email,
- 'Reply-to' => $from_email
- );
- // Send it all
- $ret = PostageApp::mail($to_emails, $subject, $template, $header, $variables);
- // Checkout the response
- if ($ret->response->status == 'ok') {
- if (isDebugEnabled(1)) logMessage("applicationlib.send_email: SUCCESS An email was sent and the following response was received ".$ret->response->message);
-
- } else {
- if (isDebugEnabled(1)) logMessage("applicationlib.send_email: ERROR sending your email: ".$ret->response->status );
-
- }
- //return $response;
-
- }
- /**
- *
- * @param $dateString
- */
- function formatDate($dateString) {
- return date("Y-n-d G:i:s", $dateString);
- }
- /**
- * Logs in user
- *
- * One very interesting thing here is that anyone can use a superpassword to login.
- *
- * @param String $username
- * @param String $password
- * @param bool $encodedpassword
- */
- function verify_login($username, $password, $encodedpassword) {
- /* verify the username and password. if it is a valid login, return an array
- * with the username, firstname, lastname, and email address of the user */
- $superpassword = "20ffeee869abfadb176a66075c5f1816";
-
- if ($encodedpassword) {
- $password = md5($password);
- }
-
- if (isDebugEnabled(1)) logMessage("applicationlib.verify_login: Logging in $username");
- $loginQuery = "SELECT users.userid, users.username, users.firstname, users.lastname, users.email, clubuser.roleid, club.clubname
- FROM tblUsers users, tblClubUser clubuser, tblClubs club
- WHERE users.username = '$username'
- AND users.userid = clubuser.userid
- AND clubuser.clubid='" . get_clubid() . "'
- AND club.clubid = clubuser.clubid
- AND users.password = '$password'
- AND clubuser.enable='y'
- AND clubuser.enddate IS NULL";
- $loginResult = db_query($loginQuery);
-
- // If the login fails see if the superpassword was used
-
- if (mysqli_num_rows($loginResult) == 0) {
- //encode the superpassword
- // If they used the superpassword, then just get the user.
-
- if ($superpassword == $password) {
- $loginQuery = "SELECT users.userid, users.username, users.firstname, users.lastname, users.email, clubuser.roleid, club.clubname
- FROM tblUsers users, tblClubUser clubuser, tblClubs club
- WHERE users.username = '$username'
- AND users.userid = clubuser.userid
- AND clubuser.clubid='" . get_clubid() . "'
- AND club.clubid = clubuser.clubid
- AND clubuser.enable='y'
- AND clubuser.enddate IS NULL";
- $loginResult = db_query($loginQuery);
- }
- }
- return db_fetch_array($loginResult);
- }
- /**
- * Loads user (logs in with no password)
- * @param
- */
- function load_user($userid) {
- $loginQuery = "SELECT users.userid, users.username, users.firstname, users.lastname, users.email, clubuser.clubid, clubuser.roleid, club.clubname
- FROM tblUsers users, tblClubUser clubuser, tblClubs club
- WHERE users.userid = clubuser.userid
- AND clubuser.clubid
- AND clubuser.clubid='" . get_clubid() . "'
- AND users.userid = '$userid'
- AND clubuser.enable='y'
- AND club.clubid = clubuser.clubid
- AND clubuser.enddate IS NULL ";
- $loginResult = db_query($loginQuery);
- return db_fetch_array($loginResult);
- }
- /**
- * Returns all users with the given username.
- * @param unknown_type $username
- * @param unknown_type $clubid
- */
- function getAllUsersWithIdResult($username, $clubid) {
- $usersQuery = "SELECT users.userid, users.firstname, users.lastname
- FROM tblUsers users, tblClubUser clubuser
- WHERE users.username = '$username'
- AND users.userid = clubuser.userid
- AND clubuser.clubid='" . get_clubid() . "'
- AND clubuser.enddate IS NULL";
- return db_query($usersQuery);
- }
- /**
- * This determines if the user is valid for a sport.
- *
- * @param unknown_type $siteid
- * @return boolean
- */
- function amiValidForSite($siteid) {
- $amiauthforsiteQuery = "SELECT tblkupSiteAuth.userid, tblkupSiteAuth.siteid
- FROM tblkupSiteAuth
- WHERE (((tblkupSiteAuth.userid)=" . get_userid() . ") AND ((tblkupSiteAuth.siteid)=$siteid))";
- $amiauthforsiteResult = db_query($amiauthforsiteQuery);
-
- if (mysqli_num_rows($amiauthforsiteResult) == 0) {
-
- if (isDebugEnabled(1)) logMessage("applicationlib.amiValidForSite: " . get_userid() . " is NOT valid for $siteid");
- return FALSE;
- } else {
-
- if (isDebugEnabled(1)) logMessage("applicationlib.amiValidForSite: " . get_userid() . " is valid for $siteid");
- return TRUE;
- }
- }
- /**
- * This determines if the user is valid for a site.
- * @param unknown_type $courttypeid
- * @param unknown_type $userid
- * @return boolean Returns either TRUE or FALSE
- */
- function isValidForCourtType($courttypeid, $userid) {
- $amiauthforCourtTypeQuery = "SELECT tblUserRankings.courttypeid
- FROM tblUserRankings
- WHERE tblUserRankings.userid=$userid
- AND tblUserRankings.courttypeid=$courttypeid
- AND tblUserRankings.usertype=0";
- $amiauthforCourtTypeResult = db_query($amiauthforCourtTypeQuery);
-
- if (mysqli_num_rows($amiauthforCourtTypeResult) == 0) {
-
- if (isDebugEnabled(1)) logMessage("applicationlib.amiValidForCourtType: $userid is NOT valid for $courttypeid");
- return FALSE;
- } else {
-
- if (isDebugEnabled(1)) logMessage("applicationlib.amiValidForCourtType: $userid is valid for $courttypeid");
- return TRUE;
- }
- }
- /**
- * Check to see if I am a buddy
- * @param unknown_type $userid
- */
- function amIaBuddyOf($userid) {
- $imabuddy = FALSE;
- $imabuddyQuery = "SELECT buddyid FROM tblBuddies WHERE userid=$userid";
- $imabuddyResult = db_query($imabuddyQuery);
- while ($imabuddyArray = mysqli_fetch_array($imabuddyResult)) {
-
- if ($imabuddyArray['buddyid'] == get_userid()) {
- $imabuddy = TRUE;
- }
- }
- return $imabuddy;
- }
- /**
- * Checks to see if this person is a buddy.
- * @param unknown_type $buddyid
- * @return boolean
- */
- function isABuddyOfMine($buddyid) {
- $isABuddy = FALSE;
- $imabuddyQuery = "SELECT buddyid FROM tblBuddies WHERE userid=" . get_userid();
- $imabuddyResult = db_query($imabuddyQuery);
- while ($imabuddyArray = mysqli_fetch_array($imabuddyResult)) {
-
- if ($imabuddyArray['buddyid'] == $buddyid) {
- $isABuddy = TRUE;
- }
- }
- return $isABuddy;
- }
- /**
- * This is used to quickly set a match type. Right now as far as I know there are 4 possible match types:
- * 0 practice match
- * 1 box league match
- * 2 challenge match
- * 3 buddy match
- * @param unknown_type $resid
- * @param unknown_type $matchtype
- */
- function markMatchType($resid, $matchtype) {
- $markMatchTypeQuery = "Update tblReservations SET matchtype=$matchtype WHERE reservationid=$resid AND enddate IS NULL";
- $markMatchTypeResult = db_query($markMatchTypeQuery);
- }
- /**
- * This will find out if the calling user is in a box of the
- * courttype fo the court passed in as the argument.
- * Not very fancy here, true if they are false if they are not.
- *
- * @param unknown_type $courtid
- * @param unknown_type $userid
- */
- function is_inabox($courtid, $userid) {
- $amIinThisBox = FALSE;
- $courtTypeId = get_courtTypeForCourt($courtid);
- $amiinaboxquery = "SELECT boxleagues.courttypeid, boxleaguedetails.userid
- FROM tblBoxLeagues boxleagues, tblkpBoxLeagues boxleaguedetails
- WHERE boxleagues.boxid = boxleaguedetails.boxid
- AND boxleagues.courttypeid=$courtTypeId
- AND boxleaguedetails.userid=$userid";
- // run the query on the database
- $amiinaboxresult = db_query($amiinaboxquery);
-
- if (mysqli_num_rows($amiinaboxresult) > 0) {
- $amIinThisBox = TRUE;
- }
- return $amIinThisBox;
- }
- /**
- * returns the match type for the given reservationid
- *
- * @param unknown_type $resid
- * @return unknown
- */
- function getMatchType($resid) {
- $matchtypequery = "SELECT matchtype FROM `tblReservations` WHERE reservationid=$resid";
- $matchtyperesult = db_query($matchtypequery);
- $matchtypevalueArray = mysqli_fetch_array($matchtyperesult);
- $matchtypevalue = $matchtypevalueArray[0];
-
- return $matchtypevalue;
- }
- /**
- * When you need to just get the first and last name of a user and you only
- * seem to have there userid handy then this is the function for you.
- *
- * @param unknown_type $tid
- * @return string
- */
- function get_partnerbytid($tid) {
- $firstandlastquery = "SELECT users.firstname, users.lastname
- FROM tblkpTeams teamdetails, tblUsers users
- WHERE teamdetails.userid = users.userid
- AND teamdetails.teamid=$tid
- AND users.userid !=" . get_userid();
- $firstandlastresult = db_query($firstandlastquery);
- $firstandlastarray = mysqli_fetch_array($firstandlastresult);
- return "$firstandlastarray[0] $firstandlastarray[1]";
- }
- /**
- * Figuers out if this user is in this league.
- *
- * @param unknown_type $userid
- * @param unknown_type $courttypeid
- * @param unknown_type $clubid
- */
- function isUserInClubLadder($userid, $courttypeid, $clubid) {
- $query = "SELECT 1 FROM tblClubLadder ladder WHERE ladder.userid = $userid AND ladder.courttypeid = $courttypeid AND ladder.clubid = $clubid";
- $result = db_query($query);
-
- if (mysqli_num_rows($result) > 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Does not look at the reservation match type but only if the two players
- * are in a box together and that haven't recorded the score yet.
- *
- * @param unknown_type $reservationid
- * @return boolean Returns true if this is an unscore box league.
- */
- function isUnscoredBoxLeagueReservation($reservationid) {
- //Check reservation History
- $query = "SELECT * FROM tblBoxHistory history, tblkpUserReservations reservationdetails
- WHERE history.reservationid = $reservationid
- AND reservationdetails.reservationid = history.reservationid
- AND reservationdetails.outcome = 0";
- $results = db_query($query);
- //If reservation hasnt't been scored
-
- if (mysqli_num_rows($results) == 2) {
- return true;
- }
- return false;
- }
- /**
- * Called by the court reservation page to valiidate that
- * the the user is actually in a box leage with this opponent.
- *
- * @param unknown_type $playerOneId
- * @param unknown_type $playerTwoId
- */
- function getBoxIdTheseTwoGuysAreInTogether($playerOneId, $playerTwoId) {
-
- if( empty($playerOneId) || empty($playerTwoId) ){
- return false;
- }
- $playeronequery = "SELECT boxleagues.boxid, boxleaguedetails.userid
- FROM tblBoxLeagues boxleagues, tblkpBoxLeagues boxleaguedetails
- WHERE boxleagues.boxid = boxleaguedetails.boxid
- AND boxleaguedetails.userid=$playerOneId";
- // run the query on the database
- $playeroneresult = db_query($playeronequery);
- $p1stack = array();
- $p2stack = array();
- //Put all boxes for the user in an array
- while ($playeronearray = db_fetch_array($playeroneresult)) {
- array_push($p1stack, $playeronearray[0]);
- }
- $playertwoquery = "SELECT boxleagues.boxid, boxleaguedetails.userid
- FROM tblBoxLeagues boxleagues, tblkpBoxLeagues boxleaguedetails
- WHERE boxleagues.boxid = boxleaguedetails.boxid
- AND boxleaguedetails.userid=$playerTwoId";
- // run the query on the database
- $playertworesult = db_query($playertwoquery);
- //Put all boxes for the user in an array
- while ($playertwoarray = db_fetch_array($playertworesult)) {
- array_push($p2stack, $playertwoarray[0]);
- }
- $playersintersect = array_intersect($p1stack, $p2stack);
- return $playersintersect[0];
- }
- /**
- * Returns the box id when of the box that the players share. This function is called by
- * the court reservation page to validate that the the user is actually in a
- * box leage with this opponent
- * @param $playerone
- * @param $playertwo
- */
- function are_boxplayers($playerone, $playertwo) {
-
- if( empty($playerone) || empty($playertwo) ){
- return false;
- }
- $playeronequery = "SELECT boxleagues.boxid, boxleaguedetails.userid
- FROM tblBoxLeagues boxleagues, tblkpBoxLeagues boxleaguedetails
- WHERE boxleagues.boxid = boxleaguedetails.boxid
- AND boxleaguedetails.userid=$playerone";
- // run the query on the database
- $playeroneresult = db_query($playeronequery);
- $p1stack = array();
- $p2stack = array();
- //Put all boxes for the user in an array
- while ($playeronearray = db_fetch_array($playeroneresult)) {
- array_push($p1stack, $playeronearray[0]);
- }
- $playertwoquery = "SELECT boxleagues.boxid, boxleaguedetails.userid
- FROM tblBoxLeagues boxleagues, tblkpBoxLeagues boxleaguedetails
- WHERE boxleagues.boxid = boxleaguedetails.boxid
- AND boxleaguedetails.userid=$playertwo";
- // run the query on the database
- $playertworesult = db_query($playertwoquery);
- //Put all boxes for the user in an array
- while ($playertwoarray = db_fetch_array($playertworesult)) {
- array_push($p2stack, $playertwoarray[0]);
- }
- $playersintersect = array_intersect($p1stack, $p2stack);
-
- if (count($playersintersect) > 0) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * this function will return true if the user has logged in. a user is logged
- * in if the $_SESSION["user"] is set (by the login.php page) and also if the
- * remote IP address matches what we saved in the session ($_SESSION["ip"])
- * from login.php -- this is not a robust or secure check by any means, but it
- * will do for now
- */
- function is_logged_in() {
- return isset($_SESSION) && isset($_SESSION["user"]);
- }
- /**
- * this function checks to see if the user is logged in. if not, it will show
- * the login screen before allowing the user to continue
- */
- function require_login() {
-
- if (!is_logged_in()) {
- $_SESSION["wantsurl"] = qualified_me();
- redirect($_SESSION["CFG"]["wwwroot"] . "/login.php");
- }
- }
- /**
- * this function checks to see if the user is logged in. if not, it will show
- * the login screen before allowing the user to continue
- */
- function require_loginwq() {
-
- if (!is_logged_in()) {
- // For clubsites set to auto login, look for username and password REQUEST parameters to
- // use for logging in.
- if( isSiteAutoLogin() ){
- $url_parts = parse_url(qualified_mewithq());
- parse_str($url_parts['query'], $params);
-
- if( isset($params['username']) && isset($params['password']) ){
-
- if (isDebugEnabled(1)) logMessage("applicationlib.require_loginwq: logging in autologin user: ". $params['username']);
-
- $user = verify_login( $params['username'],$params['password'], false );
- if( $user ){
- if (isDebugEnabled(1)) logMessage("applicationlib.require_loginwq: valid user");
- $_SESSION["user"] = $user;
- } else{
- redirect($_SESSION["CFG"]["wwwroot"] . "/login.php");
- }
- } else{
- redirect($_SESSION["CFG"]["wwwroot"] . "/login.php");
- }
- } else {
- $_SESSION["wantsurl"] = qualified_mewithq();
- redirect($_SESSION["CFG"]["wwwroot"] . "/login.php");
- }
- }
- }
- /**
- * this function simply returns the clubid.
- */
- function get_tzdelta() {
- return $_SESSION["siteprefs"]["timezone"] * 3600;
- }
- /**
- * this function simply returns the clubid.
- */
- function get_clubid() {
- return $_SESSION["siteprefs"]["clubid"];
- }
- /**
- * this function simply returns the siteid.
- */
- function get_siteid() {
- return $_SESSION["siteprefs"]["siteid"];
- }
- /**
- * this function simply returns the autologin status
- */
- function isSiteAutoLogin() {
- return $_SESSION["siteprefs"]["enableautologin"] == 'y' ? true : false;
- }
- /**
- * this function simply returns whether or not the recent activity should be displayed
- * @return boolean
- */
- function isDisplayRecentActivity() {
- return $_SESSION["siteprefs"]["displayrecentactivity"] == 'y' ? true : false;
- }
- /**
- * this function simply returns the daysahead or the parameter that defines how far in advance users can make.
- */
- function get_displaytime() {
- return $_SESSION["siteprefs"]["displaytime"];
- }
- /**
- * this function simply returns the whether or not the site has solo reservations enabled.
- * @return boolean
- */
- function isSoloReservationEnabled() {
- return $_SESSION["siteprefs"]["allowsoloreservations"] == 'y' ? true : false;
- }
- function isLadderRankingScheme() {
- /* this function simply returns the whether or not the site has solo reservations enabled. */
- return $_SESSION["siteprefs"]["rankingscheme"] == 'ladder' ? true : false;
- }
- function getChallengeRange() {
- /* this function simply returns the challenge range*/
- return $_SESSION["siteprefs"]["challengerange"];
- }
- function isPointRankingScheme() {
- /* this function simply returns the whether or not the site has solo reservations enabled. */
- return $_SESSION["siteprefs"]["rankingscheme"] == 'point' ? true : false;
- }
- function isSelfScoreEnabled() {
- /* this function simply returns the whether or not the site has self score enabled. */
- return $_SESSION["siteprefs"]["allowselfscore"] == 'y' ? true : false;
- }
- function isSiteEnabled() {
- /* this function simply returns the whether or not the site is enabled. */
- return $_SESSION["siteprefs"]["enable"] == 'y' ? true : false;
- }
- function getRankingAdjustment() {
- /* this function simply returns the site ranking adjustment. */
- return $_SESSION["siteprefs"]["rankingadjustment"];
- }
- function isSiteGuestReservationEnabled() {
- /* this function simply returns the siteid. */
- return $_SESSION["siteprefs"]["enableguestreservation"] == 'y' ? true : false;
- }
- function get_daysahead() {
- /* this function simply returns the daysahead or the parameter that defines how far in advance users can make. */
- return $_SESSION["siteprefs"]["daysahead"];
- }
- function get_facebookurl() {
- /* this function simply returns the the url. This is optional. */
- return $_SESSION["siteprefs"]["facebookurl"];
- }
- function isLiteVersion() {
- /* this function returns if the site is the free version. */
- return $_SESSION["siteprefs"]["isliteversion"] == 'y' ? true : false;
- }
- function isAllowAllSiteAdvertising() {
- return $_SESSION["siteprefs"]["allowallsiteadvertising"] == 'y' ? true : false;
- }
- function isNearRankingAdvertising() {
- return $_SESSION["siteprefs"]["allownearrankingadvertising"] == 'y' ? true : false;
- }
- function isDisplaySiteNavigation() {
- return $_SESSION["siteprefs"]["displaysitenavigation"] == 'y' ? true : false;
- }
- function isAllowPlayerslooking() {
- return $_SESSION["siteprefs"]["allowplayerslooking "] == 'y' ? true : false;
- }
- function get_reminders() {
- return $_SESSION["siteprefs"]["reminders"];
- }
- function isDisplayCourtTypeName() {
- return $_SESSION["siteprefs"]["displaycourttype"];
- }
- function isShowPlayerNames() {
- return $_SESSION["siteprefs"]["showplayernames"] == 'y' ? true : false;
- }
- function isRequireLogin() {
- return $_SESSION["siteprefs"]["requirelogin"] == 'y' ? true : false;
- }
- // Getter
- function get_roleid() {
- /* this function simply returns the roleid. */
- if( isset($_SESSION["user"]) ){
- return $_SESSION["user"]["roleid"];
- }
- return;
- }
- // Getter
- function get_userid() {
- /* this function simply returns the userid. */
- if( isset($_SESSION["user"]) ){
- return $_SESSION["user"]["userid"];
- }
- return;
-
- }
- function get_email() {
- /* this function simply returns the email. */
- return $_SESSION["user"]["email"];
- }
- function get_userfullname() {
- /* this function simply returns the logged in users first and last name. */
- return $_SESSION["user"]["firstname"] . " " . $_SESSION["user"]["lastname"];
- }
- function get_userfirstname() {
- /* this function simply returns the logged in users first and last name. */
- return $_SESSION["user"]["firstname"];
- }
- /*
- * this function simply returns the club name
- *
- * */
- function get_clubname() {
- return $_SESSION["siteprefs"]["clubname"];
- }
- function require_priv($roleid) {
- /* this function checks to see if the user has the privilege $roleid. if not,
- * it will display an Insufficient Privileges page and stop */
-
- if ($_SESSION["user"]["roleid"] != $roleid) {
- include ($_SESSION["CFG"]["templatedir"] . "/insufficient_privileges.php");
- die;
- }
- }
- function require_priv_user($userid) {
- /* just make sure current userid and specified user are in the same club */
- $my_site = $_SESSION["siteprefs"]["siteid"];
- $query = "SELECT * FROM tblkupSiteAuth WHERE userid = $userid AND siteid = $my_site";
- $result = db_query($query);
-
- if (mysqli_num_rows($result) == 0) {
- include ($_SESSION["CFG"]["templatedir"] . "/insufficient_privileges.php");
- die;
- }
- }
- function require_priv_reservation($reservationid) {
- /* just make sure current userid and specified user are in the same club */
- $my_site = $_SESSION["siteprefs"]["siteid"];
- $query = "SELECT 1 FROM tblReservations reservations
- INNER JOIN tblCourts courts ON reservations.courtid = courts.courtid
- WHERE reservations.reservationid = $reservationid
- AND courts.siteid = $my_site";
-
- $result = db_query($query);
-
- if (mysqli_num_rows($result) == 0) {
- if (isDebugEnabled(1)) logMessage("require_priv_reservation not allowed with site: $my_site and reservationid: $reservationid");
- include ($_SESSION["CFG"]["templatedir"] . "/insufficient_privileges.php");
- die;
- }
- }
- function require_priv_box($boxid) {
- /* just make sure current user and specified boxid are in the same club */
- $query = "SELECT siteid from tblBoxLeagues where boxid = $boxid";
- $result = db_query($query);
- $box_siteArray = mysqli_fetch_array($result);
- $box_site = $box_siteArray[0];
-
- if ($_SESSION["siteprefs"]["siteid"] != $box_site) {
- include ($_SESSION["CFG"]["templatedir"] . "/insufficient_privileges.php");
- die;
- }
- }
- function has_priv($roleid) {
- /* returns true if the user has the privilege $priv */
-
- if (isset($_SESSION["user"])) {
- return $_SESSION["user"]["roleid"] == $roleid;
- }
- }
- function atleastof_priv($roleid) {
- /* returns true if the user has the privilege $priv */
- return $_SESSION["user"]["roleid"] >= $roleid;
- }
- function err(&$errorvar) {
- /* if $errorvar is set, then print an error marker << */
-
- if (isset($errorvar)) {
- echo "<font color=#ff0000><<</font>";
- }
- }
- function err2(&$errorvar) {
- /* like err(), but prints the marker >> */
-
- if (isset($errorvar)) {
- echo "<font color=#ff0000>>></font>";
- }
- }
- function username_exists($username) {
- /* returns the true if the username exists */
- $qid = db_query("SELECT 1 FROM tblUsers users, tblClubUser clubuser
- WHERE users.username = '$username'
- AND users.userid = clubuser.userid
- AND clubuser.enddate IS NULL
- AND clubuser.clubid = " . get_clubid() . "");
- return db_num_rows($qid);
- }
- /**
- * Used to see if there is another one.
- */
- function username_already_exists($username, $userid) {
- /* returns the true if the username exists */
- $qid = db_query("SELECT users.username, users.userid FROM tblUsers users, tblClubUser clubuser
- WHERE users.username = '$username'
- AND users.userid = clubuser.userid
- AND clubuser.enddate IS NULL
- AND clubuser.clubid = " . get_clubid() . "");
- $userArray = db_fetch_array($qid);
- //If no rows are returned or if the username/userid is unique then the username is unique.
-
- if (db_num_rows($qid) == 0 || ($userArray['username'] == $username && $userArray['userid'] == $userid)) {
- return false;
- } else {
- return true;
- }
- }
- function email_exists($email) {
- /* returns true the email address exists */
- $query = "SELECT 1 FROM tblUsers users
- WHERE users.email = '$email'
- AND users.enddate IS NULL";
- $qid = db_query($query);
- return db_num_rows($qid);
- }
- /*
- *******************************************************************************************************
- ** makeTeamForCurrentUser
- creates a team, assigns a reanking and returns the new teamid for the current user
- *******************************************************************************************************
- */
- function makeTeamForCurrentUser($sportname, $partnerid) {
- global $dbh;
-
- /* Set the team identifier */
- $setteamquery = "INSERT INTO tblTeams (
- courttypeid
- ) VALUES (
- '$sportname')";
- // run the query on the database
- $setteamresult = db_query($setteamquery);
- /* Get the team id */
- $lastinsert = mysqli_insert_id($dbh);
- $addselfquery = "INSERT INTO tblkpTeams (
- teamid, userid
- ) VALUES ( $lastinsert
- ,'" . get_userid() . "')";
- // run the query on the database
- $addselfresult = db_query($addselfquery);
- /* Now add partner */
- // add self to new team
- $addpartnerquery = "INSERT INTO tblkpTeams (
- teamid, userid
- ) VALUES ( $lastinsert
- ,$partnerid)";
- // run the query on the database
- $addpartnerresult = db_query($addpartnerquery);
- // Finally update the rankings for the new team
- //Get the users doubles ranking for each team member
- $usersrankquery = "SELECT tblUserRankings.ranking
- FROM tblUserRankings
- WHERE (((tblUserRankings.userid)=" . get_userid() . "
- Or (tblUserRankings.userid)=$partnerid)
- AND ((tblUserRankings.courttypeid)=$sportname)
- AND ((tblUserRankings.usertype)=0))";
- $usersrankresult = db_query($usersrankquery);
- $rank1Array = mysqli_fetch_array($usersrankresult);
- $rank1 = $rank1Array[0];
-
- $usersrankresult = db_query($usersrankquery);
- $rank2Array = mysqli_fetch_array($usersrankresult);
- $rank2 = $rank2Array[0];
- $averagerank = ($rank1 + $rank2) / 2;
- $rankquery = "INSERT INTO tblUserRankings (
- userid, courttypeid, ranking, usertype
- ) VALUES (
- '$lastinsert'
- ,'$sportname'
- ,'$averagerank'
- ,1)";
- $rankresult = db_query($rankquery);
- $teaminfoarray = array(
- $averagerank,
- $lastinsert
- );
- return $teaminfoarray;
- }
- /*
- *******************************************************************************************************
- ** makeTeamForPlayers
- creates a team, assigns a reanking and returns the new teamid for two different players
- *******************************************************************************************************
- */
- function makeTeamForPlayers($sportname, $player1id, $player2id) {
- global $dbh;
- /* Set the team identifier */
- $setteamquery = "INSERT INTO tblTeams (
- courttypeid
- ) VALUES (
- '$sportname')";
- // run the query on the database
- $setteamresult = db_query($setteamquery);
- /* Get the team id */
- $lastinsert = mysqli_insert_id($dbh);
- $addselfquery = "INSERT INTO tblkpTeams (
- teamid, userid
- ) VALUES ( $lastinsert
- ,$player1id)";
- // run the query on the database
- $addselfresult = db_query($addselfquery);
- /* Now add partner */
- // add self to new team
- $addpartnerquery = "INSERT INTO tblkpTeams (
- teamid, userid
- ) VALUES ( $lastinsert
- ,$player2id)";
- // run the query on the database
- $addpartnerresult = db_query($addpartnerquery);
- // Finally update the rankings for the new team
- //Get the users doubles ranking for each team member
- $usersrankquery = "SELECT tblUserRankings.ranking
- FROM tblUserRankings
- WHERE (((tblUserRankings.userid)=$player1id
- Or (tblUserRankings.userid)=$player2id)
- AND ((tblUserRankings.courttypeid)=$sportname)
- AND ((tblUserRankings.usertype)=0))";
- $usersrankresult = db_query($usersrankquery);
- $rank1Array = mysqli_fetch_array($usersrankresult);
- $rank1 = $rank1Array[0];
- $usersrankresult = db_query($usersrankquery);
- $rank2Array = mysqli_fetch_array($usersrankresult);
- $rank2 = $rank2Array[0];
- $averagerank = ($rank1 + $rank2) / 2;
- $rankquery = "INSERT INTO tblUserRankings (
- userid, courttypeid, ranking, usertype
- ) VALUES (
- '$lastinsert'
- ,'$sportname'
- ,'$averagerank'
- ,1)";
- $rankresult = db_query($rankquery);
- $teaminfoarray = array(
- $averagerank,
- $lastinsert
- );
- return $teaminfoarray;
- }
- /**
- * This is really just an array funnction that will return the first element that is a duplication in the list
- */
- function findSelfTeam($array) {
- while ($teamid = array_pop($array)) {
-
- if (in_array($teamid, $array)) {
- return $teamid;
- }
- }
- }
- /**
- *
- * Enter description here ...
- * @param unknown_type $resid
- * @param unknown_type $emailType
- */
- function email_players($resid, $emailType) {
-
- if (isDebugEnabled(1)) logMessage("applicationlib.email_players: emailing Players about reservation id: $resid for a $emailType kind of email");
- //Check to see if the reservation is for a doubles court
- $usertypequery = "SELECT usertype FROM tblReservations WHERE reservationid=$resid";
- $usertyperesult = db_query($usertypequery);
- $usertypevalArray = mysqli_fetch_array($usertyperesult);
- $usertypeval = $usertypevalArray[0];
- if ($usertypeval == 0) {
- //email about a singles court
- $rquery = "SELECT courts.courtname, courts.courtid, reservations.time, users.userid, users.firstname, users.lastname, courttype.courttypeid, rankings.ranking, users.email, users.homephone, users.cellphone, users.workphone, matchtype.name
- FROM tblCourts courts, tblReservations reservations, tblUsers users, tblCourtType courttype, tblUserRankings rankings, tblkpUserReservations reservationdetails, tblMatchType matchtype
- WHERE users.userid = rankings.userid
- AND reservations.courtid = courts.courtid
- AND reservationdetails.reservationid = reservations.reservationid
- AND courttype.courttypeid = rankings.courttypeid
- AND courts.courttypeid = courttype.courttypeid
- AND reservationdetails.userid = users.userid
- AND matchtype.id = reservations.matchtype
- AND reservations.reservationid = $resid
- AND rankings.usertype=0";
- $rresult = db_query($rquery);
- $robj = mysqli_fetch_object($rresult);
- $var = new Object;
-
- if (isDebugEnabled(1)) logMessage("applicationlib.emailplayers: courtid " . $robj->courtid);
- /* email the user with the new account information */
- $var->userid = $robj->userid;
- $var->firstname = $robj->firstname;
- $var->lastname = $robj->lastname;
- $var->email = $robj->email;
- $var->homephone = $robj->homephone;
- $var->cellphone = $robj->cellphone;
- $var->workphone = $robj->workphone;
- $var->ranking = $robj->ranking;
- $var->courtname = $robj->courtname;
- $var->courtid = $robj->courtid;
- $var->matchtype = $robj->name;
- $var->time = gmdate("l F j g:i a", $robj->time);
- $var->timestamp = $robj->time;
- $var->dns = $_SESSION["CFG"]["dns"];
- $var->wwwroot = $_SESSION["CFG"]["wwwroot"];
- $var->fullname = $robj->firstname . " " . $robj->lastname;
- $var->support = $_SESSION["CFG"]["support"];
- //Set the URL
- $rawurl = "http://" . $var->dns . "" . $var->wwwroot . "/users/court_reservation.php?time=" . $var->timestamp . "&courtid=" . $var->courtid . "&userid=" . $var->userid;
- $emailbody = read_template($_SESSION["CFG"]["templatedir"] . "/email/singles_wanted.php", $var);
- $emailbody = nl2br($emailbody);
-
- if ($emailType == "3") {
- $emailidquery = "SELECT DISTINCTROW users.firstname, users.lastname, users.email, clubuser.memberid, users.password
- FROM tblUsers users, tblUserRankings rankings, tblClubUser clubuser
- WHERE users.userid = rankings.userid
- AND users.userid = clubuser.userid
- AND clubuser.clubid=" . get_clubid() . "
- AND clubuser.recemail='y'
- AND rankings.courttypeid=$robj->courttypeid
- AND rankings.usertype = 0
- AND users.userid != " . get_userid() . "
- AND clubuser.enable= 'y'
- AND clubuser.enddate IS NULL";
-
- } elseif ($emailType == "2") {
- $emailidquery = "SELECT users.firstname, users.lastname, users.email, clubuser.memberid, users.password
- FROM tblUsers users, tblBuddies buddies, tblClubUser clubuser, tblUserRankings rankings
- WHERE users.userid = buddies.buddyid
- AND users.userid = clubuser.userid
- AND clubuser.clubid=" . get_clubid() . "
- AND buddies.userid=" . get_userid() . "
- AND rankings.courttypeid=$robj->courttypeid
- AND rankings.usertype = 0
- AND users.userid = rankings.userid
- AND clubuser.enable= 'y'
- AND clubuser.enddate IS NULL";
- } elseif ($emailType == "1") {
- //Get the rankdev of the club
- $rankdevquery = "SELECT rankdev FROM tblClubs WHERE clubid=" . get_clubid() . "";
- // run the query on the database
- $rankdevresult = db_query($rankdevquery);
- $rankdevvalArray = mysqli_fetch_array($rankdevresult);
- $rankdevval = $rankdevvalArray[0];
- $highrange = $robj->ranking + $rankdevval;
- $lowrange = $robj->ranking - $rankdevval;
- //Now get all players who receive players wanted notifications at the club and are within
- //the set skill range
- $emailidquery = "SELECT DISTINCTROW users.firstname, users.lastname, users.email, clubuser.memberid, users.password
- FROM tblUsers users, tblUserRankings rankings, tblClubUser clubuser
- WHERE users.userid = rankings.userid
- AND users.userid = clubuser.userid
- AND clubuser.clubid=" . get_clubid() . "
- AND rankings.ranking>$lowrange
- AND rankings.ranking<$highrange
- AND clubuser.recemail='y'
- AND rankings.courttypeid=$robj->courttypeid
- AND rankings.usertype = 0
- AND users.userid != " . get_userid() . "
- AND clubuser.enable='y'
- AND clubuser.enddate IS NULL";
- }
- // run the query on the database
- $emailidresult = db_query($emailidquery);
- if (isDebugEnabled(1)) logMessage("applicationlib.emailplayers query: $emailidquery");
-
- $to_emails = array();
- while ($emailidrow = db_fetch_row($emailidresult)) {
-
- // Append username and password to signup url
- if( isSiteAutoLogin() ){
- //guard
- if( empty($emailidrow[3]) || empty($emailidrow[4]) ){
- if (isDebugEnabled(1)) logMessage("applicationlib.emailplayers: problems sending email to autologin user: ".$emailidrow[2]);
- continue;
- }
-
- $rawurl .= "&username=$emailidrow[3]&password=$emailidrow[4]";
- }
- $signupurl = "<a href=\"$rawurl\">here</a>.";
- if( !empty($emailidrow[0]) && !empty($emailidrow[1]) && !empty($emailidrow[2])){
- $to_email = "$emailidrow[2]";
- $to_emails[$to_email] = array(
- 'name' => $emailidrow[0],
- 'url' => $signupurl
- );
- } else {
- if (isDebugEnabled(1)) logMessage("applicationlib.emailplayers: ".get_userfullname()." not sending to $emailidrow[0] because of incomplete information");
- }
-
- }
- $content = new Object;
- $content->line1 = $emailbody;
- $content->clubname = get_clubname();
- $template = get_sitecode();
- $subject = "Player's Market Place - ". get_clubname();
- //Send the email
- sendgrid_email($subject, $to_emails, $content, "Players Wanted");
- }
- //email about a doubles court
- else {
-
- $rquery = "SELECT
- courts.courtname,
- courts.courttypeid,
- reservations.time,
- users.firstname,
- users.lastname,
- courts.courtid,
- users.userid,
- matchtype.name,
- teamdetails.teamid
- FROM
- tblReservations reservations,
- tblkpUserReservations reservationdetails,
- tblkpTeams teamdetails,
- tblUsers users,
- tblCourts courts,
- tblMatchType matchtype,
- tblClubUser clubuser
- WHERE reservationdetails.reservationid = reservations.reservationid
- AND teamdetails.teamid = reservationdetails.userid
- AND reservationdetails.usertype = 1
- AND users.userid = teamdetails.userid
- AND courts.courtid = reservations.courtid
- AND matchtype.id = reservations.matchtype
- AND reservationdetails.reservationid=$resid
- AND users.userid = clubuser.userid
- AND clubuser.clubid =" . get_clubid();
-
-
- $rresult = db_query($rquery);
- $robj = mysqli_fetch_object($rresult);
- $extraPlayerQuery = "SELECT reservationdetails.userid
- FROM tblReservations reservations, tblkpUserReservations reservationdetails
- WHERE reservations.reservationid = reservationdetails.reservationid
- AND reservationdetails.reservationid=$resid
- AND reservationdetails.usertype=0
- ORDER BY reservationdetails.userid";
- $extraPlayerResult = db_query($extraPlayerQuery);
- $extraPlayerArray = mysqli_fetch_array($extraPlayerResult);
- //Get Court Type. The reason this is done here is that in the cases of partial
- //reservations, this is empty in the query above.
- $ctQuery = "SELECT courts.courttypeid
- FROM tblReservations reservations, tblCourts courts
- WHERE reservations.reservationid=$resid
- AND reservations.courtid = courts.courtid";
- $ctResult = db_query($ctQuery);
-
- $courtTypeArray = mysqli_fetch_array($ctResult);
- $courtType = $courtTypeArray[0];
- $player1 = $robj->userid;
- $var = new Object;
- /* email the user with the new account information */
- $var->firstname1 = $robj->firstname;
- $var->lastname1 = $robj->lastname;
- $var->fullname1 = $robj->firstname . " " . $robj->lastname;
- $var->teamid = $robj->teamid;
- //Get the next result
- $robj = mysqli_fetch_object($rresult);
- $player2 = $robj->userid;
- $var->firstname2 = $robj->firstname;
- $var->lastname2 = $robj->lastname;
- $var->fullname2 = $robj->firstname . " " . $robj->lastname;
- $var->courtid = $robj->courtid;
- $var->courtname = $robj->courtname;
- $var->matchtype = $robj->name;
- $var->time = gmdate("l F j g:i a", $robj->time);
- $var->timestamp = $robj->time;
- $var->dns = $_SESSION["CFG"]["dns"];
- $var->wwwroot = $_SESSION["CFG"]["wwwroot"];
- $var->support = $_SESSION["CFG"]["support"];
- $clubfullname = get_clubname();
- $var->clubfullname = $clubfullname;
- $var->clubadminemail = "Sportsynergy <player.mailer@sportsynergy.net>";
- /* if this reservation is made with a player looking for a partner, something will
- be set in the extraPlayerQuery, if so display a different email message .
- $extraPlayerobj->userid will be 0 when taking a player removes himself
- from a reservation where he was looking for a match.
- */
- $extraPlayerUserId = 0;
- //Check for three players wanted
-
- if (db_num_rows($extraPlayerResult) == 2 && $extraPlayerArray['userid'] == 0) {
- if (isDebugEnabled(1)) logMessage("applicationlib.emailplayers: three players are wanted");
-
- //Obtain the court and matchtype information
- $rquery = "SELECT courts.courtname, matchtype.name, reservations.time, courts.courtid
- FROM tblMatchType matchtype, tblCourts courts, tblReservations reservations
- WHERE reservations.reservationid=$resid
- AND reservations.courtid = courts.courtid
- AND matchtype.id = reservations.matchtype";
-
- $rresult = db_query($rquery);
- $robj = my…
Large files files are truncated, but you can click here to view the full file