Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Advanced Web Core - www.Awcore.com
- */
- final class Awcore{
- const NEWS_RSS = 'http://system.awcore.com/mujaz_rss';
- protected $license_key = null;
- protected $initialized = false;
- protected $sessionState = Awcore::SESSION_STATE_UNINITIALIZED;
- const SESSION_STATE_UNAVAILABLE = -1;
- const SESSION_STATE_UNINITIALIZED = 0;
- const SESSION_STATE_INITIALIZED = 1;
- const SESSION_STATE_EXTERNAL = 2;
- protected $config = null;
- protected $translation = array();
- protected $html = array();
- protected $ad = array();
- protected $css = array();
- protected $js = array();
- function __construct() {
- Awcore::protect();
- if(include (dirname(__FILE__).'/version.php')){
- $this->version = $version;
- }
- $config_file = dirname(__FILE__).'/config.inc.php';
- if(file_exists($config_file)){
- if (include ($config_file)) {
- $this->license_key = $license_key;
- if($db_class = $this->loadClass('NotORM','db')){
- try {
- $connect = new PDO($db_info['dsn'], $db_info['user'], $db_info['pass']);
- } catch (Exception $e) {
- exit($e->getMessage());
- }
- $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- $connect->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
- $structure = new NotORM_Structure_Convention(
- $primary = "id",
- $foreign = "%s_id",
- $table = "%ss",
- $prefix = $db_info['prefix']
- );
- $this->db = new NotORM($connect,$structure,new NotORM_Cache_Include(CACHE_PATH.'db.php'));
- }
- unset($license_key,$db_info);
- }
- }else{
- exit(header("Location: setup"));
- }
- }
- public static function protect() {
- if (isset ($_SERVER['QUERY_STRING']) && strpos(urldecode($_SERVER['QUERY_STRING']), chr(0)) !== false) die();
- if (@ ini_get('register_globals') && isset ($_REQUEST)) {
- while (list($key, $value)= each($_REQUEST)) {
- $GLOBALS[$key] = null;
- unset ($GLOBALS[$key]);
- }
- }
- $targets= array ('PHP_SELF', 'HTTP_USER_AGENT', 'HTTP_REFERER', 'QUERY_STRING');
- foreach ($targets as $target) {
- $_SERVER[$target] = isset ($_SERVER[$target]) ? htmlspecialchars($_SERVER[$target], ENT_QUOTES) : null;
- }
- if (get_magic_quotes_gpc()) {
- $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST);
- while (list($key, $val) = each($process)) {
- foreach ($val as $k => $v) {
- unset($process[$key][$k]);
- if (is_array($v)) {
- $process[$key][stripslashes($k)] = $v;
- $process[] = &$process[$key][stripslashes($k)];
- } else {
- $process[$key][stripslashes($k)] = stripslashes($v);
- }
- }
- }
- unset($process);
- }
- }
- public function start($_smarty = true,$_manager = false,$_utility = true,$_session = true) {
- if (!$this->initialized) {
- if($this->loadClass('cache','tools')){
- $this->cache = new cache(array(
- 'cacheDir' => CACHE_PATH,
- 'lifeTime' => 90
- ));
- }
- $this->getConfig();
- if($_manager == true){
- $this->checkLicense();
- }
- if($_session){
- $this->session_start();
- }
- if ($auth_class = $this->loadClass('auth','auth')) {
- $this->auth = new $auth_class ($this);
- }
- if ($tools_class = $this->loadClass('tools','tools')) {
- $this->tools = new $tools_class ($this->config);
- }
- if($_utility){
- if ($news_class = $this->loadClass('news','news')) {
- $this->news = new $news_class ($this);
- }
- if ($videos_class = $this->loadClass('videos','videos')) {
- $this->videos = new $videos_class ($this);
- }
- if ($ads_class = $this->loadClass('ads','tools')) {
- $this->ads = new $ads_class ($this);
- $this->ad(160,600,'اعلان الجانب واحد','right');
- $this->ad(160,600,'اعلان الجانب اتنين','left');
- }
- }
- if($_smarty){
- if ($this->loadClass('smarty','smarty') && $this->smarty = new smarty()) {
- $this->smarty->template_dir = $this->getOption('base_path') . 'themes/'.$this->getOption('theme');
- $this->smarty->compile_dir = $this->getOption('cache_path');
- $this->smarty->plugins_dir = array(
- $this->getOption('base_path') . 'core/smarty/plugins',
- );
- if(!$_manager){
- $this->css = array_merge($this->css,(array) unserialize($this->getOption('css')));
- $this->js = array_merge($this->js,(array) unserialize($this->getOption('js')));
- if($this->tools->referer('facebook') && $this->getOption('facebook_auth',0)){
- $this->js('facebook.js');
- }
- if($this->auth->logged()){
- $this->js('notification.js');
- }
- if(!$links = $this->cache->get('links')){
- $links = array(
- 'header' => $this->db->links('position',1)->order('`sort` ASC'),
- 'footer' => $this->db->links('position',2)->order('`sort` ASC')->fetchPairs('cat_id','title'),
- );
- $this->cache->save($links,'links');
- }
- $this->smarty->assign( 'links' , $links );
- if($polls_class = $this->loadClass('polls','tools')){
- $poll = new $polls_class($this);
- $this->smarty->assign( 'poll' , $poll->display() );
- }
- }
- $this->smarty->assign( 'config' , $this->config );
- $this->smarty->assign( 'user' , $this->auth->data() );
- $this->smarty->assign( 'tools' , $this->tools );
- if($_utility){
- $this->smarty->assign( 'bar' , $this->news->bar() );
- $this->smarty->assign( 'cats' , $this->news->catgories_tree() );
- }
- $this->smarty->assign( 'version' , $this->getVersion() );
- }
- }
- $this->initialized= true;
- }
- }
- final private function checkLicense($reload = false){
- $host = $this->get_host();
- $key = $this->getLicenseKey();
- if(empty($host)){
- return false;
- }
- $file = $this->getOption('cache_path').md5($host).'.file.google.tpl.php';
- if(!file_exists($file)){
- $this->createLicense($file,$key,$host);
- }else{
- $last_fetch = filemtime($file);
- $check_time = ($reload) ? 5 : 7200;
- if((time() - $last_fetch) > $check_time){
- if(!$this->createLicense($file,$key,$host)){
- exit('حدث خطأ فى تصريحات مجلدات الملفات المؤقتة');
- }
- }
- if($reload){
- return true;
- }
- }
- if(file_exists($file)){
- include ($file);
- $x = $google_search;
- }else{
- $x = false;
- }
- if(!$x){
- $this->checkLicense(true);
- if($html = $this->loadUrl('http://system.awcore.com/license/index.html')){
- exit($html);
- }else{
- exit('لا يمكن العمل الا فى وجود اتصال بالانترنت');
- }
- }
- }
- function createLicense($file,$key,$host){
- if(!$this->prepareDir(dirname($file))){
- exit('مجلد الكاش يجب ان يكون قابل للكتابة');
- }
- if($response = $this->loadUrl("http://system.awcore.com/mujaz_license?host={$host}&key={$key}&i=".uniqid())){
- $this->updateOption('license_info',$response);
- $response = (array) unserialize($response);
- if($response['response']){
- if(in_array($host,(array) $response['domains'])){
- if($response['expire'] > time()){
- $auth = 'true';
- }else{
- $auth = 'false';
- }
- }else{
- $auth = 'false';
- }
- }else{
- $auth = 'false';
- }
- }else{
- $auth = 'false';
- }
- $fp = fopen($file,'w');
- $written = fwrite($fp,html_entity_decode("<?php \$google_search = $auth; ?>"));
- fclose($fp);
- return $written;
- }
- final function get_host() {
- if ($host = $_SERVER['HTTP_X_FORWARDED_HOST'])
- {
- $elements = explode(',', $host);
- $host = trim(end($elements));
- }
- else
- {
- if (!$host = $_SERVER['HTTP_HOST'])
- {
- if (!$host = $_SERVER['SERVER_NAME'])
- {
- $host = !empty($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '';
- }
- }
- }
- $host = preg_replace('/:\d+$/', '', $host);
- preg_match("/^(http:\/\/)?([^\/]+)/i",$host, $matches);
- preg_match("/[^\.\/]+\.[^\.\/]+$/", $matches[2], $matches);
- return $matches[0];
- }
- private function prepareDir($directory) {
- $chmod = 0755;
- if (!is_dir($directory)) {
- $old = umask(0000);
- mkdir($directory, $chmod);
- umask($old);
- } elseif (!$this->testPermissions($directory, $chmod)) {
- return false;
- }
- return true;
- }
- private function testPermissions($dir, $chmod) {
- if (is_writable($dir)) return true;
- if (!is_dir($dir)) {
- trigger_error('المجلد '.$dir.' غير موجود',E_USER_WARNING);
- return false;
- }
- if (function_exists('posix_getuid')) {
- if (fileowner($dir) === posix_getuid()) {
- $chmod = $chmod | 0700;
- if (chmod($dir, $chmod)) return true;
- } elseif (filegroup($dir) === posix_getgid()) {
- $chmod = $chmod | 0070;
- } else {
- $chmod = $chmod | 0777;
- }
- trigger_error('المجلد '.$dir.' غير قابل للكتابة, '.'من فضلك اعطه التصريح ' . decoct($chmod),E_USER_WARNING);
- } else {
- trigger_error('المجلد '.$dir.' غير قابل للكتابة, '.'من فضلك قم بتغيير التصاريح',E_USER_WARNING);
- }
- return false;
- }
- public function getVersion(){
- return $this->version;
- }
- public function getLicenseKey(){
- return $this->license_key;
- }
- public function getOption($key, $default = null){
- if (is_string($key) && !empty($key)) {
- if (is_array($this->config) && !empty($this->config) && array_key_exists($key, $this->config)) {
- $option = ($this->config[$key] !== '') ? $this->config[$key] : $default;
- }
- }
- return $option;
- }
- public function updateOption($key, $value){
- return $this->db->options('name' , $key)->update(array('value' => $value));
- }
- public function getConfig() {
- if (!$this->initialized || !is_array($this->config) || empty ($this->config)) {
- if (!isset ($this->config['base_url']))
- $this->config['base_url']= BASE_URL;
- if (!isset ($this->config['base_path']))
- $this->config['base_path']= BASE_PATH;
- if (!isset ($this->config['cache_path']))
- $this->config['cache_path']= CACHE_PATH;
- if (!isset ($this->config['url_scheme']))
- $this->config['url_scheme']= URL_SCHEME;
- if (!isset ($this->config['http_host']))
- $this->config['http_host']= HTTP_HOST;
- if (!isset ($this->config['site_url']))
- $this->config['site_url']= SITE_URL;
- if (!isset ($this->config['https_port']))
- $this->config['https_port']= isset($GLOBALS['https_port']) ? $GLOBALS['https_port'] : 443;
- if(!$config = $this->cache->get('options')){
- foreach ($this->db->options()->fetchPairs('name','value') as $name => $value) {
- $config[$name] = $this->plaintext($value);
- }
- $this->cache->save($config,'options');
- }
- if(is_array($config)){
- $this->config = array_merge($this->config, $config);
- }
- }
- return $this->config;
- }
- function loadClass($className,$packageName = '',$constructor = ''){
- if (empty($constructor)){
- $constructor=$className;
- }
- if(!is_callable(array($className,$constructor))){
- $pkg = dirname(__FILE__)."/{$packageName}/";
- if (is_dir($pkg)){
- $class = $pkg.strtolower($className).".class.php";
- if(file_exists($class)){
- include_once($class);
- }else{
- die("مجمع الاكواد <b>$className</b> غير موجود فى المجموعة الكودية <b>$packageName</b>");
- }
- }else{
- die("المجموعة الكودية <b>$packageName</b> غير موجودة");
- }
- }
- return $className;
- }
- function loadUrl($url,$expected_result = ''){
- $url = trim($url);
- Awcore::loadClass('url','dom');
- $url = get_final_url(html_entity_decode($url));
- $timeout = 30;
- $userAgent = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091030 Gentoo Firefox/3.5.4';
- $ch = curl_init();
- curl_setopt($ch,CURLOPT_URL,$url);
- curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
- curl_setopt($ch,CURLOPT_USERAGENT, $userAgent);
- curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
- if (!ini_get('open_basedir') && ini_get('safe_mode' == 'Off')){
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- }
- switch ($expected_result){
- case 'html':
- curl_setopt($ch, CURLOPT_HEADER, 1);
- break;
- case 'images':
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
- break;
- }
- $data = curl_exec($ch);
- if($expected_result == 'html'){
- $offset = strpos($data, "\r\n\r\n");
- $header = substr($data, 0, $offset);
- $data = substr($data, $offset + 4);
- $encoding = false;
- if(preg_match('/^Content-Type:\s+([^;]+)(?:;\s*charset=(.*))?/im', $header, $match)){
- if(isset($match[2])){
- $encoding = strtolower(trim($match[2]));
- }else{
- if(preg_match('/charset=([^"]+)/i', $data, $match)){
- if(isset($match[1])){
- $encoding = strtolower(trim($match[1]));
- }
- }
- }
- }
- if($encoding && $encoding <> 'utf-8'){
- if (function_exists('iconv')) {
- $data = iconv($encoding, 'utf-8', $data);
- } elseif (function_exists('mb_convert_encoding')) {
- $data = mb_convert_encoding($data, 'utf-8', $encoding);
- } elseif (function_exists('recode_string')) {
- $data = recode_string($encoding . '..utf-8', $data);
- }
- }
- }
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- return ($httpCode == 200) ? $data : $httpCode;
- }
- function loadlang($file){
- if(include_once(dirname(__FILE__)."/language/{$file}.php")){
- $this->translation = array_merge($this->translation,(array)$lang);
- }
- }
- protected function session_start() {
- if (!in_array($this->getSessionState(), array(Awcore::SESSION_STATE_INITIALIZED, Awcore::SESSION_STATE_EXTERNAL, Awcore::SESSION_STATE_UNAVAILABLE), true)) {
- if($this->getOption('db_session')){
- if ($shClass = $this->loadClass('sessions','auth')) {
- if ($sh= new $shClass($this->db)) {
- $sh->lifetime($this->getOption('session_gc_maxlifetime'));
- session_set_save_handler(
- array (& $sh, 'open'),
- array (& $sh, 'close'),
- array (& $sh, 'read'),
- array (& $sh, 'write'),
- array (& $sh, 'destroy'),
- array (& $sh, 'gc')
- );
- }
- }
- }
- $cookieDomain= $this->getOption('session_cookie_domain','');
- $cookiePath= $this->getOption('session_cookie_path','/');
- $cookieSecure= (boolean) $this->getOption('session_cookie_secure',false);
- $cookieLifetime= (integer) $this->getOption('session_cookie_lifetime',0);
- $gcMaxlifetime = (integer) $this->getOption('session_gc_maxlifetime',$cookieLifetime);
- if ($gcMaxlifetime > 0) {
- ini_set('session.gc_maxlifetime', $gcMaxlifetime);
- }
- $site_sessionname= $this->getOption('session_name', null);
- if (!empty($site_sessionname)){
- session_name($site_sessionname);
- }
- session_set_cookie_params($cookieLifetime, $cookiePath, $cookieDomain, $cookieSecure);
- session_start();
- $this->sessionState = Awcore::SESSION_STATE_INITIALIZED;
- $cookieExpiration= 0;
- if (isset ($_SESSION['Awcore.session.cookie.lifetime'])) {
- $sessionCookieLifetime= (integer) $_SESSION['Awcore.session.cookie.lifetime'];
- if ($sessionCookieLifetime !== $cookieLifetime) {
- if ($sessionCookieLifetime) {
- $cookieExpiration= time() + $sessionCookieLifetime;
- }
- setcookie(session_name(), session_id(), $cookieExpiration, $cookiePath, $cookieDomain, $cookieSecure);
- }
- }
- }
- }
- public function getSessionState() {
- if ($this->sessionState !== Awcore::SESSION_STATE_INITIALIZED) {
- if (headers_sent()) {
- $this->sessionState = Awcore::SESSION_STATE_UNAVAILABLE;
- }elseif (isset($_SESSION)) {
- $this->sessionState = Awcore::SESSION_STATE_EXTERNAL;
- }
- }
- return $this->sessionState;
- }
- function translate($str, $tokens = array()) {
- $explode = explode(':',$str);
- if (array_key_exists($explode[0],(array) $this->translation)){
- $str = $this->translation[$explode[0]];
- }
- if (is_array($str)){
- $str = $str[$explode[1]];
- }
- if (is_array($tokens) && sizeof($tokens) > 0){
- $str = vsprintf($str, $tokens);
- }
- return $str;
- }
- function photo_list($id,$prefix = 'N'){
- return $this->db->upload_imgs('prefix',"{$prefix}:{$id}")->fetchPairs('id','img_file');
- }
- function delete_photo($id){
- $path = $this->getOption('base_path').'dynamic/uploads/images/';
- if(is_int($id)){
- $row = $this->db->upload_imgs('id',$id);
- $file = $path.$row->fetchSingle('img_file');
- if(file_exists($file)){
- if(unlink($file)){
- $row->delete();
- }
- }
- }else{
- $file = $path.$id;
- if(file_exists($file)){
- unlink($file);
- }
- }
- }
- function plaintext($str,$quotes = true){
- return ($quotes) ? htmlspecialchars($str,ENT_NOQUOTES,"UTF-8") : htmlspecialchars($str,ENT_QUOTES,"UTF-8");
- }
- function safehtml($str,$options = array()){
- $this->loadClass('load','purifier');
- $config = HTMLPurifier_Config::createDefault();
- $config->set('Cache.SerializerPath', $this->getOption('cache_path'));
- $config->set('AutoFormat.RemoveEmpty.RemoveNbsp', true);
- $config->set('AutoFormat.RemoveEmpty.RemoveNbsp.Exceptions', 'iframe');
- $config->set('AutoFormat.AutoParagraph', true);
- $config->set('HTML.SafeIframe', true);
- $config->set('URI.SafeIframeRegexp', '%^http://(www.youtube.com/embed/|player.vimeo.com/video/|www.dailymotion.com/embed/video/)%');
- $config->set('Filter.YouTube', true);
- $config->set('HTML.SafeObject', true);
- $config->set('HTML.SafeEmbed', true);
- if(is_array($options['ForbiddenElements']) && count($options['ForbiddenElements'])){
- $config->set('HTML.ForbiddenElements', $options['ForbiddenElements']);
- }
- $filter = new HTMLPurifier($config);
- return $filter->purify($str);
- }
- function cron($key,$duration){
- $now = time();
- $last = (int) $this->getOption($key);
- if(($now - $last) > $duration){
- $this->updateOption($key,$now);
- $this->cache->remove('options');
- return true;
- }
- return false;
- }
- public function ad($width,$height,$info,$key = false){
- if($key){
- return $this->ad[$key] = $this->ads->draw($width,$height,$info);
- }else{
- return $this->ad[] = $this->ads->draw($width,$height,$info);
- }
- }
- public function html($code,$place = 'header'){
- return $this->html[$place][] = $code;
- }
- public function css(){
- foreach(func_get_args() as $file){
- $this->css[] = $file;
- }
- }
- public function js(){
- foreach(func_get_args() as $file){
- $this->js[] = $file;
- }
- }
- final function display($tpl_file = null,$title = 'غير مسمى',$options = array()){
- $css_files = $css_rules = $js_files = array();
- foreach ($this->css as $css_item) {
- if(strpos($css_item, ":") !== false){
- $css_rules[] = $css_item;
- }else{
- $css_files[] = $this->getOption('base_url').'/static/css/'.$css_item;
- }
- }
- $this->smarty->assign('css', array('files' => $css_files,'rules' => $css_rules));
- foreach ($this->js as $js_item) {
- $js_files[] = $this->getOption('base_url').'/static/js/'.$js_item;
- }
- $this->smarty->assign('js', $js_files );
- $this->smarty->assign('html', $this->html );
- $this->smarty->assign('ad', $this->ad );
- $this->smarty->assign('tpl_file',$tpl_file);
- if(is_array($title)){
- $title = array_reverse($title);
- $title = implode(' | ',$title);
- }
- $this->smarty->assign('title',$title);
- $page = array_merge(array(
- 'description' => null,
- 'keywords' => null,
- 'noindex' => false,
- 'og_image' => false
- ),$options);
- $this->smarty->assign('page',$page);
- $this->smarty->display($this->smarty->structure_tpl);
- unset($this->db);
- #echo gen_time();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement