chat = $chat; if ($state == 1) { // New application form echo RegForm::getRegForm(); // Show a clean reg form } elseif ($state == 2) { // Edit application form } elseif ($state == 3) { // Delete application form } elseif (isset($_REQUEST['reg_afm_form_sub'])) { // Save application (new / edited) if (is_numeric($_REQUEST['reg_afm_form_sub'])) { if ($this->saveForm($_REQUEST['reg_afm_form_sub'], $_REQUEST['reg_afm_form_name'], $_REQUEST['reg_afm_field'], $_REQUEST['reg_afm_sel'])) { echo $GLOBALS['strings']['registration']['RegForm']['form_saved']; } else { echo $GLOBALS['strings']['registration']['RegForm']['errors']['save_fail']; } } else { echo $GLOBALS['strings']['registration']['RegForm']['errors']['save_form_type_error']; } } else { // List application forms $this->showIndex(); } return true; } public function showIndex() { echo $GLOBALS['theme']['admin_chat']['registration']['list_top']; $forms = $this->getChatAppForms(); if (count($forms) < 1) { echo $GLOBALS['strings']['registration']['RegForm']['no_forms']; } else { $find = array( '{REG_AFM_LIST_NAME}', '{REG_AFM_LIST_CREATOR}', '{REG_AFM_LIST_DELETE}', '{REG_AFM_LIST_ID}' ); echo $GLOBALS['theme']['admin_chat']['registration']['list_app_head']; for ($i = 0;$i < count($forms);$i++) { $repl = array( $forms[$i]->name, $forms[$i]->creator, $GLOBALS['strings']['registration']['afm']['list_delete'], $forms[$i]->id ); echo str_replace($find, $repl, $GLOBALS['theme']['admin_chat']['registration']['list_app']); } } echo $GLOBALS['theme']['admin_chat']['registration']['list_bottom']; } private function getChatAppForms() { $chat = SQL_EscapeString($this->chat)+0; $rez = SQL_SingleQuery("SELECT R.id,R.name,R.fields,S.username FROM {$GLOBALS['sql_prefix']}application_forms R, {$GLOBALS['sql_prefix']}users S WHERE R.chat={$chat} AND R.creator=S.id"); $num = SQL_NumRows($rez); $forms = array(); // Array of RegFormInfo "structs" if ($num > 0) { for($i = 0; $i < $num;$i++) { $temp = SQL_FetchAssoc($rez, $i); $forms[$i] = new RegFormInfo(); $forms[$i]->id = $temp['id']; $forms[$i]->chat = $this->chat; $forms[$i]->name = $temp['name']; $forms[$i]->fields = $temp['fields']; $forms[$i]->creator = $temp['username']; } } SQL_FreeResult($rez); return $forms; } public static function getRegForm() { // TODO: Make this able to load a saved form if needed be, instad of just outputting a blank. return $GLOBALS['theme']['admin_chat']['registration']['new_top']. $GLOBALS['theme']['admin_chat']['registration']['new_note']. $GLOBALS['theme']['admin_chat']['registration']['new_base_form']. $GLOBALS['theme']['admin_chat']['registration']['new_bottom']; } private function saveForm($type, $name, $cFieldNames, $cFieldTypes) { // TODO: URGENT - Check how to escape malicious data. Using SQL_* escape functions for now. // TODO: URGENT - Is $_REQUEST['userid'] ALWAYS valid? if (($name == "") || (($type > 2) || ($type < 1))) return false; $fields = array(); for ($i = 0;$i < count($cFieldNames);$i++) { if ($cFieldNames[$i] == "") continue; $fields[][] = array($cFieldNames[$i], $cFieldTypes[$i]); } $name = SQL_EscapeOrNullString($name); $fields = SQL_EscapeOrNullString(serialize($fields)); $creator = SQL_EscapeOrNullInt($_REQUEST['userid']); if ($type == 1) { $result = SQL_SingleQuery("INSERT INTO {$GLOBALS['sql_prefix']}application_forms (chat, name, fields, creator) VALUES(".$this->chat.", {$name}, {$fields}, {$creator})"); } elseif ($type == 2){ // For updating a form instead of inserting a new. // TODO: Add this. } if (!$result) return false; return true; } }; class RegFormInfo { public $id; public $chat; public $name; public $fields; public $creator; }; ?>