YaWK  24.1
Yet another WebKit
plugin.php
Go to the documentation of this file.
1 <?php
2 namespace YAWK {
3  /**
4  * @details <b>Plugin class: handles some basic plugin functions</b>
5  *
6  * Handles plugin functions of YaWK. Plugins are bigger extensions than widgets.
7  * A plugin can handle any data from a mysql database. This is useful if your project
8  * needs more than just static pages, like a bulletin board, shop system, blog and so on.
9  * There are many Plugins done yet.
10  * See how the plugin system is organized:<ul>
11  * <li>system/plugins/pluginname/ = <strong>Plugin folder</strong></li>
12  * <li>system/plugins/pluginname/admin = <strong>Plugin Backend (view) </strong></li>
13  * <li>system/plugins/pluginname/classes = <strong>Plugin logic (controller) </strong></li>
14  * <li>system/plugins/pluginname/pluginname.php = <strong>Plugin Frontend (view)</strong></li></ul>
15  * <br>
16  * If you need to build a custom extension, read the docs about the plugin system to understand how to
17  * fit it into YaWK. If you follow the Plugin's MVC structure, it is easy to integrate your own
18  * extensions into any project.
19  *
20  * <i>Example:</i>
21  * <code><?php \YAWK\plugin::getPlugins(); ?></code>
22  * gets a list of all plugins, like its used in the backend in "Plugin" Menu.
23  * <p><i>This class covers backend functionality.
24  * See Methods Summary for Details!</i></p>
25  *
26  * @author Daniel Retzl <[email protected]>
27  * @copyright 2009-2016 Daniel Retzl
28  * @license https://opensource.org/licenses/MIT
29  * @version 1.0.0
30  * @brief Handles the Plugin System.
31  */
32  class plugin
33  {
34  /** * @param int plugin ID */
35  public $id;
36  /** * @param string plugin name*/
37  public $name;
38  /** * @param string plugin description */
39  public $description;
40  /** * @param string plugin font awesome or glyph icon */
41  public $icon;
42  /** * @param int plugin status ID */
43  public $activated;
44 
45  /**
46  * @brief plugin constructor.
47  */
48  function __construct()
49  {
50  // ...
51  }
52 
53  /**
54  * @brief get and draw a list of all active plugins
55  * @author Daniel Retzl <[email protected]>
56  * @copyright 2009-2016 Daniel Retzl
57  * @license https://opensource.org/licenses/MIT
58  * @version 1.0.0
59  * @param object $db database
60  * @param array $lang language array
61  * @param int $manage 0|1 if manage is 1, only active plugins will be shown
62  * @return null|string html output
63  */
64  function getPlugins($db, $lang, $manage)
65  { /** @var $db \YAWK\db */
66  if (isset($manage) && ($manage == 1))
67  {
68  $sqlcode = "WHERE activated='1'";
69  }
70  else
71  {
72  $sqlcode = "";
73  }
74 
75  if (!$res = $db->query("SELECT * FROM {plugins} $sqlcode ORDER by name"))
76  {
77  \YAWK\sys::setSyslog($db, 31, 1, "failed to select plugin from database", 0, 0, 0, 0);
78  print \YAWK\alert::draw("danger", "$lang[ERROR]", "$lang[PLUGIN_DB_ERROR]", "","");
79  $html = null;
80  }
81  else
82  {
83  /* TABLE HEADER */
84  $html = '';
85  /* TABLE CONTENT */
86  while ($row = mysqli_fetch_array($res))
87  {
88  $this->id = $row[0];
89  $this->name = $row[1];
90  $this->description = $row[2];
91  $this->icon = $row[3];
92  $this->activated = $row[4];
93 
94 
95  // set plugin folder
96  $folder = "../system/plugins/" . $this->name;
97  // & check it - only show data
98  if (file_exists($folder) === true)
99  {
100  $html .= "<tr>
101  <td style=\"text-align: center;\"><a href=\"index.php?plugin=$this->name\"><i class=\"$this->icon fa-4x\"></i></a></td>
102  <td><br><a href=\"index.php?plugin=$this->name\"><div>$this->name</div></a></td>
103  <td><br>$this->description</td>
104  <td style=\"text-align: center;\"><br>
105  <a class=\"fa fa-edit\" title=\"" . $lang['EDIT'] . "\" href=\"index.php?plugin=$this->name\"></a>&nbsp;
106  </td>
107  </tr>";
108  }
109  else
110  {
111  $name = ucfirst($this->name);
112  echo \YAWK\alert::draw("warning", "Fehler!", "$lang[PLUGIN]: <b>\"$name\"</b> $lang[PLUGIN_FALSE_INSTALLED] <b>system/plugins/$this->name/</b>","","4800");
113  }
114  }
115  }
116  return $html;
117  /* EOFunction getPlugins */
118  }
119 
120  /**
121  * @brief get requested plugin name for given plugin ID
122  * @author Daniel Retzl <[email protected]>
123  * @copyright 2009-2016 Daniel Retzl
124  * @license https://opensource.org/licenses/MIT
125  * @version 1.0.0
126  * @param object $db database
127  * @param int $pluginId affected plugin ID
128  * @return string|bool returns the name or false
129  */
130  static function getNameById($db, $pluginId)
131  { /** @param $db \YAWK\db $res */
132  if ($res = $db->query("SELECT name FROM {plugins} WHERE id = '" . $pluginId . "'"))
133  { // fetch data from db
134  while ($row = mysqli_fetch_row($res))
135  { // return plugin name
136  return $row['0'];
137  }
138  }
139  else
140  { // q failed
141  \YAWK\sys::setSyslog($db, 31, 1, "failed to get name of plugin $pluginId ", 0, 0, 0, 0);
142  return false;
143  }
144  // something else happened
145  return false;
146  }
147 
148  /**
149  * @brief get requested plugin ID by given name
150  * @author Daniel Retzl <[email protected]>
151  * @copyright 2009-2016 Daniel Retzl
152  * @license https://opensource.org/licenses/MIT
153  * @version 1.0.0
154  * @param object $db database
155  * @param string $plugin plugin name
156  * @return string|bool returns the plugin ID or false
157  */
158  static function getIdByName($db, $plugin)
159  { /** @param $db \YAWK\db $res */
160  if ($res = $db->query("SELECT id FROM {plugins} WHERE name ='".$plugin."'"))
161  { // fetch data from db
162  while ($row = mysqli_fetch_row($res))
163  { // return plugin ID
164  return $row['0'];
165  }
166  }
167  else
168  { // q failed, throw error
169  \YAWK\sys::setSyslog($db, 31, 1, "failed to get id of plugin $plugin ", 0, 0, 0, 0);
170  return \YAWK\alert::draw("danger", "Error!", "Could not get id of plugin: ".$plugin."","page=plugins","4800");
171  }
172  return false;
173  }
174 
175  /**
176  * @brief create a static page wich includes the according plugin content
177  * @author Daniel Retzl <[email protected]>
178  * @copyright 2009-2016 Daniel Retzl
179  * @license https://opensource.org/licenses/MIT
180  * @version 1.0.0
181  * @param object $db database
182  * @param string $alias filename of the static page
183  * @param int $plugin plugin ID
184  * @return bool
185  */
186  static function createPluginPage($db, $alias, $plugin)
187  { /** @var $db \YAWK\db */
188  if (!isset($alias)){ $alias = ''; }
189  if (!isset($plugin)){ $alias = ''; }
190  if (!file_exists("../content/pages/$alias.php"))
191  { // frontend plugin file not found
192  if (!isset($page))
193  { // check if page obj exists
194  $page = new \YAWK\page();
195  }
196  // create new frontend plugin page
197  if ($page->create($db, $alias, 1, 0, 0, $plugin))
198  { // all good
199  \YAWK\sys::setSyslog($db, 29, 0, "created page $alias for plugin $plugin", 0, 0, 0, 0);
200  return true;
201  }
202  else
203  { // could not create page
204  \YAWK\sys::setSyslog($db, 31, 1, "failed to create page $alias for plugin $plugin - check file and folder permissions!", 0, 0, 0, 0);
205  return false;
206  }
207  }
208  else
209  { // file exits
210  \YAWK\sys::setSyslog($db, 31, 1, "failed to create page $alias for plugin $plugin - a page with that name still exits. File will not be overwritten.", 0, 0, 0, 0);
211  return false;
212  }
213  }
214 
215  } // ./ class plugin
216 } // ./ namespace
$folder
Definition: actions.php:9
print $lang['FILEMAN_UPLOAD']
$blog icon
$blog description
Definition: blog-setup.php:49
Handles the Plugin System.
Definition: plugin.php:33
static getNameById($db, $pluginId)
get requested plugin name for given plugin ID
Definition: plugin.php:130
static getIdByName($db, $plugin)
get requested plugin ID by given name
Definition: plugin.php:158
__construct()
plugin constructor.
Definition: plugin.php:48
This class serves methods to create backup from files.
Definition: AdminLTE.php:2
$page
Definition: pages.php:355
$plugin
$template name