Ejemplo n.º 1
0
 /**
  * Add new article
  *
  * @param	array	$in_article
  * @return	boolean	true on success or false on fail
  */
 function add_article($in_article)
 {
     if (!$this->check_token()) {
         return false;
     }
     if (!vivvo_hooks_manager::call('article_add', array(&$in_article))) {
         return vivvo_hooks_manager::get_status();
     }
     $sm = vivvo_lite_site::get_instance();
     if ($sm->user) {
         $editor_restriction = $sm->user->get_privilege_object_ids('EDITOR', 'Categories');
         $writer_restriction = $sm->user->get_privilege_object_ids('WRITE', 'Categories');
         if (!empty($writer_restriction)) {
             $in_article['title'] = trim($in_article['title']);
             $article_list = new Articles_list();
             $new_order_num = $article_list->get_max('order_num');
             if ($new_order_num) {
                 $in_article['order_num'] = $new_order_num + 1;
             } else {
                 $in_article['order_num'] = 1;
             }
             if (!strlen(trim($in_article['sefriendly']))) {
                 $in_article['sefriendly'] = make_sefriendly($in_article['title']);
                 $source = 'title';
             } else {
                 $in_article['sefriendly'] = make_sefriendly($in_article['sefriendly']);
                 $source = 'sefriendly';
             }
             if (!empty($in_article['sefriendly'])) {
                 $sql = 'SELECT id, sefriendly, status FROM ' . VIVVO_DB_PREFIX . 'articles WHERE `sefriendly` = \'' . secure_sql($in_article['sefriendly']) . '\' GROUP BY `status`; ';
                 $res = $sm->get_db()->query($sql);
                 $arrData = array();
                 if (!PEAR::isError($res)) {
                     $arrData = $res->fetchAll(MDB2_FETCHMODE_ASSOC);
                     $res->free();
                 }
                 // First check trashed articles, if sefriendly occupied, return error
                 foreach ($arrData as $key => $value) {
                     if ($value['status'] == -2) {
                         $this->set_error_code(2037);
                         return false;
                     }
                 }
                 // If sefriendly found among Active articles
                 foreach ($arrData as $key => $value) {
                     if (is_array($value) && count($value) > 0 && $value['status'] > -2) {
                         if ($source == 'sefriendly') {
                             $this->set_error_code(2001);
                             return false;
                         } else {
                             $in_article['sefriendly'] = '';
                         }
                     }
                 }
                 unset($arrData);
             }
             if (!$sm->user->can_change_status($in_article['status'])) {
                 $in_article['status'] = 0;
             }
             if ($sm->user->is('EDITOR', 'Categories', $in_article['category_id'])) {
                 if (!isset($in_article['user_id']) or $in_article['user_id'] == '') {
                     $in_article['user_id'] = $sm->user->get_id();
                 }
             } else {
                 $in_article['user_id'] = $sm->user->get_id();
                 $in_article['author'] = $sm->user->get_name();
             }
             $in_article['user_domain'] = VIVVO_USER_SOURCE;
             if (!$sm->user->is_admin()) {
                 if (!in_array('0', $writer_restriction) && !in_array($in_article['category_id'], $writer_restriction)) {
                     $this->set_error_code(2036);
                     return false;
                 }
             }
             $body = $in_article['body'];
             array_walk($in_article, 'array_htmlspecialchars');
             $in_article['body'] = $body;
             $in_article['created'] = date('Y-m-d H:i:00', $in_article['created']);
             $article = new Articles($sm);
             if ($article->populate($in_article, true) === false) {
                 $this->set_error_info($article->get_error_info());
                 return false;
             }
             if ($article->author == '') {
                 $article->author = $article->get_author_name();
             }
             $this->_post_master->set_data_object($article);
             if ($this->_post_master->sql_insert()) {
                 $id = $this->_post_master->get_work_id();
                 $article->set_id($id);
                 if (!vivvo_hooks_manager::call('article_add_postinsert', array(&$article))) {
                     return vivvo_hooks_manager::get_status();
                 }
                 $um = $sm->get_url_manager();
                 $um->set_param('ARTICLE_id', $id);
                 $fm = $sm->get_file_manager();
                 if ($fm->is_uploaded('ARTICLE_abstact_image')) {
                     $abstract_image = $fm->upload('ARTICLE_abstact_image', '', 'jpg,jpeg,gif,png');
                     if ($abstract_image != false) {
                         $article->set_image($abstract_image);
                         $this->_post_master->set_data_object($article);
                         $this->_post_master->sql_update();
                     }
                 }
                 if ($fm->is_uploaded('ARTICLE_attachment')) {
                     $attachment = $fm->upload('ARTICLE_attachment');
                     if ($attachment != false) {
                         $article->set_document($attachment);
                         $this->_post_master->set_data_object($article);
                         $this->_post_master->sql_update();
                     }
                 }
                 if (defined('VIVVO_CUSTOM_FIELD_UPLOAD') && VIVVO_CUSTOM_FIELD_UPLOAD == 1) {
                     $files = $fm->get_upload_keys();
                     if ($files) {
                         $dbm = $sm->get_db_manager();
                         $fields = $dbm->get_table_fields(VIVVO_DB_PREFIX . 'articles');
                         if (!empty($fields)) {
                             foreach ($files as $file) {
                                 $file_field = substr($file, 8);
                                 if (preg_match('/^ARTICLE_file_/', $file) && in_array($file_field, $fields)) {
                                     $uploaded = $fm->upload($file);
                                     if ($uploaded != false) {
                                         $article->__set($file_field, $uploaded);
                                         $this->_post_master->set_data_object($article);
                                         $this->_post_master->sql_update();
                                     }
                                 }
                             }
                         }
                     }
                 }
                 isset($in_article['tags']) or $in_article['tags'] = '';
                 $this->manageTags(explode(',', $in_article['tags']), $id);
                 admin_log($sm->user->get_username(), 'Created article #' . $this->_post_master->get_work_id());
                 return true;
             } else {
                 $this->set_error_code(2002);
                 return false;
             }
         } else {
             $this->set_error_code(2003);
             return false;
         }
     } else {
         $this->set_error_code(2004);
         return false;
     }
 }