PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/webapp/_lib/model/class.PluginMySQLDAO.php

http://github.com/ginatrapani/ThinkUp
PHP | 252 lines | 204 code | 13 blank | 35 comment | 51 complexity | 5d8c18ce3f5bbe5324b345fed0dda7a7 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.PluginMySQLDAO.php
  5. *
  6. * Copyright (c) 2009-2012 Gina Trapani
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Plugin Data Access Object
  25. * The data access object for retrieving and saving plugin data for thinkup
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2012 Gina Trapani
  28. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  29. * @author Mark Wilkie <mwilkie[at]gmail[dot]com>
  30. */
  31. class PluginMySQLDAO extends PDODAO implements PluginDAO {
  32. public function getAllPlugins($is_active = false) {
  33. $q = " SELECT * FROM #prefix#plugins p";
  34. if ($is_active != "") {
  35. $q .= ' where p.is_active = 1';
  36. }
  37. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  38. $stmt = $this->execute($q);
  39. return $this->getDataRowsAsObjects($stmt, 'Plugin');
  40. }
  41. public function getActivePlugins() {
  42. return $this->getAllPlugins(true);
  43. }
  44. public function isPluginActive($id) {
  45. $q = 'SELECT is_active FROM #prefix#plugins p WHERE p.id = :id';
  46. $status = false;
  47. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  48. $stmt = $this->execute($q, array(':id' => $id));
  49. $plugin = $this->getDataRowAsObject($stmt, 'Plugin');
  50. if ($plugin && $plugin->is_active == 1) {
  51. $status = true;
  52. }
  53. return $status;
  54. }
  55. public function insertPlugin($plugin) {
  56. if (!is_object($plugin) || !isset($plugin->name) || !isset($plugin->folder_name)
  57. || !isset($plugin->is_active) ) {
  58. throw new BadArgumentException("PluginDAO::insertPlugin requires a valid plugin data object");
  59. }
  60. $q = 'INSERT INTO
  61. #prefix#plugins (name, folder_name, description, author, version, homepage, is_active)
  62. VALUES
  63. (:name, :folder_name, :description, :author, :version, :homepage, :is_active)';
  64. $is_active = $plugin->is_active ? 1 : 0;
  65. $vars = array(
  66. ':name' => $plugin->name,
  67. 'folder_name' => $plugin->folder_name,
  68. ':description' => $plugin->description,
  69. ':author' => $plugin->author,
  70. ':version' => $plugin->version,
  71. ':homepage' => $plugin->homepage,
  72. ':is_active' => $is_active);
  73. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  74. $stmt = $this->execute($q, $vars);
  75. if ( $this->getInsertCount($stmt) > 0) {
  76. return $this->getInsertId($stmt);
  77. } else {
  78. return false;
  79. }
  80. }
  81. public function updatePlugin($plugin) {
  82. if (! is_object($plugin) || get_class($plugin) != 'Plugin'
  83. || ! isset($plugin->name) || ! isset($plugin->folder_name)
  84. || ! isset($plugin->is_active) || ! isset($plugin->id) )
  85. {
  86. throw new BadArgumentException("updatePlugin() requires a valid plugin data object");
  87. }
  88. $q = 'UPDATE
  89. #prefix#plugins
  90. SET
  91. name = :name,
  92. folder_name = :folder_name,
  93. description = :description,
  94. author = :author,
  95. version = :version,
  96. homepage = :homepage,
  97. is_active = :is_active
  98. WHERE
  99. id = :id';
  100. $is_active = $plugin->is_active ? 1 : 0;
  101. $vars = array(
  102. ':name' => $plugin->name,
  103. 'folder_name' => $plugin->folder_name,
  104. ':description' => $plugin->description,
  105. ':author' => $plugin->author,
  106. ':version' => $plugin->version,
  107. ':homepage' => $plugin->homepage,
  108. ':is_active' => $is_active,
  109. ':id' => $plugin->id);
  110. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  111. $stmt = $this->execute($q, $vars);
  112. if ( $this->getUpdateCount($stmt) > 0) {
  113. return true;
  114. } else {
  115. return false;
  116. }
  117. }
  118. public function getPluginId($folder_name) {
  119. $q = "SELECT id FROM #prefix#plugins WHERE folder_name = :folder_name";
  120. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  121. $stmt = $this->execute($q, array(':folder_name' => $folder_name) );
  122. $row = $this->getDataRowAsArray($stmt);
  123. // get the id if there is one
  124. $id = $row && $row['id'] ? $row['id'] : null;
  125. return $id;
  126. }
  127. public function getPluginFolder($plugin_id) {
  128. $q = "SELECT folder_name FROM #prefix#plugins WHERE id = :plugin_id";
  129. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  130. $stmt = $this->execute($q, array(':plugin_id' => $plugin_id) );
  131. $row = $this->getDataRowAsArray($stmt);
  132. // get the id if there is one
  133. $folder_name = $row && $row['folder_name'] ? $row['folder_name'] : null;
  134. return $folder_name;
  135. }
  136. public function setActive($id, $active) {
  137. if (is_bool($active)) {
  138. $active = $this->convertBoolToDB($active);
  139. }
  140. $q = "
  141. UPDATE
  142. #prefix#plugins
  143. SET
  144. is_active = :active
  145. WHERE
  146. id = :id";
  147. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  148. $stmt = $this->execute($q, array(':active' => $active, ':id' => $id));
  149. return $this->getUpdateCount($stmt);
  150. }
  151. public function getInstalledPlugins() {
  152. // Detect what plugins exist in the filesystem; parse their header comments for plugin metadata
  153. Loader::definePathConstants();
  154. $active_plugins = $inactive_plugins = array();
  155. $plugin_files = Utils::getPlugins(THINKUP_WEBAPP_PATH.'plugins');
  156. foreach ($plugin_files as $pf) {
  157. foreach (glob(THINKUP_WEBAPP_PATH.'plugins/'.$pf."/controller/".$pf.".php") as $includefile) {
  158. $fhandle = fopen($includefile, "r");
  159. $contents = fread($fhandle, filesize($includefile));
  160. fclose($fhandle);
  161. $plugin_vals = $this->parseFileContents($contents, $pf);
  162. if (isset($plugin_vals['class'])) {
  163. require_once THINKUP_WEBAPP_PATH.'plugins/'.$pf."/model/class.".$plugin_vals['class'].".php";
  164. $installed_plugin = new $plugin_vals['class']($plugin_vals);
  165. } else {
  166. $installed_plugin = new Plugin($plugin_vals);
  167. }
  168. if (isset($installed_plugin)) {
  169. // Insert or update plugin entries in the database
  170. if (!isset($installed_plugin->id)) {
  171. $new_plugin_id = $this->insertPlugin($installed_plugin);
  172. if ($new_plugin_id === false) {
  173. $this->updatePlugin($installed_plugin);
  174. } else {
  175. $installed_plugin->id = $new_plugin_id;
  176. }
  177. }
  178. // Store in list, active first
  179. if ($installed_plugin->is_active) {
  180. array_push($active_plugins, $installed_plugin);
  181. } else {
  182. array_push($inactive_plugins, $installed_plugin);
  183. }
  184. }
  185. }
  186. }
  187. return array_merge($active_plugins, $inactive_plugins);
  188. }
  189. private function parseFileContents($contents, $pf) {
  190. $plugin_vals = array();
  191. $start = strpos($contents, '/*');
  192. $end = strpos($contents, '*/');
  193. if ($start > 0 && $end > $start) {
  194. $scriptData = substr($contents, $start + 2, $end - $start - 2);
  195. $scriptData = preg_split('/[\n\r]+/', $scriptData);
  196. foreach ($scriptData as $line) {
  197. $m = array();
  198. if (preg_match('/Plugin Name:(.*)/', $line, $m)) {
  199. $plugin_vals['name'] = trim($m[1]);
  200. }
  201. if (preg_match('/Plugin URI:(.*)/', $line, $m)) {
  202. $plugin_vals['homepage'] = trim($m[1]);
  203. }
  204. if (preg_match('/Description:(.*)/', $line, $m)) {
  205. $plugin_vals['description'] = trim($m[1]);
  206. }
  207. if (preg_match('/Version:(.*)/', $line, $m)) {
  208. $plugin_vals['version'] = trim($m[1]);
  209. }
  210. if (preg_match('/Author:(.*)/', $line, $m)) {
  211. $plugin_vals['author'] = trim($m[1]);
  212. }
  213. if (preg_match('/Icon:(.*)/', $line, $m)) {
  214. $plugin_vals['icon'] = trim($m[1]);
  215. }
  216. if (preg_match('/Class:(.*)/', $line, $m)) {
  217. $plugin_vals['class'] = trim($m[1]);
  218. }
  219. }
  220. $plugin_vals["folder_name"] = $pf;
  221. $plugin_vals["id"] = $this->getPluginId($pf);
  222. if (isset($plugin_vals["id"])) {
  223. $plugin_vals["is_active"] = $this->isPluginActive($plugin_vals["id"]);
  224. } else {
  225. $plugin_vals["is_active"] = 0;
  226. }
  227. return $plugin_vals;
  228. } else {
  229. return null;
  230. }
  231. }
  232. public function isValidPluginId($plugin_id) {
  233. $q = 'SELECT id FROM #prefix#plugins where id = :id';
  234. $data = array(':id' => $plugin_id);
  235. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  236. $stmt = $this->execute($q, $data);
  237. return $this->getDataIsReturned($stmt);
  238. }
  239. }