PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/application/libraries/Core.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 1376 lines | 1028 code | 190 blank | 158 comment | 153 complexity | 45b6911b2581e9f032b644d48a3bdce3 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Halogy
  4. *
  5. * A user friendly, modular content management system for PHP 5.0
  6. * Built on CodeIgniter - http://codeigniter.com
  7. *
  8. * @package Halogy
  9. * @author Haloweb Ltd.
  10. * @copyright Copyright (c) 2008-2011, Haloweb Ltd.
  11. * @license http://halogy.com/license
  12. * @link http://halogy.com/
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. class Core {
  18. var $CI; // CI instance
  19. var $table ; // default table
  20. var $siteID; // id of the site
  21. var $uri_assoc_segment = 4; // segment where the magic happens
  22. var $adminOverRide = FALSE; // allows for override of siteID
  23. var $currentPage;
  24. var $where = array();
  25. var $set = array();
  26. var $required = array();
  27. function Core()
  28. {
  29. // init vars
  30. $this->CI =& get_instance();
  31. // get siteID, if available
  32. if (defined('SITEID'))
  33. {
  34. $this->siteID = SITEID;
  35. }
  36. // set groupID from session (if set)
  37. $this->groupID = ($this->CI->session->userdata('groupID')) ? $this->CI->session->userdata('groupID') : 0;
  38. }
  39. function get_page($pageID = FALSE, $uri = FALSE)
  40. {
  41. // get page data
  42. $this->CI->db->where('siteID', $this->siteID);
  43. // if getting by uri check its not deleted
  44. if ($uri)
  45. {
  46. $this->CI->db->where('deleted', 0);
  47. }
  48. if (intval($pageID))
  49. {
  50. $this->CI->db->where('pageID', $pageID);
  51. }
  52. elseif ($uri !== FALSE && $pageID === FALSE)
  53. {
  54. $this->CI->db->where('uri', $uri);
  55. }
  56. else
  57. {
  58. return FALSE;
  59. }
  60. // get the latest one, not a deleted one from the past
  61. $this->CI->db->order_by('dateCreated', 'desc');
  62. $query = $this->CI->db->get('pages', 1);
  63. if ($query->num_rows() == 1)
  64. {
  65. return $query->row_array();
  66. }
  67. else
  68. {
  69. return FALSE;
  70. }
  71. }
  72. function get_active_page($uri = '')
  73. {
  74. // get published page
  75. if (!$uri)
  76. {
  77. return FALSE;
  78. }
  79. $this->CI->db->where('siteID', $this->siteID);
  80. $this->CI->db->where('active', 1);
  81. $this->CI->db->where('deleted', 0);
  82. $this->CI->db->where('uri', $uri);
  83. $query = $this->CI->db->get('pages', 1);
  84. if ($query->num_rows() == 1)
  85. {
  86. return $query->row_array();
  87. }
  88. else
  89. {
  90. return FALSE;
  91. }
  92. }
  93. function generate_page($pageID, $admin = FALSE, $templateID = '')
  94. {
  95. // get page data
  96. $pagedata = $this->get_page($pageID);
  97. // load template, either from override or from page data
  98. if ($templateID)
  99. {
  100. $page = $this->CI->template->generate_template(array('templateID' => $templateID));
  101. }
  102. else
  103. {
  104. $page = $this->CI->template->generate_template($pagedata);
  105. }
  106. // set default parse variable to nothing
  107. $page['error'] = '';
  108. $page['message'] = '';
  109. // tell the parser some important info like versionID
  110. $page['pageID'] = $pagedata['pageID'];
  111. $page['templateID'] = $pagedata['templateID'];
  112. $page['versionID'] = $pagedata['versionID'];
  113. // if logged in as admin, then get the blocks from draft, otherwise get them from the published version
  114. $versionID = ($admin === TRUE) ? $pagedata['draftID'] : $pagedata['versionID'];
  115. // populate blocks from db (if they exist)
  116. if ($blocksResult = $this->get_blocks($versionID))
  117. {
  118. foreach($blocksResult as $blockRow)
  119. {
  120. // set bodies and get images for mkdn view
  121. $body[$blockRow['blockRef']] = form_prep($blockRow['body']);
  122. $mkdnBody[$blockRow['blockRef']] = $this->CI->template->parse_body($blockRow['body']);
  123. }
  124. }
  125. // parse for blocks
  126. preg_match_all('/block([a-z0-9\-_]+)/i', $page['body'], $blocks);
  127. if ($blocks)
  128. {
  129. foreach($blocks[1] as $block => $value)
  130. {
  131. $blockRef = 'block'.$value;
  132. if ($admin)
  133. {
  134. $page[$blockRef] = '
  135. <div class="halogycms_container">
  136. <div id="'.$blockRef.'" class="halogycms_edit">
  137. <div class="halogycms_buttons">
  138. <a href="#" class="halogycms_boldbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_bold.png" alt="Bold" title="Bold" class="halogycms_helper" /></a>
  139. <a href="#" class="halogycms_italicbutton"><img src="'.$this->CI->config->item('staticPath').'/images/btn_italic.png" alt="Italic" title="Italic" class="halogycms_helper" /></a>
  140. <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>
  141. <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>
  142. <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>
  143. <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>
  144. <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>
  145. <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>
  146. <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>
  147. <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>
  148. <a href="#" class="halogycms_editbutton">Edit</a>
  149. </div>
  150. <div class="halogycms_blockelement">'.@$mkdnBody[$blockRef].'</div>
  151. <div class="halogycms_editblock"><textarea rows="8" cols="10" class="code">'.@$body[$blockRef].'</textarea></div>
  152. </div>
  153. </div>
  154. ';
  155. }
  156. else
  157. {
  158. $page[$blockRef] = @$mkdnBody[$blockRef];
  159. }
  160. }
  161. }
  162. return $page;
  163. }
  164. function get_versions($pageID)
  165. {
  166. $this->CI->db->where('pageID', $pageID);
  167. $this->CI->db->where('published', 1);
  168. $this->CI->db->order_by('dateCreated', 'desc');
  169. $query = $this->CI->db->get('page_versions', 30);
  170. // get data
  171. if ($query->num_rows())
  172. {
  173. return $query->result_array();
  174. }
  175. else
  176. {
  177. return false;
  178. }
  179. }
  180. function get_drafts($pageID)
  181. {
  182. $this->CI->db->where('pageID', $pageID);
  183. $this->CI->db->where('published', 0);
  184. $this->CI->db->order_by('dateCreated', 'desc');
  185. $query = $this->CI->db->get('page_versions');
  186. // get data
  187. if ($query->num_rows())
  188. {
  189. return $query->result_array();
  190. }
  191. else
  192. {
  193. return false;
  194. }
  195. }
  196. function get_blocks($versionID)
  197. {
  198. $this->CI->db->where('siteID', $this->siteID);
  199. $this->CI->db->select('MAX(blockID) as blockID');
  200. $this->CI->db->where('versionID', $versionID);
  201. $this->CI->db->group_by('blockRef');
  202. $this->CI->db->order_by('dateCreated','DESC');
  203. $query = $this->CI->db->get('page_blocks');
  204. $result = $query->result_array();
  205. $numBlocks = $query->num_rows();
  206. // get data
  207. if ($numBlocks > 0)
  208. {
  209. foreach($result as $row)
  210. {
  211. $blockIDs[] = $row['blockID'];
  212. }
  213. $this->CI->db->where('siteID', $this->siteID);
  214. $this->CI->db->where_in('blockID', $blockIDs);
  215. $this->CI->db->where('versionID', $versionID);
  216. $this->CI->db->order_by('blockID');
  217. $query = $this->CI->db->get('page_blocks', $numBlocks);
  218. return $query->result_array();
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }
  225. function get_template($templateID = '')
  226. {
  227. // default where
  228. $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
  229. $this->CI->db->where('t1.deleted', 0, FALSE);
  230. $this->CI->db->where('templateID', $templateID);
  231. // select
  232. $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
  233. // join revisions
  234. $this->CI->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
  235. // get em
  236. $query = $this->CI->db->get('templates t1', 1);
  237. if ($query->num_rows())
  238. {
  239. return $query->row_array();
  240. }
  241. else
  242. {
  243. return FALSE;
  244. }
  245. }
  246. function get_module_template($modulePath = '')
  247. {
  248. // default where
  249. $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
  250. $this->CI->db->where('t1.deleted', 0, FALSE);
  251. $this->CI->db->where('modulePath', $modulePath);
  252. // select
  253. $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
  254. // join revisions
  255. $this->CI->db->join('template_versions t2', 't2.versionID = t1 . versionID', 'left');
  256. // get em
  257. $query = $this->CI->db->get('templates t1', 1);
  258. if ($query->num_rows())
  259. {
  260. return $query->row_array();
  261. }
  262. else
  263. {
  264. return FALSE;
  265. }
  266. }
  267. function get_include($includeRef = '', $includeID = '')
  268. {
  269. // default where
  270. $this->CI->db->where('t1.siteID', $this->siteID, FALSE);
  271. $this->CI->db->where('t1.deleted', 0, FALSE);
  272. // get by reference
  273. if ($includeRef)
  274. {
  275. $this->CI->db->where('includeRef', $includeRef);
  276. }
  277. // get by ID
  278. elseif ($includeID)
  279. {
  280. $this->CI->db->where('includeID', $includeID);
  281. }
  282. // or fail
  283. else
  284. {
  285. return FALSE;
  286. }
  287. // select
  288. $this->CI->db->select('t1.*, t2.body, t2.dateCreated, t2.userID');
  289. // join revisions
  290. $this->CI->db->join('include_versions t2', 't2.versionID = t1 . versionID', 'left');
  291. // get em
  292. $query = $this->CI->db->get('includes t1', 1);
  293. if ($query->num_rows())
  294. {
  295. return $query->row_array();
  296. }
  297. else
  298. {
  299. return FALSE;
  300. }
  301. }
  302. function lookup_user($userID, $display = FALSE)
  303. {
  304. // default wheres
  305. $this->CI->db->where('userID', $userID);
  306. // grab
  307. $query = $this->CI->db->get('users', 1);
  308. if ($query->num_rows())
  309. {
  310. $row = $query->row_array();
  311. if ($display !== FALSE)
  312. {
  313. return ($row['displayName']) ? $row['displayName'] : $row['firstName'].' '.$row['lastName'];
  314. }
  315. else
  316. {
  317. return $row;
  318. }
  319. }
  320. else
  321. {
  322. return FALSE;
  323. }
  324. }
  325. function add_draft($pageID)
  326. {
  327. if ($pagedata = $this->get_page($pageID))
  328. {
  329. // add new version
  330. $this->CI->db->set('pageID', $pageID);
  331. $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
  332. $this->CI->db->set('userID', $this->CI->session->userdata('userID'));
  333. $this->CI->db->set('siteID', $this->siteID);
  334. $this->CI->db->insert('page_versions');
  335. // get version ID
  336. $draftID = $this->CI->db->insert_id();
  337. // update page draft
  338. $this->CI->db->set('draftID', $draftID);
  339. $this->CI->db->where('siteID', $this->siteID);
  340. $this->CI->db->where('pageID', $pageID);
  341. $this->CI->db->update('pages');
  342. // add the old blocks in to this new version
  343. if ($blocks = $this->get_blocks($pagedata['draftID']))
  344. {
  345. foreach($blocks as $block)
  346. {
  347. $body = $block['body'];
  348. $this->add_block($body, $draftID, $block['blockRef']);
  349. }
  350. }
  351. return $draftID;
  352. }
  353. else
  354. {
  355. return FALSE;
  356. }
  357. }
  358. function get_latest_block($versionID)
  359. {
  360. $this->CI->db->where('versionID', $versionID);
  361. $this->CI->db->where('siteID', $this->siteID);
  362. $this->CI->db->order_by('dateCreated', 'desc');
  363. // grab
  364. $query = $this->CI->db->get('page_blocks', 1);
  365. if ($query->num_rows())
  366. {
  367. $row = $query->row_array();
  368. return $row;
  369. }
  370. else
  371. {
  372. return FALSE;
  373. }
  374. }
  375. function publish_page($pageID, $draftID)
  376. {
  377. $this->CI->db->set('dateModified', date("Y-m-d H:i:s"));
  378. $this->CI->db->set('datePublished', date("Y-m-d H:i:s"));
  379. $this->CI->db->set('versionID', $draftID);
  380. $this->CI->db->set('active', 1);
  381. $this->CI->db->where('pageID', $pageID);
  382. $this->CI->db->where('siteID', $this->siteID);
  383. $this->CI->db->update('pages');
  384. return TRUE;
  385. }
  386. function publish_draft($draftID)
  387. {
  388. // publish version
  389. $this->CI->db->set('published', 1);
  390. $this->CI->db->where('siteID', $this->siteID);
  391. $this->CI->db->where('versionID', $draftID);
  392. $this->CI->db->update('page_versions');
  393. return TRUE;
  394. }
  395. function revert_version($pageID, $versionID)
  396. {
  397. // update the template with version
  398. $this->CI->db->set('versionID', $versionID);
  399. $this->CI->db->where('pageID', $pageID);
  400. $this->CI->db->where('siteID', $this->siteID);
  401. $this->CI->db->update('pages');
  402. return TRUE;
  403. }
  404. function revert_draft($pageID, $draftID)
  405. {
  406. // update the template with version
  407. $this->CI->db->set('draftID', $draftID);
  408. $this->CI->db->where('pageID', $pageID);
  409. $this->CI->db->where('siteID', $this->siteID);
  410. $this->CI->db->update('pages');
  411. return TRUE;
  412. }
  413. function add_block($body, $versionID, $blockRef = 'block')
  414. {
  415. // delete blocks for this version
  416. $this->CI->db->where('page_blocks.siteID', $this->siteID);
  417. $this->CI->db->where('page_blocks.versionID', $versionID);
  418. $this->CI->db->where('page_blocks.blockRef', $blockRef);
  419. $this->CI->db->delete('page_blocks');
  420. // add block
  421. $this->CI->db->query("SET NAMES 'utf8'");
  422. $this->CI->db->set('versionID', $versionID);
  423. $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
  424. $this->CI->db->set('siteID', $this->siteID);
  425. $this->CI->db->set('blockRef', $blockRef);
  426. $this->CI->db->set('body', $body);
  427. $this->CI->db->insert('page_blocks');
  428. return TRUE;
  429. }
  430. function add_view($pageID)
  431. {
  432. $this->CI->db->set('views', 'views+1', false);
  433. $this->CI->db->where('pageID', $pageID);
  434. $this->CI->db->where('siteID', $this->siteID);
  435. $this->CI->db->update('pages');
  436. }
  437. function get_web_form_by_ref($formRef)
  438. {
  439. $this->CI->db->where('formRef', $formRef);
  440. $this->CI->db->where('deleted', 0);
  441. $this->CI->db->where('siteID', $this->siteID);
  442. $query = $this->CI->db->get('web_forms', 1);
  443. if ($query->num_rows())
  444. {
  445. return $query->row_array();
  446. }
  447. else
  448. {
  449. return FALSE;
  450. }
  451. }
  452. function web_form()
  453. {
  454. // get web form
  455. if (!$webform = $this->CI->core->get_web_form_by_ref($this->CI->core->decode($this->CI->input->post('formID'))))
  456. {
  457. return FALSE;
  458. }
  459. // set main required field
  460. $this->CI->form_validation->set_rules('email', 'Email', 'required|valid_email');
  461. // find out if a user account needs to be created
  462. $account = ($webform['account']) ? TRUE : FALSE;
  463. // get required fields
  464. $required = $this->CI->input->post('required', TRUE);
  465. // get optional required fields
  466. if ($required)
  467. {
  468. $requiredArray = explode('|', $required);
  469. foreach($requiredArray as $field)
  470. {
  471. $this->CI->form_validation->set_rules($field, ucfirst($field), 'required');
  472. }
  473. }
  474. // get first and last name
  475. if ($this->CI->input->post('firstName', TRUE))
  476. {
  477. $firstName = $this->CI->input->post('firstName', TRUE);
  478. $lastName = $this->CI->input->post('lastName', TRUE);
  479. }
  480. elseif ($fullName = $this->CI->input->post('fullName', TRUE))
  481. {
  482. $fullNameArray = @explode(' ', $fullName);
  483. $lastName = (sizeof($fullNameArray) > 0) ? ucfirst(trim(end($fullNameArray))) : '';
  484. $firstName = (sizeof($fullNameArray) > 0) ? ucfirst(trim($fullNameArray[0])) : $fullName;
  485. }
  486. else
  487. {
  488. $firstName = '';
  489. $lastName = '';
  490. }
  491. // at least set the name and email in to a session
  492. if (!$this->CI->session->userdata('session_user'))
  493. {
  494. $this->CI->session->set_userdata('email', $this->CI->input->post('email', TRUE));
  495. $this->CI->session->set_userdata('firstName', $firstName);
  496. $this->CI->session->set_userdata('lastName', $lastName);
  497. }
  498. // if capturing check user is unique and a password matches
  499. if ($account)
  500. {
  501. // email and message are always required
  502. $this->CI->form_validation->set_rules('email', 'Email', 'required|valid_email|unique[users.email]|trim');
  503. // check if password was submitted, make it required if so
  504. if (array_key_exists('password', $_POST))
  505. {
  506. // require password confirm?
  507. if (isset($_POST['confirmPassword']))
  508. {
  509. $this->form_validation->set_rules('password', 'Password', 'required|matches[confirmPassword]');
  510. }
  511. else
  512. {
  513. $this->form_validation->set_rules('password', 'Password', 'required');
  514. }
  515. }
  516. }
  517. // look for files
  518. $files = FALSE;
  519. if ($webform['fileTypes'] && count($_FILES))
  520. {
  521. foreach($_FILES as $name => $file)
  522. {
  523. $this->CI->uploads->maxSize = '2000';
  524. $this->CI->uploads->allowedTypes = $webform['fileTypes'];
  525. // check a file has actually been uploaded
  526. if ($file['name'] != '')
  527. {
  528. if ($fileData = $this->CI->uploads->upload_file($name))
  529. {
  530. $files[$name] = $fileData;
  531. }
  532. else
  533. {
  534. $this->CI->form_validation->set_error($this->CI->uploads->errors);
  535. }
  536. }
  537. }
  538. }
  539. // captcha
  540. if (isset($_POST['captcha']) && !$this->_captcha_check())
  541. {
  542. $this->CI->form_validation->set_error('Sorry you didn\'t pass the spam check. Please make sure Javascript is enabled.');
  543. }
  544. // add ticket
  545. if ($this->CI->form_validation->run())
  546. {
  547. if ($account)
  548. {
  549. // create user
  550. $this->create_user();
  551. // set admin session name, if given
  552. if (!$this->CI->site->config['activation'])
  553. {
  554. $this->CI->load->library('auth');
  555. $username = array('field' => 'email', 'label' => 'Email address', 'value' => $this->CI->input->post('email'));
  556. $password = ($this->CI->input->post('password')) ? $this->CI->input->post('password', TRUE) : substr(md5(time()),0,6);
  557. // login or get error message
  558. if (!$this->CI->auth->login($username, $password, 'session_user', FALSE))
  559. {
  560. $this->CI->form_validation->set_error($this->CI->auth->error);
  561. }
  562. }
  563. }
  564. // add ticket
  565. $this->add_ticket($webform, $files);
  566. // redirect if set
  567. if ($redirect = $webform['outcomeRedirect'])
  568. {
  569. redirect($redirect);
  570. }
  571. // get message if set
  572. if ($message = $webform['outcomeMessage'])
  573. {
  574. return $message;
  575. }
  576. else
  577. {
  578. return 'Thank you, your message was sent successfully.';
  579. }
  580. }
  581. else
  582. {
  583. return FALSE;
  584. }
  585. }
  586. function add_ticket($webform, $files = '')
  587. {
  588. // get web form
  589. if (!$webform)
  590. {
  591. return FALSE;
  592. }
  593. if ($this->CI->input->post('email'))
  594. {
  595. // set system fields
  596. $fields = array('required', 'formID', 'fieldSet', 'fileTypes', 'account', 'formName', 'outcomeEmails', 'outcomeRedirect', 'outcomeMessage', 'fullName', 'email', 'subject', 'message', 'toEmail', 'captcha', 'firstName', 'lastName', 'password', 'confirmPassword', 'groupID');
  597. // set default message
  598. $message = '';
  599. $filepaths = '';
  600. // get extra posted info and prepend to message
  601. if (count($_POST))
  602. {
  603. foreach($_POST as $post => $value)
  604. {
  605. if (!in_array($post, $fields) && !preg_match('/^submit$|^submit\_x$|^submit\_y|^x|^y/i', $post))
  606. {
  607. $postValue = $this->CI->input->post($post, TRUE);
  608. $message .= "\t".ucfirst($post) . ": ".$value."\n\n";
  609. }
  610. }
  611. }
  612. // get files and prepend to message
  613. if ($files)
  614. {
  615. $message .= "\tFiles: ".count($files).((count($files) != 1) ? ' files' : ' file')." uploaded\n\n";
  616. $filepaths .= '<br />';
  617. foreach($files as $name => $fileData)
  618. {
  619. $filepaths .= '<br /><a href="'.site_url($this->CI->uploads->uploadsPath.'/'.$fileData['file_name']).'">'.$fileData['client_name'].'</a>';
  620. }
  621. }
  622. // get posted message
  623. $message .= (strlen($message) > 1) ? "\n" : '';
  624. $message .= $this->CI->input->post('message', TRUE);
  625. // set defaults
  626. $fullName = ($this->CI->input->post('fullName')) ? $this->CI->input->post('fullName', TRUE) : 'N/A';
  627. $subject = ($this->CI->input->post('subject')) ? $this->CI->input->post('subject', TRUE) : (($webform['formName']) ? $webform['formName'] : 'No Subject');
  628. // set outcome emails
  629. if ($this->CI->input->post('outcomeEmails'))
  630. {
  631. $outcomeEmails = explode(',', $this->CI->input->post('outcomeEmails'));
  632. }
  633. else
  634. {
  635. $outcomeEmails = ($webform['outcomeEmails']) ? explode(',', $webform['outcomeEmails']) : $this->CI->site->config['siteEmail'];
  636. }
  637. // get first name and last name
  638. $names = explode(' ', $fullName);
  639. $firstName = (sizeof($names) > 1 && $names[0]) ? ucfirst(trim($names[0])) : $name;
  640. $lastName = (sizeof($names) > 1) ? ucfirst(end($names)) : '';
  641. // add ticket
  642. $this->CI->db->set('siteID', $this->siteID);
  643. $this->CI->db->set('dateCreated', date("Y-m-d H:i:s"));
  644. ($webform['formName']) ? $this->CI->db->set('formName', $webform['formName']) : '';
  645. $this->CI->db->set('fullName', $fullName);
  646. $this->CI->db->set('email', $this->CI->input->post('email', TRUE));
  647. $this->CI->db->set('subject', $subject);
  648. $this->CI->db->set('body', $message.$filepaths);
  649. $this->CI->db->insert('tickets');
  650. $ticketID = $this->CI->db->insert_id();
  651. // set header and footer
  652. $emailHeader = str_replace('{name}', $fullName, $this->CI->site->config['emailHeader']);
  653. $emailHeader = str_replace('{first-name}', $firstName, $emailHeader);
  654. $emailHeader = str_replace('{last-name}', $lastName, $emailHeader);
  655. $emailHeader = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailHeader);
  656. $emailFooter = str_replace('{name}', $fullName, $this->CI->site->config['emailFooter']);
  657. $emailFooter = str_replace('{first-name}', $firstName, $emailFooter);
  658. $emailFooter = str_replace('{last-name}', $lastName, $emailFooter);
  659. $emailFooter = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailFooter);
  660. $emailTicket = str_replace('{name}', $fullName, $this->CI->site->config['emailTicket']);
  661. $emailTicket = str_replace('{first-name}', $firstName, $emailTicket);
  662. $emailTicket = str_replace('{last-name}', $lastName, $emailTicket);
  663. $emailTicket = str_replace('{email}', $this->CI->input->post('email', TRUE), $emailTicket);
  664. // send despatch email to customer
  665. $body = $emailHeader."\n\n";
  666. $body .= $emailTicket."\n\n";
  667. $body .= "\tTicket ID: ".$ticketID."\n";
  668. $body .= "\tSubject: ".$subject."\n";
  669. $body .= "\tName: ".$fullName."\n";
  670. $body .= "\tEmail: ".$this->CI->input->post('email')."\n\n";
  671. // attach message
  672. if ($message)
  673. {
  674. $body .= "Message:\n";
  675. $body .= "---------------------------------------------\n\n";
  676. $body .= $message."\n\n";
  677. $body .= "---------------------------------------------\n\n";
  678. }
  679. // send username and password
  680. if ($webform['account'])
  681. {
  682. $body .= "Your login details are below:\n";
  683. $body .= "---------------------------------------------\n\n";
  684. $body .= "Your email: \t".$this->CI->input->post('email')."\n";
  685. $body .= "Your password: \t".(($this->CI->input->post('password', TRUE)) ? $this->CI->input->post('password', TRUE) : substr(md5(time()),0,6))."\n\n";
  686. $body .= "---------------------------------------------\n\n";
  687. }
  688. $footerBody = $emailFooter;
  689. // load email lib and email user and admin
  690. $this->CI->load->library('email');
  691. // attach files
  692. if ($files)
  693. {
  694. foreach ($files as $file)
  695. {
  696. $this->CI->email->attach($file['full_path']);
  697. }
  698. }
  699. // send to recipient
  700. $this->CI->email->to($this->CI->input->post('email', TRUE));
  701. $this->CI->email->from($this->CI->site->config['siteEmail'], $this->CI->site->config['siteName']);
  702. $this->CI->email->subject('[#'.$ticketID.']: ' . $subject);
  703. $this->CI->email->message($body.$footerBody);
  704. $this->CI->email->send();
  705. $this->CI->email->clear();
  706. // send to CC or admin
  707. $this->CI->email->to($outcomeEmails);
  708. $this->CI->email->from($this->CI->input->post('email', TRUE));
  709. $this->CI->email->subject('FW: [#'.$ticketID.']: ' . $this->CI->input->post('subject', TRUE));
  710. $this->CI->email->message("A web form was submitted on ".$this->CI->site->config['siteName'].".\n\n---------------------------------------------\n\n".$body.$footerBody);
  711. $this->CI->email->send();
  712. return $ticketID;
  713. }
  714. else
  715. {
  716. return FALSE;
  717. }
  718. }
  719. function create_user()
  720. {
  721. // get values
  722. $this->CI->core->get_values('users');
  723. // security check
  724. if ($this->CI->input->post('username')) $this->CI->core->set['username'] = '';
  725. if ($this->CI->input->post('subscribed')) $this->CI->core->set['subscribed'] = '';
  726. if ($this->CI->input->post('plan')) $this->CI->core->set['plan'] = '';
  727. if ($this->CI->input->post('siteID')) $this->CI->core->set['siteID'] = $this->siteID;
  728. if ($this->CI->input->post('userID')) $this->CI->core->set['userID'] = '';
  729. if ($this->CI->input->post('kudos')) $this->CI->core->set['kudos'] = '';
  730. if ($this->CI->input->post('posts')) $this->CI->core->set['posts'] = '';
  731. // set folder (making sure it's not an admin folder)
  732. $permissionGroupsArray = $this->CI->permission->get_groups('admin');
  733. foreach((array)$permissionGroupsArray as $group)
  734. {
  735. $permissionGroups[$group['groupID']] = $group['groupName'];
  736. }
  737. if ($this->CI->input->post('groupID') > 0 && !@in_array($this->CI->input->post('groupID'), $permissionGroups))
  738. {
  739. $this->CI->core->set['groupID'] = $this->CI->input->post('groupID');
  740. }
  741. // set date
  742. $this->CI->core->set['dateCreated'] = date("Y-m-d H:i:s");
  743. // init null name
  744. $firstName = '';
  745. $lastName = '';
  746. // set name if only fullName is posted
  747. if ($this->CI->input->post('fullName') && (!$this->CI->input->post('firstName') && !$this->CI->input->post('lastName')))
  748. {
  749. $fullName = $this->CI->input->post('fullName', TRUE);
  750. $fullNameArray = @explode(' ', $fullName);
  751. $lastName = (sizeof($fullNameArray) > 0) ? ucfirst(trim(end($fullNameArray))) : '';
  752. $firstName = (sizeof($fullNameArray) > 0) ? ucfirst(trim($fullNameArray[0])) : $fullName;
  753. $this->CI->core->set['firstName'] = $firstName;
  754. $this->CI->core->set['lastName'] = $lastName;
  755. }
  756. // set first name
  757. if ($this->CI->input->post('firstName'))
  758. {
  759. $firstName = ucfirst($this->CI->input->post('firstName', TRUE));
  760. $this->CI->core->set['firstName'] = $firstName;
  761. }
  762. // set last name
  763. if ($this->CI->input->post('lastName'))
  764. {
  765. $lastName = ucfirst($this->CI->input->post('lastName', TRUE));
  766. $this->CI->core->set['lastName'] = $lastName;
  767. }
  768. // generate password
  769. if (!$this->CI->input->post('password'))
  770. {
  771. $password = md5(substr(md5(time()),0,6));
  772. $this->CI->core->set['password'] = $password;
  773. }
  774. // set manual activation
  775. if ($this->CI->site->config['activation'])
  776. {
  777. $this->CI->core->set['active'] = 0;
  778. }
  779. // set email on flash data
  780. $flashEmail = $this->CI->session->flashdata('email');
  781. // update table
  782. if ($this->CI->input->post('email') && ($this->CI->input->post('password') || $password))
  783. {
  784. if ($this->CI->core->update('users'))
  785. {
  786. $result = array(
  787. 'userID' => $this->CI->db->insert_id(),
  788. 'email' => $this->CI->input->post('email', TRUE),
  789. 'password' => ($this->CI->input->post('password')) ? $this->CI->input->post('password', TRUE) : $password,
  790. 'firstName' => $firstName,
  791. 'lastName' => $lastName
  792. );
  793. return $result;
  794. }
  795. else
  796. {
  797. return FALSE;
  798. }
  799. }
  800. else
  801. {
  802. return FALSE;
  803. }
  804. }
  805. function _captcha_check()
  806. {
  807. // if captcha is posted, check its not a bot (requires js)
  808. if ($this->CI->input->post('captcha') == 'notabot')
  809. {
  810. return TRUE;
  811. }
  812. elseif ($this->CI->input->post('captcha') != 'notabot')
  813. {
  814. $this->CI->form_validation->set_message('captcha_check', 'You didn\'t pass the spam check, please contact us to post a comment.');
  815. return FALSE;
  816. }
  817. }
  818. /* utilities */
  819. // gets posted values
  820. function get_post()
  821. {
  822. if (count($_POST))
  823. {
  824. $post = array();
  825. foreach($_POST as $key => $value)
  826. {
  827. $post[$key] = $this->CI->input->post($key);
  828. }
  829. return $post;
  830. }
  831. else
  832. {
  833. return FALSE;
  834. }
  835. }
  836. // gets values from post and/or the row
  837. function get_values($data = '', $id = '')
  838. {
  839. // init array
  840. $values = array();
  841. // populate by row if set
  842. if (@is_array($data))
  843. {
  844. $row = $data;
  845. $values = $data;
  846. }
  847. // get data from database
  848. else
  849. {
  850. $table = $data;
  851. if ($id)
  852. {
  853. $query = $this->CI->db->get_where($table, $id);
  854. if ($query->num_rows())
  855. {
  856. $row = $query->row_array();
  857. $values = $row;
  858. }
  859. }
  860. }
  861. // get post if there is any
  862. if ($post = $this->get_post())
  863. {
  864. // check posted data is in fields
  865. foreach ($post as $field => $value)
  866. {
  867. // make sure the value is just a normal value and not an array
  868. if (!is_array($value))
  869. {
  870. if (isset($row) && isset($row[$field]) && $value == $row[$field])
  871. {
  872. unset($this->required[$field]);
  873. }
  874. else
  875. {
  876. // prep password
  877. if ($field == 'password')
  878. {
  879. if ($value != '')
  880. {
  881. $values[$field] = md5($value);
  882. }
  883. }
  884. // overwrite value with posted value
  885. else
  886. {
  887. $values[$field] = $value;
  888. }
  889. }
  890. if (array_key_exists($field, $this->set))
  891. {
  892. unset($values[$field]);
  893. }
  894. }
  895. }
  896. }
  897. return $values;
  898. }
  899. // is ajax?
  900. function is_ajax()
  901. {
  902. return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
  903. }
  904. // check for errors
  905. function check_errors()
  906. {
  907. // set rules for validation
  908. if (isset($this->required))
  909. {
  910. $config = array();
  911. foreach ($this->required as $field => $name)
  912. {
  913. if (is_array($name))
  914. {
  915. $config[$field] = array('field' => $field, 'label' => $name['label'], 'rules' => $name['rules']);
  916. }
  917. else
  918. {
  919. if ($field == 'email')
  920. {
  921. $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required|valid_email');
  922. }
  923. elseif ($field == 'password')
  924. {
  925. $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required|matches[confirmPassword]');
  926. }
  927. else
  928. {
  929. $config[$field] = array('field' => $field, 'label' => $name, 'rules' => 'required');
  930. }
  931. }
  932. }
  933. // set rules and fields for validation
  934. $this->CI->form_validation->set_rules($config);
  935. if (!$this->CI->form_validation->run() && isset($this->required) && count($this->required))
  936. {
  937. return FALSE;
  938. }
  939. else
  940. {
  941. return TRUE;
  942. }
  943. }
  944. else
  945. {
  946. return TRUE;
  947. }
  948. }
  949. // get all rows from a table
  950. function viewall($table, $where = '', $order = '', $limit = '')
  951. {
  952. // get table fields
  953. $fields = $this->CI->db->list_fields($table);
  954. // set limit from uri if set
  955. $limit = (!$limit) ? $this->CI->site->config['paging'] : $limit;
  956. // get uri array for ordering
  957. $uriArray = $this->CI->uri->uri_to_assoc($this->uri_assoc_segment);
  958. // set order on order array
  959. if (count($uriArray))
  960. {
  961. foreach($uriArray as $key => $value)
  962. {
  963. if ($key)
  964. {
  965. if ($key == 'orderasc')
  966. {
  967. $this->CI->db->order_by($value,'asc');
  968. }
  969. elseif ($key == 'orderdesc')
  970. {
  971. $this->CI->db->order_by($value,'desc');
  972. }
  973. }
  974. }
  975. }
  976. // order override
  977. elseif ($order && !is_array($order))
  978. {
  979. $this->CI->db->order_by($order, 'asc');
  980. }
  981. elseif ($order && is_array($order))
  982. {
  983. $this->CI->db->order_by($order[0], $order[1]);
  984. }
  985. if (!(isset($uriArray['orderasc']) || isset($uriArray['orderdesc'])) && in_array('dateCreated', $fields))
  986. {
  987. $this->CI->db->order_by('dateCreated', 'desc');
  988. }
  989. // wheres
  990. if ($where)
  991. {
  992. $this->CI->db->where($where);
  993. }
  994. if (!$this->adminOverRide && $this->siteID)
  995. {
  996. $this->CI->db->where('siteID', $this->siteID);
  997. }
  998. if (in_array('deleted', $fields))
  999. {
  1000. $this->CI->db->where('deleted', 0);
  1001. }
  1002. // get and return results
  1003. $query = $this->CI->db->get($table, $limit, $this->CI->pagination->offset);
  1004. $output[$table] = $query->result_array();
  1005. // do same thing again but get count
  1006. if ($where)
  1007. {
  1008. $this->CI->db->where($where);
  1009. }
  1010. if (!$this->adminOverRide && $this->siteID)
  1011. {
  1012. $this->CI->db->where('siteID', $this->siteID);
  1013. }
  1014. if (in_array('deleted', $fields))
  1015. {
  1016. $this->CI->db->where('deleted', 0);
  1017. }
  1018. $query_total = $this->CI->db->get($table);
  1019. $totalRows = $query_total->num_rows();
  1020. // set pagination config
  1021. $this->set_paging($totalRows, $limit);
  1022. return $output;
  1023. }
  1024. // update table
  1025. function update($table, $id = '')
  1026. {
  1027. if (count($_POST) || count($_FILES))
  1028. {
  1029. // get fields of this table
  1030. $fields = $this->CI->db->list_fields($table);
  1031. // get data from database
  1032. if ($id)
  1033. {
  1034. $query = $this->CI->db->get_where($table, $id);
  1035. if ($query->num_rows())
  1036. {
  1037. $row = $query->row_array();
  1038. }
  1039. }
  1040. // get values
  1041. $values = @$this->get_values($row);
  1042. // check posted data is in fields
  1043. foreach ($values as $field => $value)
  1044. {
  1045. if (@!in_array($field, $fields))
  1046. {
  1047. unset($values[$field]);
  1048. }
  1049. if (array_key_exists($field, $this->set))
  1050. {
  1051. unset($values[$field]);
  1052. }
  1053. }
  1054. // if validate is unsuccessful show errors (return false) else insert and redirect
  1055. if ($this->check_errors())
  1056. {
  1057. // set siteID
  1058. if (!$this->adminOverRide && $this->siteID)
  1059. {
  1060. $this->set['siteID'] = SITEID;
  1061. }
  1062. // set fields
  1063. if ($this->set && sizeof($this->set) > 0)
  1064. {
  1065. $this->CI->db->set($this->set);
  1066. unset($this->set);
  1067. }
  1068. // add row
  1069. if (@!$row && !$id)
  1070. {
  1071. $this->CI->db->insert($table, $values);
  1072. }
  1073. // edit row
  1074. else
  1075. {
  1076. if ($this->where && sizeof($this->where) > 0)
  1077. {
  1078. $this->CI->db->where($this->where);
  1079. }
  1080. $this->CI->db->where($id);
  1081. $this->CI->db->update($table, $values);
  1082. }
  1083. unset($this->required);
  1084. return TRUE;
  1085. }
  1086. else
  1087. {
  1088. return FALSE;
  1089. }
  1090. }
  1091. else
  1092. {
  1093. return FALSE;
  1094. }
  1095. }
  1096. // set paging
  1097. function set_paging($totalRows, $limit = '')
  1098. {
  1099. // get default limit
  1100. $limit = ($limit) ? $limit : $this->CI->site->config['paging'];
  1101. // set pagination config
  1102. $config['total_rows'] = $totalRows;
  1103. $config['per_page'] = $limit;
  1104. $config['full_tag_open'] = '<div class="pagination"><p>';
  1105. $config['full_tag_close'] = '</p></div>';
  1106. $config['num_links'] = 6;
  1107. $this->CI->pagination->initialize($config);
  1108. }
  1109. // delete permanently
  1110. function delete($table, $id)
  1111. {
  1112. // delete item from db
  1113. if (!$this->adminOverRide && $this->siteID)
  1114. {
  1115. $this->CI->db->where('siteID', $this->siteID);
  1116. }
  1117. if ($this->where && sizeof($this->where) > 0)
  1118. {
  1119. $this->CI->db->where($this->where);
  1120. }
  1121. $this->CI->db->delete($table, $id);
  1122. if ($this->CI->db->affected_rows())
  1123. {
  1124. return true;
  1125. }
  1126. else
  1127. {
  1128. return false;
  1129. }
  1130. }
  1131. // delete from site but keep in database
  1132. function soft_delete($table, $id)
  1133. {
  1134. // soft delete item from db
  1135. if (!$this->adminOverRide && $this->siteID)
  1136. {
  1137. $this->CI->db->where('siteID', $this->siteID);
  1138. }
  1139. $this->CI->db->set('deleted', 1);
  1140. if ($this->where && sizeof($this->where) > 0)
  1141. {
  1142. $this->CI->db->where($this->where);
  1143. }
  1144. $this->CI->db->where($id);
  1145. $this->CI->db->update($table);
  1146. if ($this->CI->db->affected_rows())
  1147. {
  1148. return true;
  1149. }
  1150. else
  1151. {
  1152. return false;
  1153. }
  1154. }
  1155. // order rows
  1156. function order($table = '', $field = '')
  1157. {
  1158. // for each posted item, order it with new row id
  1159. if ($table && $field)
  1160. {
  1161. foreach ($_POST[$table] as $key => $value)
  1162. {
  1163. if ($this->siteID)
  1164. {
  1165. $this->CI->db->where('siteID', $this->siteID);
  1166. }
  1167. $this->CI->db->where($field.'ID', $value);
  1168. $this->CI->db->update($table, array($field.'Order' => ($key + 1)));
  1169. }
  1170. }
  1171. else
  1172. {
  1173. return false;
  1174. }
  1175. }
  1176. // encode url
  1177. function encode($data)
  1178. {
  1179. return strtr(rtrim(base64_encode($data), '='), '+/', '-_');
  1180. }
  1181. // decode url
  1182. function decode($base64)
  1183. {
  1184. return base64_decode(strtr($base64, '-_', '+/'));
  1185. }
  1186. }