home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / class / xoopscomments.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  15.7 KB  |  349 lines

  1. <?php
  2. // $Id: xoopscomments.php 1099 2007-10-19 01:08:14Z dugris $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31. if (!defined('XOOPS_ROOT_PATH')) {
  32.     exit();
  33. }
  34. include_once XOOPS_ROOT_PATH."/class/xoopstree.php";
  35. require_once XOOPS_ROOT_PATH.'/class/xoopsobject.php';
  36. include_once XOOPS_ROOT_PATH.'/language/'.$GLOBALS['xoopsConfig']['language'].'/comment.php';
  37.  
  38. class XoopsComments extends XoopsObject
  39. {
  40.     var $ctable;
  41.     var $db;
  42.  
  43.     function XoopsComments($ctable, $id=null)
  44.     {
  45.         $this->ctable = $ctable;
  46.         $this->db =& Database::getInstance();
  47.         $this->XoopsObject();
  48.         $this->initVar('comment_id', XOBJ_DTYPE_INT, null, false);
  49.         $this->initVar('item_id', XOBJ_DTYPE_INT, null, false);
  50.         $this->initVar('order', XOBJ_DTYPE_INT, null, false);
  51.         $this->initVar('mode', XOBJ_DTYPE_OTHER, null, false);
  52.         $this->initVar('subject', XOBJ_DTYPE_TXTBOX, null, false, 255);
  53.         $this->initVar('comment', XOBJ_DTYPE_TXTAREA, null, false, null);
  54.         $this->initVar('ip', XOBJ_DTYPE_OTHER, null, false);
  55.         $this->initVar('pid', XOBJ_DTYPE_INT, 0, false);
  56.         $this->initVar('date', XOBJ_DTYPE_INT, null, false);
  57.         $this->initVar('nohtml', XOBJ_DTYPE_INT, 1, false);
  58.         $this->initVar('nosmiley', XOBJ_DTYPE_INT, 0, false);
  59.         $this->initVar('noxcode', XOBJ_DTYPE_INT, 0, false);
  60.         $this->initVar('user_id', XOBJ_DTYPE_INT, null, false);
  61.         $this->initVar('icon', XOBJ_DTYPE_OTHER, null, false);
  62.         $this->initVar('prefix', XOBJ_DTYPE_OTHER, null, false);
  63.         if ( !empty($id) ) {
  64.             if ( is_array($id) ) {
  65.                 $this->assignVars($id);
  66.             } else {
  67.                 $this->load(intval($id));
  68.             }
  69.         }
  70.     }
  71.  
  72.     function load($id)
  73.     {
  74.         $id = intval($id);
  75.         $sql = "SELECT * FROM ".$this->ctable." WHERE comment_id=".$id."";
  76.         $arr = $this->db->fetchArray($this->db->query($sql));
  77.         $this->assignVars($arr);
  78.     }
  79.  
  80.     function store()
  81.     {
  82.         if ( !$this->cleanVars() ) {
  83.             return false;
  84.         }
  85.         foreach ( $this->cleanVars as $k=>$v ) {
  86.             $$k = $v;
  87.         }
  88.         $isnew = false;
  89.         if ( empty($comment_id ) ) {
  90.             $isnew = true;
  91.             $comment_id = $this->db->genId($this->ctable."_comment_id_seq");
  92.             $sql = sprintf("INSERT INTO %s (comment_id, pid, item_id, date, user_id, ip, subject, comment, nohtml, nosmiley, noxcode, icon) VALUES (%u, %u, %u, %u, %u, '%s', '%s', '%s', %u, %u, %u, '%s')", $this->ctable, $comment_id, $pid, $item_id, time(), $user_id, $ip, $subject, $comment, $nohtml, $nosmiley, $noxcode, $icon);
  93.         } else {
  94.             $sql = sprintf("UPDATE %s SET subject = '%s', comment = '%s', nohtml = %u, nosmiley = %u, noxcode = %u, icon = '%s'  WHERE comment_id = %u", $this->ctable, $subject, $comment, $nohtml, $nosmiley, $noxcode, $icon, $comment_id);
  95.         }
  96.         if ( !$result = $this->db->query($sql) ) {
  97.             //echo $sql;
  98.             return false;
  99.         }
  100.         if ( empty($comment_id) ) {
  101.             $comment_id = $this->db->getInsertId();
  102.         }
  103.         if ( $isnew != false ) {
  104.             $sql = sprintf("UPDATE %s SET posts = posts+1 WHERE uid = %u", $this->db->prefix("users"), $user_id);
  105.             if (!$result = $this->db->query($sql)) {
  106.                 echo "Could not update user posts.";
  107.             }
  108.         }
  109.         return $comment_id;
  110.     }
  111.  
  112.     function delete()
  113.     {
  114.         $sql = sprintf("DELETE FROM %s WHERE comment_id = %u", $this->ctable, $this->getVar('comment_id'));
  115.         if ( !$result = $this->db->query($sql) ) {
  116.             return false;
  117.         }
  118.         $sql = sprintf("UPDATE %s SET posts = posts-1 WHERE uid = %u", $this->db->prefix("users"), $this->getVar("user_id"));
  119.         if ( !$result = $this->db->query($sql) ) {
  120.             echo "Could not update user posts.";
  121.         }
  122.         $mytree = new XoopsTree($this->ctable, "comment_id", "pid");
  123.         $arr = $mytree->getAllChild($this->getVar("comment_id"), "comment_id");
  124.         $size = count($arr);
  125.         if ( $size > 0 ) {
  126.             for ( $i = 0; $i < $size; $i++ ) {
  127.                 $sql = sprintf("DELETE FROM %s WHERE comment_bid = %u", $this->ctable, $arr[$i]['comment_id']);
  128.                 if ( !$result = $this->db->query($sql) ) {
  129.                     echo "Could not delete comment.";
  130.                 }
  131.                 $sql = sprintf("UPDATE %s SET posts = posts-1 WHERE uid = %u", $this->db->prefix("users"), $arr[$i]['user_id']);
  132.                 if ( !$result = $this->db->query($sql) ) {
  133.                     echo "Could not update user posts.";
  134.                 }
  135.             }
  136.         }
  137.         return ($size + 1);
  138.     }
  139.  
  140.     function getCommentTree()
  141.     {
  142.         $mytree = new XoopsTree($this->ctable, "comment_id", "pid");
  143.         $ret = array();
  144.         $tarray = $mytree->getChildTreeArray($this->getVar("comment_id"), "comment_id");
  145.         foreach ( $tarray as $ele ) {
  146.             $ret[] = new XoopsComments($this->ctable,$ele);
  147.         }
  148.         return $ret;
  149.     }
  150.  
  151.     function getAllComments($criteria=array(), $asobject=true, $orderby="comment_id ASC", $limit=0, $start=0)
  152.     {
  153.         $ret = array();
  154.         $where_query = "";
  155.         if ( is_array($criteria) && count($criteria) > 0 ) {
  156.             $where_query = " WHERE";
  157.             foreach ( $criteria as $c ) {
  158.                 $where_query .= " $c AND";
  159.             }
  160.             $where_query = substr($where_query, 0, -4);
  161.         }
  162.         if ( !$asobject ) {
  163.             $sql = "SELECT comment_id FROM ".$this->ctable."$where_query ORDER BY $orderby";
  164.             $result = $this->db->query($sql,$limit,$start);
  165.             while ( $myrow = $this->db->fetchArray($result) ) {
  166.                 $ret[] = $myrow['comment_id'];
  167.             }
  168.         } else {
  169.             $sql = "SELECT * FROM ".$this->ctable."".$where_query." ORDER BY $orderby";
  170.             $result = $this->db->query($sql,$limit,$start);
  171.             while ( $myrow = $this->db->fetchArray($result) ) {
  172.                 $ret[] = new XoopsComments($this->ctable,$myrow);
  173.             }
  174.         }
  175.         //echo $sql;
  176.         return $ret;
  177.     }
  178.  
  179.     /* Methods below will be moved to maybe another class? */
  180.     function printNavBar($item_id, $mode="flat", $order=1)
  181.     {
  182.         global $xoopsConfig, $xoopsUser;
  183.         echo "<form method='get' action='".$_SERVER['PHP_SELF']."'><table width='100%' border='0' cellspacing='1' cellpadding='2'><tr><td class='bg1' align='center'><select name='mode'><option value='nocomments'";
  184.         if ( $mode == "nocomments" ) {
  185.             echo " selected='selected'";
  186.         }
  187.         echo ">". _NOCOMMENTS ."</option><option value='flat'";
  188.         if ($mode == 'flat') {
  189.             echo " selected='selected'";
  190.         }
  191.         echo ">". _FLAT ."</option><option value='thread'";
  192.         if ( $mode == "thread" || $mode == "" ) {
  193.             echo " selected='selected'";
  194.         }
  195.         echo ">". _THREADED ."</option></select><select name='order'><option value='0'";
  196.         if ( $order != 1 ) {
  197.             echo " selected='selected'";
  198.         }
  199.         echo ">". _OLDESTFIRST ."</option><option value='1'";
  200.         if ( $order == 1 ) {
  201.             echo " selected='selected'";
  202.         }
  203.         echo ">". _NEWESTFIRST ."</option></select><input type='hidden' name='item_id' value='".intval($item_id)."' /><input type='submit' value='". _CM_REFRESH ."' />";
  204.         if ( $xoopsConfig['anonpost'] == 1 || $xoopsUser ) {
  205.             if ($mode != "flat" || $mode != "nocomments" || $mode != "thread" ) {
  206.                 $mode = "flat";
  207.             }
  208.             echo " <input type='button' onclick='location=\"newcomment.php?item_id=".intval($item_id)."&order=".intval($order)."&mode=".$mode."\"' value='"._CM_POSTCOMMENT."' />";
  209.         }
  210.         echo "</td></tr></table></form>";
  211.     }
  212.  
  213.     function showThreadHead()
  214.     {
  215.         openThread();
  216.     }
  217.  
  218.     function showThreadPost($order, $mode, $adminview=0, $color_num=1)
  219.     {
  220.         global $xoopsConfig, $xoopsUser;
  221.         $edit_image = "";
  222.         $reply_image = "";
  223.         $delete_image = "";
  224.         $post_date = formatTimestamp($this->getVar("date"),"m");
  225.         if ( $this->getVar("user_id") != 0 ) {
  226.             $poster = new XoopsUser($this->getVar("user_id"));
  227.             if ( !$poster->isActive() ) {
  228.                 $poster = 0;
  229.             }
  230.         } else {
  231.             $poster = 0;
  232.         }
  233.         if ( $this->getVar("icon") != null && $this->getVar("icon") != "" ) {
  234.             $subject_image = "<a name='".$this->getVar("comment_id")."' id='".$this->getVar("comment_id")."'></a><img src='".XOOPS_URL."/images/subject/".$this->getVar("icon")."' alt='' />";
  235.         } else {
  236.             $subject_image =  "<a name='".$this->getVar("comment_id")."' id='".$this->getVar("comment_id")."'></a><img src='".XOOPS_URL."/images/icons/no_posticon.gif' alt='' />";
  237.         }
  238.         if ( $adminview ) {
  239.             $ip_image = "<img src='".XOOPS_URL."/images/icons/ip.gif' alt='".$this->getVar("ip")."' />";
  240.         } else {
  241.             $ip_image = "<img src='".XOOPS_URL."/images/icons/ip.gif' alt='' />";
  242.         }
  243.         if ( $adminview || ($xoopsUser && $this->getVar("user_id") == $xoopsUser->getVar("uid")) ) {
  244.             $edit_image = "<a href='editcomment.php?comment_id=".$this->getVar("comment_id")."&mode=".$mode."&order=".intval($order)."'><img src='".XOOPS_URL."/images/icons/edit.gif' alt='"._EDIT."' /></a>";
  245.         }
  246.         if ( $xoopsConfig['anonpost'] || $xoopsUser ) {
  247.             $reply_image = "<a href='replycomment.php?comment_id=".$this->getVar("comment_id")."&mode=".$mode."&order=".intval($order)."'><img src='".XOOPS_URL."/images/icons/reply.gif' alt='"._REPLY."' /></a>";
  248.         }
  249.         if ( $adminview ) {
  250.             $delete_image = "<a href='deletecomment.php?comment_id=".$this->getVar("comment_id")."&mode=".$mode."&order=".intval($order)."'><img src='".XOOPS_URL."/images/icons/delete.gif' alt='"._DELETE."' /></a>";
  251.         }
  252.  
  253.         if ( $poster ) {
  254.             $text = $this->getVar("comment");
  255.             if ( $poster->getVar("attachsig") ) {
  256.                 $text .= "<p><br />_________________<br />". $poster->user_sig()."</p>";
  257.             }
  258.             $reg_date = _CM_JOINED;
  259.             $reg_date .= formatTimestamp($poster->getVar("user_regdate"),"s");
  260.             $posts = _CM_POSTS;
  261.             $posts .= $poster->getVar("posts");
  262.             $user_from = _CM_FROM;
  263.             $user_from .= $poster->getVar("user_from");
  264.             $rank = $poster->rank();
  265.             if ( $rank['image'] != "" ) {
  266.                 $rank['image'] = "<img src='".XOOPS_UPLOAD_URL."/".$rank['image']."' alt='' />";
  267.             }
  268.             $avatar_image = "<img src='".XOOPS_UPLOAD_URL."/".$poster->getVar("user_avatar")."' alt='' />";
  269.             if ( $poster->isOnline() ) {
  270.                 $online_image = "<span style='color:#ee0000;font-weight:bold;'>"._ONLINE."</span>";
  271.             } else {
  272.                 $online_image = "";
  273.             }
  274.             $profile_image = "<a href='".XOOPS_URL."/userinfo.php?uid=".$poster->getVar("uid")."'><img src='".XOOPS_URL."/images/icons/profile.gif' alt='"._PROFILE."' /></a>";
  275.             if ( $xoopsUser ) {
  276.                 $pm_image =  "<a href='javascript:openWithSelfMain(\"".XOOPS_URL."/pmlite.php?send2=1&to_userid=".$poster->getVar("uid")."\",\"pmlite\",450,370);'><img src='".XOOPS_URL."/images/icons/pm.gif' alt='".sprintf(_SENDPMTO,$poster->getVar("uname", "E"))."' /></a>";
  277.             } else {
  278.                 $pm_image = "";
  279.             }
  280.                if ( $poster->getVar("user_viewemail") ) {
  281.                 $email_image = "<a href='mailto:".$poster->getVar("email", "E")."'><img src='".XOOPS_URL."/images/icons/email.gif' alt='".sprintf(_SENDEMAILTO,$poster->getVar("uname", "E"))."' /></a>";
  282.             } else {
  283.                 $email_image = "";
  284.             }
  285.             $posterurl = $poster->getVar("url");
  286.                if ( $posterurl != "" ) {
  287.                 $www_image = "<a href='$posterurl' target='_blank'><img src='".XOOPS_URL."/images/icons/www.gif' alt='"._VISITWEBSITE."' /></a>";
  288.             } else {
  289.                 $www_image = "";
  290.             }
  291.                if ( $poster->getVar("user_icq") != "" ) {
  292.                 $icq_image = "<a href='http://wwp.icq.com/scripts/search.dll?to=".$poster->getVar("user_icq", "E")."'><img src='".XOOPS_URL."/images/icons/icq_add.gif' alt='"._ADD."' /></a>";
  293.             } else {
  294.                 $icq_image = "";
  295.             }
  296.             if ( $poster->getVar("user_aim") != "" ) {
  297.                 $aim_image = "<a href='aim:goim?screenname=".$poster->getVar("user_aim", "E")."&message=Hi+".$poster->getVar("user_aim")."+Are+you+there?'><img src='".XOOPS_URL."/images/icons/aim.gif' alt='aim' /></a>";
  298.             } else {
  299.                 $aim_image = "";
  300.             }
  301.                if ( $poster->getVar("user_yim") != "" ) {
  302.                 $yim_image = "<a href='http://edit.yahoo.com/config/send_webmesg?.target=".$poster->getVar("user_yim", "E")."&.src=pg'><img src='".XOOPS_URL."/images/icons/yim.gif' alt='yim' /></a>";
  303.             } else {
  304.                 $yim_image = "";
  305.             }
  306.             if ( $poster->getVar("user_msnm") != "" ) {
  307.                 $msnm_image = "<a href='".XOOPS_URL."/userinfo.php?uid=".$poster->getVar("uid")."'><img src='".XOOPS_URL."/images/icons/msnm.gif' alt='msnm' /></a>";
  308.             } else {
  309.                 $msnm_image = "";
  310.             }
  311.             showThread($color_num, $subject_image, $this->getVar("subject"), $text, $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $poster->getVar("uname"), $rank['title'], $rank['image'], $avatar_image, $reg_date, $posts, $user_from, $online_image, $profile_image, $pm_image, $email_image, $www_image, $icq_image, $aim_image, $yim_image, $msnm_image);
  312.         } else {
  313.             showThread($color_num, $subject_image, $this->getVar("subject"), $this->getVar("comment"), $post_date, $ip_image, $reply_image, $edit_image, $delete_image, $xoopsConfig['anonymous']);
  314.         }
  315.     }
  316.  
  317.     function showThreadFoot()
  318.     {
  319.         closeThread();
  320.     }
  321.  
  322.     function showTreeHead($width="100%")
  323.     {
  324.         echo "<table border='0' class='outer' cellpadding='0' cellspacing='0' align='center' width='$width'><tr class='bg3' align='center'><td colspan='3'>". _CM_REPLIES ."</td></tr><tr class='bg3' align='left'><td width='60%' class='fg2'>". _CM_TITLE ."</td><td width='20%' class='fg2'>". _CM_POSTER ."</td><td class='fg2'>". _CM_POSTED ."</td></tr>";
  325.     }
  326.  
  327.     function showTreeItem($order, $mode, $color_num)
  328.     {
  329.         if ( $color_num == 1 ) {
  330.             $bg = 'even';
  331.         } else {
  332.             $bg = 'odd';
  333.         }
  334.         $prefix = str_replace(".", "    ", $this->getVar("prefix"));
  335.         $date = formatTimestamp($this->getVar("date"),"m");
  336.         if ( $this->getVar("icon") != "" ) {
  337.             $icon = "subject/".$this->getVar("icon", "E");
  338.         } else {
  339.             $icon = "icons/no_posticon.gif";
  340.         }
  341.         echo "<tr class='$bg' align='left'><td>".$prefix."<img src='".XOOPS_URL."/images/".$icon."'> <a href='".$_SERVER['PHP_SELF']."?item_id=".$this->getVar("item_id")."&comment_id=".$this->getVar("comment_id")."&mode=".$mode."&order=".$order."#".$this->getVar("comment_id")."'>".$this->getVar("subject")."</a></td><td><a href='".XOOPS_URL."/userinfo.php?uid=".$this->getVar("user_id")."'>".XoopsUser::getUnameFromId($this->getVar("user_id"))."</a></td><td>".$date."</td></tr>";
  342.     }
  343.  
  344.     function showTreeFoot()
  345.     {
  346.         echo "</table><br />";
  347.     }
  348. }
  349. ?>