- <?php
- /* ***** BEGIN LICENSE BLOCK *****
- * __
- * ____ __ / /____ _________ _______ ______ _ _
- * / ____) \ / / ____) |___ ___| _____/ ____)| | | |
- * | (___ \ \_/ / (___ _____ | | | |___ | | | |___| |
- * \___ \ \ / \___ \|_____|| | | ___| | | | ___ |
- * ____) | | | ____) | | | | |_____| |____ | | | |
- * (_____/ |_| (_____/ |_| |_______\______)|_| |_|
- * Web, Médias et Technologies libres
- *
- * Copyright notice
- *
- * (c) 2009 Yugoo (SYS-TECH) <guillaume.coguiec@sys-tech.net>
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- *
- * ***** END LICENSE BLOCK ***** */
- /* #Requires / #Includes */
- require_once(PATH_tslib.'class.tslib_pibase.php');
- if (!class_exists('Dwoo')) require_once(t3lib_extMgm::extPath('dwoo').'lib/dwooAutoload.php');
- /**
- * Plugin 'st_feuser_dmail_unsubscribe' for the 'st_feuser_dmail_unsubscribe' extension.
- *
- * @author <st_yugoo>
- * @package TYPO3
- * @subpackage tx_stfeuserdmailunsubscribe
- */
- class tx_stfeuserdmailunsubscribe_pi1 extends tslib_pibase {
- public $prefixId = 'tx_stfeuserdmailunsubscribe_pi1'; // Same as class name
- public $scriptRelPath = 'pi1/class.tx_stfeuserdmailunsubscribe_pi1.php'; // Path to this script relative to the extension dir.
- public $extKey = 'st_feuser_dmail_unsubscribe'; // The extension key.
- public $pi_checkCHash = true;
- private $output = null;
- private $DB = null;
- private $TSFE = null;
- private $compiler = null;
- private $dwoo = null;
- public function __construct() {
- $this->ytools = new yTools();
- $this->ytools->invisibleErrors(); // error-reporting for developpers only
- $this->dwoo = new Dwoo();
- $this->compiler = new Dwoo_Compiler();
- }
- public function __destruct() { }
- public function main($content, $conf) {
- $this->conf = $conf;
- $this->pi_setPiVarDefaults();
- $this->pi_loadLL();
- $this->DB = &$GLOBALS['TYPO3_DB'];
- $this->TSFE = &$GLOBALS['TSFE'];
- $this->fe_user = &$GLOBALS['TSFE']->fe_user->user['uid'];
- $this->TSFE->no_cache = 1;
- // Si on ne peut récupérer le fe_user.uid on ne charge pas le pi.
- // TODO: gérer mieux la sécurité (typo3 fe_user sessions).
- if (!(int)$this->fe_user) return;
- // Association conf[] => $obj->attr
- $this->cset = Array(
- Array('template.', 'template'),
- );
- if ($this->TS2Attribute($this->conf, $this->cset)) {
- $this->initTemplate($this->template['file']);
- $this->CatArray = $this->getCategoriesInfo($this->fe_user);
- $this->buildTemplate();
- }
- return $this->pi_wrapInBaseClass($this->output);
- }
- /**
- * Supprime les catégories DirectMail d'un fe_user
- */
- private function unsetCategories($arr) {
- if (is_array($arr)) {
- $VALUES = $query = null;
- foreach ($arr as $k => $v) $VALUES[] = '('.$this->newID.','.$v.')';
- $query[] = 'UPDATE fe_users SET module_sys_dmail_category = 0 WHERE uid = '.$this->newID.' LIMIT 1';
- $query[] = 'INSERT INTO sys_dmail_feuser_category_mm (uid_local,uid_foreign) VALUES '.implode(',', $VALUES);
- foreach ($query as $a => $b) $res = $this->DB->sql_query($query[$a]);
- return $query;
- }
- return false;
- }
- /**
- * Le fe_user utilise-t'il les catégories ?
- */
- private function useDMAILCategories($user) {
- $query = 'SELECT module_sys_dmail_category FROM fe_users WHERE uid = '.$user;
- $res = $this->DB->sql_query($query);
- $row = $this->DB->sql_fetch_assoc($res);
- if (!(int)$row['module_sys_dmail_category']) return false;
- else return true;
- }
- /**
- * Produit un CatArray contenant les informations liées au catégories DMAIL d'un fe_user
- */
- private function getCategoriesInfo($user) {
- $usercat = $rootcat = $usersubscr = $tbl = null;
- $arr = $this->getTypoTemplate($this->getRoot($this->getAssocPID($user)));
- if (!$arr['tx_directmail.']['default.']['categories']) return false;
- $usersubscr = $this->useDMAILCategories($user);
- $usercat = $this->getUserCategories($user);
- $rootcat = explode(',', $arr['tx_directmail.']['default.']['categories']);
- if ($usersubscr && $usercat) {
- foreach ($rootcat as $i => $j) {
- if (($t = array_search($j, $usercat)) !== false) $tbl[] = $usercat[$t];
- }
- return array(
- 'rootcat' => $rootcat,
- 'usercat' => $tbl,
- );
- }
- else return array('rootcat' => $rootcat, 'usercat' => null);
- }
- /**
- * Retourne le nombre de catégories abonnées
- */
- private function getMyTotalCats($CatArray) {
- $nb = sizeof($CatArray['usercat']);
- if ($nb) return $nb;
- else return $this->LLStr('status.void');
- }
- /**
- * Récupère les catégories DMAIL liées à l'utilisateur
- */
- private function getUserCategories($user) {
- $query = 'SELECT uid_foreign FROM sys_dmail_feuser_category_mm WHERE uid_local = '.$user;
- $res = $this->DB->sql_query($query);
- while ($row = $this->DB->sql_fetch_assoc($res)) $tmp[] = $row['uid_foreign'];
- return $tmp;
- }
- /**
- * Initialise la template
- */
- private function initTemplate($tmpl) {
- $this->myTemplate = new Dwoo_Template_File($tmpl);
- }
- /**
- * Construit la template
- */
- private function buildTemplate() {
- $this->content = null;
- $data = array(
- 'legend' => $this->LLStr('legend'),
- 'form' => array(
- 'submit' => $this->LLStr('submit'),
- ),
- 'categories' => $this->genCheckboxArray($this->CatArray),
- 'total' => $this->getMyTotalCats($this->CatArray),
- 'status' => $this->LLStr('status'),
- 'subscribed' => $this->useDMAILCategories($this->fe_user),
- 'unsubscribed' => array(
- 'text' => $this->LLStr('unsubscribed.text'),
- 'option' => $this->LLStr('unsubscribed.option'),
- ),
- );
- $this->output .= $this->dwoo->get($this->template['file'], $data);
- }
- /**
- * Récupère le titre d'une catégorie
- */
- private function getCatname($id) {
- $res = $GLOBALS['TYPO3_DB']->sql_query('SELECT category FROM sys_dmail_category WHERE uid = '.$id);
- $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
- return $row['category'];
- }
- /**
- * Génère le tableau de configuration de la liste de checkboxes présente au sein du formulaire.
- */
- private function genCheckboxArray($CatArray) {
- $arr = null;
- foreach ($CatArray['rootcat'] as $a => $z) {
- if ($CatArray['usercat']) {
- if (array_search($z, $CatArray['usercat']) === false)
- $arr[] = array('name' => 'cat'.$z, 'id' => 'cat'.$z,'checked' => null, 'title' => $this->getCatname($z));
- else $arr[] = array('name' => 'cat'.$z, 'id' => 'cat'.$z,'checked' => 'checked', 'title' => $this->getCatname($z));
- }
- else $arr[] = array('name' => 'cat'.$z, 'id' => 'cat'.$z,'checked' => null, 'title' => $this->getCatname($z));
- }
- return $arr;
- }
- /**
- * Transforme les variables typoscript en attribut de classe
- */
- private function TS2Attribute($conf, $cset) {
- if (is_array($cset) && $conf) {
- foreach ($cset as $k) {
- if (array_key_exists(pos($k), $conf))
- (is_array(next($k))) ? $this->{pos(pos($k))}[next(pos($k))] = $conf[prev($k)] : $this->{pos($k)} = $conf[prev($k)];
- else return false;
- }
- return true;
- }
- return false;
- }
- /**
- * Retourne le chaine de caractère correspondante à la clef présente dans le localconf du plugin
- * TODO: faire en sorte que le path soit dynamique, on cut $scriptRelPath = 'pi1/class.tx_stfeuserdmailunsubscribe_pi1.php'
- * on récupère pixx et on fabrique extKey + pixxxx + locallang.xml
- */
- private function LLStr($str) { return $GLOBALS["TSFE"]->sL('LLL:EXT:'.$this->extKey.'/pi1/locallang.xml:'.$str); }
- /**
- * Récupère le root d'une arborescence
- */
- private function getRoot($id) {
- $sys_page = t3lib_div::makeInstance("t3lib_pageSelect");
- $arr = $sys_page->getRootLine($id);
- return $arr[0]['uid'];
- }
- /**
- * Récupère la template typoscript d'une page donnée
- */
- private function getTypoTemplate($id) {
- $q = $this->DB->exec_SELECTquery('config', 'sys_template', 'pid ='.$id);
- $res = $this->DB->sql_fetch_assoc($q);
- $tsparserObj = t3lib_div::makeInstance('t3lib_TSparser');
- $tsparserObj->parse($res['config']);
- return $tsparserObj->setup;
- }
- /**
- * Récupère le PID du sys-folder associé au fe_user
- */
- private function getAssocPID($feuser_id) {
- $res = $GLOBALS['TYPO3_DB']->sql_query('SELECT pid FROM fe_users WHERE uid = '.$feuser_id);
- $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
- return $row['pid'];
- }
- }
- /**
- * Yugoo's tools class
- */
- class yTools {
- /* $IP */
- public $ipArr = array('207.107.183.10' /* sys-tech */, '127.0.0.1');
- /**
- * dumpy - Vardump your variable if you're an admin.
- */
- public function dumpy(&$pVar) {
- if (!in_array($_SERVER['REMOTE_ADDR'], $this->ipArr)) return;
- var_dump($pVar);
- flush();
- }
- /**
- * invisibleErrors - Enable admin only error reporting.
- */
- public function invisibleErrors() {
- if (!in_array($_SERVER['REMOTE_ADDR'], $this->ipArr)) return;
- if (!ini_get('display_errors')) ini_set('display_errors', 1);
- error_reporting (E_ALL ^ E_NOTICE);
- }
- /**
- * customTrunc - truncate text as you wish.
- */
- public function customTrunc($string, $length = 20, $etc = '...', $break_words = false, $middle = false) {
- if ($length == 0) return '';
- if (strlen($string) > $length) {
- $length -= min($length, strlen($etc));
- if (!$break_words && !$middle)
- $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
- if(!$middle) return substr($string, 0, $length) . $etc;
- else return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
- }
- else return $string;
- }
- }
- if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/st_feuser_dmail_unsubscribe/pi1/class.tx_stfeuserdmailunsubscribe_pi1.php']) {
- include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/st_feuser_dmail_unsubscribe/pi1/class.tx_stfeuserdmailunsubscribe_pi1.php']);
- }
- ?>