Advertisement
Guest User

dbpages.class.php

a guest
Apr 23rd, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.38 KB | None | 0 0
  1. <?php defined('BLUDIT') or die('Bludit CMS.');
  2.  
  3. class dbPages extends dbJSON
  4. {
  5.     private $parentKeyList = array();
  6.  
  7.     private $dbFields = array(
  8.         'title'=>       array('inFile'=>true,   'value'=>''),
  9.         'content'=>     array('inFile'=>true,   'value'=>''),
  10.         'description'=>     array('inFile'=>false,  'value'=>''),
  11.         'username'=>        array('inFile'=>false,  'value'=>''),
  12.         'tags'=>        array('inFile'=>false,  'value'=>array()),
  13.         'status'=>      array('inFile'=>false,  'value'=>'draft'),
  14.         'date'=>        array('inFile'=>false,  'value'=>''),
  15.         'position'=>        array('inFile'=>false,  'value'=>0),
  16.         'coverImage'=>      array('inFile'=>false,  'value'=>''),
  17.         'checksum'=>        array('inFile'=>false,  'value'=>'')
  18.     );
  19.  
  20.     function __construct()
  21.     {
  22.         parent::__construct(PATH_DATABASES.'pages.php');
  23.     }
  24.  
  25.     public function add($args)
  26.     {
  27.         $dataForDb = array();   // This data will be saved in the database
  28.         $dataForFile = array(); // This data will be saved in the file
  29.  
  30.         $key = $this->generateKey($args['slug'], $args['parent']);
  31.  
  32.         // The user is always the one loggued.
  33.         $args['username'] = Session::get('username');
  34.         if( Text::isEmpty($args['username']) ) {
  35.             return false;
  36.         }
  37.  
  38.         // Current date.
  39.         $args['date'] = Date::current(DB_DATE_FORMAT);
  40.  
  41.         // Verify arguments with the database fields.
  42.         foreach($this->dbFields as $field=>$options)
  43.         {
  44.             if( isset($args[$field]) )
  45.             {
  46.                 if($field=='tags') {
  47.                     $tmpValue = $this->generateTags($args['tags']);
  48.                 }
  49.                 else {
  50.                     // Sanitize if will be saved on database.
  51.                     if( !$options['inFile'] ) {
  52.                         $tmpValue = Sanitize::html($args[$field]);
  53.                     }
  54.                     else {
  55.                         $tmpValue = $args[$field];
  56.                     }
  57.                 }
  58.             }
  59.             // Default value for the field.
  60.             else
  61.             {
  62.                 $tmpValue = $options['value'];
  63.             }
  64.  
  65.             // Check where the field will be written, in file or database.
  66.             if($options['inFile']) {
  67.                 $dataForFile[$field] = Text::firstCharUp($field).': '.$tmpValue;
  68.             }
  69.             else
  70.             {
  71.                 // Set type
  72.                 settype($tmpValue, gettype($options['value']));
  73.  
  74.                 // Save on database
  75.                 $dataForDb[$field] = $tmpValue;
  76.             }
  77.         }
  78.  
  79.         // Create Hash
  80.         $serialize = serialize($dataForDb+$dataForFile);
  81.         $dataForDb['checksum'] = sha1($serialize);
  82.  
  83.         // Make the directory. Recursive.
  84.         if( Filesystem::mkdir(PATH_PAGES.$key, true) === false ) {
  85.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to create the directory '.PATH_PAGES.$key);
  86.             return false;
  87.         }
  88.  
  89.         // Make the index.txt and save the file.
  90.         $data = implode("\n", $dataForFile);
  91.         if( file_put_contents(PATH_PAGES.$key.'/index.txt', $data) === false ) {
  92.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file index.txt');
  93.             return false;
  94.         }
  95.  
  96.         // Save the database
  97.         $this->db[$key] = $dataForDb;
  98.         if( $this->save() === false ) {
  99.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
  100.             return false;
  101.         }
  102.  
  103.         return $key;
  104.     }
  105.  
  106.     public function edit($args)
  107.     {
  108.         $dataForDb = array();
  109.         $dataForFile = array();
  110.  
  111.         $newKey = $this->generateKey($args['slug'], $args['parent'], false, $args['key']);
  112.  
  113.         // The user is always the one loggued.
  114.         $args['username'] = Session::get('username');
  115.         if( Text::isEmpty($args['username']) ) {
  116.             return false;
  117.         }
  118.  
  119.         // If the page is draft then the time created is now.
  120.         if( $this->db[$args['key']]['status']=='draft' ) {
  121.             $args['date'] = Date::current(DB_DATE_FORMAT);
  122.         }
  123.         else {
  124.             $args['date'] = $this->db[$args['key']]['date'];
  125.         }
  126.  
  127.         // Verify arguments with the database fields.
  128.         foreach($this->dbFields as $field=>$options)
  129.         {
  130.             if( isset($args[$field]) )
  131.             {
  132.                 if($field=='tags') {
  133.                     $tmpValue = $this->generateTags($args['tags']);
  134.                 }
  135.                 else {
  136.                     // Sanitize if will be saved on database.
  137.                     if( !$options['inFile'] ) {
  138.                         $tmpValue = Sanitize::html($args[$field]);
  139.                     }
  140.                     else {
  141.                         $tmpValue = $args[$field];
  142.                     }
  143.                 }
  144.             }
  145.             // Default value for the field.
  146.             else
  147.             {
  148.                 $tmpValue = $options['value'];
  149.             }
  150.  
  151.             // Check where the field will be written, if in the file or in the database.
  152.             if($options['inFile']) {
  153.                 $dataForFile[$field] = Text::firstCharUp($field).': '.$tmpValue;
  154.             }
  155.             else
  156.             {
  157.                 // Set type
  158.                 settype($tmpValue, gettype($options['value']));
  159.  
  160.                 // Save on database
  161.                 $dataForDb[$field] = $tmpValue;
  162.             }
  163.         }
  164.  
  165.         // Create Hash
  166.         $serialize = serialize($dataForDb+$dataForFile);
  167.         $dataForDb['checksum'] = sha1($serialize);
  168.  
  169.         // Move the directory from old key to new key.
  170.         if($newKey!==$args['key'])
  171.         {
  172.             if( Filesystem::mv(PATH_PAGES.$args['key'], PATH_PAGES.$newKey) === false ) {
  173.                 Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to move the directory to '.PATH_PAGES.$newKey);
  174.                 return false;
  175.             }
  176.         }
  177.  
  178.         // Make the index.txt and save the file.
  179.         $data = implode("\n", $dataForFile);
  180.         if( file_put_contents(PATH_PAGES.$newKey.DS.'index.txt', $data) === false ) {
  181.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to put the content in the file index.txt');
  182.             return false;
  183.         }
  184.  
  185.         // Remove the old key.
  186.         unset($this->db[$args['key']]);
  187.  
  188.         // Save the database
  189.         $this->db[$newKey] = $dataForDb;
  190.         if( $this->save() === false ) {
  191.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
  192.             return false;
  193.         }
  194.  
  195.         return $newKey;
  196.     }
  197.  
  198.     public function delete($key)
  199.     {
  200.         // Page doesn't exist in database.
  201.         if(!$this->pageExists($key)) {
  202.             Log::set(__METHOD__.LOG_SEP.'The page does not exist. Key: '.$key);
  203.         }
  204.  
  205.         // Delete the index.txt file.
  206.         if( Filesystem::rmfile(PATH_PAGES.$key.DS.'index.txt') === false ) {
  207.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the file index.txt');
  208.         }
  209.  
  210.         // Delete the directory.
  211.         if( Filesystem::rmdir(PATH_PAGES.$key) === false ) {
  212.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to delete the directory '.PATH_PAGES.$key);
  213.         }
  214.  
  215.         // Remove from database.
  216.         unset($this->db[$key]);
  217.  
  218.         // Save the database.
  219.         if( $this->save() === false ) {
  220.             Log::set(__METHOD__.LOG_SEP.'Error occurred when trying to save the database file.');
  221.         }
  222.  
  223.         return true;
  224.     }
  225.  
  226.     // Return an array with the database for a page, FALSE otherwise.
  227.     public function getPageDB($key)
  228.     {
  229.         if($this->pageExists($key)) {
  230.             return $this->db[$key];
  231.         }
  232.  
  233.         return false;
  234.     }
  235.  
  236.     public function setPageDb($key, $field, $value)
  237.     {
  238.         if($this->pageExists($key)) {
  239.             $this->db[$key][$field] = $value;
  240.         }
  241.  
  242.         return false;
  243.     }
  244.  
  245.     // Return TRUE if the page exists, FALSE otherwise.
  246.     public function pageExists($key)
  247.     {
  248.         return isset($this->db[$key]);
  249.     }
  250.  
  251.     public function parentKeyList()
  252.     {
  253.         return $this->parentKeyList;
  254.     }
  255.  
  256.     public function parentKeyExists($key)
  257.     {
  258.         return isset($this->parentKeyList[$key]);
  259.     }
  260.  
  261.     public function addParentKey($key)
  262.     {
  263.         $this->parentKeyList[$key] = $key;
  264.     }
  265.  
  266.     // Generate a valid Key/Slug.
  267.     public function generateKey($text, $parent=NO_PARENT_CHAR, $returnSlug=false, $oldKey='')
  268.     {
  269.         if(Text::isEmpty($text)) {
  270.             $text = 'empty';
  271.         }
  272.  
  273.         if( Text::isEmpty($parent) || ($parent==NO_PARENT_CHAR) ) {
  274.             $newKey = Text::cleanUrl($text);
  275.         }
  276.         else {
  277.             $newKey = Text::cleanUrl($parent).'/'.Text::cleanUrl($text);
  278.         }
  279.  
  280.         if($newKey!==$oldKey)
  281.         {
  282.             // Verify if the key is already been used.
  283.             if( isset($this->db[$newKey]) )
  284.             {
  285.                 if( !Text::endsWithNumeric($newKey) ) {
  286.                     $newKey = $newKey.'-0';
  287.                 }
  288.  
  289.                 while( isset($this->db[$newKey]) ) {
  290.                     $newKey++;
  291.                 }
  292.             }
  293.         }
  294.  
  295.         if($returnSlug)
  296.         {
  297.             $explode = explode('/', $newKey);
  298.  
  299.             if(isset($explode[1])) {
  300.                 return $explode[1];
  301.             }
  302.  
  303.             return $explode[0];
  304.         }
  305.  
  306.         return $newKey;
  307.     }
  308.  
  309.     // Returns the database
  310.     public function getDB()
  311.     {
  312.         return $this->db;
  313.     }
  314.  
  315.     // Returns an Array, array('tagSlug'=>'tagName')
  316.     // (string) $tags, tag list separeted by comma.
  317.     public function generateTags($tags)
  318.     {
  319.         $tmp = array();
  320.  
  321.         $tags = trim($tags);
  322.  
  323.         if(empty($tags)) {
  324.             return $tmp;
  325.         }
  326.  
  327.         // Make array
  328.         $tags = explode(',', $tags);
  329.  
  330.         foreach($tags as $tag)
  331.         {
  332.             $tag = trim($tag);
  333.             $tagKey = Text::cleanUrl($tag);
  334.             $tmp[$tagKey] = $tag;
  335.         }
  336.  
  337.         return $tmp;
  338.     }
  339.  
  340.     public function count()
  341.     {
  342.         $count = parent::count();
  343.  
  344.         // DEBUG: Less than - 1 because the error page.
  345.         return $count - 1;
  346.     }
  347.  
  348.     public function regenerateCli()
  349.     {
  350.         return false;
  351.     }
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement