PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/system/expressionengine/modules/file/mod.file.php

https://bitbucket.org/tdevonshire/hoolux
PHP | 1021 lines | 652 code | 230 blank | 139 comment | 146 complexity | 4412ae168900dbc12f8210ca26ea26f6 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author EllisLab Dev Team
  7. * @copyright Copyright (c) 2003 - 2012, EllisLab, Inc.
  8. * @license http://ellislab.com/expressionengine/user-guide/license.html
  9. * @link http://ellislab.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // --------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Channel Module
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Modules
  19. * @category Modules
  20. * @author EllisLab Dev Team
  21. * @link http://ellislab.com
  22. */
  23. class File {
  24. var $limit = '100'; // Default maximum query results if not specified.
  25. var $reserved_cat_segment = '';
  26. var $use_category_names = FALSE;
  27. var $categories = array();
  28. var $catfields = array();
  29. var $valid_thumbs = array();
  30. var $sql = '';
  31. var $return_data = ''; // Final data
  32. // Pagination variables
  33. var $paginate = FALSE;
  34. var $paginate_data = '';
  35. var $pagination_links = '';
  36. var $page_next = '';
  37. var $page_previous = '';
  38. var $current_page = 1;
  39. var $total_pages = 1;
  40. var $display_by = '';
  41. var $total_rows = 0;
  42. var $pager_sql = '';
  43. var $p_page = '';
  44. /**
  45. * Constructor
  46. */
  47. public function __construct()
  48. {
  49. // Make a local reference to the ExpressionEngine super object
  50. $this->EE =& get_instance();
  51. $this->limit = $this->limit;
  52. $this->query_string = ($this->EE->uri->page_query_string != '') ? $this->EE->uri->page_query_string : $this->EE->uri->query_string;
  53. if ($this->EE->config->item("use_category_name") == 'y' && $this->EE->config->item("reserved_category_word") != '')
  54. {
  55. $this->use_category_names = $this->EE->config->item("use_category_name");
  56. $this->reserved_cat_segment = $this->EE->config->item("reserved_category_word");
  57. }
  58. }
  59. // ------------------------------------------------------------------------
  60. /**
  61. * Files tag
  62. */
  63. function entries()
  64. {
  65. $this->uri = ($this->query_string != '') ? $this->query_string : 'index.php';
  66. $this->_fetch_disable_param();
  67. $this->fetch_pagination_data();
  68. //$this->parse_gallery_tag();
  69. $this->build_sql_query();
  70. if (empty($this->sql))
  71. {
  72. return $this->EE->TMPL->no_results();
  73. }
  74. $this->query = $this->sql;
  75. if ($this->query->num_rows() == 0)
  76. {
  77. return $this->EE->TMPL->no_results();
  78. }
  79. $this->EE->load->library('typography');
  80. $this->EE->typography->initialize(array(
  81. 'convert_curly' => FALSE)
  82. );
  83. $this->fetch_categories();
  84. $this->fetch_valid_thumbs();
  85. $this->parse_file_entries();
  86. if ($this->enable['pagination'] == TRUE)
  87. {
  88. $this->add_pagination_data();
  89. }
  90. return $this->return_data;
  91. }
  92. // ------------------------------------------------------------------------
  93. /**
  94. * Build SQL Query
  95. */
  96. function build_sql_query($qstring = '')
  97. {
  98. $file_id = '';
  99. $cat_id = '';
  100. $dynamic = TRUE;
  101. // If dynamic is off, we'll override all dynamically set variables
  102. if ($this->EE->TMPL->fetch_param('dynamic') == 'no')
  103. {
  104. $dynamic = FALSE;
  105. }
  106. $this->limit = ( ! is_numeric($this->EE->TMPL->fetch_param('limit'))) ? $this->limit : $this->EE->TMPL->fetch_param('limit');
  107. // Parse the URL query string
  108. $this->uristr = $this->EE->uri->uri_string;
  109. if ($qstring == '')
  110. {
  111. $qstring = $this->query_string;
  112. }
  113. $this->basepath = $this->EE->functions->create_url($this->uristr);
  114. if ($qstring != '')
  115. {
  116. if ($dynamic && is_numeric($qstring))
  117. {
  118. $file_id = $qstring;
  119. }
  120. else
  121. {
  122. // Man- this is way redundant. Maybe move some to url helper or some such??
  123. $this->EE->load->helper('segment');
  124. // Parse ID
  125. if ($dynamic)
  126. {
  127. $seg = parse_id($qstring);
  128. $qstring = $seg['qstring'];
  129. $file_id = $seg['entry_id'];
  130. }
  131. // Parse page number
  132. if ($dynamic OR $this->EE->TMPL->fetch_param('paginate'))
  133. {
  134. $seg = parse_page_number($qstring, $this->basepath, $this->uristr);
  135. $this->p_page = $seg['p_page'];
  136. $this->basepath = $seg['basepath'];
  137. $this->uristr = $seg['uristr'];
  138. $qstring = $seg['qstring'];
  139. $page_marker = ($this->p_page) ? TRUE : FALSE;
  140. }
  141. /** --------------------------------------
  142. /** Parse category indicator
  143. /** --------------------------------------*/
  144. // Text version of the category
  145. if ($qstring != '' AND $this->reserved_cat_segment != '' AND in_array($this->reserved_cat_segment, explode("/", $qstring)) AND $dynamic)
  146. {
  147. $qstring = preg_replace("/(.*?)\/".preg_quote($this->reserved_cat_segment)."\//i", '', '/'.$qstring);
  148. $this->EE->db->distinct();
  149. $this->EE->db->select('cat_group');
  150. $this->EE->db->where_in('site_id', $this->EE->TMPL->site_ids);
  151. $this->EE->functions->ar_andor_string($this->EE->TMPL->fetch_param('directory_id'), 'id');
  152. $query = $this->EE->db->get('upload_prefs');
  153. if ($query->num_rows() > 0)
  154. {
  155. $valid = 'y';
  156. $last = explode('|', $query->row('cat_group') );
  157. $valid_cats = array();
  158. foreach($query->result_array() as $row)
  159. {
  160. if ($this->EE->TMPL->fetch_param('relaxed_categories') == 'yes')
  161. {
  162. $valid_cats = array_merge($valid_cats, explode('|', $row['cat_group']));
  163. }
  164. else
  165. {
  166. $valid_cats = array_intersect($last, explode('|', $row['cat_group']));
  167. }
  168. $valid_cats = array_unique($valid_cats);
  169. if (count($valid_cats) == 0)
  170. {
  171. $valid = 'n';
  172. break;
  173. }
  174. }
  175. }
  176. else
  177. {
  178. $valid = 'n';
  179. }
  180. if ($valid == 'y')
  181. {
  182. // the category URL title should be the first segment left at this point in $qstring,
  183. // but because prior to this feature being added, category names were used in URLs,
  184. // and '/' is a valid character for category names. If they have not updated their
  185. // category url titles since updating to 1.6, their category URL title could still
  186. // contain a '/'. So we'll try to get the category the correct way first, and if
  187. // it fails, we'll try the whole $qstring
  188. // do this as separate commands to work around a PHP 5.0.x bug
  189. $arr = explode('/', $qstring);
  190. $cut_qstring = array_shift($arr);
  191. unset($arr);
  192. $result = $this->EE->db->select('cat_id')
  193. ->where('cat_url_title', $cut_qstring)
  194. ->where_in('group_id', $valid_cats)
  195. ->get('categories');
  196. if ($result->num_rows() == 1)
  197. {
  198. $qstring = str_replace($cut_qstring, 'C'.$result->row('cat_id') , $qstring);
  199. }
  200. else
  201. {
  202. $result = $this->EE->db->select('cat_id')
  203. ->where('cat_url_title', $qstring)
  204. ->where_in('group_id', $valid_cats)
  205. ->get('categories');
  206. if ($result->num_rows() == 1)
  207. {
  208. $qstring = 'C'.$result->row('cat_id') ;
  209. }
  210. }
  211. }
  212. }
  213. // Numeric version of the category
  214. if ($dynamic && preg_match("#(^|\/)C(\d+)#", $qstring, $match))
  215. {
  216. $this->cat_request = TRUE;
  217. $cat_id = $match[2];
  218. $qstring = trim_slashes(str_replace($match[0], '', $qstring));
  219. }
  220. // Remove "N"
  221. // The recent comments feature uses "N" as the URL indicator
  222. // It needs to be removed if present
  223. if ($dynamic)
  224. {
  225. $seg = parse_n($qstring, $this->uristr);
  226. $qstring = $seg['qstring'];
  227. $this->uristr = $seg['uristr'];
  228. }
  229. }
  230. }
  231. // If the "File ID" was hard-coded, use it instead of
  232. // using the dynamically set one above
  233. if ($this->EE->TMPL->fetch_param('file_id'))
  234. {
  235. $file_id = $this->EE->TMPL->fetch_param('file_id');
  236. }
  237. // Setup Orderby
  238. $allowed_sorts = array('date', 'upload_date', 'random');
  239. $order_by = $this->EE->TMPL->fetch_param('orderby', 'upload_date');
  240. $sort = $this->EE->TMPL->fetch_param('sort', 'desc');
  241. $random = ($order_by == 'random') ? TRUE : FALSE;
  242. $order_by = ($order_by == 'date' OR ! in_array($order_by, $allowed_sorts)) ? 'upload_date' : $order_by;
  243. // Need to add a short_name to the file upload prefs to be consistent with gallery
  244. //$dir_ids = array();
  245. // Start the cache so we can use for pagination
  246. $this->EE->db->start_cache();
  247. // Fetch the file ids
  248. if ($this->EE->TMPL->fetch_param('category') OR $this->EE->TMPL->fetch_param('category_group') OR $cat_id != '')
  249. {
  250. $this->EE->db->distinct();
  251. // We use 'LEFT' JOIN when there is a 'not' so that we get
  252. // entries that are not assigned to a category.
  253. if ((substr($this->EE->TMPL->fetch_param('category_group'), 0, 3) == 'not' OR substr($this->EE->TMPL->fetch_param('category'), 0, 3) == 'not') && $this->EE->TMPL->fetch_param('uncategorized_entries') !== 'n')
  254. {
  255. $this->EE->db->join('file_categories', 'exp_files.file_id = exp_file_categories.file_id', 'LEFT');
  256. $this->EE->db->join('categories', 'exp_file_categories.cat_id = exp_categories.cat_id', 'LEFT');
  257. }
  258. else
  259. {
  260. $this->EE->db->join('file_categories', 'exp_files.file_id = exp_file_categories.file_id', 'INNER');
  261. $this->EE->db->join('categories', 'exp_file_categories.cat_id = exp_categories.cat_id', 'INNER');
  262. }
  263. }
  264. $this->EE->db->select('exp_files.file_id');
  265. $this->EE->db->from('files');
  266. if ($file_id != '')
  267. {
  268. $this->EE->functions->ar_andor_string($file_id, 'exp_files.file_id').' ';
  269. }
  270. // If directory_id is set in template
  271. if (($directory_ids = $this->EE->TMPL->fetch_param('directory_id')) != FALSE)
  272. {
  273. $this->EE->functions->ar_andor_string($directory_ids, 'upload_location_id');
  274. }
  275. // If no directory_id is set, restrict files to current site
  276. else
  277. {
  278. $this->EE->db->where_in('exp_files.site_id', $this->EE->TMPL->site_ids);
  279. }
  280. // Limit query by category
  281. if ($this->EE->TMPL->fetch_param('category'))
  282. {
  283. // Doing a simplified version for now- no & allowed ??
  284. if (stristr($this->EE->TMPL->fetch_param('category'), '&'))
  285. {
  286. }
  287. else
  288. {
  289. if (substr($this->EE->TMPL->fetch_param('category'), 0, 3) == 'not' && $this->EE->TMPL->fetch_param('uncategorized_entries') !== 'n')
  290. {
  291. // $str, $field, $prefix = '', $null=FALSE
  292. $this->EE->functions->ar_andor_string($this->EE->TMPL->fetch_param('category'), 'exp_categories.cat_id', '', TRUE);
  293. }
  294. else
  295. {
  296. $this->EE->functions->ar_andor_string($this->EE->TMPL->fetch_param('category'), 'exp_categories.cat_id');
  297. }
  298. }
  299. }
  300. if ($this->EE->TMPL->fetch_param('category_group'))
  301. {
  302. if (substr($this->EE->TMPL->fetch_param('category_group'), 0, 3) == 'not' && $this->EE->TMPL->fetch_param('uncategorized_entries') !== 'n')
  303. {
  304. $this->EE->functions->ar_andor_string($this->EE->TMPL->fetch_param('category_group'), 'exp_categories.group_id', '', TRUE);
  305. }
  306. else
  307. {
  308. $this->EE->functions->ar_andor_string($this->EE->TMPL->fetch_param('category_group'), 'exp_categories.group_id');
  309. }
  310. }
  311. if ($this->EE->TMPL->fetch_param('category') === FALSE && $this->EE->TMPL->fetch_param('category_group') === FALSE)
  312. {
  313. if ($cat_id != '' AND $dynamic)
  314. {
  315. $this->EE->db->where('exp_categories.cat_id', $cat_id);
  316. }
  317. }
  318. $this->EE->db->stop_cache();
  319. if ($this->paginate == TRUE)
  320. {
  321. //$this->EE->db->select('exp_files.file_id');
  322. $total = $this->EE->db->count_all_results();
  323. $this->absolute_results = $total;
  324. $this->create_pagination($total);
  325. }
  326. // We do the select down here as it could have been lost from cache anyway
  327. if ($this->paginate == TRUE)
  328. {
  329. $this->EE->db->select('exp_files.file_id');
  330. }
  331. // Add sorting
  332. $this_sort = ($random) ? 'random' : strtolower($sort);
  333. $this->EE->db->order_by($order_by, $this_sort);
  334. // Add the limit
  335. $this_page = ($this->p_page == '' OR ($this->limit > 1 AND $this->p_page == 1)) ? 0 : $this->p_page;
  336. $this->EE->db->limit($this->limit, $this_page);
  337. //Fetch the file_id numbers
  338. $query = $this->EE->db->get();
  339. $this->EE->db->flush_cache();
  340. if ($query->num_rows() == 0)
  341. {
  342. $this->sql = '';
  343. return;
  344. }
  345. foreach ($query->result() as $row)
  346. {
  347. $file_ids[] = $row->file_id;
  348. }
  349. // Build the full SQL query
  350. $this->EE->db->select('*');
  351. $this->EE->db->from('files');
  352. $this->EE->db->join('upload_prefs', 'upload_prefs.id = files.upload_location_id', 'LEFT');
  353. $this->EE->db->where_in('file_id', $file_ids);
  354. $this->EE->db->order_by($order_by, $this_sort);
  355. $this->sql = $this->EE->db->get();
  356. }
  357. // ------------------------------------------------------------------------
  358. /**
  359. * Fetch pagination data
  360. */
  361. function fetch_pagination_data()
  362. {
  363. if (strpos($this->EE->TMPL->tagdata, LD.'paginate'.RD) === FALSE)
  364. {
  365. return;
  366. }
  367. if (preg_match("/".LD."paginate".RD."(.+?)".LD.'\/'."paginate".RD."/s", $this->EE->TMPL->tagdata, $match))
  368. {
  369. $this->paginate = TRUE;
  370. $this->paginate_data = $match[1];
  371. $this->EE->TMPL->tagdata = preg_replace("/".LD."paginate".RD.".+?".LD.'\/'."paginate".RD."/s", "", $this->EE->TMPL->tagdata);
  372. }
  373. }
  374. // ------------------------------------------------------------------------
  375. /**
  376. * Create Pagination
  377. */
  378. function create_pagination($count = 0)
  379. {
  380. if ($this->paginate == TRUE)
  381. {
  382. /* --------------------------------------
  383. /* For subdomain's or domains using $template_group and $template
  384. /* in path.php, the pagination for the main index page requires
  385. /* that the template group and template are specified.
  386. /* --------------------------------------*/
  387. if (($this->EE->uri->uri_string == '' OR $this->EE->uri->uri_string == '/') && $this->EE->config->item('template_group') != '' && $this->EE->config->item('template') != '')
  388. {
  389. $this->basepath = $this->EE->functions->create_url($this->EE->config->slash_item('template_group').'/'.$this->EE->config->item('template'));
  390. }
  391. if ($this->basepath == '')
  392. {
  393. $this->basepath = $this->EE->functions->create_url($this->EE->uri->uri_string);
  394. if (preg_match("#^P(\d+)|/P(\d+)#", $this->query_string, $match))
  395. {
  396. $this->p_page = (isset($match[2])) ? $match[2] : $match[1];
  397. $this->basepath = $this->EE->functions->remove_double_slashes(str_replace($match[0], '', $this->basepath));
  398. }
  399. }
  400. // Standard pagination - base values
  401. if ($count == 0)
  402. {
  403. $this->sql = '';
  404. return;
  405. }
  406. $this->total_rows = $count;
  407. $this->p_page = ($this->p_page == '' OR ($this->limit > 1 AND $this->p_page == 1)) ? 0 : $this->p_page;
  408. if ($this->p_page > $this->total_rows)
  409. {
  410. $this->p_page = 0;
  411. }
  412. $this->current_page = floor(($this->p_page / $this->limit) + 1);
  413. $this->total_pages = intval(floor($this->total_rows / $this->limit));
  414. // Create the pagination
  415. if ($this->total_rows > 0 && $this->limit > 0)
  416. {
  417. if ($this->total_rows % $this->limit)
  418. {
  419. $this->total_pages++;
  420. }
  421. }
  422. if ($this->total_rows > $this->limit)
  423. {
  424. $this->EE->load->library('pagination');
  425. if (strpos($this->basepath, SELF) === FALSE && $this->EE->config->item('site_index') != '')
  426. {
  427. $this->basepath .= SELF;
  428. }
  429. if ($this->EE->TMPL->fetch_param('paginate_base'))
  430. {
  431. // Load the string helper
  432. $this->EE->load->helper('string');
  433. $this->basepath = $this->EE->functions->create_url(trim_slashes($this->EE->TMPL->fetch_param('paginate_base')));
  434. }
  435. $config['base_url'] = $this->basepath;
  436. $config['prefix'] = 'P';
  437. $config['total_rows'] = $this->total_rows;
  438. $config['per_page'] = $this->limit;
  439. $config['cur_page'] = $this->p_page;
  440. $config['first_link'] = $this->EE->lang->line('pag_first_link');
  441. $config['last_link'] = $this->EE->lang->line('pag_last_link');
  442. // Allows $config['cur_page'] to override
  443. $config['uri_segment'] = 0;
  444. $this->EE->pagination->initialize($config);
  445. $this->pagination_links = $this->EE->pagination->create_links();
  446. if ((($this->total_pages * $this->limit) - $this->limit) > $this->p_page)
  447. {
  448. $this->page_next = reduce_double_slashes($this->basepath.'/P'.($this->p_page + $this->limit));
  449. }
  450. if (($this->p_page - $this->limit ) >= 0)
  451. {
  452. $this->page_previous = reduce_double_slashes($this->basepath.'/P'.($this->p_page - $this->limit));
  453. }
  454. }
  455. else
  456. {
  457. $this->p_page = '';
  458. }
  459. }
  460. }
  461. // ------------------------------------------------------------------------
  462. /**
  463. * Add pagination data to result
  464. */
  465. function add_pagination_data()
  466. {
  467. if ($this->pagination_links == '')
  468. {
  469. return;
  470. }
  471. if ($this->paginate == TRUE)
  472. {
  473. $this->paginate_data = str_replace(LD.'current_page'.RD, $this->current_page, $this->paginate_data);
  474. $this->paginate_data = str_replace(LD.'total_pages'.RD, $this->total_pages, $this->paginate_data);
  475. $this->paginate_data = str_replace(LD.'pagination_links'.RD, $this->pagination_links, $this->paginate_data);
  476. if (preg_match_all("/".LD."if previous_page".RD."(.+?)".LD.'\/'."if".RD."/s", $this->paginate_data, $matches))
  477. {
  478. if ($this->page_previous == '')
  479. {
  480. $this->paginate_data = preg_replace("/".LD."if previous_page".RD.".+?".LD.'\/'."if".RD."/s", '', $this->paginate_data);
  481. }
  482. else
  483. {
  484. foreach($matches[1] as $count => $match)
  485. {
  486. $match = preg_replace("/".LD.'path.*?'.RD."/", $this->page_previous, $match);
  487. $match = preg_replace("/".LD.'auto_path'.RD."/", $this->page_previous, $match);
  488. $this->paginate_data = str_replace($matches[0][$count], $match, $this->paginate_data);
  489. }
  490. }
  491. }
  492. if (preg_match_all("/".LD."if next_page".RD."(.+?)".LD.'\/'."if".RD."/s", $this->paginate_data, $matches))
  493. {
  494. if ($this->page_next == '')
  495. {
  496. $this->paginate_data = preg_replace("/".LD."if next_page".RD.".+?".LD.'\/'."if".RD."/s", '', $this->paginate_data);
  497. }
  498. else
  499. {
  500. foreach ($matches[1] as $count => $match)
  501. {
  502. $match = preg_replace("/".LD.'path.*?'.RD."/", $this->page_next, $match);
  503. $match = preg_replace("/".LD.'auto_path'.RD."/", $this->page_next, $match);
  504. $this->paginate_data = str_replace($matches[0][$count], $match, $this->paginate_data);
  505. }
  506. }
  507. }
  508. $this->paginate_data = $this->EE->functions->prep_conditionals($this->paginate_data, array('total_pages' => $this->total_pages));
  509. $position = ( ! $this->EE->TMPL->fetch_param('paginate')) ? '' : $this->EE->TMPL->fetch_param('paginate');
  510. switch ($position)
  511. {
  512. case "top" : $this->return_data = $this->paginate_data.$this->return_data;
  513. break;
  514. case "both" : $this->return_data = $this->paginate_data.$this->return_data.$this->paginate_data;
  515. break;
  516. default : $this->return_data .= $this->paginate_data;
  517. break;
  518. }
  519. }
  520. }
  521. // ------------------------------------------------------------------------
  522. /**
  523. * Fetch categories
  524. */
  525. function fetch_categories()
  526. {
  527. $this->EE->db->select('field_id, field_name')
  528. ->from('category_fields')
  529. ->where_in('site_id', $this->EE->TMPL->site_ids);
  530. $query = $this->EE->db->get();
  531. if ($query->num_rows() > 0)
  532. {
  533. foreach ($query->result_array() as $row)
  534. {
  535. $this->catfields[] = array('field_name' => $row['field_name'], 'field_id' => $row['field_id']);
  536. }
  537. }
  538. $categories = array();
  539. foreach ($this->query->result_array() as $row)
  540. {
  541. $categories[] = $row['file_id'];
  542. }
  543. $sql = $this->EE->db->select('c.cat_name, c.cat_url_title, c.cat_id, c.cat_image, c.cat_description,
  544. c.parent_id, p.cat_id, p.file_id, c.group_id, cg.field_html_formatting, fd.*')
  545. ->from('exp_categories AS c, exp_file_categories AS p')
  546. ->join('category_field_data AS fd', 'fd.cat_id = c.cat_id', 'LEFT')
  547. ->join('category_groups AS cg', 'cg.group_id = c.group_id', 'LEFT')
  548. ->where('c.cat_id = p.cat_id')
  549. ->where_in('file_id', $categories)
  550. ->order_by('c.group_id, c.parent_id, c.cat_order');
  551. $query = $this->EE->db->get();
  552. if ($query->num_rows() == 0)
  553. {
  554. return;
  555. }
  556. foreach ($categories as $val)
  557. {
  558. $this->temp_array = array();
  559. $this->cat_array = array();
  560. $parents = array();
  561. foreach ($query->result_array() as $row)
  562. {
  563. if ($val == $row['file_id'])
  564. {
  565. $this->temp_array[$row['cat_id']] = array('category_id' => $row['cat_id'], 'parent_id' => $row['parent_id'], 'category_name' => $row['cat_name'], 'category_image' => $row['cat_image'], 'category_description' => $row['cat_description'], 'category_group_id' => $row['group_id'], 'category_url_title' => $row['cat_url_title']);
  566. // Add in the path variable
  567. $this->temp_array[$row['cat_id']]['path'] = ($this->use_category_names == TRUE)
  568. ? array($this->reserved_cat_segment.'/'.$row['cat_url_title'], array('path_variable' => TRUE)) :
  569. array('/C'.$row['cat_id'], array('path_variable' => TRUE));
  570. foreach ($row as $k => $v)
  571. {
  572. if (strpos($k, 'field') !== FALSE)
  573. {
  574. $this->temp_array[$row['cat_id']][$k] = $v;
  575. }
  576. }
  577. if ($row['parent_id'] > 0 && ! isset($this->temp_array[$row['parent_id']])) $parents[$row['parent_id']] = '';
  578. unset($parents[$row['cat_id']]);
  579. }
  580. }
  581. if (count($this->temp_array) == 0)
  582. {
  583. $temp = FALSE;
  584. }
  585. else
  586. {
  587. foreach($this->temp_array as $k => $v)
  588. {
  589. if (isset($parents[$v['parent_id']])) $v['parent_id'] = 0;
  590. if (0 == $v['parent_id'])
  591. {
  592. $this->cat_array[] = $v;
  593. $this->process_subcategories($k);
  594. }
  595. }
  596. }
  597. $this->categories[$val] = $this->cat_array;
  598. }
  599. unset($this->temp_array);
  600. unset($this->cat_array);
  601. }
  602. // ------------------------------------------------------------------------
  603. /**
  604. * Process Subcategories
  605. */
  606. function process_subcategories($parent_id)
  607. {
  608. foreach($this->temp_array as $key => $val)
  609. {
  610. if ($parent_id == $val['parent_id'])
  611. {
  612. $this->cat_array[] = $val;
  613. $this->process_subcategories($key);
  614. }
  615. }
  616. }
  617. // ------------------------------------------------------------------------
  618. /**
  619. * Fetch Valid Thumbs
  620. */
  621. function fetch_valid_thumbs()
  622. {
  623. $this->EE->db->select('upload_location_id, short_name');
  624. $this->EE->db->from('upload_prefs');
  625. $this->EE->db->join('file_dimensions', 'upload_prefs.id = file_dimensions.upload_location_id');
  626. $this->EE->db->where_in('upload_prefs.site_id', $this->EE->TMPL->site_ids);
  627. if (($directory_ids = $this->EE->TMPL->fetch_param('directory_id')) != FALSE)
  628. {
  629. $this->EE->functions->ar_andor_string($directory_ids, 'upload_location_id');
  630. }
  631. $sql = $this->EE->db->get();
  632. if ($sql->num_rows() == 0)
  633. {
  634. return;
  635. }
  636. foreach ($sql->result_array() as $row)
  637. {
  638. $this->valid_thumbs[] = array('dir' => $row['upload_location_id'], 'name' => $row['short_name']);
  639. }
  640. }
  641. // ------------------------------------------------------------------------
  642. /**
  643. * Parse file entries
  644. */
  645. function parse_file_entries()
  646. {
  647. // Fetch the "category chunk"
  648. // We'll grab the category data now to avoid processing cycles in the foreach loop below
  649. $cat_chunk = array();
  650. if (strpos($this->EE->TMPL->tagdata, LD.'/categories'.RD) !== FALSE)
  651. {
  652. if (preg_match_all("/".LD."categories(.*?)".RD."(.*?)".LD.'\/'.'categories'.RD."/s", $this->EE->TMPL->tagdata, $matches))
  653. {
  654. for ($j = 0; $j < count($matches[0]); $j++)
  655. {
  656. $cat_chunk[] = array($matches[2][$j], $this->EE->functions->assign_parameters($matches[1][$j]), $matches[0][$j]);
  657. }
  658. }
  659. }
  660. // dates still need localizing!
  661. $parse_data = array();
  662. $default_variables = array('description', 'caption', 'title');
  663. $this->EE->load->model('file_upload_preferences_model');
  664. $upload_prefs = $this->EE->file_upload_preferences_model->get_file_upload_preferences(1, NULL, TRUE);
  665. foreach ($this->query->result_array() as $count => $row)
  666. {
  667. $row_prefs = $upload_prefs[$row['upload_location_id']];
  668. $row['absolute_count'] = $this->p_page + $count + 1;
  669. // More Variables, Mostly for Conditionals
  670. $row['logged_in'] = ($this->EE->session->userdata('member_id') == 0) ? 'FALSE' : 'TRUE';
  671. $row['logged_out'] = ($this->EE->session->userdata('member_id') != 0) ? 'FALSE' : 'TRUE';
  672. $row['entry_date'] = $row['upload_date'];
  673. $row['edit_date'] = $row['modified_date'];
  674. $row['directory_id'] = $row['id'];
  675. $row['directory_title'] = $row['name'];
  676. $row['entry_id'] = $row['file_id'];
  677. $row['file_url'] = rtrim($row_prefs['url'], '/').'/'.$row['file_name'];
  678. $row['filename'] = $row['file_name'];
  679. $row['viewable_image'] = $this->is_viewable_image($row['file_name']);
  680. // Add in the path variable
  681. $row['id_path'] = array('/'.$row['file_id'], array('path_variable' => TRUE));
  682. // typography on title?
  683. $row['title'] = $this->EE->typography->format_characters($row['title']);
  684. // typography on caption
  685. $this->EE->typography->parse_type($row['description'],
  686. array(
  687. 'text_format' => 'xhtml',
  688. 'html_format' => 'safe',
  689. 'auto_links' => 'y',
  690. 'allow_img_url' => 'y'
  691. )
  692. );
  693. // Caption is now called Description, but keep supporting
  694. // caption so it doesn't break on upgrade
  695. $row['caption'] = $row['description'];
  696. // Get File Size/H/W data
  697. $size_data = $this->get_file_sizes(reduce_double_slashes($row_prefs['server_path'].'/'.$row['filename']));
  698. foreach($size_data as $k => $v)
  699. {
  700. $row[$k] = $v;
  701. }
  702. // Thumbnail data
  703. foreach ($this->valid_thumbs as $data)
  704. {
  705. if ($row['viewable_image'] && $row['id'] == $data['dir'])
  706. {
  707. $size_data = array();
  708. $row[$data['name'].'_file_url'] = rtrim($row_prefs['url'], '/').'/_'.$data['name'].'/'.$row['file_name'];
  709. $size_data = $this->get_file_sizes(reduce_double_slashes($row_prefs['server_path'].'/_'.$data['name'].'/'.$row['file_name']));
  710. foreach($size_data as $k => $v)
  711. {
  712. $row[$data['name'].'_'.$k] = $v;
  713. }
  714. }
  715. else
  716. {
  717. $row[$data['name'].'_height'] = '';
  718. $row[$data['name'].'_width'] = '';
  719. $row[$data['name'].'_size'] = '';
  720. $row[$data['name'].'_file_url'] = '';
  721. }
  722. }
  723. // Category variables
  724. $row['categories'] = (isset($this->categories[$row['file_id']])) ? $this->categories[$row['file_id']] : array();
  725. $parse_data[] = $row;
  726. }
  727. $this->return_data = $this->EE->TMPL->parse_variables( $this->EE->TMPL->tagdata, $parse_data);
  728. }
  729. function is_viewable_image($file)
  730. {
  731. $viewable_image = array('bmp','gif','jpeg','jpg','jpe','png');
  732. $ext = strtolower(substr(strrchr($file, '.'), 1));
  733. $viewable = (in_array($ext, $viewable_image)) ? TRUE : FALSE;
  734. return $viewable;
  735. }
  736. // --------------------------------------------------------------------
  737. /**
  738. * Gets File Metadata- may move to db
  739. *
  740. * @param string $file_path The full path to the file to check
  741. * @return array
  742. */
  743. function get_file_sizes($file_path)
  744. {
  745. $this->EE->load->helper('file');
  746. $filedata = array('height' => '', 'width' => '');
  747. $filedata['is_image'] = $this->is_viewable_image($file_path);
  748. if ($filedata['is_image'] && function_exists('getimagesize'))
  749. {
  750. $D = @getimagesize($file_path);
  751. $filedata['height'] = $D['1'];
  752. $filedata['width'] = $D['0'];
  753. }
  754. $s = get_file_info($file_path, array('size'));
  755. $filedata['size'] = ($s) ? $s['size'] : FALSE;
  756. return $filedata;
  757. }
  758. // ------------------------------------------------------------------------
  759. /**
  760. * Fetch Disable Parameter
  761. */
  762. function _fetch_disable_param()
  763. {
  764. $this->enable = array(
  765. 'categories' => TRUE,
  766. 'category_fields' => TRUE,
  767. 'member_data' => TRUE,
  768. 'pagination' => TRUE,
  769. );
  770. if ($disable = $this->EE->TMPL->fetch_param('disable'))
  771. {
  772. if (strpos($disable, '|') !== FALSE)
  773. {
  774. foreach (explode("|", $disable) as $val)
  775. {
  776. if (isset($this->enable[$val]))
  777. {
  778. $this->enable[$val] = FALSE;
  779. }
  780. }
  781. }
  782. elseif (isset($this->enable[$disable]))
  783. {
  784. $this->enable[$disable] = FALSE;
  785. }
  786. }
  787. }
  788. }
  789. // END CLASS
  790. /* End of file mod.file.php */
  791. /* Location: ./system/expressionengine/modules/file/mod.file.php */