config = array(); } public function get_var($key) { if (!isset($this->config)) return null; $k = explode(".", $key); // split on dots return $this->do_get($this->config, $k, false); } public function get_var_all($key) { if (!isset($this->config)) return null; $k = explode(".", $key); // split on dots return $this->do_get($this->config, $k, true); } public function set_var($key, $value) { if (!isset($this->config)) // error recovery $this->config = array(); $k = explode(".", $key); // split on dots $this->config = $this->do_set($this->config, $k, $value); // do the set return TRUE; } private function do_set($target, $path, $value) { if (count($path) <= 0) { // we've reached our destination return $value; // return the value } $element = array_shift($path); // get the next element if (!isset($target)) $target = array(); $target[$element] = $this->do_set($target[$element], $path, $value); return $target; } private function do_get($target, $path, $return_all) { if (count($path) <= 0) { // we've reached our destination $to_ret = $target; /* check if we want to return everything or just the first element */ if (is_array($target) && $return_all == false) $to_ret = array_shift($target); if ($return_all == true && (!is_array($target) || $this->associative($to_ret))) $to_ret = array($to_ret); return $to_ret; // return the value } $element = array_shift($path); // get the next element if (!isset($target[$element])) return null; return $this->do_get($target[$element], $path, $return_all); } /* here we want to detect whether this is a file config or otherwise */ function connect($file) { $new_config = FileResource::connect($file); return $new_config; } protected function associative($array) { foreach($array as $key => $value) { if (!is_int($key)) return TRUE; } return FALSE; } } abstract class FileResource extends Resource { abstract function serialize(); protected $contents; protected $filename; function __construct($filename) { parent::__construct(); $this->filename = $filename; } function connect($file) { /* get the file extension */ $ext = str_replace('.','',strstr($file, '.')); /* check if the file is an XML file */ //if (strtolower($ext) == "xml") $config_obj = new XMLResource($file); /* otherwise use a plain text output */ // else // $new_config = new TextResource($file); return $config_obj; } function read_file() { $temp = file_get_contents($this->filename); if ($temp == FALSE) // file read error return $temp; $this->contents = $temp; return true; } function write_file() { $result = file_put_contents($this->filename, $this->contents); if ($result <= 0 && count($contents) > 0) // file write error return FALSE; return TRUE; // return to child class } function sync() { /* check to make sure we can sync */ // if (is_writeable($this->filename) == FALSE) { // return FALSE; // } /* make a serialization of this config */ $serial = $this->serialize(); if ($serial == FALSE) return FALSE; $old_contents = $this->contents; /* record old contents */ $this->contents = $serial; /* record this serialization */ $result = $this->write_file(); if ($result == FALSE) { // the write failed $this->contents = $old_contents; return FALSE; } return TRUE; } function is_synced() { /* make a serialization of this config */ $serial = $this->serialize(); /* grab the file contents */ $temp = file_get_contents($this->filename); if ($temp == $serial && $serial != FALSE) // if this didn't work out return TRUE; // success else // something went wrong return FALSE; } } class XMLResource extends FileResource { function serialize() { $dom = new DOMDocument('1.0'); // create a dom /* create the root config node */ $root = $dom->createElement('resource'); $root = $dom->appendChild($root); $this->arrayToDOM($this->config, $root, $dom); // turn config to dom $dom->formatOutput = true; // format output return $dom->saveXML(); // serialize the dom } function __construct($filename) { parent::__construct($filename); $this->initialize(); } function initialize() { /* we must be able to either read or write this file */ if (is_readable($this->filename) == FALSE && is_writeable($this->filename) == FALSE) { return FALSE; } $this->dom = new DOMDocument(); // create a DOM /* if we can read the DOM */ if (is_readable($this->filename)) { $this->read_file(); // read its contents $this->config = $this->parse_xml($this->contents); } } private function arrayToDOM($array, $node, $doc) { if (!is_array($array)) { $text = $doc->createTextnode($array); $node->appendChild($text); return; } foreach ($array as $key => $value) { if (is_array($value) && !$this->associative($value)) { foreach ($value as $vvalue) { $newNode = $doc->createElement($key); $newNode = $node->appendChild($newNode); $this->arrayToDOM($vvalue, $newNode, $doc); } } else { $newNode = $doc->createElement($key); $newNode = $node->appendChild($newNode); $this->arrayToDOM($value, $newNode, $doc); } } } private function parse_xml($xml) { $simplexml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); return $this->xml_to_array($simplexml); } private function xml_to_array($xml) { if ($xml instanceof SimpleXMLElement) { $children = $xml->children(); $return = null; } $arrayed = FALSE; foreach ($children as $element => $value) { if ($value instanceof SimpleXMLElement) { $values = (array)$value->children(); if (count($values) > 0) { if (!isset($return[$element])) { $return[$element] = $this->xml_to_array($value); } else { if ($arrayed == FALSE) { $arrayed = TRUE; $return[$element] = array($return[$element], $this->xml_to_array($value)); } else { $return[$element][] = $this->xml_to_array($value); } } } else { if (!isset($return[$element])) { $return[$element] = (string) $value; } else { if (!is_array($return[$element])) $return[$element] = array($return[$element], (string)$value); else $return[$element][] = (string)$value; } } } } if (is_array($return)) { return $return; } else { return false; } } } //$config = Resource::connect("my.xml"); //$config->set_var("birthday.month", "June"); //$config->set_var("birthday.year", "1983"); //$config->set_var("birthday.files.file", array("martha", "bobby", "billy")); //print_r($config->get_var("birthday.files.file")); //print_r($config->get_var("user")); //print($config->serialize()); class Error { public static $errors = array(); public function __construct($message) { Error::$errors[] = $message; } public static function clear() { Error::$errors = array(); } public static function getNext() { return array_pop(Error::$errors); } public static function hasMore() { if (count(Error::$errors) > 0) return true; else return false; } } class Args { public function get($name) { global $argv; global $argc; if (isset($_POST[$name])) { return $_POST[$name]; } else if (isset($_GET[$name])) { return $_GET[$name]; } else { for ($i = 1; $i < $argc; $i++) { $arg = $argv[$i]; $splitted = explode("=", $arg); if ($splitted[0] == $name && count($splitted) > 1) return $splitted[1]; } } return null; } } class Users { public $userdb; public $config; public function __construct($user_file, $config_file) { if (is_a($config_file, 'Resource')) $this->config = $config_file; else $this->config = Resource::connect($config_file); if (is_a($user_file, 'Resource')) $this->userdb = $user_file; else $this->userdb = Resource::connect($user_file); } public function register($username, $password, $email) { $data['username'] = $username; $data['password'] = $password; $data['email'] = $email; return $this->add_user($data); } public function unregister($username, $password, $email) { $data['username'] = $username; $data['password'] = $password; $data['email'] = $email; return $this->remove_user($data); } public function login($username, $password) { $data['username'] = $username; $data['password'] = $password; if ($this->login_correct($data)) { $_SESSION['username'] = $data['username']; $_SESSION['password'] = $data['password']; return TRUE; } new Error($this->config->get_var("strings.incorrect_login_error")); return FALSE; } public function is_logged_in() { if (isset($_SESSION['username']) && isset($_SESSION['password'])) return TRUE; else return FALSE; } public function change_password($username, $password, $new_password) { $data['username'] = $username; $data['password'] = $password; $data['new_password'] = $new_password; if ($this->login_correct($data)) { return $this->save_new_password($data); } else { new Error($this->config->get_var("strings.incorrect_login_error")); return FALSE; } return TRUE; } public function logout() { unset($_SESSION['username']); unset($_SESSION['password']); return TRUE; } public function recover_password($username, $email) { $subject = $this->config->get_var("mailer.recover_subject"); $body = $this->config->get_var("mailer.recover_message"); $users = $this->userdb->get_var_all("users.user"); foreach($users as $user) { if ( (!isset($username) || $username == $user['username']) && (!isset($email) || $email == $user['email']) ) { /* create the message body */ $mail['body'] = $body; $mail['subject'] = $subject; $mail['body'] = str_replace('{$username}', $user['username'], $mail['body']); $mail['body'] = str_replace('{$password}', $user['password'], $mail['body']); return $this->mail($user, $mail); // mailing the user } } new Error($this->config->get_var("strings.mail_error")); return FALSE; // something went wrong } public function make_changes() { $this->userdb->sync(); } public function add_user($userData) { $users = $this->userdb->get_var_all("users.user"); if (!isset($users)) // prepare the array if no users exist $users = array(); if ($this->user_exists($userData['username'])) { new Error($this->config->get_var("strings.username_taken")); return FALSE; } $users[] = $userData; $this->userdb->set_var("users.user" , $users); return TRUE; } public function user_exists($username) { $users = $this->userdb->get_var_all("users.user"); foreach($users as $user) // check if this username is taken if ($user['username'] == $username) return TRUE; return FALSE; } public function remove_user($userData) { $users = $this->userdb->get_var_all("users.user"); if (!isset($users)) // prepare the array if no users exist $users = array(); foreach($users as $key => $user) { // check if this username is taken if ($user['username'] == $userData['username']) { array_splice($users, $key, 1); $this->userdb->set_var("users.user" , $users); return TRUE; } } return FALSE; } public function login_correct($userData) { if (!isset($userData['username']) || !isset($userData['password'])) return FALSE; $users = $this->userdb->get_var_all("users.user"); if (!isset($users)) // prepare the array if no users exist return FALSE; foreach($users as $user) { // check if username/password are correct if ( ($user['username'] == $userData['username']) && ($user['password'] == $userData['password'])) { return TRUE; } } return FALSE; } public function mail($userData, $mail) { $smtp_host = $this->config->get_var("mailer.host"); $from = $this->config->get_var("mailer.from_address"); $from_name = $this->config->get_var("mailer.from_name"); $mailer = new PHPMailer(); $mailer->IsMail(); $mailer->Host = $smtp_host; $mailer->AddAddress($userData["email"]); $mailer->From = $from; $mailer->FromName = $from_name; $mailer->Mailer = "smtp"; $mailer->Subject = $mail['subject']; $mailer->Body = $mail['body']; mail($userData["email"], $mail['subject'], $mail['body'], "From: " . $mail['email']); if (!$mailer->Send()) { new Error($this->config->get_var("strings.mail_error")); return FALSE; } return TRUE; } public function save_new_password($userData) { $users = $this->userdb->get_var_all("users.user"); if (!isset($users)) // prepare the array if no users exist return FALSE; foreach($users as $key => $user) { // find the user if ( ($user['username'] == $userData['username']) && ($user['password'] == $userData['password'])) { if (!$this->validate_password($userData['new_password'])) return FALSE; $users[$key]['password'] = $userData['new_password']; $this->userdb->set_var("users.user", $users); return TRUE; } } new Error($this->config->get_var("strings.incorrect_login_error")); return FALSE; } public function set_property($puser, $prop, $value) { $users = $this->userdb->get_var_all("users.user"); if (!isset($users)) // prepare the array if no users exist return FALSE; foreach($users as $key => $user) { // find the user if ($user['username'] == $puser) { $users[$key][$prop] = $value; $this->userdb->set_var("users.user", $users); return TRUE; } } return FALSE; } public function get_property($puser, $prop) { $users = $this->userdb->get_var_all("users.user"); if (!isset($users)) // prepare the array if no users exist return FALSE; foreach($users as $key => $user) { // find the user if ($user['username'] == $puser) { return $user[$prop]; } } return FALSE; } private function validate_password($password) { $mpasslen = $this->config->get_var("password.minimum_length"); if (!isset($mpasslen)) $mpasslen = 0; if (!isset($password)) { new Error($config->this->get_var("strings.no_password")); return FALSE; } else if (strlen($password) < $mpasslen) { $string = $this->config->get_var("strings.password_length"); $string = str_replace('{$length}', $mpasslen, $string); new Error($string); return FALSE; } else { return TRUE; } } } function read_form_file($form_file) { global $form_items; $xml = simplexml_load_file($form_file); $form_items = array(); foreach ($xml->screen as $screen) { $new_screen = array(); foreach ($screen->form_item as $item) { $new_item = array('name' => (string) $item->name, 'type' => (string) $item->type, 'required' => (string) $item->required == "Yes" ? true:false, 'description' => (string) $item->label, 'options' => array()); foreach ($item->value as $value) { if (!isset($value) || $value == "") continue; $new_item['options'][] = array('value' => (string) $value, 'description' => (string) $value); } $new_screen[] = $new_item; } $form_items[] = $new_screen; } return $form_items; } /* gets the path of this user's listing */ function get_path($id) { global $config; $cascade_user = $config->get_var("login.user"); $cascade_passwd = $config->get_var("login.password"); $cascade_server = $config->get_var("login.server"); $params = array ( 'authentication' => array( 'username' => $cascade_user, 'password' => $cascade_passwd ), 'identifier' => array( 'id' => $id, 'path' => "", 'type' => "page")); $client = new SoapClient($cascade_server . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); try { $response = $client->read($params); } catch (Exception $e) { $mesg = $config->get_var("strings.read_error"); new Error($mesg); return FALSE; } if ($response->readReturn->success == true) return $response->readReturn->page->path; return null; } function get_id_from_path($path) { global $config; $cascade_user = $config->get_var("login.user"); $cascade_passwd = $config->get_var("login.password"); $cascade_server = $config->get_var("login.server"); $params = array ( 'authentication' => array( 'username' => $cascade_user, 'password' => $cascade_passwd ), 'identifier' => array( 'path' => $path, 'type' => "page")); $client = new SoapClient($cascade_server . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); try { $response = $client->read($params); } catch (Exception $e) { $mesg = $config->get_var("strings.add_error"); new Error($mesg); return FALSE; } if (!isset($response) || $response->readReturn->success == "false") { $mesg = $config->get_var("strings.add_error"); new Error($mesg); return FALSE; } return $response->readReturn->page->id; } function get_listing_from_server($id) { global $config; $cascade_user = $config->get_var("login.user"); $cascade_passwd = $config->get_var("login.password"); $cascade_server = $config->get_var("login.server"); $params = array ( 'authentication' => array( 'username' => $cascade_user, 'password' => $cascade_passwd ), 'identifier' => array( 'id' => $id, //'path' => "", 'type' => "page")); $client = new SoapClient($cascade_server . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); try { $response = $client->read($params); } catch (Exception $e) { $mesg = $config->get_var("strings.read_error"); new Error($mesg); return FALSE; } $xml = $client->__getLastResponse(); preg_match("&.*&is", $xml, $matches); $sxml = new SimpleXMLElement($matches[0]); foreach($sxml->structuredDataNodes->structuredDataNodes as $node) { $text = preg_replace("/^(.*)<\/system-xml>$/is", "$1", (string)$node->text); $results[(string)$node->identifier] = $text; } return $results; } function XMLToArray($xml) { if ($xml instanceof SimpleXMLElement) { $children = $xml->children(); $return = null; } foreach ($children as $element => $value) { if ($value instanceof SimpleXMLElement) { $values = (array)$value->children(); if (count($values) > 0) { $return[$element] = XMLToArray($value); } else { if (!isset($return[$element])) { $return[$element] = (string)$value; } else { if (!is_array($return[$element])) { $return[$element] = array($return[$element], (string)$value); } else { $return[$element][] = (string)$value; } } } } } if (is_array($return)) { return $return; } else { return $false; } } function edit_listing($path) { global $config; global $form_items; $data_definition = $config->get_var("cms.data_definition"); $configuration_set = $config->get_var("cms.configuration_set"); $metadata_set = $config->get_var("cms.metadata_set"); $cascade_user = $config->get_var("login.user"); $cascade_passwd = $config->get_var("login.password"); $cascade_server = $config->get_var("login.server"); $name_after = $config->get_var("cms.name_listing_after"); $pagename = basename($path); $parentdir = dirname($path); $params = array ( 'authentication' => array( 'username' => $cascade_user, 'password' => $cascade_passwd ), 'asset' => array( 'page' => array( //'id' => "", 'path' => $path, 'name' => $pagename, 'parentFolderPath' => $parentdir, 'metadataSetPath' => $metadata_set, 'configurationSetPath' => $configuration_set, 'metadata' => array( 'displayName' => $_SESSION['edit'][$name_after], 'title' => $_SESSION['edit'][$name_after] ), 'structuredData' => array( 'definitionPath' => $data_definition, 'structuredDataNodes' => array( 'structuredDataNode' => array()))))); foreach ($form_items as $screen) { foreach ($screen as $item) { $name = $item['name']; $value = $_SESSION['edit'][$name]; if (($item['type'] == 'radio' || $item['type'] == 'checkbox' || $item['type'] == 'select') && isset($value) && is_array($value)) { $value = implode("|", $value); } $params['asset']['page']['structuredData'] ['structuredDataNodes']['structuredDataNode'][] = array('type' => "text", 'identifier' => $name, 'text' => $value); } } $params['asset']['page']['structuredData'] ['structuredDataNodes']['structuredDataNode'][] = array('type' => "text", 'identifier' => "username", 'text' => $_SESSION['username']); $client = new SoapClient($cascade_server . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); try { $response = $client->edit($params); } catch (Exception $e) { $mesg = $config->get_var("strings.edit_error"); new Error($mesg); return FALSE; } if ($response->editReturn->success != "true") { $mesg = $config->get_var("strings.edit_error"); new Error($mesg); return FALSE; } return TRUE; } function create_listing() { global $config, $form_items; $review_path = $config->get_var("cms.review_path"); $data_definition = $config->get_var("cms.data_definition"); $configuration_set = $config->get_var("cms.configuration_set"); $metadata_set = $config->get_var("cms.metadata_set"); $cascade_user = $config->get_var("login.user"); $cascade_passwd = $config->get_var("login.password"); $cascade_server = $config->get_var("login.server"); $name_after = $config->get_var("cms.name_listing_after"); $pagename = $_SESSION['register'][$name_after] . "-" . date("U"); $pagename = str_replace("'", "-", $pagename); $pagename = str_replace(" ", "-", $pagename); $params = array ( 'authentication' => array( 'username' => $cascade_user, 'password' => $cascade_passwd ), 'asset' => array( 'page' => array( 'name' => $pagename, 'parentFolderPath' => $review_path, 'metadataSetPath' => $metadata_set, 'configurationSetPath' => $configuration_set, 'metadata' => array( 'displayName' => $_SESSION['register'][$name_after], 'title' => $_SESSION['register'][$name_after] ), 'structuredData' => array( 'definitionPath' => $data_definition, 'structuredDataNodes' => array())))); foreach ($form_items as $screen) { foreach ($screen as $item) { $name = $item['name']; $value = $_SESSION['register'][$name]; if (($item['type'] == 'radio' || $item['type'] == 'checkbox' || $item['type'] == 'select') && isset($value) && is_array($value)) { $value = implode("|", $value); } $params['asset']['page']['structuredData'] ['structuredDataNodes']['structuredDataNode'][] = array('type' => "text", 'identifier' => $name, 'text' => $value); } } $params['asset']['page']['structuredData']['structuredDataNodes']['structuredDataNode'][] = array('type' => "text", 'identifier' => "username", 'text' => $_SESSION['register']["__username"]); $soap_client = new SoapClient($cascade_server . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); try { $response = $soap_client->create($params); } catch (Exception $e) { $mesg = $config->get_var("strings.add_error"); new Error($mesg); return FALSE; } if ($response->createReturn->success != "true") { $mesg = $config->get_var("strings.add_error"); new Error($mesg); return FALSE; } return get_id_from_path($review_path . "/" . $pagename); } function username_and_password_valid() { global $config, $users; $username = Args::get("__username"); $password = Args::get("__password"); $cpassword = Args::get("__cpassword"); $email = Args::get("__email"); if (!isset($username) || $username == "") $mesg = $config->get_var("strings.no_username"); else if (!isset($password) || $password == "") $mesg = $config->get_var("strings.no_password"); else if (!isset($cpassword) || $cpassword == "") $mesg = $config->get_var("strings.no_confirm_password"); else if ($cpassword != $password) $mesg = $config->get_var("strings.password_mismatch"); else if (!isset($email) || $email == "") $mesg = $config->get_var("strings.no_email"); else if ($users->user_exists($username)) $mesg = $config->get_var("strings.username_taken"); else return TRUE; new Error($mesg); return FALSE; } function change_password_valid() { global $config; $username = $_SESSION["username"]; $password = Args::get("__password"); $new_password = Args::get("__new_password"); $cnew_password = Args::get("__cnew_password"); if (!isset($password) || $password == "") $mesg = $config->get_var("strings.no_password"); else if (!isset($new_password) || $new_password == "") $mesg = $config->get_var("strings.no_new_password"); else if (!isset($cnew_password) || $cnew_password == "") $mesg = $config->get_var("strings.no_confirm_password"); else if ($cnew_password != $new_password) $mesg = $config->get_var("strings.password_mismatch"); else return TRUE; new Error($mesg); return FALSE; } function has_required($screen) { global $form_items, $config; foreach ($form_items[$screen] as $item) { $value = $_POST[$item['name']]; if ($item['required'] && (!isset($value) || $value == "")) { $mesg = $config->get_var("strings.missing_required_field"); $mesg = str_replace('{$field-description}', $item['description'], $mesg); new Error($mesg); return FALSE; } } return TRUE; } function form($values, $screen) { global $users, $config; global $form_items; $me = $_SERVER['PHP_SELF']; $sum = ""; if (is_string($screen) && $screen == "summary") $sum = "_summary"; if (!$users->is_logged_in()) // user is registering $form = $config->get_var("templates.form.register" . $sum); else // user is editing $form = $config->get_var("templates.form.edit" . $sum); if (!isset($screen) || $screen < 0 || $screen >= count($form_items)) { $inputs = $config->get_var("templates.form.user_registration"); $items = array(); } else if (is_string($screen) && $screen == "summary") { $items = array(); foreach ($form_items as $a_screen) $items = array_merge($items, $a_screen); $inputs = ""; } else { $items = $form_items[$screen]; $inputs = ""; } /* add each field */ foreach ($items as $item) { if (is_string($screen) && $screen == "summary") $itext = $config->get_var("templates.form.form_item_summary"); else if ($item['required']) $itext = $config->get_var("templates.form.required_form_item"); else $itext = $config->get_var("templates.form.form_item"); /* prepare the options for this item, if they exist */ $options = ""; $input = ""; if (isset($values[$item['name']]) && !is_array($values[$item['name']])) $svals = explode("|", $values[$item['name']]); else if (!isset($values[$item['name']])) $svals = array(); else $svals = $values[$item['name']]; if ($item['type'] == "text") { $input = $config->get_var("templates.form.text_input"); } else if ($item['type'] == "textarea") { $input = $config->get_var("templates.form.textarea_input"); } else if ($item['type'] == "radio") { $input = $config->get_var("templates.form.radio_input"); foreach ($item['options'] as $option) { if (in_array($option['value'], $svals)) $option_t = $config->get_var("templates.form.checked_radio_option"); else $option_t = $config->get_var("templates.form.radio_option"); $option_t = str_replace('{$opt_value}', $option['value'], $option_t); $option_t = str_replace('{$opt_description}', $option['description'], $option_t); $options .= $option_t; } } else if ($item['type'] == "checkbox") { $input = $config->get_var("templates.form.checkbox_input"); foreach ($item['options'] as $option) { if (in_array($option['value'], $svals)) $option_t = $config->get_var("templates.form.checked_checkbox_option"); else $option_t = $config->get_var("templates.form.checkbox_option"); $option_t = str_replace('{$opt_value}', $option['value'], $option_t); $option_t = str_replace('{$opt_description}', $option['description'], $option_t); $options .= $option_t; } } else if ($item['type'] == "select") { $input = $config->get_var("templates.form.select_input"); foreach ($item['options'] as $option) { if (in_array($option['value'], $svals)) $option_t = $config->get_var("templates.form.checked_select_option"); else $option_t = $config->get_var("templates.form.select_option"); $option_t = str_replace('{$opt_value}', $option['value'], $option_t); $option_t = str_replace('{$opt_description}', $option['description'], $option_t); $options .= $option_t; } } $input = str_replace('{$options}', $options, $input); $itext = str_replace('{$input}', $input, $itext); $itext = str_replace('{$name}', $item['name'], $itext); if (isset($values[$item['name']])) if (is_array($values[$item['name']])) { $itext = str_replace('{$value}', implode(",", $values[$item['name']]), $itext); } else { $itext = str_replace('{$value}', $values[$item['name']], $itext); } else $itext = str_replace('{$value}', "", $itext); $itext = str_replace('{$description}', $item['description'], $itext); $inputs .= $itext; } $form = str_replace('{$form_items}', $inputs, $form); $form = str_replace('{$screen}', $screen, $form); $form = replace_template_keywords($form); return $form; } function menu() { global $users, $config; if ($users->is_logged_in()) $menu = $config->get_var("templates.menu.logged_in"); else $menu = $config->get_var("templates.menu.logged_out"); $menu = replace_template_keywords($menu); return $menu; } function replace_template_keywords($input) { global $users; $username = isset($_SESSION["username"]) ? $_SESSION["username"] : Args::get("__username"); $password = isset($_SESSION["password"]) ? $_SESSION["password"] : Args::get("__password"); $email = Args::get("__email"); if (!isset($email)) $email = $users->get_property($username, "email"); $input = str_replace('{$me}', $_SERVER['PHP_SELF'], $input); $input = str_replace('{$username}', $username, $input); $input = str_replace('{$password}', $password, $input); $input = str_replace('{$email}', $email, $input); return $input; } function handle_action() { global $config, $users; global $config_file, $users_file, $form_file, $form_items; $config = Resource::connect($config_file); $users = new Users($users_file, $config); read_form_file($form_file); $form_data = array(); $next_screen = -1; /* grab some form parameters */ $mode = Args::get("__action"); if (Args::get("submit") == "Restart") $mode = str_replace("do", "", $mode); $screen = Args::get("__screen"); if ($mode == "doregister") { $failed = false; /* this is the first screen */ if ($screen == -1 && username_and_password_valid()) { $_SESSION['register'] = $_POST; // add elements to session $next_screen = $screen + 1; $output = $config->get_var("templates.pages.register"); /* this is the summary screen */ } else if (is_string($screen) && $screen == "summary") { $username = $_SESSION['register']['__username']; $password = $_SESSION['register']['__password']; $email = $_SESSION['register']['__email']; if ($users->register($username, $password, $email) && ($id = create_listing())) { // put a listing in the cms $users->set_property($username, "pageid", $id); // set the user id $users->make_changes(); // commit the changes $output = $config->get_var("templates.pages.register_success"); } else { $failed = true; $users->unregister($username, $password, $email); } /* this is not the first (or pre-first) screen and it has required data */ } else if ($screen >= 0 && has_required($screen)) { // add elements to session $_SESSION['register'] = array_merge($_SESSION['register'], $_POST); $next_screen = $screen + 1; // increment screen $output = $config->get_var("templates.pages.register"); if (($screen == count($form_items) - 1)) { // last screen $next_screen = "summary"; // show the summary screen $form_data = $_SESSION['register']; } } else { $failed = true; } if ($failed == true) { $output = $config->get_var("templates.pages.register_failed"); $form_data = $_POST; $next_screen = $screen; } } else if ($mode == "doedit") { $screen = Args::get("__screen"); if (!has_required($screen)) { $form_data = $_POST; $next_screen = $screen; $output = $config->get_var("templates.pages.edit"); } else if (is_string($screen) && $screen == "summary") { if (($id = $users->get_property($_SESSION['username'], "pageid")) && ($path = get_path($id)) && (edit_listing($path))) { $output = $config->get_var("templates.pages.edit_success"); } else { new Error($config->get_var("strings.edit_error")); $form_data = $_POST; $output = $config->get_var("templates.pages.edit_failed"); $next_screen = $screen; } } else if ($screen >= 0) { if (!isset($_SESSION['edit'])) $_SESSION['edit'] = array(); $_SESSION['edit'] = array_merge($_SESSION['edit'], $_POST); $output = $config->get_var("templates.pages.edit"); $id = $users->get_property($_SESSION['username'], "pageid"); $next_screen = $screen + 1; if (!($listing = get_listing_from_server($id))) { $mesg = $config->get_var("strings.cannot_find"); new Error($mesg); $output = $config->get_var("templates.pages.logged_in"); } else { $form_data = $listing; $output = $config->get_var("templates.pages.edit"); } if (($screen == count($form_items) - 1)) { $next_screen = "summary"; $form_data = $_SESSION['edit']; } } } else if ($mode == "login") { $username = Args::get("__username"); $password = Args::get("__password"); if ($users->login($username, $password)) $output = $config->get_var("templates.pages.login_success"); else $output = $config->get_var("templates.pages.login_failure"); } else if ($mode == "logout") { $users->logout(); $output = $config->get_var("templates.pages.logout_success"); } else if ($mode == "recover") { $email = Args::get("__email"); if ($users->recover_password(null, $email)) $output = $config->get_var("templates.pages.recover_password_success"); else $output = $config->get_var("templates.pages.recover_password_failure"); } else if ($mode == "changepassword") { $username = $_SESSION["username"]; $password = Args::get("__password"); $new_password = Args::get("__new_password"); $cnew_password = Args::get("__cnew_password"); if (change_password_valid() && $users->change_password($username, $password, $new_password)) { $users->make_changes(); // commit the changes $output = $config->get_var("templates.pages.change_password_success"); } else { $output = $config->get_var("templates.pages.change_password_failure"); } } else { if (!$users->is_logged_in()) $output = $config->get_var("templates.pages.logged_in"); else $output = $config->get_var("templates.pages.logged_out"); } if ($mode == "edit") { if (!$users->is_logged_in()) { $mesg = $config->get_var("strings.need_login"); new Error($mesg); $output = $config->get_var("templates.pages.logged_out"); } else { $id = $users->get_property($_SESSION['username'], "pageid"); $next_screen = 0; if (!($listing = get_listing_from_server($id))) { $mesg = $config->get_var("strings.cannot_find"); new Error($mesg); $output = $config->get_var("templates.pages.logged_in"); } else { $form_data = $listing; $output = $config->get_var("templates.pages.edit"); } } } if ($mode == "register") { $next_screen = -1; $output = $config->get_var("templates.pages.register"); } $errors = ""; while (Error::hasMore()) { $case = $config->get_var("templates.error"); $case = str_replace('{$error}', Error::getNext(), $case); $errors .= $case; } $output = str_replace('{$errors}', $errors, $output); $output = str_replace('{$menu}', menu(), $output); $output = str_replace('{$form}', form($form_data, $next_screen), $output); $output = replace_template_keywords($output); print ($output); } handle_action(); ?>