FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
admin-tag.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2013-2015 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
24 define("TITLE_ADMIN_TAG", _("Create Tag"));
25 
26 class admin_tag extends FO_Plugin
27 {
28  function __construct()
29  {
30  $this->Name = "admin_tag";
31  $this->Title = TITLE_ADMIN_TAG;
32  $this->MenuList = "Admin::Tag::Create Tag";
33  $this->Version = "1.3";
34  $this->DBaccess = PLUGIN_DB_ADMIN;
35  parent::__construct();
36  }
37 
43  function CreateTag()
44  {
45  global $PG_CONN;
46 
47  $tag_name = GetParm('tag_name', PARM_TEXT);
48  $tag_desc = GetParm('tag_desc', PARM_TEXT);
49  if (empty($tag_name)) {
50  $text = _("TagName must be specified. Tag Not created.");
51  return ($text);
52  }
53  if (! preg_match('/^[A-Za-z0-9_~\-!@#\$%\^\*\.\(\)]+$/i', $tag_name)) {
54  $text = _(
55  "A Tag is only allowed to contain characters from <b>" .
56  htmlentities("A-Za-z0-9_~-!@#$%^*.()") . "</b>. Tag Not created.");
57  return ($text);
58  }
59 
60  /* See if the tag already exists */
61  $sql = "SELECT * FROM tag WHERE tag = '".pg_escape_string($tag_name)."'";
62  $result = pg_query($PG_CONN, $sql);
63  DBCheckResult($result, $sql, __FILE__, __LINE__);
64  if (pg_num_rows($result) < 1) {
65  pg_free_result($result);
66 
67  $sql = "INSERT INTO tag (tag,tag_desc) VALUES ('" .
68  pg_escape_string($tag_name) . "', '" . pg_escape_string($tag_desc) .
69  "');";
70  $result = pg_query($PG_CONN, $sql);
71  DBCheckResult($result, $sql, __FILE__, __LINE__);
72  }
73  pg_free_result($result);
74 
75  /* Make sure it was added */
76  $sql = "SELECT * FROM tag WHERE tag = '".pg_escape_string($tag_name)."' LIMIT 1;";
77  $result = pg_query($PG_CONN, $sql);
78  DBCheckResult($result, $sql, __FILE__, __LINE__);
79  if (pg_num_rows($result) < 1) {
80  pg_free_result($result);
81  $text = _("Failed to create tag.");
82  return ($text);
83  }
84  pg_free_result($result);
85 
86  return (null);
87  }
88 
92  function ShowExistTags()
93  {
94  global $PG_CONN;
95  $VE = _("<h3>Current Tags:</h3>\n");
96  $sql = "SELECT tag_pk, tag, tag_desc FROM tag ORDER BY tag_pk desc;";
97  $result = pg_query($PG_CONN, $sql);
98  DBCheckResult($result, $sql, __FILE__, __LINE__);
99  if (pg_num_rows($result) > 0) {
100  $VE .= "<table border=1>\n";
101  $text1 = _("Tag pk");
102  $text2 = _("Tag");
103  $text3 = _("Tag Description");
104  $VE .= "<tr><th>$text1</th><th>$text2</th><th>$text3</th></tr>\n";
105  while ($row = pg_fetch_assoc($result)) {
106  $VE .= "<tr><td align='center'>" . $row['tag_pk'] .
107  "</td><td align='center'>" . htmlspecialchars($row['tag']) .
108  "</td><td align='center'>" . htmlspecialchars($row['tag_desc']) .
109  "</td>";
110  }
111  $VE .= "</table><p>\n";
112  }
113  pg_free_result($result);
114  return $VE;
115  }
116 
120  function ShowCreateTagPage()
121  {
122  $VC = "";
123  $VC .= _("<h3>Create Tag:</h3>\n");
124  $VC.= "<form name='form' method='POST' action='" . Traceback_uri() ."?mod=admin_tag'>\n";
125  $VC .= "<p>";
126  $text = _("Tag");
127  $VC .= "$text: <input type='text' id='tag_name' name='tag_name' maxlength='32' utocomplete='off'/> ";
128  $VC .= "</p>";
129  $text = _("Tag description:");
130  $VC .= "<p>$text <input type='text' name='tag_desc'/></p>";
131  $text = _("Create");
132  $VC .= "<input type='hidden' name='action' value='add'/>\n";
133  $VC .= "<input type='submit' value='$text'>\n";
134  $VC .= "</form>\n";
135  return $VC;
136  }
137 
138 
139  public function Output()
140  {
141  $V="";
142  $action = GetParm('action', PARM_TEXT);
143 
144  if ($action == 'add') {
145  $rc = $this->CreateTag();
146  if (!empty($rc)) {
147  $text = _("Create Tag Failed");
148  $V .= displayMessage("$text: $rc");
149  } else {
150  $text = _("Create Tag Successful!");
151  $V .= displayMessage($text);
152  }
153  }
154  $V .= $this->ShowCreateTagPage();
155  $V .= $this->ShowExistTags();
156  return $V;
157  }
158 }
159 
160 $NewPlugin = new admin_tag;
161 $NewPlugin->Initialize();
Traceback_uri()
Get the URI without query to this location.
displayMessage($Message, $keep=null)
Display a message.
const PARM_TEXT
Definition: common-parm.php:31
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
ShowCreateTagPage()
Display the create tag page.
Definition: admin-tag.php:120
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
CreateTag()
Create Tag without tagging anything.
Definition: admin-tag.php:43
ShowExistTags()
Show all tags.
Definition: admin-tag.php:92