PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/devsatish/ThinkUp
PHP | 243 lines | 195 code | 13 blank | 35 comment | 50 complexity | 3255d5f889bf0ff96c610024f1f149db MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.PluginMySQLDAO.php
  5. *
  6. * Copyright (c) 2009-2011 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-2011 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) || get_class($plugin) != 'Plugin'
  57. || ! isset($plugin->name) || ! isset($plugin->folder_name)
  58. || ! isset($plugin->is_active) ) {
  59. throw new BadArgumentException("insertPlugin() requires a valid plugin data object");
  60. }
  61. $q = 'INSERT INTO
  62. #prefix#plugins (name, folder_name, description, author, version, homepage, is_active)
  63. VALUES
  64. (:name, :folder_name, :description, :author, :version, :homepage, :is_active)';
  65. $is_active = $plugin->is_active ? 1 : 0;
  66. $vars = array(
  67. ':name' => $plugin->name,
  68. 'folder_name' => $plugin->folder_name,
  69. ':description' => $plugin->description,
  70. ':author' => $plugin->author,
  71. ':version' => $plugin->version,
  72. ':homepage' => $plugin->homepage,
  73. ':is_active' => $is_active);
  74. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  75. $stmt = $this->execute($q, $vars);
  76. if ( $this->getInsertCount($stmt) > 0) {
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. public function updatePlugin($plugin) {
  83. if (! is_object($plugin) || get_class($plugin) != 'Plugin'
  84. || ! isset($plugin->name) || ! isset($plugin->folder_name)
  85. || ! isset($plugin->is_active) || ! isset($plugin->id) )
  86. {
  87. throw new BadArgumentException("updatePlugin() requires a valid plugin data object");
  88. }
  89. $q = 'UPDATE
  90. #prefix#plugins
  91. SET
  92. name = :name,
  93. folder_name = :folder_name,
  94. description = :description,
  95. author = :author,
  96. version = :version,
  97. homepage = :homepage,
  98. is_active = :is_active
  99. WHERE
  100. id = :id';
  101. $is_active = $plugin->is_active ? 1 : 0;
  102. $vars = array(
  103. ':name' => $plugin->name,
  104. 'folder_name' => $plugin->folder_name,
  105. ':description' => $plugin->description,
  106. ':author' => $plugin->author,
  107. ':version' => $plugin->version,
  108. ':homepage' => $plugin->homepage,
  109. ':is_active' => $is_active,
  110. ':id' => $plugin->id);
  111. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  112. $stmt = $this->execute($q, $vars);
  113. if ( $this->getUpdateCount($stmt) > 0) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. public function getPluginId($folder_name) {
  120. $q = "SELECT id FROM #prefix#plugins WHERE folder_name = :folder_name";
  121. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  122. $stmt = $this->execute($q, array(':folder_name' => $folder_name) );
  123. $row = $this->getDataRowAsArray($stmt);
  124. // get the id if there is one
  125. $id = $row && $row['id'] ? $row['id'] : null;
  126. return $id;
  127. }
  128. public function getPluginFolder($plugin_id) {
  129. $q = "SELECT folder_name FROM #prefix#plugins WHERE id = :plugin_id";
  130. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  131. $stmt = $this->execute($q, array(':plugin_id' => $plugin_id) );
  132. $row = $this->getDataRowAsArray($stmt);
  133. // get the id if there is one
  134. $folder_name = $row && $row['folder_name'] ? $row['folder_name'] : null;
  135. return $folder_name;
  136. }
  137. public function setActive($id, $active) {
  138. if (is_bool($active)) {
  139. $active = $this->convertBoolToDB($active);
  140. }
  141. $q = "
  142. UPDATE
  143. #prefix#plugins
  144. SET
  145. is_active = :active
  146. WHERE
  147. id = :id";
  148. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  149. $stmt = $this->execute($q, array(':active' => $active, ':id' => $id));
  150. return $this->getUpdateCount($stmt);
  151. }
  152. public function getInstalledPlugins($plugin_path) {
  153. // Detect what plugins exist in the filesystem; parse their header comments for plugin metadata
  154. Utils::defineConstants();
  155. $active_plugins = $inactive_plugins = array();
  156. $plugin_files = Utils::getPlugins(THINKUP_WEBAPP_PATH.'plugins');
  157. foreach ($plugin_files as $pf) {
  158. foreach (glob(THINKUP_WEBAPP_PATH.'plugins/'.$pf."/controller/".$pf.".php") as $includefile) {
  159. $fhandle = fopen($includefile, "r");
  160. $contents = fread($fhandle, filesize($includefile));
  161. fclose($fhandle);
  162. $installed_plugin = $this->parseFileContents($contents, $pf);
  163. if (isset($installed_plugin)) {
  164. // Insert or update plugin entries in the database
  165. if (!isset($installed_plugin->id)) {
  166. if ($this->insertPlugin($installed_plugin)) {
  167. $installed_plugin->id = $this->getPluginId($installed_plugin->folder_name);
  168. } else {
  169. $this->updatePlugin($installed_plugin);
  170. }
  171. }
  172. // Store in list, active first
  173. if ($installed_plugin->is_active) {
  174. array_push($active_plugins, $installed_plugin);
  175. } else {
  176. array_push($inactive_plugins, $installed_plugin);
  177. }
  178. }
  179. }
  180. }
  181. return array_merge($active_plugins, $inactive_plugins);
  182. }
  183. private function parseFileContents($contents, $pf) {
  184. $plugin_vals = array();
  185. $start = strpos($contents, '/*');
  186. $end = strpos($contents, '*/');
  187. if ($start > 0 && $end > $start) {
  188. $scriptData = substr($contents, $start + 2, $end - $start - 2);
  189. $scriptData = preg_split('/[\n\r]+/', $scriptData);
  190. foreach ($scriptData as $line) {
  191. $m = array();
  192. if (preg_match('/Plugin Name:(.*)/', $line, $m)) {
  193. $plugin_vals['name'] = trim($m[1]);
  194. }
  195. if (preg_match('/Plugin URI:(.*)/', $line, $m)) {
  196. $plugin_vals['homepage'] = trim($m[1]);
  197. }
  198. if (preg_match('/Description:(.*)/', $line, $m)) {
  199. $plugin_vals['description'] = trim($m[1]);
  200. }
  201. if (preg_match('/Version:(.*)/', $line, $m)) {
  202. $plugin_vals['version'] = trim($m[1]);
  203. }
  204. if (preg_match('/Author:(.*)/', $line, $m)) {
  205. $plugin_vals['author'] = trim($m[1]);
  206. }
  207. if (preg_match('/Icon:(.*)/', $line, $m)) {
  208. $plugin_vals['icon'] = trim($m[1]);
  209. }
  210. }
  211. $plugin_vals["folder_name"] = $pf;
  212. $plugin_vals["id"] = $this->getPluginId($pf);
  213. if (isset($plugin_vals["id"])) {
  214. $plugin_vals["is_active"] = $this->isPluginActive($plugin_vals["id"]);
  215. } else {
  216. $plugin_vals["is_active"] = 0;
  217. }
  218. return new Plugin($plugin_vals);
  219. } else {
  220. return null;
  221. }
  222. }
  223. public function isValidPluginId($plugin_id) {
  224. $q = 'SELECT id FROM #prefix#plugins where id = :id';
  225. $data = array(':id' => $plugin_id);
  226. if ($this->profiler_enabled) Profiler::setDAOMethod(__METHOD__);
  227. $stmt = $this->execute($q, $data);
  228. return $this->getDataIsReturned($stmt);
  229. }
  230. }