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

/forum/attach_mod/includes/functions_attach.php

https://code.google.com/p/torrentpier/
PHP | 901 lines | 687 code | 126 blank | 88 comment | 128 complexity | 91adf9bfc83787c259c3815295484989 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. *
  4. * @package attachment_mod
  5. * @version $Id: functions_attach.php,v 1.4 2005/11/17 17:41:36 acydburn Exp $
  6. * @copyright (c) 2002 Meik Sievertsen
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * All Attachment Functions needed everywhere
  12. */
  13. /**
  14. * html_entity_decode replacement (from php manual)
  15. */
  16. if (!function_exists('html_entity_decode'))
  17. {
  18. function html_entity_decode($given_html, $quote_style = ENT_QUOTES)
  19. {
  20. $trans_table = array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style));
  21. $trans_table['&#39;'] = "'";
  22. return (strtr($given_html, $trans_table));
  23. }
  24. }
  25. /**
  26. * A simple dectobase64 function
  27. */
  28. function base64_pack($number)
  29. {
  30. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
  31. $base = strlen($chars);
  32. if ($number > 4096)
  33. {
  34. return;
  35. }
  36. else if ($number < $base)
  37. {
  38. return $chars[$number];
  39. }
  40. $hexval = '';
  41. while ($number > 0)
  42. {
  43. $remainder = $number%$base;
  44. if ($remainder < $base)
  45. {
  46. $hexval = $chars[$remainder] . $hexval;
  47. }
  48. $number = floor($number/$base);
  49. }
  50. return $hexval;
  51. }
  52. /**
  53. * base64todec function
  54. */
  55. function base64_unpack($string)
  56. {
  57. $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+-';
  58. $base = strlen($chars);
  59. $length = strlen($string);
  60. $number = 0;
  61. for($i = 1; $i <= $length; $i++)
  62. {
  63. $pos = $length - $i;
  64. $operand = strpos($chars, substr($string,$pos,1));
  65. $exponent = pow($base, $i-1);
  66. $decValue = $operand * $exponent;
  67. $number += $decValue;
  68. }
  69. return $number;
  70. }
  71. /**
  72. * Per Forum based Extension Group Permissions (Encode Number) -> Theoretically up to 158 Forums saveable. :)
  73. * We are using a base of 64, but splitting it to one-char and two-char numbers. :)
  74. */
  75. function auth_pack($auth_array)
  76. {
  77. $one_char_encoding = '#';
  78. $two_char_encoding = '.';
  79. $one_char = $two_char = false;
  80. $auth_cache = '';
  81. for ($i = 0; $i < sizeof($auth_array); $i++)
  82. {
  83. $val = base64_pack(intval($auth_array[$i]));
  84. if (strlen($val) == 1 && !$one_char)
  85. {
  86. $auth_cache .= $one_char_encoding;
  87. $one_char = true;
  88. }
  89. else if (strlen($val) == 2 && !$two_char)
  90. {
  91. $auth_cache .= $two_char_encoding;
  92. $two_char = true;
  93. }
  94. $auth_cache .= $val;
  95. }
  96. return $auth_cache;
  97. }
  98. /**
  99. * Reverse the auth_pack process
  100. */
  101. function auth_unpack($auth_cache)
  102. {
  103. $one_char_encoding = '#';
  104. $two_char_encoding = '.';
  105. $auth = array();
  106. $auth_len = 1;
  107. for ($pos = 0; $pos < strlen($auth_cache); $pos += $auth_len)
  108. {
  109. $forum_auth = substr($auth_cache, $pos, 1);
  110. if ($forum_auth == $one_char_encoding)
  111. {
  112. $auth_len = 1;
  113. continue;
  114. }
  115. else if ($forum_auth == $two_char_encoding)
  116. {
  117. $auth_len = 2;
  118. $pos--;
  119. continue;
  120. }
  121. $forum_auth = substr($auth_cache, $pos, $auth_len);
  122. $forum_id = base64_unpack($forum_auth);
  123. $auth[] = intval($forum_id);
  124. }
  125. return $auth;
  126. }
  127. /**
  128. * Used for determining if Forum ID is authed, please use this Function on all Posting Screens
  129. */
  130. function is_forum_authed($auth_cache, $check_forum_id)
  131. {
  132. $one_char_encoding = '#';
  133. $two_char_encoding = '.';
  134. if (trim($auth_cache) == '')
  135. {
  136. return true;
  137. }
  138. $auth = array();
  139. $auth_len = 1;
  140. for ($pos = 0; $pos < strlen($auth_cache); $pos+=$auth_len)
  141. {
  142. $forum_auth = substr($auth_cache, $pos, 1);
  143. if ($forum_auth == $one_char_encoding)
  144. {
  145. $auth_len = 1;
  146. continue;
  147. }
  148. else if ($forum_auth == $two_char_encoding)
  149. {
  150. $auth_len = 2;
  151. $pos--;
  152. continue;
  153. }
  154. $forum_auth = substr($auth_cache, $pos, $auth_len);
  155. $forum_id = (int) base64_unpack($forum_auth);
  156. if ($forum_id == $check_forum_id)
  157. {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * Init FTP Session
  165. */
  166. function attach_init_ftp($mode = false)
  167. {
  168. global $lang, $attach_config;
  169. $server = (trim($attach_config['ftp_server']) == '') ? 'localhost' : trim($attach_config['ftp_server']);
  170. $ftp_path = ($mode == MODE_THUMBNAIL) ? trim($attach_config['ftp_path']) . '/' . THUMB_DIR : trim($attach_config['ftp_path']);
  171. $conn_id = @ftp_connect($server);
  172. if (!$conn_id)
  173. {
  174. message_die(GENERAL_ERROR, sprintf($lang['FTP_ERROR_CONNECT'], $server));
  175. }
  176. $login_result = @ftp_login($conn_id, $attach_config['ftp_user'], $attach_config['ftp_pass']);
  177. if (!$login_result)
  178. {
  179. message_die(GENERAL_ERROR, sprintf($lang['FTP_ERROR_LOGIN'], $attach_config['ftp_user']));
  180. }
  181. if (!@ftp_pasv($conn_id, intval($attach_config['ftp_pasv_mode'])))
  182. {
  183. message_die(GENERAL_ERROR, $lang['FTP_ERROR_PASV_MODE']);
  184. }
  185. $result = @ftp_chdir($conn_id, $ftp_path);
  186. if (!$result)
  187. {
  188. message_die(GENERAL_ERROR, sprintf($lang['FTP_ERROR_PATH'], $ftp_path));
  189. }
  190. return $conn_id;
  191. }
  192. /**
  193. * Deletes an Attachment
  194. */
  195. function unlink_attach($filename, $mode = false)
  196. {
  197. global $upload_dir, $attach_config, $lang;
  198. $filename = basename($filename);
  199. if (!intval($attach_config['allow_ftp_upload']))
  200. {
  201. if ($mode == MODE_THUMBNAIL)
  202. {
  203. $filename = $upload_dir . '/' . THUMB_DIR . '/t_' . $filename;
  204. }
  205. else
  206. {
  207. $filename = $upload_dir . '/' . $filename;
  208. }
  209. $deleted = @unlink($filename);
  210. }
  211. else
  212. {
  213. $conn_id = attach_init_ftp($mode);
  214. if ($mode == MODE_THUMBNAIL)
  215. {
  216. $filename = 't_' . $filename;
  217. }
  218. $res = @ftp_delete($conn_id, $filename);
  219. if (!$res)
  220. {
  221. return $deleted;
  222. }
  223. @ftp_quit($conn_id);
  224. $deleted = true;
  225. }
  226. return $deleted;
  227. }
  228. /**
  229. * FTP File to Location
  230. */
  231. function ftp_file($source_file, $dest_file, $mimetype, $disable_error_mode = false)
  232. {
  233. global $attach_config, $lang, $error, $error_msg;
  234. $conn_id = attach_init_ftp();
  235. // Binary or Ascii ?
  236. $mode = FTP_BINARY;
  237. if (preg_match("/text/i", $mimetype) || preg_match("/html/i", $mimetype))
  238. {
  239. $mode = FTP_ASCII;
  240. }
  241. $res = @ftp_put($conn_id, $dest_file, $source_file, $mode);
  242. if (!$res && !$disable_error_mode)
  243. {
  244. $error = true;
  245. if (!empty($error_msg))
  246. {
  247. $error_msg .= '<br />';
  248. }
  249. $error_msg = sprintf($lang['FTP_ERROR_UPLOAD'], $attach_config['ftp_path']) . '<br />';
  250. @ftp_quit($conn_id);
  251. return false;
  252. }
  253. if (!$res)
  254. {
  255. return false;
  256. }
  257. @ftp_site($conn_id, 'CHMOD 0644 ' . $dest_file);
  258. @ftp_quit($conn_id);
  259. return true;
  260. }
  261. /**
  262. * Check if Attachment exist
  263. */
  264. function attachment_exists($filename)
  265. {
  266. global $upload_dir, $attach_config;
  267. $filename = basename($filename);
  268. if (!intval($attach_config['allow_ftp_upload']))
  269. {
  270. if (!@file_exists(@amod_realpath($upload_dir . '/' . $filename)))
  271. {
  272. return false;
  273. }
  274. else
  275. {
  276. return true;
  277. }
  278. }
  279. else
  280. {
  281. $found = false;
  282. $conn_id = attach_init_ftp();
  283. $file_listing = array();
  284. $file_listing = @ftp_rawlist($conn_id, $filename);
  285. for ($i = 0, $size = sizeof($file_listing); $i < $size; $i++)
  286. {
  287. if (preg_match("/([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)/", $file_listing[$i], $regs))
  288. {
  289. if ($regs[1] == 'd')
  290. {
  291. $dirinfo[0] = 1; // Directory == 1
  292. }
  293. $dirinfo[1] = $regs[2]; // Size
  294. $dirinfo[2] = $regs[3]; // Date
  295. $dirinfo[3] = $regs[4]; // Filename
  296. $dirinfo[4] = $regs[5]; // Time
  297. }
  298. if ($dirinfo[0] != 1 && $dirinfo[4] == $filename)
  299. {
  300. $found = true;
  301. }
  302. }
  303. @ftp_quit($conn_id);
  304. return $found;
  305. }
  306. }
  307. /**
  308. * Check if Thumbnail exist
  309. */
  310. function thumbnail_exists($filename)
  311. {
  312. global $upload_dir, $attach_config;
  313. $filename = basename($filename);
  314. if (!intval($attach_config['allow_ftp_upload']))
  315. {
  316. if (!@file_exists(@amod_realpath($upload_dir . '/' . THUMB_DIR . '/t_' . $filename)))
  317. {
  318. return false;
  319. }
  320. else
  321. {
  322. return true;
  323. }
  324. }
  325. else
  326. {
  327. $found = false;
  328. $conn_id = attach_init_ftp(MODE_THUMBNAIL);
  329. $file_listing = array();
  330. $filename = 't_' . $filename;
  331. $file_listing = @ftp_rawlist($conn_id, $filename);
  332. for ($i = 0, $size = sizeof($file_listing); $i < $size; $i++)
  333. {
  334. if (preg_match("/([-d])[rwxst-]{9}.* ([0-9]*) ([a-zA-Z]+[0-9: ]*[0-9]) ([0-9]{2}:[0-9]{2}) (.+)/", $file_listing[$i], $regs))
  335. {
  336. if ($regs[1] == 'd')
  337. {
  338. $dirinfo[0] = 1; // Directory == 1
  339. }
  340. $dirinfo[1] = $regs[2]; // Size
  341. $dirinfo[2] = $regs[3]; // Date
  342. $dirinfo[3] = $regs[4]; // Filename
  343. $dirinfo[4] = $regs[5]; // Time
  344. }
  345. if ($dirinfo[0] != 1 && $dirinfo[4] == $filename)
  346. {
  347. $found = true;
  348. }
  349. }
  350. @ftp_quit($conn_id);
  351. return $found;
  352. }
  353. }
  354. /**
  355. * Physical Filename stored already ?
  356. */
  357. function physical_filename_already_stored($filename)
  358. {
  359. global $db;
  360. if ($filename == '')
  361. {
  362. return false;
  363. }
  364. $filename = basename($filename);
  365. $sql = 'SELECT attach_id
  366. FROM ' . ATTACHMENTS_DESC_TABLE . "
  367. WHERE physical_filename = '" . attach_mod_sql_escape($filename) . "'
  368. LIMIT 1";
  369. if (!($result = $db->sql_query($sql)))
  370. {
  371. message_die(GENERAL_ERROR, 'Could not get attachment information for filename: ' . htmlspecialchars($filename), '', __LINE__, __FILE__, $sql);
  372. }
  373. $num_rows = $db->sql_numrows($result);
  374. $db->sql_freeresult($result);
  375. return ($num_rows == 0) ? false : true;
  376. }
  377. /**
  378. * get all attachments from a post (could be an post array too)
  379. */
  380. function get_attachments_from_post($post_id_array)
  381. {
  382. global $db, $attach_config;
  383. $attachments = array();
  384. if (!is_array($post_id_array))
  385. {
  386. if (empty($post_id_array))
  387. {
  388. return $attachments;
  389. }
  390. $post_id = intval($post_id_array);
  391. $post_id_array = array();
  392. $post_id_array[] = $post_id;
  393. }
  394. $post_id_array = implode(', ', array_map('intval', $post_id_array));
  395. if ($post_id_array == '')
  396. {
  397. return $attachments;
  398. }
  399. $display_order = (intval($attach_config['display_order']) == 0) ? 'DESC' : 'ASC';
  400. $sql = 'SELECT a.post_id, d.*
  401. FROM ' . ATTACHMENTS_TABLE . ' a, ' . ATTACHMENTS_DESC_TABLE . " d
  402. WHERE a.post_id IN ($post_id_array)
  403. AND a.attach_id = d.attach_id
  404. ORDER BY d.filetime $display_order";
  405. if ( !($result = $db->sql_query($sql)) )
  406. {
  407. message_die(GENERAL_ERROR, 'Could not get Attachment Informations for post number ' . $post_id_array, '', __LINE__, __FILE__, $sql);
  408. }
  409. $num_rows = $db->sql_numrows($result);
  410. $attachments = $db->sql_fetchrowset($result);
  411. $db->sql_freeresult($result);
  412. if ($num_rows == 0)
  413. {
  414. return array();
  415. }
  416. return $attachments;
  417. }
  418. /**
  419. * Count Filesize of Attachments in Database based on the attachment id
  420. */
  421. function get_total_attach_filesize($attach_ids)
  422. {
  423. global $db;
  424. if (!is_array($attach_ids) || !sizeof($attach_ids))
  425. {
  426. return 0;
  427. }
  428. $attach_ids = implode(', ', array_map('intval', $attach_ids));
  429. if (!$attach_ids)
  430. {
  431. return 0;
  432. }
  433. $sql = 'SELECT filesize
  434. FROM ' . ATTACHMENTS_DESC_TABLE . "
  435. WHERE attach_id IN ($attach_ids)";
  436. if ( !($result = $db->sql_query($sql)) )
  437. {
  438. message_die(GENERAL_ERROR, 'Could not query Total Filesize', '', __LINE__, __FILE__, $sql);
  439. }
  440. $total_filesize = 0;
  441. while ($row = $db->sql_fetchrow($result))
  442. {
  443. $total_filesize += (int) $row['filesize'];
  444. }
  445. $db->sql_freeresult($result);
  446. return $total_filesize;
  447. }
  448. /**
  449. * Get allowed Extensions and their respective Values
  450. */
  451. function get_extension_informations()
  452. {
  453. return $GLOBALS['datastore']->get('attach_extensions');
  454. }
  455. //
  456. // Sync Topic
  457. //
  458. function attachment_sync_topic ($topics)
  459. {
  460. global $db;
  461. if (is_array($topics))
  462. {
  463. $topics = join(',', $topics);
  464. }
  465. $posts_without_attach = $topics_without_attach = array();
  466. // Check orphan post_attachment markers
  467. $sql = "SELECT p.post_id
  468. FROM ". POSTS_TABLE ." p
  469. LEFT JOIN ". ATTACHMENTS_TABLE ." a USING(post_id)
  470. WHERE p.topic_id IN($topics)
  471. AND p.post_attachment = 1
  472. AND a.post_id IS NULL";
  473. if ($rowset = $db->fetch_rowset($sql))
  474. {
  475. foreach ($rowset as $row)
  476. {
  477. $posts_without_attach[] = $row['post_id'];
  478. }
  479. if ($posts_sql = join(',', $posts_without_attach))
  480. {
  481. $db->query("UPDATE ". POSTS_TABLE ." SET post_attachment = 0 WHERE post_id IN($posts_sql)");
  482. }
  483. }
  484. // Update missing topic_attachment markers
  485. $db->query("
  486. UPDATE ". TOPICS_TABLE ." t, ". POSTS_TABLE ." p SET
  487. t.topic_attachment = 1
  488. WHERE p.topic_id IN($topics)
  489. AND p.post_attachment = 1
  490. AND p.topic_id = t.topic_id
  491. ");
  492. // Fix orphan topic_attachment markers
  493. $sql = "SELECT t.topic_id
  494. FROM ". POSTS_TABLE ." p, ". TOPICS_TABLE ." t
  495. WHERE t.topic_id = p.topic_id
  496. AND t.topic_id IN($topics)
  497. AND t.topic_attachment = 1
  498. GROUP BY p.topic_id
  499. HAVING SUM(p.post_attachment) = 0";
  500. if ($rowset = $db->fetch_rowset($sql))
  501. {
  502. foreach ($rowset as $row)
  503. {
  504. $topics_without_attach[] = $row['topic_id'];
  505. }
  506. if ($topics_sql = join(',', $topics_without_attach))
  507. {
  508. $db->query("UPDATE ". TOPICS_TABLE ." SET topic_attachment = 0 WHERE topic_id IN($topics_sql)");
  509. }
  510. }
  511. }
  512. /**
  513. * Get Extension
  514. */
  515. function get_extension($filename)
  516. {
  517. if (!stristr($filename, '.'))
  518. {
  519. return '';
  520. }
  521. $extension = strrchr(strtolower($filename), '.');
  522. $extension[0] = ' ';
  523. $extension = strtolower(trim($extension));
  524. if (is_array($extension))
  525. {
  526. return '';
  527. }
  528. else
  529. {
  530. return $extension;
  531. }
  532. }
  533. /**
  534. * Delete Extension
  535. */
  536. function delete_extension($filename)
  537. {
  538. return substr($filename, 0, strrpos(strtolower(trim($filename)), '.'));
  539. }
  540. /**
  541. * Check if a user is within Group
  542. */
  543. function user_in_group($user_id, $group_id)
  544. {
  545. global $db;
  546. $user_id = (int) $user_id;
  547. $group_id = (int) $group_id;
  548. if (!$user_id || !$group_id)
  549. {
  550. return false;
  551. }
  552. $sql = 'SELECT u.group_id
  553. FROM ' . USER_GROUP_TABLE . ' u, ' . GROUPS_TABLE . " g
  554. WHERE g.group_single_user = 0
  555. AND u.group_id = g.group_id
  556. AND u.user_id = $user_id
  557. AND g.group_id = $group_id
  558. LIMIT 1";
  559. if (!($result = $db->sql_query($sql)))
  560. {
  561. message_die(GENERAL_ERROR, 'Could not get User Group', '', __LINE__, __FILE__, $sql);
  562. }
  563. $num_rows = $db->sql_numrows($result);
  564. $db->sql_freeresult($result);
  565. if ($num_rows == 0)
  566. {
  567. return false;
  568. }
  569. return true;
  570. }
  571. /**
  572. * Realpath replacement for attachment mod
  573. */
  574. function amod_realpath($path)
  575. {
  576. return (function_exists('realpath')) ? realpath($path) : $path;
  577. }
  578. /**
  579. * _set_var
  580. *
  581. * Set variable, used by {@link get_var the get_var function}
  582. *
  583. * @private
  584. */
  585. function _set_var(&$result, $var, $type, $multibyte = false)
  586. {
  587. settype($var, $type);
  588. $result = $var;
  589. if ($type == 'string')
  590. {
  591. $result = trim(str_replace(array("\r\n", "\r", '\xFF'), array("\n", "\n", ' '), $result));
  592. // 2.0.x is doing addslashes on all variables
  593. $result = stripslashes($result);
  594. if ($multibyte)
  595. {
  596. $result = preg_replace('#&amp;(\#[0-9]+;)#', '&\1', $result);
  597. }
  598. }
  599. }
  600. /**
  601. * get_var
  602. *
  603. * Used to get passed variable
  604. */
  605. function get_var($var_name, $default, $multibyte = false)
  606. {
  607. $request_var = (isset($_POST[$var_name])) ? $_POST : $_GET;
  608. if (!isset($request_var[$var_name]) || (is_array($request_var[$var_name]) && !is_array($default)) || (is_array($default) && !is_array($request_var[$var_name])))
  609. {
  610. return (is_array($default)) ? array() : $default;
  611. }
  612. $var = $request_var[$var_name];
  613. if (!is_array($default))
  614. {
  615. $type = gettype($default);
  616. }
  617. else
  618. {
  619. list($key_type, $type) = each($default);
  620. $type = gettype($type);
  621. $key_type = gettype($key_type);
  622. }
  623. if (is_array($var))
  624. {
  625. $_var = $var;
  626. $var = array();
  627. foreach ($_var as $k => $v)
  628. {
  629. if (is_array($v))
  630. {
  631. foreach ($v as $_k => $_v)
  632. {
  633. _set_var($k, $k, $key_type);
  634. _set_var($_k, $_k, $key_type);
  635. _set_var($var[$k][$_k], $_v, $type, $multibyte);
  636. }
  637. }
  638. else
  639. {
  640. _set_var($k, $k, $key_type);
  641. _set_var($var[$k], $v, $type, $multibyte);
  642. }
  643. }
  644. }
  645. else
  646. {
  647. _set_var($var, $var, $type, $multibyte);
  648. }
  649. return $var;
  650. }
  651. /**
  652. * Escaping SQL
  653. */
  654. function attach_mod_sql_escape($text)
  655. {
  656. switch (SQL_LAYER)
  657. {
  658. case 'postgresql':
  659. return pg_escape_string($text);
  660. break;
  661. case 'mysql':
  662. case 'mysql4':
  663. if (function_exists('mysql_real_escape_string'))
  664. {
  665. GLOBAL $db;
  666. return $db->escape_string($text);
  667. }
  668. else
  669. {
  670. return str_replace("'", "''", str_replace('\\', '\\\\', $text));
  671. }
  672. break;
  673. default:
  674. return str_replace("'", "''", str_replace('\\', '\\\\', $text));
  675. break;
  676. }
  677. }
  678. /**
  679. * Build sql statement from array for insert/update/select statements
  680. *
  681. * Idea for this from Ikonboard
  682. * Possible query values: INSERT, INSERT_SELECT, MULTI_INSERT, UPDATE, SELECT
  683. */
  684. function attach_mod_sql_build_array($query, $assoc_ary = false)
  685. {
  686. if (!is_array($assoc_ary))
  687. {
  688. return false;
  689. }
  690. $fields = array();
  691. $values = array();
  692. if ($query == 'INSERT' || $query == 'INSERT_SELECT')
  693. {
  694. foreach ($assoc_ary as $key => $var)
  695. {
  696. $fields[] = $key;
  697. if (is_null($var))
  698. {
  699. $values[] = 'NULL';
  700. }
  701. else if (is_string($var))
  702. {
  703. $values[] = "'" . attach_mod_sql_escape($var) . "'";
  704. }
  705. else if (is_array($var) && is_string($var[0]))
  706. {
  707. $values[] = $var[0];
  708. }
  709. else
  710. {
  711. $values[] = (is_bool($var)) ? intval($var) : $var;
  712. }
  713. }
  714. $query = ($query == 'INSERT') ? ' (' . implode(', ', $fields) . ') VALUES (' . implode(', ', $values) . ')' : ' (' . implode(', ', $fields) . ') SELECT ' . implode(', ', $values) . ' ';
  715. }
  716. else if ($query == 'MULTI_INSERT')
  717. {
  718. $ary = array();
  719. foreach ($assoc_ary as $id => $sql_ary)
  720. {
  721. $values = array();
  722. foreach ($sql_ary as $key => $var)
  723. {
  724. if (is_null($var))
  725. {
  726. $values[] = 'NULL';
  727. }
  728. elseif (is_string($var))
  729. {
  730. $values[] = "'" . attach_mod_sql_escape($var) . "'";
  731. }
  732. else
  733. {
  734. $values[] = (is_bool($var)) ? intval($var) : $var;
  735. }
  736. }
  737. $ary[] = '(' . implode(', ', $values) . ')';
  738. }
  739. $query = ' (' . implode(', ', array_keys($assoc_ary[0])) . ') VALUES ' . implode(', ', $ary);
  740. }
  741. else if ($query == 'UPDATE' || $query == 'SELECT')
  742. {
  743. $values = array();
  744. foreach ($assoc_ary as $key => $var)
  745. {
  746. if (is_null($var))
  747. {
  748. $values[] = "$key = NULL";
  749. }
  750. elseif (is_string($var))
  751. {
  752. $values[] = "$key = '" . attach_mod_sql_escape($var) . "'";
  753. }
  754. else
  755. {
  756. $values[] = (is_bool($var)) ? "$key = " . intval($var) : "$key = $var";
  757. }
  758. }
  759. $query = implode(($query == 'UPDATE') ? ', ' : ' AND ', $values);
  760. }
  761. return $query;
  762. }