PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/dynamic-widgets/classes/dynwid_class.php

https://bitbucket.org/Thane2376/death-edge.ru
PHP | 875 lines | 553 code | 90 blank | 232 comment | 132 complexity | 9340ad14e1d21ea7f1c868b5d775ec34 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * dynWid class
  4. *
  5. * @version $Id$
  6. * @copyright 2011 Jacco Drabbe
  7. */
  8. class dynWid {
  9. private $dbtable;
  10. public $device;
  11. public $dwoptions = array();
  12. public $dynwid_list;
  13. public $enabled;
  14. private $firstmessage = TRUE;
  15. public $ip_address;
  16. public $listmade = FALSE;
  17. public $overrule_maintype = array();
  18. private $registered_sidebars;
  19. public $registered_widget_controls;
  20. public $registered_widgets;
  21. public $removelist = array();
  22. public $sidebars;
  23. public $template;
  24. public $url;
  25. public $plugin_url;
  26. public $useragent;
  27. public $userrole;
  28. public $whereami;
  29. private $wpdb;
  30. /**
  31. * dynWid::__construct() Master class
  32. *
  33. */
  34. public function __construct() {
  35. if ( is_user_logged_in() ) {
  36. $this->userrole = $GLOBALS['current_user']->roles;
  37. } else {
  38. $this->userrole = array('anonymous');
  39. }
  40. $this->registered_sidebars = $GLOBALS['wp_registered_sidebars'];
  41. $this->registered_widget_controls = &$GLOBALS['wp_registered_widget_controls'];
  42. $this->registered_widgets = &$GLOBALS['wp_registered_widgets'];
  43. $this->sidebars = wp_get_sidebars_widgets();
  44. $this->useragent = $this->getBrowser();
  45. $this->ip_address = $this->getIP();
  46. // DB init
  47. $this->wpdb = $GLOBALS['wpdb'];
  48. $this->dbtable = $this->wpdb->prefix . DW_DB_TABLE;
  49. $query = "SHOW TABLES LIKE '" . $this->dbtable . "'";
  50. $result = $this->wpdb->get_var($query);
  51. $this->enabled = ( is_null($result) ) ? FALSE : TRUE;
  52. }
  53. /**
  54. * dynWid::__get() Overload get
  55. *
  56. * @param string $name
  57. * @return mixed
  58. */
  59. public function __get($name) {
  60. return $this->$name;
  61. }
  62. /**
  63. * dynWid::__isset() Overload isset
  64. *
  65. * @param mixed $name
  66. * @return boolean
  67. */
  68. public function __isset($name) {
  69. if ( isset($this->$name) ) {
  70. return TRUE;
  71. }
  72. return FALSE;
  73. }
  74. /**
  75. * dynWid::__set() Overload set
  76. *
  77. * @param string $name
  78. * @param mixed $value
  79. */
  80. public function __set($name, $value) {
  81. $this->$name = $value;
  82. }
  83. /**
  84. * dynWid::addChilds() Save child options
  85. *
  86. * @param string $widget_id ID of the widget
  87. * @param string $maintype Name of module
  88. * @param string $default Default module setting
  89. * @param array $act Parent options
  90. * @param array $childs Options
  91. */
  92. public function addChilds($widget_id, $maintype, $default, $act, $childs) {
  93. $child_act = array();
  94. foreach ( $childs as $opt ) {
  95. if ( in_array($opt, $act) ) {
  96. $childs_act[ ] = $opt;
  97. }
  98. }
  99. $this->addMultiOption($widget_id, $maintype, $default, $childs_act);
  100. }
  101. /**
  102. * dynWid::addDate() Saves date options
  103. *
  104. * @param string $widget_id ID of the widget
  105. * @param array $dates Dates
  106. */
  107. public function addDate($widget_id, $dates) {
  108. $query = "INSERT INTO " . $this->dbtable . "
  109. (widget_id, maintype, name, value)
  110. VALUES
  111. ('" . $widget_id . "', 'date', 'default', '0')";
  112. $this->wpdb->query($query);
  113. foreach ( $dates as $name => $date ) {
  114. $query = "INSERT INTO " . $this->dbtable . "
  115. (widget_id, maintype, name, value)
  116. VALUES
  117. ('" . esc_sql($widget_id) . "', 'date', '" . esc_sql($name) . "', '" . esc_sql($date) . "')";
  118. $this->wpdb->query($query);
  119. }
  120. }
  121. /**
  122. * dynWid::addIPs() Saves IP options
  123. *
  124. * @param string $widget_id ID of the widget
  125. * @param array $default Default setting
  126. * @param string $ips IPs
  127. */
  128. public function addIPs($widget_id, $default, $ips) {
  129. $value = serialize($ips);
  130. if ( $default == 'no' ) {
  131. $query = "INSERT INTO " . $this->dbtable . "
  132. (widget_id, maintype, name, value)
  133. VALUES
  134. ('" . esc_sql($widget_id) . "', 'ip', 'default', '0')";
  135. $this->wpdb->query($query);
  136. }
  137. $query = "INSERT INTO " . $this->dbtable . "
  138. (widget_id, maintype, name, value)
  139. VALUES
  140. ('" . esc_sql($widget_id) . "', 'ip', 'ip', '" . $value . "')";
  141. $this->wpdb->query($query);
  142. }
  143. /**
  144. * dynWid::addUrls() Saves url options
  145. *
  146. * @param string $widget_id ID of the widget
  147. * @param array $default Default setting
  148. * @param string $urls URLs
  149. */
  150. public function addUrls($widget_id, $default, $urls) {
  151. $value = serialize($urls);
  152. if ( $default == 'no' ) {
  153. $query = "INSERT INTO " . $this->dbtable . "
  154. (widget_id, maintype, name, value)
  155. VALUES
  156. ('" . esc_sql($widget_id) . "', 'url', 'default', '0')";
  157. $this->wpdb->query($query);
  158. }
  159. $query = "INSERT INTO " . $this->dbtable . "
  160. (widget_id, maintype, name, value)
  161. VALUES
  162. ('" . esc_sql($widget_id) . "', 'url', 'url', '" . $value . "')";
  163. $this->wpdb->query($query);
  164. }
  165. /**
  166. * dynWid::addMultiOption() Save multi (complex) options
  167. *
  168. * @param string $widget_id ID of the widget
  169. * @param string $maintype Name of the module
  170. * @param string $default Default setting
  171. * @param array $act Options
  172. */
  173. public function addMultiOption($widget_id, $maintype, $default, $act = array()) {
  174. $insert = TRUE;
  175. if ( $default == 'no' ) {
  176. $opt_default = '0';
  177. $opt_act = '1';
  178. } else {
  179. $opt_default = '1';
  180. $opt_act = '0';
  181. }
  182. // Check single-post or single-option coming from posts or tags screen to prevent database polution
  183. $types = array();
  184. $args = array(
  185. 'public' => TRUE,
  186. '_builtin' => FALSE
  187. );
  188. $post_types = get_post_types($args, 'objects', 'and');
  189. foreach ( array_keys($post_types) as $t ){
  190. $types[ ] = $t . '-post';
  191. }
  192. $post_types = array_merge( $types, array('single-post', 'single-tag') );
  193. if ( in_array($maintype, $post_types) ) {
  194. $query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype . "' AND name = 'default'";
  195. $count = $this->wpdb->get_var($query);
  196. if ( $count > 0 ) {
  197. $insert = FALSE;
  198. }
  199. }
  200. if ( $insert ) {
  201. $query = "INSERT INTO " . $this->dbtable . "
  202. (widget_id, maintype, name, value)
  203. VALUES
  204. ('" . esc_sql($widget_id) . "', '" . esc_sql($maintype) . "', 'default', '" . esc_sql($opt_default) . "')";
  205. $this->wpdb->query($query);
  206. }
  207. foreach ( $act as $option ) {
  208. $query = "INSERT INTO " . $this->dbtable . "
  209. (widget_id, maintype, name, value)
  210. VALUES
  211. ('" . esc_sql($widget_id) . "', '" . esc_sql($maintype) . "', '" . esc_sql($option) . "', '" . esc_sql($opt_act) . "')";
  212. $this->wpdb->query($query);
  213. }
  214. }
  215. /**
  216. * dynWid::addSingleOption() Save single (simple) options
  217. *
  218. * @param string $widget_id ID of the widget
  219. * @param string $maintype Name of the module
  220. * @param integer $value Default setting
  221. */
  222. public function addSingleOption($widget_id, $maintype, $value = '0') {
  223. $query = "INSERT INTO " . $this->dbtable . "
  224. (widget_id, maintype, value)
  225. VALUES
  226. ('" . esc_sql($widget_id) . "', '" . esc_sql($maintype) . "', '" . esc_sql($value) . "')";
  227. $this->wpdb->query($query);
  228. }
  229. /**
  230. * dynWid::checkWPhead() Checks for wp_head()
  231. *
  232. * @return integer
  233. */
  234. public function checkWPhead() {
  235. $ct = current_theme_info();
  236. $headerfile = $ct->template_dir . '/header.php';
  237. if ( file_exists($headerfile) ) {
  238. $buffer = file_get_contents($headerfile);
  239. if ( strpos($buffer, 'wp_head()') ) {
  240. // wp_head() found
  241. return 1;
  242. } else {
  243. // wp_head() not found
  244. return 0;
  245. }
  246. } else {
  247. // wp_head() unable to determine
  248. return 2;
  249. }
  250. }
  251. /**
  252. * dynWid::createList() Creates full list of options
  253. *
  254. */
  255. private function createList() {
  256. $this->dynwid_list = array();
  257. foreach ( $this->sidebars as $sidebar_id => $widgets ) {
  258. if ( count($widgets) > 0 ) {
  259. foreach ( $widgets as $widget_id ) {
  260. if ( $this->hasOptions($widget_id) ) {
  261. $this->dynwid_list[ ] = $widget_id;
  262. }
  263. } // END foreach widgets
  264. }
  265. } // END foreach sidebars
  266. }
  267. /**
  268. * dynWid::deleteOption() Removes option
  269. *
  270. * @param string $widget_id ID of widget
  271. * @param string $maintype Name of module
  272. * @param string $name Name of option
  273. */
  274. public function deleteOption($widget_id, $maintype, $name = '') {
  275. $query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "' AND maintype = '" . $maintype ."'";
  276. if (! empty($name) ) {
  277. $query .= " AND name = '" . $name . "'";
  278. }
  279. $this->wpdb->query($query);
  280. }
  281. /**
  282. * dynWid::detectPage() Page detection
  283. *
  284. * @return string
  285. */
  286. public function detectPage() {
  287. // First we register the Path URL
  288. $this->url = $_SERVER['REQUEST_URI'];
  289. if ( is_front_page() && get_option('show_on_front') == 'posts' ) {
  290. return 'front-page';
  291. } else if ( is_home() && get_option('show_on_front') == 'page' ) {
  292. return 'front-page';
  293. } else if ( is_attachment() ) {
  294. return 'attachment'; // must be before is_single(), otherwise detects as 'single'
  295. } else if ( is_single() ) {
  296. return 'single';
  297. } else if ( is_page() ) {
  298. return 'page';
  299. } else if ( is_author() ) {
  300. return 'author';
  301. } else if ( is_category() ) {
  302. return 'category';
  303. } else if ( is_tag() ) {
  304. return 'tag';
  305. } else if ( function_exists('is_post_type_archive') && is_post_type_archive() ) {
  306. return 'cp_archive'; // must be before is_archive(), otherwise detects as 'archive' in WP 3.1.0
  307. } else if ( function_exists('is_tax') && is_tax() ) {
  308. return 'tax_archive';
  309. } else if ( is_archive() && ! is_category() && ! is_author() && ! is_tag() ) {
  310. return 'archive';
  311. } else if ( function_exists('bbp_is_single_user') && (bbp_is_single_user() || bbp_is_single_user_edit()) ) { // must be before is_404(), otherwise bbPress profile page is detected as 'e404'.
  312. return 'bbp_profile';
  313. } else if ( is_404() ) {
  314. return 'e404';
  315. } else if ( is_search() ) {
  316. return 'search';
  317. } else if ( function_exists('is_pod_page') && is_pod_page() ) {
  318. return 'pods';
  319. } else {
  320. return 'undef';
  321. }
  322. }
  323. /**
  324. * dynWid::dump() Dump file creation
  325. *
  326. */
  327. public function dump() {
  328. echo "wp version: " . $GLOBALS['wp_version'] . "\n";
  329. echo "wp_head: " . $this->checkWPhead() . "\n";
  330. echo "dw version: " . DW_VERSION . "\n";
  331. echo "php version: " . PHP_VERSION . "\n";
  332. echo "\n";
  333. echo "front: " . get_option('show_on_front') . "\n";
  334. if ( get_option('show_on_front') == 'page' ) {
  335. echo "front page: " . get_option('page_on_front') . "\n";
  336. echo "posts page: " . get_option('page_for_posts') . "\n";
  337. }
  338. echo "\n";
  339. echo "list: \n";
  340. $list = array();
  341. $this->createList();
  342. foreach ( $this->dynwid_list as $widget_id ) {
  343. $list[$widget_id] = strip_tags($this->getName($widget_id));
  344. }
  345. print_r($list);
  346. echo "wp_registered_widgets: \n";
  347. print_r($this->registered_widgets);
  348. echo "options: \n";
  349. print_r( $this->getOpt('%', NULL) );
  350. echo "\n";
  351. echo serialize($this->getOpt('%', NULL));
  352. }
  353. /**
  354. * dynWid::dumpOpt() Debug dump option
  355. *
  356. * @param object $opt
  357. */
  358. public function dumpOpt($opt) {
  359. if ( DW_DEBUG && count($opt) > 0 ) {
  360. var_dump($opt);
  361. }
  362. }
  363. // replacement for createList() to make the worker faster
  364. /**
  365. * dynWid::dwList() Option list creation
  366. *
  367. * @param string $whereami Page
  368. */
  369. public function dwList($whereami) {
  370. $this->dynwid_list = array();
  371. if ( $whereami == 'home' ) {
  372. $whereami = 'page';
  373. }
  374. $query = "SELECT DISTINCT widget_id FROM " . $this->dbtable . "
  375. WHERE maintype LIKE '" . $whereami . "%'";
  376. if ( count($this->overrule_maintype) > 0 ) {
  377. $query .= " OR maintype IN ";
  378. $q = array();
  379. foreach ( $this->overrule_maintype as $omt ) {
  380. $q[ ] = "'" . $omt . "'";
  381. }
  382. $query .= "(" . implode(', ', $q) . ")";
  383. }
  384. $results = $this->wpdb->get_results($query);
  385. foreach ( $results as $myrow ) {
  386. $this->dynwid_list[ ] = $myrow->widget_id;
  387. }
  388. }
  389. /**
  390. * dynWid::getBrowser() Browser detection
  391. *
  392. * @return string
  393. */
  394. private function getBrowser() {
  395. global $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome;
  396. if ( $is_gecko ) {
  397. return 'gecko';
  398. } else if ( $is_IE ) {
  399. if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE ) {
  400. return 'msie6';
  401. } else {
  402. return 'msie';
  403. }
  404. } else if ( $is_opera ) {
  405. return 'opera';
  406. } else if ( $is_NS4 ) {
  407. return 'ns';
  408. } else if ( $is_safari ) {
  409. return 'safari';
  410. } else if ( $is_chrome ) {
  411. return 'chrome';
  412. } else {
  413. return 'undef';
  414. }
  415. }
  416. /**
  417. * dynWid::getDWOpt() Gets SQL object used in DWOpts
  418. *
  419. * @param string $widget_id ID of widget
  420. * @param string $maintype Name of module
  421. * @return object
  422. */
  423. public function getDWOpt($widget_id, $maintype) {
  424. if ( $maintype == 'home' ) {
  425. $maintype = 'page';
  426. }
  427. $query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
  428. WHERE widget_id LIKE '" . $widget_id . "'
  429. AND maintype LIKE '" . $maintype . "%'
  430. ORDER BY maintype, name";
  431. $results = new DWOpts($this->wpdb->get_results($query), $maintype);
  432. return $results;
  433. }
  434. private function getIP() {
  435. $ip = $_SERVER['REMOTE_ADDR'];
  436. $this->message( 'Raw IP: ' . $ip );
  437. return ( strstr($ip, '.') !== FALSE ) ? $ip : NULL;
  438. }
  439. /**
  440. * dynWid::getModuleName() Full registration of the modules
  441. *
  442. */
  443. public function getModuleName() {
  444. $dwoptions = array();
  445. // I NEED PHP > 5.3!!
  446. DWModule::registerOption(DW_Archive::$option);
  447. DWModule::registerOption(DW_Attachment::$option);
  448. DWModule::registerOption(DW_Author::$option);
  449. DWModule::registerOption(DW_bbPress::$option);
  450. DWModule::registerOption(DW_BP::$option);
  451. DWModule::registerOption(DW_Browser::$option);
  452. DWModule::registerOption(DW_IP::$option);
  453. DWModule::registerOption(DW_Category::$option);
  454. DW_CustomPost::registerOption(NULL);
  455. DWModule::registerOption(DW_Date::$option);
  456. DWModule::registerOption(DW_Day::$option);
  457. DWModule::registerOption(DW_E404::$option);
  458. DWModule::registerOption(DW_Front_page::$option);
  459. DWModule::registerOption(DW_Device::$option);
  460. DWModule::registerOption(DW_Page::$option);
  461. DWModule::registerOption(DW_Pods::$option);
  462. DWModule::registerOption(DW_QT::$option);
  463. DWModule::registerOption(DW_Role::$option);
  464. DWModule::registerOption(DW_Search::$option);
  465. DWModule::registerOption(DW_Single::$option);
  466. DWModule::registerOption(DW_Tag::$option);
  467. DWModule::registerOption(DW_Tpl::$option);
  468. DWModule::registerOption(DW_URL::$option);
  469. DWModule::registerOption(DW_Week::$option);
  470. DWModule::registerOption(DW_WPSC::$option);
  471. DWModule::registerOption(DW_WPML::$option);
  472. }
  473. /**
  474. * dynWid::getName() Gets the lookup name
  475. *
  476. * @return string
  477. */
  478. public function getName($id, $type = 'W') {
  479. switch ( $type ) {
  480. case 'S':
  481. $lookup = $this->registered_sidebars;
  482. break;
  483. default:
  484. $lookup = $this->registered_widgets;
  485. // end default
  486. }
  487. if ( isset($lookup[$id]['name']) ) {
  488. $name = $lookup[$id]['name'];
  489. if ( $type == 'W' && isset($lookup[$id]['params'][0]['number']) ) {
  490. // Retrieve optional set title
  491. $number = $lookup[$id]['params'][0]['number'];
  492. $option_name = $lookup[$id]['callback'][0]->option_name;
  493. $option = get_option($option_name);
  494. if (! empty($option[$number]['title']) ) {
  495. $name .= ': <span class="in-widget-title">' . $option[$number]['title'] . '</span>';
  496. }
  497. }
  498. } else {
  499. $name = NULL;
  500. }
  501. return $name;
  502. }
  503. /**
  504. * dynWid::getOpt() Get SQL object of Opt
  505. *
  506. * @param string $widget_id ID of the widget
  507. * @param string $maintype Name of the module
  508. * @param boolean $admin Admin page
  509. * @return object
  510. */
  511. public function getOpt($widget_id, $maintype, $admin = TRUE) {
  512. $opt = array();
  513. if ( $admin ) {
  514. $query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
  515. WHERE widget_id LIKE '" . $widget_id . "'
  516. AND maintype LIKE '" . $maintype . "%'
  517. ORDER BY maintype, name";
  518. } else {
  519. if ( $maintype == 'home' ) {
  520. $maintype = 'page';
  521. }
  522. $query = "SELECT widget_id, maintype, name, value FROM " . $this->dbtable . "
  523. WHERE widget_id LIKE '" . $widget_id . "'
  524. AND (maintype LIKE '" . $maintype . "%'";
  525. if ( count($this->overrule_maintype) > 0 ) {
  526. $query .= " OR maintype IN (";
  527. $q = array();
  528. foreach ( $this->overrule_maintype as $omt ) {
  529. $q[ ] = "'" . $omt . "'";
  530. }
  531. $query .= implode(', ', $q);
  532. $query .= ")";
  533. }
  534. $query .= ") ORDER BY maintype, name";
  535. }
  536. $this->message('Q: ' . $query);
  537. $results = $this->wpdb->get_results($query);
  538. return $results;
  539. }
  540. /**
  541. * dynWid::getPostCatParents() Gets parents from post category
  542. *
  543. * @param array $post_category Categories
  544. * @return array
  545. */
  546. public function getPostCatParents($post_category) {
  547. // Getting all parents from the categories this post is in
  548. $parents = array();
  549. foreach ( $post_category as $id ) {
  550. $tp = $this->getTaxParents('category', array(), $id);
  551. // Now checking if the parent is already known
  552. foreach ( $tp as $p ) {
  553. if (! in_array($p, $parents) ) {
  554. $parents[ ] = $p;
  555. }
  556. }
  557. }
  558. return $parents;
  559. }
  560. /**
  561. * dynWid::getParents() Gets parents from posts or pages
  562. *
  563. * @param string $type Type
  564. * @param array $arr
  565. * @param integer $id Child ID
  566. * @return
  567. */
  568. public function getParents($type, $arr, $id) {
  569. if ( $type == 'page' ) {
  570. $obj = get_page($id);
  571. } else {
  572. $obj = get_post($id);
  573. }
  574. if ( $obj->post_parent > 0 ) {
  575. $arr[ ] = $obj->post_parent;
  576. $a = &$arr;
  577. $a = $this->getParents($type, $a, $obj->post_parent);
  578. }
  579. return $arr;
  580. }
  581. /**
  582. * dynWid::getTaxParents() Get parents for Taxonomy
  583. *
  584. * @param string $tax_name Taxonomy name
  585. * @param array $arr
  586. * @param integer $id Child ID
  587. * @return
  588. */
  589. public function getTaxParents($tax_name, $arr, $id) {
  590. $obj = get_term_by('id', $id, $tax_name);
  591. if ( $obj->parent > 0 ) {
  592. $arr[ ] = $obj->parent;
  593. $a = &$arr;
  594. $a = $this->getTaxParents($tax_name, $a, $obj->parent);
  595. }
  596. return $arr;
  597. }
  598. /**
  599. * dynWid::getURLPrefix() Gets the optional prefix this blog is under
  600. *
  601. * @return string
  602. */
  603. public function getURLPrefix() {
  604. $proto = ( is_ssl() ) ? 'https' : 'http';
  605. $name = ( isset($_SERVER['HTTP_HOST']) ) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
  606. $server = $proto . '://' . $name;
  607. $prefix = substr( home_url('/'), strlen($server) );
  608. // Apply filters
  609. $prefix = apply_filters('dynwid_urlprefix', $prefix);
  610. if ( $prefix != '/' ) {
  611. $prefix = substr($prefix, 0, strlen($prefix) - 1 );
  612. return $prefix;
  613. }
  614. return;
  615. }
  616. /**
  617. * dynWid::hasOptions() Checks if a widget has options set
  618. *
  619. * @param string $widget_id ID of the widget
  620. * @return boolean
  621. */
  622. public function hasOptions($widget_id) {
  623. $query = "SELECT COUNT(1) AS total FROM " . $this->dbtable . "
  624. WHERE widget_id = '" . $widget_id . "' AND
  625. maintype != 'individual'";
  626. $count = $this->wpdb->get_var($query);
  627. if ( $count > 0 ) {
  628. return TRUE;
  629. }
  630. return FALSE;
  631. }
  632. /**
  633. * dynWid::housekeeping() Housekeeping
  634. *
  635. */
  636. public function housekeeping() {
  637. $widgets = array_keys($this->registered_widgets);
  638. $query = "SELECT DISTINCT widget_id FROM " . $this->dbtable;
  639. $results = $this->wpdb->get_results($query);
  640. foreach ( $results as $myrow ) {
  641. if (! in_array($myrow->widget_id, $widgets) ) {
  642. $this->resetOptions($myrow->widget_id);
  643. }
  644. }
  645. }
  646. /**
  647. * dynWid::IPinRange() IP address in range
  648. *
  649. * @param $ip string IP address
  650. * @param $range string IP range
  651. * @return boolean
  652. */
  653. public function IPinRange($ip, $range) {
  654. /* Copyright 2008: Paul Gregg <pgregg@pgregg.com>
  655. * 10 January 2008
  656. * Version: 1.2
  657. *
  658. * Source website: http://www.pgregg.com/projects/php/ip_in_range/
  659. * Version 1.2
  660. */
  661. if ( strpos($range, '/') !== FALSE ) {
  662. // $range is in IP/NETMASK format
  663. list($range, $netmask) = explode('/', $range, 2);
  664. if ( strpos($netmask, '.') !== FALSE ) {
  665. // $netmask is a 255.255.0.0 format
  666. $netmask = str_replace('*', '0', $netmask);
  667. $netmask_dec = ip2long($netmask);
  668. return ( (ip2long($ip) & $netmask_dec) == (ip2long($range) & $netmask_dec) );
  669. } else {
  670. // $netmask is a CIDR size block
  671. // fix the range argument
  672. $x = explode('.', $range);
  673. while ( count($x) < 4 ) {
  674. $x[ ] = '0';
  675. }
  676. list( $a, $b, $c, $d ) = $x;
  677. $range = sprintf( "%u.%u.%u.%u", empty($a) ? '0' : $a, empty($b) ? '0' : $b, empty($c) ? '0' : $c, empty($d) ? '0' : $d );
  678. $range_dec = ip2long($range);
  679. $ip_dec = ip2long($ip);
  680. // Use math to create it
  681. $wildcard_dec = pow( 2, (32-$netmask) ) - 1;
  682. $netmask_dec = ~ $wildcard_dec;
  683. return ( ($ip_dec & $netmask_dec) == ($range_dec & $netmask_dec) );
  684. }
  685. } else {
  686. // range might be 255.255.*.* or 1.2.3.0-1.2.3.255
  687. if ( strpos($range, '*') !== FALSE ) { // a.b.*.* format
  688. // Just convert to A-B format by setting * to 0 for A and 255 for B
  689. $lower = str_replace('*', '0', $range);
  690. $upper = str_replace('*', '255', $range);
  691. $range = "$lower-$upper";
  692. }
  693. if ( strpos($range, '-') !== FALSE ) { // A-B format
  694. list( $lower, $upper ) = explode('-', $range, 2);
  695. $lower_dec = (float) sprintf( "%u", ip2long($lower) );
  696. $upper_dec = (float) sprintf( "%u", ip2long($upper) );
  697. $ip_dec = (float) sprintf( "%u",ip2long($ip) );
  698. return ( ($ip_dec >= $lower_dec) && ($ip_dec <= $upper_dec) );
  699. }
  700. // last resort
  701. if ( substr($range, -3) != '/32' ) {
  702. $range .= '/32';
  703. return $this->IPinRange($ip, $range);
  704. }
  705. $this->message('Range argument is not in 1.2.3.4/24 or 1.2.3.4/255.255.255.0 format');
  706. return FALSE;
  707. }
  708. }
  709. /**
  710. * dynWid::loadModules() Full load of all modules
  711. *
  712. */
  713. public function loadModules() {
  714. $dh = opendir(DW_MODULES);
  715. while ( ($file = readdir($dh)) !== FALSE) {
  716. if ( $file != '.' && $file != '..' && substr(strrchr($file, '_'), 1) == 'module.php' ) {
  717. include_once(DW_MODULES . $file);
  718. }
  719. }
  720. }
  721. /**
  722. * dynWid::log() Write text to debug log
  723. *
  724. */
  725. public function log($text) {
  726. if ( WP_DEBUG && DW_DEBUG ) {
  727. error_log($text);
  728. }
  729. }
  730. /**
  731. * dynWid::message() Debug message
  732. *
  733. * @param string $text
  734. */
  735. public function message($text) {
  736. if ( DW_DEBUG ) {
  737. if ( $this->firstmessage ) {
  738. echo "\n";
  739. $this->firstmessage = FALSE;
  740. }
  741. echo '<!-- ' . $text . ' //-->';
  742. echo "\n";
  743. }
  744. }
  745. /**
  746. * dynWid::registerOverrulers() Overrule module regsitering
  747. *
  748. */
  749. public function registerOverrulers() {
  750. include_once(DW_MODULES . 'browser_module.php');
  751. include_once(DW_MODULES . 'date_module.php');
  752. include_once(DW_MODULES . 'day_module.php');
  753. include_once(DW_MODULES . 'week_module.php');
  754. include_once(DW_MODULES . 'role_module.php');
  755. include_once(DW_MODULES . 'tpl_module.php');
  756. include_once(DW_MODULES . 'url_module.php');
  757. include_once(DW_MODULES . 'device_module.php');
  758. include_once(DW_MODULES . 'ip_module.php');
  759. DW_Browser::checkOverrule('DW_Browser');
  760. DW_Date::checkOverrule('DW_Date');
  761. DW_Day::checkOverrule('DW_Day');
  762. DW_Week::checkOverrule('DW_Week');
  763. DW_Role::checkOverrule('DW_Role');
  764. DW_Tpl::checkOverrule('DW_Tpl');
  765. DW_URL::checkOverrule('DW_URL');
  766. DW_URL::checkOverrule('DW_Device');
  767. DW_URL::checkOverrule('DW_IP');
  768. // WPML Plugin Support
  769. include_once(DW_MODULES . 'wpml_module.php');
  770. DW_WPML::detectLanguage();
  771. // QT Plugin Support
  772. include_once(DW_MODULES . 'qt_module.php');
  773. DW_QT::detectLanguage();
  774. }
  775. /**
  776. * dynWid::resetOptions() Full reset (remove) of options
  777. *
  778. * @param string $widget_id ID of the widget
  779. */
  780. public function resetOptions($widget_id) {
  781. $query = "DELETE FROM " . $this->dbtable . " WHERE widget_id = '" . $widget_id . "'";
  782. $this->wpdb->query($query);
  783. }
  784. }
  785. ?>