/halogy/application/libraries/Core.php
PHP | 1376 lines | 1028 code | 190 blank | 158 comment | 153 complexity | 45b6911b2581e9f032b644d48a3bdce3 MD5 | raw file
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * Halogy
- *
- * A user friendly, modular content management system for PHP 5.0
- * Built on CodeIgniter - http://codeigniter.com
- *
- * @package Halogy
- * @author Haloweb Ltd.
- * @copyright Copyright (c) 2008-2011, Haloweb Ltd.
- * @license http://halogy.com/license
- * @link http://halogy.com/
- * @since Version 1.0
- * @filesource
- */
- // ------------------------------------------------------------------------
- class Core {
-
- var $CI; // CI instance
- var $table ; // default table
- var $siteID; // id of the site
- var $uri_assoc_segment = 4; // segment where the magic happens
- var $adminOverRide = FALSE; // allows for override of siteID
- var $currentPage;
- var $where = array();
- var $set = array();
- var $required = array();
-
- function Core()
- {
- // init vars
- $this->CI =& get_instance();
-
- // get siteID, if available
- if (defined('SITEID'))
- {
- $this->siteID = SITEID;
- }
-
- // set groupID from session (if set)
- $this->groupID = ($this->CI->session->userdata('groupID')) ? $this->CI->session->userdata('groupID') : 0;
- }
- function get_page($pageID = FALSE, $uri = FALSE)
- {
- // get page data
- $this->CI->db->where('siteID', $this->siteID);
- // if getting by uri check its not deleted
- if ($uri)
- {
- $this->CI->db->where('deleted', 0);
- }
-
- if (intval($pageID))
- {
- $this->CI->db->where('pageID', $pageID);
- }
- elseif ($uri !== FALSE && $pageID === FALSE)
- {
- $this->CI->db->where('uri', $uri);
- }
- else
- {
- return FALSE;
- }
- // get the latest one, not a deleted one from the past
- $this->CI->db->order_by('dateCreated', 'desc');
-
- $query = $this->CI->db->get('pages', 1);
-
- if ($query->num_rows() == 1)
- {
- return $query->row_array();
- }
- else
- {
- return FALSE;
- }
- }
- function get_active_page($uri = '')
- {
- // get published page
- if (!$uri)
- {
- return FALSE;
- }
-
- $this->CI->db->where('siteID', $this->siteID);
-
- $this->CI->db->where('active', 1);
- $this->CI->db->where('deleted', 0);
- $this->CI->db->where('uri', $uri);
-
- $query = $this->CI->db->get('pages', 1);
-
- if ($query->num_rows() == 1)
- {
- return $query->row_array();
- }
- else
- {
- return FALSE;
- }
- }
- function generate_page($pageID, $admin = FALSE, $templateID = '')
- {
- // get page data
- $pagedata = $this->get_page($pageID);
- // load template, either from override or from page data
- if ($templateID)
- {
- $page = $this->CI->template->generate_template(array('templateID' => $templateID));
- }
- else
- {
- $page = $this->CI->template->generate_template($pagedata);
- }
-
- // set default parse variable to nothing
- $page['error'] = '';
- $page['message'] = '';
- // tell the parser some important info like versionID
- $page['pageID'] = $pagedata['pageID'];
- $page['templateID'] = $pagedata['templateID'];
- $page['versionID'] = $pagedata['versionID'];
- // if logged in as admin, then get the blocks from draft, otherwise get them from the published version
- $versionID = ($admin === TRUE) ? $pagedata['draftID'] : $pagedata['versionID'];
- // populate blocks from db (if they exist)
- if ($blocksResult = $this->get_blocks($versionID))
- {
- foreach($blocksResult as $blockRow)
- {
- // set bodies and get images for mkdn view
- $body[$blockRow['blockRef']] = form_prep($blockRow['body']);
- $mkdnBody[$blockRow['blockRef']] = $this->CI->template->parse_body($blockRow['body']);
- }
- }
- // parse for blocks
- preg_match_all('/block([a-z0-9\-_]+)/i', $page['body'], $blocks);
- if ($blocks)
- {
- foreach($blocks[1] as $block => $value)
- {
- $blockRef = 'block'.$value;
- if ($admin)
- {
- $page[$blockRef] = '
- <div class="halogycms_container">
- <div id="'.$blockRef.'" class="halogycms_edit">
- <div class="halogycms_buttons">
- <a href="#" class="halogycms_boldbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_bold.png" alt="Bold" title="Bold" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_italicbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_italic.png" alt="Italic" title="Italic" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_h1button"><img src="'.$this->CI->config->item('staticPath').'/images/btn_h1.png" alt="Heading 1" title="Heading 1" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_h2button"><img src="'.$this->CI->config->item('staticPath').'/images/btn_h2.png" alt="Heading 2" title="Heading 2" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_h3button"><img src="'.$this->CI->config->item('staticPath').'/images/btn_h3.png" alt="Heading 3" title="Heading 3" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_urlbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_url.png" alt="Insert Link" title="Insert Link" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_imagebutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_image.png" alt="Insert Image" title="Insert Image" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_filebutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_file.png" alt="Insert File" title="Insert File" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_cancelbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_cancel.png" alt="Cancel" title="Cancel Changes" class="halogycms_helper" /></a>
- <a href="/admin/pages/add_block/'.$versionID.'/'.$blockRef.'" class="halogycms_savebutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_save.png" alt="Save" title="Save Changes" class="halogycms_helper" /></a>
- <a href="#" class="halogycms_editbutton">Edit</a>
- </div>
- <div class="halogycms_blockelement">'.@$mkdnBody[$blockRef].'</div>
- <div class="halogycms_editblock"><textarea rows="8" cols="10" class="code">'.@$body[$blockRef].'</textarea></div>
- </div>
- </div>
- ';
- }
- else
- {
- $page[$blockRef] = @$mkdnBody[$blockRef];
- }
- }
- }
-
- return $page;
- }
- function get_versions($pageID)
- {
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->where('published', 1);
- $this->CI->db->order_by('dateCreated', 'desc');
-
- $query = $this->CI->db->get('page_versions', 30);
-
- // get data
- if ($query->num_rows())
- {
- return $query->result_array();
- }
- else
- {
- return false;
- }
- }
- function get_drafts($pageID)
- {
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->where('published', 0);
- $this->CI->db->order_by('dateCreated', 'desc');
-
- $query = $this->CI->db->get('page_versions');
-
- // get data
- if ($query->num_rows())
- {
- return $query->result_array();
- }
- else
- {
- return false;
- }
- }
- function get_blocks($versionID)
- {
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->select('MAX(blockID) as blockID');
- $this->CI->db->where('versionID', $versionID);
- $this->CI->db->group_by('blockRef');
- $this->CI->db->order_by('dateCreated','DESC');
- $query = $this->CI->db->get('page_blocks');
- $result = $query->result_array();
- $numBlocks = $query->num_rows();
-
- // get data
- if ($numBlocks > 0)
- {
- foreach($result as $row)
- {
- $blockIDs[] = $row['blockID'];
- }
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->where_in('blockID', $blockIDs);
- $this->CI->db->where('versionID', $versionID);
- $this->CI->db->order_by('blockID');
- $query = $this->CI->db->get('page_blocks', $numBlocks);
-
- return $query->result_array();
- }
- else
- {
- return false;
- }
- }
- function get_template($templateID = '')
- {
- // default where
- $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
- $this->CI->db->where('t1.deleted', 0, FALSE);
- $this->CI->db->where('templateID', $templateID);
- // select
- $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
- // join revisions
- $this->CI->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
-
- // get em
- $query = $this->CI->db->get('templates t1', 1);
-
- if ($query->num_rows())
- {
- return $query->row_array();
- }
- else
- {
- return FALSE;
- }
- }
- function get_module_template($modulePath = '')
- {
- // default where
- $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
- $this->CI->db->where('t1.deleted', 0, FALSE);
- $this->CI->db->where('modulePath', $modulePath);
- // select
- $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
- // join revisions
- $this->CI->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
-
- // get em
- $query = $this->CI->db->get('templates t1', 1);
-
- if ($query->num_rows())
- {
- return $query->row_array();
- }
- else
- {
- return FALSE;
- }
- }
- function get_include($includeRef = '', $includeID = '')
- {
- // default where
- $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
- $this->CI->db->where('t1.deleted', 0, FALSE);
- // get by reference
- if ($includeRef)
- {
- $this->CI->db->where('includeRef', $includeRef);
- }
- // get by ID
- elseif ($includeID)
- {
- $this->CI->db->where('includeID', $includeID);
- }
- // or fail
- else
- {
- return FALSE;
- }
- // select
- $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
- // join revisions
- $this->CI->db->join('include_versions t2', 't2.versionID = t1 . versionID', 'left');
-
- // get em
- $query = $this->CI->db->get('includes t1', 1);
-
- if ($query->num_rows())
- {
- return $query->row_array();
- }
- else
- {
- return FALSE;
- }
- }
- function lookup_user($userID, $display = FALSE)
- {
- // default wheres
- $this->CI->db->where('userID', $userID);
- // grab
- $query = $this->CI->db->get('users', 1);
- if ($query->num_rows())
- {
- $row = $query->row_array();
-
- if ($display !== FALSE)
- {
- return ($row['displayName']) ? $row['displayName'] : $row['firstName'].' '.$row['lastName'];
- }
- else
- {
- return $row;
- }
- }
- else
- {
- return FALSE;
- }
- }
- function add_draft($pageID)
- {
- if ($pagedata = $this->get_page($pageID))
- {
- // add new version
- $this->CI->db->set('pageID', $pageID);
- $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
- $this->CI->db->set('userID', $this->CI->session->userdata('userID'));
- $this->CI->db->set('siteID', $this->siteID);
- $this->CI->db->insert('page_versions');
-
- // get version ID
- $draftID = $this->CI->db->insert_id();
- // update page draft
- $this->CI->db->set('draftID', $draftID);
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->update('pages');
- // add the old blocks in to this new version
- if ($blocks = $this->get_blocks($pagedata['draftID']))
- {
- foreach($blocks as $block)
- {
- $body = $block['body'];
- $this->add_block($body, $draftID, $block['blockRef']);
- }
- }
-
- return $draftID;
- }
- else
- {
- return FALSE;
- }
- }
- function get_latest_block($versionID)
- {
- $this->CI->db->where('versionID', $versionID);
- $this->CI->db->where('siteID', $this->siteID);
-
- $this->CI->db->order_by('dateCreated', 'desc');
- // grab
- $query = $this->CI->db->get('page_blocks', 1);
- if ($query->num_rows())
- {
- $row = $query->row_array();
- return $row;
- }
- else
- {
- return FALSE;
- }
- }
-
- function publish_page($pageID, $draftID)
- {
- $this->CI->db->set('dateModified', date("Y-m-d H:i:s"));
- $this->CI->db->set('datePublished', date("Y-m-d H:i:s"));
- $this->CI->db->set('versionID', $draftID);
- $this->CI->db->set('active', 1);
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->update('pages');
- return TRUE;
- }
- function publish_draft($draftID)
- {
- // publish version
- $this->CI->db->set('published', 1);
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->where('versionID', $draftID);
- $this->CI->db->update('page_versions');
- return TRUE;
- }
- function revert_version($pageID, $versionID)
- {
- // update the template with version
- $this->CI->db->set('versionID', $versionID);
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->update('pages');
- return TRUE;
- }
- function revert_draft($pageID, $draftID)
- {
- // update the template with version
- $this->CI->db->set('draftID', $draftID);
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->update('pages');
- return TRUE;
- }
- function add_block($body, $versionID, $blockRef = 'block')
- {
- // delete blocks for this version
- $this->CI->db->where('page_blocks.siteID', $this->siteID);
- $this->CI->db->where('page_blocks.versionID', $versionID);
- $this->CI->db->where('page_blocks.blockRef', $blockRef);
- $this->CI->db->delete('page_blocks');
- // add block
- $this->CI->db->query("SET NAMES 'utf8'");
- $this->CI->db->set('versionID', $versionID);
- $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
- $this->CI->db->set('siteID', $this->siteID);
- $this->CI->db->set('blockRef', $blockRef);
- $this->CI->db->set('body', $body);
- $this->CI->db->insert('page_blocks');
- return TRUE;
- }
- function add_view($pageID)
- {
- $this->CI->db->set('views', 'views+1', false);
- $this->CI->db->where('pageID', $pageID);
- $this->CI->db->where('siteID', $this->siteID);
- $this->CI->db->update('pages');
- }
- function get_web_form_by_ref($formRef)
- {
- $this->CI->db->where('formRef', $formRef);
-
- $this->CI->db->where('deleted', 0);
- $this->CI->db->where('siteID', $this->siteID);
-
- $query = $this->CI->db->get('web_forms', 1);
- if ($query->num_rows())
- {
- return $query->row_array();
- }
- else
- {
- return FALSE;
- }
- }
-
- function web_form()
- {
- // get web form
- if (!$webform = $this->CI->core->get_web_form_by_ref($this->CI->core->decode($this->CI->input->post('formID'))))
- {
- return FALSE;
- }
-
- // set main required field
- $this->CI->form_validation->set_rules('email', 'Email', 'required|valid_email');
-
- // find out if a user account needs to be created
- $account = ($webform['account']) ? TRUE : FALSE;
- // get required fields
- $required = $this->CI->input->post('required', TRUE);
- // get optional required fields
- if ($required)
- {
- $requiredArray = explode('|', $required);
- foreach($requiredArray as $field)
- {
- $this->CI->form_validation->set_rules($field, ucfirst($field), 'required');
- }
- }
- // get first and last name
- if ($this->CI->input->post('firstName', TRUE))
- {
- $firstName = $this->CI->input->post('firstName', TRUE);
- $lastName = $this->CI->input->post('lastName', TRUE);
- }
- elseif ($fullName = $this->CI->input->post('fullName', TRUE))
- {
- $fullNameArray = @explode(' ', $fullName);
- $lastName = (sizeof($fullNameArray) > 0) ? ucfirst(trim(end($fullNameArray))) : '';
- $firstName = (sizeof($fullNameArray) > 0) ? ucfirst(trim($fullNameArray[0])) : $fullName;
- }
- else
- {
- $firstName = '';
- $lastName = '';
- }
- // at least set the name and email in to a session
- if (!$this->CI->session->userdata('session_user'))
- {
- $this->CI->session->set_userdata('email', $this->CI->input->post('email', TRUE));
- $this->CI->session->set_userdata('firstName', $firstName);
- $this->CI->session->set_userdata('lastName', $lastName);
- }
-
- // if capturing check user is unique and a password matches
- if ($account)
- {
- // email and message are always required
- $this->CI->form_validation->set_rules('email', 'Email', 'required|valid_email|unique[users.email]|trim');
-
- // check if password was submitted, make it required if so
- if (array_key_exists('password', $_POST))
- {
- // require password confirm?
- if (isset($_POST['confirmPassword']))
- {
- $this->form_validation->set_rules('password', 'Password', 'required|matches[confirmPassword]');
- }
- else
- {
- $this->form_validation->set_rules('password', 'Password', 'required');
- }
- }
- }
-
- // look for files
- $files = FALSE;
- if ($webform['fileTypes'] && count($_FILES))
- {
- foreach($_FILES as $name => $file)
- {
- $this->CI->uploads->maxSize = '2000';
- $this->CI->uploads->allowedTypes = $webform['fileTypes'];
-
- // check a file has actually been uploaded
- if ($file['name'] != '')
- {
- if ($fileData = $this->CI->uploads->upload_file($name))
- {
- $files[$name] = $fileData;
- }
- else
- {
- $this->CI->form_validation->set_error($this->CI->uploads->errors);
- }
- }
- }
- }
-
- // captcha
- if (isset($_POST['captcha']) && !$this->_captcha_check())
- {
- $this->CI->form_validation->set_error('Sorry you didn\'t pass the spam check. Please make sure Javascript is enabled.');
- }
-
- // add ticket
- if ($this->CI->form_validation->run())
- {
- if ($account)
- {
- // create user
- $this->create_user();
-
- // set admin session name, if given
- if (!$this->CI->site->config['activation'])
- {
- $this->CI->load->library('auth');
- $username = array('field' => 'email', 'label' => 'Email address', 'value' => $this->CI->input->post('email'));
- $password = ($this->CI->input->post('password')) ? $this->CI->input->post('password', TRUE) : substr(md5(time()),0,6);
- // login or get error message
- if (!$this->CI->auth->login($username, $password, 'session_user', FALSE))
- {
- $this->CI->form_validation->set_error($this->CI->auth->error);
- }
- }
- }
- // add ticket
- $this->add_ticket($webform, $files);
- // redirect if set
- if ($redirect = $webform['outcomeRedirect'])
- {
- redirect($redirect);
- }
- // get message if set
- if ($message = $webform['outcomeMessage'])
- {
- return $message;
- }
- else
- {
- return 'Thank you, your message was sent successfully.';
- }
- }
- else
- {
- return FALSE;
- }
- }
- function add_ticket($webform, $files = '')
- {
- // get web form
- if (!$webform)
- {
- return FALSE;
- }
-
- if ($this->CI->input->post('email'))
- {
- // set system fields
- $fields = array('required', 'formID', 'fieldSet', 'fileTypes', 'account', 'formName', 'outcomeEmails', 'outcomeRedirect', 'outcomeMessage', 'fullName', 'email', 'subject', 'message', 'toEmail', 'captcha', 'firstName', 'lastName', 'password', 'confirmPassword', 'groupID');
-
- // set default message
- $message = '';
- $filepaths = '';
- // get extra posted info and prepend to message
- if (count($_POST))
- {
- foreach($_POST as $post => $value)
- {
- if (!in_array($post, $fields) && !preg_match('/^submit$|^submit\_x$|^submit\_y|^x|^y/i', $post))
- {
- $postValue = $this->CI->input->post($post, TRUE);
- $message .= "\t".ucfirst($post) . ": ".$value."\n\n";
- }
- }
- }
-
- // get files and prepend to message
- if ($files)
- {
- $message .= "\tFiles: ".count($files).((count($files) != 1) ? ' files' : ' file')." uploaded\n\n";
- $filepaths .= '<br />';
- foreach($files as $name => $fileData)
- {
- $filepaths .= '<br /><a href="'.site_url($this->CI->uploads->uploadsPath.'/'.$fileData['file_name']).'">'.$fileData['client_name'].'</a>';
- }
- }
-
- // get posted message
- $message .= (strlen($message) > 1) ? "\n" : '';
- $message .= $this->CI->input->post('message', TRUE);
- // set defaults
- $fullName = ($this->CI->input->post('fullName')) ? $this->CI->input->post('fullName', TRUE) : 'N/A';
- $subject = ($this->CI->input->post('subject')) ? $this->CI->input->post('subject', TRUE) : (($webform['formName']) ? $webform['formName'] : 'No Subject');
-
- // set outcome emails
- if ($this->CI->input->post('outcomeEmails'))
- {
- $outcomeEmails = explode(',', $this->CI->input->post('outcomeEmails'));
- }
- else
- {
- $outcomeEmails = ($webform['outcomeEmails']) ? explode(',', $webform['outcomeEmails']) : $this->CI->site->config['siteEmail'];
- }
-
- // get first name and last name
- $names = explode(' ', $fullName);
- $firstName = (sizeof($names) > 1 && $names[0]) ? ucfirst(trim($names[0])) : $name;
- $lastName = (sizeof($names) > 1) ? ucfirst(end($names)) : '';
-
- // add ticket
- $this->CI->db->set('siteID', $this->siteID);
- $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
- ($webform['formName']) ? $this->CI->db->set('formName', $webform['formName']) : '';
- $this->CI->db->set('fullName', $fullName);
- $this->CI->db->set('email', $this->CI->input->post('email', TRUE));
- $this->CI->db->set('subject', $subject);
- $this->CI->db->set('body', $message.$filepaths);
- $this->CI->db->insert('tickets');
- $ticketID = $this->CI->db->insert_id();
- // set header and footer
- $emailHeader = str_replace('{name}', $fullName, $this->CI->site->config['emailHeader']);
- $emailHeader = str_replace('{first-name}', $firstName, $emailHeader);
- $emailHeader = str_replace('{last-name}', $lastName, $emailHeader);
- $emailHeader = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailHeader);
- $emailFooter = str_replace('{name}', $fullName, $this->CI->site->config['emailFooter']);
- $emailFooter = str_replace('{first-name}', $firstName, $emailFooter);
- $emailFooter = str_replace('{last-name}', $lastName, $emailFooter);
- $emailFooter = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailFooter);
- $emailTicket = str_replace('{name}', $fullName, $this->CI->site->config['emailTicket']);
- $emailTicket = str_replace('{first-name}', $firstName, $emailTicket);
- $emailTicket = str_replace('{last-name}', $lastName, $emailTicket);
- $emailTicket = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailTicket);
-
- // send despatch email to customer
- $body = $emailHeader."\n\n";
- $body .= $emailTicket."\n\n";
- $body .= "\tTicket ID: ".$ticketID."\n";
- $body .= "\tSubject: ".$subject."\n";
- $body .= "\tName: ".$fullName."\n";
- $body .= "\tEmail: ".$this->CI->input->post('email')."\n\n";
- // attach message
- if ($message)
- {
- $body .= "Message:\n";
- $body .= "---------------------------------------------\n\n";
- $body .= $message."\n\n";
- $body .= "---------------------------------------------\n\n";
- }
- // send username and password
- if ($webform['account'])
- {
- $body .= "Your login details are below:\n";
- $body .= "---------------------------------------------\n\n";
- $body .= "Your email: \t".$this->CI->input->post('email')."\n";
- $body .= "Your password: \t".(($this->CI->input->post('password', TRUE)) ? $this->CI->input->post('password', TRUE) : substr(md5(time()),0,6))."\n\n";
- $body .= "---------------------------------------------\n\n";
- }
-
- $footerBody = $emailFooter;
-
- // load email lib and email user and admin
- $this->CI->load->library('email');
- // attach files
- if ($files)
- {
- foreach ($files as $file)
- {
- $this->CI->email->attach($file['full_path']);
- }
- }
- // send to recipient
- $this->CI->email->to($this->CI->input->post('email', TRUE));
- $this->CI->email->from($this->CI->site->config['siteEmail'], $this->CI->site->config['siteName']);
- $this->CI->email->subject('[#'.$ticketID.']: ' . $subject);
- $this->CI->email->message($body.$footerBody);
- $this->CI->email->send();
- $this->CI->email->clear();
- // send to CC or admin
- $this->CI->email->to($outcomeEmails);
- $this->CI->email->from($this->CI->input->post('email', TRUE));
- $this->CI->email->subject('FW: [#'.$ticketID.']: ' . $this->CI->input->post('subject', TRUE));
- $this->CI->email->message("A web form was submitted on ".$this->CI->site->config['siteName'].".\n\n---------------------------------------------\n\n".$body.$footerBody);
- $this->CI->email->send();
- return $ticketID;
- }
- else
- {
- return FALSE;
- }
- }
- function create_user()
- {
- // get values
- $this->CI->core->get_values('users');
- // security check
- if ($this->CI->input->post('username')) $this->CI->core->set['username'] = '';
- if ($this->CI->input->post('subscribed')) $this->CI->core->set['subscribed'] = '';
- if ($this->CI->input->post('plan')) $this->CI->core->set['plan'] = '';
- if ($this->CI->input->post('siteID')) $this->CI->core->set['siteID'] = $this->siteID;
- if ($this->CI->input->post('userID')) $this->CI->core->set['userID'] = '';
- if ($this->CI->input->post('kudos')) $this->CI->core->set['kudos'] = '';
- if ($this->CI->input->post('posts')) $this->CI->core->set['posts'] = '';
- // set folder (making sure it's not an admin folder)
- $permissionGroupsArray = $this->CI->permission->get_groups('admin');
- foreach((array)$permissionGroupsArray as $group)
- {
- $permissionGroups[$group['groupID']] = $group['groupName'];
- }
- if ($this->CI->input->post('groupID') > 0 && !@in_array($this->CI->input->post('groupID'), $permissionGroups))
- {
- $this->CI->core->set['groupID'] = $this->CI->input->post('groupID');
- }
- // set date
- $this->CI->core->set['dateCreated'] = date("Y-m-d H:i:s");
- // init null name
- $firstName = '';
- $lastName = '';
-
- // set name if only fullName is posted
- if ($this->CI->input->post('fullName') && (!$this->CI->input->post('firstName') && !$this->CI->input->post('lastName')))
- {
- $fullName = $this->CI->input->post('fullName', TRUE);
- $fullNameArray = @explode(' ', $fullName);
- $lastName = (sizeof($fullNameArray) > 0) ? ucfirst(trim(end($fullNameArray))) : '';
- $firstName = (sizeof($fullNameArray) > 0) ? ucfirst(trim($fullNameArray[0])) : $fullName;
-
- $this->CI->core->set['firstName'] = $firstName;
- $this->CI->core->set['lastName'] = $lastName;
- }
-
- // set first name
- if ($this->CI->input->post('firstName'))
- {
- $firstName = ucfirst($this->CI->input->post('firstName', TRUE));
- $this->CI->core->set['firstName'] = $firstName;
- }
-
- // set last name
- if ($this->CI->input->post('lastName'))
- {
- $lastName = ucfirst($this->CI->input->post('lastName', TRUE));
- $this->CI->core->set['lastName'] = $lastName;
- }
- // generate password
- if (!$this->CI->input->post('password'))
- {
- $password = md5(substr(md5(time()),0,6));
- $this->CI->core->set['password'] = $password;
- }
- // set manual activation
- if ($this->CI->site->config['activation'])
- {
- $this->CI->core->set['active'] = 0;
- }
- // set email on flash data
- $flashEmail = $this->CI->session->flashdata('email');
- // update table
- if ($this->CI->input->post('email') && ($this->CI->input->post('password') || $password))
- {
- if ($this->CI->core->update('users'))
- {
- $result = array(
- 'userID' => $this->CI->db->insert_id(),
- 'email' => $this->CI->input->post('email', TRUE),
- 'password' => ($this->CI->input->post('password')) ? $this->CI->input->post('password', TRUE) : $password,
- 'firstName' => $firstName,
- 'lastName' => $lastName
- );
-
- return $result;
- }
- else
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- }
- function _captcha_check()
- {
- // if captcha is posted, check its not a bot (requires js)
- if ($this->CI->input->post('captcha') == 'notabot')
- {
- return TRUE;
- }
- elseif ($this->CI->input->post('captcha') != 'notabot')
- {
- $this->CI->form_validation->set_message('captcha_check', 'You didn\'t pass the spam check, please contact us to post a comment.');
- return FALSE;
- }
- }
-
-
- /* utilities */
-
-
- // gets posted values
- function get_post()
- {
- if (count($_POST))
- {
- $post = array();
- foreach($_POST as $key => $value)
- {
- $post[$key] = $this->CI->input->post($key);
- }
-
- return $post;
- }
- else
- {
- return FALSE;
- }
- }
- // gets values from post and/or the row
- function get_values($data = '', $id = '')
- {
- // init array
- $values = array();
- // populate by row if set
- if (@is_array($data))
- {
- $row = $data;
- $values = $data;
- }
- // get data from database
- else
- {
- $table = $data;
-
- if ($id)
- {
- $query = $this->CI->db->get_where($table, $id);
-
- if ($query->num_rows())
- {
- $row = $query->row_array();
- $values = $row;
- }
- }
- }
- // get post if there is any
- if ($post = $this->get_post())
- {
- // check posted data is in fields
- foreach ($post as $field => $value)
- {
- // make sure the value is just a normal value and not an array
- if (!is_array($value))
- {
- if (isset($row) && isset($row[$field]) && $value == $row[$field])
- {
- unset($this->required[$field]);
- }
- else
- {
- // prep password
- if ($field == 'password')
- {
- if ($value != '')
- {
- $values[$field] = md5($value);
- }
- }
-
- // overwrite value with posted value
- else
- {
- $values[$field] = $value;
- }
- }
-
- if (array_key_exists($field, $this->set))
- {
- unset($values[$field]);
- }
- }
- }
- }
- return $values;
- }
- // is ajax?
- function is_ajax()
- {
- return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
- }
- // check for errors
- function check_errors()
- {
- // set rules for validation
- if (isset($this->required))
- {
- $config = array();
-
- foreach ($this->required as $field => $name)
- {
- if (is_array($name))
- {
- $config[$field] = array('field' => $field, 'label' => $name['label'], 'rules' => $name['rules']);
- }
- else
- {
- if ($field == 'email')
- {
- $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required|valid_email');
- }
- elseif ($field == 'password')
- {
- $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required|matches[confirmPassword]');
- }
- else
- {
- $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required');
- }
- }
- }
- // set rules and fields for validation
- $this->CI->form_validation->set_rules($config);
- if (!$this->CI->form_validation->run() && isset($this->required) && count($this->required))
- {
- return FALSE;
- }
- else
- {
- return TRUE;
- }
- }
- else
- {
- return TRUE;
- }
- }
-
- // get all rows from a table
- function viewall($table, $where = '', $order = '', $limit = '')
- {
- // get table fields
- $fields = $this->CI->db->list_fields($table);
-
- // set limit from uri if set
- $limit = (!$limit) ? $this->CI->site->config['paging'] : $limit;
- // get uri array for ordering
- $uriArray = $this->CI->uri->uri_to_assoc($this->uri_assoc_segment);
- // set order on order array
- if (count($uriArray))
- {
- foreach($uriArray as $key => $value)
- {
- if ($key)
- {
- if ($key == 'orderasc')
- {
- $this->CI->db->order_by($value,'asc');
- }
- elseif ($key == 'orderdesc')
- {
- $this->CI->db->order_by($value,'desc');
- }
- }
- }
- }
-
- // order override
- elseif ($order && !is_array($order))
- {
- $this->CI->db->order_by($order, 'asc');
- }
- elseif ($order && is_array($order))
- {
- $this->CI->db->order_by($order[0], $order[1]);
- }
- if (!(isset($uriArray['orderasc']) || isset($uriArray['orderdesc'])) && in_array('dateCreated', $fields))
- {
- $this->CI->db->order_by('dateCreated', 'desc');
- }
-
- // wheres
- if ($where)
- {
- $this->CI->db->where($where);
- }
- if (!$this->adminOverRide && $this->siteID)
- {
- $this->CI->db->where('siteID', $this->siteID);
- }
- if (in_array('deleted', $fields))
- {
- $this->CI->db->where('deleted', 0);
- }
- // get and return results
- $query = $this->CI->db->get($table, $limit, $this->CI->pagination->offset);
- $output[$table] = $query->result_array();
- // do same thing again but get count
- if ($where)
- {
- $this->CI->db->where($where);
- }
- if (!$this->adminOverRide && $this->siteID)
- {
- $this->CI->db->where('siteID', $this->siteID);
- }
- if (in_array('deleted', $fields))
- {
- $this->CI->db->where('deleted', 0);
- }
- $query_total = $this->CI->db->get($table);
- $totalRows = $query_total->num_rows();
- // set pagination config
- $this->set_paging($totalRows, $limit);
- return $output;
- }
- // update table
- function update($table, $id = '')
- {
- if (count($_POST) || count($_FILES))
- {
- // get fields of this table
- $fields = $this->CI->db->list_fields($table);
- // get data from database
- if ($id)
- {
- $query = $this->CI->db->get_where($table, $id);
-
- if ($query->num_rows())
- {
- $row = $query->row_array();
- }
- }
- // get values
- $values = @$this->get_values($row);
- // check posted data is in fields
- foreach ($values as $field => $value)
- {
- if (@!in_array($field, $fields))
- {
- unset($values[$field]);
- }
- if (array_key_exists($field, $this->set))
- {
- unset($values[$field]);
- }
- }
- // if validate is unsuccessful show errors (return false) else insert and redirect
- if ($this->check_errors())
- {
- // set siteID
- if (!$this->adminOverRide && $this->siteID)
- {
- $this->set['siteID'] = SITEID;
- }
-
- // set fields
- if ($this->set && sizeof($this->set) > 0)
- {
- $this->CI->db->set($this->set);
- unset($this->set);
- }
-
- // add row
- if (@!$row && !$id)
- {
- $this->CI->db->insert($table, $values);
- }
- // edit row
- else
- {
- if ($this->where && sizeof($this->where) > 0)
- {
- $this->CI->db->where($this->where);
- }
- $this->CI->db->where($id);
- $this->CI->db->update($table, $values);
- }
- unset($this->required);
- return TRUE;
- }
- else
- {
- return FALSE;
- }
- }
- else
- {
- return FALSE;
- }
- }
- // set paging
- function set_paging($totalRows, $limit = '')
- {
- // get default limit
- $limit = ($limit) ? $limit : $this->CI->site->config['paging'];
-
- // set pagination config
- $config['total_rows'] = $totalRows;
- $config['per_page'] = $limit;
- $config['full_tag_open'] = '<div class="pagination"><p>';
- $config['full_tag_close'] = '</p></div>';
- $config['num_links'] = 6;
- $this->CI->pagination->initialize($config);
- }
- // delete permanently
- function delete($table, $id)
- {
- // delete item from db
- if (!$this->adminOverRide && $this->siteID)
- {
- $this->CI->db->where('siteID', $this->siteID);
- }
- if ($this->where && sizeof($this->where) > 0)
- {
- $this->CI->db->where($this->where);
- }
- $this->CI->db->delete($table, $id);
- if ($this->CI->db->affected_rows())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- // delete from site but keep in database
- function soft_delete($table, $id)
- {
- // soft delete item from db
- if (!$this->adminOverRide && $this->siteID)
- {
- $this->CI->db->where('siteID', $this->siteID);
- }
- $this->CI->db->set('deleted', 1);
- if ($this->where && sizeof($this->where) > 0)
- {
- $this->CI->db->where($this->where);
- }
- $this->CI->db->where($id);
- $this->CI->db->update($table);
- if ($this->CI->db->affected_rows())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
-
- // order rows
- function order($table = '', $field = '')
- {
- // for each posted item, order it with new row id
- if ($table && $field)
- {
- foreach ($_POST[$table] as $key => $value)
- {
- if ($this->siteID)
- {
- $this->CI->db->where('siteID', $this->siteID);
- }
- $this->CI->db->where($field.'ID', $value);
- $this->CI->db->update($table, array($field.'Order' => ($key + 1)));
- }
- }
- else
- {
- return false;
- }
- }
- // encode url
- function encode($data)
- {
- return strtr(rtrim(base64_encode($data), '='), '+/', '-_');
- }
- // decode url
- function decode($base64)
- {
- return base64_decode(strtr($base64, '-_', '+/'));
- }
-
- }