PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/e107_admin/frontpage.php

https://github.com/CasperGemini/e107
PHP | 685 lines | 464 code | 95 blank | 126 comment | 53 complexity | 009ba2a627804d1c41d3a227716da77b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2009 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. * Administration Area - Front page
  10. *
  11. * $URL$
  12. * $Id$
  13. *
  14. */
  15. /**
  16. * e107 Front page administration
  17. *
  18. * @package e107
  19. * @subpackage admin
  20. * @version $Id$;
  21. */
  22. require_once ('../class2.php');
  23. if(! getperms('G'))
  24. {
  25. header('location:'.e_BASE.'index.php');
  26. exit();
  27. }
  28. include_lan(e_LANGUAGEDIR.e_LANGUAGE.'/admin/lan_'.e_PAGE);
  29. $e_sub_cat = 'frontpage';
  30. require_once ('auth.php');
  31. $mes = e107::getMessage();
  32. $frontPref = e107::pref('core'); // Get prefs
  33. // Get list of possible options for front page
  34. $front_page['news'] = array('page' => 'news.php', 'title' => ADLAN_0);
  35. //$front_page['download'] = array('page' => 'download.php', 'title' => ADLAN_24); // Its a plugin now
  36. $front_page['wmessage'] = array('page' => 'index.php', 'title' => ADLAN_28);
  37. if($sql->db_Select('page', 'page_id, page_title', "menu_name=''"))
  38. {
  39. $front_page['custom']['title'] = FRTLAN_30;
  40. while($row = $sql->db_Fetch())
  41. {
  42. $front_page['custom']['page'][] = array('page' => 'page.php?'.$row['page_id'], 'title' => $row['page_title']);
  43. }
  44. }
  45. // Now let any plugins add to the options - must append to the $front_page array as above
  46. if(varset($frontPref['e_frontpage_list']))
  47. {
  48. foreach($frontPref['e_frontpage_list'] as $val)
  49. {
  50. if(is_readable(e_PLUGIN.$val.'/e_frontpage.php'))
  51. {
  52. require_once (e_PLUGIN.$val.'/e_frontpage.php');
  53. }
  54. }
  55. }
  56. // Make sure links relative to SITEURL
  57. foreach($front_page as &$front_value)
  58. {
  59. if(is_array($front_value['page']))
  60. { // Its a URL with multiple options
  61. foreach($front_value['page'] as &$multipage)
  62. {
  63. $multipage = str_replace(e_HTTP, '', $multipage);
  64. //if (substr($multipage, 0, 1) != '/') $multipage = '/'.$multipage;
  65. }
  66. }
  67. else
  68. {
  69. $front_value = str_replace(e_HTTP, '', $front_value);
  70. //if (substr($front_value, 0, 1) != '/') $front_value = '/'.$front_value;
  71. }
  72. }
  73. // Now sort out list of rules for display (based on $pref data to start with)
  74. $gotpub = FALSE;
  75. if(is_array($frontPref['frontpage']))
  76. {
  77. $i = 1;
  78. foreach($frontPref['frontpage'] as $class => $val)
  79. {
  80. if($class == 'all')
  81. {
  82. $class = e_UC_PUBLIC;
  83. $gotpub = TRUE;
  84. }
  85. if($val)
  86. { // Only add non-null pages
  87. $fp_settings[$i] = array('order' => $i, 'class' => $class, 'page' => $val, 'force' => varset($frontPref['frontpage_force'][$class], ''));
  88. $i ++;
  89. }
  90. }
  91. }
  92. else
  93. { // Legacy stuff to convert
  94. $fp_settings = array();
  95. $fp_settings[] = array('order' => 0, 'class' => e_UC_PUBLIC, 'page' => varset($frontPref['frontpage'], 'news.php'), 'force' => '');
  96. }
  97. if(!$gotpub)
  98. { // Need a 'default' setting - usually 'all'
  99. $fp_settings[] = array('order' => $i, 'class' => e_UC_PUBLIC, 'page' => (isset($frontPref['frontpage']['all']) ? $frontPref['frontpage']['all'] : 'news.php'), 'force' => '');
  100. }
  101. $fp_update_prefs = FALSE;
  102. /*
  103. Following code replaced - values not passed on image clicks with Firefox
  104. if(isset($_POST['fp_inc']))
  105. {
  106. $mv = intval($_POST['fp_inc']);
  107. echo "Increment: {$mv}<br />";
  108. if(($mv > 1) && ($mv <= count($fp_settings)))
  109. {
  110. $temp = $fp_settings[$mv - 1];
  111. $fp_settings[$mv - 1] = $fp_settings[$mv];
  112. $fp_settings[$mv] = $temp;
  113. $fp_update_prefs = TRUE;
  114. frontpage_adminlog('01', 'Inc '.$mv);
  115. }
  116. }
  117. elseif(isset($_POST['fp_dec']))
  118. {
  119. $mv = intval($_POST['fp_dec']);
  120. echo "Decrement: {$mv}<br />";
  121. if(($mv > 0) && ($mv < count($fp_settings)))
  122. {
  123. $temp = $fp_settings[$mv + 1];
  124. $fp_settings[$mv + 1] = $fp_settings[$mv];
  125. $fp_settings[$mv] = $temp;
  126. $fp_update_prefs = TRUE;
  127. frontpage_adminlog('01', 'Dec '.$mv);
  128. }
  129. }
  130. */
  131. if (isset($_POST))
  132. {
  133. foreach ($_POST as $k => $v)
  134. {
  135. $incDec = substr($k, 0, 6);
  136. $idNum = substr($k, 6);
  137. if ($incDec == 'fp_inc')
  138. {
  139. $mv = intval($idNum);
  140. if(($mv > 1) && ($mv <= count($fp_settings)))
  141. {
  142. $temp = $fp_settings[$mv - 1];
  143. $fp_settings[$mv - 1] = $fp_settings[$mv];
  144. $fp_settings[$mv] = $temp;
  145. $fp_update_prefs = TRUE;
  146. frontpage_adminlog('01', 'Inc '.$mv);
  147. }
  148. break;
  149. }
  150. elseif ($incDec == 'fp_dec')
  151. {
  152. $mv = intval($idNum);
  153. if(($mv > 0) && ($mv < count($fp_settings)))
  154. {
  155. $temp = $fp_settings[$mv + 1];
  156. $fp_settings[$mv + 1] = $fp_settings[$mv];
  157. $fp_settings[$mv] = $temp;
  158. $fp_update_prefs = TRUE;
  159. frontpage_adminlog('01', 'Dec '.$mv);
  160. }
  161. break;
  162. }
  163. }
  164. }
  165. // Edit an existing rule
  166. if(isset($_POST['fp_edit_rule']))
  167. {
  168. $_POST['type'] = (isset($_POST['edit']['all'])) ? 'all_users' : 'user_class';
  169. $_POST['class'] = key($_POST['edit']);
  170. }
  171. // Cancel Edit
  172. if(isset($_POST['fp_save_new']))
  173. { // Add or edit an existing rule here.
  174. // fp_order - zero for a new rule, non-zero if editing an existing rule
  175. // class - user class for rule
  176. // frontpage - radio button option indicating type of page (for home page)
  177. // frontpage_multipage[] - the other information for custom pages and similar - array index matches value of 'frontpage' when selected
  178. // frontpage_other - URL for 'other' home page
  179. // fp_force_page - radio button option indicating type of page (for post-login page)
  180. // fp_force_page_multipage[] - the other information for custom pages and similar - array index matches value of 'frontpage' when selected
  181. // fp_force_page_other - URL for forced post-login 'other' page
  182. if($_POST['frontpage'] == 'other')
  183. {
  184. $_POST['frontpage_other'] = trim($tp->toForm($_POST['frontpage_other']));
  185. $frontpage_value = $_POST['frontpage_other'] ? $_POST['frontpage_other'] : 'news.php';
  186. }
  187. else
  188. {
  189. if(is_array($front_page[$_POST['frontpage']]['page']))
  190. {
  191. $frontpage_value = $front_page[$_POST['frontpage']]['page'][$_POST['frontpage_multipage'][$_POST['frontpage']]]['page'];
  192. }
  193. else
  194. {
  195. $frontpage_value = $front_page[$_POST['frontpage']]['page'];
  196. }
  197. }
  198. if($_POST['fp_force_page'] == 'other')
  199. {
  200. $_POST['fp_force_page_other'] = trim($tp->toForm($_POST['fp_force_page_other']));
  201. $forcepage_value = $_POST['fp_force_page_other']; // A null value is allowable here
  202. }
  203. else
  204. {
  205. if(is_array($front_page[$_POST['fp_force_page']]['page']))
  206. {
  207. $forcepage_value = $front_page[$_POST['fp_force_page']]['page'][$_POST['fp_force_page_multipage'][$_POST['fp_force_page']]]['page'];
  208. }
  209. else
  210. {
  211. $forcepage_value = $front_page[$_POST['fp_force_page']]['page'];
  212. }
  213. }
  214. $temp = array('order' => intval($_POST['fp_order']), 'class' => $_POST['class'], 'page' => $frontpage_value, 'force' => trim($forcepage_value));
  215. if($temp['order'] == 0)
  216. { // New index to add
  217. $ind = 0;
  218. for($i = 1; $i <= count($fp_settings); $i ++)
  219. {
  220. if($fp_settings[$i]['class'] == $temp['class'])
  221. $ind = $i;
  222. }
  223. if($ind)
  224. {
  225. unset($fp_settings[$ind]); // Knock out duplicate definition for class
  226. echo "duplicate definition for class: ".$ind."<br />";
  227. }
  228. array_unshift($fp_settings, $temp); // Deliberately add twice
  229. array_unshift($fp_settings, $temp); // ....because re-indexed from zero
  230. unset($fp_settings[0]); // Then knock out index zero
  231. $fp_update_prefs = TRUE;
  232. frontpage_adminlog('02', "class => {$_POST['class']},[!br!]page => {$frontpage_value},[!br!]force => {$forcepage_value}");
  233. }
  234. elseif(array_key_exists($temp['order'], $fp_settings))
  235. {
  236. $fp_settings[$temp['order']] = $temp;
  237. $fp_update_prefs = TRUE;
  238. frontpage_adminlog('03', "posn => {$temp},[!br!]class => {$_POST['class']},[!br!]page => {$frontpage_value},[!br!]force => {$forcepage_value}");
  239. }
  240. else
  241. { // Someone playing games
  242. $mes->addError('Software error'); // TODO LAN
  243. }
  244. }
  245. if(isset($_POST['fp_delete_rule']))
  246. {
  247. if(isset($fp_settings[key($_POST['fp_delete_rule'])]))
  248. {
  249. $rule_no = key($_POST['fp_delete_rule']);
  250. $array_size = count($fp_settings);
  251. frontpage_adminlog('04', "Rule {$rule_no},[!br!]class => {$fp_settings[$rule_no]['class']},[!br!]page => {$fp_settings[$rule_no]['page']},[!br!]force => {$fp_settings[$rule_no]['force']}");
  252. unset($fp_settings[$rule_no]);
  253. while($rule_no < $array_size)
  254. { // Move up and renumber any entries after the deleted rule
  255. $fp_settings[$rule_no] = $fp_settings[$rule_no + 1];
  256. $rule_no ++;
  257. unset($fp_settings[$rule_no]);
  258. }
  259. $fp_update_prefs = TRUE;
  260. }
  261. }
  262. if($fp_update_prefs)
  263. { // Save the two arrays
  264. $fp_list = array();
  265. $fp_force = array();
  266. for($i = 1; $i <= count($fp_settings); $i ++)
  267. {
  268. $fp_list[$fp_settings[$i]['class']] = $fp_settings[$i]['page'];
  269. $fp_force[$fp_settings[$i]['class']] = $fp_settings[$i]['force'];
  270. }
  271. $corePrefs = e107::getConfig('core'); // Core Prefs Object.
  272. $corePrefs->set('frontpage', $fp_list);
  273. $corePrefs->set('frontpage_force', $fp_force);
  274. $result = $corePrefs->save(FALSE, TRUE);
  275. }
  276. // All updates complete now - latest data is in the $fp_settings, $fp_list and $fp_force arrays
  277. $fp = new frontpage($front_page);
  278. class frontpage
  279. {
  280. protected $frm;
  281. protected $frontPage = array(); // List of options for front page
  282. public function __construct($fp)
  283. {
  284. $this->frm = e107::getForm();
  285. $this->frontPage = $fp;
  286. $ns = e107::getRender();
  287. $mes = e107::getMessage();
  288. global $fp_settings;
  289. if(vartrue($_GET['mode']) == 'create')
  290. {
  291. $text = $this->edit_rule(array('order' => 0, 'class' => e_UC_PUBLIC, 'page' => 'news.php', 'force' => FALSE)); // Display edit form as well
  292. // $text .= $this->select_class($fp_settings, FALSE);
  293. $ns->tablerender(FRTLAN_PAGE_TITLE.SEP.FRTLAN_42, $text);
  294. }
  295. elseif(vartrue($_GET['id']))
  296. {
  297. $key = intval($_GET['id']);
  298. $text = $this->edit_rule($fp_settings[$key]); // Display edit form as well
  299. // $text .= $this->select_class($fp_settings, FALSE);
  300. $ns->tablerender(FRTLAN_PAGE_TITLE.SEP.FRTLAN_46, $text);
  301. }
  302. else
  303. { // Just show existing rules
  304. $ns->tablerender(FRTLAN_PAGE_TITLE.SEP.FRTLAN_13, $mes->render().$this->select_class($fp_settings, TRUE));
  305. }
  306. }
  307. /**
  308. * Show a list of existing rules, with edit/delete/move buttons, and optional button to add a new rule
  309. *
  310. * @param boolean $show_button - show the 'Add new rule' button if true
  311. *
  312. * @return string text for display
  313. */
  314. function select_class(&$fp_settings, $show_button = TRUE)
  315. {
  316. $frm = e107::getForm();
  317. // List of current settings
  318. $show_legend = $show_button ? " class='e-hideme'" : '';
  319. $text = "
  320. <form method='post' action='".e_SELF."'>
  321. <fieldset id='frontpage-settings'>
  322. <legend{$show_legend}>".FRTLAN_13."</legend>
  323. <table class='table adminlist'>
  324. <colgroup>
  325. <col style='width: 5%' />
  326. <col style='width: 25%' />
  327. <col style='width: 30%' />
  328. <col style='width: 25%' />
  329. <col style='width: 15%' />
  330. </colgroup>
  331. <thead>
  332. <tr>
  333. <th class='first'>".LAN_ORDER."</th>
  334. <th>".FRTLAN_53."</th>
  335. <th>".FRTLAN_49."</th>
  336. <th>".FRTLAN_35."</th>
  337. <th class='center last'>".LAN_OPTIONS."</th>
  338. </tr>
  339. </thead>
  340. <tbody>";
  341. foreach($fp_settings as $order => $current_value)
  342. {
  343. $title = e107::getUserClass()->uc_get_classname($current_value['class']);
  344. $text .= "
  345. <tr>
  346. <td class='right'>".$order."</td>
  347. <td>".$title."</td>
  348. <td>".$this->lookup_path($current_value['page'])."</td>
  349. <td>".$this->lookup_path($current_value['force'])."</td>
  350. <td class='center options last'>
  351. <div class='btn-group'>
  352. ".$frm->admin_button('fp_inc',$order,'up',ADMIN_UP_ICON)."
  353. ".$frm->admin_button('fp_dec',$order,'down',ADMIN_DOWN_ICON)."
  354. <a class='btn' title='".LAN_EDIT."' href='".e_SELF."?id=".$order."' >".ADMIN_EDIT_ICON."</a>
  355. ".$frm->admin_button('fp_delete_rule['.$order.']',$order,'',ADMIN_DELETE_ICON)."
  356. </div>
  357. </td>
  358. </tr>";
  359. }
  360. $text .= "
  361. </tbody>
  362. </table>";
  363. if($show_button)
  364. {
  365. $text .= "
  366. <div class='buttons-bar center'>
  367. <a href='".e_SELF."?mode=create' class='btn btn-success'>".FRTLAN_42."</a>
  368. </div>";
  369. }
  370. $text .= "
  371. </fieldset>
  372. </form>";
  373. return $text;
  374. }
  375. /**
  376. * Display form to add/edit rules
  377. *
  378. * @param array $rule_info - initial data (must be preset if new rule)
  379. *
  380. * @return string - text for display
  381. */
  382. function edit_rule($rule_info)
  383. {
  384. $is_other_home = TRUE;
  385. $is_other_force = TRUE;
  386. //$force_checked = $rule_info['force'] ? " checked='checked'" : '';
  387. $text_tmp_1 = '';
  388. $text_tmp_2 = '';
  389. foreach($this->frontPage as $front_key => $front_value)
  390. {
  391. //$type_selected = FALSE;
  392. $text_tmp_1 .= "
  393. <tr>
  394. ".$this->show_front_val('frontpage', $front_key, $front_value, $is_other_home, $rule_info['page'])."
  395. </tr>
  396. ";
  397. $text_tmp_2 .= "
  398. <tr>
  399. ".$this->show_front_val('fp_force_page', $front_key, $front_value, $is_other_force, $rule_info['force'])."
  400. </tr>
  401. ";
  402. }
  403. // <legend class='e-hideme'>".($rule_info['order'] ? FRTLAN_46 : FRTLAN_42)."</legend>
  404. $text = "
  405. <form method='post' action='".e_SELF."'>";
  406. $text .= '<ul class="nav nav-tabs" id="myTabs">
  407. <li class="active"><a data-toggle="tab" href="#home">'.FRTLAN_49.'</a></li>
  408. <li><a data-toggle="tab" href="#postlogin">'.FRTLAN_35.'</a></li>
  409. </ul>
  410. ';
  411. $text .= "
  412. <div class='tab-content'>
  413. <div class='tab-pane active' id='home'>
  414. <table class='table adminform'>
  415. <colgroup>
  416. <col style='width: 30%' />
  417. <col style='width: 70%' />
  418. </colgroup>
  419. <tbody>
  420. ".$text_tmp_1."
  421. <tr>
  422. ".$this->add_other('frontpage', $is_other_home, $rule_info['page'])."
  423. </tr>
  424. </tbody>
  425. </table>
  426. </div>
  427. <div class='tab-pane' id='postlogin'>
  428. <table class='table adminform'>
  429. <colgroup>
  430. <col style='width: 30%' />
  431. <col style='width: 70%' />
  432. </colgroup>
  433. <tbody>
  434. ".$text_tmp_2."
  435. <tr>
  436. ".$this->add_other('fp_force_page', $is_other_force, $rule_info['force'])."
  437. </tr>
  438. </tbody>
  439. </table>
  440. </div>
  441. </div>
  442. <div class='buttons-bar center'>
  443. ".$this->frm->hidden('fp_order', $rule_info['order'])."
  444. ".FRTLAN_43.": ".e107::getUserClass()->uc_dropdown('class', $rule_info['class'], 'public,guest,member,admin,main,classes')."
  445. ".$this->frm->admin_button('fp_save_new', LAN_UPDATE, 'update')."
  446. ".$this->frm->admin_button('fp_cancel', LAN_CANCEL, 'cancel')."
  447. </div>
  448. </form>
  449. ";
  450. return $text;
  451. }
  452. /**
  453. * Given a path string related to a choice, returns the 'type' (title) for it
  454. *
  455. * @param string $path
  456. *
  457. * @return string - title of option
  458. */
  459. function lookup_path($path)
  460. {
  461. foreach($this->frontPage as $front_value)
  462. {
  463. if(is_array($front_value['page']))
  464. { // Its a URL with multiple options
  465. foreach($front_value['page'] as $multipage)
  466. {
  467. if($path == $multipage['page'])
  468. {
  469. // return $front_value['title'].":".$path;
  470. return $front_value['title'].":".$multipage['title'];
  471. }
  472. }
  473. }
  474. else
  475. {
  476. if($path == $front_value['page'])
  477. {
  478. return $front_value['title'];
  479. }
  480. }
  481. }
  482. if(strlen($path))
  483. return FRTLAN_51.":".$path; // 'Other'
  484. else
  485. return LAN_NONE; // 'None'
  486. }
  487. /**
  488. * Show the selection options for a possible target of a rule
  489. *
  490. * @param string $ob_name - name of the radio button which selects this element
  491. * @param string $front_key
  492. * @param array|string $front_value - array of choices, or a single value
  493. * @param boolean $is_other - passed by reference - set if some other option is selected
  494. * @param string $current_setting - current value
  495. *
  496. * @return string - text for display
  497. */
  498. function show_front_val($ob_name, $front_key, $front_value, &$is_other, $current_setting)
  499. {
  500. $type_selected = FALSE;
  501. $text = '';
  502. // First, work out if the selection os one of these options
  503. if (is_array($front_value['page']))
  504. { // Its a URL with multiple options
  505. foreach($front_value['page'] as $multipage)
  506. {
  507. if($current_setting == $multipage['page'])
  508. {
  509. $type_selected = TRUE;
  510. $is_other = FALSE;
  511. }
  512. }
  513. }
  514. else
  515. {
  516. if($current_setting == $front_value['page'])
  517. {
  518. $type_selected = TRUE;
  519. $is_other = FALSE;
  520. }
  521. }
  522. // Now generate the display text - two table cells worth
  523. if (is_array($front_value['page']))
  524. { // Multiple options for same page name
  525. $text .= "
  526. <td>
  527. ".$this->frm->radio($ob_name, $front_key, $type_selected, array('label'=>$front_value['title']))."
  528. </td>
  529. <td>
  530. ";
  531. $text .= $this->frm->select_open($ob_name.'_multipage['.$front_key.']');
  532. foreach($front_value['page'] as $multipage_key => $multipage_value)
  533. {
  534. $text .= "\n".$this->frm->option($multipage_value['title'], $multipage_key, ($current_setting == $multipage_value['page']))."\n";
  535. }
  536. $text .= $this->frm->select_close();
  537. $text .= "</td>";
  538. }
  539. else
  540. { // Single option for URL
  541. $text .= "
  542. <td>
  543. ".$this->frm->radio($ob_name, $front_key, $type_selected, array('label'=>$front_value['title']))."
  544. </td>
  545. <td>&nbsp;</td>";
  546. }
  547. return $text;
  548. }
  549. /**
  550. * Provide the text for an 'other' option - a text box for URL entry
  551. *
  552. * @param string $ob_name - name of the radio button which selects this element
  553. * @param string $front_key
  554. * @param string $curval - current 'selected' value
  555. * @param string $cur_page - probably the secondary (e.g. custom page) value for any option that has one
  556. *
  557. * @return string - text for display
  558. */
  559. function add_other($ob_name, $cur_val, $cur_page)
  560. {
  561. $label = ($cur_val) ? "Disabled or Enter Custom URL:" : "Custom URL: ";
  562. return "
  563. <td>".$this->frm->radio($ob_name, 'other', $cur_val, array('label'=> $label))."</td>
  564. <td>".$this->frm->text($ob_name.'_other', ($cur_val ? $cur_page : ''), 150, "size=xxlarge&id={$ob_name}-other-txt")."</td>
  565. ";
  566. }
  567. }
  568. require_once(e_ADMIN.'footer.php');
  569. /**
  570. * Log event to admin log
  571. *
  572. * @param string $msg_num - exactly two numeric characters corresponding to a log message
  573. * @param string $woffle - information for the body of the log entre
  574. *
  575. * @return none
  576. */
  577. function frontpage_adminlog($msg_num = '00', $woffle = '')
  578. {
  579. e107::getAdminLog()->log_event('FRONTPG_'.$msg_num, $woffle, E_LOG_INFORMATIVE, '');
  580. }
  581. function frontpage_adminmenu()
  582. {
  583. $action = vartrue($_GET['mode'],'main');
  584. $var['main']['text'] = LAN_MANAGE;
  585. $var['main']['link'] = e_SELF;
  586. $var['create']['text'] = LAN_CREATE;
  587. $var['create']['link'] = e_SELF."?mode=create";
  588. show_admin_menu(FRTLAN_PAGE_TITLE, $action, $var);
  589. }
  590. ?>