FOSSology  4.4.0
Open Source License Compliance by Open Source Software
admin-tag.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2013-2015 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 define("TITLE_ADMIN_TAG", _("Create Tag"));
14 
15 class admin_tag extends FO_Plugin
16 {
17  function __construct()
18  {
19  $this->Name = "admin_tag";
20  $this->Title = TITLE_ADMIN_TAG;
21  $this->MenuList = "Admin::Tag::Create Tag";
22  $this->Version = "1.3";
23  $this->DBaccess = PLUGIN_DB_ADMIN;
24  parent::__construct();
25  }
26 
32  function CreateTag()
33  {
34  global $PG_CONN;
35 
36  $tag_name = GetParm('tag_name', PARM_TEXT);
37  $tag_desc = GetParm('tag_desc', PARM_TEXT);
38  if (empty($tag_name)) {
39  $text = _("TagName must be specified. Tag Not created.");
40  return ($text);
41  }
42  if (! preg_match('/^[A-Za-z0-9_~\-!@#\$%\^\*\.\‍(\‍)]+$/i', $tag_name)) {
43  $text = _(
44  "A Tag is only allowed to contain characters from <b>" .
45  htmlentities("A-Za-z0-9_~-!@#$%^*.()") . "</b>. Tag Not created.");
46  return ($text);
47  }
48 
49  /* See if the tag already exists */
50  $sql = "SELECT * FROM tag WHERE tag = '".pg_escape_string($tag_name)."'";
51  $result = pg_query($PG_CONN, $sql);
52  DBCheckResult($result, $sql, __FILE__, __LINE__);
53  if (pg_num_rows($result) < 1) {
54  pg_free_result($result);
55 
56  $sql = "INSERT INTO tag (tag,tag_desc) VALUES ('" .
57  pg_escape_string($tag_name) . "', '" . pg_escape_string($tag_desc) .
58  "');";
59  $result = pg_query($PG_CONN, $sql);
60  DBCheckResult($result, $sql, __FILE__, __LINE__);
61  }
62  pg_free_result($result);
63 
64  /* Make sure it was added */
65  $sql = "SELECT * FROM tag WHERE tag = '".pg_escape_string($tag_name)."' LIMIT 1;";
66  $result = pg_query($PG_CONN, $sql);
67  DBCheckResult($result, $sql, __FILE__, __LINE__);
68  if (pg_num_rows($result) < 1) {
69  pg_free_result($result);
70  $text = _("Failed to create tag.");
71  return ($text);
72  }
73  pg_free_result($result);
74 
75  return (null);
76  }
77 
81  function ShowExistTags()
82  {
83  global $PG_CONN;
84  $VE = _("<h3>Current Tags:</h3>\n");
85  $sql = "SELECT tag_pk, tag, tag_desc FROM tag ORDER BY tag_pk desc;";
86  $result = pg_query($PG_CONN, $sql);
87  DBCheckResult($result, $sql, __FILE__, __LINE__);
88  if (pg_num_rows($result) > 0) {
89  $VE .= "<table border=1>\n";
90  $text1 = _("Tag pk");
91  $text2 = _("Tag");
92  $text3 = _("Tag Description");
93  $VE .= "<tr><th>$text1</th><th>$text2</th><th>$text3</th></tr>\n";
94  while ($row = pg_fetch_assoc($result)) {
95  $VE .= "<tr><td align='center'>" . $row['tag_pk'] .
96  "</td><td align='center'>" . htmlspecialchars($row['tag']) .
97  "</td><td align='center'>" . htmlspecialchars($row['tag_desc']) .
98  "</td>";
99  }
100  $VE .= "</table><p>\n";
101  }
102  pg_free_result($result);
103  return $VE;
104  }
105 
109  function ShowCreateTagPage()
110  {
111  $VC = _("<h3>Create Tag:</h3>\n");
112  $VC.= "<form name='form' method='POST' action='" . Traceback_uri() ."?mod=admin_tag'>\n";
113  $VC .= "<p>";
114  $text = _("Tag");
115  $VC .= "$text: <input type='text' id='tag_name' name='tag_name' maxlength='32' utocomplete='off'/> ";
116  $VC .= "</p>";
117  $text = _("Tag description:");
118  $VC .= "<p>$text <input type='text' name='tag_desc'/></p>";
119  $text = _("Create");
120  $VC .= "<input type='hidden' name='action' value='add'/>\n";
121  $VC .= "<input type='submit' value='$text'>\n";
122  $VC .= "</form>\n";
123  return $VC;
124  }
125 
126 
127  public function Output()
128  {
129  $V="";
130  $action = GetParm('action', PARM_TEXT);
131 
132  if ($action == 'add') {
133  $rc = $this->CreateTag();
134  if (!empty($rc)) {
135  $text = _("Create Tag Failed");
136  $V .= displayMessage("$text: $rc");
137  } else {
138  $text = _("Create Tag Successful!");
139  $V .= displayMessage($text);
140  }
141  }
142  $V .= $this->ShowCreateTagPage();
143  $V .= $this->ShowExistTags();
144  return $V;
145  }
146 }
147 
148 $NewPlugin = new admin_tag;
149 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
__construct()
base constructor. Most plugins will just use this
Definition: admin-tag.php:17
ShowCreateTagPage()
Display the create tag page.
Definition: admin-tag.php:109
CreateTag()
Create Tag without tagging anything.
Definition: admin-tag.php:32
ShowExistTags()
Show all tags.
Definition: admin-tag.php:81
Output()
This function is called when user output is requested. This function is responsible for content....
Definition: admin-tag.php:127
displayMessage($Message, $keep=null)
Display a message.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_TEXT
Definition: common-parm.php:20
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN