From 55821e04c188997d258645975220828e195d0df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 5 Aug 2013 15:54:37 +0200 Subject: share email +twitter / class messages --- inc/3rdparty/class.messages.php | 231 ++++++++++++++++++++++++++++++++++++++++ inc/poche/Poche.class.php | 27 ++++- inc/poche/Tools.class.php | 11 ++ inc/poche/config.inc.php | 5 +- 4 files changed, 268 insertions(+), 6 deletions(-) create mode 100755 inc/3rdparty/class.messages.php (limited to 'inc') diff --git a/inc/3rdparty/class.messages.php b/inc/3rdparty/class.messages.php new file mode 100755 index 00000000..e60bd3a1 --- /dev/null +++ b/inc/3rdparty/class.messages.php @@ -0,0 +1,231 @@ +X\n%s\n"; + var $msgBefore = '

'; + var $msgAfter = "

\n"; + + + /** + * Constructor + * @author Mike Everhart + */ + public function __construct() { + + // Generate a unique ID for this user and session + $this->msgId = md5(uniqid()); + + // Create the session array if it doesnt already exist + if( !array_key_exists('flash_messages', $_SESSION) ) $_SESSION['flash_messages'] = array(); + + } + + /** + * Add a message to the queue + * + * @author Mike Everhart + * + * @param string $type The type of message to add + * @param string $message The message + * @param string $redirect_to (optional) If set, the user will be redirected to this URL + * @return bool + * + */ + public function add($type, $message, $redirect_to=null) { + + if( !isset($_SESSION['flash_messages']) ) return false; + + if( !isset($type) || !isset($message[0]) ) return false; + + // Replace any shorthand codes with their full version + if( strlen(trim($type)) == 1 ) { + $type = str_replace( array('h', 'i', 'w', 'e', 's'), array('help', 'info', 'warning', 'error', 'success'), $type ); + + // Backwards compatibility... + } elseif( $type == 'information' ) { + $type = 'info'; + } + + // Make sure it's a valid message type + if( !in_array($type, $this->msgTypes) ) die('"' . strip_tags($type) . '" is not a valid message type!' ); + + // If the session array doesn't exist, create it + if( !array_key_exists( $type, $_SESSION['flash_messages'] ) ) $_SESSION['flash_messages'][$type] = array(); + + $_SESSION['flash_messages'][$type][] = $message; + + if( !is_null($redirect_to) ) { + header("Location: $redirect_to"); + exit(); + } + + return true; + + } + + //----------------------------------------------------------------------------------------------- + // display() + // print queued messages to the screen + //----------------------------------------------------------------------------------------------- + /** + * Display the queued messages + * + * @author Mike Everhart + * + * @param string $type Which messages to display + * @param bool $print True = print the messages on the screen + * @return mixed + * + */ + public function display($type='all', $print=true) { + $messages = ''; + $data = ''; + + if( !isset($_SESSION['flash_messages']) ) return false; + + if( $type == 'g' || $type == 'growl' ) { + $this->displayGrowlMessages(); + return true; + } + + // Print a certain type of message? + if( in_array($type, $this->msgTypes) ) { + foreach( $_SESSION['flash_messages'][$type] as $msg ) { + $messages .= $this->msgBefore . $msg . $this->msgAfter; + } + + $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages); + + // Clear the viewed messages + $this->clear($type); + + // Print ALL queued messages + } elseif( $type == 'all' ) { + foreach( $_SESSION['flash_messages'] as $type => $msgArray ) { + $messages = ''; + foreach( $msgArray as $msg ) { + $messages .= $this->msgBefore . $msg . $this->msgAfter; + } + $data .= sprintf($this->msgWrapper, $this->msgClass, $type, $messages); + } + + // Clear ALL of the messages + $this->clear(); + + // Invalid Message Type? + } else { + return false; + } + + // Print everything to the screen or return the data + if( $print ) { + echo $data; + } else { + return $data; + } + } + + + /** + * Check to see if there are any queued error messages + * + * @author Mike Everhart + * + * @return bool true = There ARE error messages + * false = There are NOT any error messages + * + */ + public function hasErrors() { + return empty($_SESSION['flash_messages']['error']) ? false : true; + } + + /** + * Check to see if there are any ($type) messages queued + * + * @author Mike Everhart + * + * @param string $type The type of messages to check for + * @return bool + * + */ + public function hasMessages($type=null) { + if( !is_null($type) ) { + if( !empty($_SESSION['flash_messages'][$type]) ) return $_SESSION['flash_messages'][$type]; + } else { + foreach( $this->msgTypes as $type ) { + if( !empty($_SESSION['flash_messages']) ) return true; + } + } + return false; + } + + /** + * Clear messages from the session data + * + * @author Mike Everhart + * + * @param string $type The type of messages to clear + * @return bool + * + */ + public function clear($type='all') { + if( $type == 'all' ) { + unset($_SESSION['flash_messages']); + } else { + unset($_SESSION['flash_messages'][$type]); + } + return true; + } + + public function __toString() { return $this->hasMessages(); } + + public function __destruct() { + //$this->clear(); + } + + +} // end class +?> \ No newline at end of file diff --git a/inc/poche/Poche.class.php b/inc/poche/Poche.class.php index f9bcf85b..80bf6919 100644 --- a/inc/poche/Poche.class.php +++ b/inc/poche/Poche.class.php @@ -12,6 +12,7 @@ class Poche { public $store; public $tpl; + public $messages; function __construct($storage_type) { @@ -41,6 +42,9 @@ class Poche 'cache' => CACHE, )); $this->tpl->addExtension(new Twig_Extensions_Extension_I18n()); + # filter to display domain name of an url + $filter = new Twig_SimpleFilter('getDomain', 'Tools::getDomain'); + $this->tpl->addFilter($filter); Tools::initPhp(); Session::init(); @@ -113,10 +117,12 @@ class Poche case 'toggle_fav' : $this->store->favoriteById($id); Tools::logm('mark as favorite link #' . $id); + Tools::redirect(); break; case 'toggle_archive' : $this->store->archiveById($id); Tools::logm('archive link #' . $id); + Tools::redirect(); break; default: break; @@ -174,16 +180,21 @@ class Poche public function updatePassword() { - if (isset($_POST['password']) && isset($_POST['password_repeat'])) { - if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { - if (!MODE_DEMO) { + if (MODE_DEMO) { + $this->messages->add('i', 'in demo mode, you can\'t update your password'); + Tools::logm('in demo mode, you can\'t do this'); + } + else { + if (isset($_POST['password']) && isset($_POST['password_repeat'])) { + if ($_POST['password'] == $_POST['password_repeat'] && $_POST['password'] != "") { Tools::logm('password updated'); + $this->messages->add('s', 'your password has been updated'); $this->store->updatePassword(Tools::encodeString($_POST['password'] . $_SESSION['login'])); Session::logout(); Tools::redirect(); } else { - Tools::logm('in demo mode, you can\'t do this'); + $this->messages->add('e', 'the two fields have to be filled & the password must be the same in the two fields'); } } } @@ -194,7 +205,7 @@ class Poche if (!empty($_POST['login']) && !empty($_POST['password'])) { if (Session::login($_SESSION['login'], $_SESSION['pass'], $_POST['login'], Tools::encodeString($_POST['password'] . $_POST['login']))) { Tools::logm('login successful'); - + $this->messages->add('s', 'login successful, welcome to your poche'); if (!empty($_POST['longlastingsession'])) { $_SESSION['longlastingsession'] = 31536000; $_SESSION['expires_on'] = time() + $_SESSION['longlastingsession']; @@ -205,9 +216,11 @@ class Poche session_regenerate_id(true); Tools::redirect($referer); } + $this->messages->add('e', 'login failed, bad login or password'); Tools::logm('login failed'); Tools::redirect(); } else { + $this->messages->add('e', 'login failed, you have to fill all fields'); Tools::logm('login failed'); Tools::redirect(); } @@ -215,6 +228,7 @@ class Poche public function logout() { + $this->messages->add('s', 'logout successful, see you soon!'); Tools::logm('logout'); Session::logout(); Tools::redirect(); @@ -244,6 +258,7 @@ class Poche # the second
    is for read links $read = 1; } + $this->messages->add('s', 'import from instapaper completed'); Tools::logm('import from instapaper completed'); Tools::redirect(); } @@ -272,6 +287,7 @@ class Poche # the second