} if ($ERROR == 100) { $resp = _VIEW("lobbies", array("LobbyKey" => $PARAMS['LobbyKey'])); if ($resp[0] == false) { $ERROR = $resp[1]; } if (count($resp[1]) != 1) { $ERROR = "That Lobby cannot be found"; } else { $LobbyID = $resp[1][0]["LobbyID"]; $LobbyName = $resp[1][0]["LobbyName"]; $OUTPUT["LobbyID"] = $LobbyID; $OUTPUT["LobbyName"] = $LobbyName; } } if ($ERROR == 100) { $UpperLimit = $PARAMS["UpperLimit"]; $LowerLimit = $PARAMS["LowerLimit"]; if (empty($UpperLimit)) { $UpperLimit = 10; } if (empty($LowerLimit)) { $LowerLimit = 0; } // null values for some of these are OK $resp = _VIEW("messages", array("LobbyID" => $LobbyID, "Speaker" => $PARAMS["Speaker"]), array("SQL_Override" => true, "sort" => "ORDER BY Timestamp DESC", "UpperLimit" => $UpperLimit, "LowerLimit" => $LowerLimit)); $OUTPUT["Messages"] = array(); foreach ($resp[1] as $message) { $OUTPUT["Messages"][] = $message; } }
<?php /*********************************************** DAVE PHP API https://github.com/evantahler/PHP-DAVE-API Evan Tahler | 2011 I am an example function to view a user. If "this" user is viewing (indicated by propper password hash along with another key, all data is shown), otherwise, just basic info is returned ***********************************************/ if ($ERROR == 100) { list($msg, $ReturnedUsers) = _VIEW("users", array("UserID" => $PARAMS['UserID'], "ScreenName" => $PARAMS['ScreenName'], "EMail" => $PARAMS['EMail'])); if ($msg == false) { $ERROR = $ReturnedUsers; } elseif (count($ReturnedUsers) == 1) { if (!empty($PARAMS["PasswordHash"]) || !empty($PARAMS["Password"])) { $OUTPUT["User"]['InformationType'] = "Private"; $AuthResp = AuthenticateUser(); if ($AuthResp[0] !== true) { $ERROR = $AuthResp[1]; } else { foreach ($ReturnedUsers[0] as $key => $val) { $OUTPUT["User"][$key] = $val; } } } else { $OUTPUT["User"]['InformationType'] = "Public"; $OUTPUT["User"]['ScreenName'] = $ReturnedUsers[0]['ScreenName']; $OUTPUT["User"]['Joined'] = $ReturnedUsers[0]['Joined']; } } else {
Evan Tahler | 2011 This page will attempt to check that the API key in use is OK for the function in qusetion. I use the APIKey and DeveloperID pair to authenticate the action. I am used for Private functions ***********************************************/ // Check that there is an APIKey if ($ERROR == 100) { if ($PARAMS["APIKey"] == "") { $ERROR = "You need to provide an APIKey"; } } // Check that the API Key is in the DB if ($ERROR == 100) { $CacheKey = $PARAMS["APIKey"] . "_CheckAPIKey"; $result = GetCache($CacheKey); if ($result === false) { $Results = _VIEW("developers", array("APIKey" => $PARAMS["APIKey"])); } if (count($Results[1]) == 1) { $DeveloperID_ = $Results[1][0]['DeveloperID']; $APIKey_ = $Results[1][0]['APIKey']; $UserActions = $Results[1][0]['UserActions']; $IsAdmin = $Results[1][0]['IsAdmin']; } else { $ERROR = "API Key not found"; } } // Check that the API Key has admin rights for user Actions if ($ERROR == 100) { if ($UserActions == 1) { $UserAction = true; } else {
<?php /*********************************************** DAVE PHP API https://github.com/evantahler/PHP-DAVE-API Evan Tahler | 2011 I return true or false letting a user know if they have the right LobbyName and LobbyKey. I return the LobbyID as well ***********************************************/ if ($ERROR == 100) { if (empty($PARAMS["LobbyName"])) { $ERROR = "Please Provide a LobbyName"; } } if ($ERROR == 100) { if (empty($PARAMS["LobbyKey"])) { $ERROR = "Please Provide a LobbyKey"; } } if ($ERROR == 100) { $resp = _VIEW("lobbies", array("LobbyName" => $PARAMS['LobbyName'], "LobbyKey" => $PARAMS["LobbyKey"])); if ($resp[0] == false) { $ERROR = $resp[1]; } if (count($resp[1]) == 0) { $OUTPUT["LobbyAuthentication"] = "FALSE"; } else { $OUTPUT["LobbyDetails"] = $resp[1][0]; $OUTPUT["LobbyAuthentication"] = "TRUE"; } }
function _EDIT($Table, $VARS = null) { global $TABLES, $DBOBJ, $Connection, $PARAMS; if ($VARS == null) { $VARS = $PARAMS; } if (_tableCheck($Table)) { $UniqueVars = _getUniqueTableVars($Table); $RequiredVars = _getRequiredTableVars($Table); $AllTableVars = _getAllTableCols($Table); $SQLKeys = array(); $SQLValues = array(); $Status = $DBOBJ->GetStatus(); if ($Status !== true) { return array(false, $Status); } // get the META KEY if it wasn't provided explicitly if ($VARS[$TABLES[$Table]['META']['KEY']] == "") { $SQL = 'SELECT ' . $TABLES[$Table]['META']['KEY'] . ' FROM `' . $Table . '` WHERE ( '; $NeedAnd = false; foreach ($VARS as $var => $val) { if (in_array($var, $UniqueVars) && $val != "") { if ($NeedAnd) { $SQL .= " AND "; } $SQL .= ' `' . $var . '` = "' . $val . '" '; $NeedAnd = true; } } $SQL .= ' ) ;'; $DBOBJ->Query($SQL); $Status = $DBOBJ->GetStatus(); if ($Status === true) { $results = $DBOBJ->GetResults(); if (count($results) == 1) { $VARS[$TABLES[$Table]['META']['KEY']] = $results[0][$TABLES[$Table]['META']['KEY']]; } else { return array(false, "You need to supply the META KEY for this table, " . $TABLES[$Table]['META']['KEY']); } } else { return array(false, "You need to supply the META KEY for this table, " . $TABLES[$Table]['META']['KEY'] . ", or one of the unique keys."); } } //loop if (is_array($VARS)) { foreach ($VARS as $var => $val) { if ($var != $TABLES[$Table]['META']['KEY']) { // if (in_array($var, $RequiredVars) && _isSpecialString($val)) // required // { // return array(false,$var." is a required value and you must provide a value"); // } if (in_array($var, $AllTableVars)) { if (in_array($var, $UniqueVars) && strlen($val) > 0) { $SQL = 'SELECT COUNT(1) FROM `' . $Table . '` WHERE (`' . $var . '` = "' . $val . '" AND `' . $TABLES[$Table]['META']['KEY'] . '` != "' . $VARS[$TABLES[$Table]['META']['KEY']] . '") ;'; $DBOBJ->Query($SQL); $Status = $DBOBJ->GetStatus(); if ($Status === true) { $results = $DBOBJ->GetResults(); if ($results[0]['COUNT(1)'] > 0) { return array(false, "There is already an entry of '" . $val . "' for " . $var); } else { $SQLKeys[] = $var; $SQLValues[] = $val; } } } elseif (strlen($val) > 0) { $SQLKeys[] = $var; $SQLValues[] = $val; } } } } } // if (strlen($VARS[$TABLES[$Table]['META']['KEY']]) > 0) { if (count($SQLKeys) > 0) { $SQL = "UPDATE `" . $Table . "` SET "; $i = 0; $needComma = false; while ($i < count($SQLKeys)) { if ($needComma) { $SQL .= ", "; } $SQL .= ' `' . $SQLKeys[$i] . '` = "' . mysql_real_escape_string($SQLValues[$i], $Connection) . '" '; $needComma = true; $i++; } $SQL .= ' WHERE ( `' . $TABLES[$Table]['META']['KEY'] . '` = "' . $VARS[$TABLES[$Table]['META']['KEY']] . '" ); '; $DBOBJ->Query($SQL); $Status = $DBOBJ->GetStatus(); if ($Status === true) { $NewKey = $DBOBJ->GetLastInsert(); return _VIEW($Table, $VARS); // do a view again to return fresh data } else { return array(false, $Status); } } else { return array(false, "There is nothing to change"); } } else { return array(false, "You need to provide a parameter for the KEY of this table, " . $VARS[$TABLES[$Table]['META']['KEY']]); } } else { return array(false, "This table cannot be found"); } }
<?php /*********************************************** DAVE PHP API https://github.com/evantahler/PHP-DAVE-API Evan Tahler | 2011 I will create a new lobby for folks to chat within ***********************************************/ if ($ERROR == 100) { $LobbyKey = md5($PARAMS['LobbyName'] . time() . rand()); } if ($ERROR == 100) { $resp = _ADD("lobbies", array("LobbyName" => $PARAMS['LobbyName'], "LobbyKey" => $LobbyKey)); if ($resp[0] == false) { $ERROR = $resp[1]; } else { $LobbyID = $resp[1]["LobbyID"]; $details = _VIEW("lobbies", array("LobbyID" => $LobbyID)); $OUTPUT["LobbyID"] = $LobbyID; $OUTPUT["LobbyName"] = $details[1][0]["LobbyName"]; $OUTPUT["LobbyKey"] = $details[1][0]["LobbyKey"]; } }
<?php /*********************************************** DAVE PHP API https://github.com/evantahler/PHP-DAVE-API Evan Tahler | 2011 I will list lobbies ***********************************************/ if ($ERROR == 100) { $resp = _VIEW("lobbies", null, array("SQL_Override" => true, "sort" => "ORDER BY TimeStamp DESC")); if ($resp[0] == false) { $ERROR = $resp[1]; } else { foreach ($resp[1] as $lobby) { $OUTPUT["Lobbies"][] = array("LobbyID" => $lobby["LobbyID"], "LobbyName" => $lobby["LobbyName"], "TimeStamp" => $lobby["TimeStamp"]); } } }
function AuthenticateUser($DATA = null) { // (UserID || ScreenName || EMail) + (Password || PasswordHash) || (Hash + Rand + UserID) // Hash = md5(UserID.Password.Rand) // returns arrray(status, note || user_details) global $PARAMS; $OUT = array(false, ""); if ($DATA == null) { $DATA = $PARAMS; } if (empty($DATA['UserID']) && empty($DATA['EMail']) && empty($DATA['ScreenName'])) { $OUT[1] = "Authentication: Provide either UserID, EMail, or ScreenName"; } list($msg, $ReturnedUsers) = _VIEW("users", array("UserID" => $DATA['UserID'], "ScreenName" => $DATA['ScreenName'], "EMail" => $DATA['EMail'])); if ($msg != true) { $OUT[1] = "Authentication: " . $ReturnedUsers; } else { if (count($ReturnedUsers) != 1) { $OUT[1] = "Authentication: User not found"; } elseif (!empty($DATA['Hash'])) { if (empty($DATA['Rand'])) { $OUT[1] = "Authentication: Rand is required"; } else { $LocalHash = md5($ReturnedUsers[0]['UserID'] . $ReturnedUsers[0]['Password'] . $DATA['Rand']); if ($DATA['Hash'] == $LocalHash) { $OUT = true; } else { $OUT[1] = "Authentication: Hash does not match expected"; } } } elseif (!empty($DATA['Password']) || !empty($DATA['PasswordHash'])) { if (empty($DATA['PasswordHash'])) { $DATA['PasswordHash'] = md5($DATA['Password'] . $ReturnedUsers[0]['Salt']); } if ($DATA['PasswordHash'] == $ReturnedUsers[0]['PasswordHash']) { $OUT[0] = true; $OUT[1] = $ReturnedUsers[0]; } else { $OUT[1] = "Authentication: Password or PasswordHash does not match"; } } else { $OUT[1] = "Authentication: Send either Hash [ md5(UserID.Password.Rand) ], Password, or PasswordHash "; } } return $OUT; }
***********************************************/ if ($ERROR == 100) { if (empty($PARAMS["LobbyKey"])) { $ERROR = "Please Provide a LobbyKey"; } } if ($ERROR == 100) { if (empty($PARAMS["Speaker"])) { $ERROR = "Please Provide a Speaker. Who are you?"; } } if ($ERROR == 100) { if (empty($PARAMS["Message"])) { $ERROR = "Please Provide a Message"; } } if ($ERROR == 100) { $resp = _VIEW("lobbies", array("LobbyKey" => $PARAMS['LobbyKey'])); if ($resp[0] == false) { $ERROR = $resp[1]; } if (count($resp[1]) != 1) { $ERROR = "That Lobby cannot be found"; } else { $LobbyID = $resp[1][0]["LobbyID"]; } } if ($ERROR == 100) { $resp = _ADD("messages", array("LobbyID" => $LobbyID, "Speaker" => $PARAMS["Speaker"], "Message" => $PARAMS["Message"])); $OUTPUT["MessageID"] = $resp["1"]["MessageID"]; }
function _EDIT($Table, $VARS = null) { global $TABLES, $DBOBJ, $Connection, $PARAMS; if ($VARS == null) { $VARS = $PARAMS; } $UniqueVars = _getUniqueTableVars($Table); $RequiredVars = _getRequiredTableVars($Table); $attrs = array(); $Status = $DBOBJ->GetStatus(); if ($Status !== true) { return array(false, $Status); } $MongoDB = $DBOBJ->GetMongoDB(); $Collection = $MongoDB->{$Table}; $resp = _VIEW($Table, $VARS); if ($resp[0] == false) { return array(false, $resp[1]); } if (count($resp[1]) > 1) { return array(false, "You need to supply the META KEY for this table, " . $TABLES[$Table]['META']['KEY']); } if (count($resp[1]) == 0) { $msg = "You have supplied none of the required parameters to make this edit. At least one of the following is required: "; foreach ($UniqueVars as $var) { $msg .= $var . " "; } return array(false, $msg); } if ($VARS[$TABLES[$Table]['META']['KEY']] == "") { $VARS[$TABLES[$Table]['META']['KEY']] = $resp[1][0][$TABLES[$Table]['META']['KEY']]; } $current_values = $resp[1][0]; $new_data = false; foreach ($VARS as $var => $val) { if ($var != $TABLES[$Table]['META']['KEY']) { if (in_array($var, $UniqueVars) && strlen($val) > 0 && $val != $current_values[$var]) { $count = $Collection->count(array($var => $val)); if ($count > 0) { return array(false, "There is already an entry of '" . $val . "' for " . $var); } else { $attrs[$var] = $val; } } elseif (strlen($val) > 0) { $attrs[$var] = $val; } if ($attrs[$var] != $current_values[$var] && $var != $TABLES[$Table]['META']['KEY']) { $new_data = true; } } } // fill in old values foreach ($current_values as $var => $val) { if (empty($attrs[$var])) { if (is_object($val) == false) { $attrs[$var] = $val; } } } if (count($attrs) > 0 && $new_data) { $MongoId = new MongoID($VARS[$TABLES[$Table]['META']['KEY']]); $resp = $Collection->update(array("_id" => $MongoId), $attrs); if ($resp === true) { return _VIEW($Table, $VARS); // do a view again to return fresh data } else { return array(false, $Status); } } else { return array(false, "There is nothing to change"); } }
public function VIEW() { $resp = _VIEW($this->DaveTableObject->table(), $this->DATA); return $resp[1][0]; }