From dda7884ace3a3906b65668669fb67b37f596fd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 19 Jan 2015 11:29:25 +0100 Subject: pagination with composer and move FlattrItem --- inc/3rdparty/FlattrItem.class.php | 57 ------- inc/3rdparty/Session.class.php | 346 -------------------------------------- inc/3rdparty/paginator.php | 202 ---------------------- inc/poche/FlattrItem.class.php | 57 +++++++ inc/poche/global.inc.php | 2 +- 5 files changed, 58 insertions(+), 606 deletions(-) delete mode 100644 inc/3rdparty/FlattrItem.class.php delete mode 100644 inc/3rdparty/Session.class.php delete mode 100644 inc/3rdparty/paginator.php create mode 100644 inc/poche/FlattrItem.class.php (limited to 'inc') diff --git a/inc/3rdparty/FlattrItem.class.php b/inc/3rdparty/FlattrItem.class.php deleted file mode 100644 index ef8c62f7..00000000 --- a/inc/3rdparty/FlattrItem.class.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @copyright 2013 - * @license http://opensource.org/licenses/MIT see COPYING file - */ - -class FlattrItem -{ - public $status; - public $urlToFlattr; - public $flattrItemURL; - public $numFlattrs; - - public function checkItem($urlToFlattr, $id) - { - $this->_cacheFlattrFile($urlToFlattr, $id); - $flattrResponse = file_get_contents(CACHE . "/flattr/".$id.".cache"); - if($flattrResponse != FALSE) { - $result = json_decode($flattrResponse); - if (isset($result->message)) { - if ($result->message == "flattrable") { - $this->status = FLATTRABLE; - } - } - elseif (is_object($result) && $result->link) { - $this->status = FLATTRED; - $this->flattrItemURL = $result->link; - $this->numFlattrs = $result->flattrs; - } - else { - $this->status = NOT_FLATTRABLE; - } - } - else { - $this->status = "FLATTR_ERR_CONNECTION"; - } - } - - private function _cacheFlattrFile($urlToFlattr, $id) - { - if (!is_dir(CACHE . '/flattr')) { - mkdir(CACHE . '/flattr', 0777); - } - - // if a cache flattr file for this url already exists and it's been less than one day than it have been updated, see in /cache - if ((!file_exists(CACHE . "/flattr/".$id.".cache")) || (time() - filemtime(CACHE . "/flattr/".$id.".cache") > 86400)) { - $askForFlattr = Tools::getFile(FLATTR_API . $urlToFlattr); - $flattrCacheFile = fopen(CACHE . "/flattr/".$id.".cache", 'w+'); - fwrite($flattrCacheFile, $askForFlattr); - fclose($flattrCacheFile); - } - } -} diff --git a/inc/3rdparty/Session.class.php b/inc/3rdparty/Session.class.php deleted file mode 100644 index b56e4c54..00000000 --- a/inc/3rdparty/Session.class.php +++ /dev/null @@ -1,346 +0,0 @@ - $value) { - $_SESSION[$key] = $value; - } - - return true; - } - self::banLoginFailed(); - } - - self::init(); - return false; - } - - /** - * Unset SESSION variable to force logout - */ - public static function logout() - { - // unset($_SESSION['uid'],$_SESSION['ip'],$_SESSION['expires_on'],$_SESSION['tokens'], $_SESSION['login'], $_SESSION['pass'], $_SESSION['longlastingsession'], $_SESSION['poche_user']); - - // Destruction du cookie (le code peut paraître complexe mais c'est pour être certain de reprendre les mêmes paramètres) - $args = array_merge(array(session_name(), ''), array_values(session_get_cookie_params())); - $args[2] = time() - 3600; - call_user_func_array('setcookie', $args); - // Suppression physique de la session - session_destroy(); - } - - /** - * Make sure user is logged in. - * - * @return true|false True if user is logged in, false otherwise - */ - public static function isLogged() - { - if (!isset ($_SESSION['uid']) - || (self::$disableSessionProtection === false - && $_SESSION['ip'] !== self::_allIPs()) - || time() >= $_SESSION['expires_on']) { - //self::logout(); - - return false; - } - // User accessed a page : Update his/her session expiration date. - $_SESSION['expires_on'] = time() + self::$inactivityTimeout; - if (!empty($_SESSION['longlastingsession'])) { - $_SESSION['expires_on'] += $_SESSION['longlastingsession']; - } - - return true; - } - - /** - * Create a token, store it in SESSION and return it - * - * @param string $salt to prevent birthday attack - * - * @return string Token created - */ - public static function getToken($salt = '') - { - if (!isset($_SESSION['tokens'])) { - $_SESSION['tokens']=array(); - } - // We generate a random string and store it on the server side. - $rnd = sha1(uniqid('', true).'_'.mt_rand().$salt); - $_SESSION['tokens'][$rnd]=1; - - return $rnd; - } - - /** - * Tells if a token is ok. Using this function will destroy the token. - * - * @param string $token Token to test - * - * @return true|false True if token is correct, false otherwise - */ - public static function isToken($token) - { - if (isset($_SESSION['tokens'][$token])) { - unset($_SESSION['tokens'][$token]); // Token is used: destroy it. - - return true; // Token is ok. - } - - return false; // Wrong token, or already used. - } - - /** - * Signal a failed login. Will ban the IP if too many failures: - */ - public static function banLoginFailed() - { - if (self::$banFile !== '') { - $ip = $_SERVER["REMOTE_ADDR"]; - $gb = $GLOBALS['IPBANS']; - - if (!isset($gb['FAILURES'][$ip])) { - $gb['FAILURES'][$ip] = 0; - } - $gb['FAILURES'][$ip]++; - if ($gb['FAILURES'][$ip] > (self::$banAfter - 1)) { - $gb['BANS'][$ip]= time() + self::$banDuration; - } - - $GLOBALS['IPBANS'] = $gb; - file_put_contents(self::$banFile, ""); - } - } - - /** - * Signals a successful login. Resets failed login counter. - */ - public static function banLoginOk() - { - if (self::$banFile !== '') { - $ip = $_SERVER["REMOTE_ADDR"]; - $gb = $GLOBALS['IPBANS']; - unset($gb['FAILURES'][$ip]); unset($gb['BANS'][$ip]); - $GLOBALS['IPBANS'] = $gb; - file_put_contents(self::$banFile, ""); - } - } - - /** - * Ban init - */ - public static function banInit() - { - if (self::$banFile !== '') { - if (!is_file(self::$banFile)) { - file_put_contents(self::$banFile, "array(), 'BANS'=>array()), true).";\n?>"); - } - include self::$banFile; - } - } - - /** - * Checks if the user CAN login. If 'true', the user can try to login. - * - * @return boolean true if user is banned, false otherwise - */ - public static function banCanLogin() - { - if (self::$banFile !== '') { - $ip = $_SERVER["REMOTE_ADDR"]; - $gb = $GLOBALS['IPBANS']; - if (isset($gb['BANS'][$ip])) { - // User is banned. Check if the ban has expired: - if ($gb['BANS'][$ip] <= time()) { - // Ban expired, user can try to login again. - unset($gb['FAILURES'][$ip]); - unset($gb['BANS'][$ip]); - file_put_contents(self::$banFile, ""); - - return true; // Ban has expired, user can login. - } - - return false; // User is banned. - } - } - - return true; // User is not banned. - } - - - /** - * Tells if a param exists in session - * - * @param $name name of the param to test - * @return bool - */ - public static function isInSession($name) - { - return (isset($_SESSION[$name]) ? : FALSE); - } - - /** - * Returns param in session - * - * @param $name name of the param to return - * @return mixed param or null - */ - public static function getParam($name) - { - return (self::isInSession($name) ? $_SESSION[$name] : NULL); - } - - /** - * Store value in session - * - * @param $name name of the variable to store - * @param $value value to store - */ - public static function setParam($name, $value) - { - $_SESSION[$name] = $value; - } -} diff --git a/inc/3rdparty/paginator.php b/inc/3rdparty/paginator.php deleted file mode 100644 index 306756c0..00000000 --- a/inc/3rdparty/paginator.php +++ /dev/null @@ -1,202 +0,0 @@ -_instance = $instance; - $this->_perPage = $perPage; - $this->set_instance(); - } - - /** - * get_start - * - * creates the starting point for limiting the dataset - * @return numeric - */ - private function get_start(){ - return ($this->_page * $this->_perPage) - $this->_perPage; - } - - /** - * set_instance - * - * sets the instance parameter, if numeric value is 0 then set to 1 - * - * @var numeric - */ - private function set_instance(){ - $this->_page = (int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]); - $this->_page = ($this->_page == 0 ? 1 : $this->_page); - } - - /** - * set_total - * - * collect a numberic value and assigns it to the totalRows - * - * @var numeric - */ - public function set_total($_totalRows){ - $this->_totalRows = $_totalRows; - } - - /** - * get_limit - * - * returns the limit for the data source, calling the get_start method and passing in the number of items perp page - * - * @return string - */ - public function get_limit(){ - if (STORAGE == 'postgres') { - return "LIMIT ".$this->_perPage." OFFSET ".$this->get_start(); - } else { - return "LIMIT ".$this->get_start().",".$this->_perPage; - } - } - - /** - * page_links - * - * create the html links for navigating through the dataset - * - * @var sting $path optionally set the path for the link - * @var sting $ext optionally pass in extra parameters to the GET - * @return string returns the html menu - */ - public function page_links($path='?',$ext=null) - { - $adjacents = "2"; - $prev = $this->_page - 1; - $next = $this->_page + 1; - $lastpage = ceil($this->_totalRows/$this->_perPage); - $lpm1 = $lastpage - 1; - - $pagination = ""; - if($lastpage > 1) - { - $pagination .= "\n"; - } - - - return $pagination; - } -} diff --git a/inc/poche/FlattrItem.class.php b/inc/poche/FlattrItem.class.php new file mode 100644 index 00000000..ef8c62f7 --- /dev/null +++ b/inc/poche/FlattrItem.class.php @@ -0,0 +1,57 @@ + + * @copyright 2013 + * @license http://opensource.org/licenses/MIT see COPYING file + */ + +class FlattrItem +{ + public $status; + public $urlToFlattr; + public $flattrItemURL; + public $numFlattrs; + + public function checkItem($urlToFlattr, $id) + { + $this->_cacheFlattrFile($urlToFlattr, $id); + $flattrResponse = file_get_contents(CACHE . "/flattr/".$id.".cache"); + if($flattrResponse != FALSE) { + $result = json_decode($flattrResponse); + if (isset($result->message)) { + if ($result->message == "flattrable") { + $this->status = FLATTRABLE; + } + } + elseif (is_object($result) && $result->link) { + $this->status = FLATTRED; + $this->flattrItemURL = $result->link; + $this->numFlattrs = $result->flattrs; + } + else { + $this->status = NOT_FLATTRABLE; + } + } + else { + $this->status = "FLATTR_ERR_CONNECTION"; + } + } + + private function _cacheFlattrFile($urlToFlattr, $id) + { + if (!is_dir(CACHE . '/flattr')) { + mkdir(CACHE . '/flattr', 0777); + } + + // if a cache flattr file for this url already exists and it's been less than one day than it have been updated, see in /cache + if ((!file_exists(CACHE . "/flattr/".$id.".cache")) || (time() - filemtime(CACHE . "/flattr/".$id.".cache") > 86400)) { + $askForFlattr = Tools::getFile(FLATTR_API . $urlToFlattr); + $flattrCacheFile = fopen(CACHE . "/flattr/".$id.".cache", 'w+'); + fwrite($flattrCacheFile, $askForFlattr); + fclose($flattrCacheFile); + } + } +} diff --git a/inc/poche/global.inc.php b/inc/poche/global.inc.php index 4d119456..c17d54e6 100755 --- a/inc/poche/global.inc.php +++ b/inc/poche/global.inc.php @@ -29,7 +29,7 @@ require_once INCLUDES . '/3rdparty/paginator.php'; require_once INCLUDES . '/3rdparty/libraries/feedwriter/FeedItem.php'; require_once INCLUDES . '/3rdparty/libraries/feedwriter/FeedWriter.php'; -require_once INCLUDES . '/3rdparty/FlattrItem.class.php'; +require_once INCLUDES . '/poche/FlattrItem.class.php'; # epub library require_once INCLUDES . '/3rdparty/libraries/PHPePub/Logger.php'; -- cgit v1.2.3