form = "core-newspost-create-form"; // form id of the form that will have it's values saved. $pst->page = "newspost.php?create"; // display preset options on which page(s). $pst->id = "admin_newspost"; // ------------------------------ // done in class2: require_once(e_LANGUAGEDIR.e_LANGUAGE."/admin/lan_admin.php"); // maybe this should be put in class2.php when 'admin' is detected. $newspost = new admin_newspost(e_QUERY, $pst); e107::setRegistry('_newspost_admin', $newspost); $gen = new convert(); //Handle Ajax Calls if($newspost->ajax_observer()) exit; function headerjs() { $newspost = e107::getRegistry('_newspost_admin'); /* $ret .= ""; */ // TODO - move this to external JS when news becomes a plugin $ret .= " "; e107::getJs()->requireCoreLib('core/admin.js'); if($newspost->getAction() == 'cat') { $ret .= " "; } elseif ($newspost->getAction() == 'pref') { $ret .= " "; } $ret .= $newspost->_cal->load_files(); return $ret; } $e_sub_cat = 'news'; require_once('auth.php'); /* * Observe for delete action */ $newspost->observer(); /* * Show requested page */ $newspost->show_page(); /* OLD JS? Can't find references to this func echo " \n"; */ require_once("footer.php"); exit; // FIXME - advanced filter, ready to be chunked, cleaned up and pluginized some day... class admin_newspost { var $_request = array(); var $_cal = array(); var $_pst; var $_fields; var $_sort_order; var $_sort_link; var $fieldpref; var $news_categories; var $news_renderTypes = array(); public $error = false; function __construct($qry, $pstobj) { global $user_pref; $this->parseRequest($qry); require_once(e_HANDLER."cache_handler.php"); require_once(e_HANDLER."news_class.php"); require_once(e_HANDLER."calendar/calendar_class.php"); $this->_cal = new DHTML_Calendar(true); $this->_pst = $pstobj; $this->fieldpref = varset($user_pref['admin_news_columns'], array('news_id', 'news_title', 'news_author', 'news_render_type', 'options')); $this->fields = array( 'checkboxes' => array('title' => '', 'type' => null, 'width' => '3%', 'thclass' => 'center first', 'class' => 'center', 'nosort' => true, 'toggle' => 'news_selected', 'forced' => TRUE), 'news_id' => array('title' => LAN_NEWS_45, 'type' => 'number', 'width' => '5%', 'thclass' => 'center', 'class' => 'center', 'nosort' => false), 'news_title' => array('title' => NWSLAN_40, 'type' => 'text', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false), 'news_rewrite_string' => array('title' => 'SEF URL', 'type' => 'text', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false), 'user_name' => array('title' => LAN_NEWS_50, 'type' => 'text', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false), 'news_datestamp' => array('title' => LAN_NEWS_32, 'type' => 'datestamp', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false, 'parms' => 'mask=%A %d %B %Y'), 'category_name' => array('title' => NWSLAN_6, 'type' => 'text', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false), 'news_class' => array('title' => NWSLAN_22, 'type' => 'userclass', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false), 'news_render_type' => array('title' => LAN_NEWS_49, 'type' => 'number', 'width' => 'auto', 'thclass' => 'center', 'class' => null, 'nosort' => false), 'news_thumbnail' => array('title' => LAN_NEWS_22, 'type' => 'text', 'width' => 'auto', 'thclass' => '', 'class' => null, 'nosort' => false), 'news_sticky' => array('title' => LAN_NEWS_28, 'type' => 'boolean', 'width' => 'auto', 'thclass' => 'center', 'class' => 'center', 'nosort' => false), 'news_allow_comments' => array('title' => NWSLAN_15, 'type' => 'boolean', 'width' => 'auto', 'thclass' => 'center', 'class' => 'center', 'nosort' => false), 'news_comment_total' => array('title' => LAN_NEWS_60, 'type' => 'number', 'width' => '10%', 'thclass' => '', 'class' => null, 'nosort' => false), 'options' => array('title' => LAN_OPTIONS, 'type' => null, 'width' => '10%', 'thclass' => 'center last', 'class' => 'center', 'nosort' => true, 'forced' => TRUE) ); /* $ren_type = array(NWSLAN_75,NWSLAN_76,NWSLAN_77,NWSLAN_77." 2"); $r_array = array(); foreach($ren_type as $key=>$value) { $this->news_renderTypes[$key] = $value; }*/ $this->news_renderTypes = array(NWSLAN_75,NWSLAN_76,NWSLAN_77,NWSLAN_77." 2"); } function parseRequest($qry) { $tmp = explode(".", $qry); $action = varsettrue($tmp[0], 'main'); $sub_action = varset($tmp[1], ''); $id = isset($tmp[2]) && is_numeric($tmp[2]) ? intval($tmp[2]) : 0; $this->_sort_order = isset($tmp[2]) && !is_numeric($tmp[2]) ? $tmp[2] : 'desc'; $from = intval(varset($tmp[3],0)); unset($tmp); if ($this->_sort_order != 'asc') $this->_sort_order = 'desc'; $this->_sort_link = ($this->_sort_order) == 'asc' ? 'desc' : 'asc'; $this->_request = array($action, $sub_action, $id, $sort_order, $from); } function getAction() { return $this->_request[0]; } /** * @param string $action * @return admin_newspost */ function setAction($action) { $this->_request[0] = $action; return $this; } function getSubAction() { return $this->_request[1]; } /** * @param string $action * @return admin_newspost */ function setSubAction($action) { $this->_request[1] = $action; return $this; } function getId() { return $this->_request[2]; } /** * @param integer $id * @return admin_newspost */ function setId($id) { $this->_request[2] = intval($id); return $this; } function getSortOrder() { return $this->_request[3]; } function getFrom() { return $this->_request[4]; } function clear_cache() { $ecache = e107::getCache(); $ecache->clear("news.php"); //TODO change it to 'news_*' everywhere $ecache->clear("news_", false, true); //NEW global news cache prefix //$ecache->clear("nq_news_"); - supported by cache::clear() now //$ecache->clear("nomd5_news_"); supported by cache::clear() now $ecache->clear("othernews"); //TODO change it to 'news_othernews' everywhere $ecache->clear("othernews2"); //TODO change it to 'news_othernews2' everywhere return $this; } function clear_rwcache($sefstr = '') { /*if($sefstr) $sefstr = md5($sefstr); ecache::clear_sys("news_sefurl".$sefstr);*/ // news::clearRewriteCache($sefstr); } function set_rwcache($sefstr, $data) { /**$sefstr = md5($sefstr); if(is_array($data)) $data = e107::getArrayStorage()->WriteArray($data, false); ecache::set_sys("news_sefurl".$sefstr, $data, true);*/ // news::setRewriteCache($sefstr, $data); } function ajax_observer() { $method = 'ajax_exec_'.$this->getAction(); if(e_AJAX_REQUEST && method_exists($this, $method)) { $this->$method(); return true; } return false; } function observer() { e107::getDb()->db_Mark_Time('News Administration'); $this->news_categories = array(); if(e107::getDb()->db_Select('news_category', '*', (getperms('0') ? '' : 'category_manager IN ('.USERCLASS_LIST.')'))) { $this->news_categories = e107::getDb()->db_getList('ALL', FALSE, FALSE, 'category_id'); } //Required on create & savepreset action triggers if(isset($_POST['news_userclass']) && is_array($_POST['news_userclass'])) { $_POST['news_class'] = implode(",", $_POST['news_userclass']); unset($_POST['news_userclass']); } $main = getperms('0'); if(isset($_POST['delete']) && is_array($_POST['delete'])) { $this->_observe_delete(); } elseif(isset($_POST['execute_batch'])) { $this->process_batch($_POST['news_selected']); } elseif(isset($_POST['submit_news'])) { $this->_observe_submit_item($this->getSubAction(), $this->getId()); } elseif($main && isset($_POST['create_category'])) { $this->_observe_create_category(); } elseif($main && isset($_POST['update_category'])) { $this->_observe_update_category(); } elseif($main && isset($_POST['multi_update_category'])) { $this->_observe_multi_create_category(); } elseif($main && isset($_POST['save_prefs'])) { $this->_observe_save_prefs(); } elseif(isset($_POST['submitupload'])) { $this->_observe_upload(); } elseif(isset($_POST['news_comments_recalc'])) { $this->_observe_newsCommentsRecalc(); } if(isset($_POST['etrigger_ecolumns'])) //elseif fails. { $this->_observe_saveColumns(); } } function show_page() { // print_a($POST); switch ($this->getAction()) { case 'savepreset': case 'clr_preset': $this->_pst->save_preset('news_datestamp', false); // save and render result using unique name. Don't save item datestamp $_POST = array(); $this->parseRequest(''); $this->show_existing_items(); break; case 'create': $this->_pst->read_preset('admin_newspost'); //only works here because $_POST is used. $this->show_create_item(); break; case 'cat': if(!getperms('0|7')) { $this->noPermissions(); } $this->show_categories(); break; case 'sn': $this->show_submitted_news(); break; case 'pref': if(!getperms('0')) { $this->noPermissions(); } $this->show_news_prefs(); break; case 'maint' : if(!getperms('0')) { $this->noPermissions(); } $this->showMaintenance(); break; default: $this->show_existing_items(); break; } } function _observe_delete() { global $admin_log; //FIXME - SEF URL cache $tmp = array_keys($_POST['delete']); list($delete, $del_id) = explode("_", $tmp[0]); $del_id = intval($del_id); if(!$del_id) return false; $e107 = e107::getInstance(); switch ($delete) { case 'main': if ($e107->sql->db_Count('news','(*)',"WHERE news_id={$del_id}")) { e107::getEvent()->trigger("newsdel", $del_id); if($e107->sql->db_Delete("news", "news_id={$del_id}")) { $admin_log->log_event('NEWS_01',$del_id,E_LOG_INFORMATIVE,''); $this->show_message(NWSLAN_31." #".$del_id." ".NWSLAN_32, E_MESSAGE_SUCCESS); $this->clear_cache(); $data = array('method'=>'delete', 'table'=>'news', 'id'=>$del_id, 'plugin'=>'news', 'function'=>'delete'); $this->show_message(e107::getEvent()->triggerHook($data), E_MESSAGE_WARNING); admin_purge_related("news", $del_id); } } break; case 'category': if(!getperms('0|7')) $this->noPermissions(); if ($e107->sql->db_Count('news_category','(*)',"WHERE category_id={$del_id}")) { e107::getEvent()->trigger("newscatdel", $del_id); if ($e107->sql->db_Delete("news_category", "category_id={$del_id}")) { $admin_log->log_event('NEWS_02',$del_id,E_LOG_INFORMATIVE,''); $this->show_message(NWSLAN_33." #".$del_id." ".NWSLAN_32, E_MESSAGE_SUCCESS); $this->clear_cache(); } } break; case 'sn': if ($e107->sql->db_Delete("submitnews", "submitnews_id={$del_id}")) { $admin_log->log_event('NEWS_03',$del_id,E_LOG_INFORMATIVE,''); $this->show_message(NWSLAN_34." #".$del_id." ".NWSLAN_32); $this->clear_cache(); } break; default: return false; } return true; } function _observe_submit_item($sub_action, $id) { // ##### Format and submit item to DB $ix = new news; if($_POST['news_start']) { $tmp = explode("/", $_POST['news_start']); $_POST['news_start'] = mktime(0, 0, 0, $tmp[1], $tmp[0], $tmp[2]); } else { $_POST['news_start'] = 0; } if($_POST['news_end']) { $tmp = explode("/", $_POST['news_end']); $_POST['news_end'] = mktime(0, 0, 0, $tmp[1], $tmp[0], $tmp[2]); } else { $_POST['news_end'] = 0; } $matches = array(); if(preg_match('#(.*?)/(.*?)/(.*?) (.*?):(.*?):(.*?)$#', $_POST['news_datestamp'], $matches)) { $_POST['news_datestamp'] = mktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[1], $matches[3]); } else { $_POST['news_datestamp'] = time(); } if($_POST['update_datestamp']) { $_POST['news_datestamp'] = time(); } if ($id && $sub_action != "sn" && $sub_action != "upload") { $_POST['news_id'] = $id; } else { e107::getDb()->db_Update('submitnews', "submitnews_auth=1 WHERE submitnews_id ={$id}"); e107::getAdminLog()->log_event('NEWS_07', $id, E_LOG_INFORMATIVE,''); } if (!isset($_POST['cat_id'])) { $_POST['cat_id'] = 0; } $_POST['news_category'] = $_POST['cat_id']; if(!isset($this->news_categories[$_POST['news_category']])) { $this->noPermissions(); } /*if(isset($_POST['news_thumbnail'])) { $_POST['news_thumbnail'] = urldecode(basename($_POST['news_thumbnail'])); }*/ $tmp = explode(chr(35), $_POST['news_author']); $_POST['news_author'] = $tmp[0]; $ret = $ix->submit_item($_POST, !vartrue($_POST['create_edit_stay'])); if($ret['error']) { eMessage::getInstance()->mergeWithSession(); //merge with session messages eMessage::getInstance()->add(($id ? LAN_UPDATED_FAILED : LAN_CREATED_FAILED), E_MESSAGE_ERROR); return false; } $this->clear_cache(); if(isset($_POST['create_edit_stay']) && !empty($_POST['create_edit_stay'])) { if($this->getSubAction() != 'edit') { session_write_close(); $rurl = e_SELF.(varsettrue($ret['id']) ? "?create.edit.".$ret['id'] : ''); header('Location:'.($rurl ? $rurl : e_SELF)); exit; } } else { session_write_close(); header('Location:'.e_SELF); exit; } } function _observe_create_category() { if(!getperms('0|7')) { $this->noPermissions(); } //FIXME - lan, e_model based news administration model $this->error = false; if(empty($_POST['category_name'])) { $this->show_message('Validation Error: Missing Category name', E_MESSAGE_ERROR); $this->error = true; } // if(!empty($_POST['news_rewrite_string']) && preg_match('#[^\w\pL\-]#u', $_POST['news_rewrite_string'])) { // $this->show_message('Validation Error: Bad value for Category friendly URL', E_MESSAGE_ERROR); // $this->error = true; } if (!$this->error) { $inserta = array(); /* Why? Categoty Icon is not required field if (empty($_POST['category_icon'])) { $handle = opendir(e_IMAGE."icons"); while ($file = readdir($handle)) { if ($file != "." && $file != ".." && $file != "/" && $file != "null.txt" && $file != "CVS") { $iconlist[] = $file; } } closedir($handle); $inserta['category_icon'] = $iconlist[0]; } else { $inserta['category_icon'] = e107::getParser()->toDB($_POST['category_icon']); }*/ $inserta['data']['category_icon'] = $_POST['category_icon']; $inserta['_FIELD_TYPES']['category_icon'] = 'todb'; $inserta['data']['category_name'] = $_POST['category_name']; $inserta['_FIELD_TYPES']['category_name'] = 'todb'; $inserta['data']['category_meta_description'] = strip_tags($_POST['category_meta_description']); $inserta['_FIELD_TYPES']['category_meta_description'] = 'str'; $inserta['data']['category_meta_keywords'] = $_POST['category_meta_keywords']; $inserta['_FIELD_TYPES']['category_meta_keywords'] = 'str'; $inserta['data']['category_manager'] = $_POST['category_manager']; $inserta['_FIELD_TYPES']['category_manager'] = 'int'; $inserta['data']['category_order'] = $_POST['category_order']; $inserta['_FIELD_TYPES']['category_order'] = 'int'; //e107::getDb()->db_Insert('news_category', "'0', '{$_POST['category_name']}', '{$_POST['category_icon']}'"); $id = e107::getDb()->db_Insert('news_category', $inserta); if($id) { $inserta['data']['category_id'] = $id; //admin log now supports DB array and method chaining e107::getAdminLog()->log_event('NEWS_04', $inserta, E_LOG_INFORMATIVE, ''); $this->show_message(NWSLAN_35, E_MESSAGE_SUCCESS); $this->clear_cache(); //TODO - add to WIKI docs e107::getEvent()->trigger("newscatpost", array_merge($inserta['data'], $rwinserta['data'])); } else { //debug + error message if(e107::getDb()->getLastErrorNumber()) { $this->error = true; $this->show_message('mySQL Error detected!', E_MESSAGE_ERROR); eMessage::getInstance()->addS('mySQL error #'.e107::getDb()->getLastErrorNumber().': '.e107::getDb()->getLastErrorText(), E_MESSAGE_DEBUG); } } } } function _observe_update_category() { if(!getperms('0|7')) { $this->noPermissions(); } $this->setId(intval($_POST['category_id'])); if(!$this->getId()) { return; } //FIXME - lan, e_model based news administration model $this->error = false; if(empty($_POST['category_name'])) { $this->show_message('Validation Error: Missing Category name', E_MESSAGE_ERROR); $this->error = true; } if (!$this->error) { $updatea = array(); $updatea['data']['category_icon'] = $_POST['category_icon']; $updatea['_FIELD_TYPES']['category_icon'] = 'todb'; $updatea['data']['category_name'] = $_POST['category_name']; $updatea['_FIELD_TYPES']['category_name'] = 'todb'; $updatea['data']['category_meta_description'] = strip_tags($_POST['category_meta_description']); $updatea['_FIELD_TYPES']['category_meta_description'] = 'str'; $updatea['data']['category_meta_keywords'] = $_POST['category_meta_keywords']; $updatea['_FIELD_TYPES']['category_meta_keywords'] = 'str'; $updatea['data']['category_manager'] = $_POST['category_manager']; $updatea['_FIELD_TYPES']['category_manager'] = 'int'; $updatea['data']['category_order'] = $_POST['category_order']; $updatea['_FIELD_TYPES']['category_order'] = 'int'; $updatea['WHERE'] = 'category_id='.$this->getId(); $inserta = array(); $rid = 0; $upcheck = e107::getDb()->db_Update("news_category", $updatea); $rwupcheck = false; if($upcheck || !e107::getDb()->getLastErrorNumber()) { if ($upcheck || $rwupcheck) { //admin log now supports DB array and method chaining $updatea['data']['category_id'] = $this->getId(); if($upcheck) e107::getAdminLog()->log_event('NEWS_05', $updatea['data'], E_LOG_INFORMATIVE, ''); if($rwupcheck && $inserta['data']) e107::getAdminLog()->log_event('NEWS_10', $inserta['data'], E_LOG_INFORMATIVE, ''); $this->show_message(NWSLAN_36, E_MESSAGE_SUCCESS); $this->clear_cache(); //TODO - add to WIKI docs e107::getEvent()->trigger("newscatupd", array_merge($updatea['data'], $inserta['data'])); } else { $this->show_message(LAN_NO_CHANGE); } $this->setId(0); } else { $this->error = true; $this->setSubAction('edit'); $this->show_message('mySQL Error detected!', E_MESSAGE_ERROR); $this->show_message('#'.e107::getDb()->getLastErrorNumber().': '.e107::getDb()->getLastErrorText(), E_MESSAGE_DEBUG); return; } } } function _observe_multi_create_category() { if(!getperms('0|7')) { $this->noPermissions(); } $cnt = 0; foreach ($_POST['multi_category_manager'] as $cid => $val) { $order = $_POST['multi_category_order'][$cid]; $cnt += (int) e107::getDb()->db_Update('news_category', 'category_manager='.intval($val).', category_order='.intval($order).' WHERE category_id='.intval($cid)); } if($cnt) eMessage::getInstance()->add(LAN_UPDATED, E_MESSAGE_SUCCESS); } function _observe_save_prefs() { if(!getperms('0')) { $this->noPermissions(); } $temp = array(); $temp['newsposts'] = intval($_POST['newsposts']); $temp['newsposts_archive'] = intval($_POST['newsposts_archive']); $temp['newsposts_archive_title'] = e107::getParser()->toDB($_POST['newsposts_archive_title']); $temp['news_cats'] = intval($_POST['news_cats']); $temp['nbr_cols'] = intval($_POST['nbr_cols']); $temp['subnews_attach'] = intval($_POST['subnews_attach']); $temp['subnews_resize'] = intval($_POST['subnews_resize']); $temp['subnews_class'] = intval($_POST['subnews_class']); $temp['subnews_htmlarea'] = intval($_POST['subnews_htmlarea']); $temp['news_subheader'] = e107::getParser()->toDB($_POST['news_subheader']); $temp['news_newdateheader'] = intval($_POST['news_newdateheader']); $temp['news_unstemplate'] = intval($_POST['news_unstemplate']); $temp['news_editauthor'] = intval($_POST['news_editauthor']); $temp['news_sefbase'] = preg_replace('#[^\w\pL\-]#u', '', $_POST['news_sefbase']); e107::getConfig()->updatePref($temp); if(e107::getConfig()->save(false)) { e107::getAdminLog()->logArrayDiffs($temp, e107::getPref(), 'NEWS_06'); $this->clear_cache(); //$this->show_message(NWSLAN_119, E_MESSAGE_SUCCESS); } } function _observe_upload() { //$pref['upload_storagetype'] = "1"; require_once(e_HANDLER."upload_handler.php"); $uploaded = file_upload(e_NEWSIMAGE); foreach($_POST['uploadtype'] as $key=>$uploadtype) { if($uploadtype == "thumb") { rename(e_NEWSIMAGE.$uploaded[$key]['name'],e_NEWSIMAGE."thumb_".$uploaded[$key]['name']); } if($uploadtype == "file") { rename(e_NEWSIMAGE.$uploaded[$key]['name'],e_DOWNLOAD.$uploaded[$key]['name']); } if ($uploadtype == "resize" && $_POST['resize_value']) { require_once(e_HANDLER."resize_handler.php"); resize_image(e_NEWSIMAGE.$uploaded[$key]['name'], e_NEWSIMAGE.$uploaded[$key]['name'], $_POST['resize_value'], "copy"); } } } function _observe_saveColumns() { global $user_pref,$admin_log; $user_pref['admin_news_columns'] = $_POST['e-columns']; save_prefs('user'); $this->fieldpref = $user_pref['admin_news_columns']; } function show_existing_items() { global $user_pref,$gen; if(!getperms('H')) { return; } require_once(e_HANDLER."form_handler.php"); $frm = new e_form(true); //enable inner tabindex counter // Effectively toggle setting for headings $amount = 10;//TODO - pref if(!is_array($user_pref['admin_news_columns'])) { $user_pref['admin_news_columns'] = array("news_id","news_title","news_author","news_render_type"); } $field_columns = $this->fields; $e107 = e107::getInstance(); // ------ Search Filter ------ $text .= "
"; // -------------------------------------------- $query = " SELECT n.*, nc.*, u.user_name, u.user_id FROM #news AS n LEFT JOIN #news_category AS nc ON n.news_category=nc.category_id LEFT JOIN #user AS u ON n.news_author=u.user_id "; $check_perms = !getperms('0') ? " nc.category_manager IN (".USERCLASS_LIST.") " : ''; if (vartrue($_POST['searchquery'])) { $query .= "WHERE {$check_perms}n.news_title REGEXP('".$_POST['searchquery']."') OR n.news_body REGEXP('".$_POST['searchquery']."') OR n.news_extended REGEXP('".$_POST['searchquery']."') ORDER BY n.news_datestamp DESC"; } else { $ordfield = 'n.news_datestamp'; if($this->getSubAction() == 'user_name') { $ordfield = "u.user_name"; } elseif(strpos($this->getSubAction(), 'category_')) { $ordfield = 'nc.'.$this->getSubAction(); } elseif($this->getSubAction()) { $ordfield = 'n.'.$this->getSubAction(); } $query .= ($check_perms ? "WHERE {$check_perms}" : '')."ORDER BY {$ordfield} ".strtoupper($this->_sort_order)." LIMIT ".$this->getFrom().", {$amount}"; } if ($e107->sql->db_Select_gen($query)) { $newsarray = $e107->sql->db_getList(); $text .= " "; } else { $tmp = NWSLAN_43; if(vartrue($_POST['searchquery'])) { $tmp = sprintf(NWSLAN_121, '"'.$_POST['searchquery'])."" « ".LAN_BACK.""; } $text = "