/** * @param int $n * * @return mixed * @throws Exception */ static function &getConnection($n = 0) { static $instance = array(); if (!isset($instance['__' . $n])) { $options = array(); $sufix = $n == 0 ? '' : '#' . $n; switch (__Config::get('DB_TYPE' . $sufix)) { case 'sqlite': $dsn = 'sqlite:' . __Paths::getRealPath('APPLICATION', 'data/' . __Config::get('DB_NAME' . $sufix)); $dbUser = ''; $dbPassword = ''; break; default: $socket = __Config::get('DB_SOCKET' . $sufix); $socket = empty($socket) ? '' : ';unix_socket=' . $socket; $dsn = 'mysql:dbname=' . __Config::get('DB_NAME' . $sufix) . ';host=' . __Config::get('DB_HOST' . $sufix) . $socket; $dbUser = __Config::get('DB_USER' . $sufix); $dbPassword = __Config::get('DB_PSW' . $sufix); if (__Config::get('CHARSET') == 'utf-8') { $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); } if (__Config::get('DB_MYSQL_BUFFERED_QUERY')) { $options[PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = true; } break; } $i = 0; $lastException = null; while (!isset($instance['__' . $n])) { try { if ($i < 3) { $instance['__' . $n] = new Connection($dsn, $dbUser, $dbPassword, $options); } else { break; } } catch (Exception $e) { $i++; $lastException = $e; $eventInfo = array('type' => GLZ_LOG_EVENT, 'data' => array('level' => GLZ_LOG_ERROR, 'group' => 'glizy.sql', 'message' => array('errorMessage' => $e->getMessage(), 'errorCode' => $e->getCode(), 'attempt' => $i))); $evt = org_glizy_ObjectFactory::createObject('org.glizy.events.Event', org_glizy_ObjectValues::get('org.glizy', 'application'), $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); } } if (!isset($instance['__' . $n])) { throw new Exception($lastException->getMessage(), $lastException->getCode(), $lastException); } } return $instance['__' . $n]; }
/** * @param int $errno * @param string $errstr * @param string $errfile * @param int $errline * @param string $message * @param int $headerCode */ public static function show($errno, $errstr, $errfile, $errline, $message = '', $headerCode = 500) { $eventInfo = array('type' => 'dumpException', 'data' => array('message' => $message, 'errono' => $errono, 'file' => $errfile, 'errline' => $errline)); $evt =& org_glizy_ObjectFactory::createObject('org.glizy.events.Event', $this, $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); @header('HTTP/1.0 500 Internal Server Error'); $errors = array(1 => 'E_ERROR', 2 => 'E_WARNING', 4 => 'E_PARSE', 8 => 'E_NOTICE', 16 => 'E_CORE_ERROR', 32 => 'E_CORE_WARNING', 64 => 'E_COMPILE_ERROR', 128 => 'E_COMPILE_WARNING', 256 => 'E_USER_ERROR', 512 => 'E_USER_WARNING', 2047 => 'E_ALL', 2048 => 'E_STRICT', 4096 => 'E_RECOVERABLE_ERROR'); $e = array(); $e['code'] = isset($errors[$errno]) ? $errors[$errno] : $errors[1]; $e['description'] = $errstr; $e['message'] = $message; if (class_exists('org_glizy_Config') && org_glizy_Config::get('DEBUG') === true) { $e['file'] = $errfile; $e['line'] = $errline; $e['stacktrace'] = array_slice(debug_backtrace(), 2); include_once dirname(__FILE__) . '/../../../pages/errors/debug.php'; } else { include_once dirname(__FILE__) . '/../../../pages/errors/general.php'; } exit; }
/** * Creates a connection object based on the config values. * This method returns a Doctrine\DBAL\Connection which wraps the underlying * driver connection. * * @param integer $n connection number * @return Doctrine\DBAL\Connection */ public static function getConnection($n = 0) { if (!self::$isLibLoaded) { self::loadLibrary(); } if (!isset(self::$connections['__' . $n])) { // se non è settato a true si effettua l'override dei tipi standard di doctrine if (!self::$defaultTypesOverridden) { \Doctrine\DBAL\Types\Type::overrideType(\Doctrine\DBAL\Types\Type::BOOLEAN, 'org_glizy_dataAccessDoctrine_types_Boolean'); \Doctrine\DBAL\Types\Type::overrideType(\Doctrine\DBAL\Types\Type::TARRAY, 'org_glizy_dataAccessDoctrine_types_Array'); \Doctrine\DBAL\Types\Type::overrideType(\Doctrine\DBAL\Types\Type::TIME, 'org_glizy_dataAccessDoctrine_types_Time'); \Doctrine\DBAL\Types\Type::overrideType(\Doctrine\DBAL\Types\Type::DATE, 'org_glizy_dataAccessDoctrine_types_Date'); \Doctrine\DBAL\Types\Type::overrideType(\Doctrine\DBAL\Types\Type::DATETIME, 'org_glizy_dataAccessDoctrine_types_DateTime'); \Doctrine\DBAL\Types\Type::overrideType(\Doctrine\DBAL\Types\Type::DATETIMETZ, 'org_glizy_dataAccessDoctrine_types_DateTimeTz'); \Doctrine\DBAL\Types\Type::addType(org_glizy_dataAccessDoctrine_types_Types::ARRAY_ID, 'org_glizy_dataAccessDoctrine_types_ArrayID'); self::$defaultTypesOverridden = true; } $sufix = $n == 0 ? '' : '#' . $n; $dbType = __Config::get('DB_TYPE' . $sufix); switch ($dbType) { case 'mysql': $params = array('driver' => 'pdo_mysql', 'host' => __Config::get('DB_HOST' . $sufix), 'user' => __Config::get('DB_USER' . $sufix), 'password' => __Config::get('DB_PSW' . $sufix), 'dbname' => __Config::get('DB_NAME' . $sufix), 'driverOptions' => array()); $socket = __Config::get('DB_SOCKET' . $sufix); if ($socket) { $params['unix_socket'] = $socket; } if (__Config::get('DB_MYSQL_BUFFERED_QUERY' . $sufix)) { $params['driverOptions'][PDO::MYSQL_ATTR_USE_BUFFERED_QUERY] = true; } if (__Config::get('DB_ATTR_PERSISTENT' . $sufix)) { $params['driverOptions'][PDO::ATTR_PERSISTENT] = true; } $charset = __Config::get('CHARSET'); if ($charset) { if (strtolower($charset) == 'utf-8') { $params['charset'] = 'utf8'; $params['driverOptions'][PDO::MYSQL_ATTR_INIT_COMMAND] = "SET NAMES utf8"; } else { $params['charset'] = $charset; } } break; case 'sqlite': $params = array('driver' => 'pdo_sqlite', 'user' => __Config::get('DB_USER' . $sufix), 'password' => __Config::get('DB_PSW' . $sufix)); $path = __Config::get('DB_NAME' . $sufix); if ($path == 'memory:') { $params['memory'] = true; } else { $params['path'] = $path; } break; case 'pgsql': $params = array('driver' => 'pdo_pgsql', 'host' => __Config::get('DB_HOST' . $sufix), 'user' => __Config::get('DB_USER' . $sufix), 'password' => __Config::get('DB_PSW' . $sufix), 'dbname' => __Config::get('DB_NAME' . $sufix)); break; } if (__Config::get('DB_PORT' . $sufix)) { $params['port'] = __Config::get('DB_PORT' . $sufix); } $params['wrapperClass'] = 'org_glizy_dataAccessDoctrine_Connection'; $checkDbConnection = __Config::get('DB_CHECK_CONNECTION' . $sufix) === true; $attempt = 0; $lastException = null; while ($attempt < 3) { try { $conn = \Doctrine\DBAL\DriverManager::getConnection($params); if ($checkDbConnection) { $conn->connect(); } self::$connections['__' . $n] = $conn; break; } catch (\PDOException $e) { $attempt++; $lastException = $e; } } if (!isset(self::$connections['__' . $n])) { $eventInfo = array('type' => GLZ_LOG_EVENT, 'data' => array('level' => GLZ_LOG_ERROR, 'group' => 'glizy.sql', 'message' => array('errorMessage' => $e->getMessage(), 'errorCode' => $e->getCode(), 'attempt' => $attempt))); $evt = org_glizy_ObjectFactory::createObject('org.glizy.events.Event', org_glizy_ObjectValues::get('org.glizy', 'application'), $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); throw new Exception($lastException->getMessage(), $lastException->getCode(), $lastException); } else { $platform = self::$connections['__' . $n]->getDatabasePlatform(); $platform->registerDoctrineTypeMapping('enum', 'string'); } } return self::$connections['__' . $n]; }
/** * @param $sql * @param array $param * @param bool $silent * * @return bool|DBResultSet */ function execute($sql, $param = array(), $silent = false) { $time_start = 0; if ($this->debug) { $time_start = microtime(); } if (strpos($this->_dsn, 'sqlite:') !== false) { preg_match_all('/(CONCAT\\(([^\\)]*)\\))/', $sql, $match); if (count($match[0])) { $sql = str_replace($match[0], str_replace(',', ' || ', $match[2]), $sql); } } // controlla se sono passati dei parametri e se questi sono un array ( es. per clausola IN) if (count($param)) { foreach ($param as $k => $v) { if (is_array($v) && $v[1] == 'EXPRESSION') { $sql = str_replace($k, $v[0], $sql); unset($param[$k]); } else { if (preg_match('/IN\\s*?\\(\\s*?' . $k . '\\s*?\\)/i', $sql)) { $sql = str_replace($k, $v, $sql); unset($param[$k]); } } } } $sth = $this->prepare($sql); if (count($param)) { if (strpos($sql, '?') !== false) { $r = $sth->execute($param); } else { foreach ($param as $k => $v) { if (is_array($v)) { $sth->bindValue($k, $v[0], $v[1]); } else { $sth->bindValue($k, $v); } } $r = $sth->execute(); } } else { $r = $sth->execute(); } $this->_errorCode = $sth->errorCode(); $this->_errorInfo = $sth->errorInfo(); if ($this->debug) { $time_end = microtime(); $time = $time_end - $time_start; echo "[time: " . $time . ", " . ($r ? "records: " . $sth->rowCount() : "") . "] "; $sql_br = nl2br($sql); if (count($param)) { if (strpos($sql_br, '?') !== false) { $sql_br = vsprintf(str_replace(array("%", "?"), array("%%", "'%s'"), $sql_br), $param); } else { foreach ($param as $k => $v) { $sql_br = str_replace($k, is_array($v) ? $v[0] : '"' . $v . '"', $sql_br); } } } echo $sql_br; echo "<br />\n\r"; } if (!$r) { if (!$this->debug) { // $sql = nl2br($sql); if (count($param)) { if (strpos($sql, '?') !== false) { $sql = vsprintf(str_replace(array("%", "?"), array("%%", "'%s'"), $sql), $param); } else { foreach ($param as $k => $v) { $sql = str_replace($k, is_array($v) ? $v[0] : '"' . $v . '"', $sql); } } } } $eventInfo = array('type' => GLZ_LOG_EVENT, 'data' => array('level' => GLZ_LOG_ERROR, 'group' => 'glizy.sql', 'message' => array('sql' => $sql, 'errorCode' => $this->_errorCode, 'errorInfo' => $this->_errorInfo))); $evt = org_glizy_ObjectFactory::createObject('org.glizy.events.Event', $this, $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); if (!$silent) { echo $this->ErrorMsg() . "<br />\n\r"; } return false; } return new DBResultSet($sth, $this, strpos($this->_dsn, 'sqlite:') !== false); }
/** * @param $type * @return bool */ function willTrigger($type) { return org_glizy_events_EventDispatcher::willTrigger($type); }
/** * @param $sql * @param array $params * @param null $limitLength * @param null $limitStart * @return mixed */ function _execute($sql, $params = array(), $limitLength = null, $limitStart = null) { if ($sql != 'SELECT FOUND_ROWS() as tot;') { $this->lastSql = $sql; } $debugState = $this->_conn->debug; if ($this->enableLog) { $this->_conn->debug = true; } $startTime = microtime(); $sqlForLog = $sql; ob_start(); if (is_null($limitLength)) { $r = $this->_conn->execute($sql, $params); } else { $r = $this->_conn->selectLimit($sql, $limitLength, $limitStart, $params); } $sql = ob_get_clean(); $this->_conn->debug = $debugState; $eventInfo = array('type' => GLZ_EVT_AR_EXEC_SQL, 'data' => array('level' => GLZ_LOG_DEBUG, 'group' => '', 'message' => array('sql' => $sqlForLog, 'params' => $params, 'startTime' => $startTime, 'resultSet' => $r))); $evt = org_glizy_ObjectFactory::createObject('org.glizy.events.Event', $this, $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); if ($this->_conn->debug || $this->enableLog) { if ($this->enableLog) { $eventInfo = array('type' => GLZ_LOG_EVENT, 'data' => array('level' => GLZ_LOG_DEBUG, 'group' => '', 'message' => strip_tags($sql))); $evt = org_glizy_ObjectFactory::createObject('org.glizy.events.Event', $this, $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); } if ($this->_conn->debug) { echo $sql . "<br />\n\r"; } } return $r; }
/** * @param $to * @param $from * @param $subject * @param $body * @param array $attach * @param array $cc * @param array $bcc * @param null $embedDir * * @return array */ static function sendEmail($to, $from, $subject, $body, $attach = array(), $cc = array(), $bcc = array(), $embedDir = NULL, $templateHeader = '', $templateFooter = '') { try { require_once GLZ_LIBS_DIR . "phpmailer/class.phpmailer.php"; /** @var PHPMailer $mail */ $mail = new PHPMailer(); $mail->CharSet = __Config::get('CHARSET'); if (org_glizy_Config::get('SMTP_HOST') != '') { $mail->IsSMTP(); // telling the class to use SMTP $mail->Host = org_glizy_Config::get('SMTP_HOST'); // SMTP server if (org_glizy_Config::get('SMTP_USER') != '') { $mail->Username = org_glizy_Config::get('SMTP_USER'); $mail->Password = org_glizy_Config::get('SMTP_PSW'); } $port = org_glizy_Config::get('SMTP_PORT'); if ($port) { $mail->Port = $port; } } $mail->From = trim($from['email']); $mail->FromName = $from['name']; $mail->AddAddress(trim($to['email']), $to['name']); $mail->Subject = $subject; $mail->AddAddress(trim($to['email']), $to['name']); if ($cc) { if (!is_array($cc)) { $cc = array($cc); } foreach ($cc as $v) { if ($v) { $mail->AddCC($v); } } } if ($bcc) { if (!is_array($bcc)) { $bcc = array($bcc); } foreach ($bcc as $v) { if ($v) { $mail->AddBCC($v); } } } $bodyTxt = $body; $bodyTxt = str_replace('<br>', "\r\n", $bodyTxt); $bodyTxt = str_replace('<br />', "\r\n", $bodyTxt); $bodyTxt = str_replace('</p>', "\r\n\r\n", $bodyTxt); $bodyTxt = strip_tags($bodyTxt); $bodyTxt = html_entity_decode($bodyTxt); if (!is_null($attach)) { foreach ($attach as $a) { $mail->AddAttachment($a['fileName'], $a['originalFileName']); } } if (!is_null($embedDir)) { $processedImage = array(); $embImage = 0; // controlla se c'è da fare l'embed delle immagini preg_match_all('/<img[^>]*src=("|\')([^("|\')]*)("|\')/i', $body, $inlineImages); if (count($inlineImages) && count($inlineImages[2])) { for ($i = 0; $i < count($inlineImages[2]); $i++) { if (in_array($inlineImages[2][$i], $processedImage)) { continue; } $processedImage[] = $inlineImages[2][$i]; $embImage++; $imageType = explode('.', $inlineImages[2][$i]); $code = str_pad($embImage, 3, '0', STR_PAD_LEFT); $mail->AddEmbeddedImage($embedDir . $inlineImages[2][$i], $code, $inlineImages[2][$i], "base64", "image/" . $imageType[count($imageType) - 1]); $body = str_replace($inlineImages[2][$i], 'cid:' . $code, $body); } } preg_match_all('/<td[^>]*background=("|\')([^("|\')]*)("|\')/i', $body, $inlineImages); if (count($inlineImages) && count($inlineImages[2])) { for ($i = 0; $i < count($inlineImages[2]); $i++) { if (in_array($inlineImages[2][$i], $processedImage)) { continue; } $processedImage[] = $inlineImages[2][$i]; $embImage++; $imageType = explode('.', $inlineImages[2][$i]); $code = str_pad($embImage, 3, '0', STR_PAD_LEFT); $mail->AddEmbeddedImage($embedDir . $inlineImages[2][$i], $code, $inlineImages[2][$i], "base64", "image/" . $imageType[count($imageType) - 1]); $body = str_replace($inlineImages[2][$i], 'cid:' . $code, $body); } } } $mail->Body = $templateHeader . $body . $templateFooter; $mail->AltBody = $bodyTxt; $r = array('status' => $mail->Send(), 'error' => $mail->ErrorInfo); } catch (Exception $e) { $r = array('status' => false, 'error' => $e->getMessage()); } if (isset($mail)) { $smtp_host = $mail->Host; $smtp_port = $mail->Port; } else { $smtp_host = ''; $smtp_port = ''; } $eventInfo = array('type' => GLZ_LOG_EVENT, 'data' => array('level' => $r['status'] ? GLZ_LOG_DEBUG : GLZ_LOG_ERROR, 'group' => 'glizy.helpers.Mail', 'message' => array('result' => $r, 'to' => $to, 'from' => $from, 'subject' => $subject, 'body' => $body, 'attach' => $attach, 'cc' => $cc, 'bcc' => $bcc, 'smtp_host' => $smtp_host, 'smtp_port' => $smtp_port))); $evt = org_glizy_ObjectFactory::createObject('org.glizy.events.Event', null, $eventInfo); org_glizy_events_EventDispatcher::dispatchEvent($evt); return $r; }