pearweb
[ class tree: pearweb ] [ index: pearweb ] [ all elements ]

Source for file ManualNotes.class.php

Documentation is available at ManualNotes.class.php

  1. <?php
  2. /*
  3.    +----------------------------------------------------------------------+
  4.    | PEAR Web site version 1.0                                            |
  5.    +----------------------------------------------------------------------+
  6.    | Copyright (c) 2001-2007 The PHP Group                                |
  7.    +----------------------------------------------------------------------+
  8.    | This source file is subject to version 2.02 of the PHP license,      |
  9.    | that is bundled with this package in the file LICENSE, and is        |
  10.    | available at through the world-wide-web at                           |
  11.    | http://www.php.net/license/2_02.txt.                                 |
  12.    | If you did not receive a copy of the PHP license and are unable to   |
  13.    | obtain it through the world-wide-web, please send a note to          |
  14.    | license@php.net so we can mail you a copy immediately.               |
  15.    +----------------------------------------------------------------------+
  16.    | Authors: David Coallier <davidc@php.net>                             |
  17.    |                                                                      |
  18.    +----------------------------------------------------------------------+
  19.  */
  20. // {{{ class Manual_Notes
  21. /**
  22.  * Manual Notes
  23.  *
  24.  * This class will be handling most of the
  25.  * manual notes adding, deleting, approving, etc
  26.  *
  27.  * @package pearweb
  28.  * @author  David Coallier <davidc@php.net>
  29.  * @uses    DB
  30.  * @version 1.0
  31.  */
  32. {
  33.     // {{{ properties
  34.     /**
  35.      * Database Connection
  36.      *
  37.      * This variables holds the database connection
  38.      * into a variable.
  39.      *
  40.      * @access protected
  41.      * @var    Object    $dbc  Database Connection
  42.      */
  43.     var $dbc;
  44.  
  45.     /**
  46.      * Notes table
  47.      *
  48.      * This is the variable that holds
  49.      * the name of the manual notes table.
  50.      *
  51.      * @access protected
  52.      * @var    string    $notesTableName The notes table name
  53.      */
  54.     var $notesTableName = 'manual_notes';
  55.  
  56.     // }}}
  57.     // {{{ php5 Constructor
  58.     function __construct()
  59.     {
  60.         global $dbh;
  61.         $this->dbc = $dbh;
  62.     }
  63.     // }}}
  64.     // {{{ public function addComment
  65.     /**
  66.      * Add a comment
  67.      *
  68.      * This function will add a comment to the database
  69.      * using the credentials passed to it.
  70.      *
  71.      * @access public
  72.      * @param  string $pageUrl  The page url
  73.      * @param  string $userName The user adding the comment
  74.      * @param  string $note     The note to add
  75.      * @param  string $approved Is it approved ? "Default: pending"
  76.      */
  77.     function addComment($pageUrl$userName$note$approved 'pending')
  78.     {
  79.         $user = isset($GLOBALS['auth_user']$GLOBALS['auth_user']->handle : '';
  80.         if ($user{
  81.             $sql = "
  82.                 INSERT INTO {$this->notesTableName}
  83.                 (page_url, user_name, user_handle, note_text, note_time,
  84.                  note_approved, note_approved_by, note_deleted)
  85.                 VALUES (?, ?, ?, ?, NOW(), ?, ?, 0)
  86.             ";
  87.  
  88.             // always approve pear.dev account holder comments, moderate others
  89.             $res = $this->dbc->query($sqlarray($pageUrl$userName$user$note,
  90.                 auth_check('pear.dev''yes' $approved,
  91.                 auth_check('pear.dev'$user ''));
  92.         } else {
  93.             $sql = "
  94.                 INSERT INTO {$this->notesTableName}
  95.                 (page_url, user_name, user_handle, note_text, note_time,
  96.                  note_approved, note_approved_by, note_deleted)
  97.                 VALUES (?, ?, ?, ?, NOW(), ?, null, 0)
  98.             ";
  99.  
  100.             $res = $this->dbc->query($sqlarray($pageUrl$userName$user$note$approved));
  101.         }
  102.  
  103.         if (PEAR::isError($res)) {
  104.             return $res;
  105.         }
  106.  
  107.         $this->_compileComment($this->dbc->getOne('SELECT LAST_INSERT_ID()')$note);
  108.         return true;
  109.     }
  110.     // }}}
  111.     function _compileComment($id, $note)
  112.     {
  113.         $split = explode('<?php', $note);
  114.         if (count($split) == 1) {
  115.             // no PHP code
  116.             $compiled = nl2br(htmlspecialchars($note));
  117.         } else {
  118.             // compile PHP code
  119.             $compiled = nl2br(htmlspecialchars(array_shift($split)));
  120.             foreach ($split as $segment) {
  121.                 $segment = explode('?>', $segment);
  122.                 $compiled .= highlight_string('<?php' . $segment[0] . '?>', true);
  123.                 if (isset($segment[1])) {
  124.                     $compiled .= nl2br(htmlspecialchars($segment[1]));
  125.                 }
  126.             }
  127.         }
  128.         $sql = 'UPDATE ' . $this->notesTableName . ' SET note_compiled = ? WHERE note_id = ?';
  129.         $this->dbc->query($sqlarray($compiled$id));
  130.     }
  131.     // {{{ public function getSingleCommentById
  132.     /**
  133.      * Get a single comment by id
  134.      *
  135.      * This function will retrieve the single
  136.      * comment's information by note_id.
  137.      *
  138.      * @access public
  139.      * @param  integer  $noteId  The note id to retrieve
  140.      * @return mixed    $res     Error on query fail and associative
  141.      *                           array on success.
  142.      */
  143.     function getSingleCommentById($noteId)
  144.     {
  145.         $sql = "
  146.             SELECT note_id, page_url, user_name, user_handle,
  147.             note_compiled as note_text, note_time, note_approved,
  148.             note_approved_by, note_deleted
  149.              FROM {$this->notesTableName}
  150.               WHERE note_id = ?";
  151.         $res = $this->dbc->getRow($sqlarray($noteId)DB_FETCHMODE_ASSOC);
  152.         if (PEAR::isError($res)) {
  153.             return $res;
  154.         }
  155.  
  156.         return $res;
  157.     }
  158.     // }}}
  159.     // {{{ public function getPageComments
  160.     /**
  161.      * Get Page Comments
  162.      *
  163.      * This function will get the comments depending
  164.      * on whether a method will need approved, unapproved
  165.      * pending comments, etc. (Per manual page)
  166.      *
  167.      * @access public
  168.      * @param  string      $url    The url of the comments
  169.      * @param  string|bool $status The status of the comment.. whether
  170.      *                             it's approved, unapproved, pending.  If
  171.      *                             a boolean is passed in, determine whether to
  172.      *                             display approved/pending, or just approved
  173.      * @param  bool        $all    if true, return all comments matching this status
  174.      *
  175.      * @return mixed  $res    It returns an error object if there was an error
  176.      *                        executing the query, will return an empty array
  177.      *                        if there was nothing returned from the query, or
  178.      *                        this will return an associative array of the comments
  179.      *                        per page.
  180.      */
  181.     function getPageComments($url, $status = '1', $all = false)
  182.     {
  183.  
  184.         if ($all) {
  185.             $sql = "
  186.                 SELECT note_id, page_url, user_name, user_handle,
  187.                     note_compiled as note_text, note_time, note_approved,
  188.                     note_approved_by, note_deleted, note_text as unfiltered_note
  189.                  FROM {$this->notesTableName}
  190.                   WHERE
  191.                   note_approved = ?
  192.                   ORDER BY note_time DESC
  193.             ";
  194.  
  195.             $res = $this->dbc->getAll($sqlarray($status)DB_FETCHMODE_ASSOC);
  196.         } else {
  197.             if ($status === true) {
  198.                 $sql = "
  199.                     SELECT note_id, page_url, user_name, user_handle,
  200.                     note_compiled as note_text, note_time, note_approved,
  201.                     note_approved_by, note_deleted, note_text as unfiltered_note
  202.                      FROM {$this->notesTableName}
  203.                       WHERE page_url = ?
  204.                       AND (note_approved = 'yes' OR note_approved = 'pending')
  205.                      ORDER BY note_time DESC
  206.                 ";
  207.                 $res = $this->dbc->getAll($sqlarray($url)DB_FETCHMODE_ASSOC);
  208.             } elseif ($status === false) {
  209.                 $sql = "
  210.                     SELECT note_id, page_url, user_name, user_handle,
  211.                     note_compiled as note_text, note_time, note_approved,
  212.                     note_approved_by, note_deleted, note_text as unfiltered_note
  213.                      FROM {$this->notesTableName}
  214.                       WHERE page_url = ?
  215.                       AND note_approved = 'yes'
  216.                      ORDER BY note_time DESC
  217.                 ";
  218.                 $res = $this->dbc->getAll($sqlarray($url)DB_FETCHMODE_ASSOC);
  219.             } else {
  220.                 $sql = "
  221.                     SELECT note_id, page_url, user_name, user_handle,
  222.                     note_compiled as note_text, note_time, note_approved,
  223.                     note_approved_by, note_deleted, note_text as unfiltered_note
  224.                      FROM {$this->notesTableName}
  225.                       WHERE page_url = ?
  226.                       AND note_approved = ?
  227.                      ORDER BY note_time DESC
  228.                 ";
  229.  
  230.                 $res = $this->dbc->getAll($sqlarray($url$status)DB_FETCHMODE_ASSOC);
  231.             }
  232.         }
  233.  
  234.         if (PEAR::isError($res)) {
  235.             return $res;
  236.         }
  237.  
  238.         return (array)$res;
  239.     }
  240.     // }}}
  241.     // {{{ public function updateCommentList
  242.     /**
  243.      * Update Comment List
  244.      *
  245.      * This function will update a current comment (status, note text, url,
  246.      * username, etc)
  247.      *
  248.      * @access public
  249.      * @param  integer $noteId   The id of the note to update
  250.      *
  251.      * @param  string $status    The status of the note, default = 'pending'
  252.      *
  253.      * @return mixed  $res       An error if an error object occured with the query
  254.      */
  255.     function updateCommentList($noteIds, $status)
  256.     {
  257.         $qs = array();
  258.         $noteIdList = array($status);
  259.         foreach ($noteIds as $noteId) {
  260.             $noteIdList[]   = $noteId;
  261.             $qs[] = 'note_id = ?';
  262.         }
  263.         $qs = implode(' OR ', $qs);
  264.  
  265.         $sql = "
  266.             UPDATE {$this->notesTableName}
  267.              SET note_approved = ?
  268.               WHERE $qs
  269.               LIMIT ?
  270.         ";
  271.         $noteIdList[] = count($noteIdList);
  272.  
  273.         $res = $this->dbc->query($sql$noteIdList);
  274.         if (PEAR::isError($res)) {
  275.             return $res;
  276.         }
  277.  
  278.         return true;
  279.     }
  280.     // }}}
  281.     // {{{ public function updateComment
  282.     /**
  283.      * Update Comment
  284.      *
  285.      * This function will update a current comment (status, note text, url,
  286.      * username, etc)
  287.      *
  288.      * @access public
  289.      * @param  integer $noteId   The id of the note to update
  290.      * @param  string  $url      The url of the page that the
  291.      *                           note belongs to.
  292.      *
  293.      * @param  string  $userName The user[name|address] of the author
  294.      *                           of the note.
  295.      *
  296.      * @param  string $approved  The status of the note, default = 'pending'
  297.      *
  298.      * @return mixed  $res       An error if an error object occured with the query
  299.      */
  300.     function updateComment($noteId, $url, $userName, $approved)
  301.     {
  302.         $sql = "
  303.             UPDATE {$this->notesTableName}
  304.              SET page_url   = ?,
  305.                  user_name  = ?,
  306.                  note_approved   = ?
  307.               WHERE note_id = ?
  308.               LIMIT 1
  309.         ";
  310.  
  311.         $res = $this->dbc->query($sqlarray($url$userName$approved$noteId));
  312.         if (PEAR::isError($res)) {
  313.             return $res;
  314.         }
  315.  
  316.         return true;
  317.     }
  318.     // }}}
  319.     // {{{ public function deleteComments
  320.     /**
  321.      * Delete Comments
  322.      *
  323.      * This function will delete a comment by it's note_id
  324.      * This function will mainly be used by administrators and
  325.      * people with enough karma to manage comments.
  326.      *
  327.      * @access public
  328.      * @param  Array $note_ids  The array of the notes to delete
  329.      *
  330.      * @return Mixed   $res      An error object if query was erroneous, bool
  331.      *                           if it was successful
  332.      */
  333.     function deleteComments($note_ids)
  334.     {
  335.         if (!is_array($note_ids)) {
  336.             return false;
  337.         }
  338.  
  339.         /**
  340.          * Let's just format the note ids so they are simple to
  341.          * read within an IN()
  342.          */
  343.         $notes = "'" . implode(', ', $note_ids) . "'";
  344.  
  345.         $sql = "
  346.             UPDATE {$this->notesTableName}
  347.              SET note_deleted = 1, note_approved='no'
  348.               WHERE note_id IN($notes)
  349.         ";
  350.  
  351.         $res = $this->dbc->query($sql);
  352.         if (PEAR::isError($res)) {
  353.             return $res;
  354.         }
  355.  
  356.         return true;
  357.     }
  358.     // }}}
  359.     // {{{ public function deleteSingleComment
  360.     /**
  361.      * Delete a single comment
  362.      *
  363.      * This function will delete a single comment
  364.      * by it's id.
  365.      *
  366.      * @access public
  367.      * @param  Integer $note_id  The note id to delete
  368.      * @return Mixed   $res      Error object if query is an error
  369.      *                           otherwise return a bool on success
  370.      */
  371.     function deleteSingleComment($note_id)
  372.     {
  373.         $res = $this->deleteComments(array($note_id));
  374.         if (PEAR::isError($res)) {
  375.             return $res;
  376.         }
  377.  
  378.         return true;
  379.     }
  380.     // }}}
  381.     function display($comment)
  382.     {
  383.         // MySQL 4.1 displays timestamps as if they were datetimes, so make
  384.         // sure this doesn't break on upgrade
  385.         if (is_numeric($comment['note_time'])) {
  386.             $pretime = strptime($comment['note_time'], '%Y%m%d%H%M%S');
  387.             $mytime = mktime($pretime['tm_hour'], $pretime['tm_min'], $pretime['tm_sec'],
  388.                    $pretime['tm_mon'] + 1, $pretime['tm_mday'], $pretime['tm_year'] + 1900);
  389.             $time = date('Y-m-d H:i', $mytime - date('Z', $mytime)) . ' UTC';
  390.         } else {
  391.             $date = strtotime($comment['note_time']);
  392.             $time = date('Y-m-d H:i', $date - date('Z', $date)) . ' UTC';
  393.         }
  394.         $noteId     =  (int)$comment['note_id'];
  395.         $userHandle = $comment['user_handle'] ?
  396.             '<a href="/user/' . $comment['user_handle'] . '">' . $comment['user_handle'] .
  397.             '</a>' :
  398.             $this->obfuscateAnonLink($comment['user_name']);
  399.         $pending    $comment['note_approved'== 'pending';
  400.         $id $comment['page_url'];
  401.         $comment    $comment['note_text'];
  402.         $linkUrl    '<a href="#' $noteId '">' $time '</a>';
  403.         $linkName   '<a name="' $noteId '"></a>';
  404.         include PEARWEB_TEMPLATEDIR . '/notes/note.tpl.php';
  405.     }
  406.  
  407.     // {{{ public function obfuscateAnonLink
  408.     /**
  409.      * Obfuscate Anonymous link
  410.      *
  411.      * This function will take a parameter and
  412.      * make it obfuscated in a manner that no
  413.      * script can find @ . , etc. This is the same
  414.      * method used for bugs and all mailto_links
  415.      * on the site (site-wide)
  416.      *
  417.      * @access public
  418.      * @param  string $text   The text to obfuscate
  419.      * @return string $obText The text obfuscated
  420.      */
  421.     public function obfuscateAnonLink($text)
  422.     {
  423.         $tmp = '';
  424.         for ($i = 0, $l = strlen($text)$i<$l$i++) {
  425.             if ($i % 2) {
  426.                 $tmp .= '&#' . ord($text[$i]) . ';';
  427.             } else {
  428.                 $tmp .= '&#x' . dechex(ord($text[$i])) . ';';
  429.             }
  430.         }
  431.         return $tmp;
  432.     }
  433.     // }}}
  434. }

Documentation generated on Mon, 11 Mar 2019 16:04:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.