'Admin panel module

#INCLUDE "modules.bi"

CONST AS STRING ModuleName = "admin"
DIM SHARED ModulePath AS STRING
ModulePath = "modules" + PATH_DELIMITER + ModuleName + PATH_DELIMITER

'arrayIndex = UBOUND(compiledModules) + 1
'REDIM PRESERVE compiledModules(arrayIndex)
'compiledModules(arrayIndex) = "admin"

'NOTE: These constants have the group number assigned to administrators, editors, etc.
'Groups can be modified by editing groups.txt in data folder, you can add as many groups as you need.
'Please adjust constant values according to the file.
CONST AS SHORT AdminGroup = 1, EditorGroup = 2

SUB LoadAdminPanel()
  DIM AS STRING Section, Action, FileName, Result
  DIM AS SHORT UserGroup

  'Is the user is logged in?
  IF IsAuth() THEN
    'Get the user group
    UserGroup = CINT(VAL(ReadUserData("user_group")))
    Section = ValidateChar(QueryString("section"))
    IF Section <> "" THEN
      SELECT CASE Section
        CASE "pages"
          DIM PageList AS STRING
          'Section only allowed to administrators
          IF UserGroup = AdminGroup THEN
            Action = ValidateChar(QueryString("action"))
            IF Action <> "" THEN
              DIM PageName AS STRING
              SELECT CASE Action
                CASE "edit"
                  DIM AS STRING CheckedStatus, LastModDate
                  SiteTitle = Language("module_admin_pages_editor")
                  FileName = ValidateChar(QueryString("pagename"))
                  IF FileName <> "" THEN
                    PageContent = ReadFile(DataPath + "pages" + PATH_DELIMITER + FileName + ".txt")
                    IF PageContent <> "" THEN
                      'The page exists
                      IF CINT(PageParser("page_status")) THEN
                        CheckedStatus = "checked=" + CHR(34) + "checked" + CHR(34)
                      END IF
                      LastModDate = SystemDate() + "-" + SystemTime()
                    END IF
                  END IF
                  Result = LoadTplFile(ModulePath, "pages-editor.html")
                  Result = Replace(Result, "checked_status", CheckedStatus)
                  Result = Replace(Result, "lastmod_date", LastModDate)
                  Result = Replace(Result, "raw_page_content", Replace(PageParser("page_content"), "<br />", CHR(10)))
                CASE "delete"
                  SiteTitle = Language("module_admin_pages_delete")
                  Result = LoadTplFile(ModulePath, "message.html")
                  PageName = ValidateChar(QueryString("pagename"))
                  IF PageName <> "" THEN
                    FileName = DataPath + "pages" + PATH_DELIMITER + PageName + ".txt"
                    IF FileExists(FileName) THEN
                      IF CINT(Post("page-delete")) THEN
                        'Deleting an existing page
                        DeleteFile(FileName)
                        'Remove the page name from the page list
                        PageList = ReadFile(TempPath + "pagelist.txt")
                        IF INSTR(PageList, PageName) > 0 THEN
                          PageList = Replace(PageList, PageName + LINE_ENDING, "")
                          WriteFile(TempPath + "pagelist.txt", PageList)
                        END IF
                        PageList = ""
                        Result = Replace(Result, "error_message", Language("success_page_deleted"))
                      ELSE
                        Result = LoadTplFile(ModulePath, "pages-delete.html")
                        Result = Replace(Result, "error_message", "")
                      END IF
                    ELSE
                      Result = Replace(Result, "error_message", Language("error_page_not_exists"))
                    END IF
                  ELSE
                    Result = Replace(Result, "error_message", Language("error_page_name_empty"))
                  END IF
                CASE "save"
                  SiteTitle = Language("module_admin_pages_editor")
                  Result = LoadTplFile(ModulePath, "message.html")
                  PageName = ValidateChar(Post("page-name"))
                  IF PageName = "" AND Post("page-title") <> "" THEN
                    'Page name field empty, create a new name based on the title
                    PageName = CreatePermalink(Post("page-title"))
                  END IF
                  IF PageName <> "" THEN
                    'Hopefully we have a page name now
                    FileName = DataPath + "pages" + PATH_DELIMITER + PageName + ".txt"
                    PageContent = ""
                    PageContent += "page_name=" + PageName + LINE_ENDING
                    PageContent += "page_title=" + Post("page-title") + LINE_ENDING
                    IF Post("page-date") <> "" THEN
                      PageContent += "page_date=" + Post("page-date") + LINE_ENDING
                    ELSE
                      PageContent += "page_date=" + SystemDate() + "-" + SystemTime() + LINE_ENDING
                    END IF
                    IF Post("page-lastmod") <> "" THEN
                      PageContent += "page_lastmod=" + Post("page-lastmod") + LINE_ENDING
                    ELSE
                      PageContent += "page_lastmod=" + SystemDate() + "-" + SystemTime() + LINE_ENDING
                    END IF
                    IF CINT(Post("page-status")) THEN
                      PageContent += "page_status=" + Post("page-status") + LINE_ENDING
                    ELSE
                      PageContent += "page_status=0" + LINE_ENDING
                    END IF
                    PageContent += "page_content=" + Replace(Post("page-content"), CHR(10), "<br />") + LINE_ENDING
                    WriteFile(FileName, PageContent)
                    Result = Replace(Result, "error_message", Language("success_page_saved"))
                    'Update page list
                    PageList = ReadFile(TempPath + "pagelist.txt")
                    IF INSTR(PageList, PageName) = 0 THEN
                      'Insert new page name at top of page list
                      PageList = PageName + LINE_ENDING + PageList
                      WriteFile(TempPath + "pagelist.txt", PageList)
                    END IF
                    PageList = ""
                  ELSE
                    'We have failed all the previous attempts to get a Page Name, so die with dignity
                    Result = Replace(Result, "error_message", Language("error_page_name_undefined"))
                  END IF
              END SELECT
            ELSE
              DIM AS STRING TempLine, ListBody
              DIM AS SHORT PageCount, StartPos, CurPos, EolPos
              SiteTitle = Language("module_admin_page_list")
              Result = LoadTplFile(ModulePath, "pages-list.html")
              'Initialize cursors
              PageCount = 20 'How many pages are listed
              StartPos = 1
              CurPos = 0
              EolPos = 1
              'Load page list
              PageList = ReadFile(TempPath + "pagelist.txt")
              DO UNTIL CurPos = PageCount OR EolPos = 0
                'Find the next line feed
                EolPos = INSTR(MID(PageList, StartPos), LINE_ENDING)
                'Read the file
                PageContent = ReadFile(DataPath + "pages" + PATH_DELIMITER + MID(PageList, StartPos, EolPos - 1) + ".txt")
                IF PageContent <> "" THEN
                  'The Page exists
                  ListBody += LoadTplFile(ModulePath, "pages-list-body.html")
                  CurPos += 1
                END IF
                'Place the pointer on the next line
                StartPos += EolPos
                IF LEN(LINE_ENDING) > 1 THEN
                  StartPos += 1
                END IF
              LOOP
              'Free memory
              PageList = ""
              IF ListBody = "" THEN
                Result = Replace(Result, "error_message", Language("error_page_list_empty"))
                Result = Replace(Result, "admin_page_list", "")
              ELSE
                Result = Replace(Result, "error_message", "")
                Result = Replace(Result, "admin_page_list", ListBody)
              END IF
            END IF
          ELSE
            'User is not admin
            Result = "403"
          END IF
        CASE "modules"
        CASE "templates"
        CASE "settings"
          'This section is only allowed to administrators
          IF UserGroup = AdminGroup THEN
            Action = ValidateChar(QueryString("action"))
            IF Action <> "" THEN
              SELECT CASE Action
                CASE "edit"
                  SiteTitle = "Settings Editor"
                  Result = LoadTplFile(ModulePath, "settings-editor.html")
                CASE "save"
                  IF Post("save_changes") <> "" THEN
                    DIM NewSettings AS STRING
                    NewSettings = "" + _
                    "site_title=" + Post("site_title") + LINE_ENDING + _
                    "site_description=" + Post("site_description") + LINE_ENDING + _
                    "site_url=" + Post("site_url") + LINE_ENDING + _
                    "site_admin=" + Post("site_admin") + LINE_ENDING + _
                    "site_email=" + Post("site_email") + LINE_ENDING + _
                    "site_encoding=" + Post("site_encoding") + LINE_ENDING + _
                    "site_template=" + Post("site_template") + LINE_ENDING + _
                    "site_language=" + Post("site_language") + LINE_ENDING + _
                    "site_timezone=" + Post("site_timezone") + LINE_ENDING + _
                    "site_index_type=" + Post("site_index_type") + LINE_ENDING + _
                    "site_index_page=" + Post("site_index_page") + LINE_ENDING + _
                    "site_index_module=" + Post("site_index_module") + LINE_ENDING + _
                    "site_fancy_url=" + Post("site_fancy_url") + LINE_ENDING + _
                    "site_fancy_url_extension=" + Post("site_fancy_url_extension") + LINE_ENDING + _
                    "site_cache_mode=" + Post("site_cache_mode") + LINE_ENDING + _
                    "site_uploads_path=" + Post("site_uploads_path") + LINE_ENDING + _
                    "site_smtp_server=" + Post("site_smtp_server") + LINE_ENDING
                    WriteFile(DataPath + "settings.txt", NewSettings)
                    NewSettings = ""
                    SiteTitle = Language("module_admin_settings_editor")
                    Result = LoadTplFile(ModulePath, "message.html")
                    Result = Replace(Result, "error_message", Language("success_settings_saved"))
                  END IF
              END SELECT
            ELSE
              SiteTitle = Language("module_admin_settings_list")
              Result = LoadTplFile(ModulePath, "settings-display.html")
            END IF
          ELSE
            'User is not admin
            Result = "403"
          END IF
        CASE ELSE
      END SELECT
    ELSE
      SiteTitle = "Admin Control Panel"
      Result = LoadTplFile(ModulePath, "panel.html")
    END IF
  ELSE
    'User is not logged in
    Result = "403"
  END IF
  SiteContent = Result
  Result = ""
END SUB
      