Source for file DatabaseModel.php

Documentation is available at DatabaseModel.php

  1. <?php
  2.  
  3. /**
  4.  * @package inc
  5.  * @subpackage model
  6.  */
  7.  
  8. /**
  9.  *
  10.  * DatabaseModel class
  11.  *
  12.  * @author Felix Rupp <kontakt@felixrupp.com>
  13.  * @version $Id$
  14.  * @copyright Copyright (c) 2011, Felix Rupp, Nicole Reinhardt
  15.  * @license http://www.opensource.org/licenses/mit-license.php MIT-License
  16.  * @license http://www.gnu.org/licenses/gpl.html GNU GPL
  17.  * 
  18.  * @package inc
  19.  * @subpackage model
  20.  */
  21. class DatabaseModel {
  22.  
  23.     # Attributes
  24.     /**
  25.      * @var DatabaseModel $_databaseModel Contains single instance of our DatabaseModel
  26.      * @staticvar $_databaseModel 
  27.      */
  28.     private static $_databaseModel NULL;
  29.  
  30.     /**
  31.      * @var SimpleXMLExtended $_dataBase Contains the SimpleXMLExtended of our xml-database
  32.      */
  33.     private $_dataBase;
  34.  
  35.  
  36.     /**
  37.      * Configurator class-constructor
  38.      * 
  39.      * @return void 
  40.      */
  41.     private function DatabaseModel({
  42.         
  43.         try {
  44.             $this->reloadDatabaseFile();
  45.         }
  46.         catch(CaramelException $e{
  47.             $e->getDetails();
  48.         }
  49.             
  50.     // End of constructor declaration
  51.     
  52.     
  53.     
  54.     /**
  55.      * Singleton-create-method
  56.      * 
  57.      * @static
  58.      * @return Single instance of DatabaseModel-Class
  59.      */
  60.     public static function getDatabaseModel({
  61.     
  62.         if (self::$_databaseModel === NULL{
  63.             self::$_databaseModel new self();
  64.         }
  65.         return self::$_databaseModel;
  66.  
  67.     // End of method declaration
  68.     
  69.     
  70.     
  71.     /**
  72.      * Overwrite __clone() method to prevent instance-cloning
  73.      * 
  74.      * @return void 
  75.      */
  76.     private function __clone({}
  77.     
  78.     
  79.     
  80.     /**
  81.      * Method to get all used languages out of database
  82.      * 
  83.      * @throws CaramelException
  84.      * @return Array with all used language-codes
  85.      */
  86.     public function frontendGetAllLanguagesAction({
  87.         
  88.         $allLanguages array();
  89.         
  90.         # Fill our languages array
  91.         try{
  92.             
  93.             $xPathResult $this->_dataBase->xpath('//@lang')# Find all lang-elements
  94.             $xPathResult array_unique($xPathResult)# Remove double entries
  95.             
  96.             foreach($xPathResult as $langCode{
  97.                 array_push($allLanguages(string)$langCode)# Convert SimpleXMLElements into strings
  98.             }
  99.             
  100.         catch(CaramelException $e{
  101.             $e->getDetails();
  102.         }
  103.         
  104.         return $allLanguages;
  105.         
  106.     // End of method declaration
  107.     
  108.     
  109.     
  110.     /**
  111.      * Method to return all meta information for the given page/language
  112.      * 
  113.      * @param string $lang Current language used
  114.      * @param string $pageName Name of the current page
  115.      * 
  116.      * @throws CaramelException
  117.      * @return String with all needed meta information to one page
  118.      */
  119.     public function frontendGetAllMetaTagsAction($lang$pageId{
  120.         
  121.         
  122.         if($pageId!=NULL{
  123.             $xPathResultMetaDescription $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record[@lang="'.$lang.'"]/meta[@name="description"]');
  124.         }
  125.         else{
  126.             $xPathResultMetaDescription $this->_dataBase->xpath('//page/record[@lang="'.$lang.'"]/meta[@name="description"]');
  127.         }
  128.         
  129.         
  130.         if(count($xPathResultMetaDescription)>&& $xPathResultMetaDescription!=false{
  131.             $metaDescription "<meta name=\"description\" content=\"".$xPathResultMetaDescription[0]."\">";
  132.         }
  133.         else // Take first language
  134.             
  135.             if($pageId!=NULL{
  136.                 $xPathResultMetaDescription $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record/meta[@name="description"]');
  137.             }
  138.             else{
  139.                 $xPathResultMetaDescription $this->_dataBase->xpath('//page/record/meta[@name="description"]');
  140.             }
  141.             
  142.             
  143.             if(count($xPathResultMetaDescription)>&& $xPathResultMetaDescription!=false{
  144.                 $metaDescription "<meta name=\"description\" content=\"".$xPathResultMetaDescription[0]."\">";
  145.             }
  146.             else {
  147.                 throw new CaramelException(10);
  148.             }
  149.             
  150.         }
  151.         
  152.         
  153.         
  154.         
  155.         if($pageId!=NULL{
  156.             $xPathResultMetaKeywords $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record[@lang="'.$lang.'"]/meta[@name="keywords"]');
  157.         }
  158.         else{
  159.             $xPathResultMetaKeywords $this->_dataBase->xpath('//page/record[@lang="'.$lang.'"]/meta[@name="keywords"]');
  160.         }
  161.         
  162.         if(count($xPathResultMetaKeywords)>&& $xPathResultMetaKeywords!=false{
  163.             $metaKeywords "<meta name=\"keywords\" content=\"".$xPathResultMetaKeywords[0]."\">";
  164.         }
  165.         else // Take first language
  166.             
  167.             if($pageId!=NULL{
  168.                 $xPathResultMetaKeywords $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record/meta[@name="keywords"]');
  169.             }
  170.             else{
  171.                 $xPathResultMetaKeywords $this->_dataBase->xpath('//page/record/meta[@name="keywords"]');
  172.             }
  173.             
  174.             if(count($xPathResultMetaKeywords)>&& $xPathResultMetaKeywords!=false{
  175.                 $metaKeywords "<meta name=\"keywords\" content=\"".$xPathResultMetaKeywords[0]."\">";
  176.             }
  177.             else {
  178.                 throw new CaramelException(10);
  179.             }
  180.             
  181.         }
  182.         
  183.         
  184.         
  185.         
  186.         if($pageId!=NULL{
  187.             $xPathResultMetaAuthor $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record[@lang="'.$lang.'"]/meta[@name="author"]');
  188.         }
  189.         else{
  190.             $xPathResultMetaAuthor $this->_dataBase->xpath('//page/record[@lang="'.$lang.'"]/meta[@name="author"]');
  191.         }
  192.         
  193.         if(count($xPathResultMetaAuthor)>&& $xPathResultMetaAuthor!=false{
  194.             $metaAuthor "<meta name=\"author\" content=\"".$xPathResultMetaAuthor[0]."\">";
  195.         }
  196.         else // Take first language
  197.             
  198.             if($pageId!=NULL{
  199.                 $xPathResultMetaAuthor $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record/meta[@name="author"]');
  200.             }
  201.             else{
  202.                 $xPathResultMetaAuthor $this->_dataBase->xpath('//page/record/meta[@name="author"]');
  203.             }
  204.             
  205.             if(count($xPathResultMetaAuthor)>&& $xPathResultMetaAuthor!=false{
  206.                 $metaAuthor "<meta name=\"author\" content=\"".$xPathResultMetaAuthor[0]."\">";
  207.             }
  208.             else {
  209.                 throw new CaramelException(10);
  210.             }
  211.             
  212.         }
  213.         
  214.         return $metaDescription."\n".$metaKeywords."\n".$metaAuthor."\n";
  215.         
  216.     // End of method declaration
  217.     
  218.     
  219.     
  220.     /**
  221.      * Method to return the needed website title
  222.      * 
  223.      * @param string $lang Current language used
  224.      * @param string $pageName Name of the current page
  225.      * 
  226.      * @throws CaramelException
  227.      * @return String with correct website title
  228.      */
  229.     public function frontendGetWebsiteTitleAction($lang$pageId{
  230.                 
  231.         if($pageId!=NULL{
  232.             $xPathResultTitle $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record[@lang="'.$lang.'"]/title');
  233.         }
  234.         else{
  235.             $xPathResultTitle $this->_dataBase->xpath('//page/record[@lang="'.$lang.'"]/title');
  236.         }
  237.         
  238.         if(count($xPathResultTitle)>&& $xPathResultTitle!=false{
  239.             $title = (string)$xPathResultTitle[0][0];
  240.         }
  241.         else // Take first language
  242.             
  243.             if($pageId!=NULL{
  244.                 $xPathResultTitle $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record/title');
  245.             }
  246.             else{
  247.                 $xPathResultTitle $this->_dataBase->xpath('//page/record/title');
  248.             }
  249.             
  250.             
  251.             if(count($xPathResultTitle)>&& $xPathResultTitle!=false{
  252.                 $title = (string)$xPathResultTitle[0][0];
  253.             }
  254.             else {
  255.                 throw new CaramelException(10);
  256.             }
  257.         }
  258.         
  259.         return $title;
  260.         
  261.     // End of method declaration
  262.     
  263.     
  264.     
  265.     /**
  266.      * Method to return the content of a page
  267.      * 
  268.      * @param string $lang Current language used
  269.      * @param string $pageName Name of the current page
  270.      * 
  271.      * @throws CaramelException
  272.      * @return String with correct website content
  273.      */
  274.     public function frontendGetWebsiteContentAction($lang$pageId{
  275.         
  276.         
  277.         if($pageId!=NULL{
  278.             $xPathResultContent $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record[@lang="'.$lang.'"]/content');
  279.         }
  280.         else{
  281.             $xPathResultContent $this->_dataBase->xpath('//page/record[@lang="'.$lang.'"]/content');
  282.         }
  283.         
  284.         
  285.         if(count($xPathResultContent)>&& $xPathResultContent!=false{
  286.                         
  287.             $content = (string)$xPathResultContent[0][0];
  288.  
  289.         }
  290.         else // take first language available
  291.             
  292.             if($pageId!=NULL{
  293.                 $xPathResultContent $this->_dataBase->xpath('//page[@id="'.$pageId.'"]/record/content');
  294.             }
  295.             else{
  296.                 $xPathResultContent $this->_dataBase->xpath('//page/record/content');
  297.             }
  298.             
  299.             if(count($xPathResultContent)>&& $xPathResultContent!=false{
  300.                 $content = (string)$xPathResultContent[0][0];
  301.             }
  302.             else {
  303.                 throw new CaramelException(10);
  304.             }
  305.  
  306.         }
  307.         
  308.         # Replace mailto: to prevent email-spam
  309.         $content str_replace("mailto:""&#109;&#97;&#105;&#108;&#116;&#111;&#58;"$content);
  310.         
  311.         # Find all eMail adresses and save them in an array
  312.         $result preg_match_all('/([-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@{1}([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?)/i'$content$hits);
  313.         
  314.         foreach($hits[0as $email{
  315.             
  316.             $content str_replace($email$this->plainToUnicode($email)$content);
  317.             
  318.         }
  319.                 
  320.         return $content;
  321.         
  322.     // End of method declaration
  323.     
  324.     
  325.     
  326.     /**
  327.      * Method to return array with localized navigation
  328.      * Note: Navigation is restricted to one sublevel
  329.      * 
  330.      * @param string $lang Current language
  331.      * 
  332.      * @throws CaramelException
  333.      * @return Array with localized navigation information
  334.      */
  335.     public function frontendGetWebsiteNavigationAction($lang{
  336.                 
  337.         $orderedNavi array();
  338.         
  339.         foreach($this->_dataBase->page as $page{
  340.             
  341.             ## Get records
  342.             $xPathResultRecord $page->xpath('record[@lang="'.$lang.'"]');
  343.             
  344.             if(count($xPathResultRecord&& $xPathResultRecord!=false{
  345.             
  346.                 $record $xPathResultRecord[0];
  347.             
  348.                 $orderedNavi[(int)$page->attributes()->idarray(
  349.                     "path" => (string)$page->attributes()->path
  350.                     "visible" => (string)$record->navigation->attributes()->visible,
  351.                     "navigation" => (string)$record->navigation,
  352.                     "title" => (string)$record->title,
  353.                     "titletag" => (string)$record->titletag,
  354.                 );
  355.                 
  356.             else // Take first language
  357.                 
  358.                 $xPathResultRecord $page->xpath('record');
  359.                 
  360.                 if(count($xPathResultRecord&& $xPathResultRecord!=false{
  361.                     $record $xPathResultRecord[0];
  362.                         
  363.                     $orderedNavi[(int)$page->attributes()->idarray(
  364.                         "path" => (string)$page->attributes()->path
  365.                         "visible" => (string)$record->navigation->attributes()->visible,
  366.                         "navigation" => (string)$record->navigation,
  367.                         "title" => (string)$record->title,
  368.                         "titletag" => (string)$record->titletag,
  369.                     );
  370.                 }
  371.                 else {
  372.                     throw new CaramelException(10);
  373.                 }
  374.                 
  375.             }
  376.             
  377.             $orderedNavi[(int)$page->attributes()->id]["subpages"array();
  378.             
  379.             
  380.             ## SubPages
  381.             $subPages $page->page;
  382.             
  383.             foreach($subPages as $subPage{
  384.                 
  385.                 ## Get subrecords
  386.                 $xPathResultRecord $subPage->xpath('record[@lang="'.$lang.'"]');
  387.                         
  388.                 if(count($xPathResultRecord&& $xPathResultRecord!=false{
  389.                             
  390.                     $subRecord $xPathResultRecord[0];
  391.                             
  392.                     $orderedNavi[(int)$page->attributes()->id]["subpages"][(int)$subPage->attributes()->idarray(
  393.                         "path" => (string)$subPage->attributes()->path,
  394.                         "visible" => (string)$subRecord->navigation->attributes()->visible,
  395.                         "navigation" => (string)$subRecord->navigation,
  396.                         "title" => (string)$subRecord->title,
  397.                         "titletag" => (string)$subRecord->titletag,
  398.                     );
  399.     
  400.                 else // Take first language
  401.                     
  402.                     $xPathResultRecord $subPage->xpath('record');
  403.                     
  404.                     
  405.                     if(count($xPathResultRecord&& $xPathResultRecord!=false{
  406.                         $subRecord $xPathResultRecord[0];
  407.                             
  408.                         $orderedNavi[(int)$page->attributes()->id]["subpages"][(int)$subPage->attributes()->idarray(
  409.                             "path" => (string)$subPage->attributes()->path,
  410.                             "visible" => (string)$subRecord->navigation->attributes()->visible,
  411.                             "navigation" => (string)$subRecord->navigation,
  412.                             "title" => (string)$subRecord->title,
  413.                             "titletag" => (string)$subRecord->titletag,
  414.                         );
  415.                     }
  416.                     else {
  417.                         throw new CaramelException(10);
  418.                     }
  419.                     
  420.                 }
  421.             }
  422.         }
  423.         
  424.         return $orderedNavi;
  425.         
  426.     // End of method declaration
  427.     
  428.     
  429.     
  430.     /**
  431.      * Method to get the additional CSS file for this page
  432.      *
  433.      * @param String $id ID of the page
  434.      * @throws CaramelException
  435.      *
  436.      * @return Name of the additional CSS file
  437.      */
  438.     public function frontendGetPageAdditionalCss($id{
  439.     
  440.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$id.'"]');
  441.             
  442.         if(count($xPathResultPage)>0{
  443.     
  444.             return stripslashes(trim((string)$xPathResultPage[0]->stylesheet));
  445.         }
  446.         else {
  447.             throw new CaramelException(10);
  448.         }
  449.     
  450.     // End of method declaration
  451.     
  452.     
  453.     
  454.     /**
  455.      * Method to get the additional JS file for this page
  456.      *
  457.      * @param String $id ID of the page
  458.      * @throws CaramelException
  459.      *
  460.      * @return Name of the additional JS file
  461.      */
  462.     public function frontendGetPageAdditionalJs($id{
  463.     
  464.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$id.'"]');
  465.             
  466.         if(count($xPathResultPage)>0{
  467.     
  468.             return stripslashes(trim((string)$xPathResultPage[0]->scriptfile));
  469.         }
  470.         else {
  471.             throw new CaramelException(10);
  472.         }
  473.     
  474.     // End of method declaration
  475.     
  476.     
  477.     
  478.     /**
  479.      * Method to return array with all pages and subpages
  480.      * Note: Navigation is restricted to one sublevel
  481.      *
  482.      * @param string $lang Current language
  483.      *
  484.      * @throws CaramelException
  485.      * @return Array with all pages
  486.      */
  487.     public function backendGetWebsitePagesAction($lang{
  488.     
  489.         $orderedNavi array();
  490.     
  491.         foreach($this->_dataBase->page as $page{
  492.                 
  493.             ## Get records
  494.             $xPathResultRecord $page->xpath('record[@lang="'.$lang.'"]');
  495.             
  496.             if(count($xPathResultRecord&& $xPathResultRecord!=false{
  497.                 
  498.                 $record $xPathResultRecord[0];
  499.                 
  500.                 $orderedNavi[(int)$page->attributes()->idarray(
  501.                     "path" => (string)$page->attributes()->path
  502.                     "visible" => (string)$record->navigation->attributes()->visible,
  503.                     "id" => (int)$page->attributes()->id,
  504.                     "navigation" => (string)$record->navigation,
  505.                 );
  506.     
  507.             else // Take first language 
  508.                 
  509.                 $xPathResultRecord $page->xpath('record');
  510.                     
  511.                 if(count($xPathResultRecord&& $xPathResultRecord!=false{
  512.                 
  513.                     $record $xPathResultRecord[0];
  514.                 
  515.                     $orderedNavi[(int)$page->attributes()->idarray(
  516.                         "path" => (string)$page->attributes()->path
  517.                         "visible" => (string)$record->navigation->attributes()->visible,
  518.                         "id" => (int)$page->attributes()->id,
  519.                         "navigation" => (string)$record->navigation,
  520.                     );
  521.                 
  522.                 else {
  523.                     throw new CaramelException(10);
  524.                 }
  525.             }
  526.             
  527.             $orderedNavi[(int)$page->attributes()->id]["subpages"array();
  528.             
  529.             
  530.             ## SubPages
  531.             $subPages $page->page;
  532.             
  533.             foreach($subPages as $subPage{
  534.     
  535.                 ## Get subrecords
  536.                 $xPathResultRecord $subPage->xpath('record[@lang="'.$lang.'"]');
  537.                             
  538.                 if(count($xPathResultRecord&& $xPathResultRecord!=false{
  539.                 
  540.                     $subRecord $xPathResultRecord[0];
  541.             
  542.                     $orderedNavi[(int)$page->attributes()->id]["subpages"][(int)$subPage->attributes()->idarray(
  543.                         "path" => (string)$subPage->attributes()->path,
  544.                         "visible" => (string)$subRecord->navigation->attributes()->visible,
  545.                         "id" => (int)$subPage->attributes()->id,
  546.                         "navigation" => (string)$subRecord->navigation,
  547.                     );
  548.     
  549.                 else // Take first language
  550.                     
  551.                     $xPathResultRecord $subPage->xpath('record');
  552.                                 
  553.                     if(count($xPathResultRecord&& $xPathResultRecord!=false{
  554.                     
  555.                         $subRecord $xPathResultRecord[0];
  556.                 
  557.                         $orderedNavi[(int)$page->attributes()->id]["subpages"][(int)$subPage->attributes()->idarray(
  558.                             "path" => (string)$subPage->attributes()->path,
  559.                             "visible" => (string)$subRecord->navigation->attributes()->visible,
  560.                             "id" => (int)$subPage->attributes()->id,
  561.                             "navigation" => (string)$subRecord->navigation,
  562.                         );
  563.         
  564.                     else {
  565.                         throw new CaramelException(10);
  566.                     }
  567.                 }
  568.             }
  569.         }
  570.     
  571.         return $orderedNavi;
  572.     
  573.     // End of method declaration
  574.     
  575.     
  576.     
  577.     /**
  578.      * Method to get all page information incl. records for one page-id
  579.      * 
  580.      * @param int $id Id from one specific page
  581.      * 
  582.      * @throws CaramelException
  583.      * @return Array with all information to page with $id
  584.      */
  585.     public function backendGetPageInformation($id{
  586.         
  587.         date_default_timezone_set('Europe/Berlin');
  588.         
  589.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$id.'"]');
  590.         
  591.         $versions array();
  592.         
  593.         $page array();
  594.         
  595.         $page["id"$id;
  596.         $page["path"]["label""URL path to page:";
  597.         $page["stylesheet"]["label""Additional CSS file (only filename, not path):";
  598.         $page["scriptfile"]["label""Additional JavaScript file (only filename, not path):";
  599.         
  600.         if(count($xPathResultPage)>0{
  601.             
  602.             $page["path"]["value"= (string)$xPathResultPage[0]["path"];
  603.             
  604.             $page["stylesheet"]["value"stripslashes(trim((string)$xPathResultPage[0]->stylesheet));
  605.             $page["scriptfile"]["value"stripslashes(trim((string)$xPathResultPage[0]->scriptfile));
  606.             
  607.             foreach($xPathResultPage[0]->record as $record{
  608.                 
  609.                 $lang = (string)$record->attributes()->lang;
  610.                 
  611.                 $page["records"][$lang]["visible"]["label""Visible in navigation:";
  612.                 $page["records"][$lang]["visible"]["value"stripslashes(trim((string)$record->navigation["visible"]));
  613.                 
  614.                 $page["records"][$lang]["navigation"]["label""Name used in navigation:";
  615.                 $page["records"][$lang]["navigation"]["value"stripslashes(trim((string)$record->navigation));
  616.                 
  617.                 $page["records"][$lang]["title"]["label""Website title:";
  618.                 $page["records"][$lang]["title"]["value"stripslashes(trim((string)$record->title));
  619.                 
  620.                 $page["records"][$lang]["titletag"]["label""Title-tag used in navigation:";
  621.                 $page["records"][$lang]["titletag"]["value"stripslashes(trim((string)$record->titletag));
  622.                 
  623.                 $page["records"][$lang]["metadescription"]["label""Description:";
  624.                 $page["records"][$lang]["metadescription"]["value"stripslashes(trim((string)$record->meta[0]));
  625.                 
  626.                 $page["records"][$lang]["metakeywords"]["label""Keywords:";
  627.                 $page["records"][$lang]["metakeywords"]["value"stripslashes(trim((string)$record->meta[1]));
  628.                 
  629.                 $page["records"][$lang]["metaauthor"]["label""Author:";
  630.                 $page["records"][$lang]["metaauthor"]["value"stripslashes(trim((string)$record->meta[2]));
  631.                 
  632.                 $page["records"][$lang]["socialbar"]["label""Activate Socialbar:";
  633.                 $page["records"][$lang]["socialbar"]["value"stripslashes(trim((string)$record->socialbar));
  634.                 
  635.                 $page["records"][$lang]["content"]["label""Page content:";
  636.                 $page["records"][$lang]["content"]["value"trim((string)$record->content);
  637.                 
  638.             }
  639.             
  640.             
  641.             foreach($xPathResultPage[0]->version as $version{
  642.             
  643.                 $lang = (string)$version->attributes()->lang;
  644.                 $timestamp intval($version->attributes()->timestamp);
  645.  
  646.                 $page["records"][$lang]["versions"]["label""Versions for this record:";
  647.                 $page["records"][$lang]["versions"]["timestamps"][$timestamp]["label""Version: ".strftime("%d.%m.%Y %H:%M:%S %Z"$timestamp);
  648.                 
  649.             }
  650.             
  651.             
  652.         }
  653.         else {
  654.             throw new CaramelException(10);
  655.         }
  656.         
  657.         return $page;
  658.         
  659.     // End of method declaration
  660.     
  661.     
  662.     
  663.     /**
  664.      * Method to get all page information incl. records for one page-id
  665.      *
  666.      * @param int $id Id from one specific page
  667.      * @param array $page Array with modified page information
  668.      *
  669.      * @throws CaramelException
  670.      * @return Result of file_put_contents
  671.      */
  672.     public function backendSetPageInformation($id$page{
  673.         
  674.         $this->createDatabaseBackup();
  675.         
  676.         $result false;
  677.         
  678.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$id.'"]');
  679.         
  680.         if(count($xPathResultPage)>0{
  681.             
  682.             $xPathResultPage[0]["path"$page["path"]["value"];
  683.             
  684.             # Set stylesheet and javascript file
  685.             $xPathResultPage[0]->stylesheet null;
  686.             $xPathResultPage[0]->stylesheet->addCData(stripslashes(trim($page["stylesheet"]["value"])));
  687.             
  688.             $xPathResultPage[0]->scriptfile null;
  689.             $xPathResultPage[0]->scriptfile->addCData(stripslashes(trim($page["scriptfile"]["value"])));
  690.             
  691.             $result file_put_contents(BASEDIR.'/database/data.xml'$this->_dataBase->asXML());
  692.             
  693.         }
  694.         else {
  695.             throw new CaramelException(10);
  696.         }
  697.         
  698.         
  699.         foreach($page["records"as $lang => $record{
  700.             
  701.             $xPathResultRecord $this->_dataBase->xpath('//page[@id="'.$id.'"]/record[@lang="'.$lang.'"]');
  702.             
  703.             if(count($xPathResultRecord)>0{
  704.                 
  705.                 if(stripslashes(trim($record["visible"]["value"])) == "true"{
  706.                     $visible "true";
  707.                 else {
  708.                     $visible "false";
  709.                 }
  710.                 
  711.                 $xPathResultRecord[0]->navigation null;
  712.                 $xPathResultRecord[0]->navigation->addCData(stripslashes(trim($record["navigation"]["value"])));
  713.                 $xPathResultRecord[0]->navigation->attributes()->visible $visible;
  714.                 $xPathResultRecord[0]->title null;
  715.                 $xPathResultRecord[0]->title->addCData(stripslashes(trim($record["title"]["value"])));
  716.                 $xPathResultRecord[0]->titletag null;
  717.                 $xPathResultRecord[0]->titletag->addCData(stripslashes(trim($record["titletag"]["value"])));
  718.                 $xPathResultRecord[0]->meta[0null;
  719.                 $xPathResultRecord[0]->meta[0]->addCData(stripslashes(trim($record["metadescription"]["value"])));
  720.                 $xPathResultRecord[0]->meta[1null;
  721.                 $xPathResultRecord[0]->meta[1]->addCData(stripslashes(trim($record["metakeywords"]["value"])));
  722.                 $xPathResultRecord[0]->meta[2null;
  723.                 $xPathResultRecord[0]->meta[2]->addCData(stripslashes(trim($record["metaauthor"]["value"])));
  724.                 $xPathResultRecord[0]->socialbar null;
  725.                 $xPathResultRecord[0]->socialbar->addCData(stripslashes(trim($record["socialbar"]["value"])));
  726.                 $xPathResultRecord[0]->content null;
  727.                 $xPathResultRecord[0]->content->addCData(stripslashes(trim($record["content"]["value"])));
  728.                 
  729.                 $result file_put_contents(BASEDIR.'/database/data.xml'$this->_dataBase->asXML());
  730.                 
  731.             }
  732.             else {
  733.                 throw new CaramelException(10);
  734.             }
  735.             
  736.         }
  737.         
  738.         return $result;
  739.         
  740.     // End of method declaration
  741.     
  742.     
  743.     
  744.     /**
  745.      * Method to delete a single page with all records
  746.      * 
  747.      * @param int $pageId Id of the page
  748.      * 
  749.      * @throws CaramelException
  750.      * @return Result of file_put_contents
  751.      */
  752.     public function backendDeletePageAction($pageId{
  753.         
  754.         $this->createDatabaseBackup();
  755.         
  756.         $result false;
  757.         
  758.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$pageId.'"]');
  759.         
  760.         if(count($xPathResultPage)>0{
  761.             
  762.             $xPathResultPage[0]->removeNode();
  763.             
  764.             $result file_put_contents(BASEDIR.'/database/data.xml'$this->_dataBase->asXML());
  765.             
  766.         }
  767.         else {
  768.             throw new CaramelException(10);
  769.         }
  770.         
  771.         return $result;
  772.         
  773.     // End of method declaration
  774.     
  775.     
  776.     
  777.     /**
  778.      * Method to create a new page with one default record
  779.      * 
  780.      * @param string $path URL path of the new page
  781.      * @param string $defaultLang Language for default record of the new page
  782.      * @param array $recordContents Array with contents of default record
  783.      * 
  784.      * @throws CaramelException
  785.      * @return Result of file_put_contents
  786.      */
  787.     public function backendCreatePageAction($path$defaultLang$recordContents{
  788.         
  789.         $this->createDatabaseBackup();
  790.         
  791.         $result false;
  792.         $idArray array();
  793.         
  794.         $xPathId $this->_dataBase->xpath("page/@id");
  795.                 
  796.         if(count($xPathId)>0{
  797.             
  798.             foreach($xPathId as $idNode{
  799.                 $idArray[= (int)$idNode["id"];
  800.             }
  801.             
  802.             $newID max($idArray)+1;
  803.             
  804.             
  805.             # Add page
  806.             $newPage $this->_dataBase->addChildCData("page");
  807.             
  808.             ## Set ID
  809.             $newPage->addAttribute("id"$newID);
  810.             
  811.             ## Set path
  812.             $newPage->addAttribute("path"$path);
  813.             
  814.             
  815.             # Add record
  816.             $newRecord $newPage->addChildCData("record");
  817.             
  818.             ## Set language
  819.             $newRecord->addAttribute("lang"$defaultLang);
  820.             
  821.             ## Add rest of children
  822.             $navigation $newRecord->addChildCData("navigation"$recordContents["navigation"]);
  823.             
  824.             if($recordContents["visible"]=="true"{
  825.                 $navigation->addAttribute("visible""true");
  826.             }
  827.             else {
  828.                 $navigation->addAttribute("visible""false");
  829.             }
  830.             
  831.             $newRecord->addChild("title"$recordContents["title"]);
  832.             
  833.             $newRecord->addChild("titletag"$recordContents["titletag"]);
  834.             
  835.             $metaDescription $newRecord->addChildCData("meta"$recordContents["metadescription"]);
  836.             $metaDescription->addAttribute("name""description");
  837.             
  838.             $metaKeywords $newRecord->addChildCData("meta"$recordContents["metakeywords"]);
  839.             $metaKeywords->addAttribute("name""keywords");
  840.             
  841.             $metaAuthor $newRecord->addChildCData("meta"$recordContents["metaauthor"]);
  842.             $metaAuthor->addAttribute("name""author");
  843.             
  844.             $newRecord->addChildCData("socialbar""false");
  845.             
  846.             $newRecord->addChildCData("content"$recordContents["content"]);
  847.             
  848.             $result file_put_contents(BASEDIR.'/database/data.xml'$this->_dataBase->asXML());
  849.             
  850.         }
  851.         else {
  852.             throw new CaramelException(10);
  853.         }
  854.         
  855.         return $result;
  856.         
  857.     // End of method declaration
  858.     
  859.     
  860.     
  861.     /**
  862.      * Wrapper method to move a node one up
  863.      * 
  864.      * @param int $id ID of the page-node
  865.      * 
  866.      * @throws CaramelException
  867.      * @return Result of the save action
  868.      */
  869.     public function backendMovePageUpAction($id{
  870.         
  871.         $this->createDatabaseBackup();
  872.         
  873.         $result false;
  874.         
  875.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$id.'"]');
  876.                 
  877.         if(count($xPathResultPage)>0{
  878.             
  879.             $xPathResultSibling $xPathResultPage[0]->xpath('preceding-sibling::*[1]');
  880.                 
  881.             if(count($xPathResultSibling)>0{
  882.                 
  883.                 $xPathResultPage[0]->moveNodeUp($xPathResultSibling[0]);
  884.                     
  885.                 $result file_put_contents(BASEDIR.'/database/data.xml'$this->_dataBase->asXML());
  886.             }
  887.                         
  888.         }
  889.         else {
  890.             throw new CaramelException(10);
  891.         }
  892.         
  893.         return $result;
  894.         
  895.     // End of method declaration
  896.     
  897.     
  898.     
  899.     /**
  900.      * Wrapper method to move a node one down
  901.      * 
  902.      * @param int $id ID of the page-node
  903.      * 
  904.      * @throws CaramelException
  905.      * @return Result of the save action
  906.      */
  907.     public function backendMovePageDownAction($id{
  908.         
  909.         $this->createDatabaseBackup();
  910.     
  911.         $result false;
  912.     
  913.         $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$id.'"]');
  914.     
  915.         if(count($xPathResultPage)>0{
  916.             
  917.             $xPathResultSibling $xPathResultPage[0]->xpath('following-sibling::*[1]');
  918.             
  919.             if(count($xPathResultSibling)>0{
  920.                 
  921.                 $xPathResultPage[0]->moveNodeDown($xPathResultSibling[0]);
  922.                 
  923.                 $result file_put_contents(BASEDIR.'/database/data.xml'$this->_dataBase->asXML());
  924.                 
  925.             }
  926.                         
  927.         }
  928.         else {
  929.             throw new CaramelException(10);
  930.         }
  931.         
  932.         return $result;
  933.     
  934.     // End of method declaration
  935.     
  936.     
  937.     
  938.     /**
  939.      * Method to return only all page paths
  940.      *
  941.      * @throws CaramelException
  942.      * @return Array with all page paths.
  943.      */
  944.     public function backendGetAllPageNamesAction({
  945.     
  946.         $pageNames array();
  947.     
  948.         $xPathResultPages $this->_dataBase->xpath("//page");
  949.     
  950.         if(count($xPathResultPages)>0{
  951.     
  952.             foreach($xPathResultPages as $page{
  953.                     
  954.                 $pageNames[array("path"=>(string)$page->attributes()->path"id"=>(int)$page->attributes()->id);
  955.     
  956.             }
  957.         }
  958.         else{
  959.             throw new CaramelException(10);
  960.         }
  961.     
  962.         return $pageNames;
  963.     
  964.     // End of method declaration
  965.     
  966.     
  967.     
  968.     /**
  969.      * Method to return a page ID for given page path
  970.      * 
  971.      * @param String $pagePath 
  972.      * @throws CaramelException
  973.      * 
  974.      * @return Integer of the page ID
  975.      */
  976.     public function getPageId($pagePath{
  977.         
  978.         if($pagePath != false{
  979.         
  980.             $xPathResultPage $this->_dataBase->xpath('//page[@path="'.$pagePath.'"]');
  981.                 
  982.             if(count($xPathResultPage)>0{
  983.             
  984.                 return (int)stripslashes(trim((string)$xPathResultPage[0]["id"]));
  985.             }
  986.             else {
  987.                 throw new CaramelException(10);
  988.             }
  989.             
  990.         else {
  991.             return false;
  992.         }
  993.         
  994.     // End of method declaration
  995.     
  996.     
  997.     
  998.     /**
  999.      * Method to return a page path for given page ID
  1000.      * 
  1001.      * @param String $pageId 
  1002.      * @throws CaramelException
  1003.      * 
  1004.      * @return String of the page path
  1005.      */
  1006.     public function getPagePath($pageId{
  1007.         
  1008.         if($pageId 0{
  1009.     
  1010.             $xPathResultPage $this->_dataBase->xpath('//page[@id="'.$pageId.'"]');
  1011.                 
  1012.             if(count($xPathResultPage)>0{
  1013.         
  1014.                 return (int)stripslashes(trim((string)$xPathResultPage[0]["path"]));
  1015.             }
  1016.             else {
  1017.                 throw new CaramelException(10);
  1018.             }
  1019.         
  1020.         else {
  1021.             return false;
  1022.         }
  1023.     
  1024.     // End of method declaration
  1025.     
  1026.     
  1027.     
  1028. ########################
  1029. ##
  1030. ## Helper methods
  1031. ##
  1032. ########################
  1033.  
  1034.     
  1035.     /**
  1036.      * Method to reload the database file
  1037.      * 
  1038.      * @throws CaramelException
  1039.      * @return void 
  1040.      */
  1041.     private function reloadDatabaseFile({
  1042.         
  1043.         # Try to import the database-file
  1044.         try {
  1045.             $this->_dataBase simplexml_load_file(BASEDIR.'/database/data.xml'"SimpleXMLExtended");
  1046.         }
  1047.         catch(Exception $e{
  1048.             throw new CaramelException(11);
  1049.         }
  1050.         
  1051.     // End of method declaration
  1052.     
  1053.     
  1054.     
  1055.     /**
  1056.      * Method to return string encoded with unicode entities for eMail spam protection
  1057.      * 
  1058.      * @param string $string String to encode
  1059.      * 
  1060.      * @return String of unicode entities representing the input string
  1061.      */
  1062.     private function plainToUnicode($string{
  1063.         
  1064.         $stringArray str_split($string);
  1065.         $stringConverted "";
  1066.         
  1067.         foreach($stringArray as $char{
  1068.             
  1069.             $stringConverted .= "&#".ord($char).";";
  1070.             
  1071.         }
  1072.         
  1073.         return $stringConverted;
  1074.         
  1075.     // End of method declaration
  1076.     
  1077.     
  1078.     
  1079.     /**
  1080.      * Method to create a backup of the xml database file
  1081.      */
  1082.     private function createDatabaseBackup({        
  1083.         
  1084.         $result file_put_contents(BASEDIR.'/database/data_'.date("Y-m-d-H-i-s"time()).'.xml'$this->_dataBase->asXML());
  1085.         
  1086.         if($result!=false{
  1087.             $this->reloadDatabaseFile();
  1088.         }
  1089.         
  1090.     // End of method declaration
  1091.     
  1092. }
  1093. ?>