home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / cms.class.inc < prev    next >
Text File  |  2004-03-08  |  26KB  |  882 lines

  1. <?php
  2. /*
  3. Copyright Intermesh 2003
  4. Author: Merijn Schering <mschering@intermesh.nl>
  5. Version: 1.0 Release date: 08 July 2003
  6.  
  7. This program is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11. */
  12.  
  13.  
  14. class cms extends db
  15. {
  16.     function cms()
  17.     {
  18.         $this->db();
  19.     }
  20.  
  21.     function resolve_url($path, $site_id)
  22.     {
  23.         $site = $this->get_site($site_id);
  24.  
  25.         $path = explode('/', $path);
  26.  
  27.         $last_folder_id = $site['root_folder_id'];
  28.         $count = count($path);
  29.         for($i=0;$i<$count;$i++)
  30.         {
  31.             if ($path[$i] != '')
  32.             {
  33.                 if(!$new_folder_id = $this->folder_exists($last_folder_id, $path[$i]))
  34.                 {
  35.                     $file_id = $this->file_exists($last_folder_id, $path[$i]);
  36.                     if(!$file_id = $this->file_exists($last_folder_id, $path[$i]))
  37.                     {
  38.                         echo $i.': '.$path[$i].' last folder id: '.$last_folder_id.'<br />';
  39.                         return false;
  40.                     }
  41.                 }else
  42.                 {
  43.                     $last_folder_id = $new_folder_id;
  44.                 }
  45.             }
  46.         }
  47.         $path['folder_id'] = $last_folder_id;
  48.         if (isset($file_id))
  49.         {
  50.             $path['file_id'] = $file_id;
  51.         }
  52.         return $path;
  53.  
  54.     }
  55.  
  56.     function move_file($file_id, $new_folder_id)
  57.     {
  58.         //if the name exists add (1) behind it.
  59.         if($file = $this->get_file($file_id))
  60.         {
  61.             $name = $file['name'];
  62.             $x=0;
  63.             while ($this->file_exists($new_folder_id, $name))
  64.             {
  65.                 $x++;
  66.                 $name = strip_extension($file['name']).' ('.$x.').'.get_extension($file['name']);
  67.             }
  68.             return $this->query("UPDATE cms_files SET folder_id='$new_folder_id', name='".smart_addslashes($name)."' WHERE id='$file_id'");
  69.         }
  70.         return false;
  71.     }
  72.  
  73.     function move_folder($folder_id, $new_parent_id)
  74.     {
  75.         if ($folder = $this->get_folder($folder_id))
  76.         {
  77.             if ($folder['parent_id'] != $new_parent_id)
  78.             {
  79.                 //don't move folders into thier own path
  80.                 if (!$this->is_in_path($folder_id, $new_parent_id))
  81.                 {
  82.                     //if the name exists add (1) behind it.
  83.                     $name = $folder['name'];
  84.                     $x=0;
  85.                     while ($this->folder_exists($new_parent_id, $name))
  86.                     {
  87.                         $x++;
  88.                         $name = $folder['name'].' ('.$x.')';
  89.                     }
  90.                     return $this->query("UPDATE cms_folders SET parent_id='$new_parent_id', name='$name' WHERE id='$folder_id'");
  91.                 }
  92.             }
  93.         }
  94.         return false;
  95.     }
  96.  
  97.     function copy_file($file_id, $new_folder_id)
  98.     {
  99.         //if the name exists add (1) behind it.
  100.         if($file = $this->get_file($file_id))
  101.         {
  102.             $name = $file['name'];
  103.             $x=0;
  104.             while ($this->file_exists($new_folder_id, $name))
  105.             {
  106.                 $x++;
  107.                 $name = $file['name'].' ('.$x.')';
  108.             }
  109.             return $this->add_file($new_folder_id, $name, addslashes($file['content']), $file['title'], $file['description'], $file['keywords']);
  110.         }
  111.         return false;
  112.     }
  113.  
  114.     function copy_folder($folder_id, $new_parent_id)
  115.     {
  116.         if ($folder = $this->get_folder($folder_id))
  117.         {
  118.             //don't move folders into thier own path
  119.             if (!$this->is_in_path($folder_id, $new_parent_id))
  120.             {
  121.                 //if the name exists add (1) behind it.
  122.                 $name = $folder['name'];
  123.                 $x=0;
  124.                 while ($this->folder_exists($new_parent_id, $name))
  125.                 {
  126.                     $x++;
  127.                     $name = $folder['name'].' ('.$x.')';
  128.                 }
  129.                 if($new_folder_id = $this->add_folder($new_parent_id, $name))
  130.                 {
  131.                     $cms = new cms();
  132.                     $this->get_files($folder_id);
  133.                     while($this->next_record())
  134.                     {
  135.                         if(!$cms->copy_file($this->f('id'), $new_folder_id))
  136.                         {
  137.                             return false;
  138.                         }
  139.                     }
  140.  
  141.                     $this->get_folders($folder_id);
  142.                     while($this->next_record())
  143.                     {
  144.                         if(!$this->copy_folder($this->f('id'),$new_folder_id))
  145.                         {
  146.                             return false;
  147.                         }
  148.                     }
  149.                     return true;
  150.                 }
  151.             }
  152.         }
  153.         return false;
  154.     }
  155.  
  156.     /*
  157.     Check if a folder is in the path of another folder.
  158.     This is used to check if we can move a folder into another.
  159.     */
  160.     function is_in_path($check_folder_id, $target_folder_id)
  161.     {
  162.         if($target_folder_id == 0)
  163.         {
  164.             return false;
  165.         }elseif ($target_folder_id == $check_folder_id)
  166.         {
  167.             return true;
  168.         }else
  169.         {
  170.             $folder = $this->get_folder($target_folder_id);
  171.             return $this->is_in_path($check_folder_id, $folder['parent_id']);
  172.         }
  173.     }
  174.  
  175.     function file_exists($folder_id, $filename)
  176.     {
  177.         $this->query("SELECT id FROM cms_files WHERE folder_id='$folder_id' AND name='".smart_addslashes($filename)."'");
  178.         if ($this->next_record())
  179.         {
  180.             return $this->f('id');
  181.         }
  182.         return false;
  183.     }
  184.  
  185.     //builds the full path to this directory with links
  186.     function get_path($folder_id)
  187.     {
  188.         $path = '';
  189.  
  190.         while($folder = $this->get_folder($folder_id))
  191.         {
  192.             $path = '/'.$folder['name'].$path;
  193.             $folder_id = $folder['parent_id'];
  194.         }
  195.         return $path;
  196.     }
  197.  
  198.     //functions for personal sites
  199.     function add_site($user_id, $name, $acl_read, $acl_write, $description, $keywords, $template_id)
  200.     {
  201.         //create a root folder for the new site
  202.         if ($root_folder_id = $this->add_folder(0, $name))
  203.         {
  204.             //create the site
  205.             $site_id = $this->nextid('cms_sites');
  206.             if ($site_id > 0)
  207.             {
  208.                 if($this->query("INSERT INTO cms_sites (id, user_id, root_folder_id, name, acl_read, acl_write, description, keywords, template_id) VALUES ('$site_id', '$user_id', '$root_folder_id', '".smart_addslashes($name)."', '$acl_read', '$acl_write', '".smart_addslashes($description)."', '".smart_addslashes($keywords)."', '$template_id')"))
  209.                 {
  210.                     return $site_id;
  211.                 }
  212.             }else
  213.             {
  214.                 //on error clean up
  215.                 $this->delete_folder($folder_id);
  216.             }
  217.         }
  218.         return false;
  219.     }
  220.  
  221.     function delete_site($site_id)
  222.     {
  223.         if($site = $this->get_site($site_id))
  224.         {
  225.             if ($this->delete_folder($site['root_folder_id']))
  226.             {
  227.                 if($this->query("DELETE FROM cms_sites WHERE id='$site_id'"))
  228.                 {
  229.                     global $GO_SECURITY;
  230.                     $GO_SECURITY->delete_acl($site['acl_read']);
  231.                     $GO_SECURITY->delete_acl($site['acl_write']);
  232.                     return true;
  233.                 }
  234.             }
  235.         }
  236.         return false;
  237.     }
  238.  
  239.     function update_site($site_id, $name, $description, $keywords, $template_id, $acl_read)
  240.     {
  241.         return $this->query("UPDATE cms_sites SET name='".smart_addslashes($name)."', description='".smart_addslashes($description)."', keywords='".smart_addslashes($keywords)."', template_id='$template_id', acl_read='$acl_read' WHERE id='$site_id'");
  242.     }
  243.  
  244.     function set_publishing($site_id, $publish_style, $publish_path)
  245.     {
  246.         return $this->query("UPDATE cms_sites SET publish_style='$publish_style', publish_path='$publish_path' WHERE id='$site_id'");
  247.     }
  248.  
  249.     function get_site_by_publish_path($path)
  250.     {
  251.         $this->query("SELECT * FROM cms_sites WHERE publish_path='$path'");
  252.         if ($this->next_record())
  253.         {
  254.             return $this->Record;
  255.         }else
  256.         {
  257.             return false;
  258.         }
  259.     }
  260.  
  261.     function get_site($site_id)
  262.     {
  263.         $this->query("SELECT * FROM cms_sites WHERE id='$site_id'");
  264.         if ($this->next_record())
  265.         {
  266.             return $this->Record;
  267.         }
  268.         return false;
  269.     }
  270.  
  271.     function get_site_by_name($name)
  272.     {
  273.         $this->query("SELECT * FROM cms_sites WHERE name='".smart_addslashes($name)."'");
  274.         if ($this->next_record())
  275.         {
  276.             return $this->Record;
  277.         }
  278.         return false;
  279.     }
  280.  
  281.     function get_sites()
  282.     {
  283.         $this->query("SELECT * FROM cms_sites");
  284.         return $this->num_rows();
  285.     }
  286.  
  287.     function get_authorized_sites($user_id)
  288.     {
  289.         $sql = "SELECT DISTINCT cms_sites.* FROM cms_sites, acl, users_groups WHERE ".
  290.                 "cms_sites.acl_write = acl.acl_id".
  291.                 " AND (( acl.group_id = users_groups.group_id AND users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  292.                 "acl.group_id = 0 AND acl.user_id = ".$user_id." ) )";
  293.         $this->query($sql);
  294.         return $this->num_rows();
  295.     }
  296.  
  297.     function get_subscribed_sites($user_id)
  298.     {
  299.         $this->query("SELECT cms_sites.* FROM cms_sites LEFT JOIN cms_subscribed ON (cms_subscribed.site_id=cms_sites.id) WHERE cms_subscribed.user_id='$user_id' ORDER BY cms_sites.name ASC");
  300.         return $this->num_rows();
  301.     }
  302.  
  303.  
  304.     function subscribe_site($user_id, $site_id)
  305.     {
  306.         return $this->query("INSERT INTO cms_subscribed (user_id, site_id) VALUES ('$user_id', '$site_id')");
  307.     }
  308.  
  309.     function unsubscribe_site($user_id, $site_id)
  310.     {
  311.         return $this->query("DELETE FROM cms_subscribed WHERE user_id='$user_id' AND site_id='$site_id'");
  312.     }
  313.  
  314.     function is_subscribed($user_id, $site_id)
  315.     {
  316.         $this->query("SELECT * FROM cms_subscribed WHERE user_id='$user_id' AND site_id='$site_id'");
  317.         return $this->next_record();
  318.     }
  319.  
  320.  
  321.     //functions for files and folders of sites
  322.     function get_files($folder_id, $sort = "name", $direction = "ASC")
  323.     {
  324.         $sql = "SELECT cms_files.*, filetypes.friendly FROM cms_files INNER JOIN filetypes ON filetypes.extension=cms_files.extension WHERE folder_id='$folder_id' ORDER BY $sort $direction";
  325.         $this->query($sql);
  326.         return $this->num_rows();
  327.     }
  328.  
  329.     function get_visible_folders($folder_id)
  330.     {
  331.         $this->query("SELECT * FROM cms_folders WHERE parent_id='$folder_id' AND disabled='0' ORDER BY priority ASC");
  332.         return $this->num_rows();
  333.     }
  334.  
  335.     function get_folders($folder_id, $sort = "name", $direction = "ASC")
  336.     {
  337.         $this->query("SELECT * FROM cms_folders WHERE parent_id='$folder_id' ORDER BY '$sort' $direction");
  338.         return $this->num_rows();
  339.     }
  340.  
  341.     function add_folder($parent_id, $name, $priority=0)
  342.     {
  343.         $folder_id = $this->nextid('cms_folders');
  344.         if ($folder_id > 0)
  345.         {
  346.             $mtime = get_gmt_time();
  347.             if($this->query("INSERT INTO cms_folders (id, parent_id, name, mtime, priority) VALUES ('$folder_id', '$parent_id', '".smart_addslashes($name)."', '$mtime', '$priority')"))
  348.             {
  349.                 return $folder_id;
  350.             }
  351.         }
  352.         return false;
  353.     }
  354.  
  355.     function get_folder($folder_id)
  356.     {
  357.         $this->query("SELECT * FROM cms_folders WHERE id='$folder_id';");
  358.         if ($this->next_record())
  359.         {
  360.             return $this->Record;
  361.         }
  362.         return false;
  363.  
  364.     }
  365.  
  366.     function update_folder($folder_id, $name, $disabled, $priority)
  367.     {
  368.         return $this->query("UPDATE cms_folders SET name='".smart_addslashes($name)."', mtime='".get_gmt_time()."', disabled='$disabled', priority='$priority' WHERE id='$folder_id'");
  369.     }
  370.  
  371.     function folder_exists($parent_id, $name)
  372.     {
  373.         $this->query("SELECT id FROM cms_folders WHERE parent_id='$parent_id' AND name='".smart_addslashes($name)."'");
  374.         if($this->next_record())
  375.         {
  376.             return $this->f('id');
  377.         }
  378.         return false;
  379.     }
  380.  
  381.     function delete_folder($folder_id)
  382.     {
  383.         if ($folder_id > 0)
  384.         {
  385.             //add a second cms object for simultanious select and delete from the db
  386.             $cms2 = new cms();
  387.  
  388.             //get all folders
  389.             $this->get_folders($folder_id);
  390.             while($this->next_record())
  391.             {
  392.                 if (!$cms2->delete_folder($this->f('id')))
  393.                 {
  394.                     return false;
  395.                 }
  396.             }
  397.  
  398.             $this->get_files($folder_id);
  399.             while ($this->next_record())
  400.             {
  401.                 if(!$cms2->delete_file($this->f('id')))
  402.                 {
  403.                     return false;
  404.                 }
  405.             }
  406.  
  407.             return $this->query("DELETE FROM cms_folders WHERE id='$folder_id'");
  408.         }else
  409.         {
  410.             return false;
  411.         }
  412.     }
  413.  
  414.     function delete_file($file_id)
  415.     {
  416.         return $this->query("DELETE FROM cms_files WHERE id='$file_id'");
  417.     }
  418.  
  419.     function get_file($file_id)
  420.     {
  421.         $this->query("SELECT cms_files.*, filetypes.mime AS content_type, filetypes.friendly AS content_type_friendly FROM cms_files LEFT JOIN filetypes ON (filetypes.extension=cms_files.extension) WHERE id='$file_id'");
  422.         if ($this->next_record())
  423.         {
  424.             return $this->Record;
  425.         }
  426.         return false;
  427.     }
  428.  
  429.     function add_file($folder_id, $name, $content, $title='', $description='', $keywords='', $priority='0')
  430.     {
  431.         $file_id = $this->nextid('cms_files');
  432.         if ($file_id > 0)
  433.         {
  434.             $size = strlen($content);
  435.             $extension = get_extension($name);
  436.             $mtime = get_gmt_time();
  437.  
  438.             $sql  = "INSERT INTO cms_files (id, folder_id, name, extension, mtime, size, content, title, description, keywords, priority) ";
  439.             $sql .= "VALUES ('$file_id', '$folder_id', '".smart_addslashes($name)."', '$extension', '$mtime', '$size', '$content', '".smart_addslashes($title)."', '".smart_addslashes($description)."', '".smart_addslashes($keywords)."', '$priority')";
  440.             if ($this->query($sql))
  441.             {
  442.                 return $file_id;
  443.             }
  444.         }
  445.         return false;
  446.     }
  447.  
  448.     function update_file($file_id, $name, $content, $title='', $description='', $keywords='', $priority='0')
  449.     {
  450.         $size = strlen($content);
  451.         $extension = get_extension($name);
  452.         $mtime = get_gmt_time();
  453.         return $this->query("UPDATE cms_files SET name='".smart_addslashes($name)."', extension='$extension', mtime='$mtime', size='$size', content='$content', title='".smart_addslashes($title)."', description='".smart_addslashes($description)."', keywords='".smart_addslashes($keywords)."', priority='$priority' WHERE id='$file_id'");
  454.     }
  455.  
  456.     //template functions
  457.  
  458.     function add_template($user_id, $name, $style, $additional_style, $restrict_editor, $acl_read, $acl_write)
  459.     {
  460.         //create the template
  461.         $template_id = $this->nextid('cms_templates');
  462.         if ($template_id > 0)
  463.         {
  464.             $restrict_editor = ($restrict_editor == "true") ? '1' : '0';
  465.  
  466.             if($this->query("INSERT INTO cms_templates (id, user_id, name, style, additional_style, restrict_editor, acl_read, acl_write) VALUES ('$template_id', '$user_id', '".smart_addslashes($name)."', '".smart_addslashes($style)."', '".smart_addslashes($additional_style)."', '$restrict_editor', '$acl_read', '$acl_write')"))
  467.             {
  468.                 return $template_id;
  469.             }
  470.         }
  471.  
  472.         return false;
  473.     }
  474.  
  475.     function update_template($template_id, $name, $style, $additional_style, $restrict_editor)
  476.     {
  477.         $restrict_editor = ($restrict_editor == "true") ? '1' : '0';
  478.         return $this->query("UPDATE cms_templates SET restrict_editor='$restrict_editor', name='".smart_addslashes($name)."', style='".smart_addslashes($style)."', additional_style='".smart_addslashes($additional_style)."' WHERE id='$template_id'");
  479.     }
  480.  
  481.     function get_templates()
  482.     {
  483.         $this->query("SELECT * FROM cms_templates");
  484.         return $this->num_rows();
  485.     }
  486.  
  487.     function get_authorized_templates($user_id)
  488.     {
  489.  
  490.         $sql = "SELECT DISTINCT cms_templates.* FROM cms_templates, acl, users_groups WHERE ".
  491.                         "(cms_templates.acl_write = acl.acl_id OR cms_templates.acl_read = acl.acl_id)".
  492.                         " AND (( acl.group_id = users_groups.group_id AND users_groups.user_id = ".$user_id." AND acl.user_id = 0 ) OR (".
  493.                         "acl.group_id = 0 AND acl.user_id = ".$user_id." ) )";
  494.         $this->query($sql);
  495.         return $this->num_rows();
  496.     }
  497.  
  498.     function get_template($template_id)
  499.     {
  500.         $this->query("SELECT * FROM cms_templates WHERE id='$template_id'");
  501.         if ($this->next_record())
  502.         {
  503.             return $this->Record;
  504.         }
  505.         return false;
  506.     }
  507.  
  508.     function get_template_by_name($user_id, $name)
  509.     {
  510.         $this->query("SELECT * FROM cms_templates WHERE user_id='$user_id' AND name='".smart_addslashes($name)."'");
  511.         if ($this->next_record())
  512.         {
  513.             return $this->Record;
  514.         }
  515.         return false;
  516.     }
  517.  
  518.     function delete_template($template_id)
  519.     {
  520.         if($this->query("DELETE FROM cms_template_items WHERE template_id='$template_id'"))
  521.         {
  522.             return $this->query("DELETE FROM cms_templates WHERE id='$template_id'");
  523.         }
  524.         return false;
  525.  
  526.     }
  527.  
  528.     function add_template_item($template_id, $name, $content)
  529.     {
  530.         //create the template
  531.         $template_item_id = $this->nextid('cms_template_items');
  532.         if ($template_item_id > 0)
  533.         {
  534.             if($this->query("INSERT INTO cms_template_items (id, template_id, name, content) VALUES ('$template_item_id', '$template_id', '".smart_addslashes($name)."', '".smart_addslashes($content)."')"))
  535.             {
  536.                 return $template_item_id;
  537.             }
  538.         }
  539.  
  540.         return false;
  541.     }
  542.  
  543.     function update_template_item($template_item_id, $name, $content)
  544.     {
  545.         return $this->query("UPDATE cms_template_items SET name='".smart_addslashes($name)."',content='".smart_addslashes($content)."' WHERE id='$template_item_id'");
  546.     }
  547.  
  548.     function get_template_items($template_id)
  549.     {
  550.         $this->query("SELECT * FROM cms_template_items WHERE template_id='$template_id'");
  551.         return $this->num_rows();
  552.     }
  553.  
  554.     function get_template_item($template_item_id)
  555.     {
  556.         $this->query("SELECT * FROM cms_template_items WHERE id='$template_item_id'");
  557.         if ($this->next_record())
  558.         {
  559.             return $this->Record;
  560.         }
  561.         return false;
  562.     }
  563.  
  564.     function delete_template_item($template_item_id)
  565.     {
  566.         return $this->query("DELETE FROM cms_template_items WHERE id='$template_item_id'");
  567.     }
  568.     function get_template_item_by_name($template_id, $name)
  569.     {
  570.         $this->query("SELECT * FROM cms_template_items WHERE template_id='$template_id' AND name='".smart_addslashes($name)."'");
  571.         if ($this->next_record())
  572.         {
  573.             return $this->Record;
  574.         }
  575.         return false;
  576.     }
  577.  
  578.     function set_main_template_item($template_id, $template_item_id)
  579.     {
  580.         $this->query("UPDATE cms_template_items SET main='0' WHERE template_id='$template_id' AND main='1'");
  581.         $this->query("UPDATE cms_template_items SET main='1' WHERE template_id='$template_id' AND id='$template_item_id'");
  582.     }
  583.  
  584.     function get_main_template_item($template_id)
  585.     {
  586.         $this->query("SELECT * FROM cms_template_items WHERE template_id='$template_id' AND main='1'");
  587.         if ($this->next_record())
  588.         {
  589.             return $this->Record;
  590.         }
  591.         return false;
  592.     }
  593.  
  594.     function get_template_files($template_id)
  595.     {
  596.         $this->query("SELECT cms_template_files.*, filetypes.mime AS content_type, filetypes.friendly AS content_type_friendly FROM cms_template_files LEFT JOIN filetypes ON (filetypes.extension=cms_template_files.extension) WHERE template_id='$template_id'");
  597.         return $this->num_rows();
  598.     }
  599.  
  600.     function add_template_file($template_id, $name, $content)
  601.     {
  602.         $template_file_id = $this->nextid('cms_template_files');
  603.         if ($template_file_id > 0)
  604.         {
  605.             $size = strlen($content);
  606.             $extension = get_extension($name);
  607.             $mtime = get_gmt_time();
  608.             return $this->query("INSERT INTO cms_template_files (id, template_id, name, extension, size, mtime, content) VALUES ('$template_file_id','$template_id','".smart_addslashes($name)."','$extension','$size','$mtime','$content')");
  609.         }
  610.         return false;
  611.     }
  612.  
  613.     function get_template_file($template_file_id)
  614.     {
  615.         $this->query("SELECT cms_template_files.*, filetypes.mime AS content_type, filetypes.friendly AS content_type_friendly FROM cms_template_files LEFT JOIN filetypes ON (filetypes.extension=cms_template_files.extension) WHERE id='$template_file_id'");
  616.         if ($this->next_record())
  617.         {
  618.             return $this->Record;
  619.         }
  620.         return false;
  621.     }
  622.  
  623.     function template_file_exists($template_id, $name)
  624.     {
  625.         $this->query("SELECT id FROM cms_template_files WHERE template_id='$template_id' AND name='".smart_addslashes($name)."'");
  626.         if ($this->next_record())
  627.         {
  628.             return $this->f('id');
  629.         }
  630.         return false;
  631.     }
  632.  
  633.     function delete_template_file($template_file_id)
  634.     {
  635.         return $this->query("DELETE FROM cms_template_files WHERE id='$template_file_id'");
  636.     }
  637.  
  638.     function add_search_word($site_id, $search_word)
  639.     {
  640.         //create the template
  641.         $search_word_id = $this->nextid('cms_search_words');
  642.         if ($search_word_id > 0)
  643.         {
  644.             if($this->query("INSERT INTO cms_search_words VALUES ('$search_word_id', '$site_id', '".smart_addslashes($search_word)."')"))
  645.             {
  646.                 return $search_word_id;
  647.             }
  648.         }
  649.         return false;
  650.     }
  651.  
  652.     function delete_search_word($search_word_id)
  653.     {
  654.         if($this->query("DELETE FROM cms_search_files WHERE search_word_id='$search_word_id'"))
  655.         {
  656.             return $this->query("DELETE FROM cms_search_words WHERE id='$search_word_id'");
  657.         }
  658.     }
  659.  
  660.     function update_search_word($search_word_id, $search_word)
  661.     {
  662.         return $this->query("UPDATE cms_search_words SET search_word='".smart_addslashes($search_word)."' WHERE id='$search_word_id'");
  663.     }
  664.  
  665.     function get_search_word($search_word_id)
  666.     {
  667.         $this->query("SELECT * FROM cms_search_words WHERE id='$search_word_id'");
  668.         if ($this->next_record())
  669.         {
  670.             return $this->Record;
  671.         }
  672.         return false;
  673.     }
  674.  
  675.  
  676.     function get_search_words($site_id)
  677.     {
  678.         $this->query("SELECT * FROM cms_search_words WHERE site_id='$site_id' ORDER BY search_word ASC");
  679.         return $this->num_rows();
  680.     }
  681.  
  682.     function get_search_word_by_name($site_id, $search_word)
  683.     {
  684.         $this->query("SELECT * FROM cms_search_words WHERE site_id='$site_id' AND search_word='".smart_addslashes($search_word)."'");
  685.         if ($this->next_record())
  686.         {
  687.             return $this->Record;
  688.         }
  689.         return false;
  690.     }
  691.  
  692.     function file_is_attached($file_id, $search_word_id)
  693.     {
  694.         $this->query("SELECT * FROM cms_search_files WHERE search_word_id='$search_word_id' AND file_id='$file_id'");
  695.         return $this->next_record();
  696.     }
  697.  
  698.     function attach_file($file_id, $search_word_id)
  699.     {
  700.         return $this->query("INSERT INTO cms_search_files (search_word_id, file_id) VALUES ('$search_word_id', '$file_id')");
  701.     }
  702.  
  703.     function detach_file($file_id, $search_word_id)
  704.     {
  705.         return $this->query("DELETE FROM cms_search_files WHERE file_id='$file_id' AND search_word_id='$search_word_id'");
  706.     }
  707.  
  708.     function search_files($folder_id, $search_word_id, $attach_results=true)
  709.     {
  710.         $cms = new cms();
  711.         $search_word = $this->get_search_word($search_word_id);
  712.         $files = array();
  713.         $sql = "SELECT id FROM cms_files WHERE extension='html' AND folder_id='$folder_id' AND (UPPER(content) REGEXP '[[:<:]]".strtoupper($search_word['search_word'])."[[:>:]]' OR UPPER(content) REGEXP '[[:<:]]".htmlentities(strtoupper($search_word['search_word']))."[[:>:]]')";
  714.         $this->query($sql);
  715.         while ($this->next_record())
  716.         {
  717.             if ($attach_results && !$cms->file_is_attached($this->f('id'), $search_word_id))
  718.             {
  719.                 $cms->attach_file($this->f('id'), $search_word_id);
  720.             }
  721.             $files[] = $this->f('id');
  722.  
  723.         }
  724.  
  725.         $this->get_folders($folder_id);
  726.         while($this->next_record())
  727.         {
  728.             $files = array_merge($files, $cms->search_files($this->f('id'), $search_word_id, $attach_results));
  729.         }
  730.         return $files;
  731.     }
  732.  
  733.     function get_attached_files($search_word_id)
  734.     {
  735.         $this->query("SELECT cms_files.* FROM cms_files LEFT JOIN cms_search_files ON (cms_search_files.file_id=cms_files.id) WHERE cms_search_files.search_word_id='$search_word_id'");
  736.         return $this->num_rows();
  737.     }
  738.  
  739.     function get_attached_search_words($file_id)
  740.     {
  741.         $this->query("SELECT cms_search_words.* FROM cms_search_words LEFT JOIN cms_search_files ON (cms_search_files.search_word_id=cms_search_words.id) WHERE cms_search_files.file_id='$file_id'");
  742.         return $this->num_rows();
  743.     }
  744.  
  745.     function get_body($html)
  746.     {
  747.         $to_removed_array = array (
  748.                     "'<html[^>]*>'si",
  749.                     "'</html>'si",
  750.                     "'<body[^>]*>'si",
  751.                     "'</body>'si",
  752.                     "'<head[^>]*>.*?</head>'si",
  753.                     "'<style[^>]*>.*?</style>'si",
  754.                     "'<object[^>]*>.*?</object>'si",
  755.                 );
  756.         $html = preg_replace($to_removed_array, '', $html);
  757.         //$html = preg_replace("/class\s*=\s*[\"']?.*?[\"']?\s/is", '', $html);
  758.         //$html = preg_replace("/style\s*=\s*[\"'].*?[\"']\s/is", '', $html);
  759.         //werkt min of meer$html = preg_replace("/class.*?\s/is", '', $html);
  760.         //$html = preg_replace("/style.*?\s/is", '', $html);
  761.  
  762.  
  763.         return $html;
  764.  
  765.     }
  766.  
  767.     function remove_styles($html)
  768.     {
  769.         $to_removed_array = array (
  770.             "'class=[\w]*\b'si",
  771.             "'class=\"[^\"]*\"'si",
  772.             "'class=\'[^\']*\''si",
  773.             "'style=\"[^\"]*\"'si",
  774.             "'style=\'[^\']*\''si",
  775.         );
  776.         //$html = preg_replace("/class\s*=\s*.*?\s/is", '', $html);
  777.         //$html = preg_replace("/class\s*=\s*.*?\s/is", '', $html);
  778.         $html = preg_replace($to_removed_array, '', $html);
  779.         return $html;
  780.     }
  781.  
  782.  
  783.     function extract_styles($style)
  784.     {
  785.         $styles = array();
  786.         $style_name = '';
  787.         $in_style = true;
  788.  
  789.         $style = str_replace("\r", '', $style);
  790.         $style = str_replace("\n", '', $style);
  791.         $style = str_replace(' ', '', $style);
  792.  
  793.         for ($i=0; $i<strlen($style); $i++)
  794.         {
  795.             $char = $style[$i];
  796.  
  797.             if ($char == '{' || $char == ',')
  798.             {
  799.                 $in_style = false;
  800.                 if (trim($style_name) != '')
  801.                 {
  802.                     $styles[] = $style_name;
  803.                 }
  804.                 $style_name = '';
  805.             }elseif($char == '.')
  806.             {
  807.                 if ($style_name != '')
  808.                 {
  809.                     $style_name = '';
  810.                     $in_style = false;
  811.                 }else
  812.                 {
  813.                     $in_style = true;
  814.                 }
  815.             }elseif($char == ':')
  816.             {
  817.                 $style_name = '';
  818.                 $in_style = false;
  819.             }elseif($char == '}')
  820.             {
  821.                 $in_style = true;
  822.                 $style_name = '';
  823.             }else
  824.             {
  825.                 if ($in_style)
  826.                 {
  827.                     $style_name .= $char;
  828.                 }
  829.             }
  830.         }
  831.  
  832.         return $styles;
  833.     }
  834.  
  835.     function clean_up_html($html)
  836.     {
  837.         // remove escape slashes
  838.         $html = stripslashes($html);
  839.  
  840.         // strip tags, still leaving attributes, second variable is allowable tags
  841.         $html = strip_tags($html, '<p><b><br><img><i><u><a><h1><h2><h3><h4><h4><h5><h6><table><tr><td>');
  842.  
  843.         /*
  844.             // removes the attributes for allowed tags, use separate replace for heading tags since a
  845.             // heading tag is two characters
  846.             $html = ereg_replace("<([p|b|i|u])[^>]*>", "<\\1>", $html);
  847.             $html = ereg_replace("<([h1|h2|h3|h4|h5|h6][1-6])[^>]*>", "<\\1>", $html);
  848.         */
  849.         // remove all class and style attributes from the remaining tags
  850.         $to_removed_array = array (
  851.             "'class=[\w]*\b'si",
  852.             "'class=\"[^\"]*\"'si",
  853.             "'class=\'[^\']*\''si",
  854.             "'style=\"[^\"]*\"'si",
  855.             "'style=\'[^\']*\''si",
  856.         );
  857.         //$html = preg_replace("/class\s*=\s*.*?\s/is", '', $html);
  858.         //$html = preg_replace("/class\s*=\s*.*?\s/is", '', $html);
  859.         $html = preg_replace($to_removed_array, '', $html);
  860.         return $html;
  861.     }
  862.  
  863.     function delete_user($user_id)
  864.     {
  865.         $cms = new cms();
  866.         $sql = "SELECT id FROM cms_sites WHERE user_id='$user_id'";
  867.         $this->query($sql);
  868.         while($this->next_record())
  869.         {
  870.             $cms->delete_site($this->f('id'));
  871.         }
  872.  
  873.         $sql = "SELECT id FROM cms_templates WHERE user_id='$user_id'";
  874.         $this->query($sql);
  875.         while($this->next_record())
  876.         {
  877.             $cms->delete_template($this->f('id'));
  878.         }
  879.  
  880.     }
  881. }
  882. ?>