PageRenderTime 26ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/oc-includes/osclass/plugins.php

https://code.google.com/
PHP | 479 lines | 379 code | 72 blank | 28 comment | 69 complexity | 2e2af5a4f83659d57a518998a3090956 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php if ( ! defined('ABS_PATH')) exit('ABS_PATH is not loaded. Direct access is not allowed.');
  2. /*
  3. * OSCLass รข&#x20AC;&#x201C; software for creating and publishing online classified
  4. * advertising platforms
  5. *
  6. * Copyright (C) 2010 OSCLASS
  7. *
  8. * This program is free software: you can redistribute it and/or
  9. * modify it under the terms of the GNU Affero General Public License
  10. * as published by the Free Software Foundation, either version 3 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. class Plugins
  22. {
  23. private static $hooks ;
  24. function __construct() {}
  25. static function runHook($hook)
  26. {
  27. $args = func_get_args();
  28. array_shift($args);
  29. if(isset(self::$hooks[$hook])) {
  30. for($priority = 0;$priority<=10;$priority++) {
  31. if(isset(self::$hooks[$hook][$priority]) && is_array(self::$hooks[$hook][$priority])) {
  32. foreach(self::$hooks[$hook][$priority] as $fxName) {
  33. if(is_callable($fxName)) {
  34. call_user_func_array($fxName, $args);
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. static function applyFilter($hook)
  42. {
  43. $args = func_get_args();
  44. $hook = array_shift($args);
  45. if(isset($args[0])) {
  46. $content = $args[0];
  47. } else {
  48. $content = '';
  49. }
  50. if(isset(self::$hooks[$hook])) {
  51. for($priority = 0;$priority<=10;$priority++) {
  52. if(isset(self::$hooks[$hook][$priority]) && is_array(self::$hooks[$hook][$priority])) {
  53. foreach(self::$hooks[$hook][$priority] as $fxName) {
  54. if(is_callable($fxName)) {
  55. $content = call_user_func_array($fxName, $args);
  56. $args[0] = $content;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return $content;
  63. }
  64. static function isInstalled($plugin)
  65. {
  66. if( in_array($plugin, self::listInstalled()) ) {
  67. return true ;
  68. }
  69. return false ;
  70. }
  71. static function isEnabled($plugin)
  72. {
  73. if( in_array($plugin, self::listEnabled()) ) {
  74. return true ;
  75. }
  76. return false ;
  77. }
  78. static function listAll()
  79. {
  80. $plugins = array();
  81. $pluginsPath = osc_plugins_path();
  82. $dir = opendir($pluginsPath);
  83. while($file = readdir($dir)) {
  84. if(preg_match('/^[a-zA-Z0-9_]+$/', $file, $matches)) {
  85. // This has to change in order to catch any .php file
  86. $pluginPath = $pluginsPath . "$file/index.php";
  87. if(file_exists($pluginPath)) {
  88. $plugins[] = $file."/index.php";
  89. } else {
  90. trigger_error(sprintf(__('Plugin %s is missing the index.php file %s'), $file, $pluginPath));
  91. }
  92. }
  93. }
  94. closedir($dir);
  95. return $plugins;
  96. }
  97. static function loadActive()
  98. {
  99. $data['s_value'] = osc_active_plugins() ;
  100. $plugins_list = unserialize($data['s_value']);
  101. if(is_array($plugins_list)) {
  102. foreach($plugins_list as $plugin_name) {
  103. $pluginPath = osc_plugins_path() . $plugin_name;
  104. if(file_exists($pluginPath)) {
  105. //This should include the file and adds the hooks
  106. include_once $pluginPath;
  107. }
  108. }
  109. }
  110. }
  111. static function listInstalled()
  112. {
  113. $p_array = array();
  114. $data['s_value'] = osc_installed_plugins() ;
  115. $plugins_list = unserialize($data['s_value']) ;
  116. if( is_array($plugins_list) ) {
  117. foreach($plugins_list as $plugin_name) {
  118. $p_array[] = $plugin_name ;
  119. }
  120. }
  121. return $p_array;
  122. }
  123. static function listEnabled()
  124. {
  125. $p_array = array() ;
  126. $data['s_value'] = osc_active_plugins() ;
  127. $plugins_list = unserialize($data['s_value']) ;
  128. if( is_array($plugins_list) ) {
  129. foreach($plugins_list as $plugin_name) {
  130. $p_array[] = $plugin_name;
  131. }
  132. }
  133. return $p_array;
  134. }
  135. static function findByUpdateURI($uri) {
  136. $plugins = Plugins::listAll();
  137. foreach($plugins as $p) {
  138. $info = Plugins::getInfo($p);
  139. if($info['plugin_update_uri']==$uri) {
  140. return $p;
  141. }
  142. }
  143. return false;
  144. }
  145. static function resource($path)
  146. {
  147. $fullPath = osc_plugins_path() . $path;
  148. return file_exists($fullPath) ? $fullPath : false;
  149. }
  150. static function register($path, $function)
  151. {
  152. $path = str_replace(osc_plugins_path(), '', $path);
  153. self::addHook('install_' . $path, $function);
  154. }
  155. static function install($path)
  156. {
  157. $data['s_value'] = osc_installed_plugins() ;
  158. $plugins_list = unserialize($data['s_value']) ;
  159. if( is_array($plugins_list) ) {
  160. // check if the plugin is already installed
  161. if( in_array($path, $plugins_list) ) {
  162. return array('error_code' => 'error_installed') ;
  163. }
  164. }
  165. if( !file_exists(osc_plugins_path() . $path) ) {
  166. return array('error_code' => 'error_file') ;
  167. }
  168. // check if there are spaces when you include the plugin
  169. ob_start() ;
  170. include_once( osc_plugins_path() . $path ) ;
  171. if ( ob_get_length() > 0 ) {
  172. return array('error_code' => 'error_output', 'output' => ob_get_clean()) ;
  173. }
  174. ob_end_clean() ;
  175. try {
  176. self::runHook('install_' . $path) ;
  177. } catch(Exception $e) {
  178. return array('error_code' => 'custom_error' ,'msg' => $e->getMessage()) ;
  179. }
  180. if( !self::activate($path) ) {
  181. return array('error_code' => '') ;
  182. }
  183. $plugins_list[] = $path ;
  184. $data['s_value'] = serialize($plugins_list) ;
  185. $condition = array( 's_section' => 'osclass', 's_name' => 'installed_plugins') ;
  186. Preference::newInstance()->update($data, $condition) ;
  187. return true ;
  188. }
  189. static function uninstall($path)
  190. {
  191. $data['s_value'] = osc_installed_plugins() ;
  192. $plugins_list = unserialize($data['s_value']) ;
  193. $path = str_replace(osc_plugins_path(), '', $path);
  194. if( !is_array($plugins_list) ) {
  195. return false ;
  196. }
  197. if( !self::deactivate($path) ) {
  198. return false ;
  199. }
  200. self::runHook($path . '_uninstall') ;
  201. foreach($plugins_list as $k => $v) {
  202. if($v == $path) {
  203. unset($plugins_list[$k]) ;
  204. }
  205. }
  206. $data['s_value'] = serialize($plugins_list) ;
  207. $condition = array( 's_section' => 'osclass', 's_name' => 'installed_plugins') ;
  208. Preference::newInstance()->update($data, $condition) ;
  209. $plugin = self::getInfo($path) ;
  210. self::cleanCategoryFromPlugin($plugin['short_name']) ;
  211. return true ;
  212. }
  213. static function activate($path)
  214. {
  215. $data['s_value'] = osc_active_plugins() ;
  216. $plugins_list = unserialize($data['s_value']);
  217. if( is_array($plugins_list) ) {
  218. // check if the plugin is already active
  219. if( in_array($path, $plugins_list) ) {
  220. return false ;
  221. }
  222. }
  223. $plugins_list[] = $path ;
  224. $data['s_value'] = serialize($plugins_list) ;
  225. $condition = array( 's_section' => 'osclass', 's_name' => 'active_plugins') ;
  226. Preference::newInstance()->update($data, $condition) ;
  227. self::reload() ;
  228. self::runHook($path . '_enable') ;
  229. return true ;
  230. }
  231. static function deactivate($path)
  232. {
  233. $data['s_value'] = osc_active_plugins() ;
  234. $plugins_list = unserialize($data['s_value']);
  235. $path = str_replace(osc_plugins_path(), '', $path) ;
  236. // check if there is some plugin enabled
  237. if( !is_array($plugins_list) ) {
  238. return false ;
  239. }
  240. // remove $path from the active plugins list
  241. foreach($plugins_list as $k => $v) {
  242. if($v == $path) {
  243. unset($plugins_list[$k]) ;
  244. }
  245. }
  246. self::runHook($path . '_disable') ;
  247. // update t_preference field for active plugins
  248. $data['s_value'] = serialize($plugins_list) ;
  249. $condition = array( 's_section' => 'osclass', 's_name' => 'active_plugins') ;
  250. Preference::newInstance()->update($data, $condition) ;
  251. self::reload() ;
  252. return true ;
  253. }
  254. static function isThisCategory($name, $id)
  255. {
  256. return PluginCategory::newInstance()->isThisCategory($name, $id);
  257. }
  258. static function getInfo($plugin)
  259. {
  260. $s_info = file_get_contents(osc_plugins_path() . $plugin);
  261. $info = array();
  262. if( preg_match('|Plugin Name:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  263. $info['plugin_name'] = trim($match[1]);
  264. } else {
  265. $info['plugin_name'] = $plugin;
  266. }
  267. if( preg_match('|Plugin URI:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  268. $info['plugin_uri'] = trim($match[1]);
  269. } else {
  270. $info['plugin_uri'] = "";
  271. }
  272. if( preg_match('|Plugin update URI:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  273. $info['plugin_update_uri'] = trim($match[1]);
  274. } else {
  275. $info['plugin_update_uri'] = "";
  276. }
  277. if( preg_match('|Description:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  278. $info['description'] = trim($match[1]);
  279. } else {
  280. $info['description'] = "";
  281. }
  282. if( preg_match('|Version:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  283. $info['version'] = trim($match[1]);
  284. } else {
  285. $info['version'] = "";
  286. }
  287. if( preg_match('|Author:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  288. $info['author'] = trim($match[1]);
  289. } else {
  290. $info['author'] = "";
  291. }
  292. if( preg_match('|Author URI:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  293. $info['author_uri'] = trim($match[1]);
  294. } else {
  295. $info['author_uri'] = "";
  296. }
  297. if( preg_match('|Short Name:([^\\r\\t\\n]*)|i', $s_info, $match) ) {
  298. $info['short_name'] = trim($match[1]);
  299. } else {
  300. $info['short_name'] = $info['plugin_name'];
  301. }
  302. $info['filename'] = $plugin;
  303. return $info;
  304. }
  305. static function checkUpdate($plugin) {
  306. $info = Plugins::getInfo($plugin);
  307. return osc_check_plugin_update($info['plugin_update_uri'], $info['version']);
  308. }
  309. static function configureView($path)
  310. {
  311. $plugin = str_replace(osc_plugins_path(), '', $path);
  312. if(stripos($plugin, ".php")===FALSE) {
  313. $plugins_list = unserialize(osc_active_plugins());
  314. if(is_array($plugins_list)) {
  315. foreach($plugins_list as $p){
  316. $data = self::getInfo($p);
  317. if($plugin == $data['plugin_name']) {
  318. $plugin = $p ;
  319. break ;
  320. }
  321. }
  322. }
  323. }
  324. header('Location: '.osc_plugin_configure_url($plugin));
  325. exit;
  326. }
  327. static function cleanCategoryFromPlugin($plugin)
  328. {
  329. $dao_pluginCategory = new PluginCategory() ;
  330. $dao_pluginCategory->delete(array('s_plugin_name' => $plugin)) ;
  331. unset($dao_pluginCategory) ;
  332. }
  333. static function addToCategoryPlugin($categories, $plugin)
  334. {
  335. $dao_pluginCategory = new PluginCategory() ;
  336. $dao_category = new Category() ;
  337. if(!empty($categories)) {
  338. foreach($categories as $catId)
  339. {
  340. $result = $dao_pluginCategory->isThisCategory($plugin, $catId);
  341. if($result==0) {
  342. $fields = array() ;
  343. $fields['s_plugin_name'] = $plugin ;
  344. $fields['fk_i_category_id'] = $catId ;
  345. $dao_pluginCategory->insert($fields) ;
  346. $subs = $dao_category->findSubcategories($catId);
  347. if(is_array($subs) && count($subs)>0) {
  348. $cats = array();
  349. foreach( $subs as $sub) {
  350. $cats[] = $sub['pk_i_id'];
  351. }
  352. self::addToCategoryPlugin($cats, $plugin) ;
  353. }
  354. }
  355. }
  356. }
  357. unset($dao_pluginCategory) ;
  358. unset($dao_category) ;
  359. }
  360. // Add a hook
  361. static function addHook($hook, $function, $priority = 5)
  362. {
  363. $hook = preg_replace('|/+|', '/', str_replace('\\', '/', $hook)) ;
  364. $plugin_path = str_replace('\\', '/', osc_plugins_path()) ;
  365. $hook = str_replace($plugin_path, '', $hook) ;
  366. $found_plugin = false;
  367. if(isset(self::$hooks[$hook])) {
  368. for($_priority = 0;$_priority<=10;$_priority++) {
  369. if(isset(self::$hooks[$hook][$_priority])) {
  370. foreach(self::$hooks[$hook][$_priority] as $fxName) {
  371. if($fxName==$function) {
  372. $found_plugin = true;
  373. break;
  374. }
  375. }
  376. }
  377. }
  378. }
  379. if(!$found_plugin) { self::$hooks[$hook][$priority][] = $function; }
  380. }
  381. static function removeHook($hook, $function)
  382. {
  383. for($priority = 0;$priority<=10;$priority++) {
  384. if(isset(self::$hooks[$hook][$priority])) {
  385. foreach(self::$hooks[$hook][$priority] as $k => $v) {
  386. if($v==$function) {
  387. unset(self::$hooks[$hook][$priority][$k]);
  388. }
  389. }
  390. }
  391. }
  392. }
  393. static function getActive()
  394. {
  395. return self::$hooks;
  396. }
  397. static function reload()
  398. {
  399. Preference::newInstance()->toArray();
  400. self::init();
  401. }
  402. static function init()
  403. {
  404. self::loadActive();
  405. }
  406. }
  407. ?>