Buchempfehlung
MySQL kurz & gut
MySQL kurz & gut
Das preiswerte Taschen- buch stellt MySQL-rele- vante Inhalte systematisch und knapp dar, sodass es sich optimal zum Nach- schlagen beim Pro- grammieren eignet. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

fbpp-plugin.bas

Uploader:Redakteurnemored
Datum/Zeit:03.02.2013 21:20:39

' ##### DECLARATION FOR HEADER #####

#define FB3P_VERSION (0 shl 28) + (2 shl 24) + 130202
#define FB3P_URL "www.freebasic-portal.de"
enum FBPP_Error explicit
  noError = 0, blocked, noUser, wrongUser, noPassword, wrongPassword, noID, wrongID
  noAccess, listError, noCodes, wrongCommand, unknown
end enum
enum FBPP_Action
  id2url, download, delet, readcode, savecode, newcode
  check, list, listall, info, count
end enum
declare function fbpp_query(byref message as string, action as FBPP_Action, byref id as integer = 0, _
                            user as string = "", password as string = "", byref code as string = "", _
                            title as string = "", listlength as uinteger = 0, listoffset as uinteger = 0) as FBPP_Error




' ##### FB3P FUNCTTION #####

#include once "http.bi"
function fbpp_query(byref message as string, action as FBPP_Action, byref id as integer = 0, _
                    user as string = "", password as string = "", byref code as string = "", _
                    title as string = "", listlength as uinteger = 0, listoffset as uinteger = 0) as FBPP_Error
  ' action:   what to do (readcode, savecode ...)
  ' id:       ID of the entry on fb:porticula
  ' text:     code to transmit
  ' user:     user name on fb:porticula (for savecode etc.)
  ' password: hash of users password
  ' message:  message from fb:porticula
  message = ""
  dim as string m = ""

  ' handle missing arguments
  select case action
    case FBPP_Action.id2url, FBPP_Action.download
      if id = 0 then return FBPP_Error.noID
    case FBPP_Action.delet, FBPP_Action.readcode, FBPP_Action.savecode
      if id = 0 then return FBPP_Error.noID
      if user = "" then return FBPP_Error.noUser
      if password = "" then return FBPP_Error.noPassword
    case FBPP_Action.newcode, FBPP_Action.check
      if user = "" then return FBPP_Error.noID
      if password = "" then return FBPP_Error.noPassword
    case FBPP_Action.list
      if user = "" then return FBPP_Error.noUser
    case FBPP_Action.listall
      if listlength <= 0 then return FBPP_Error.listError
    case FBPP_Action.info, FBPP_Action.count
      ' nothing to do
    case else
      return FBPP_Error.wrongCommand
  end select

  ' get http query string
  dim as string queryGet, queryPost
  select case action
    case FBPP_Action.id2url
      queryGet = "porticula.php?action=id2url&id=" & id
    case FBPP_Action.download
      queryGet = "porticula.php?action=download&id=" & id
    case FBPP_Action.delet
      queryGet = "porticula.php?action=delete&id=" & id & "&username=" & URLEncode(user) & "&pwhash=" & password
    case FBPP_Action.readcode
      queryGet = "porticula.php?action=readcode&id=" & id & "&username=" & URLEncode(user) & "&pwhash=" & password
    case FBPP_Action.savecode
      queryGet = "porticula.php?action=savecode&id=" & id & "&username=" & URLEncode(user) & "&pwhash=" & password
      queryPost = "Titel=" & URLEncode(title) & "&Quelltext=" & URLEncode(code)
    case FBPP_Action.newcode
      queryGet = "porticula.php?action=newcode&username=" & URLEncode(user) & "&pwhash=" & password
      queryPost = "Titel=" & URLEncode(title) & "&Quelltext=" & URLEncode(code)
    case FBPP_Action.check
      queryGet = "porticula.php?action=check&username=" & URLEncode(user) & "&pwhash=" & password
    case FBPP_Action.list
      queryGet = "porticula.php?action=list&username=" & URLEncode(user) & "&pwhash=" & password
    case FBPP_Action.listall
      queryGet = "porticula.php?action=listall&n=" & listlength
      if listoffset > 0 then queryGet &= "&offset=" & listoffset
    case FBPP_Action.info
      queryGet = "porticula.php?action=info"
    case FBPP_Action.count
      queryGet = "porticula.php?action=count"
    case else
      return FBPP_Error.wrongCommand
  end select

  ' send query, evaluate
  dim answer as string, search as integer
  if queryPost <> "" then
    answer = httpPost(FB3P_URL, queryGet, queryPost)
    search = instr(answer, chr(13, 10, 13, 10))
    if answer = "BLOCKED" then return FBPP_Error.blocked
    m = mid(answer, search+4)
  else
    answer = httpGet(FB3P_URL, queryGet)
    search = instr(answer, chr(13, 10, 13, 10))
    answer = mid(answer, search+4)
    if answer = "BLOCKED" then return FBPP_Error.blocked
    if action = FBPP_Action.readcode then
      search = instr(answer, chr(10)) : search = instr(search+1, answer, chr(10))
      answer = left(answer, search) & user & chr(10) & mid(answer, search+1)
    end if
    ' replace "+" by " "
    for i as integer = 0 to len(answer) - 1
      if answer[i] = 43 then answer[i] = 32
    next
    ' decoding special characters
    do
      search = instr(answer, "%")
      if search = 0 then exit do
      answer = left(answer, search-1) & chr(valint("&h" & mid(answer, search+1, 2))) & mid(answer, search+3)
    loop
    m = answer
  end if

  ' error handling
  select case m
    case "ERROR: Username not found."
      return FBPP_Error.wrongUser
    case "ERROR: No codes found"
      return FBPP_Error.noCodes
    case "ERROR: Entry not found"
      return FBPP_Error.wrongID
    case "ERROR"
      select case action
        case FBPP_Action.check, FBPP_Action.list, FBPP_Action.listall, FBPP_Action.delet, FBPP_Action.savecode
          return FBPP_Error.wrongPassword
        case FBPP_Action.id2url
          return FBPP_Error.wrongID
        case else
          return FBPP_Error.unknown
      end select
    case else
      if left(m, len(user) + 1) = user & chr(10) then
        select case mid(m, len(user) + 2)
          case "ERROR: Entry not found"
            return FBPP_Error.wrongID
          case "ERROR: You have no permission to edit this entry."
            return FBPP_Error.noAccess
          case "ERROR"
            return FBPP_Error.wrongPassword
        end select
      end if
      if left(m, 5) = "ERROR" then return FBPP_Error.unknown
  end select
  if action = FBPP_Action.newcode then
    if left(m, 8) = "SUCCESS " then
      id = val(mid(m, 9))
    else
      id = 0
      return FBPP_Error.wrongPassword
    end if
  end if
  message = m
  return FBPP_Error.noError
end function




' ##### TEST (DO NOT INCLUDE) #####

#include once "md5.bi"
function translateError(e as FBPP_Error) as string
  select case e
    case FBPP_Error.noError       : return "ok"
    case FBPP_Error.blocked       : return "query was blocked"
    case FBPP_Error.noUser        : return "no user name specified"
    case FBPP_Error.wrongUser     : return "wrong user name"
    case FBPP_Error.noPassword    : return "no password specified"
    case FBPP_Error.wrongPassword : return "wrong password"
    case FBPP_Error.noID          : return "missing id"
    case FBPP_Error.wrongID       : return "wrong id"
    case FBPP_Error.noAccess      : return "permission denied"
    case FBPP_Error.listError     : return "wrong list count"
    case FBPP_Error.noCodes       : return "no codes found"
    case FBPP_Error.wrongCommand  : return "wrong command"
    case FBPP_Error.unknown       : return "unknown error"
  end select
end function

dim as string message, user, password, code, title, line1, line2
dim as integer id
' read own code
open "fbpp-plugin.bas" for input as #1
do until eof(1)
  line input #1, line1
  code &= line1 & chr(10)
loop
close #1
' read userdata
open "pw" for input as #1
input #1, line1, line2
close #1
user = mid(line1, 6)
password = mid(line2, 8)
title = __FILE__
' upload code
print "Return:  "; translateError(fbpp_query(message, FBPP_Action.newcode, id, user, password, code, title))
print "Message: "; message
print "New ID:  "; id