Guest

Untitled

By: a guest on Jan 28th, 2012  |  syntax: None  |  size: 11.77 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. <?php
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. *                 __
  4. *    ____ __     / /____    _________ _______ ______  _     _
  5. *   / ____) \   / / ____)  |___   ___|  _____/  ____)| |   | |
  6. *  | (___  \ \_/ / (___  _____ | |   | |___  | |     | |___| |
  7. *   \___ \  \   / \___ \|_____|| |   |  ___| | |     |  ___  |
  8. *   ____) |  | |  ____) |      | |   | |_____| |____ | |   | |
  9. *  (_____/   |_| (_____/       |_|   |_______\______)|_|   |_|
  10. *                          Web, Médias et Technologies libres  
  11. *
  12. *  Copyright notice
  13. *
  14. *  (c) 2009 Yugoo (SYS-TECH) <guillaume.coguiec@sys-tech.net>
  15. *  All rights reserved
  16. *
  17. *  This script is part of the TYPO3 project. The TYPO3 project is
  18. *  free software; you can redistribute it and/or modify
  19. *  it under the terms of the GNU General Public License as published by
  20. *  the Free Software Foundation; either version 2 of the License, or
  21. *  (at your option) any later version.
  22. *
  23. *  The GNU General Public License can be found at
  24. *  http://www.gnu.org/copyleft/gpl.html.
  25. *
  26. *  This script is distributed in the hope that it will be useful,
  27. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  29. *  GNU General Public License for more details.
  30. *
  31. *  This copyright notice MUST APPEAR in all copies of the script!
  32. *
  33. * ***** END LICENSE BLOCK ***** */
  34.  
  35. /* #Requires / #Includes */
  36. require_once(PATH_tslib.'class.tslib_pibase.php');
  37. if (!class_exists('Dwoo')) require_once(t3lib_extMgm::extPath('dwoo').'lib/dwooAutoload.php');
  38.  
  39. /**
  40.  * Plugin 'st_feuser_dmail_unsubscribe' for the 'st_feuser_dmail_unsubscribe' extension.
  41.  *
  42.  * @author       <st_yugoo>
  43.  * @package     TYPO3
  44.  * @subpackage  tx_stfeuserdmailunsubscribe
  45.  */
  46. class tx_stfeuserdmailunsubscribe_pi1 extends tslib_pibase {
  47.         public $prefixId      = 'tx_stfeuserdmailunsubscribe_pi1';              // Same as class name
  48.         public $scriptRelPath = 'pi1/class.tx_stfeuserdmailunsubscribe_pi1.php';        // Path to this script relative to the extension dir.
  49.         public $extKey        = 'st_feuser_dmail_unsubscribe';  // The extension key.
  50.         public $pi_checkCHash = true;
  51.  
  52.     private $output = null;
  53.         private $DB = null;
  54.     private $TSFE = null;
  55.     private $compiler = null;
  56.     private $dwoo = null;
  57.        
  58.     public function __construct() {
  59.         $this->ytools = new yTools();
  60.         $this->ytools->invisibleErrors();    // error-reporting for developpers only
  61.         $this->dwoo = new Dwoo();
  62.         $this->compiler = new Dwoo_Compiler();
  63.     }
  64.  
  65.     public function __destruct() { }
  66.  
  67.         public function main($content, $conf) {
  68.                 $this->conf = $conf;
  69.                 $this->pi_setPiVarDefaults();
  70.         $this->pi_loadLL();
  71.  
  72.         $this->DB = &$GLOBALS['TYPO3_DB'];  
  73.         $this->TSFE = &$GLOBALS['TSFE'];
  74.         $this->fe_user = &$GLOBALS['TSFE']->fe_user->user['uid'];
  75.         $this->TSFE->no_cache = 1;
  76.  
  77.         // Si on ne peut récupérer le fe_user.uid on ne charge pas le pi.
  78.         // TODO: gérer mieux la sécurité (typo3 fe_user sessions).
  79.         if (!(int)$this->fe_user) return;
  80.  
  81.         // Association conf[] => $obj->attr
  82.         $this->cset = Array(
  83.                 Array('template.', 'template'),
  84.         );
  85.  
  86.         if ($this->TS2Attribute($this->conf, $this->cset)) {
  87.             $this->initTemplate($this->template['file']);
  88.             $this->CatArray = $this->getCategoriesInfo($this->fe_user);
  89.             $this->buildTemplate();          
  90.         }
  91.  
  92.                 return $this->pi_wrapInBaseClass($this->output);
  93.         }
  94.  
  95.     /**
  96.      * Supprime les catégories DirectMail d'un fe_user
  97.      */
  98.     private function unsetCategories($arr) {
  99.         if (is_array($arr)) {
  100.             $VALUES = $query = null;
  101.             foreach ($arr as $k => $v) $VALUES[] = '('.$this->newID.','.$v.')';  
  102.             $query[] = 'UPDATE fe_users SET module_sys_dmail_category = 0 WHERE uid = '.$this->newID.' LIMIT 1';
  103.             $query[] = 'INSERT INTO sys_dmail_feuser_category_mm (uid_local,uid_foreign) VALUES '.implode(',', $VALUES);
  104.  
  105.             foreach ($query as $a => $b) $res = $this->DB->sql_query($query[$a]);  
  106.             return $query;
  107.         }
  108.         return false;
  109.     }
  110.  
  111.     /**
  112.      * Le fe_user utilise-t'il les catégories ?
  113.      */
  114.     private function useDMAILCategories($user) {
  115.         $query = 'SELECT module_sys_dmail_category FROM fe_users WHERE uid = '.$user;
  116.         $res = $this->DB->sql_query($query);
  117.         $row = $this->DB->sql_fetch_assoc($res);
  118.  
  119.         if (!(int)$row['module_sys_dmail_category']) return false;
  120.         else return true;
  121.     }
  122.  
  123.     /**
  124.      * Produit un CatArray contenant les informations liées au catégories DMAIL d'un fe_user
  125.      */
  126.     private function getCategoriesInfo($user) {
  127.         $usercat = $rootcat = $usersubscr = $tbl = null;
  128.         $arr = $this->getTypoTemplate($this->getRoot($this->getAssocPID($user)));
  129.  
  130.         if (!$arr['tx_directmail.']['default.']['categories']) return false;
  131.         $usersubscr = $this->useDMAILCategories($user);
  132.         $usercat = $this->getUserCategories($user);
  133.         $rootcat = explode(',', $arr['tx_directmail.']['default.']['categories']);
  134.         if ($usersubscr && $usercat) {
  135.             foreach ($rootcat as $i => $j) {
  136.                 if (($t = array_search($j, $usercat)) !== false) $tbl[] = $usercat[$t];
  137.             }
  138.             return array(
  139.                 'rootcat' => $rootcat,
  140.                 'usercat' => $tbl,
  141.             );
  142.         }
  143.         else return array('rootcat' => $rootcat, 'usercat' => null);
  144.     }
  145.  
  146.     /**
  147.      * Retourne le nombre de catégories abonnées
  148.      */
  149.     private function getMyTotalCats($CatArray) {
  150.         $nb = sizeof($CatArray['usercat']);
  151.         if ($nb) return $nb;
  152.         else return $this->LLStr('status.void');
  153.     }
  154.  
  155.     /**
  156.      * Récupère les catégories DMAIL liées à l'utilisateur
  157.      */
  158.     private function getUserCategories($user) {
  159.         $query = 'SELECT uid_foreign FROM sys_dmail_feuser_category_mm WHERE uid_local = '.$user;
  160.         $res = $this->DB->sql_query($query);
  161.  
  162.         while ($row = $this->DB->sql_fetch_assoc($res)) $tmp[] = $row['uid_foreign'];  
  163.         return $tmp;
  164.     }
  165.  
  166.     /**
  167.      * Initialise la template
  168.      */
  169.     private function initTemplate($tmpl) {
  170.         $this->myTemplate = new Dwoo_Template_File($tmpl);
  171.     }
  172.  
  173.     /**
  174.      * Construit la template
  175.      */
  176.     private function buildTemplate() {
  177.         $this->content = null;
  178.         $data = array(
  179.             'legend' => $this->LLStr('legend'),
  180.             'form' => array(  
  181.                 'submit' => $this->LLStr('submit'),
  182.              ),
  183.             'categories' => $this->genCheckboxArray($this->CatArray),
  184.             'total' => $this->getMyTotalCats($this->CatArray),
  185.             'status' => $this->LLStr('status'),
  186.             'subscribed' => $this->useDMAILCategories($this->fe_user),
  187.             'unsubscribed' => array(
  188.                 'text' => $this->LLStr('unsubscribed.text'),
  189.                 'option' => $this->LLStr('unsubscribed.option'),
  190.             ),
  191.         );
  192.         $this->output .= $this->dwoo->get($this->template['file'], $data);  
  193.     }
  194.  
  195.     /**
  196.      * Récupère le titre d'une catégorie
  197.      */
  198.     private function getCatname($id) {
  199.         $res = $GLOBALS['TYPO3_DB']->sql_query('SELECT category FROM sys_dmail_category WHERE uid = '.$id);
  200.         $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);  
  201.         return $row['category'];    
  202.     }
  203.    
  204.     /**
  205.      * Génère le tableau de configuration de la liste de checkboxes présente au sein du formulaire.
  206.      */
  207.     private function genCheckboxArray($CatArray) {
  208.         $arr = null;
  209.         foreach ($CatArray['rootcat'] as $a => $z) {
  210.             if ($CatArray['usercat']) {
  211.                 if (array_search($z, $CatArray['usercat']) === false)
  212.                     $arr[] = array('name' => 'cat'.$z, 'id' => 'cat'.$z,'checked' => null, 'title' => $this->getCatname($z));
  213.                 else $arr[] = array('name' => 'cat'.$z, 'id' => 'cat'.$z,'checked' => 'checked', 'title' => $this->getCatname($z));
  214.             }
  215.             else $arr[] = array('name' => 'cat'.$z, 'id' => 'cat'.$z,'checked' => null, 'title' => $this->getCatname($z));      
  216.         }        
  217.         return $arr;
  218.     }
  219.  
  220.     /**
  221.      * Transforme les variables typoscript en attribut de classe
  222.      */
  223.     private function TS2Attribute($conf, $cset) {
  224.         if (is_array($cset) && $conf) {
  225.             foreach ($cset as $k) {
  226.                 if (array_key_exists(pos($k), $conf))
  227.                     (is_array(next($k))) ? $this->{pos(pos($k))}[next(pos($k))] = $conf[prev($k)] : $this->{pos($k)} = $conf[prev($k)];      
  228.                 else return false;
  229.             }
  230.             return true;
  231.         }
  232.         return false;    
  233.     }
  234.  
  235.     /**
  236.      * Retourne le chaine de caractère correspondante à la clef présente dans le localconf du plugin
  237.      * TODO: faire en sorte que le path soit dynamique, on cut $scriptRelPath = 'pi1/class.tx_stfeuserdmailunsubscribe_pi1.php'
  238.      *       on récupère pixx et on fabrique extKey + pixxxx + locallang.xml
  239.      */
  240.     private function LLStr($str) { return $GLOBALS["TSFE"]->sL('LLL:EXT:'.$this->extKey.'/pi1/locallang.xml:'.$str); }
  241.  
  242.     /**
  243.      * Récupère le root d'une arborescence
  244.      */
  245.     private function getRoot($id) {
  246.         $sys_page = t3lib_div::makeInstance("t3lib_pageSelect");
  247.         $arr = $sys_page->getRootLine($id);
  248.         return $arr[0]['uid'];
  249.     }
  250.  
  251.     /**
  252.      * Récupère la template typoscript d'une page donnée
  253.      */
  254.     private function getTypoTemplate($id) {
  255.         $q = $this->DB->exec_SELECTquery('config', 'sys_template', 'pid ='.$id);
  256.         $res = $this->DB->sql_fetch_assoc($q);
  257.         $tsparserObj = t3lib_div::makeInstance('t3lib_TSparser');
  258.         $tsparserObj->parse($res['config']);
  259.         return $tsparserObj->setup;  
  260.     }
  261.  
  262.     /**
  263.      * Récupère le PID du sys-folder associé au fe_user
  264.      */
  265.     private function getAssocPID($feuser_id) {
  266.         $res = $GLOBALS['TYPO3_DB']->sql_query('SELECT pid FROM fe_users WHERE uid = '.$feuser_id);
  267.         $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);  
  268.         return $row['pid'];
  269.     }
  270. }
  271.  
  272. /**
  273.  * Yugoo's tools class
  274.  */
  275. class yTools {
  276.  
  277.     /* $IP */
  278.     public $ipArr = array('207.107.183.10' /* sys-tech */, '127.0.0.1');
  279.  
  280.     /**
  281.      * dumpy - Vardump your variable if you're an admin.
  282.      */
  283.     public function dumpy(&$pVar) {
  284.         if (!in_array($_SERVER['REMOTE_ADDR'], $this->ipArr)) return;
  285.         var_dump($pVar);
  286.         flush();
  287.     }
  288.  
  289.     /**
  290.      * invisibleErrors - Enable admin only error reporting.
  291.      */
  292.     public function invisibleErrors() {
  293.         if (!in_array($_SERVER['REMOTE_ADDR'], $this->ipArr)) return;
  294.         if (!ini_get('display_errors')) ini_set('display_errors', 1);
  295.         error_reporting (E_ALL ^ E_NOTICE);
  296.     }
  297.  
  298.     /**
  299.      * customTrunc - truncate text as you wish.
  300.      */
  301.     public function customTrunc($string, $length = 20, $etc = '...', $break_words = false, $middle = false) {
  302.         if ($length == 0) return '';
  303.         if (strlen($string) > $length) {
  304.             $length -= min($length, strlen($etc));
  305.             if (!$break_words && !$middle)
  306.                 $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length+1));
  307.             if(!$middle) return substr($string, 0, $length) . $etc;
  308.             else return substr($string, 0, $length/2) . $etc . substr($string, -$length/2);
  309.         }
  310.         else return $string;
  311.     }
  312. }
  313.  
  314. if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/st_feuser_dmail_unsubscribe/pi1/class.tx_stfeuserdmailunsubscribe_pi1.php'])   {
  315.         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/st_feuser_dmail_unsubscribe/pi1/class.tx_stfeuserdmailunsubscribe_pi1.php']);
  316. }
  317.  
  318. ?>