/**
 * @version     1.0a
 * @package     virtualCityMarket
 * @copyright   Copyright (C) 2012 Logotech S.A.. All rights reserved.
 * @license     GNU Affero General Public License version 3 or later; see LICENSE.txt
 * @author      Dimitrios Mitzias for Logotech S.A.
 */
function sqlQuery($sql, &$res, $log = true)
{
    global $MARKET_db_conn;
    // Connect to database
    if (!is_resource($MARKET_db_conn)) {
        $MARKET_db_conn = @mysqli_connect(MARKET_DB_HOST, MARKET_DB_USER, MARKET_DB_PASS, MARKET_DB_DATABASE) or MARKET_Base::raiseError(MARKET_ERROR_DIE, 'sql_connect(): Cannot connect to "' . MARKET_DB_HOST . '" SQL Server', __FILE__, __LINE__);
        @mysqli_select_db($MARKET_db_conn, MARKET_DB_DATABASE) or MARKET_Base::raiseError(MARKET_ERROR_DIE, 'sql_select_db(): Cannot select database "' . MARKET_DB_DATABASE . '"', __FILE__, __LINE__);
        if (defined('MARKET_DB_COLLATION')) {
            @mysqli_query($MARKET_db_conn, "SET NAMES '" . MARKET_DB_COLLATION . "'");
        } else {
            @mysqli_query($MARKET_db_conn, "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");
        }
    }
    if (DEBUG && $log) {
        $dbg =& MARKET_Base::getRef('Debug');
        $dbg->add('sql', $sql);
        $prf =& MARKET_Base::getRef('Profiler');
        $prf->startTimer('sqlQuery', $sql);
    }
    $res = @mysqli_query($MARKET_db_conn, $sql);
    if (DEBUG && $log) {
        $prf->stopTimer('sqlQuery');
    }
    if ($res) {
        if (preg_match('@^(SELECT|SHOW)(?! CREATE)@', $sql) && ($found = @mysqli_num_rows($res))) {
            if (DEBUG && $log) {
                $dbg->add('info', 'MySQL Results: ' . $found);
            }
            return $found;
        } else {
            if (preg_match('@^EXPLAIN@', $sql) && ($found = @mysqli_num_rows($res))) {
                return true;
            } else {
                if (preg_match('@^INSERT@', $sql)) {
                    $sql = "SELECT LAST_INSERT_ID()";
                    $res = @mysqli_query($MARKET_db_conn, $sql);
                    $row = @mysqli_fetch_row($res);
                    $insert_id = $row[0];
                    $insert_id = $insert_id ? $insert_id : -1;
                    if (DEBUG && $log) {
                        $dbg->add('info', 'MySQL Insert ID: ' . $insert_id);
                    }
                    return $insert_id;
                } else {
                    if (preg_match('@^(UPDATE|DELETE|REPLACE)@', $sql)) {
                        $affected_rows = @mysqli_affected_rows($MARKET_db_conn);
                        if (DEBUG && $log) {
                            $dbg->add('info', 'MySQL Affected Rows: ' . $affected_rows);
                        }
                        return $affected_rows;
                    } else {
                        if (preg_match('@^CREATE@', $sql)) {
                            if (DEBUG && $log) {
                                $dbg->add('info', 'MySQL Results: Table creation');
                            }
                            return true;
                        } else {
                            if (preg_match('@^SHOW CREATE@', $sql)) {
                                if (DEBUG && $log) {
                                    $dbg->add('info', 'MySQL Results: Table creation SQL');
                                }
                                return true;
                            } else {
                                if (DEBUG && $log) {
                                    $dbg->add('info', 'MySQL Results: Unknown');
                                }
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (DEBUG && $log) {
            $dbg->add('info', 'MySQL Error: ' . sqlError());
        }
    }
    return false;
}
 function createThumbnail($image, $size, $complete_tag = false, $alt = 'alt=""')
 {
     // Parse size parameter
     if (preg_match('@^(\\d+)x(\\d+)$@', $size, $matches)) {
         $width = $matches[1];
         $height = $matches[2];
     } else {
         if (preg_match('@^(\\d+)$@', $size)) {
             $width = $size;
             $height = $size;
         } else {
             MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Size "' . htmlspecialchars($size) . '" is not acceptable.', __FILE__, __LINE__);
             return MARKET_Filter::_defaultThumbnail();
         }
     }
     $type = strtolower(substr(strrchr($image, '.'), 1));
     $out = '/cache/' . dirname($image) . '/' . substr(basename($image), 0, strrpos(basename($image), '.')) . '.' . $width . 'x' . $height . '.' . $type;
     if (@is_file(MARKET_ROOT_DIR . '/' . $out) && @is_readable(MARKET_ROOT_DIR . '/' . $out)) {
         // Do nothing
     } else {
         // Create thumbnail
         // Requires GD
         if (extension_loaded('gd')) {
             $gd_info = gd_info();
             $in = MARKET_ROOT_DIR . '/' . $image;
             if (@is_file($in) && @is_readable($in)) {
                 if (!(list($w, $h) = getimagesize($in))) {
                     MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" is not supported', __FILE__, __LINE__);
                     return MARKET_Filter::_defaultThumbnail();
                 }
                 if ($type == 'jpeg') {
                     $type = 'jpg';
                 }
                 switch ($type) {
                     case 'bmp':
                         $img = imagecreatefromwbmp($in);
                         break;
                     case 'gif':
                         $img = imagecreatefromgif($in);
                         break;
                     case 'jpg':
                         $img = imagecreatefromjpeg($in);
                         break;
                     case 'png':
                         $img = imagecreatefrompng($in);
                         break;
                     default:
                         MARKET_Base::raiseError(MARKET_ERROR_RETURN, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" is not supported', __FILE__, __LINE__);
                         return MARKET_Filter::_defaultThumbnail();
                 }
                 // Resize and crop
                 $sratio = $w / $h;
                 $dratio = $width / $height;
                 if ($sratio > $dratio) {
                     $temp_width = (int) ($h * $dratio);
                     $temp_height = $h;
                     $x = (int) (($w - $temp_width) / 2);
                     $y = 0;
                 } else {
                     $temp_width = $w;
                     $temp_height = (int) ($w / $dratio);
                     $x = 0;
                     $y = (int) (($h - $temp_height) / 2);
                 }
                 $source_width = $temp_width;
                 $source_height = $temp_height;
                 $dst = imagecreatetruecolor($width, $height);
                 // Preserve transparency
                 if ($type == 'gif' || $type == 'png') {
                     imagecolortransparent($dst, imagecolorallocatealpha($dst, 0, 0, 0, 127));
                     imagealphablending($dst, false);
                     imagesavealpha($dst, true);
                 }
                 imagecopyresampled($dst, $img, 0, 0, $x, $y, $width, $height, $source_width, $source_height);
                 // Create dir
                 MARKET_Base::makeDir(dirname(MARKET_ROOT_DIR . $out));
                 switch ($type) {
                     case 'bmp':
                         imagewbmp($dst, MARKET_ROOT_DIR . $out);
                         break;
                     case 'gif':
                         imagegif($dst, MARKET_ROOT_DIR . $out);
                         break;
                     case 'jpg':
                         imagejpeg($dst, MARKET_ROOT_DIR . $out);
                         break;
                     case 'png':
                         imagepng($dst, MARKET_ROOT_DIR . $out);
                         break;
                 }
             } else {
                 MARKET_Base::raiseError(MARKET_ERROR_WARNING, __FUNCTION__ . '(): Image "' . htmlspecialchars($image) . '" not found or not readable', __FILE__, __LINE__);
                 return MARKET_Filter::_defaultThumbnail('', $width, $height, $complete_tag, $alt);
             }
         } else {
             MARKET_Base::raiseError(MARKET_ERROR_WARNING, __FUNCTION__ . '(): The GD extension is not loaded', __FILE__, __LINE__);
             return MARKET_Filter::_defaultThumbnail($image, $width, $height, $complete_tag, $alt);
         }
     }
     if ($complete_tag) {
         return '<img src="' . MARKET_WEB_DIR . $out . '" width="' . $width . '" height="' . $height . '" ' . $alt . ' />';
     } else {
         return MARKET_WEB_DIR . '/' . $out;
     }
 }
 function MARKET()
 {
     // Debugging
     if (defined('DEBUG') && DEBUG) {
         // Error Reporting
         error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
         ini_set('display_errors', '1');
         // Start debugging
         $this->getRef('Debug');
     }
     // Timezone settings
     if (defined('MARKET_TIMEZONE')) {
         date_default_timezone_set(MARKET_TIMEZONE);
     } else {
         date_default_timezone_set('UTC');
     }
     // Internal encoding for multi-byte string manipulation
     if (extension_loaded('mbstring')) {
         mb_internal_encoding('UTF-8');
         mb_regex_encoding('UTF-8');
     } else {
         MARKET_Base::raiseError(MARKET_ERROR_DIE, 'The "mbstring" extension is not loaded. Please see the README file.', __FILE__, __LINE__);
     }
     // Parse the request
     $this->getRef('Request');
 }