<?php
class PowerBBFunctions
{

	/**
	 * Get sections list from cache and show it.
	 */
	function _GetSections()
	{
		global $PowerBB;
		$SecArr 						= 	array();
		$SecArr['get_from']				=	'db';

		$SecArr['proc'] 				= 	array();
		$SecArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

		$SecArr['order']				=	array();
		$SecArr['order']['field']		=	'sort';
		$SecArr['order']['type']		=	'ASC';

		$SecArr['where']				=	array();
		$SecArr['where'][0]['name']		= 	'parent';
		$SecArr['where'][0]['oper']		= 	'=';
		$SecArr['where'][0]['value']	= 	'0';

		// Get main sections
		$cats = $PowerBB->section->GetSectionsList($SecArr);

		// Loop to read the information of main sections
		foreach ($cats as $cat)
		{
	       // Get the groups information to know view this section or not
	      $sectiongroup = unserialize(base64_decode($cat['sectiongroup_cache']));
		  if ($sectiongroup[$PowerBB->_CONF['group_info']['id']]['view_section'])
	      {
             // foreach main sections
			$PowerBB->_CONF['template']['foreach']['forums_list'][$cat['id'] . '_m'] = $cat;
			unset($sectiongroup);

            // Get main Forums
			$ForumArr 						= 	array();
			$ForumArr['get_from']				=	'db';

			$ForumArr['proc'] 				= 	array();
			$ForumArr['proc']['*'] 			= 	array('method'=>'clean','param'=>'html');

			$ForumArr['order']				=	array();
			$ForumArr['order']['field']		=	'sort';
			$ForumArr['order']['type']		=	'ASC';

			$ForumArr['where']				=	array();
			$ForumArr['where'][0]['name']		= 	'parent';
			$ForumArr['where'][0]['oper']		= 	'=';
			$ForumArr['where'][0]['value']	= 	$cat['id'];

			// Get parent sections
			$forums = $PowerBB->section->GetSectionsList($ForumArr);

			foreach ($forums as $forum)
			{
				$groups = unserialize(base64_decode($forum['sectiongroup_cache']));
				if ($groups[$PowerBB->_CONF['group_info']['id']]['view_section'])
				{
					$forum_last_time1 = $forum['last_date'];
					$forum['last_post_date'] = $forum['last_time'];
					$forum['last_date'] = $PowerBB->functions->date($forum_last_time1);
					$forum['simple_title'] = $forum['title'];
					$forum['last_subject'] = $PowerBB->Powerparse->censor_words($forum['last_subject']);
					$forum['last_subject'] =  $PowerBB->Powerparse->_wordwrap($forum['last_subject'],'60');
                    $forum['last_writer'] = $PowerBB->functions->GetUsernameStyleAndUserId($forum['last_writer']);

                    if ($PowerBB->_CONF['info_row']['no_sub'])
				    {
						$forum['is_sub'] 	= 	0;
						$forum['sub']		=	'';

						if (!empty($forum['forums_cache']))
						{
							$subs = unserialize(base64_decode($forum['forums_cache']));

							$t=0;
							$forum['sub'] .= "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\"><tr valign=\"top\"> ";

							if (is_array($subs))
							{
                                foreach($subs as $sub)
								{
                                        $forum_subject_num = $forum['subject_num']+ $sub['subject_num'];
                                        $forum_reply_num =  $forum['reply_num']+ $sub['reply_num'];
                                        $forum['subject_num']   = $forum_subject_num;
                                        $forum['reply_num']   = $forum_reply_num;
                                           $forum_last_subjectid = $forum['last_subjectid'];
                                           $sub_last_subjectid = $sub['last_subjectid'];
                                           $last_time = $sub['last_time'];

									if (is_array($sub['groups'][$PowerBB->_CONF['group_info']['id']]))
									{
										if ($sub['groups'][$PowerBB->_CONF['group_info']['id']]['view_section'])
										{

												if ($sub['id'])
												{
												$forum['is_sub'] = 1;
												}
												if($t== $PowerBB->_CONF['info_row']['sub_columns_number']){
												$t=0;
												$forum['sub'] .="</tr><tr>";
												}
												$forum['sub'] .= "<td>";
												if ($PowerBB->_CONF['info_row']['rewriterule'] == '1')
												{
												$forum['sub'] .= '<a href="f' . $sub['id'] . '.html"><img border="0" alt="" src="' . $PowerBB->_CONF['template']['image_path'] . '/address_bar_start.gif" />  ' . $sub['title'] . '</a> ';
												}
												else
												{
												$forum['sub'] .= '<a href="index.php?page=forum&amp;show=1&amp;id=' . $sub['id'] . '"><img border="0" alt="" src="' . $PowerBB->_CONF['template']['image_path'] . '/address_bar_start.gif" />  ' . $sub['title'] . '</a>';
												}
												$forum['sub'] .= "&nbsp;</td>";
												$t=$t+1;

										}
									}
								}
							    	$forum['sub'] .="</tr></table>";
							}


						}
                     }
				   //////////

				  // Get the moderators list as a _link_ and store it in $forum['moderators_list']
                   if ($PowerBB->_CONF['info_row']['no_moderators'])
				   {
						$forum['is_moderators'] 		= 	0;
						$forum['moderators_list']		=	'';

						if (!empty($forum['moderators']))
						{
							$moderators = unserialize($forum['moderators']);

							if (is_array($moderators))
							{
                               foreach($moderators as $moderator)
								{
									if (!$forum['is_moderators'])
									{
										$forum['is_moderators'] = 1;
									}
								               if ($PowerBB->_CONF['info_row']['rewriterule'] == '1')
												{
												$forum['moderators_list'] .= '<a href="u' . $moderator['member_id'] . '.html">' . $PowerBB->functions->GetUsernameStyle($moderator['username']) . '</a> | ';
												}
												else
												{
									            $forum['moderators_list'] .= '<a href="index.php?page=profile&amp;show=1&amp;id=' . $moderator['member_id'] . '">' . $PowerBB->functions->GetUsernameStyle($moderator['username']) .'</a> | ';
												}
								}
							}
						}
                    }
					//////////

					// Get online forums
					if ($PowerBB->_CONF['info_row']['active_forum_online_number'])
					{
					$Forum_online_number = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['online'] . " WHERE section_id='" . $forum['id'] . "'"));
					$forum['forum_online'] = $Forum_online_number;
					}

					 $forum_title_color = "'".$forum['forum_title_color']."'";
					 $forum['title'] = "<font color=".$forum_title_color.">".$forum['title']."</font>";

					$PowerBB->_CONF['template']['foreach']['forums_list'][$forum['id'] . '_f'] = $forum;
					unset($groups);

				}// end view forums
             } // end foreach ($forums)
		   } // end view section

				unset($ForumArr);
				$ForumArr = @mysql_free_result($ForumArr);

				unset($SecArr);
				$SecArr = @mysql_free_result($SecArr);
		} // end foreach ($cats)


		//////////
	}


	/**
	 * Check if delicious cookie is here or another eat it mmmm :)
	 */
	function IsCookie($cookie_name)
 	{
 		global $PowerBB;

 		return empty($PowerBB->_COOKIE[$cookie_name]) ? false : true;
 	}

 	/**
 	 * Clean the variable from any dirty :) , we should be thankful for abuamal
 	 *
 	 * By : abuamal
 	 */
	function CleanVariable($variable, $type)
	{
		global $PowerBB;

		return $PowerBB->sys_functions->CleanVariable($variable, $type);
	}

 	function AddressBar($title)
 	{
 		global $PowerBB;

 		$PowerBB->template->display('address_bar_part1');
 		echo $title;
 		$PowerBB->template->display('address_bar_part2');
 	}

 	/**
 	 * Show footer and stop the script , footer is like water in the life :)
 	 */
 	function stop($no_style = false)
 	{
 		global $PowerBB;

 		if (!$no_style)
 		{
 			$PowerBB->template->display('footer');
 		}

 		exit();
 	}

 	/**
 	 * go to $site , abuamal hate this function :D don't ask me why , ask him ;)
 	 */
	function redirect($site,$m=1)
 	{
     global $PowerBB;
  		echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"$m; URL=$site\">\n";
		$transition_click = $PowerBB->_CONF['template']['lang']['If_your_browser_does_not_support_automatic_transition_click_here'];
      $transition = ('<a href="'.$site.'">'.$transition_click.'</a>');
      echo '	<table class="redirect" border="0" width="60%" cellspacing="0" align="center"><tr><td class="row1" align="center">' . $transition . '</td></tr></table><br /><br />';

 	}
	function redirect2($site,$m=0)
 	{
     global $PowerBB;
  		echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"$m; URL=$site\">\n";
      $transition = ('<a href="'.$site.'">'.$transition_click.'</a>');
 	}

	/**
	 * Show $msg in nice template
	 */
 	function msg($msg,$no_style = false)
    {
    	global $PowerBB;

    	if (defined('IN_ADMIN')
    		or defined('STOP_STYLE')
    		or $no_style)
 		{
    		 $PowerBB->template->assign('msg',$msg);
    		$PowerBB->template->display('show_msg');
    	}
    	else
    	{
    		echo '<font face="Tahoma" size="2"><div dir=' . $PowerBB->_CONF['info_row']['content_dir'] . ' align="center">' . $msg . '</div></font>';
    	}
 	}

	function install_msg($code)
	{
		$installation_state = $PowerBB->_CONF['template']['lang']['installation_state'];
	echo '<table border="0" cellspacing="1" width="50%" class="t_style_b" align="center">
		<tr>
			<td class="main1" colspan="2" height="23">
	'.$installation_state.'
			</td>
		</tr>
		<tr>
			<td class="row1" colspan="2" align="center">
			<p>
	';
	eval($code);

	echo '</p>
			</td>
		</tr>
	</table>
	<br />
	<br />';

	}

    function reputation_alert($msg)
    {
       global $PowerBB;
          if (!$no_header and $no_style)
          {
          $this->ShowHeader('');
          }
       echo "<script>alert('".$msg."');</script>";
       $this->stop(TRUE);
    }


 	 function stoperror($no_style = false)
 	{
 		global $PowerBB;

 		if (!$no_style)
 		{

 		}

 		exit();
 	}


 	function msgerror($msg,$no_style = false)
    {
    	global $PowerBB;

    	if (defined('IN_ADMIN')
    		or defined('STOP_STYLE')
    		or $no_style)
 		{
    		echo '<font face="Tahoma" size="2"><div dir=' . $PowerBB->_CONF['info_row']['content_dir'] . ' align="center">' . $msg . '</div></font>';

    	}

 	}

 		/**
	 * Show error massege and stop script
	 */
 	function errorstop($msgerror,$no_header = false,$no_style = false)
    {
    	global $PowerBB;

    	if (!$no_header and $no_style)
    	{
    		$this->ShowHeader('');
    	}

  		$this->msgerror($msgerror,$no_style);
  		$this->stoperror($no_style);
 	}

	/**
	 * Show error massege and stop script
	 */
 	function error($msg,$no_header = false,$no_style = false)
    {
    	global $PowerBB;

    	if (!$no_header and $no_style)
    	{
    		$this->ShowHeader('');
    	}

  		$this->msg($msg,$no_style);
  		$this->stop($no_style);
 	}

	/**
	 * Check if $email is true email or not
	 *
	 * This function by : Pal Coder from PowerBB 1.x
	 */
	function CheckEmail($email)
	{
	return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s\'"<>@,;]+\.+[a-z]{2,6}))$#si', $email) ? true : false;
	}


 	/**
 	 * Get file extention
 	 *
 	 * @param :
 	 *				filename -> the name of file which we want know it's extension
 	 */

	function GetFileExtension($filename)
	{
				global $PowerBB;

              if ( stristr($filename,'.php') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.php3') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.phtml') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.pl') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.cgi') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.asp') )
             {
             $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.3gp') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}
			if ( stristr($filename,'.exe') )
             {
              $PowerBB->functions->error($PowerBB->_CONF['template']['lang']['Can_you_raise_file_extension']);
			}

	   $temparray = explode(".", $filename);
	   $extension = $temparray[count($temparray) - 1];
	   $extension = strtolower($extension);

	   return '.' . $extension;
	}

 	/**
 	 * Show the default footer of forum page
 	 */
 	function GetFooter()
 	{
 		// The instructions stored in footer module
 		// so include footer module to execute these inctructions
 		include('modules/footer.module.php');

 		// Get the name of class
        $footer_name = FOOTER_NAME;

        // Make a new object
        $footer_name = new $footer_name;

        // Execute inctructions
        $footer_name->run();
 	}

 	/**
 	 * Show the default header of forum page
 	 */
 	function ShowHeader($title = null)
 	{
 		global $PowerBB;

 		// Check if title is empty so use the default name of forum
 		// which stored in info_row['title']
 		$title = (isset($title)) ? $title : $PowerBB->_CONF['info_row']['title'];
		$page = empty($PowerBB->_GET['page']) ? 'index' : $PowerBB->_GET['page'];
		$page_address 					= 	array();
		if ($page == 'index')
		{
		$page_address['index'] 		= 	$PowerBB->_CONF['info_row']['title'];
		$PowerBB->template->assign('description',$PowerBB->_CONF['info_row']['description']);
		$PowerBB->template->assign('keywords',$PowerBB->_CONF['info_row']['keywords']);
		$PowerBB->template->assign('index',1);
		}
		elseif ($PowerBB->_GET['page'] == 'forum')
		{
        $Forum 			= 	array();
		$Forum['where'] 	= 	array('id',$PowerBB->_GET['id']);
		$Forum_rwo = $PowerBB->section->GetSectionInfo($Forum);
		$page_address['forum'] 		= 	$Forum_rwo['title'] .' - '. $PowerBB->_CONF['info_row']['title'];
		 $PowerBB->template->assign('description',$Forum_rwo['title']);
		}
		elseif ($PowerBB->_GET['page'] == 'profile')
		{
        $MemberIDArr 			= 	array();
		$MemberIDArr['where'] 	= 	array('id',$PowerBB->_GET['id']);

		$MemIDInfo = $PowerBB->member->GetMemberInfo($MemberIDArr);

		$MemberUsernameArr 			= 	array();
		$MemberUsernameArr['where'] 	= 	array('username',$PowerBB->_GET['username']);

		$MemUserInfo= $PowerBB->member->GetMemberInfo($MemberUsernameArr);

		$page_address['profile'] 		= 	$PowerBB->_CONF['template']['lang']['View_Member_Profile'].' '. $MemUserInfo['username'].$MemIDInfo['username'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
		elseif ($PowerBB->_GET['page'] == 'topic')
		{
		$SubjectArr = array();
		$SubjectArr['where'] = array('id',$PowerBB->_GET['id']);

		$SubjectInfo = $PowerBB->subject->GetSubjectInfo($SubjectArr);

		$page_address['topic'] 		= 	$PowerBB->Powerparse->censor_words($SubjectInfo['prefix_subject'].' '.$SubjectInfo['title'] .' - '. $PowerBB->_CONF['info_row']['title']);

		$TagArr 			= 	array();
		$TagArr['where'] 	= 	array('subject_id',$PowerBB->_GET['id']);

		$PowerBB->_CONF['template']['while']['tags'] = $PowerBB->tag->GetSubjectList($TagArr);

		$num = '100';
		$SubjectInfo['text'] = $PowerBB->functions->words_count_replace_strip_tags_html2bb($SubjectInfo['text'],$num);

         $PowerBB->template->assign('keywords',$PowerBB->_CONF['template']['while']['tags']);
		 $PowerBB->template->assign('description',$SubjectInfo['text']);
		}
		elseif ($PowerBB->_GET['rules'] == '1')
		{
		$page_address['misc'] 		= 	$PowerBB->_CONF['template']['lang']['rules'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
        elseif ($PowerBB->_GET['sendmessage'] == '1')
		{
		$page_address['send'] 		= 	$PowerBB->_CONF['template']['lang']['Contact_Manager'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
        elseif ($PowerBB->_GET['folder'] == 'sent')
		{
		$page_address['pm_list'] 		= 	$PowerBB->_CONF['template']['lang']['Outgoing_Messages'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
        elseif ($PowerBB->_GET['folder'] == 'inbox')
		{
		$page_address['pm_list'] 		= 	$PowerBB->_CONF['template']['lang']['Contained_Messages'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
        elseif ($PowerBB->_GET['info'] == '1')
		{
		$page_address['usercp'] 		= 	$PowerBB->_CONF['template']['lang']['Your_Personal_Information'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
		elseif ($PowerBB->_GET['page'] == 'pages')
		{
		$PageArr 			= 	array();
		$PageArr['where'] 	= 	array('id',$PowerBB->_GET['id']);

		$PowerBB->_CONF['template']['GetPage'] = $PowerBB->pages->GetPageInfo($PageArr);

		$page_address['pages'] 		= 	$PowerBB->Powerparse->censor_words($PowerBB->_CONF['template']['GetPage']['title'] .' - '. $PowerBB->_CONF['info_row']['title']);
		}
		elseif ($PowerBB->_CONF['info_row']['board_close'] == '1')
    	{
		$title 		= 	$PowerBB->_CONF['template']['lang']['board_close'];
 		}
		elseif ($PowerBB->_GET['page'] == 'management'
		AND $PowerBB->_GET['subject'] == '1'
		AND $PowerBB->_GET['operator'] == 'edit')
    	{
		$page_address['management'] 		= 	$PowerBB->_CONF['template']['lang']['edit_subject'] .' - '. $PowerBB->_CONF['info_row']['title'];
 		}
		elseif ($PowerBB->_GET['page'] == 'management'
		AND $PowerBB->_GET['reply'] == '1'
		AND $PowerBB->_GET['operator'] == 'edit')
    	{
		$page_address['management'] 		= 	$PowerBB->_CONF['template']['lang']['edit_reply'] .' - '. $PowerBB->_CONF['info_row']['title'];
 		}
		elseif ($PowerBB->_GET['operator'] == 'move')
    	{
		$page_address['management'] 		= 	$PowerBB->_CONF['template']['lang']['mov_Subject'] .' - '. $PowerBB->_CONF['info_row']['title'];
 		}
		elseif ($PowerBB->_GET['operator'] == 'close')
    	{
		$page_address['management'] 		= 	$PowerBB->_CONF['template']['lang']['locked_Topic'] .' - '. $PowerBB->_CONF['info_row']['title'];
 		}
		elseif ($PowerBB->_GET['operator'] == 'repeated')
    	{
		$page_address['management'] 		= 	$PowerBB->_CONF['template']['lang']['repeated_Subject'] .' - '. $PowerBB->_CONF['info_row']['title'];
 		}
		elseif ($PowerBB->_GET['operator'] == 'tools_thread')
    	{
		$page_address['management'] 		= 	$PowerBB->_CONF['template']['lang']['Management_Subjects'] .' - '. $PowerBB->_CONF['info_row']['title'];
 		}
		elseif ($PowerBB->_GET['page'] == 'tags')
		{
		$TagInfoArr 			= 	array();
		$TagInfoArr['where'] 	= 	array('tag_id',$PowerBB->_GET['id']);
		$TagInfo = $PowerBB->tag_subject->GetSubjectInfo($TagInfoArr);

		$page_address['tags'] 			= 	$TagInfo['tag'] .' - '. $PowerBB->_CONF['info_row']['title'];
		}
		elseif ($PowerBB->_GET['page'] == 'portal')
		{
		$page_address['portal'] 		= 	$PowerBB->_CONF['info_row']['title_portal'];
		}
		$page_address['managementreply']= 	$PowerBB->_CONF['template']['lang']['Management_Subjects'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['static'] 		= 	$PowerBB->_CONF['template']['lang']['statics'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['member_list'] 	= 	$PowerBB->_CONF['template']['lang']['memberlist'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['search'] 		= 	$PowerBB->_CONF['template']['lang']['Search_Engine'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['announcement'] 	= 	$PowerBB->_CONF['template']['lang']['announcement'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['team'] 			= 	$PowerBB->_CONF['template']['lang']['Leaders'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['login'] 		    = 	$PowerBB->_CONF['template']['lang']['Login_mem'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['logout'] 		= 	$PowerBB->_CONF['template']['lang']['Guidance_re'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['usercp'] 		= 	$PowerBB->_CONF['template']['lang']['User_Control_Panel'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['pm'] 			= 	$PowerBB->_CONF['template']['lang']['Read_the_message'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['new_topic'] 	= 	$PowerBB->_CONF['template']['lang']['add_new_topic'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['new_reply'] 	= 	$PowerBB->_CONF['template']['lang']['add_new_reply'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['vote'] 			= 	$PowerBB->_CONF['template']['lang']['show_votes'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['online'] 		= 	$PowerBB->_CONF['template']['lang']['online_naw'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['pm_setting'] 		= 	$PowerBB->_CONF['template']['lang']['Settings_Private_Messages'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['warn'] 		       = 	$PowerBB->_CONF['template']['lang']['send_warn'];
		$page_address['pm_setting'] 		= 	$PowerBB->_CONF['template']['lang']['Settings_Private_Messages'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['report'] 		= 	$PowerBB->_CONF['template']['lang']['Report_bad_post'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['register'] 		= 	$PowerBB->_CONF['template']['lang']['register'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['latest_reply'] 		= 	$PowerBB->_CONF['template']['lang']['latest_reply'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['latest'] 		= 	$PowerBB->_CONF['template']['lang']['subject_today'];
		$page_address['special_subject'] 		= 	$PowerBB->_CONF['template']['lang']['Special_Subject'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['calendar'] 		= 	$PowerBB->_CONF['template']['lang']['Calendar'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['page'] 		= 	$PowerBB->_CONF['template']['lang']['Settings_Private_Messages'] .' - '. $PowerBB->_CONF['info_row']['title'];
		$page_address['sitemap'] 		= 	$PowerBB->_CONF['template']['lang']['Sitemap_title'] .' - '. $PowerBB->_CONF['info_row']['title'];
		if (array_key_exists($page,$page_address))
		{
			$title = $page_address[$page];
		}
         $PowerBB->template->assign('title',$title);

 		// Show header template

 		$PowerBB->template->display('headinclud');
 	}

 	/**
 	 * Get the forum's url adress
 	 */
 	function GetForumAdress()
 	{
 		global $PowerBB;

 		$domain_address 	= 	$PowerBB->_SERVER['HTTP_HOST'];
 		$filename 			= 	$PowerBB->_SERVER['REQUEST_URI'];
 		$filename 			= 	explode('/',$filename);

 		$last_part = '';

 		// The programme is under main folder (/var/www)
 		if (sizeof($filename) == 2)
 		{
 			// $last_part = '/';
 		}
 		// The programme is under something like this (/public_html/PowerBB)
 		elseif (sizeof($filename) == 3)
 		{
 			$last_part = '/' . $filename[1];
 		}
 		// The programme is under something like this (/public_html/PowerBB/PowerBB-Again)
 		elseif (sizeof($filename) == 4)
 		{
 			$last_part = '/' . $filename[1] . '/' . $filename[2];
 		}

 		$url = 'http://' . $domain_address . $last_part . '/';

 		return $url;
 	}

 	/**
 	 * Get a strong random code :)
 	 */
 	function RandomCode()
    {
  		$code = rand(1,500) . rand(1,1000) . microtime();
  		$code = ceil($code);

  		$code = base64_encode($code);
  		$code = substr($code,0,15);

  		$code = str_replace('=',rand(1,100),$code);

  		return $code;
 	}

	/**
	 * Just send email for phpmail :)
	 */
 	function mail($to,$subject,$message,$sender)
    {
    	$headers  = "MIME-Version: 1.0\n";
    	$headers .= "Content-type: text/html; charset=utf-8\n";
    	$headers .= "From: <$sender>\n";
    	$headers .= "Reply-To: $sender\n";

        $send = @mail($to, $subject, $message, $headers);

        return ($send) ? true : false;
 	}

	/**
	 * Just send email for smtp :)
	 */
  function send_this_smtp($to, $fromname, $message, $subject, $from)
    {
        global $PowerBB;
		require_once(DIR . 'includes/class.phpmailer.php');
		require_once(DIR . 'includes/class.smtp.php');
		$host = $PowerBB->_CONF['info_row']['smtp_server'];
        $port = $PowerBB->_CONF['info_row']['smtp_port'];
		$mail             = new PHPMailer();
		$header  = "MIME-Version: 1.0\n";
		$header .= "Content-type: text/html; charset=utf-8\n";
		$header .= "From: <$from>\n";
		$header .= "Reply-To: $from\n";
	    // HTML body
	    $body  = "";
	    $body .= "";
	    $body .= $message;
	    // Plain text body (for mail clients that cannot read HTML)
	    $text_body  = "";
	    $text_body .= "";
	    $text_body .= $fromname;
		$mail->IsSMTP();
        $mail->SMTPAuth   = ($PowerBB->_CONF['info_row']['smtp_username'] && $PowerBB->_CONF['info_row']['smtp_password'] ? true : false);
		$mail->SMTPSecure = $PowerBB->_CONF['info_row']['smtp_secure'];

		$mail->Host = $PowerBB->_CONF['info_row']['smtp_server'];  // specify main and backup server
        $mail->Mailer   = "smtp";
		$mail->Port = $PowerBB->_CONF['info_row']['smtp_port'];
        $mail->CharSet = "UTF-8";
		$mail->Username = $PowerBB->_CONF['info_row']['smtp_username'];  // SMTP username
		$mail->Password = $PowerBB->_CONF['info_row']['smtp_password']; // SMTP password
		$mail->From = $from;
		$mail->FromName = $fromname;
		$mail->Subject = $subject;
		$mail->Body = $body;
		$mail->AltBody = $text_body;
		$mail->WordWrap   = 60; // set word wrap
		$mail->MsgHTML($body);
        $mail->AddReplyTo($to,$PowerBB->_CONF['info_row']['title']);
		$mail->AddAddress($to,$PowerBB->_CONF['info_row']['title']);
		$mail->IsHTML(true); // send as HTML
		if(!$mail->Send()) {
          $PowerBB->functions->error("Mailer Error: " . $mail->ErrorInfo);
      	 if ($PowerBB->_CONF['info_row']['ajax_register'])
      	 {
		  $PowerBB->functions->error("Mailer Error: " . $mail->ErrorInfo,$stop,$stop);
         }
		}

        $send = $mail->Send($to, $fromname, $message, $subject, $header);

        return ($send) ? true : false;
 	}



 	function words_count($string,$words_count)
 	{
 	 global $PowerBB;

		$string_arr = explode(" ", $string);

		for($i = sizeof($string_arr); $i >= $words_count; $i--)
		{
		    unset($string_arr[$i]);
		}

		$new_string = implode(" ",$string_arr);

		return ($new_string);
    }

 	function words_count_replace_strip_tags_html2bb($string,$words_count)
 	{
 	 global $PowerBB;

		$string = $PowerBB->Powerparse->replace($string);
		$PowerBB->Powerparse->replace_smiles($string);
		$string = strip_tags($string);

		$string_arr = explode(" ", $string);

		for($i = sizeof($string_arr); $i >= $words_count; $i--)
		{
		    unset($string_arr[$i]);
		}

		$new_string = implode(" ",$string_arr);
		$new_string = $PowerBB->Powerparse->html2bb($new_string);
		$new_string = @htmlspecialchars($new_string);

		return ($new_string);
    }

 	/**
 	 * Check if $adress is true site adress or not
 	 */
 	function IsSite($adress)
 	{
 		return preg_match('~http:\/\/(.*?)~',$adress) ? true : false;
 	}

 	function GetURLExtension($path)
 	{
 		global $PowerBB;

 		$filename = basename($path);

		return $this->GetFileExtension($filename);
 	}

	function date($input)
	{
		global $PowerBB;
		return $PowerBB->sys_functions->date($input);
	}


	function time($time)
	{
		global $PowerBB;

		return $PowerBB->sys_functions->time($time);
	}

	function GetUsernameStyle($username)
	{
		global $PowerBB;

   		// Get username style of last_writer
		$MemberArr 			= 	array();
		$MemberArr['where'] 	= 	array('username',$username);

		$StyleMemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);
        $username = $StyleMemberInfo['username_style_cache'];

		return $username;
	}

	function GetUsernameStyleAndUserId($username)
	{
		global $PowerBB;

   		// Get username style of last_writer
		$MemberArr 			= 	array();
		$MemberArr['where'] 	= 	array('username',$username);

		$StyleMemberInfo = $PowerBB->member->GetMemberInfo($MemberArr);
        $username = $StyleMemberInfo['username_style_cache'];
        $user_id = $StyleMemberInfo['id'];

		if ($PowerBB->_CONF['info_row']['rewriterule'] == '1')
		{
		return $username = '<a href="u' . $user_id . '.html">' . $username . '</a> ';
		}
		else
		{
		return $username = '<a href="index.php?page=profile&amp;show=1&amp;id=' . $user_id . '">' . $username . '</a> ';
		}
	}

	function GetwriterGroupStyle($Username,$Group)
	{
		global $PowerBB;

   		// Get username style of writer
		$MemberGroupArr 			= 	array();
		$MemberGroupArr['where'] 	= 	array('id',$Group);

		$GroupInfo = $PowerBB->group->GetGroupInfo($MemberGroupArr);

         $style = $GroupInfo['username_style'];
		$username_style_cache = str_replace('[username]',$Username,$style);

		return $username_style_cache;
	}

	function GetEditorTools()
	{
		global $PowerBB;

		if (!is_object($PowerBB->icon))
		{
			trigger_error('ERROR::ICON_OBJECT_DID_NOT_FOUND',E_USER_ERROR);
		}

		$SmlArr 					= 	array();
		$SmlArr['order'] 			=	array();
		$SmlArr['order']['field']	=	'id';
		$SmlArr['order']['type']	=	'ASC';
		$SmlArr['limit']			=	$PowerBB->_CONF['info_row']['smiles_nm'];
		$SmlArr['proc'] 			= 	array();
		$SmlArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['SmileRows'] = $PowerBB->icon->GetSmileList($SmlArr);

		$IcnArr 					= 	array();
		$IcnArr['order'] 			=	array();
		$IcnArr['order']['field']	=	'id';
		$IcnArr['order']['type']	=	'DESC';
		$IcnArr['limit']			=	$PowerBB->_CONF['info_row']['icns_nm'];
		$IcnArr['proc'] 			= 	array();
		$IcnArr['proc']['*'] 		= 	array('method'=>'clean','param'=>'html');

		$PowerBB->_CONF['template']['while']['IconRows'] = $PowerBB->icon->GetIconList($IcnArr);
		$GetIconsNumber = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT smile_type FROM " . $PowerBB->table['smiles'] . " WHERE smile_type = '1'"));
		$PowerBB->template->assign('GetIconsNumber',$GetIconsNumber);


	}

	function ModeratorCheck($section_id,$username = null)
	{
		global $PowerBB;

		$Mod = false;

		if ($PowerBB->_CONF['member_permission'])
		{
			if ($PowerBB->_CONF['member_row']['usergroup'] == '1'
				or $PowerBB->_CONF['group_info']['vice'])
			{
				$Mod = true;
			}
			else
			{
				$ModArr = array();

				if ($username == null)
				{
					$ModArr['username'] = $PowerBB->_CONF['member_row']['username'];
				}
				else
				{
					$ModArr['username'] = $username;
				}

				$ModArr['section_id']	=	$section_id;

				$Mod = $PowerBB->moderator->IsModerator($ModArr);
			}

		}

		return $Mod;
	}

	/**
	 * dont Show error massege jast stop script
	 */
 	function error_stop($no_header = false,$no_style = false)
    {
    	global $PowerBB;

    	if (!$no_header and $no_style)
    	{
    		//$this->ShowHeader();
    	}

  		$this->stop($no_style);
 	}

	/**
	 * Show error massege and stop script
	 */
 	function stop_no_foot($no_style = false)
 	{
 		global $PowerBB;

 		if (!$no_style)
 		{
 			//$PowerBB->template->display('footer');
 		}

 		exit();
 	}

 	function error_no_foot($msg,$no_header = false,$no_style = false)
    {
    	global $PowerBB;

    	if (!$no_header and $no_style)
    	{
    		$this->ShowHeader();
    	}

  		$this->msg($msg,$no_style);
  		$this->stop_no_foot($no_style);
 	}

 	function GetTimezoneSet($timezone)
 	{
 		global $PowerBB;

		// Force PHP 5.3.0+ to take time zone information from OS
		if ($PowerBB->_CONF['info_row']['timeoffset'] != '')
		{
		// Get time zone
		@date_default_timezone_set($timezone);
		}

       return $timezone;
 	}

 	function GetDisplayForums()
 	{
 		global $PowerBB;
        $display_forums_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['section'] . "  WHERE sec_section<>0"));
	   if ($display_forums_nm == '0')
	   {
		$update = $PowerBB->DB->sql_query("UPDATE " . $PowerBB->table['info'] . " SET value='0' WHERE var_name='last_subject_writer_not_in'");
       }
       else
	   {
         $DisplaSectionInfo = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['section'] . " WHERE sec_section<>0");
		  while ($display_forumsInfo = $PowerBB->DB->sql_fetch_array($DisplaSectionInfo))
	       {
		         $display_forums .= '0,'.$display_forumsInfo['id']. ',0';
				$update .= $PowerBB->DB->sql_query("UPDATE " . $PowerBB->table['info'] . " SET value='$display_forums' WHERE var_name='last_subject_writer_not_in'");
	      }
       }
		$PowerBB->_CONF['info_row']['last_subject_writer_not_in'] = str_ireplace('00,','',$PowerBB->_CONF['info_row']['last_subject_writer_not_in']);

 	}

	function is_bot()
	{
 		global $PowerBB;
	$agent = $PowerBB->_SERVER["HTTP_USER_AGENT"];
	 $list_agent=array("Googlebot" , "Yahoo!" , "msnbot" , "Googlebot-Image" , "Gaisbot" , "GalaxyBot" , "msnbot" , "Rambler" , "AbachoBOT" , "accoona" , "AcoiRobot" , "ASPSeek" , "CrocCrawler" , "Dumbot" , "FAST-WebCrawler" , "GeonaBot" , "Lycos" , "MSRBOT" , "Scooter" , "AltaVista" , "IDBot" , "eStyle" , "Scrubby");
	for ($i=0;$i<count($list_agent);$i++){
	if (strstr($agent,$list_agent[$i])){return 1;}}return 0;
	}

	function bot_name()
	{
 		global $PowerBB;
	 $agent = $PowerBB->_SERVER["HTTP_USER_AGENT"];
	 $list_agent=array("Googlebot" , "Yahoo!" , "msnbot" , "Googlebot-Image" , "Gaisbot" , "GalaxyBot" , "msnbot" , "Rambler" , "AbachoBOT" , "accoona" , "AcoiRobot" , "ASPSeek" , "CrocCrawler" , "Dumbot" , "FAST-WebCrawler" , "GeonaBot" , "Lycos" , "MSRBOT" , "Scooter" , "AltaVista" , "IDBot" , "eStyle" , "Scrubby");
	for ($i=0;$i<count($list_agent);$i++){
	if (strstr($agent,$list_agent[$i])){return $list_agent[$i];}}return;
	}

 	/**
 	 *Update Section Cache ;)
 	 */
	function UpdateSectionCache($SectionCache)
 	{
     global $PowerBB;

		// The number of section's subjects number
		$reply_num = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['reply'] . " WHERE section = '$SectionCache' "));

		$UpdateArr 					= 	array();
		$UpdateArr['field']			=	array();
		$UpdateArr['field']['reply_num'] 	= 	$reply_num;
		$UpdateArr['where']					= 	array('id',$SectionCache);

		$UpdateReplyNumber = $PowerBB->section->UpdateSection($UpdateArr);


		$PowerBB->cache->UpdateReplyNumber(array('reply_num'	=>	$reply_num));


		$subject_nm = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT id FROM " . $PowerBB->table['subject'] . " WHERE section = '$SectionCache' "));

		// The number of section's subjects number
		$UpdateArr 					= 	array();
		$UpdateArr['field']			=	array();

		$UpdateArr['field']['subject_num'] 	= 	$subject_nm;
		$UpdateArr['where']					= 	array('id',$SectionCache);

		$UpdateSubjectNumber = $PowerBB->section->UpdateSection($UpdateArr);


		$PowerBB->cache->UpdateSubjectNumber(array('subject_num'	=>	$subject_nm));

		$GetLastqueryReplyForm = $PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['subject'] . " WHERE section = '$SectionCache' AND delete_topic<>1 AND review_subject<>1 ORDER by write_time DESC");
		$GetLastReplyForm = $PowerBB->DB->sql_fetch_array($GetLastqueryReplyForm);
         if ($GetLastReplyForm['last_replier'] !='')
         {
         	$last_writer = $GetLastReplyForm['last_replier'];
         }
		else
		{
         	$last_writer = $GetLastReplyForm['writer'];
        }
		if (!$GetLastReplyForm)
		{
		// Update Last subject's information in Section Form
		$UpdateLastFormSecArr = array();
		$UpdateLastFormSecArr['field']			=	array();

		$UpdateLastFormSecArr['field']['last_writer'] 		= 	'';
		$UpdateLastFormSecArr['field']['last_subject'] 		= 	'';
		$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	'';
		$UpdateLastFormSecArr['field']['last_date'] 	= 	'';
		$UpdateLastFormSecArr['field']['last_time'] 	= 	'';
		$UpdateLastFormSecArr['field']['icon'] 		    = 	'';
		$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
		$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

		$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

		// Update Last Form Sec subject's information
		$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
		}
		else
		{

		// Update Last subject's information in Section Form
		$UpdateLastFormSecArr = array();
		$UpdateLastFormSecArr['field']			=	array();

		$UpdateLastFormSecArr['field']['last_writer'] 		= 	$last_writer;
		$UpdateLastFormSecArr['field']['last_subject'] 		= 	$GetLastReplyForm['title'];
		$UpdateLastFormSecArr['field']['last_subjectid'] 	= 	$GetLastReplyForm['id'];
		$UpdateLastFormSecArr['field']['last_date'] 	= 	$GetLastReplyForm['write_time'];
		$UpdateLastFormSecArr['field']['last_time'] 	= 	$GetLastReplyForm['write_time'];
		$UpdateLastFormSecArr['field']['icon'] 		    = 	$GetLastReplyForm['icon'];
		$UpdateLastFormSecArr['field']['last_reply'] 	= 	0;
		$UpdateLastFormSecArr['field']['last_berpage_nm']  = 	0;

		$UpdateLastFormSecArr['where'] 		        = 	array('id',$SectionCache);

		// Update Last Form Sec subject's information
		$UpdateLastFormSec = $PowerBB->section->UpdateSection($UpdateLastFormSecArr);
	   }
		// Get Section Info
		$SecArr 			= 	array();
		$SecArr['where'] 	= 	array('id',$SectionCache);

		$this->SectionInfo = $PowerBB->section->GetSectionInfo($SecArr);

		// Update section's cache
		$UpdateArr 				= 	array();
		$UpdateArr['parent'] 	= 	$this->SectionInfo['parent'];

		$update_cache = $PowerBB->section->UpdateSectionsCache($UpdateArr);


		unset($UpdateArr);



       return;

 	}

	function form_simple_input($name, $value="", $size='5', $color='colors')
	{
		return "<input type='text' name='$name' value='$value' size='$size' class='$color' dir='ltr'>";
	}

	function start_form($hiddens="", $name='theAdminForm', $js="", $id="")
	{
     global $PowerBB;

		if ( ! $id )
		{
			$id = $name;
		}

		$form = "<form action=".$this->GetForumAdress()." method='post' name='$name' $js id='$id'>";

		if (is_array($hiddens))
		{
			foreach ($hiddens as $v)
			{
				$form .= "\n<input type='hidden' name='{$v[0]}' value='{$v[1]}' />";
			}
		}

		//-----------------------------------------
		// Add in auth key
		//-----------------------------------------

		$form .= "\n<input type='hidden' name='_admin_auth_key' value='".$PowerBB->_POST['_admin_auth_key']."' />";

		return $form;
	}


	function check_number($val)
	 {
	  // اذا كان ناتج القسمة هو صفر فأكيد إذا هو عدد زوجي
	  if( $val % 2 == 0 )
	    {
	    // العدد زوجي
	    return true;
	    }
	    else
	    {
	    return false;
	    }
	 }



}

class PowerBBAdminFunctions extends PowerBBFunctions
{
 	//
}


?>
