Current File : /home/karenpetzb/library/Modules/Default/Controllers/MainController.php
<?php
class Modules_Default_Controllers_MainController extends Modules_Common_Controllers_MainController
{

    
	
	protected function resetSession() { 
		$usernamespaceVar = $this->getSession();
		unset($usernamespaceVar->listallannonces); 
		unset($usernamespaceVar->listallannoncesfrontheader); 
		unset($usernamespaceVar->listallcategories); 
		unset($usernamespaceVar->listallbrends); 
		 
		unset($usernamespaceVar->myFactureValidate); 
		unset($usernamespaceVar->addresseLiv); 
	}
	
	protected function isConnected() {
		$auth = Zend_Auth::getInstance();
		$auth->setStorage($this->getSessionStorage());
		$storage = $auth->getStorage()->read(); 
		if ($auth->hasIdentity() && isset($storage['user'])) { return true; }
		return false;
	}
	
	protected function getStorageUser() {
		$auth = Zend_Auth::getInstance();
		$auth->setStorage($this->getSessionStorage());
		$storage = $auth->getStorage()->read(); 
		return $storage['user'];
	}

	protected function connectMe($login, $mdp) {
		try {
			// setup Zend_Auth adapter for a database table
			Zend_Loader::loadClass('Zend_Auth_Adapter_DbTable');
			$dbAdapter = Zend_Registry::get('dbAdapter');

			$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter,
															'user',
															'LOGIN',
															'MDP',
															'MD5(?)');

			// Set the input credential values to authenticate against
			$authAdapter->setIdentity($login);
			$authAdapter->setCredential($mdp);

			// do the authentication
			$auth = Zend_Auth::getInstance();
			$auth->setStorage($this->getSessionStorage());
			$result = $auth->authenticate($authAdapter);

			if ($result->isValid()) {
				$user = new User();
				$isEnableConnect = $user->fetchRow("LOGIN = '".$login."' AND isBAN = 1");
				if ($isEnableConnect) {
					$storage = $auth->getStorage();
					// success: store database row to auth's storage
					// system. (Not the password though!) //array('IDUSER', 'LOGIN'));
					$data = $authAdapter->getResultRowObject(null, 'MDP');

					$userAuth = $this->computUserInfo($data);

					$storage->write(array( 'user' => $userAuth));
					$this->log("Login : ".$login,'info');
					return "SUCCESS";
				} else {
					return "Vous n'avez plus acc�s � votre compte.";
					$auth->clearIdentity();
				}
			} else {
				return 'Les identifiants sont incorrects.';
				$auth->clearIdentity();
			}
		} catch (Zend_Exception $e) {
			$this->log($e->getMessage(),'err');
			return  "Une erreur est survenue, v�rifier vos informations.";
		}
	}
	protected function computUserInfo($data){
		return $userAuth = array('login'  => $data -> LOGIN,
				                     'id' => $data -> ID,
				                     'role' => $data -> ROLE,
				                     'nom' => $data -> NOM,
				                     'prenom' => $data -> PRENOM,
				                     'civilite' => $data -> CIVILITE,
				                     'fonction' => $data -> FONCTION,
				                     'raisonsocial' => $data -> RAISONSOCIAL,
				                     'adresse' => $data -> ADRESSE,
				                     'cp' => $data -> CP,
				                     'ville' => $data -> VILLE,
				                     'pays' => $data -> PAYS,
				                     'departement' => $data -> DEPARTEMENT,
				                     'region' => $data -> REGION,
				                     'adressecomplete' => $data -> ADRESSECOMPLETE,
				                     'email' => $data -> EMAIL,
				                     'tel' => $data -> TEL,
				                     'fax' => $data -> FAX,
				                     'numcompte' => $data -> NUMCOMPTE,
				                     'siret' => $data -> SIRET,
				                     'numidfisc' => $data -> NUMIDFISC,
				                     'codeape' => $data -> CODEAPE,
				                     'sectactivite' => $data -> SECTACTIVITE,
				                     'dateinsc' => $data -> DATEINSC,
				                     'commentaire' => $data -> COMMENTAIRE,
				                     'cintern' => $data -> CODEINTERN,
				                     'type' => $data -> TYPE,
				                     'modepaiement' => $data -> MODEPAIEMENT,
									 'iscredit' => $data -> isCREDIT,
									 'isrecepfacture' => $data -> isRECEPFACTURE,
									 'iscaddytype' => $data -> isCADDYTYPE);
	}

	private function getcategoriebyid($id) {
		$category = new Category();
		return $category->getTreeCatsOf($id);
	}
	
	private function initLog() {
		$registry = Zend_Registry::getInstance();
		$loggerDefault = $registry->get('loggerDefault');

		$controller = Zend_Controller_Front::getInstance()->getRequest();
		$loggerDefault->setEventItem('controller', $controller->getControllerName().'::'.$controller->getActionName());
			
		$registry->set('loggerDefault', $loggerDefault);
	}

	protected function log($message , $level) {
		$loggerDefault = Zend_Registry::get('loggerDefault');

		$message .= "<br/>USER AGENT : ".getenv("HTTP_USER_AGENT");
		$botDetector = new BotDetector();
		$isBot = $botDetector->isBot(getenv("HTTP_USER_AGENT"));
		if ($isBot != 'ERROR') {
			$message .= "<br/>Bot : ".$isBot;
		}
		 
		if ($level == 'info') {
			$loggerDefault->info($message);
		} elseif ($level == 'err') {
			$loggerDefault->err($message);
		} elseif ($level == 'warn') {
			$loggerDefault->warn($message);
		} elseif ($level == 'crit') {
			$loggerDefault->crit($message);
		}
	}
	

	protected function getSession(){ 
        $result = new stdClass();
        try {
	        $registry = Zend_Registry::getInstance();
	        $setting = $registry->get('setting'); 
	        $result = new Zend_Session_Namespace($setting->session_variable);
        } catch (Zend_Exception $e) {
			$this->log("Error getSession : ".$e->getMessage(),'warn');
            $result->myObjectCaddy = new Caddy();
		} 
        return $result;
	}

	protected function getSessionStorage() {
        try {
		    $registry = Zend_Registry::getInstance();
		    $setting = $registry->get('setting');
		    return new Zend_Auth_Storage_Session($setting->session_storage);
        } catch (Zend_Exception $e) {
			$this->log("Error getSession : ".$e->getMessage(),'warn'); 
		}
        return new stdClass();
	}

	private function checkRedirection() { 
        $front = Zend_Controller_Front::getInstance();
        
        $currentpage = $front->getRequest()->getRequestUri();
        
        if ($currentpage == "/index.php") {
			$this->_redirect($this->baseUrl_SiteCommerceUrl, array('code'=>301));
        } 
       return $currentpage;
	}

	protected function send301Redirection($url) { 
		$this->_redirect($url, array('code'=>301));
	}
	protected function setCanonicalUrl($url) { 
        $this->view->current_canonicalurl = $url;
	}


	function preDispatch()
	{ 
		$this->initLog();
		$this->view->designColors = $this->getDesignColors();
		$this->initVariables();
        
		$currentpage = $this->checkRedirection(); 
        
		$userNamespace =  $this->getSession(); 
         
		if (!isset($userNamespace->listallpays)) {
			$pays = new Pays();
			$userNamespace->listallpays = $pays->getPaysFR();
		}
		$this->view->listPays = $userNamespace->listallpays;

		$category = new Category();		
		if ($this->getRequest()->getParam('c')) {
			$userNamespace->lastCategorySelected = (int)$this->getRequest()->getParam('c');
		} elseif ($this->getRequest()->getParam('p')) {
			if (!isset($userNamespace->lastCategorySelected)) {
				$userNamespace->lastCategorySelected = $category->getIdCatFromProductId((int)$this->getRequest()->getParam('p'));
			}
		} else {
			$userNamespace->lastCategorySelected = 0;
		}
		$idCat = $userNamespace->lastCategorySelected;
		
		$controllerName = $this->getRequest()->getControllerName();
		$annoncesfooter = new  AnnonceFooter();
		$this->view->listallannoncesfooter = $annoncesfooter->getAnnoncesByShow(0,$controllerName);

		$auth = Zend_Auth::getInstance();
		$storage = $auth->getStorage()->read();

        $isBoostedHome = false;
        $boostedHome = array();
        $isBoostedTextHome = false;
        $boostedTextHome = array();
         
        $this->view->currentPage = $currentpage;
        if("/"==$currentpage) {
            $currentUser = array();
            if ($auth->hasIdentity() && isset($storage['user'])) {
                $currentUser = $storage['user'];
            }            
            $product = new Product();
            $boostedHome = $product->getBoostedHome($currentUser);
            if (isset($boostedHome) && !empty($boostedHome) && isset($boostedHome["PRODUCTS"]) && !empty($boostedHome["PRODUCTS"])) {
                $isBoostedHome = true;
            }
            
            $annoncesright = new AnnonceRight();
            /* Cas des pubs par categories a droite
            $boostedHomeText = $annoncesright->getAnnoncesByShow($idCat);
            $this->view->listannoncesright = $boostedHomeText
            $this->view->listannoncesrightCategory = $idCat;*/            
            $boostedTextHome = $annoncesright->getAnnonces();
            
            if (isset($boostedTextHome) && !empty($boostedTextHome)) {
                $isBoostedTextHome = true;
            }
            
        } 
        $this->view->boostedTextHome = $boostedTextHome;
        $this->view->isBoostedTextHome = $isBoostedTextHome;
        $this->view->boostedHome = $boostedHome;
        $this->view->isBoostedHome = $isBoostedHome;

        $generateAllCategoriesMenu = $category->generateAllCategoriesMenu(0); 
        $userNamespace->listallcategories = $generateAllCategoriesMenu;
		$this->view->listallcategories = $generateAllCategoriesMenu;
		   
		//Listing des liens de service 
		$footers = new FooterContent();
		$listFooterTemp = $footers->fetchAll();
		$listFooter = array();
		$i = 0;

		foreach ($listFooterTemp AS $row) {
			$listFooter[$i]['ID'] = $row['ID'];
			$listFooter[$i]['TITRE'] = $row['TITRE'];
			$listFooter[$i]['URL'] = $row['URL'];
			$listFooter[$i]['CONTENT'] = $row['CONTENT'];
			$listFooter[$i]['TITRENAV'] =  $this->verifyNavigationString("", $row['TITRE'], "");
			$i++;
		}
		$userNamespace->listFooters = $listFooter; 
		$this->view->listFooters = $listFooter;

		//Mots clefs
		/*$keymap = new KeyMap();
    		$this->view->listKeyMap = $keymap->getKeyByPositionAndCatId($idCat, $listIds);
        */
		$listIds = $category->getAllSubsIDByIDToString($idCat);

		if ($auth->hasIdentity() && isset($storage['user'])) {
			$this->view->user = $storage['user'];
			$this->view->connectedNameFirstname = $storage['user']['prenom'].' '.$storage['user']['nom'];           
            
		}
		//Caddy
		if (!isset($userNamespace->myObjectCaddy)) {
			$userNamespace->myObjectCaddy = new Caddy();
		}
        if (isset($userNamespace->myFactureValidate)) {
			$this->view->user_facture_validate = $userNamespace->myFactureValidate;
        }
	}
}
?>