Profile-Modify.php 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782
  1. <?php
  2. /**
  3. * This file has the primary job of showing and editing people's profiles.
  4. * It also allows the user to change some of their or another's preferences,
  5. * and such things
  6. *
  7. * Simple Machines Forum (SMF)
  8. *
  9. * @package SMF
  10. * @author Simple Machines http://www.simplemachines.org
  11. * @copyright 2014 Simple Machines and individual contributors
  12. * @license http://www.simplemachines.org/about/smf/license.php BSD
  13. *
  14. * @version 2.1 Alpha 1
  15. */
  16. if (!defined('SMF'))
  17. die('No direct access...');
  18. /**
  19. * This defines every profile field known to man.
  20. *
  21. * @param bool $force_reload = false
  22. */
  23. function loadProfileFields($force_reload = false)
  24. {
  25. global $context, $profile_fields, $txt, $scripturl, $modSettings, $user_info, $old_profile, $smcFunc, $cur_profile, $language;
  26. // Don't load this twice!
  27. if (!empty($profile_fields) && !$force_reload)
  28. return;
  29. /* This horrific array defines all the profile fields in the whole world!
  30. In general each "field" has one array - the key of which is the database column name associated with said field. Each item
  31. can have the following attributes:
  32. string $type: The type of field this is - valid types are:
  33. - callback: This is a field which has its own callback mechanism for templating.
  34. - check: A simple checkbox.
  35. - hidden: This doesn't have any visual aspects but may have some validity.
  36. - password: A password box.
  37. - select: A select box.
  38. - text: A string of some description.
  39. string $label: The label for this item - default will be $txt[$key] if this isn't set.
  40. string $subtext: The subtext (Small label) for this item.
  41. int $size: Optional size for a text area.
  42. array $input_attr: An array of text strings to be added to the input box for this item.
  43. string $value: The value of the item. If not set $cur_profile[$key] is assumed.
  44. string $permission: Permission required for this item (Excluded _any/_own subfix which is applied automatically).
  45. function $input_validate: A runtime function which validates the element before going to the database. It is passed
  46. the relevant $_POST element if it exists and should be treated like a reference.
  47. Return types:
  48. - true: Element can be stored.
  49. - false: Skip this element.
  50. - a text string: An error occured - this is the error message.
  51. function $preload: A function that is used to load data required for this element to be displayed. Must return
  52. true to be displayed at all.
  53. string $cast_type: If set casts the element to a certain type. Valid types (bool, int, float).
  54. string $save_key: If the index of this element isn't the database column name it can be overriden
  55. with this string.
  56. bool $is_dummy: If set then nothing is acted upon for this element.
  57. bool $enabled: A test to determine whether this is even available - if not is unset.
  58. string $link_with: Key which links this field to an overall set.
  59. Note that all elements that have a custom input_validate must ensure they set the value of $cur_profile correct to enable
  60. the changes to be displayed correctly on submit of the form.
  61. */
  62. $profile_fields = array(
  63. 'avatar_choice' => array(
  64. 'type' => 'callback',
  65. 'callback_func' => 'avatar_select',
  66. // This handles the permissions too.
  67. 'preload' => 'profileLoadAvatarData',
  68. 'input_validate' => 'profileSaveAvatarData',
  69. 'save_key' => 'avatar',
  70. ),
  71. 'bday1' => array(
  72. 'type' => 'callback',
  73. 'callback_func' => 'birthdate',
  74. 'permission' => 'profile_extra',
  75. 'preload' => create_function('', '
  76. global $cur_profile, $context;
  77. // Split up the birthdate....
  78. list ($uyear, $umonth, $uday) = explode(\'-\', empty($cur_profile[\'birthdate\']) || $cur_profile[\'birthdate\'] == \'0001-01-01\' ? \'0000-00-00\' : $cur_profile[\'birthdate\']);
  79. $context[\'member\'][\'birth_date\'] = array(
  80. \'year\' => $uyear == \'0004\' ? \'0000\' : $uyear,
  81. \'month\' => $umonth,
  82. \'day\' => $uday,
  83. );
  84. return true;
  85. '),
  86. 'input_validate' => create_function('&$value', '
  87. global $profile_vars, $cur_profile;
  88. if (isset($_POST[\'bday2\'], $_POST[\'bday3\']) && $value > 0 && $_POST[\'bday2\'] > 0)
  89. {
  90. // Set to blank?
  91. if ((int) $_POST[\'bday3\'] == 1 && (int) $_POST[\'bday2\'] == 1 && (int) $value == 1)
  92. $value = \'0001-01-01\';
  93. else
  94. $value = checkdate($value, $_POST[\'bday2\'], $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\']) ? sprintf(\'%04d-%02d-%02d\', $_POST[\'bday3\'] < 4 ? 4 : $_POST[\'bday3\'], $_POST[\'bday1\'], $_POST[\'bday2\']) : \'0001-01-01\';
  95. }
  96. else
  97. $value = \'0001-01-01\';
  98. $profile_vars[\'birthdate\'] = $value;
  99. $cur_profile[\'birthdate\'] = $value;
  100. return false;
  101. '),
  102. ),
  103. // Setting the birthdate the old style way?
  104. 'birthdate' => array(
  105. 'type' => 'hidden',
  106. 'permission' => 'profile_extra',
  107. 'input_validate' => create_function('&$value', '
  108. global $cur_profile;
  109. // @todo Should we check for this year and tell them they made a mistake :P? (based on coppa at least?)
  110. if (preg_match(\'/(\d{4})[\-\., ](\d{2})[\-\., ](\d{2})/\', $value, $dates) === 1)
  111. {
  112. $value = checkdate($dates[2], $dates[3], $dates[1] < 4 ? 4 : $dates[1]) ? sprintf(\'%04d-%02d-%02d\', $dates[1] < 4 ? 4 : $dates[1], $dates[2], $dates[3]) : \'0001-01-01\';
  113. return true;
  114. }
  115. else
  116. {
  117. $value = empty($cur_profile[\'birthdate\']) ? \'0001-01-01\' : $cur_profile[\'birthdate\'];
  118. return false;
  119. }
  120. '),
  121. ),
  122. 'date_registered' => array(
  123. 'type' => 'date',
  124. 'value' => empty($cur_profile['date_registered']) ? $txt['not_applicable'] : strftime('%Y-%m-%d', $cur_profile['date_registered'] + ($user_info['time_offset'] + $modSettings['time_offset']) * 3600),
  125. 'label' => $txt['date_registered'],
  126. 'log_change' => true,
  127. 'permission' => 'moderate_forum',
  128. 'input_validate' => create_function('&$value', '
  129. global $txt, $user_info, $modSettings, $cur_profile, $context;
  130. // Bad date! Go try again - please?
  131. if (($value = strtotime($value)) === -1)
  132. {
  133. $value = $cur_profile[\'date_registered\'];
  134. return $txt[\'invalid_registration\'] . \' \' . strftime(\'%d %b %Y \' . (strpos($user_info[\'time_format\'], \'%H\') !== false ? \'%I:%M:%S %p\' : \'%H:%M:%S\'), forum_time(false));
  135. }
  136. // As long as it doesn\'t equal "N/A"...
  137. elseif ($value != $txt[\'not_applicable\'] && $value != strtotime(strftime(\'%Y-%m-%d\', $cur_profile[\'date_registered\'] + ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600)))
  138. $value = $value - ($user_info[\'time_offset\'] + $modSettings[\'time_offset\']) * 3600;
  139. else
  140. $value = $cur_profile[\'date_registered\'];
  141. return true;
  142. '),
  143. ),
  144. 'email_address' => array(
  145. 'type' => 'email',
  146. 'label' => $txt['user_email_address'],
  147. 'subtext' => $txt['valid_email'],
  148. 'log_change' => true,
  149. 'permission' => 'profile_password',
  150. 'js_submit' => !empty($modSettings['send_validation_onChange']) ? '
  151. form_handle.addEventListener(\'submit\', function(event)
  152. {
  153. if (this.email_address.value != "'. $cur_profile['email_address'] .'")
  154. {
  155. alert('. JavaScriptEscape($txt['email_change_logout']) .');
  156. return true;
  157. }
  158. }, false);' : '',
  159. 'input_validate' => create_function('&$value', '
  160. global $context, $old_profile, $profile_vars, $sourcedir, $modSettings;
  161. if (strtolower($value) == strtolower($old_profile[\'email_address\']))
  162. return false;
  163. $isValid = profileValidateEmail($value, $context[\'id_member\']);
  164. // Do they need to revalidate? If so schedule the function!
  165. if ($isValid === true && !empty($modSettings[\'send_validation_onChange\']) && !allowedTo(\'moderate_forum\'))
  166. {
  167. require_once($sourcedir . \'/Subs-Members.php\');
  168. $profile_vars[\'validation_code\'] = generateValidationCode();
  169. $profile_vars[\'is_activated\'] = 2;
  170. $context[\'profile_execute_on_save\'][] = \'profileSendActivation\';
  171. unset($context[\'profile_execute_on_save\'][\'reload_user\']);
  172. }
  173. return $isValid;
  174. '),
  175. ),
  176. 'hide_email' => array(
  177. 'type' => 'check',
  178. 'value' => empty($cur_profile['hide_email']) ? true : false,
  179. 'label' => $txt['allow_user_email'],
  180. 'permission' => 'profile_identity',
  181. 'input_validate' => create_function('&$value', '
  182. $value = $value == 0 ? 1 : 0;
  183. return true;
  184. '),
  185. ),
  186. // Selecting group membership is a complicated one so we treat it separate!
  187. 'id_group' => array(
  188. 'type' => 'callback',
  189. 'callback_func' => 'group_manage',
  190. 'permission' => 'manage_membergroups',
  191. 'preload' => 'profileLoadGroups',
  192. 'log_change' => true,
  193. 'input_validate' => 'profileSaveGroups',
  194. ),
  195. 'id_theme' => array(
  196. 'type' => 'callback',
  197. 'callback_func' => 'theme_pick',
  198. 'permission' => 'profile_extra',
  199. 'enabled' => $modSettings['theme_allow'] || allowedTo('admin_forum'),
  200. 'preload' => create_function('', '
  201. global $smcFunc, $context, $cur_profile, $txt;
  202. $request = $smcFunc[\'db_query\'](\'\', \'
  203. SELECT value
  204. FROM {db_prefix}themes
  205. WHERE id_theme = {int:id_theme}
  206. AND variable = {string:variable}
  207. LIMIT 1\', array(
  208. \'id_theme\' => $cur_profile[\'id_theme\'],
  209. \'variable\' => \'name\',
  210. )
  211. );
  212. list ($name) = $smcFunc[\'db_fetch_row\']($request);
  213. $smcFunc[\'db_free_result\']($request);
  214. $context[\'member\'][\'theme\'] = array(
  215. \'id\' => $cur_profile[\'id_theme\'],
  216. \'name\' => empty($cur_profile[\'id_theme\']) ? $txt[\'theme_forum_default\'] : $name
  217. );
  218. return true;
  219. '),
  220. 'input_validate' => create_function('&$value', '
  221. $value = (int) $value;
  222. return true;
  223. '),
  224. ),
  225. 'karma_good' => array(
  226. 'type' => 'callback',
  227. 'callback_func' => 'karma_modify',
  228. 'permission' => 'admin_forum',
  229. // Set karma_bad too!
  230. 'input_validate' => create_function('&$value', '
  231. global $profile_vars, $cur_profile;
  232. $value = (int) $value;
  233. if (isset($_POST[\'karma_bad\']))
  234. {
  235. $profile_vars[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  236. $cur_profile[\'karma_bad\'] = $_POST[\'karma_bad\'] != \'\' ? (int) $_POST[\'karma_bad\'] : 0;
  237. }
  238. return true;
  239. '),
  240. 'preload' => create_function('', '
  241. global $context, $cur_profile;
  242. $context[\'member\'][\'karma\'][\'good\'] = $cur_profile[\'karma_good\'];
  243. $context[\'member\'][\'karma\'][\'bad\'] = $cur_profile[\'karma_bad\'];
  244. return true;
  245. '),
  246. 'enabled' => !empty($modSettings['karmaMode']),
  247. ),
  248. 'lngfile' => array(
  249. 'type' => 'select',
  250. 'options' => 'return $context[\'profile_languages\'];',
  251. 'label' => $txt['preferred_language'],
  252. 'permission' => 'profile_identity',
  253. 'preload' => 'profileLoadLanguages',
  254. 'enabled' => !empty($modSettings['userLanguage']),
  255. 'value' => empty($cur_profile['lngfile']) ? $language : $cur_profile['lngfile'],
  256. 'input_validate' => create_function('&$value', '
  257. global $context, $cur_profile;
  258. // Load the languages.
  259. profileLoadLanguages();
  260. if (isset($context[\'profile_languages\'][$value]))
  261. {
  262. if ($context[\'user\'][\'is_owner\'] && empty($context[\'password_auth_failed\']))
  263. $_SESSION[\'language\'] = $value;
  264. return true;
  265. }
  266. else
  267. {
  268. $value = $cur_profile[\'lngfile\'];
  269. return false;
  270. }
  271. '),
  272. ),
  273. // The username is not always editable - so adjust it as such.
  274. 'member_name' => array(
  275. 'type' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? 'text' : 'label',
  276. 'label' => $txt['username'],
  277. 'subtext' => allowedTo('admin_forum') && !isset($_GET['changeusername']) ? '[<a href="' . $scripturl . '?action=profile;u=' . $context['id_member'] . ';area=account;changeusername" style="font-style: italic;">' . $txt['username_change'] . '</a>]' : '',
  278. 'log_change' => true,
  279. 'permission' => 'profile_identity',
  280. 'prehtml' => allowedTo('admin_forum') && isset($_GET['changeusername']) ? '<div class="alert">' . $txt['username_warning'] . '</div>' : '',
  281. 'input_validate' => create_function('&$value', '
  282. global $sourcedir, $context, $user_info, $cur_profile;
  283. if (allowedTo(\'admin_forum\'))
  284. {
  285. // We\'ll need this...
  286. require_once($sourcedir . \'/Subs-Auth.php\');
  287. // Maybe they are trying to change their password as well?
  288. $resetPassword = true;
  289. if (isset($_POST[\'passwrd1\']) && $_POST[\'passwrd1\'] != \'\' && isset($_POST[\'passwrd2\']) && $_POST[\'passwrd1\'] == $_POST[\'passwrd2\'] && validatePassword($_POST[\'passwrd1\'], $value, array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\'])) == null)
  290. $resetPassword = false;
  291. // Do the reset... this will send them an email too.
  292. if ($resetPassword)
  293. resetPassword($context[\'id_member\'], $value);
  294. elseif ($value !== null)
  295. {
  296. validateUsername($context[\'id_member\'], trim(preg_replace(\'~[\t\n\r \x0B\0\' . ($context[\'utf8\'] ? \'\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}\' : \'\x00-\x08\x0B\x0C\x0E-\x19\xA0\') . \']+~\' . ($context[\'utf8\'] ? \'u\' : \'\'), \' \', $value)));
  297. updateMemberData($context[\'id_member\'], array(\'member_name\' => $value));
  298. }
  299. }
  300. return false;
  301. '),
  302. ),
  303. 'passwrd1' => array(
  304. 'type' => 'password',
  305. 'label' => ucwords($txt['choose_pass']),
  306. 'subtext' => $txt['password_strength'],
  307. 'size' => 20,
  308. 'value' => '',
  309. 'enabled' => empty($cur_profile['openid_uri']),
  310. 'permission' => 'profile_password',
  311. 'save_key' => 'passwd',
  312. // Note this will only work if passwrd2 also exists!
  313. 'input_validate' => create_function('&$value', '
  314. global $sourcedir, $user_info, $smcFunc, $cur_profile;
  315. // If we didn\'t try it then ignore it!
  316. if ($value == \'\')
  317. return false;
  318. // Do the two entries for the password even match?
  319. if (!isset($_POST[\'passwrd2\']) || $value != $_POST[\'passwrd2\'])
  320. return \'bad_new_password\';
  321. // Let\'s get the validation function into play...
  322. require_once($sourcedir . \'/Subs-Auth.php\');
  323. $passwordErrors = validatePassword($value, $cur_profile[\'member_name\'], array($cur_profile[\'real_name\'], $user_info[\'username\'], $user_info[\'name\'], $user_info[\'email\']));
  324. // Were there errors?
  325. if ($passwordErrors != null)
  326. return \'password_\' . $passwordErrors;
  327. // Set up the new password variable... ready for storage.
  328. $value = sha1(strtolower($cur_profile[\'member_name\']) . un_htmlspecialchars($value));
  329. return true;
  330. '),
  331. ),
  332. 'passwrd2' => array(
  333. 'type' => 'password',
  334. 'label' => ucwords($txt['verify_pass']),
  335. 'enabled' => empty($cur_profile['openid_uri']),
  336. 'size' => 20,
  337. 'value' => '',
  338. 'permission' => 'profile_password',
  339. 'is_dummy' => true,
  340. ),
  341. 'personal_text' => array(
  342. 'type' => 'text',
  343. 'label' => $txt['personal_text'],
  344. 'log_change' => true,
  345. 'input_attr' => array('maxlength="50"'),
  346. 'size' => 50,
  347. 'permission' => 'profile_blurb',
  348. 'input_validate' => create_function('&$value', '
  349. global $smcFunc;
  350. if ($smcFunc[\'strlen\']($value) > 50)
  351. return \'personal_text_too_long\';
  352. return true;
  353. '),
  354. ),
  355. // This does ALL the pm settings
  356. 'pm_prefs' => array(
  357. 'type' => 'callback',
  358. 'callback_func' => 'pm_settings',
  359. 'permission' => 'pm_read',
  360. 'preload' => create_function('', '
  361. global $context, $cur_profile;
  362. $context[\'display_mode\'] = $cur_profile[\'pm_prefs\'] & 3;
  363. $context[\'send_email\'] = $cur_profile[\'pm_email_notify\'];
  364. $context[\'receive_from\'] = !empty($cur_profile[\'pm_receive_from\']) ? $cur_profile[\'pm_receive_from\'] : 0;
  365. return true;
  366. '),
  367. 'input_validate' => create_function('&$value', '
  368. global $cur_profile, $profile_vars;
  369. // Simple validate and apply the two "sub settings"
  370. $value = max(min($value, 2), 0);
  371. $cur_profile[\'pm_email_notify\'] = $profile_vars[\'pm_email_notify\'] = max(min((int) $_POST[\'pm_email_notify\'], 2), 0);
  372. $cur_profile[\'pm_receive_from\'] = $profile_vars[\'pm_receive_from\'] = max(min((int) $_POST[\'pm_receive_from\'], 4), 0);
  373. return true;
  374. '),
  375. ),
  376. 'posts' => array(
  377. 'type' => 'int',
  378. 'label' => $txt['profile_posts'],
  379. 'log_change' => true,
  380. 'size' => 7,
  381. 'permission' => 'moderate_forum',
  382. 'input_validate' => create_function('&$value', '
  383. if (!is_numeric($value))
  384. return \'digits_only\';
  385. else
  386. $value = $value != \'\' ? strtr($value, array(\',\' => \'\', \'.\' => \'\', \' \' => \'\')) : 0;
  387. return true;
  388. '),
  389. ),
  390. 'real_name' => array(
  391. 'type' => allowedTo('profile_displayed_name_own') || allowedTo('profile_displayed_name_any') || allowedTo('moderate_forum') ? 'text' : 'label',
  392. 'label' => $txt['name'],
  393. 'subtext' => $txt['display_name_desc'],
  394. 'log_change' => true,
  395. 'input_attr' => array('maxlength="60"'),
  396. 'permission' => 'profile_displayed_name',
  397. 'enabled' => allowedTo('profile_displayed_name_own') || allowedTo('profile_displayed_name_any') || allowedTo('moderate_forum'),
  398. 'input_validate' => create_function('&$value', '
  399. global $context, $smcFunc, $sourcedir, $cur_profile;
  400. $value = trim(preg_replace(\'~[\t\n\r \x0B\0\' . ($context[\'utf8\'] ? \'\x{A0}\x{AD}\x{2000}-\x{200F}\x{201F}\x{202F}\x{3000}\x{FEFF}\' : \'\x00-\x08\x0B\x0C\x0E-\x19\xA0\') . \']+~\' . ($context[\'utf8\'] ? \'u\' : \'\'), \' \', $value));
  401. if (trim($value) == \'\')
  402. return \'no_name\';
  403. elseif ($smcFunc[\'strlen\']($value) > 60)
  404. return \'name_too_long\';
  405. elseif ($cur_profile[\'real_name\'] != $value)
  406. {
  407. require_once($sourcedir . \'/Subs-Members.php\');
  408. if (isReservedName($value, $context[\'id_member\']))
  409. return \'name_taken\';
  410. }
  411. return true;
  412. '),
  413. ),
  414. 'secret_question' => array(
  415. 'type' => 'text',
  416. 'label' => $txt['secret_question'],
  417. 'subtext' => $txt['secret_desc'],
  418. 'size' => 50,
  419. 'permission' => 'profile_password',
  420. ),
  421. 'secret_answer' => array(
  422. 'type' => 'text',
  423. 'label' => $txt['secret_answer'],
  424. 'subtext' => $txt['secret_desc2'],
  425. 'size' => 20,
  426. 'postinput' => '<span class="smalltext" style="margin-left: 4ex;">[<a href="' . $scripturl . '?action=helpadmin;help=secret_why_blank" onclick="return reqOverlayDiv(this.href);">' . $txt['secret_why_blank'] . '</a>]</span>',
  427. 'value' => '',
  428. 'permission' => 'profile_password',
  429. 'input_validate' => create_function('&$value', '
  430. $value = $value != \'\' ? md5($value) : \'\';
  431. return true;
  432. '),
  433. ),
  434. 'signature' => array(
  435. 'type' => 'callback',
  436. 'callback_func' => 'signature_modify',
  437. 'permission' => 'profile_signature',
  438. 'enabled' => substr($modSettings['signature_settings'], 0, 1) == 1,
  439. 'preload' => 'profileLoadSignatureData',
  440. 'input_validate' => 'profileValidateSignature',
  441. ),
  442. 'show_online' => array(
  443. 'type' => 'check',
  444. 'label' => $txt['show_online'],
  445. 'permission' => 'profile_identity',
  446. 'enabled' => !empty($modSettings['allow_hideOnline']) || allowedTo('moderate_forum'),
  447. ),
  448. 'smiley_set' => array(
  449. 'type' => 'callback',
  450. 'callback_func' => 'smiley_pick',
  451. 'enabled' => !empty($modSettings['smiley_sets_enable']),
  452. 'permission' => 'profile_extra',
  453. 'preload' => create_function('', '
  454. global $modSettings, $context, $txt, $cur_profile, $smcFunc;
  455. $context[\'member\'][\'smiley_set\'][\'id\'] = empty($cur_profile[\'smiley_set\']) ? \'\' : $cur_profile[\'smiley_set\'];
  456. $context[\'smiley_sets\'] = explode(\',\', \'none,,\' . $modSettings[\'smiley_sets_known\']);
  457. $set_names = explode("\n", $txt[\'smileys_none\'] . "\n" . $txt[\'smileys_forum_board_default\'] . "\n" . $modSettings[\'smiley_sets_names\']);
  458. foreach ($context[\'smiley_sets\'] as $i => $set)
  459. {
  460. $context[\'smiley_sets\'][$i] = array(
  461. \'id\' => $smcFunc[\'htmlspecialchars\']($set),
  462. \'name\' => $smcFunc[\'htmlspecialchars\']($set_names[$i]),
  463. \'selected\' => $set == $context[\'member\'][\'smiley_set\'][\'id\']
  464. );
  465. if ($context[\'smiley_sets\'][$i][\'selected\'])
  466. $context[\'member\'][\'smiley_set\'][\'name\'] = $set_names[$i];
  467. }
  468. return true;
  469. '),
  470. 'input_validate' => create_function('&$value', '
  471. global $modSettings;
  472. $smiley_sets = explode(\',\', $modSettings[\'smiley_sets_known\']);
  473. if (!in_array($value, $smiley_sets) && $value != \'none\')
  474. $value = \'\';
  475. return true;
  476. '),
  477. ),
  478. // Pretty much a dummy entry - it populates all the theme settings.
  479. 'theme_settings' => array(
  480. 'type' => 'callback',
  481. 'callback_func' => 'theme_settings',
  482. 'permission' => 'profile_extra',
  483. 'is_dummy' => true,
  484. 'preload' => create_function('', '
  485. global $context, $user_info, $modSettings;
  486. loadLanguage(\'Settings\');
  487. $context[\'allow_no_censored\'] = false;
  488. if ($user_info[\'is_admin\'] || $context[\'user\'][\'is_owner\'])
  489. $context[\'allow_no_censored\'] = !empty($modSettings[\'allow_no_censored\']);
  490. return true;
  491. '),
  492. ),
  493. 'time_format' => array(
  494. 'type' => 'callback',
  495. 'callback_func' => 'timeformat_modify',
  496. 'permission' => 'profile_extra',
  497. 'preload' => create_function('', '
  498. global $context, $user_info, $txt, $cur_profile, $modSettings;
  499. $context[\'easy_timeformats\'] = array(
  500. array(\'format\' => \'\', \'title\' => $txt[\'timeformat_default\']),
  501. array(\'format\' => \'%B %d, %Y, %I:%M:%S %p\', \'title\' => $txt[\'timeformat_easy1\']),
  502. array(\'format\' => \'%B %d, %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy2\']),
  503. array(\'format\' => \'%Y-%m-%d, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy3\']),
  504. array(\'format\' => \'%d %B %Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy4\']),
  505. array(\'format\' => \'%d-%m-%Y, %H:%M:%S\', \'title\' => $txt[\'timeformat_easy5\'])
  506. );
  507. $context[\'member\'][\'time_format\'] = $cur_profile[\'time_format\'];
  508. $context[\'current_forum_time\'] = timeformat(time() - $user_info[\'time_offset\'] * 3600, false);
  509. $context[\'current_forum_time_js\'] = strftime(\'%Y,\' . ((int) strftime(\'%m\', time() + $modSettings[\'time_offset\'] * 3600) - 1) . \',%d,%H,%M,%S\', time() + $modSettings[\'time_offset\'] * 3600);
  510. $context[\'current_forum_time_hour\'] = (int) strftime(\'%H\', forum_time(false));
  511. return true;
  512. '),
  513. ),
  514. 'time_offset' => array(
  515. 'type' => 'callback',
  516. 'callback_func' => 'timeoffset_modify',
  517. 'permission' => 'profile_extra',
  518. 'preload' => create_function('', '
  519. global $context, $cur_profile;
  520. $context[\'member\'][\'time_offset\'] = $cur_profile[\'time_offset\'];
  521. return true;
  522. '),
  523. 'input_validate' => create_function('&$value', '
  524. // Validate the time_offset...
  525. $value = (float) strtr($value, \',\', \'.\');
  526. if ($value < -23.5 || $value > 23.5)
  527. return \'bad_offset\';
  528. return true;
  529. '),
  530. ),
  531. 'usertitle' => array(
  532. 'type' => 'text',
  533. 'label' => $txt['custom_title'],
  534. 'log_change' => true,
  535. 'input_attr' => array('maxlength="50"'),
  536. 'size' => 50,
  537. 'permission' => 'profile_other',
  538. 'enabled' => !empty($modSettings['titlesEnable']),
  539. 'input_validate' => create_function('&$value', '
  540. global $smcFunc;
  541. if ($smcFunc[\'strlen\']($value) > 50)
  542. return \'user_title_too_long\';
  543. return true;
  544. '),
  545. ),
  546. 'website_title' => array(
  547. 'type' => 'text',
  548. 'label' => $txt['website_title'],
  549. 'subtext' => $txt['include_website_url'],
  550. 'size' => 50,
  551. 'permission' => 'profile_other',
  552. 'link_with' => 'website',
  553. ),
  554. 'website_url' => array(
  555. 'type' => 'url',
  556. 'label' => $txt['website_url'],
  557. 'subtext' => $txt['complete_url'],
  558. 'size' => 50,
  559. 'permission' => 'profile_other',
  560. // Fix the URL...
  561. 'input_validate' => create_function('&$value', '
  562. if (strlen(trim($value)) > 0 && strpos($value, \'://\') === false)
  563. $value = \'http://\' . $value;
  564. if (strlen($value) < 8 || (substr($value, 0, 7) !== \'http://\' && substr($value, 0, 8) !== \'https://\'))
  565. $value = \'\';
  566. return true;
  567. '),
  568. 'link_with' => 'website',
  569. ),
  570. );
  571. call_integration_hook('integrate_load_profile_fields', array(&$profile_fields));
  572. $disabled_fields = !empty($modSettings['disabled_profile_fields']) ? explode(',', $modSettings['disabled_profile_fields']) : array();
  573. // For each of the above let's take out the bits which don't apply - to save memory and security!
  574. foreach ($profile_fields as $key => $field)
  575. {
  576. // Do we have permission to do this?
  577. if (isset($field['permission']) && !allowedTo(($context['user']['is_owner'] ? array($field['permission'] . '_own', $field['permission'] . '_any') : $field['permission'] . '_any')) && !allowedTo($field['permission']))
  578. unset($profile_fields[$key]);
  579. // Is it enabled?
  580. if (isset($field['enabled']) && !$field['enabled'])
  581. unset($profile_fields[$key]);
  582. // Is it specifically disabled?
  583. if (in_array($key, $disabled_fields) || (isset($field['link_with']) && in_array($field['link_with'], $disabled_fields)))
  584. unset($profile_fields[$key]);
  585. }
  586. }
  587. /**
  588. * Setup the context for a page load!
  589. *
  590. * @param array $fields
  591. */
  592. function setupProfileContext($fields)
  593. {
  594. global $profile_fields, $context, $cur_profile, $smcFunc, $txt;
  595. // Make sure we have this!
  596. loadProfileFields(true);
  597. // First check for any linked sets.
  598. foreach ($profile_fields as $key => $field)
  599. if (isset($field['link_with']) && in_array($field['link_with'], $fields))
  600. $fields[] = $key;
  601. // Some default bits.
  602. $context['profile_prehtml'] = '';
  603. $context['profile_posthtml'] = '';
  604. $context['profile_javascript'] = '';
  605. $context['profile_onsubmit_javascript'] = '';
  606. $i = 0;
  607. $last_type = '';
  608. foreach ($fields as $key => $field)
  609. {
  610. if (isset($profile_fields[$field]))
  611. {
  612. // Shortcut.
  613. $cur_field = &$profile_fields[$field];
  614. // Does it have a preload and does that preload succeed?
  615. if (isset($cur_field['preload']) && !$cur_field['preload']())
  616. continue;
  617. // If this is anything but complex we need to do more cleaning!
  618. if ($cur_field['type'] != 'callback' && $cur_field['type'] != 'hidden')
  619. {
  620. if (!isset($cur_field['label']))
  621. $cur_field['label'] = isset($txt[$field]) ? $txt[$field] : $field;
  622. // Everything has a value!
  623. if (!isset($cur_field['value']))
  624. {
  625. $cur_field['value'] = isset($cur_profile[$field]) ? $cur_profile[$field] : '';
  626. }
  627. // Any input attributes?
  628. $cur_field['input_attr'] = !empty($cur_field['input_attr']) ? implode(',', $cur_field['input_attr']) : '';
  629. }
  630. // Was there an error with this field on posting?
  631. if (isset($context['profile_errors'][$field]))
  632. $cur_field['is_error'] = true;
  633. // Any javascript stuff?
  634. if (!empty($cur_field['js_submit']))
  635. $context['profile_onsubmit_javascript'] .= $cur_field['js_submit'];
  636. if (!empty($cur_field['js']))
  637. $context['profile_javascript'] .= $cur_field['js'];
  638. // Any template stuff?
  639. if (!empty($cur_field['prehtml']))
  640. $context['profile_prehtml'] .= $cur_field['prehtml'];
  641. if (!empty($cur_field['posthtml']))
  642. $context['profile_posthtml'] .= $cur_field['posthtml'];
  643. // Finally put it into context?
  644. if ($cur_field['type'] != 'hidden')
  645. {
  646. $last_type = $cur_field['type'];
  647. $context['profile_fields'][$field] = &$profile_fields[$field];
  648. }
  649. }
  650. // Bodge in a line break - without doing two in a row ;)
  651. elseif ($field == 'hr' && $last_type != 'hr' && $last_type != '')
  652. {
  653. $last_type = 'hr';
  654. $context['profile_fields'][$i++]['type'] = 'hr';
  655. }
  656. }
  657. // Some spicy JS.
  658. addInlineJavascript('
  659. var form_handle = document.forms.creator;
  660. createEventListener(form_handle);
  661. '. (!empty($context['require_password']) ? '
  662. form_handle.addEventListener(\'submit\', function(event)
  663. {
  664. if (this.oldpasswrd.value == "")
  665. {
  666. event.preventDefault();
  667. alert('. (JavaScriptEscape($txt['required_security_reasons'])) .');
  668. return false;
  669. }
  670. }, false);' : ''), true);
  671. // Any onsubmit javascript?
  672. if (!empty($context['profile_onsubmit_javascript']))
  673. addInlineJavascript($context['profile_onsubmit_javascript'], true);
  674. // Any totally custom stuff?
  675. if (!empty($context['profile_javascript']))
  676. addInlineJavascript($context['profile_javascript'], true);
  677. // Free up some memory.
  678. unset($profile_fields);
  679. }
  680. /**
  681. * Save the profile changes.
  682. */
  683. function saveProfileFields()
  684. {
  685. global $profile_fields, $profile_vars, $context, $old_profile, $post_errors, $sourcedir, $cur_profile, $smcFunc;
  686. // Load them up.
  687. loadProfileFields();
  688. // This makes things easier...
  689. $old_profile = $cur_profile;
  690. // This allows variables to call activities when they save - by default just to reload their settings
  691. $context['profile_execute_on_save'] = array();
  692. if ($context['user']['is_owner'])
  693. $context['profile_execute_on_save']['reload_user'] = 'profileReloadUser';
  694. // Assume we log nothing.
  695. $context['log_changes'] = array();
  696. // Cycle through the profile fields working out what to do!
  697. foreach ($profile_fields as $key => $field)
  698. {
  699. if (!isset($_POST[$key]) || !empty($field['is_dummy']) || (isset($_POST['preview_signature']) && $key == 'signature'))
  700. continue;
  701. // What gets updated?
  702. $db_key = isset($field['save_key']) ? $field['save_key'] : $key;
  703. // Right - we have something that is enabled, we can act upon and has a value posted to it. Does it have a validation function?
  704. if (isset($field['input_validate']))
  705. {
  706. $is_valid = $field['input_validate']($_POST[$key]);
  707. // An error occured - set it as such!
  708. if ($is_valid !== true)
  709. {
  710. // Is this an actual error?
  711. if ($is_valid !== false)
  712. {
  713. $post_errors[$key] = $is_valid;
  714. $profile_fields[$key]['is_error'] = $is_valid;
  715. }
  716. // Retain the old value.
  717. $cur_profile[$key] = $_POST[$key];
  718. continue;
  719. }
  720. }
  721. // Are we doing a cast?
  722. $field['cast_type'] = empty($field['cast_type']) ? $field['type'] : $field['cast_type'];
  723. // Finally, clean up certain types.
  724. if ($field['cast_type'] == 'int')
  725. $_POST[$key] = (int) $_POST[$key];
  726. elseif ($field['cast_type'] == 'float')
  727. $_POST[$key] = (float) $_POST[$key];
  728. elseif ($field['cast_type'] == 'check')
  729. $_POST[$key] = !empty($_POST[$key]) ? 1 : 0;
  730. // If we got here we're doing OK.
  731. if ($field['type'] != 'hidden' && (!isset($old_profile[$key]) || $_POST[$key] != $old_profile[$key]))
  732. {
  733. // Set the save variable.
  734. $profile_vars[$db_key] = $_POST[$key];
  735. // And update the user profile.
  736. $cur_profile[$key] = $_POST[$key];
  737. // Are we logging it?
  738. if (!empty($field['log_change']) && isset($old_profile[$key]))
  739. $context['log_changes'][$key] = array(
  740. 'previous' => $old_profile[$key],
  741. 'new' => $_POST[$key],
  742. );
  743. }
  744. // Logging group changes are a bit different...
  745. if ($key == 'id_group' && $field['log_change'])
  746. {
  747. profileLoadGroups();
  748. // Any changes to primary group?
  749. if ($_POST['id_group'] != $old_profile['id_group'])
  750. {
  751. $context['log_changes']['id_group'] = array(
  752. 'previous' => !empty($old_profile[$key]) && isset($context['member_groups'][$old_profile[$key]]) ? $context['member_groups'][$old_profile[$key]]['name'] : '',
  753. 'new' => !empty($_POST[$key]) && isset($context['member_groups'][$_POST[$key]]) ? $context['member_groups'][$_POST[$key]]['name'] : '',
  754. );
  755. }
  756. // Prepare additional groups for comparison.
  757. $additional_groups = array(
  758. 'previous' => !empty($old_profile['additional_groups']) ? explode(',', $old_profile['additional_groups']) : array(),
  759. 'new' => !empty($_POST['additional_groups']) ? array_diff($_POST['additional_groups'], array(0)) : array(),
  760. );
  761. sort($additional_groups['previous']);
  762. sort($additional_groups['new']);
  763. // What about additional groups?
  764. if ($additional_groups['previous'] != $additional_groups['new'])
  765. {
  766. foreach ($additional_groups as $type => $groups)
  767. {
  768. foreach ($groups as $id => $group)
  769. {
  770. if (isset($context['member_groups'][$group]))
  771. $additional_groups[$type][$id] = $context['member_groups'][$group]['name'];
  772. else
  773. unset($additional_groups[$type][$id]);
  774. }
  775. $additional_groups[$type] = implode(', ', $additional_groups[$type]);
  776. }
  777. $context['log_changes']['additional_groups'] = $additional_groups;
  778. }
  779. }
  780. }
  781. // @todo Temporary
  782. if ($context['user']['is_owner'])
  783. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own'));
  784. else
  785. $changeOther = allowedTo('profile_extra_any');
  786. if ($changeOther && empty($post_errors))
  787. {
  788. makeThemeChanges($context['id_member'], isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  789. if (!empty($_REQUEST['sa']))
  790. makeCustomFieldChanges($context['id_member'], $_REQUEST['sa'], false);
  791. }
  792. // Free memory!
  793. unset($profile_fields);
  794. }
  795. /**
  796. * Save the profile changes
  797. *
  798. * @param array &$profile_variables
  799. * @param array &$post_errors
  800. * @param int $memID id_member
  801. */
  802. function saveProfileChanges(&$profile_vars, &$post_errors, $memID)
  803. {
  804. global $txt, $user_profile;
  805. global $context, $sourcedir;
  806. global $smcFunc;
  807. // These make life easier....
  808. $old_profile = &$user_profile[$memID];
  809. // Permissions...
  810. if ($context['user']['is_owner'])
  811. {
  812. $changeIdentity = allowedTo(array('profile_identity_any', 'profile_identity_own', 'profile_password_any', 'profile_password_own'));
  813. $changeOther = allowedTo(array('profile_extra_any', 'profile_extra_own', 'profile_other_any', 'profile_other_own', 'profile_signature_any', 'profile_signature_own'));
  814. }
  815. else
  816. {
  817. $changeIdentity = allowedTo('profile_identity_any', 'profile_signature_any');
  818. $changeOther = allowedTo('profile_extra_any', 'profile_other_any', 'profile_signature_any');
  819. }
  820. // Arrays of all the changes - makes things easier.
  821. $profile_bools = array(
  822. 'notify_announcements', 'notify_send_body',
  823. );
  824. $profile_ints = array(
  825. 'notify_regularity',
  826. 'notify_types',
  827. );
  828. $profile_floats = array(
  829. );
  830. $profile_strings = array(
  831. 'buddy_list',
  832. 'ignore_boards',
  833. );
  834. if (isset($_POST['sa']) && $_POST['sa'] == 'ignoreboards' && empty($_POST['ignore_brd']))
  835. $_POST['ignore_brd'] = array();
  836. unset($_POST['ignore_boards']); // Whatever it is set to is a dirty fithy thing. Kinda like our minds.
  837. if (isset($_POST['ignore_brd']))
  838. {
  839. if (!is_array($_POST['ignore_brd']))
  840. $_POST['ignore_brd'] = array ($_POST['ignore_brd']);
  841. foreach ($_POST['ignore_brd'] as $k => $d)
  842. {
  843. $d = (int) $d;
  844. if ($d != 0)
  845. $_POST['ignore_brd'][$k] = $d;
  846. else
  847. unset($_POST['ignore_brd'][$k]);
  848. }
  849. $_POST['ignore_boards'] = implode(',', $_POST['ignore_brd']);
  850. unset($_POST['ignore_brd']);
  851. }
  852. // Here's where we sort out all the 'other' values...
  853. if ($changeOther)
  854. {
  855. makeThemeChanges($memID, isset($_POST['id_theme']) ? (int) $_POST['id_theme'] : $old_profile['id_theme']);
  856. //makeAvatarChanges($memID, $post_errors);
  857. if (!empty($_REQUEST['sa']))
  858. makeCustomFieldChanges($memID, $_REQUEST['sa'], false);
  859. foreach ($profile_bools as $var)
  860. if (isset($_POST[$var]))
  861. $profile_vars[$var] = empty($_POST[$var]) ? '0' : '1';
  862. foreach ($profile_ints as $var)
  863. if (isset($_POST[$var]))
  864. $profile_vars[$var] = $_POST[$var] != '' ? (int) $_POST[$var] : '';
  865. foreach ($profile_floats as $var)
  866. if (isset($_POST[$var]))
  867. $profile_vars[$var] = (float) $_POST[$var];
  868. foreach ($profile_strings as $var)
  869. if (isset($_POST[$var]))
  870. $profile_vars[$var] = $_POST[$var];
  871. }
  872. }
  873. /**
  874. * Make any theme changes that are sent with the profile.
  875. *
  876. * @param int $memID
  877. * @param int $id_theme
  878. */
  879. function makeThemeChanges($memID, $id_theme)
  880. {
  881. global $modSettings, $smcFunc, $context, $user_info;
  882. $reservedVars = array(
  883. 'actual_theme_url',
  884. 'actual_images_url',
  885. 'base_theme_dir',
  886. 'base_theme_url',
  887. 'default_images_url',
  888. 'default_theme_dir',
  889. 'default_theme_url',
  890. 'default_template',
  891. 'images_url',
  892. 'number_recent_posts',
  893. 'smiley_sets_default',
  894. 'theme_dir',
  895. 'theme_id',
  896. 'theme_layers',
  897. 'theme_templates',
  898. 'theme_url',
  899. );
  900. // Can't change reserved vars.
  901. if ((isset($_POST['options']) && count(array_intersect(array_keys($_POST['options']), $reservedVars)) != 0) || (isset($_POST['default_options']) && count(array_intersect(array_keys($_POST['default_options']), $reservedVars)) != 0))
  902. fatal_lang_error('no_access', false);
  903. // Don't allow any overriding of custom fields with default or non-default options.
  904. $request = $smcFunc['db_query']('', '
  905. SELECT col_name
  906. FROM {db_prefix}custom_fields
  907. WHERE active = {int:is_active}',
  908. array(
  909. 'is_active' => 1,
  910. )
  911. );
  912. $custom_fields = array();
  913. while ($row = $smcFunc['db_fetch_assoc']($request))
  914. $custom_fields[] = $row['col_name'];
  915. $smcFunc['db_free_result']($request);
  916. // These are the theme changes...
  917. $themeSetArray = array();
  918. if (isset($_POST['options']) && is_array($_POST['options']))
  919. {
  920. foreach ($_POST['options'] as $opt => $val)
  921. {
  922. if (in_array($opt, $custom_fields))
  923. continue;
  924. // These need to be controlled.
  925. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  926. $val = max(0, min($val, 50));
  927. // We don't set this per theme anymore.
  928. elseif ($opt == 'allow_no_censored')
  929. continue;
  930. $themeSetArray[] = array($memID, $id_theme, $opt, is_array($val) ? implode(',', $val) : $val);
  931. }
  932. }
  933. $erase_options = array();
  934. if (isset($_POST['default_options']) && is_array($_POST['default_options']))
  935. foreach ($_POST['default_options'] as $opt => $val)
  936. {
  937. if (in_array($opt, $custom_fields))
  938. continue;
  939. // These need to be controlled.
  940. if ($opt == 'topics_per_page' || $opt == 'messages_per_page')
  941. $val = max(0, min($val, 50));
  942. // Only let admins and owners change the censor.
  943. elseif ($opt == 'allow_no_censored' && !$user_info['is_admin'] && !$context['user']['is_owner'])
  944. continue;
  945. $themeSetArray[] = array($memID, 1, $opt, is_array($val) ? implode(',', $val) : $val);
  946. $erase_options[] = $opt;
  947. }
  948. // If themeSetArray isn't still empty, send it to the database.
  949. if (empty($context['password_auth_failed']))
  950. {
  951. if (!empty($themeSetArray))
  952. {
  953. $smcFunc['db_insert']('replace',
  954. '{db_prefix}themes',
  955. array('id_member' => 'int', 'id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534'),
  956. $themeSetArray,
  957. array('id_member', 'id_theme', 'variable')
  958. );
  959. }
  960. if (!empty($erase_options))
  961. {
  962. $smcFunc['db_query']('', '
  963. DELETE FROM {db_prefix}themes
  964. WHERE id_theme != {int:id_theme}
  965. AND variable IN ({array_string:erase_variables})
  966. AND id_member = {int:id_member}',
  967. array(
  968. 'id_theme' => 1,
  969. 'id_member' => $memID,
  970. 'erase_variables' => $erase_options
  971. )
  972. );
  973. }
  974. // Admins can choose any theme, even if it's not enabled...
  975. $themes = allowedTo('admin_forum') ? explode(',', $modSettings['knownThemes']) : explode(',', $modSettings['enableThemes']);
  976. foreach ($themes as $t)
  977. cache_put_data('theme_settings-' . $t . ':' . $memID, null, 60);
  978. }
  979. }
  980. /**
  981. * Make any notification changes that need to be made.
  982. *
  983. * @param int $memID id_member
  984. */
  985. function makeNotificationChanges($memID)
  986. {
  987. global $smcFunc;
  988. // Update the boards they are being notified on.
  989. if (isset($_POST['edit_notify_boards']) && !empty($_POST['notify_boards']))
  990. {
  991. // Make sure only integers are deleted.
  992. foreach ($_POST['notify_boards'] as $index => $id)
  993. $_POST['notify_boards'][$index] = (int) $id;
  994. // id_board = 0 is reserved for topic notifications.
  995. $_POST['notify_boards'] = array_diff($_POST['notify_boards'], array(0));
  996. $smcFunc['db_query']('', '
  997. DELETE FROM {db_prefix}log_notify
  998. WHERE id_board IN ({array_int:board_list})
  999. AND id_member = {int:selected_member}',
  1000. array(
  1001. 'board_list' => $_POST['notify_boards'],
  1002. 'selected_member' => $memID,
  1003. )
  1004. );
  1005. }
  1006. // We are editing topic notifications......
  1007. elseif (isset($_POST['edit_notify_topics']) && !empty($_POST['notify_topics']))
  1008. {
  1009. foreach ($_POST['notify_topics'] as $index => $id)
  1010. $_POST['notify_topics'][$index] = (int) $id;
  1011. // Make sure there are no zeros left.
  1012. $_POST['notify_topics'] = array_diff($_POST['notify_topics'], array(0));
  1013. $smcFunc['db_query']('', '
  1014. DELETE FROM {db_prefix}log_notify
  1015. WHERE id_topic IN ({array_int:topic_list})
  1016. AND id_member = {int:selected_member}',
  1017. array(
  1018. 'topic_list' => $_POST['notify_topics'],
  1019. 'selected_member' => $memID,
  1020. )
  1021. );
  1022. }
  1023. }
  1024. /**
  1025. * Save any changes to the custom profile fields
  1026. *
  1027. * @param int $memID
  1028. * @param string $area
  1029. * @param bool $sanitize = true
  1030. */
  1031. function makeCustomFieldChanges($memID, $area, $sanitize = true)
  1032. {
  1033. global $context, $smcFunc, $user_profile, $user_info, $modSettings, $sourcedir;
  1034. if ($sanitize && isset($_POST['customfield']))
  1035. $_POST['customfield'] = htmlspecialchars__recursive($_POST['customfield']);
  1036. $where = $area == 'register' ? 'show_reg != 0' : 'show_profile = {string:area}';
  1037. // Load the fields we are saving too - make sure we save valid data (etc).
  1038. $request = $smcFunc['db_query']('', '
  1039. SELECT col_name, field_name, field_desc, field_type, field_length, field_options, default_value, show_reg, mask, private
  1040. FROM {db_prefix}custom_fields
  1041. WHERE ' . $where . '
  1042. AND active = {int:is_active}',
  1043. array(
  1044. 'is_active' => 1,
  1045. 'area' => $area,
  1046. )
  1047. );
  1048. $changes = array();
  1049. $log_changes = array();
  1050. while ($row = $smcFunc['db_fetch_assoc']($request))
  1051. {
  1052. /* This means don't save if:
  1053. - The user is NOT an admin.
  1054. - The data is not freely viewable and editable by users.
  1055. - The data is not invisible to users but editable by the owner (or if it is the user is not the owner)
  1056. - The area isn't registration, and if it is that the field is not suppossed to be shown there.
  1057. */
  1058. if ($row['private'] != 0 && !allowedTo('admin_forum') && ($memID != $user_info['id'] || $row['private'] != 2) && ($area != 'register' || $row['show_reg'] == 0))
  1059. continue;
  1060. // Validate the user data.
  1061. if ($row['field_type'] == 'check')
  1062. $value = isset($_POST['customfield'][$row['col_name']]) ? 1 : 0;
  1063. elseif ($row['field_type'] == 'select' || $row['field_type'] == 'radio')
  1064. {
  1065. $value = $row['default_value'];
  1066. foreach (explode(',', $row['field_options']) as $k => $v)
  1067. if (isset($_POST['customfield'][$row['col_name']]) && $_POST['customfield'][$row['col_name']] == $k)
  1068. $value = $v;
  1069. }
  1070. // Otherwise some form of text!
  1071. else
  1072. {
  1073. $value = isset($_POST['customfield'][$row['col_name']]) ? $_POST['customfield'][$row['col_name']] : '';
  1074. if ($row['field_length'])
  1075. $value = $smcFunc['substr']($value, 0, $row['field_length']);
  1076. // Any masks?
  1077. if ($row['field_type'] == 'text' && !empty($row['mask']) && $row['mask'] != 'none')
  1078. {
  1079. // @todo We never error on this - just ignore it at the moment...
  1080. if ($row['mask'] == 'email' && (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $value) === 0 || strlen($value) > 255))
  1081. $value = '';
  1082. elseif ($row['mask'] == 'number')
  1083. {
  1084. $value = (int) $value;
  1085. }
  1086. elseif (substr($row['mask'], 0, 5) == 'regex' && trim($value) != '' && preg_match(substr($row['mask'], 5), $value) === 0)
  1087. $value = '';
  1088. }
  1089. }
  1090. // Did it change?
  1091. if (!isset($user_profile[$memID]['options'][$row['col_name']]) || $user_profile[$memID]['options'][$row['col_name']] != $value)
  1092. {
  1093. $log_changes[] = array(
  1094. 'action' => 'customfield_' . $row['col_name'],
  1095. 'log_type' => 'user',
  1096. 'extra' => array(
  1097. 'previous' => !empty($user_profile[$memID]['options'][$row['col_name']]) ? $user_profile[$memID]['options'][$row['col_name']] : '',
  1098. 'new' => $value,
  1099. 'applicator' => $user_info['id'],
  1100. 'member_affected' => $memID,
  1101. ),
  1102. );
  1103. $changes[] = array(1, $row['col_name'], $value, $memID);
  1104. $user_profile[$memID]['options'][$row['col_name']] = $value;
  1105. }
  1106. }
  1107. $smcFunc['db_free_result']($request);
  1108. call_integration_hook('integrate_save_custom_profile_fields', array(&$changes, &$log_changes, $memID, $area, $sanitize));
  1109. // Make those changes!
  1110. if (!empty($changes) && empty($context['password_auth_failed']))
  1111. {
  1112. $smcFunc['db_insert']('replace',
  1113. '{db_prefix}themes',
  1114. array('id_theme' => 'int', 'variable' => 'string-255', 'value' => 'string-65534', 'id_member' => 'int'),
  1115. $changes,
  1116. array('id_theme', 'variable', 'id_member')
  1117. );
  1118. if (!empty($log_changes) && !empty($modSettings['modlog_enabled']))
  1119. {
  1120. require_once($sourcedir . '/Logging.php');
  1121. logActions($log_changes);
  1122. }
  1123. }
  1124. }
  1125. /**
  1126. * Show all the users buddies, as well as a add/delete interface.
  1127. *
  1128. * @param int $memID id_member
  1129. */
  1130. function editBuddyIgnoreLists($memID)
  1131. {
  1132. global $sourcedir, $context, $txt, $scripturl, $modSettings, $user_profile;
  1133. // Do a quick check to ensure people aren't getting here illegally!
  1134. if (!$context['user']['is_owner'] || empty($modSettings['enable_buddylist']))
  1135. fatal_lang_error('no_access', false);
  1136. // Can we email the user direct?
  1137. $context['can_moderate_forum'] = allowedTo('moderate_forum');
  1138. $context['can_send_email'] = allowedTo('send_email_to_members');
  1139. $subActions = array(
  1140. 'buddies' => array('editBuddies', $txt['editBuddies']),
  1141. 'ignore' => array('editIgnoreList', $txt['editIgnoreList']),
  1142. );
  1143. $context['list_area'] = isset($_GET['sa']) && isset($subActions[$_GET['sa']]) ? $_GET['sa'] : 'buddies';
  1144. // Create the tabs for the template.
  1145. $context[$context['profile_menu_name']]['tab_data'] = array(
  1146. 'title' => $txt['editBuddyIgnoreLists'],
  1147. 'description' => $txt['buddy_ignore_desc'],
  1148. 'icon' => 'profile_hd.png',
  1149. 'tabs' => array(
  1150. 'buddies' => array(),
  1151. 'ignore' => array(),
  1152. ),
  1153. );
  1154. loadJavascriptFile('suggest.js', array('default_theme' => true, 'defer' => false), 'smf_suggest');
  1155. // Pass on to the actual function.
  1156. $context['sub_template'] = $subActions[$context['list_area']][0];
  1157. $subActions[$context['list_area']][0]($memID);
  1158. }
  1159. /**
  1160. * Show all the users buddies, as well as a add/delete interface.
  1161. *
  1162. * @param int $memID id_member
  1163. */
  1164. function editBuddies($memID)
  1165. {
  1166. global $txt, $scripturl;
  1167. global $context, $user_profile, $memberContext, $smcFunc;
  1168. // For making changes!
  1169. $buddiesArray = explode(',', $user_profile[$memID]['buddy_list']);
  1170. foreach ($buddiesArray as $k => $dummy)
  1171. if ($dummy == '')
  1172. unset($buddiesArray[$k]);
  1173. // Removing a buddy?
  1174. if (isset($_GET['remove']))
  1175. {
  1176. checkSession('get');
  1177. call_integration_hook('integrate_remove_buddy', array($memID));
  1178. $_SESSION['prf-save'] = $txt['could_not_remove_person'];
  1179. // Heh, I'm lazy, do it the easy way...
  1180. foreach ($buddiesArray as $key => $buddy)
  1181. if ($buddy == (int) $_GET['remove'])
  1182. {
  1183. unset($buddiesArray[$key]);
  1184. $_SESSION['prf-save'] = true;
  1185. }
  1186. // Make the changes.
  1187. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1188. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1189. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1190. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1191. }
  1192. elseif (isset($_POST['new_buddy']))
  1193. {
  1194. checkSession();
  1195. // Prepare the string for extraction...
  1196. $_POST['new_buddy'] = strtr($smcFunc['htmlspecialchars']($_POST['new_buddy'], ENT_QUOTES), array('&quot;' => '"'));
  1197. preg_match_all('~"([^"]+)"~', $_POST['new_buddy'], $matches);
  1198. $new_buddies = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_buddy']))));
  1199. foreach ($new_buddies as $k => $dummy)
  1200. {
  1201. $new_buddies[$k] = strtr(trim($new_buddies[$k]), array('\'' => '&#039;'));
  1202. if (strlen($new_buddies[$k]) == 0 || in_array($new_buddies[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1203. unset($new_buddies[$k]);
  1204. }
  1205. call_integration_hook('integrate_add_buddies', array($memID, &$new_buddies));
  1206. $_SESSION['prf-save'] = $txt['could_not_add_person'];
  1207. if (!empty($new_buddies))
  1208. {
  1209. // Now find out the id_member of the buddy.
  1210. $request = $smcFunc['db_query']('', '
  1211. SELECT id_member
  1212. FROM {db_prefix}members
  1213. WHERE member_name IN ({array_string:new_buddies}) OR real_name IN ({array_string:new_buddies})
  1214. LIMIT {int:count_new_buddies}',
  1215. array(
  1216. 'new_buddies' => $new_buddies,
  1217. 'count_new_buddies' => count($new_buddies),
  1218. )
  1219. );
  1220. if ($smcFunc['db_num_rows']($request) != 0)
  1221. $_SESSION['prf-save'] = true;
  1222. // Add the new member to the buddies array.
  1223. while ($row = $smcFunc['db_fetch_assoc']($request))
  1224. $buddiesArray[] = (int) $row['id_member'];
  1225. $smcFunc['db_free_result']($request);
  1226. // Now update the current users buddy list.
  1227. $user_profile[$memID]['buddy_list'] = implode(',', $buddiesArray);
  1228. updateMemberData($memID, array('buddy_list' => $user_profile[$memID]['buddy_list']));
  1229. }
  1230. // Back to the buddy list!
  1231. redirectexit('action=profile;area=lists;sa=buddies;u=' . $memID);
  1232. }
  1233. // Get all the users "buddies"...
  1234. $buddies = array();
  1235. if (!empty($buddiesArray))
  1236. {
  1237. $result = $smcFunc['db_query']('', '
  1238. SELECT id_member
  1239. FROM {db_prefix}members
  1240. WHERE id_member IN ({array_int:buddy_list})
  1241. ORDER BY real_name
  1242. LIMIT {int:buddy_list_count}',
  1243. array(
  1244. 'buddy_list' => $buddiesArray,
  1245. 'buddy_list_count' => substr_count($user_profile[$memID]['buddy_list'], ',') + 1,
  1246. )
  1247. );
  1248. while ($row = $smcFunc['db_fetch_assoc']($result))
  1249. $buddies[] = $row['id_member'];
  1250. $smcFunc['db_free_result']($result);
  1251. }
  1252. $context['buddy_count'] = count($buddies);
  1253. // Load all the members up.
  1254. loadMemberData($buddies, false, 'profile');
  1255. // Setup the context for each buddy.
  1256. $context['buddies'] = array();
  1257. foreach ($buddies as $buddy)
  1258. {
  1259. loadMemberContext($buddy);
  1260. $context['buddies'][$buddy] = $memberContext[$buddy];
  1261. }
  1262. if (isset($_SESSION['prf-save']))
  1263. {
  1264. if ($_SESSION['prf-save'] === true)
  1265. $context['saved_successful'] = true;
  1266. else
  1267. $context['saved_failed'] = $_SESSION['prf-save'];
  1268. unset($_SESSION['prf-save']);
  1269. }
  1270. call_integration_hook('integrate_view_buddies', array($memID));
  1271. }
  1272. /**
  1273. * Allows the user to view their ignore list, as well as the option to manage members on it.
  1274. *
  1275. * @param int $memID id_member
  1276. */
  1277. function editIgnoreList($memID)
  1278. {
  1279. global $txt, $scripturl;
  1280. global $context, $user_profile, $memberContext, $smcFunc;
  1281. // For making changes!
  1282. $ignoreArray = explode(',', $user_profile[$memID]['pm_ignore_list']);
  1283. foreach ($ignoreArray as $k => $dummy)
  1284. if ($dummy == '')
  1285. unset($ignoreArray[$k]);
  1286. // Removing a member from the ignore list?
  1287. if (isset($_GET['remove']))
  1288. {
  1289. checkSession('get');
  1290. $_SESSION['prf-save'] = $txt['could_not_remove_person'];
  1291. // Heh, I'm lazy, do it the easy way...
  1292. foreach ($ignoreArray as $key => $id_remove)
  1293. if ($id_remove == (int) $_GET['remove'])
  1294. {
  1295. unset($ignoreArray[$key]);
  1296. $_SESSION['prf-save'] = true;
  1297. }
  1298. // Make the changes.
  1299. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1300. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1301. // Redirect off the page because we don't like all this ugly query stuff to stick in the history.
  1302. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1303. }
  1304. elseif (isset($_POST['new_ignore']))
  1305. {
  1306. checkSession();
  1307. // Prepare the string for extraction...
  1308. $_POST['new_ignore'] = strtr($smcFunc['htmlspecialchars']($_POST['new_ignore'], ENT_QUOTES), array('&quot;' => '"'));
  1309. preg_match_all('~"([^"]+)"~', $_POST['new_ignore'], $matches);
  1310. $new_entries = array_unique(array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $_POST['new_ignore']))));
  1311. foreach ($new_entries as $k => $dummy)
  1312. {
  1313. $new_entries[$k] = strtr(trim($new_entries[$k]), array('\'' => '&#039;'));
  1314. if (strlen($new_entries[$k]) == 0 || in_array($new_entries[$k], array($user_profile[$memID]['member_name'], $user_profile[$memID]['real_name'])))
  1315. unset($new_entries[$k]);
  1316. }
  1317. $_SESSION['prf-save'] = $txt['could_not_add_person'];
  1318. if (!empty($new_entries))
  1319. {
  1320. // Now find out the id_member for the members in question.
  1321. $request = $smcFunc['db_query']('', '
  1322. SELECT id_member
  1323. FROM {db_prefix}members
  1324. WHERE member_name IN ({array_string:new_entries}) OR real_name IN ({array_string:new_entries})
  1325. LIMIT {int:count_new_entries}',
  1326. array(
  1327. 'new_entries' => $new_entries,
  1328. 'count_new_entries' => count($new_entries),
  1329. )
  1330. );
  1331. if ($smcFunc['db_num_rows']($request) != 0)
  1332. $_SESSION['prf-save'] = true;
  1333. // Add the new member to the buddies array.
  1334. while ($row = $smcFunc['db_fetch_assoc']($request))
  1335. $ignoreArray[] = (int) $row['id_member'];
  1336. $smcFunc['db_free_result']($request);
  1337. // Now update the current users buddy list.
  1338. $user_profile[$memID]['pm_ignore_list'] = implode(',', $ignoreArray);
  1339. updateMemberData($memID, array('pm_ignore_list' => $user_profile[$memID]['pm_ignore_list']));
  1340. }
  1341. // Back to the list of pityful people!
  1342. redirectexit('action=profile;area=lists;sa=ignore;u=' . $memID);
  1343. }
  1344. // Initialise the list of members we're ignoring.
  1345. $ignored = array();
  1346. if (!empty($ignoreArray))
  1347. {
  1348. $result = $smcFunc['db_query']('', '
  1349. SELECT id_member
  1350. FROM {db_prefix}members
  1351. WHERE id_member IN ({array_int:ignore_list})
  1352. ORDER BY real_name
  1353. LIMIT {int:ignore_list_count}',
  1354. array(
  1355. 'ignore_list' => $ignoreArray,
  1356. 'ignore_list_count' => substr_count($user_profile[$memID]['pm_ignore_list'], ',') + 1,
  1357. )
  1358. );
  1359. while ($row = $smcFunc['db_fetch_assoc']($result))
  1360. $ignored[] = $row['id_member'];
  1361. $smcFunc['db_free_result']($result);
  1362. }
  1363. $context['ignore_count'] = count($ignored);
  1364. // Load all the members up.
  1365. loadMemberData($ignored, false, 'profile');
  1366. // Setup the context for each buddy.
  1367. $context['ignore_list'] = array();
  1368. foreach ($ignored as $ignore_member)
  1369. {
  1370. loadMemberContext($ignore_member);
  1371. $context['ignore_list'][$ignore_member] = $memberContext[$ignore_member];
  1372. }
  1373. if (isset($_SESSION['prf-save']))
  1374. {
  1375. if ($_SESSION['prf-save'] === true)
  1376. $context['saved_successful'] = true;
  1377. else
  1378. $context['saved_failed'] = $_SESSION['prf-save'];
  1379. unset($_SESSION['prf-save']);
  1380. }
  1381. }
  1382. /**
  1383. * @todo Needs a description
  1384. *
  1385. * @param int $memID id_member
  1386. */
  1387. function account($memID)
  1388. {
  1389. global $context, $txt;
  1390. loadThemeOptions($memID);
  1391. if (allowedTo(array('profile_identity_own', 'profile_identity_any', 'profile_password_own', 'profile_password_any')))
  1392. loadCustomFields($memID, 'account');
  1393. $context['sub_template'] = 'edit_options';
  1394. $context['page_desc'] = $txt['account_info'];
  1395. setupProfileContext(
  1396. array(
  1397. 'member_name', 'real_name', 'date_registered', 'posts', 'lngfile', 'hr',
  1398. 'id_group', 'hr',
  1399. 'email_address', 'hide_email', 'show_online', 'hr',
  1400. 'passwrd1', 'passwrd2', 'hr',
  1401. 'secret_question', 'secret_answer',
  1402. )
  1403. );
  1404. }
  1405. /**
  1406. * @todo Needs a description
  1407. *
  1408. * @param int $memID id_member
  1409. */
  1410. function forumProfile($memID)
  1411. {
  1412. global $context, $txt;
  1413. loadThemeOptions($memID);
  1414. if (allowedTo(array('profile_forum_own', 'profile_forum_any')))
  1415. loadCustomFields($memID, 'forumprofile');
  1416. $context['sub_template'] = 'edit_options';
  1417. $context['page_desc'] = $txt['forumProfile_info'];
  1418. $context['show_preview_button'] = true;
  1419. setupProfileContext(
  1420. array(
  1421. 'avatar_choice', 'hr', 'personal_text', 'hr',
  1422. 'bday1', 'usertitle', 'signature', 'hr',
  1423. 'karma_good', 'hr',
  1424. 'website_title', 'website_url',
  1425. )
  1426. );
  1427. }
  1428. /**
  1429. * Recursive function to retrieve server-stored avatar files
  1430. *
  1431. * @param string $directory
  1432. * @param int $level
  1433. * @return array
  1434. */
  1435. function getAvatars($directory, $level)
  1436. {
  1437. global $context, $txt, $modSettings, $smcFunc;
  1438. $result = array();
  1439. // Open the directory..
  1440. $dir = dir($modSettings['avatar_directory'] . (!empty($directory) ? '/' : '') . $directory);
  1441. $dirs = array();
  1442. $files = array();
  1443. if (!$dir)
  1444. return array();
  1445. while ($line = $dir->read())
  1446. {
  1447. if (in_array($line, array('.', '..', 'blank.png', 'index.php')))
  1448. continue;
  1449. if (is_dir($modSettings['avatar_directory'] . '/' . $directory . (!empty($directory) ? '/' : '') . $line))
  1450. $dirs[] = $line;
  1451. else
  1452. $files[] = $line;
  1453. }
  1454. $dir->close();
  1455. // Sort the results...
  1456. natcasesort($dirs);
  1457. natcasesort($files);
  1458. if ($level == 0)
  1459. {
  1460. $result[] = array(
  1461. 'filename' => 'blank.png',
  1462. 'checked' => in_array($context['member']['avatar']['server_pic'], array('', 'blank.png')),
  1463. 'name' => $txt['no_pic'],
  1464. 'is_dir' => false
  1465. );
  1466. }
  1467. foreach ($dirs as $line)
  1468. {
  1469. $tmp = getAvatars($directory . (!empty($directory) ? '/' : '') . $line, $level + 1);
  1470. if (!empty($tmp))
  1471. $result[] = array(
  1472. 'filename' => $smcFunc['htmlspecialchars']($line),
  1473. 'checked' => strpos($context['member']['avatar']['server_pic'], $line . '/') !== false,
  1474. 'name' => '[' . $smcFunc['htmlspecialchars'](str_replace('_', ' ', $line)) . ']',
  1475. 'is_dir' => true,
  1476. 'files' => $tmp
  1477. );
  1478. unset($tmp);
  1479. }
  1480. foreach ($files as $line)
  1481. {
  1482. $filename = substr($line, 0, (strlen($line) - strlen(strrchr($line, '.'))));
  1483. $extension = substr(strrchr($line, '.'), 1);
  1484. // Make sure it is an image.
  1485. if (strcasecmp($extension, 'gif') != 0 && strcasecmp($extension, 'jpg') != 0 && strcasecmp($extension, 'jpeg') != 0 && strcasecmp($extension, 'png') != 0 && strcasecmp($extension, 'bmp') != 0)
  1486. continue;
  1487. $result[] = array(
  1488. 'filename' => $smcFunc['htmlspecialchars']($line),
  1489. 'checked' => $line == $context['member']['avatar']['server_pic'],
  1490. 'name' => $smcFunc['htmlspecialchars'](str_replace('_', ' ', $filename)),
  1491. 'is_dir' => false
  1492. );
  1493. if ($level == 1)
  1494. $context['avatar_list'][] = $directory . '/' . $line;
  1495. }
  1496. return $result;
  1497. }
  1498. /**
  1499. * @todo needs a description
  1500. *
  1501. * @param int $memID id_member
  1502. */
  1503. function theme($memID)
  1504. {
  1505. global $txt, $context, $user_profile, $smcFunc;
  1506. loadThemeOptions($memID);
  1507. if (allowedTo(array('profile_extra_own', 'profile_extra_any')))
  1508. loadCustomFields($memID, 'theme');
  1509. $context['sub_template'] = 'edit_options';
  1510. $context['page_desc'] = $txt['theme_info'];
  1511. setupProfileContext(
  1512. array(
  1513. 'id_theme', 'smiley_set', 'hr',
  1514. 'time_format', 'time_offset', 'hr',
  1515. 'theme_settings',
  1516. )
  1517. );
  1518. }
  1519. /**
  1520. * Changing authentication method? Only appropriate for people using OpenID.
  1521. *
  1522. * @param int $memID id_member
  1523. * @param bool $saving = false
  1524. */
  1525. function authentication($memID, $saving = false)
  1526. {
  1527. global $context, $cur_profile, $sourcedir, $txt, $post_errors, $modSettings;
  1528. loadLanguage('Login');
  1529. // We are saving?
  1530. if ($saving)
  1531. {
  1532. // Moving to password passed authentication?
  1533. if ($_POST['authenticate'] == 'passwd')
  1534. {
  1535. // Didn't enter anything?
  1536. if ($_POST['passwrd1'] == '')
  1537. $post_errors[] = 'no_password';
  1538. // Do the two entries for the password even match?
  1539. elseif (!isset($_POST['passwrd2']) || $_POST['passwrd1'] != $_POST['passwrd2'])
  1540. $post_errors[] = 'bad_new_password';
  1541. // Is it valid?
  1542. else
  1543. {
  1544. require_once($sourcedir . '/Subs-Auth.php');
  1545. $passwordErrors = validatePassword($_POST['passwrd1'], $cur_profile['member_name'], array($cur_profile['real_name'], $cur_profile['email_address']));
  1546. // Were there errors?
  1547. if ($passwordErrors != null)
  1548. $post_errors[] = 'password_' . $passwordErrors;
  1549. }
  1550. if (empty($post_errors))
  1551. {
  1552. // Integration?
  1553. call_integration_hook('integrate_reset_pass', array($cur_profile['member_name'], $cur_profile['member_name'], $_POST['passwrd1']));
  1554. // Go then.
  1555. $passwd = sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd1']));
  1556. // Do the important bits.
  1557. updateMemberData($memID, array('openid_uri' => '', 'passwd' => $passwd));
  1558. if ($context['user']['is_owner'])
  1559. {
  1560. setLoginCookie(60 * $modSettings['cookieTime'], $memID, sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  1561. redirectexit('action=profile;area=authentication;updated');
  1562. }
  1563. else
  1564. redirectexit('action=profile;u=' . $memID);
  1565. }
  1566. return true;
  1567. }
  1568. // Not right yet!
  1569. elseif ($_POST['authenticate'] == 'openid' && !empty($_POST['openid_identifier']))
  1570. {
  1571. require_once($sourcedir . '/Subs-OpenID.php');
  1572. $_POST['openid_identifier'] = smf_openID_canonize($_POST['openid_identifier']);
  1573. if (smf_openid_member_exists($_POST['openid_identifier']))
  1574. $post_errors[] = 'openid_in_use';
  1575. elseif (empty($post_errors))
  1576. {
  1577. // Authenticate using the new OpenID URI first to make sure they didn't make a mistake.
  1578. if ($context['user']['is_owner'])
  1579. {
  1580. $_SESSION['new_openid_uri'] = $_POST['openid_identifier'];
  1581. smf_openID_validate($_POST['openid_identifier'], false, null, 'change_uri');
  1582. }
  1583. else
  1584. updateMemberData($memID, array('openid_uri' => $_POST['openid_identifier']));
  1585. }
  1586. }
  1587. }
  1588. // Some stuff.
  1589. $context['member']['openid_uri'] = $cur_profile['openid_uri'];
  1590. $context['auth_method'] = empty($cur_profile['openid_uri']) ? 'password' : 'openid';
  1591. $context['sub_template'] = 'authentication_method';
  1592. loadJavascriptFile('register.js', array('default_theme' => true, 'defer' => false), 'smf_register');
  1593. }
  1594. /**
  1595. * Display the notifications and settings for changes.
  1596. *
  1597. * @param int $memID id_member
  1598. */
  1599. function notification($memID)
  1600. {
  1601. global $txt, $scripturl, $user_profile, $context, $smcFunc, $sourcedir;
  1602. // Going to want this for consistency.
  1603. loadCSSFile('admin.css', array(), 'admin');
  1604. // This is just a bootstrap for everything else.
  1605. $sa = array(
  1606. 'alerts' => 'alert_configuration',
  1607. 'markread' => 'alert_markread',
  1608. 'topics' => 'alert_notifications_topics',
  1609. 'boards' => 'alert_notifications_boards',
  1610. );
  1611. $subAction = !empty($_GET['sa']) && isset($sa[$_GET['sa']]) ? $_GET['sa'] : 'alerts';
  1612. $context['sub_template'] = $sa[$subAction];
  1613. $context[$context['profile_menu_name']]['tab_data'] = array(
  1614. 'title' => $txt['notification'],
  1615. 'help' => '',
  1616. 'description' => $txt['notification_info'],
  1617. );
  1618. $sa[$subAction]($memID);
  1619. }
  1620. function alert_configuration($memID)
  1621. {
  1622. global $txt, $scripturl, $user_profile, $context, $modSettings, $smcFunc, $sourcedir;
  1623. $context['token_check'] = 'profile-nt' . $memID;
  1624. is_not_guest();
  1625. if (!$context['user']['is_owner'])
  1626. isAllowedTo('profile_extra_any');
  1627. // What options are set?
  1628. $context['member'] += array(
  1629. 'notify_announcements' => $user_profile[$memID]['notify_announcements'],
  1630. 'notify_send_body' => $user_profile[$memID]['notify_send_body'],
  1631. 'notify_types' => $user_profile[$memID]['notify_types'],
  1632. 'notify_regularity' => $user_profile[$memID]['notify_regularity'],
  1633. );
  1634. loadThemeOptions($memID);
  1635. // Now load all the values for this user.
  1636. require_once($sourcedir . '/Subs-Notify.php');
  1637. $prefs = getNotifyPrefs($memID);
  1638. // And we might as well now perform the splicing of default values.
  1639. if (!empty($prefs[0]))
  1640. foreach ($prefs[0] as $this_pref => $value)
  1641. if (!isset($prefs[$memID][$this_pref]))
  1642. $prefs[$memID][$this_pref] = $value;
  1643. $context['alert_prefs'] = !empty($prefs[$memID]) ? $prefs[$memID] : array();
  1644. // Now for the exciting stuff.
  1645. // We have groups of items, each item has both an alert and an email key as well as an optional help string.
  1646. // Valid values for these keys are 'always', 'yes', 'never'; if using always or never you should add a help string.
  1647. $alert_types = array(
  1648. 'msg' => array(
  1649. 'topic_notify' => array('alert' => 'yes', 'email' => 'yes'),
  1650. 'board_notify' => array('alert' => 'yes', 'email' => 'yes'),
  1651. 'msg_mention' => array('alert' => 'yes', 'email' => 'yes'),
  1652. 'msg_quote' => array('alert' => 'yes', 'email' => 'yes'),
  1653. 'msg_like' => array('alert' => 'yes', 'email' => 'never'),
  1654. ),
  1655. 'pm' => array(
  1656. 'pm_new' => array('alert' => 'always', 'email' => 'yes', 'help' => 'alert_pm_new', 'permission' => array('name' => 'pm_read', 'is_board' => false)),
  1657. 'pm_reply' => array('alert' => 'always', 'email' => 'yes', 'help' => 'alert_pm_new', 'permission' => array('name' => 'pm_send', 'is_board' => false)),
  1658. ),
  1659. 'moderation' => array(
  1660. 'msg_report' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_board', 'is_board' => true)),
  1661. 'msg_report_reply' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_board', 'is_board' => true)),
  1662. 'member_report' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_forum', 'is_board' => false)),
  1663. 'member_report_reply' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_forum', 'is_board' => false)),
  1664. ),
  1665. 'members' => array(
  1666. 'member_register' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'moderate_forum', 'is_board' => false)),
  1667. 'request_group' => array('alert' => 'yes', 'email' => 'yes'),
  1668. 'warn_any' => array('alert' => 'yes', 'email' => 'yes', 'permission' => array('name' => 'issue_warning', 'is_board' => false)),
  1669. ),
  1670. 'calendar' => array(
  1671. 'event_new' => array('alert' => 'yes', 'email' => 'yes', 'help' => 'alert_event_new'),
  1672. ),
  1673. );
  1674. $group_options = array(
  1675. 'msg' => array(
  1676. array('check', 'msg_auto_notify', 'label' => 'after'),
  1677. array('select', 'msg_notify_pref', 'label' => 'before', 'opts' => array(
  1678. 0 => $txt['alert_opt_msg_notify_pref_nothing'],
  1679. 1 => $txt['alert_opt_msg_notify_pref_instant'],
  1680. 2 => $txt['alert_opt_msg_notify_pref_first'],
  1681. 3 => $txt['alert_opt_msg_notify_pref_daily'],
  1682. 4 => $txt['alert_opt_msg_notify_pref_weekly'],
  1683. )),
  1684. ),
  1685. );
  1686. $disabled_options = array();
  1687. // There are certain things that are disabled at the group level.
  1688. if (empty($modSettings['cal_enabled']))
  1689. {
  1690. foreach ($alert_types['calendar'] as $k => $v)
  1691. $disabled_options[] = $k;
  1692. unset ($alert_types['calendar']);
  1693. }
  1694. // Now, now, we could pass this through global but we should really get into the habit of
  1695. // passing content to hooks, not expecting hooks to splatter everything everywhere.
  1696. call_integration_hook('integrate_alert_types', array(&$alert_types, &$group_options, &$disabled_options));
  1697. // Now we have to do some permissions testing.
  1698. require_once($sourcedir . '/Subs-Members.php');
  1699. $perms_cache = array();
  1700. foreach ($alert_types as $group => $items)
  1701. {
  1702. foreach ($items as $alert_key => $alert_value)
  1703. {
  1704. if (!isset($alert_value['permission']))
  1705. continue;
  1706. if (!isset($perms_cache[$alert_value['permission']['name']]))
  1707. {
  1708. $in_board = !empty($alert_value['permission']['is_board']) ? 0 : null;
  1709. $members = membersAllowedTo($alert_value['permission']['name'], $in_board);
  1710. $perms_cache[$alert_value['permission']['name']] = in_array($memID, $members);
  1711. }
  1712. if (!$perms_cache[$alert_value['permission']['name']])
  1713. {
  1714. $disabled_options[] = $alert_key;
  1715. unset ($alert_types[$group][$alert_key]);
  1716. }
  1717. }
  1718. if (empty($alert_types[$group]))
  1719. unset ($alert_types[$group]);
  1720. }
  1721. // Slightly different for group requests
  1722. $request = $smcFunc['db_query']('', '
  1723. SELECT COUNT(*)
  1724. FROM {db_prefix}group_moderators
  1725. WHERE id_member = {int:memID}',
  1726. array(
  1727. 'memID' => $memID,
  1728. )
  1729. );
  1730. list($can_mod) = $smcFunc['db_fetch_row']($request);
  1731. if (!isset($perms_cache['manage_membergroups']))
  1732. {
  1733. $members = membersAllowedTo('manage_membergroups');
  1734. $perms_cache['manage_membergroups'] = in_array($memID, $members);
  1735. }
  1736. if (!($perms_cache['manage_membergroups'] || $can_mod != 0))
  1737. unset($alert_types['members']['request_group']);
  1738. // And finally, exporting it to be useful later.
  1739. $context['alert_types'] = $alert_types;
  1740. $context['alert_group_options'] = $group_options;
  1741. $context['disabled_alerts'] = $disabled_options;
  1742. $context['alert_bits'] = array(
  1743. 'alert' => 0x01,
  1744. 'email' => 0x02,
  1745. );
  1746. if (isset($_POST['notify_submit']))
  1747. {
  1748. checkSession();
  1749. validateToken($context['token_check'], 'post');
  1750. // We need to step through the list of valid settings and figure out what the user has set.
  1751. $update_prefs = array();
  1752. // Now the group level options
  1753. foreach ($context['alert_group_options'] as $opt_group => $group)
  1754. {
  1755. foreach ($group as $this_option)
  1756. {
  1757. switch ($this_option[0])
  1758. {
  1759. case 'check':
  1760. $update_prefs[$this_option[1]] = !empty($_POST['opt_' . $this_option[1]]) ? 1 : 0;
  1761. break;
  1762. case 'select':
  1763. if (isset($_POST['opt_' . $this_option[1]], $this_option['opts'][$_POST['opt_' . $this_option[1]]]))
  1764. $update_prefs[$this_option[1]] = $_POST['opt_' . $this_option[1]];
  1765. else
  1766. {
  1767. // We didn't have a sane value. Let's grab the first item from the possibles.
  1768. $keys = array_keys($this_option['opts']);
  1769. $first = array_shift($keys);
  1770. $update_prefs[$this_option[1]] = $first;
  1771. }
  1772. break;
  1773. }
  1774. }
  1775. }
  1776. // Now the individual options
  1777. foreach ($context['alert_types'] as $alert_group => $items)
  1778. {
  1779. foreach ($items as $item_key => $this_options)
  1780. {
  1781. $this_value = 0;
  1782. foreach ($context['alert_bits'] as $type => $bitvalue)
  1783. {
  1784. if ($this_options[$type] == 'yes' && !empty($_POST[$type . '_' . $item_key]))
  1785. $this_value |= $bitvalue;
  1786. }
  1787. $update_prefs[$item_key] = $this_value;
  1788. }
  1789. }
  1790. setNotifyPrefs($memID, $update_prefs);
  1791. foreach ($update_prefs as $pref => $value)
  1792. $context['alert_prefs'][$pref] = $value;
  1793. makeNotificationChanges($memID);
  1794. $context['profile_updated'] = $txt['profile_updated_own'];
  1795. }
  1796. createToken($context['token_check'], 'post');
  1797. }
  1798. function alert_markread($memID)
  1799. {
  1800. global $context, $db_show_debug, $smcFunc;
  1801. // We do not want to output debug information here.
  1802. $db_show_debug = false;
  1803. // We only want to output our little layer here.
  1804. $context['template_layers'] = array();
  1805. $context['sub_template'] = 'alerts_all_read';
  1806. loadLanguage('Alerts');
  1807. // Now we're all set up.
  1808. is_not_guest();
  1809. if (!$context['user']['is_owner'])
  1810. fatal_error('no_access');
  1811. checkSession('get');
  1812. // Assuming we're here, mark everything as read and head back.
  1813. // We only spit back the little layer because this should be called AJAXively.
  1814. $smcFunc['db_query']('', '
  1815. UPDATE {db_prefix}user_alerts
  1816. SET is_read = {int:now}
  1817. WHERE id_member = {int:current_member}
  1818. AND is_read = 0',
  1819. array(
  1820. 'now' => time(),
  1821. 'current_member' => $memID,
  1822. )
  1823. );
  1824. updateMemberData($memID, array('alerts' => 0));
  1825. }
  1826. function alert_notifications_topics($memID)
  1827. {
  1828. global $txt, $scripturl, $user_profile, $context, $modSettings, $smcFunc, $sourcedir;
  1829. // Because of the way this stuff works, we want to do this ourselves.
  1830. if (isset($_POST['edit_notify_topics']))
  1831. {
  1832. checkSession();
  1833. validateToken(str_replace('%u', $memID, 'profile-nt%u'), 'post');
  1834. makeNotificationChanges($memID);
  1835. $context['profile_updated'] = $txt['profile_updated_own'];
  1836. }
  1837. // Now set up for the token check.
  1838. $context['token_check'] = str_replace('%u', $memID, 'profile-nt%u');
  1839. createToken($context['token_check'], 'post');
  1840. // Gonna want this for the list.
  1841. require_once($sourcedir . '/Subs-List.php');
  1842. // Do the topic notifications.
  1843. $listOptions = array(
  1844. 'id' => 'topic_notification_list',
  1845. 'width' => '100%',
  1846. 'items_per_page' => $modSettings['defaultMaxMessages'],
  1847. 'no_items_label' => $txt['notifications_topics_none'] . '<br><br>' . $txt['notifications_topics_howto'],
  1848. 'no_items_align' => 'left',
  1849. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification;sa=topics',
  1850. 'default_sort_col' => 'last_post',
  1851. 'get_items' => array(
  1852. 'function' => 'list_getTopicNotifications',
  1853. 'params' => array(
  1854. $memID,
  1855. ),
  1856. ),
  1857. 'get_count' => array(
  1858. 'function' => 'list_getTopicNotificationCount',
  1859. 'params' => array(
  1860. $memID,
  1861. ),
  1862. ),
  1863. 'columns' => array(
  1864. 'subject' => array(
  1865. 'header' => array(
  1866. 'value' => $txt['notifications_topics'],
  1867. 'class' => 'lefttext first_th',
  1868. ),
  1869. 'data' => array(
  1870. 'function' => create_function('$topic', '
  1871. global $txt;
  1872. $link = $topic[\'link\'];
  1873. if ($topic[\'new\'])
  1874. $link .= \' <a href="\' . $topic[\'new_href\'] . \'"><span class="new_posts">\' . $txt[\'new\'] . \'</span></a>\';
  1875. $link .= \'<br><span class="smalltext"><em>\' . $txt[\'in\'] . \' \' . $topic[\'board_link\'] . \'</em></span>\';
  1876. return $link;
  1877. '),
  1878. ),
  1879. 'sort' => array(
  1880. 'default' => 'ms.subject',
  1881. 'reverse' => 'ms.subject DESC',
  1882. ),
  1883. ),
  1884. 'started_by' => array(
  1885. 'header' => array(
  1886. 'value' => $txt['started_by'],
  1887. 'class' => 'lefttext',
  1888. ),
  1889. 'data' => array(
  1890. 'db' => 'poster_link',
  1891. ),
  1892. 'sort' => array(
  1893. 'default' => 'real_name_col',
  1894. 'reverse' => 'real_name_col DESC',
  1895. ),
  1896. ),
  1897. 'last_post' => array(
  1898. 'header' => array(
  1899. 'value' => $txt['last_post'],
  1900. 'class' => 'lefttext',
  1901. ),
  1902. 'data' => array(
  1903. 'sprintf' => array(
  1904. 'format' => '<span class="smalltext">%1$s<br>' . $txt['by'] . ' %2$s</span>',
  1905. 'params' => array(
  1906. 'updated' => false,
  1907. 'poster_updated_link' => false,
  1908. ),
  1909. ),
  1910. ),
  1911. 'sort' => array(
  1912. 'default' => 'ml.id_msg DESC',
  1913. 'reverse' => 'ml.id_msg',
  1914. ),
  1915. ),
  1916. 'delete' => array(
  1917. 'header' => array(
  1918. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);">',
  1919. 'style' => 'width: 4%;',
  1920. 'class' => 'centercol',
  1921. ),
  1922. 'data' => array(
  1923. 'sprintf' => array(
  1924. 'format' => '<input type="checkbox" name="notify_topics[]" value="%1$d" class="input_check">',
  1925. 'params' => array(
  1926. 'id' => false,
  1927. ),
  1928. ),
  1929. 'class' => 'centercol',
  1930. ),
  1931. ),
  1932. ),
  1933. 'form' => array(
  1934. 'href' => $scripturl . '?action=profile;area=notification;sa=topics',
  1935. 'include_sort' => true,
  1936. 'include_start' => true,
  1937. 'hidden_fields' => array(
  1938. 'u' => $memID,
  1939. 'sa' => $context['menu_item_selected'],
  1940. $context['session_var'] => $context['session_id'],
  1941. ),
  1942. 'token' => $context['token_check'],
  1943. ),
  1944. 'additional_rows' => array(
  1945. array(
  1946. 'position' => 'bottom_of_list',
  1947. 'value' => '<input type="submit" name="edit_notify_topics" value="' . $txt['notifications_update'] . '" class="button_submit">',
  1948. 'align' => 'right',
  1949. ),
  1950. ),
  1951. );
  1952. // Create the notification list.
  1953. createList($listOptions);
  1954. }
  1955. function alert_notifications_boards($memID)
  1956. {
  1957. global $txt, $scripturl, $user_profile, $context, $smcFunc, $sourcedir;
  1958. // Because of the way this stuff works, we want to do this ourselves.
  1959. if (isset($_POST['edit_notify_boards']))
  1960. {
  1961. checkSession();
  1962. validateToken(str_replace('%u', $memID, 'profile-nt%u'), 'post');
  1963. makeNotificationChanges($memID);
  1964. $context['profile_updated'] = $txt['profile_updated_own'];
  1965. }
  1966. // Now set up for the token check.
  1967. $context['token_check'] = str_replace('%u', $memID, 'profile-nt%u');
  1968. createToken($context['token_check'], 'post');
  1969. // Gonna want this for the list.
  1970. require_once($sourcedir . '/Subs-List.php');
  1971. // Fine, start with the board list.
  1972. $listOptions = array(
  1973. 'id' => 'board_notification_list',
  1974. 'width' => '100%',
  1975. 'no_items_label' => $txt['notifications_boards_none'] . '<br><br>' . $txt['notifications_boards_howto'],
  1976. 'no_items_align' => 'left',
  1977. 'base_href' => $scripturl . '?action=profile;u=' . $memID . ';area=notification;sa=boards',
  1978. 'default_sort_col' => 'board_name',
  1979. 'get_items' => array(
  1980. 'function' => 'list_getBoardNotifications',
  1981. 'params' => array(
  1982. $memID,
  1983. ),
  1984. ),
  1985. 'columns' => array(
  1986. 'board_name' => array(
  1987. 'header' => array(
  1988. 'value' => $txt['notifications_boards'],
  1989. 'class' => 'lefttext first_th',
  1990. ),
  1991. 'data' => array(
  1992. 'function' => create_function('$board', '
  1993. global $txt;
  1994. $link = $board[\'link\'];
  1995. if ($board[\'new\'])
  1996. $link .= \' <a href="\' . $board[\'href\'] . \'"><span class="new_posts">' . $txt['new'] . '</span></a>\';
  1997. return $link;
  1998. '),
  1999. ),
  2000. 'sort' => array(
  2001. 'default' => 'name',
  2002. 'reverse' => 'name DESC',
  2003. ),
  2004. ),
  2005. 'delete' => array(
  2006. 'header' => array(
  2007. 'value' => '<input type="checkbox" class="input_check" onclick="invertAll(this, this.form);">',
  2008. 'style' => 'width: 4%;',
  2009. 'class' => 'centercol',
  2010. ),
  2011. 'data' => array(
  2012. 'sprintf' => array(
  2013. 'format' => '<input type="checkbox" name="notify_boards[]" value="%1$d" class="input_check">',
  2014. 'params' => array(
  2015. 'id' => false,
  2016. ),
  2017. ),
  2018. 'class' => 'centercol',
  2019. ),
  2020. ),
  2021. ),
  2022. 'form' => array(
  2023. 'href' => $scripturl . '?action=profile;area=notification;sa=boards',
  2024. 'include_sort' => true,
  2025. 'include_start' => true,
  2026. 'hidden_fields' => array(
  2027. 'u' => $memID,
  2028. 'sa' => $context['menu_item_selected'],
  2029. $context['session_var'] => $context['session_id'],
  2030. ),
  2031. 'token' => $context['token_check'],
  2032. ),
  2033. 'additional_rows' => array(
  2034. array(
  2035. 'position' => 'bottom_of_list',
  2036. 'value' => '<input type="submit" name="edit_notify_boards" value="' . $txt['notifications_update'] . '" class="button_submit">',
  2037. 'align' => 'right',
  2038. ),
  2039. ),
  2040. );
  2041. // Create the board notification list.
  2042. createList($listOptions);
  2043. }
  2044. /**
  2045. * @todo needs a description
  2046. *
  2047. * @param int $memID id_member
  2048. * @return string
  2049. */
  2050. function list_getTopicNotificationCount($memID)
  2051. {
  2052. global $smcFunc, $user_info, $context, $modSettings;
  2053. $request = $smcFunc['db_query']('', '
  2054. SELECT COUNT(*)
  2055. FROM {db_prefix}log_notify AS ln' . (!$modSettings['postmod_active'] && $user_info['query_see_board'] === '1=1' ? '' : '
  2056. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic)') . ($user_info['query_see_board'] === '1=1' ? '' : '
  2057. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board)') . '
  2058. WHERE ln.id_member = {int:selected_member}' . ($user_info['query_see_board'] === '1=1' ? '' : '
  2059. AND {query_see_board}') . ($modSettings['postmod_active'] ? '
  2060. AND t.approved = {int:is_approved}' : ''),
  2061. array(
  2062. 'selected_member' => $memID,
  2063. 'is_approved' => 1,
  2064. )
  2065. );
  2066. list ($totalNotifications) = $smcFunc['db_fetch_row']($request);
  2067. $smcFunc['db_free_result']($request);
  2068. // @todo make this an integer before it gets returned
  2069. return $totalNotifications;
  2070. }
  2071. /**
  2072. * @todo Needs a description
  2073. *
  2074. * @param int $start
  2075. * @param int $items_per_page
  2076. * @param string $sort
  2077. * @param int $memID id_member
  2078. * @return array $notification_topics
  2079. */
  2080. function list_getTopicNotifications($start, $items_per_page, $sort, $memID)
  2081. {
  2082. global $smcFunc, $txt, $scripturl, $user_info, $context, $modSettings;
  2083. // All the topics with notification on...
  2084. $request = $smcFunc['db_query']('', '
  2085. SELECT
  2086. IFNULL(lt.id_msg, IFNULL(lmr.id_msg, -1)) + 1 AS new_from, b.id_board, b.name,
  2087. t.id_topic, ms.subject, ms.id_member, IFNULL(mem.real_name, ms.poster_name) AS real_name_col,
  2088. ml.id_msg_modified, ml.poster_time, ml.id_member AS id_member_updated,
  2089. IFNULL(mem2.real_name, ml.poster_name) AS last_real_name
  2090. FROM {db_prefix}log_notify AS ln
  2091. INNER JOIN {db_prefix}topics AS t ON (t.id_topic = ln.id_topic' . ($modSettings['postmod_active'] ? ' AND t.approved = {int:is_approved}' : '') . ')
  2092. INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board AND {query_see_board})
  2093. INNER JOIN {db_prefix}messages AS ms ON (ms.id_msg = t.id_first_msg)
  2094. INNER JOIN {db_prefix}messages AS ml ON (ml.id_msg = t.id_last_msg)
  2095. LEFT JOIN {db_prefix}members AS mem ON (mem.id_member = ms.id_member)
  2096. LEFT JOIN {db_prefix}members AS mem2 ON (mem2.id_member = ml.id_member)
  2097. LEFT JOIN {db_prefix}log_topics AS lt ON (lt.id_topic = t.id_topic AND lt.id_member = {int:current_member})
  2098. LEFT JOIN {db_prefix}log_mark_read AS lmr ON (lmr.id_board = b.id_board AND lmr.id_member = {int:current_member})
  2099. WHERE ln.id_member = {int:selected_member}
  2100. ORDER BY {raw:sort}
  2101. LIMIT {int:offset}, {int:items_per_page}',
  2102. array(
  2103. 'current_member' => $user_info['id'],
  2104. 'is_approved' => 1,
  2105. 'selected_member' => $memID,
  2106. 'sort' => $sort,
  2107. 'offset' => $start,
  2108. 'items_per_page' => $items_per_page,
  2109. )
  2110. );
  2111. $notification_topics = array();
  2112. while ($row = $smcFunc['db_fetch_assoc']($request))
  2113. {
  2114. censorText($row['subject']);
  2115. $notification_topics[] = array(
  2116. 'id' => $row['id_topic'],
  2117. 'poster_link' => empty($row['id_member']) ? $row['real_name_col'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['real_name_col'] . '</a>',
  2118. 'poster_updated_link' => empty($row['id_member_updated']) ? $row['last_real_name'] : '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member_updated'] . '">' . $row['last_real_name'] . '</a>',
  2119. 'subject' => $row['subject'],
  2120. 'href' => $scripturl . '?topic=' . $row['id_topic'] . '.0',
  2121. 'link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['subject'] . '</a>',
  2122. 'new' => $row['new_from'] <= $row['id_msg_modified'],
  2123. 'new_from' => $row['new_from'],
  2124. 'updated' => timeformat($row['poster_time']),
  2125. 'new_href' => $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new',
  2126. 'new_link' => '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.msg' . $row['new_from'] . '#new">' . $row['subject'] . '</a>',
  2127. 'board_link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  2128. );
  2129. }
  2130. $smcFunc['db_free_result']($request);
  2131. return $notification_topics;
  2132. }
  2133. /**
  2134. * @todo needs a description
  2135. *
  2136. * @param int $start
  2137. * @param int $items_per_page
  2138. * @param string $sort
  2139. * @param int $memID id_member
  2140. * @return array
  2141. */
  2142. function list_getBoardNotifications($start, $items_per_page, $sort, $memID)
  2143. {
  2144. global $smcFunc, $txt, $scripturl, $user_info;
  2145. $request = $smcFunc['db_query']('', '
  2146. SELECT b.id_board, b.name, IFNULL(lb.id_msg, 0) AS board_read, b.id_msg_updated
  2147. FROM {db_prefix}log_notify AS ln
  2148. INNER JOIN {db_prefix}boards AS b ON (b.id_board = ln.id_board)
  2149. LEFT JOIN {db_prefix}log_boards AS lb ON (lb.id_board = b.id_board AND lb.id_member = {int:current_member})
  2150. WHERE ln.id_member = {int:selected_member}
  2151. AND {query_see_board}
  2152. ORDER BY ' . $sort,
  2153. array(
  2154. 'current_member' => $user_info['id'],
  2155. 'selected_member' => $memID,
  2156. )
  2157. );
  2158. $notification_boards = array();
  2159. while ($row = $smcFunc['db_fetch_assoc']($request))
  2160. $notification_boards[] = array(
  2161. 'id' => $row['id_board'],
  2162. 'name' => $row['name'],
  2163. 'href' => $scripturl . '?board=' . $row['id_board'] . '.0',
  2164. 'link' => '<a href="' . $scripturl . '?board=' . $row['id_board'] . '.0">' . $row['name'] . '</a>',
  2165. 'new' => $row['board_read'] < $row['id_msg_updated']
  2166. );
  2167. $smcFunc['db_free_result']($request);
  2168. return $notification_boards;
  2169. }
  2170. /**
  2171. * @todo needs a description
  2172. *
  2173. * @param int $memID id_member
  2174. */
  2175. function loadThemeOptions($memID)
  2176. {
  2177. global $context, $options, $cur_profile, $smcFunc;
  2178. if (isset($_POST['default_options']))
  2179. $_POST['options'] = isset($_POST['options']) ? $_POST['options'] + $_POST['default_options'] : $_POST['default_options'];
  2180. if ($context['user']['is_owner'])
  2181. {
  2182. $context['member']['options'] = $options;
  2183. if (isset($_POST['options']) && is_array($_POST['options']))
  2184. foreach ($_POST['options'] as $k => $v)
  2185. $context['member']['options'][$k] = $v;
  2186. }
  2187. else
  2188. {
  2189. $request = $smcFunc['db_query']('', '
  2190. SELECT id_member, variable, value
  2191. FROM {db_prefix}themes
  2192. WHERE id_theme IN (1, {int:member_theme})
  2193. AND id_member IN (-1, {int:selected_member})',
  2194. array(
  2195. 'member_theme' => (int) $cur_profile['id_theme'],
  2196. 'selected_member' => $memID,
  2197. )
  2198. );
  2199. $temp = array();
  2200. while ($row = $smcFunc['db_fetch_assoc']($request))
  2201. {
  2202. if ($row['id_member'] == -1)
  2203. {
  2204. $temp[$row['variable']] = $row['value'];
  2205. continue;
  2206. }
  2207. if (isset($_POST['options'][$row['variable']]))
  2208. $row['value'] = $_POST['options'][$row['variable']];
  2209. $context['member']['options'][$row['variable']] = $row['value'];
  2210. }
  2211. $smcFunc['db_free_result']($request);
  2212. // Load up the default theme options for any missing.
  2213. foreach ($temp as $k => $v)
  2214. {
  2215. if (!isset($context['member']['options'][$k]))
  2216. $context['member']['options'][$k] = $v;
  2217. }
  2218. }
  2219. }
  2220. /**
  2221. * @todo needs a description
  2222. *
  2223. * @param int $memID id_member
  2224. */
  2225. function ignoreboards($memID)
  2226. {
  2227. global $txt, $context, $modSettings, $smcFunc, $cur_profile;
  2228. // Have the admins enabled this option?
  2229. if (empty($modSettings['allow_ignore_boards']))
  2230. fatal_lang_error('ignoreboards_disallowed', 'user');
  2231. // Find all the boards this user is allowed to see.
  2232. $request = $smcFunc['db_query']('order_by_board_order', '
  2233. SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level,
  2234. '. (!empty($cur_profile['ignore_boards']) ? 'b.id_board IN ({array_int:ignore_boards})' : '0') . ' AS is_ignored
  2235. FROM {db_prefix}boards AS b
  2236. LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
  2237. WHERE {query_see_board}
  2238. AND redirect = {string:empty_string}',
  2239. array(
  2240. 'ignore_boards' => !empty($cur_profile['ignore_boards']) ? explode(',', $cur_profile['ignore_boards']) : array(),
  2241. 'empty_string' => '',
  2242. )
  2243. );
  2244. $context['num_boards'] = $smcFunc['db_num_rows']($request);
  2245. $context['categories'] = array();
  2246. while ($row = $smcFunc['db_fetch_assoc']($request))
  2247. {
  2248. // This category hasn't been set up yet..
  2249. if (!isset($context['categories'][$row['id_cat']]))
  2250. $context['categories'][$row['id_cat']] = array(
  2251. 'id' => $row['id_cat'],
  2252. 'name' => $row['cat_name'],
  2253. 'boards' => array()
  2254. );
  2255. // Set this board up, and let the template know when it's a child. (indent them..)
  2256. $context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
  2257. 'id' => $row['id_board'],
  2258. 'name' => $row['name'],
  2259. 'child_level' => $row['child_level'],
  2260. 'selected' => $row['is_ignored'],
  2261. );
  2262. }
  2263. $smcFunc['db_free_result']($request);
  2264. // Now, let's sort the list of categories into the boards for templates that like that.
  2265. $temp_boards = array();
  2266. foreach ($context['categories'] as $category)
  2267. {
  2268. // Include a list of boards per category for easy toggling.
  2269. $context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
  2270. $temp_boards[] = array(
  2271. 'name' => $category['name'],
  2272. 'child_ids' => array_keys($category['boards'])
  2273. );
  2274. $temp_boards = array_merge($temp_boards, array_values($category['boards']));
  2275. }
  2276. $max_boards = ceil(count($temp_boards) / 2);
  2277. if ($max_boards == 1)
  2278. $max_boards = 2;
  2279. // Now, alternate them so they can be shown left and right ;).
  2280. $context['board_columns'] = array();
  2281. for ($i = 0; $i < $max_boards; $i++)
  2282. {
  2283. $context['board_columns'][] = $temp_boards[$i];
  2284. if (isset($temp_boards[$i + $max_boards]))
  2285. $context['board_columns'][] = $temp_boards[$i + $max_boards];
  2286. else
  2287. $context['board_columns'][] = array();
  2288. }
  2289. loadThemeOptions($memID);
  2290. }
  2291. /**
  2292. * Load all the languages for the profile.
  2293. * @return boolean
  2294. */
  2295. function profileLoadLanguages()
  2296. {
  2297. global $context, $cur_profile, $language, $smcFunc;
  2298. $context['profile_languages'] = array();
  2299. // Get our languages!
  2300. getLanguages(true, true);
  2301. // Setup our languages.
  2302. foreach ($context['languages'] as $lang)
  2303. {
  2304. $context['profile_languages'][$lang['filename']] = strtr($lang['name'], array('-utf8' => ''));
  2305. }
  2306. ksort($context['profile_languages']);
  2307. // Return whether we should proceed with this.
  2308. return count($context['profile_languages']) > 1 ? true : false;
  2309. }
  2310. /**
  2311. * @todo needs description
  2312. *
  2313. * @return true
  2314. */
  2315. function profileLoadGroups()
  2316. {
  2317. global $cur_profile, $txt, $context, $smcFunc, $user_settings;
  2318. $context['member_groups'] = array(
  2319. 0 => array(
  2320. 'id' => 0,
  2321. 'name' => $txt['no_primary_membergroup'],
  2322. 'is_primary' => $cur_profile['id_group'] == 0,
  2323. 'can_be_additional' => false,
  2324. 'can_be_primary' => true,
  2325. )
  2326. );
  2327. $curGroups = explode(',', $cur_profile['additional_groups']);
  2328. // Load membergroups, but only those groups the user can assign.
  2329. $request = $smcFunc['db_query']('', '
  2330. SELECT group_name, id_group, hidden
  2331. FROM {db_prefix}membergroups
  2332. WHERE id_group != {int:moderator_group}
  2333. AND min_posts = {int:min_posts}' . (allowedTo('admin_forum') ? '' : '
  2334. AND group_type != {int:is_protected}') . '
  2335. ORDER BY min_posts, CASE WHEN id_group < {int:newbie_group} THEN id_group ELSE 4 END, group_name',
  2336. array(
  2337. 'moderator_group' => 3,
  2338. 'min_posts' => -1,
  2339. 'is_protected' => 1,
  2340. 'newbie_group' => 4,
  2341. )
  2342. );
  2343. while ($row = $smcFunc['db_fetch_assoc']($request))
  2344. {
  2345. // We should skip the administrator group if they don't have the admin_forum permission!
  2346. if ($row['id_group'] == 1 && !allowedTo('admin_forum'))
  2347. continue;
  2348. $context['member_groups'][$row['id_group']] = array(
  2349. 'id' => $row['id_group'],
  2350. 'name' => $row['group_name'],
  2351. 'is_primary' => $cur_profile['id_group'] == $row['id_group'],
  2352. 'is_additional' => in_array($row['id_group'], $curGroups),
  2353. 'can_be_additional' => true,
  2354. 'can_be_primary' => $row['hidden'] != 2,
  2355. );
  2356. }
  2357. $smcFunc['db_free_result']($request);
  2358. $context['member']['group_id'] = $user_settings['id_group'];
  2359. return true;
  2360. }
  2361. /**
  2362. * Load key signature context data.
  2363. *
  2364. * @return true
  2365. */
  2366. function profileLoadSignatureData()
  2367. {
  2368. global $modSettings, $context, $txt, $cur_profile, $smcFunc, $memberContext;
  2369. // Signature limits.
  2370. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2371. $sig_limits = explode(',', $sig_limits);
  2372. $context['signature_enabled'] = isset($sig_limits[0]) ? $sig_limits[0] : 0;
  2373. $context['signature_limits'] = array(
  2374. 'max_length' => isset($sig_limits[1]) ? $sig_limits[1] : 0,
  2375. 'max_lines' => isset($sig_limits[2]) ? $sig_limits[2] : 0,
  2376. 'max_images' => isset($sig_limits[3]) ? $sig_limits[3] : 0,
  2377. 'max_smileys' => isset($sig_limits[4]) ? $sig_limits[4] : 0,
  2378. 'max_image_width' => isset($sig_limits[5]) ? $sig_limits[5] : 0,
  2379. 'max_image_height' => isset($sig_limits[6]) ? $sig_limits[6] : 0,
  2380. 'max_font_size' => isset($sig_limits[7]) ? $sig_limits[7] : 0,
  2381. 'bbc' => !empty($sig_bbc) ? explode(',', $sig_bbc) : array(),
  2382. );
  2383. // Kept this line in for backwards compatibility!
  2384. $context['max_signature_length'] = $context['signature_limits']['max_length'];
  2385. // Warning message for signature image limits?
  2386. $context['signature_warning'] = '';
  2387. if ($context['signature_limits']['max_image_width'] && $context['signature_limits']['max_image_height'])
  2388. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_size'], $context['signature_limits']['max_image_width'], $context['signature_limits']['max_image_height']);
  2389. elseif ($context['signature_limits']['max_image_width'] || $context['signature_limits']['max_image_height'])
  2390. $context['signature_warning'] = sprintf($txt['profile_error_signature_max_image_' . ($context['signature_limits']['max_image_width'] ? 'width' : 'height')], $context['signature_limits'][$context['signature_limits']['max_image_width'] ? 'max_image_width' : 'max_image_height']);
  2391. $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && (function_exists('pspell_new') || (function_exists('enchant_broker_init') && ($txt['lang_charset'] == 'UTF-8' || function_exists('iconv'))));
  2392. if (empty($context['do_preview']))
  2393. $context['member']['signature'] = empty($cur_profile['signature']) ? '' : str_replace(array('<br>', '<', '>', '"', '\''), array("\n", '&lt;', '&gt;', '&quot;', '&#039;'), $cur_profile['signature']);
  2394. else
  2395. {
  2396. $signature = !empty($_POST['signature']) ? $_POST['signature'] : '';
  2397. $validation = profileValidateSignature($signature);
  2398. if (empty($context['post_errors']))
  2399. {
  2400. loadLanguage('Errors');
  2401. $context['post_errors'] = array();
  2402. }
  2403. $context['post_errors'][] = 'signature_not_yet_saved';
  2404. if ($validation !== true && $validation !== false)
  2405. $context['post_errors'][] = $validation;
  2406. censorText($context['member']['signature']);
  2407. $context['member']['current_signature'] = $context['member']['signature'];
  2408. censorText($signature);
  2409. $context['member']['signature_preview'] = parse_bbc($signature, true, 'sig' . $memberContext[$context['id_member']]);
  2410. $context['member']['signature'] = $_POST['signature'];
  2411. }
  2412. // Load the spell checker?
  2413. if ($context['show_spellchecking'])
  2414. loadJavascriptFile('spellcheck.js', array('default_theme' => true, 'defer' => false), 'smf_spellcheck');
  2415. return true;
  2416. }
  2417. /**
  2418. * Load avatar context data.
  2419. *
  2420. * @return true
  2421. */
  2422. function profileLoadAvatarData()
  2423. {
  2424. global $context, $cur_profile, $modSettings, $scripturl;
  2425. $context['avatar_url'] = $modSettings['avatar_url'];
  2426. // Default context.
  2427. $context['member']['avatar'] += array(
  2428. 'custom' => stristr($cur_profile['avatar'], 'http://') || stristr($cur_profile['avatar'], 'https://') ? $cur_profile['avatar'] : 'http://',
  2429. 'selection' => $cur_profile['avatar'] == '' || (stristr($cur_profile['avatar'], 'http://') || stristr($cur_profile['avatar'], 'https://')) ? '' : $cur_profile['avatar'],
  2430. 'id_attach' => $cur_profile['id_attach'],
  2431. 'filename' => $cur_profile['filename'],
  2432. 'allow_server_stored' => allowedTo('profile_server_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2433. 'allow_upload' => allowedTo('profile_upload_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2434. 'allow_external' => allowedTo('profile_remote_avatar') || (!$context['user']['is_owner'] && allowedTo('profile_extra_any')),
  2435. );
  2436. if ($cur_profile['avatar'] == '' && $cur_profile['id_attach'] > 0 && $context['member']['avatar']['allow_upload'])
  2437. {
  2438. $context['member']['avatar'] += array(
  2439. 'choice' => 'upload',
  2440. 'server_pic' => 'blank.png',
  2441. 'external' => 'http://'
  2442. );
  2443. $context['member']['avatar']['href'] = empty($cur_profile['attachment_type']) ? $scripturl . '?action=dlattach;attach=' . $cur_profile['id_attach'] . ';type=avatar' : $modSettings['custom_avatar_url'] . '/' . $cur_profile['filename'];
  2444. }
  2445. elseif ((stristr($cur_profile['avatar'], 'http://') || stristr($cur_profile['avatar'], 'https://')) && $context['member']['avatar']['allow_external'])
  2446. $context['member']['avatar'] += array(
  2447. 'choice' => 'external',
  2448. 'server_pic' => 'blank.png',
  2449. 'external' => $cur_profile['avatar']
  2450. );
  2451. elseif ($cur_profile['avatar'] != '' && file_exists($modSettings['avatar_directory'] . '/' . $cur_profile['avatar']) && $context['member']['avatar']['allow_server_stored'])
  2452. $context['member']['avatar'] += array(
  2453. 'choice' => 'server_stored',
  2454. 'server_pic' => $cur_profile['avatar'] == '' ? 'blank.png' : $cur_profile['avatar'],
  2455. 'external' => 'http://'
  2456. );
  2457. else
  2458. $context['member']['avatar'] += array(
  2459. 'choice' => 'none',
  2460. 'server_pic' => 'blank.png',
  2461. 'external' => 'http://'
  2462. );
  2463. // Get a list of all the avatars.
  2464. if ($context['member']['avatar']['allow_server_stored'])
  2465. {
  2466. $context['avatar_list'] = array();
  2467. $context['avatars'] = is_dir($modSettings['avatar_directory']) ? getAvatars('', 0) : array();
  2468. }
  2469. else
  2470. $context['avatars'] = array();
  2471. // Second level selected avatar...
  2472. $context['avatar_selected'] = substr(strrchr($context['member']['avatar']['server_pic'], '/'), 1);
  2473. return !empty($context['member']['avatar']['allow_server_stored']) || !empty($context['member']['avatar']['allow_external']) || !empty($context['member']['avatar']['allow_upload']);
  2474. }
  2475. /**
  2476. * Save a members group.
  2477. *
  2478. * @param int &$value
  2479. * @return true
  2480. */
  2481. function profileSaveGroups(&$value)
  2482. {
  2483. global $profile_vars, $old_profile, $context, $smcFunc, $cur_profile;
  2484. // Do we need to protect some groups?
  2485. if (!allowedTo('admin_forum'))
  2486. {
  2487. $request = $smcFunc['db_query']('', '
  2488. SELECT id_group
  2489. FROM {db_prefix}membergroups
  2490. WHERE group_type = {int:is_protected}',
  2491. array(
  2492. 'is_protected' => 1,
  2493. )
  2494. );
  2495. $protected_groups = array(1);
  2496. while ($row = $smcFunc['db_fetch_assoc']($request))
  2497. $protected_groups[] = $row['id_group'];
  2498. $smcFunc['db_free_result']($request);
  2499. $protected_groups = array_unique($protected_groups);
  2500. }
  2501. // The account page allows the change of your id_group - but not to a protected group!
  2502. if (empty($protected_groups) || count(array_intersect(array((int) $value, $old_profile['id_group']), $protected_groups)) == 0)
  2503. $value = (int) $value;
  2504. // ... otherwise it's the old group sir.
  2505. else
  2506. $value = $old_profile['id_group'];
  2507. // Find the additional membergroups (if any)
  2508. if (isset($_POST['additional_groups']) && is_array($_POST['additional_groups']))
  2509. {
  2510. $additional_groups = array();
  2511. foreach ($_POST['additional_groups'] as $group_id)
  2512. {
  2513. $group_id = (int) $group_id;
  2514. if (!empty($group_id) && (empty($protected_groups) || !in_array($group_id, $protected_groups)))
  2515. $additional_groups[] = $group_id;
  2516. }
  2517. // Put the protected groups back in there if you don't have permission to take them away.
  2518. $old_additional_groups = explode(',', $old_profile['additional_groups']);
  2519. foreach ($old_additional_groups as $group_id)
  2520. {
  2521. if (!empty($protected_groups) && in_array($group_id, $protected_groups))
  2522. $additional_groups[] = $group_id;
  2523. }
  2524. if (implode(',', $additional_groups) !== $old_profile['additional_groups'])
  2525. {
  2526. $profile_vars['additional_groups'] = implode(',', $additional_groups);
  2527. $cur_profile['additional_groups'] = implode(',', $additional_groups);
  2528. }
  2529. }
  2530. // Too often, people remove delete their own account, or something.
  2531. if (in_array(1, explode(',', $old_profile['additional_groups'])) || $old_profile['id_group'] == 1)
  2532. {
  2533. $stillAdmin = $value == 1 || (isset($additional_groups) && in_array(1, $additional_groups));
  2534. // If they would no longer be an admin, look for any other...
  2535. if (!$stillAdmin)
  2536. {
  2537. $request = $smcFunc['db_query']('', '
  2538. SELECT id_member
  2539. FROM {db_prefix}members
  2540. WHERE (id_group = {int:admin_group} OR FIND_IN_SET({int:admin_group}, additional_groups) != 0)
  2541. AND id_member != {int:selected_member}
  2542. LIMIT 1',
  2543. array(
  2544. 'admin_group' => 1,
  2545. 'selected_member' => $context['id_member'],
  2546. )
  2547. );
  2548. list ($another) = $smcFunc['db_fetch_row']($request);
  2549. $smcFunc['db_free_result']($request);
  2550. if (empty($another))
  2551. fatal_lang_error('at_least_one_admin', 'critical');
  2552. }
  2553. }
  2554. // If we are changing group status, update permission cache as necessary.
  2555. if ($value != $old_profile['id_group'] || isset($profile_vars['additional_groups']))
  2556. {
  2557. if ($context['user']['is_owner'])
  2558. $_SESSION['mc']['time'] = 0;
  2559. else
  2560. updateSettings(array('settings_updated' => time()));
  2561. }
  2562. return true;
  2563. }
  2564. /**
  2565. * The avatar is incredibly complicated, what with the options... and what not.
  2566. * @todo argh, the avatar here. Take this out of here!
  2567. *
  2568. * @param array &$value
  2569. * @return mixed
  2570. */
  2571. function profileSaveAvatarData(&$value)
  2572. {
  2573. global $modSettings, $sourcedir, $smcFunc, $profile_vars, $cur_profile, $context;
  2574. $memID = $context['id_member'];
  2575. if (empty($memID) && !empty($context['password_auth_failed']))
  2576. return false;
  2577. require_once($sourcedir . '/ManageAttachments.php');
  2578. // We're going to put this on a nice custom dir.
  2579. $uploadDir = $modSettings['custom_avatar_dir'];
  2580. $id_folder = 1;
  2581. $downloadedExternalAvatar = false;
  2582. if ($value == 'external' && allowedTo('profile_remote_avatar') && (stripos($_POST['userpicpersonal'], 'http://') === 0 || stripos($_POST['userpicpersonal'], 'https://') === 0) && strlen($_POST['userpicpersonal']) > 7 && !empty($modSettings['avatar_download_external']))
  2583. {
  2584. if (!is_writable($uploadDir))
  2585. fatal_lang_error('attachments_no_write', 'critical');
  2586. require_once($sourcedir . '/Subs-Package.php');
  2587. $url = parse_url($_POST['userpicpersonal']);
  2588. $contents = fetch_web_data($url['scheme'] . '://' . $url['host'] . (empty($url['port']) ? '' : ':' . $url['port']) . str_replace(' ', '%20', trim($url['path'])));
  2589. $new_filename = $uploadDir . '/' . getAttachmentFilename('avatar_tmp_' . $memID, false, null, true);
  2590. if ($contents != false && $tmpAvatar = fopen($new_filename, 'wb'))
  2591. {
  2592. fwrite($tmpAvatar, $contents);
  2593. fclose($tmpAvatar);
  2594. $downloadedExternalAvatar = true;
  2595. $_FILES['attachment']['tmp_name'] = $new_filename;
  2596. }
  2597. }
  2598. // Removes whatever attachment there was before updating
  2599. if ($value == 'none')
  2600. {
  2601. $profile_vars['avatar'] = '';
  2602. // Reset the attach ID.
  2603. $cur_profile['id_attach'] = 0;
  2604. $cur_profile['attachment_type'] = 0;
  2605. $cur_profile['filename'] = '';
  2606. removeAttachments(array('id_member' => $memID));
  2607. }
  2608. // An avatar from the server-stored galleries.
  2609. elseif ($value == 'server_stored' && allowedTo('profile_server_avatar'))
  2610. {
  2611. $profile_vars['avatar'] = strtr(empty($_POST['file']) ? (empty($_POST['cat']) ? '' : $_POST['cat']) : $_POST['file'], array('&amp;' => '&'));
  2612. $profile_vars['avatar'] = preg_match('~^([\w _!@%*=\-#()\[\]&.,]+/)?[\w _!@%*=\-#()\[\]&.,]+$~', $profile_vars['avatar']) != 0 && preg_match('/\.\./', $profile_vars['avatar']) == 0 && file_exists($modSettings['avatar_directory'] . '/' . $profile_vars['avatar']) ? ($profile_vars['avatar'] == 'blank.png' ? '' : $profile_vars['avatar']) : '';
  2613. // Clear current profile...
  2614. $cur_profile['id_attach'] = 0;
  2615. $cur_profile['attachment_type'] = 0;
  2616. $cur_profile['filename'] = '';
  2617. // Get rid of their old avatar. (if uploaded.)
  2618. removeAttachments(array('id_member' => $memID));
  2619. }
  2620. elseif ($value == 'external' && allowedTo('profile_remote_avatar') && (stripos($_POST['userpicpersonal'], 'http://') === 0 || stripos($_POST['userpicpersonal'], 'https://') === 0) && empty($modSettings['avatar_download_external']))
  2621. {
  2622. // We need these clean...
  2623. $cur_profile['id_attach'] = 0;
  2624. $cur_profile['attachment_type'] = 0;
  2625. $cur_profile['filename'] = '';
  2626. // Remove any attached avatar...
  2627. removeAttachments(array('id_member' => $memID));
  2628. $profile_vars['avatar'] = str_replace(' ', '%20', preg_replace('~action(?:=|%3d)(?!dlattach)~i', 'action-', $_POST['userpicpersonal']));
  2629. if ($profile_vars['avatar'] == 'http://' || $profile_vars['avatar'] == 'http:///')
  2630. $profile_vars['avatar'] = '';
  2631. // Trying to make us do something we'll regret?
  2632. elseif (substr($profile_vars['avatar'], 0, 7) != 'http://' && substr($profile_vars['avatar'], 0, 8) != 'https://')
  2633. return 'bad_avatar_invalid_url';
  2634. // Should we check dimensions?
  2635. elseif (!empty($modSettings['avatar_max_height_external']) || !empty($modSettings['avatar_max_width_external']))
  2636. {
  2637. // Now let's validate the avatar.
  2638. $sizes = url_image_size($profile_vars['avatar']);
  2639. if (is_array($sizes) && (($sizes[0] > $modSettings['avatar_max_width_external'] && !empty($modSettings['avatar_max_width_external'])) || ($sizes[1] > $modSettings['avatar_max_height_external'] && !empty($modSettings['avatar_max_height_external']))))
  2640. {
  2641. // Houston, we have a problem. The avatar is too large!!
  2642. if ($modSettings['avatar_action_too_large'] == 'option_refuse')
  2643. return 'bad_avatar_too_large';
  2644. elseif ($modSettings['avatar_action_too_large'] == 'option_download_and_resize')
  2645. {
  2646. // @todo remove this if appropriate
  2647. require_once($sourcedir . '/Subs-Graphics.php');
  2648. if (downloadAvatar($profile_vars['avatar'], $memID, $modSettings['avatar_max_width_external'], $modSettings['avatar_max_height_external']))
  2649. {
  2650. $profile_vars['avatar'] = '';
  2651. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2652. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2653. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2654. }
  2655. else
  2656. return 'bad_avatar';
  2657. }
  2658. }
  2659. }
  2660. }
  2661. elseif (($value == 'upload' && allowedTo('profile_upload_avatar')) || $downloadedExternalAvatar)
  2662. {
  2663. if ((isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != '') || $downloadedExternalAvatar)
  2664. {
  2665. // Get the dimensions of the image.
  2666. if (!$downloadedExternalAvatar)
  2667. {
  2668. if (!is_writable($uploadDir))
  2669. fatal_lang_error('attachments_no_write', 'critical');
  2670. $new_filename = $uploadDir . '/' . getAttachmentFilename('avatar_tmp_' . $memID, false, null, true);
  2671. if (!move_uploaded_file($_FILES['attachment']['tmp_name'], $new_filename))
  2672. fatal_lang_error('attach_timeout', 'critical');
  2673. $_FILES['attachment']['tmp_name'] = $new_filename;
  2674. }
  2675. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2676. // No size, then it's probably not a valid pic.
  2677. if ($sizes === false)
  2678. {
  2679. @unlink($_FILES['attachment']['tmp_name']);
  2680. return 'bad_avatar';
  2681. }
  2682. // Check whether the image is too large.
  2683. elseif ((!empty($modSettings['avatar_max_width_upload']) && $sizes[0] > $modSettings['avatar_max_width_upload']) || (!empty($modSettings['avatar_max_height_upload']) && $sizes[1] > $modSettings['avatar_max_height_upload']))
  2684. {
  2685. if (!empty($modSettings['avatar_resize_upload']))
  2686. {
  2687. // Attempt to chmod it.
  2688. @chmod($_FILES['attachment']['tmp_name'], 0644);
  2689. // @todo remove this require when appropriate
  2690. require_once($sourcedir . '/Subs-Graphics.php');
  2691. if (!downloadAvatar($_FILES['attachment']['tmp_name'], $memID, $modSettings['avatar_max_width_upload'], $modSettings['avatar_max_height_upload']))
  2692. {
  2693. @unlink($_FILES['attachment']['tmp_name']);
  2694. return 'bad_avatar';
  2695. }
  2696. // Reset attachment avatar data.
  2697. $cur_profile['id_attach'] = $modSettings['new_avatar_data']['id'];
  2698. $cur_profile['filename'] = $modSettings['new_avatar_data']['filename'];
  2699. $cur_profile['attachment_type'] = $modSettings['new_avatar_data']['type'];
  2700. }
  2701. // Admin doesn't want to resize large avatars, can't do much about it but to tell you to use a different one :(
  2702. else
  2703. {
  2704. @unlink($_FILES['attachment']['tmp_name']);
  2705. return 'bad_avatar_too_large';
  2706. }
  2707. }
  2708. // So far, so good, checks lies ahead!
  2709. elseif (is_array($sizes))
  2710. {
  2711. // Now try to find an infection.
  2712. require_once($sourcedir . '/Subs-Graphics.php');
  2713. if (!checkImageContents($_FILES['attachment']['tmp_name'], !empty($modSettings['avatar_paranoid'])))
  2714. {
  2715. // It's bad. Try to re-encode the contents?
  2716. if (empty($modSettings['avatar_reencode']) || (!reencodeImage($_FILES['attachment']['tmp_name'], $sizes[2])))
  2717. {
  2718. @unlink($_FILES['attachment']['tmp_name']);
  2719. return 'bad_avatar_fail_reencode';
  2720. }
  2721. // We were successful. However, at what price?
  2722. $sizes = @getimagesize($_FILES['attachment']['tmp_name']);
  2723. // Hard to believe this would happen, but can you bet?
  2724. if ($sizes === false)
  2725. {
  2726. @unlink($_FILES['attachment']['tmp_name']);
  2727. return 'bad_avatar';
  2728. }
  2729. }
  2730. $extensions = array(
  2731. '1' => 'gif',
  2732. '2' => 'jpg',
  2733. '3' => 'png',
  2734. '6' => 'bmp'
  2735. );
  2736. $extension = isset($extensions[$sizes[2]]) ? $extensions[$sizes[2]] : 'bmp';
  2737. $mime_type = 'image/' . ($extension === 'jpg' ? 'jpeg' : ($extension === 'bmp' ? 'x-ms-bmp' : $extension));
  2738. $destName = 'avatar_' . $memID . '_' . time() . '.' . $extension;
  2739. list ($width, $height) = getimagesize($_FILES['attachment']['tmp_name']);
  2740. $file_hash = '';
  2741. // Remove previous attachments this member might have had.
  2742. removeAttachments(array('id_member' => $memID));
  2743. $smcFunc['db_insert']('',
  2744. '{db_prefix}attachments',
  2745. array(
  2746. 'id_member' => 'int', 'attachment_type' => 'int', 'filename' => 'string', 'file_hash' => 'string', 'fileext' => 'string', 'size' => 'int',
  2747. 'width' => 'int', 'height' => 'int', 'mime_type' => 'string', 'id_folder' => 'int',
  2748. ),
  2749. array(
  2750. $memID, 1, $destName, $file_hash, $extension, filesize($_FILES['attachment']['tmp_name']),
  2751. (int) $width, (int) $height, $mime_type, $id_folder,
  2752. ),
  2753. array('id_attach')
  2754. );
  2755. $cur_profile['id_attach'] = $smcFunc['db_insert_id']('{db_prefix}attachments', 'id_attach');
  2756. $cur_profile['filename'] = $destName;
  2757. $cur_profile['attachment_type'] = 1;
  2758. $destinationPath = $uploadDir . '/' . (empty($file_hash) ? $destName : $cur_profile['id_attach'] . '_' . $file_hash . '.dat');
  2759. if (!rename($_FILES['attachment']['tmp_name'], $destinationPath))
  2760. {
  2761. // I guess a man can try.
  2762. removeAttachments(array('id_member' => $memID));
  2763. fatal_lang_error('attach_timeout', 'critical');
  2764. }
  2765. // Attempt to chmod it.
  2766. @chmod($uploadDir . '/' . $destinationPath, 0644);
  2767. }
  2768. $profile_vars['avatar'] = '';
  2769. // Delete any temporary file.
  2770. if (file_exists($_FILES['attachment']['tmp_name']))
  2771. @unlink($_FILES['attachment']['tmp_name']);
  2772. }
  2773. // Selected the upload avatar option and had one already uploaded before or didn't upload one.
  2774. else
  2775. $profile_vars['avatar'] = '';
  2776. }
  2777. else
  2778. $profile_vars['avatar'] = '';
  2779. // Setup the profile variables so it shows things right on display!
  2780. $cur_profile['avatar'] = $profile_vars['avatar'];
  2781. return false;
  2782. }
  2783. /**
  2784. * Validate the signature
  2785. *
  2786. * @param mixed &$value
  2787. * @return boolean|string
  2788. */
  2789. function profileValidateSignature(&$value)
  2790. {
  2791. global $sourcedir, $modSettings, $smcFunc, $txt;
  2792. require_once($sourcedir . '/Subs-Post.php');
  2793. // Admins can do whatever they hell they want!
  2794. if (!allowedTo('admin_forum'))
  2795. {
  2796. // Load all the signature limits.
  2797. list ($sig_limits, $sig_bbc) = explode(':', $modSettings['signature_settings']);
  2798. $sig_limits = explode(',', $sig_limits);
  2799. $disabledTags = !empty($sig_bbc) ? explode(',', $sig_bbc) : array();
  2800. $unparsed_signature = strtr(un_htmlspecialchars($value), array("\r" => '', '&#039' => '\''));
  2801. // Too many lines?
  2802. if (!empty($sig_limits[2]) && substr_count($unparsed_signature, "\n") >= $sig_limits[2])
  2803. {
  2804. $txt['profile_error_signature_max_lines'] = sprintf($txt['profile_error_signature_max_lines'], $sig_limits[2]);
  2805. return 'signature_max_lines';
  2806. }
  2807. // Too many images?!
  2808. if (!empty($sig_limits[3]) && (substr_count(strtolower($unparsed_signature), '[img') + substr_count(strtolower($unparsed_signature), '<img')) > $sig_limits[3])
  2809. {
  2810. $txt['profile_error_signature_max_image_count'] = sprintf($txt['profile_error_signature_max_image_count'], $sig_limits[3]);
  2811. return 'signature_max_image_count';
  2812. }
  2813. // What about too many smileys!
  2814. $smiley_parsed = $unparsed_signature;
  2815. parsesmileys($smiley_parsed);
  2816. $smiley_count = substr_count(strtolower($smiley_parsed), '<img') - substr_count(strtolower($unparsed_signature), '<img');
  2817. if (!empty($sig_limits[4]) && $sig_limits[4] == -1 && $smiley_count > 0)
  2818. return 'signature_allow_smileys';
  2819. elseif (!empty($sig_limits[4]) && $sig_limits[4] > 0 && $smiley_count > $sig_limits[4])
  2820. {
  2821. $txt['profile_error_signature_max_smileys'] = sprintf($txt['profile_error_signature_max_smileys'], $sig_limits[4]);
  2822. return 'signature_max_smileys';
  2823. }
  2824. // Maybe we are abusing font sizes?
  2825. if (!empty($sig_limits[7]) && preg_match_all('~\[size=([\d\.]+)?(px|pt|em|x-large|larger)~i', $unparsed_signature, $matches) !== false && isset($matches[2]))
  2826. {
  2827. foreach ($matches[1] as $ind => $size)
  2828. {
  2829. $limit_broke = 0;
  2830. // Attempt to allow all sizes of abuse, so to speak.
  2831. if ($matches[2][$ind] == 'px' && $size > $sig_limits[7])
  2832. $limit_broke = $sig_limits[7] . 'px';
  2833. elseif ($matches[2][$ind] == 'pt' && $size > ($sig_limits[7] * 0.75))
  2834. $limit_broke = ((int) $sig_limits[7] * 0.75) . 'pt';
  2835. elseif ($matches[2][$ind] == 'em' && $size > ((float) $sig_limits[7] / 16))
  2836. $limit_broke = ((float) $sig_limits[7] / 16) . 'em';
  2837. elseif ($matches[2][$ind] != 'px' && $matches[2][$ind] != 'pt' && $matches[2][$ind] != 'em' && $sig_limits[7] < 18)
  2838. $limit_broke = 'large';
  2839. if ($limit_broke)
  2840. {
  2841. $txt['profile_error_signature_max_font_size'] = sprintf($txt['profile_error_signature_max_font_size'], $limit_broke);
  2842. return 'signature_max_font_size';
  2843. }
  2844. }
  2845. }
  2846. // The difficult one - image sizes! Don't error on this - just fix it.
  2847. if ((!empty($sig_limits[5]) || !empty($sig_limits[6])))
  2848. {
  2849. // Get all BBC tags...
  2850. preg_match_all('~\[img(\s+width=([\d]+))?(\s+height=([\d]+))?(\s+width=([\d]+))?\s*\](?:<br>)*([^<">]+?)(?:<br>)*\[/img\]~i', $unparsed_signature, $matches);
  2851. // ... and all HTML ones.
  2852. preg_match_all('~<img\s+src=(?:")?((?:http://|ftp://|https://|ftps://).+?)(?:")?(?:\s+alt=(?:")?(.*?)(?:")?)?(?:\s?/)?>~i', $unparsed_signature, $matches2, PREG_PATTERN_ORDER);
  2853. // And stick the HTML in the BBC.
  2854. if (!empty($matches2))
  2855. {
  2856. foreach ($matches2[0] as $ind => $dummy)
  2857. {
  2858. $matches[0][] = $matches2[0][$ind];
  2859. $matches[1][] = '';
  2860. $matches[2][] = '';
  2861. $matches[3][] = '';
  2862. $matches[4][] = '';
  2863. $matches[5][] = '';
  2864. $matches[6][] = '';
  2865. $matches[7][] = $matches2[1][$ind];
  2866. }
  2867. }
  2868. $replaces = array();
  2869. // Try to find all the images!
  2870. if (!empty($matches))
  2871. {
  2872. foreach ($matches[0] as $key => $image)
  2873. {
  2874. $width = -1; $height = -1;
  2875. // Does it have predefined restraints? Width first.
  2876. if ($matches[6][$key])
  2877. $matches[2][$key] = $matches[6][$key];
  2878. if ($matches[2][$key] && $sig_limits[5] && $matches[2][$key] > $sig_limits[5])
  2879. {
  2880. $width = $sig_limits[5];
  2881. $matches[4][$key] = $matches[4][$key] * ($width / $matches[2][$key]);
  2882. }
  2883. elseif ($matches[2][$key])
  2884. $width = $matches[2][$key];
  2885. // ... and height.
  2886. if ($matches[4][$key] && $sig_limits[6] && $matches[4][$key] > $sig_limits[6])
  2887. {
  2888. $height = $sig_limits[6];
  2889. if ($width != -1)
  2890. $width = $width * ($height / $matches[4][$key]);
  2891. }
  2892. elseif ($matches[4][$key])
  2893. $height = $matches[4][$key];
  2894. // If the dimensions are still not fixed - we need to check the actual image.
  2895. if (($width == -1 && $sig_limits[5]) || ($height == -1 && $sig_limits[6]))
  2896. {
  2897. $sizes = url_image_size($matches[7][$key]);
  2898. if (is_array($sizes))
  2899. {
  2900. // Too wide?
  2901. if ($sizes[0] > $sig_limits[5] && $sig_limits[5])
  2902. {
  2903. $width = $sig_limits[5];
  2904. $sizes[1] = $sizes[1] * ($width / $sizes[0]);
  2905. }
  2906. // Too high?
  2907. if ($sizes[1] > $sig_limits[6] && $sig_limits[6])
  2908. {
  2909. $height = $sig_limits[6];
  2910. if ($width == -1)
  2911. $width = $sizes[0];
  2912. $width = $width * ($height / $sizes[1]);
  2913. }
  2914. elseif ($width != -1)
  2915. $height = $sizes[1];
  2916. }
  2917. }
  2918. // Did we come up with some changes? If so remake the string.
  2919. if ($width != -1 || $height != -1)
  2920. $replaces[$image] = '[img' . ($width != -1 ? ' width=' . round($width) : '') . ($height != -1 ? ' height=' . round($height) : '') . ']' . $matches[7][$key] . '[/img]';
  2921. }
  2922. if (!empty($replaces))
  2923. $value = str_replace(array_keys($replaces), array_values($replaces), $value);
  2924. }
  2925. }
  2926. // Any disabled BBC?
  2927. $disabledSigBBC = implode('|', $disabledTags);
  2928. if (!empty($disabledSigBBC))
  2929. {
  2930. if (preg_match('~\[(' . $disabledSigBBC . '[ =\]/])~i', $unparsed_signature, $matches) !== false && isset($matches[1]))
  2931. {
  2932. $disabledTags = array_unique($disabledTags);
  2933. $txt['profile_error_signature_disabled_bbc'] = sprintf($txt['profile_error_signature_disabled_bbc'], implode(', ', $disabledTags));
  2934. return 'signature_disabled_bbc';
  2935. }
  2936. }
  2937. }
  2938. preparsecode($value);
  2939. // Too long?
  2940. if (!allowedTo('admin_forum') && !empty($sig_limits[1]) && $smcFunc['strlen'](str_replace('<br>', "\n", $value)) > $sig_limits[1])
  2941. {
  2942. $_POST['signature'] = trim($smcFunc['htmlspecialchars'](str_replace('<br>', "\n", $value), ENT_QUOTES));
  2943. $txt['profile_error_signature_max_length'] = sprintf($txt['profile_error_signature_max_length'], $sig_limits[1]);
  2944. return 'signature_max_length';
  2945. }
  2946. return true;
  2947. }
  2948. /**
  2949. * Validate an email address.
  2950. *
  2951. * @param string $email
  2952. * @param int $memID = 0
  2953. * @return boolean|string
  2954. */
  2955. function profileValidateEmail($email, $memID = 0)
  2956. {
  2957. global $smcFunc, $context;
  2958. $email = strtr($email, array('&#039;' => '\''));
  2959. // Check the name and email for validity.
  2960. if (trim($email) == '')
  2961. return 'no_email';
  2962. if (preg_match('~^[0-9A-Za-z=_+\-/][0-9A-Za-z=_\'+\-/\.]*@[\w\-]+(\.[\w\-]+)*(\.[\w]{2,6})$~', $email) == 0)
  2963. return 'bad_email';
  2964. // Email addresses should be and stay unique.
  2965. $request = $smcFunc['db_query']('', '
  2966. SELECT id_member
  2967. FROM {db_prefix}members
  2968. WHERE ' . ($memID != 0 ? 'id_member != {int:selected_member} AND ' : '') . '
  2969. email_address = {string:email_address}
  2970. LIMIT 1',
  2971. array(
  2972. 'selected_member' => $memID,
  2973. 'email_address' => $email,
  2974. )
  2975. );
  2976. if ($smcFunc['db_num_rows']($request) > 0)
  2977. return 'email_taken';
  2978. $smcFunc['db_free_result']($request);
  2979. return true;
  2980. }
  2981. /**
  2982. * Reload a users settings.
  2983. */
  2984. function profileReloadUser()
  2985. {
  2986. global $sourcedir, $modSettings, $context, $cur_profile, $smcFunc, $profile_vars;
  2987. // Log them back in - using the verify password as they must have matched and this one doesn't get changed by anyone!
  2988. if (isset($_POST['passwrd2']) && $_POST['passwrd2'] != '')
  2989. {
  2990. require_once($sourcedir . '/Subs-Auth.php');
  2991. setLoginCookie(60 * $modSettings['cookieTime'], $context['id_member'], sha1(sha1(strtolower($cur_profile['member_name']) . un_htmlspecialchars($_POST['passwrd2'])) . $cur_profile['password_salt']));
  2992. }
  2993. loadUserSettings();
  2994. writeLog();
  2995. }
  2996. /**
  2997. * Send the user a new activation email if they need to reactivate!
  2998. */
  2999. function profileSendActivation()
  3000. {
  3001. global $sourcedir, $profile_vars, $txt, $context, $scripturl, $smcFunc, $cookiename, $cur_profile, $language, $modSettings;
  3002. require_once($sourcedir . '/Subs-Post.php');
  3003. // Shouldn't happen but just in case.
  3004. if (empty($profile_vars['email_address']))
  3005. return;
  3006. $replacements = array(
  3007. 'ACTIVATIONLINK' => $scripturl . '?action=activate;u=' . $context['id_member'] . ';code=' . $profile_vars['validation_code'],
  3008. 'ACTIVATIONCODE' => $profile_vars['validation_code'],
  3009. 'ACTIVATIONLINKWITHOUTCODE' => $scripturl . '?action=activate;u=' . $context['id_member'],
  3010. );
  3011. // Send off the email.
  3012. $emaildata = loadEmailTemplate('activate_reactivate', $replacements, empty($cur_profile['lngfile']) || empty($modSettings['userLanguage']) ? $language : $cur_profile['lngfile']);
  3013. sendmail($profile_vars['email_address'], $emaildata['subject'], $emaildata['body'], null, 'reactivate', false, 0);
  3014. // Log the user out.
  3015. $smcFunc['db_query']('', '
  3016. DELETE FROM {db_prefix}log_online
  3017. WHERE id_member = {int:selected_member}',
  3018. array(
  3019. 'selected_member' => $context['id_member'],
  3020. )
  3021. );
  3022. $_SESSION['log_time'] = 0;
  3023. $_SESSION['login_' . $cookiename] = serialize(array(0, '', 0));
  3024. if (isset($_COOKIE[$cookiename]))
  3025. $_COOKIE[$cookiename] = '';
  3026. loadUserSettings();
  3027. $context['user']['is_logged'] = false;
  3028. $context['user']['is_guest'] = true;
  3029. redirectexit('action=sendactivation');
  3030. }
  3031. /**
  3032. * Function to allow the user to choose group membership etc...
  3033. *
  3034. * @param int $memID id_member
  3035. */
  3036. function groupMembership($memID)
  3037. {
  3038. global $txt, $scripturl, $user_profile, $context, $smcFunc;
  3039. $curMember = $user_profile[$memID];
  3040. $context['primary_group'] = $curMember['id_group'];
  3041. // Can they manage groups?
  3042. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  3043. $context['can_manage_protected'] = allowedTo('admin_forum');
  3044. $context['can_edit_primary'] = $context['can_manage_protected'];
  3045. $context['update_message'] = isset($_GET['msg']) && isset($txt['group_membership_msg_' . $_GET['msg']]) ? $txt['group_membership_msg_' . $_GET['msg']] : '';
  3046. // Get all the groups this user is a member of.
  3047. $groups = explode(',', $curMember['additional_groups']);
  3048. $groups[] = $curMember['id_group'];
  3049. // Ensure the query doesn't croak!
  3050. if (empty($groups))
  3051. $groups = array(0);
  3052. // Just to be sure...
  3053. foreach ($groups as $k => $v)
  3054. $groups[$k] = (int) $v;
  3055. // Get all the membergroups they can join.
  3056. $request = $smcFunc['db_query']('', '
  3057. SELECT mg.id_group, mg.group_name, mg.description, mg.group_type, mg.online_color, mg.hidden,
  3058. IFNULL(lgr.id_member, 0) AS pending
  3059. FROM {db_prefix}membergroups AS mg
  3060. LEFT JOIN {db_prefix}log_group_requests AS lgr ON (lgr.id_member = {int:selected_member} AND lgr.id_group = mg.id_group AND lgr.status = {int:status_open})
  3061. WHERE (mg.id_group IN ({array_int:group_list})
  3062. OR mg.group_type > {int:nonjoin_group_id})
  3063. AND mg.min_posts = {int:min_posts}
  3064. AND mg.id_group != {int:moderator_group}
  3065. ORDER BY group_name',
  3066. array(
  3067. 'group_list' => $groups,
  3068. 'selected_member' => $memID,
  3069. 'status_open' => 0,
  3070. 'nonjoin_group_id' => 1,
  3071. 'min_posts' => -1,
  3072. 'moderator_group' => 3,
  3073. )
  3074. );
  3075. // This beast will be our group holder.
  3076. $context['groups'] = array(
  3077. 'member' => array(),
  3078. 'available' => array()
  3079. );
  3080. while ($row = $smcFunc['db_fetch_assoc']($request))
  3081. {
  3082. // Can they edit their primary group?
  3083. if (($row['id_group'] == $context['primary_group'] && $row['group_type'] > 1) || ($row['hidden'] != 2 && $context['primary_group'] == 0 && in_array($row['id_group'], $groups)))
  3084. $context['can_edit_primary'] = true;
  3085. // If they can't manage (protected) groups, and it's not publically joinable or already assigned, they can't see it.
  3086. if (((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0)) && $row['id_group'] != $context['primary_group'])
  3087. continue;
  3088. $context['groups'][in_array($row['id_group'], $groups) ? 'member' : 'available'][$row['id_group']] = array(
  3089. 'id' => $row['id_group'],
  3090. 'name' => $row['group_name'],
  3091. 'desc' => $row['description'],
  3092. 'color' => $row['online_color'],
  3093. 'type' => $row['group_type'],
  3094. 'pending' => $row['pending'],
  3095. 'is_primary' => $row['id_group'] == $context['primary_group'],
  3096. 'can_be_primary' => $row['hidden'] != 2,
  3097. // Anything more than this needs to be done through account settings for security.
  3098. 'can_leave' => $row['id_group'] != 1 && $row['group_type'] > 1 ? true : false,
  3099. );
  3100. }
  3101. $smcFunc['db_free_result']($request);
  3102. // Add registered members on the end.
  3103. $context['groups']['member'][0] = array(
  3104. 'id' => 0,
  3105. 'name' => $txt['regular_members'],
  3106. 'desc' => $txt['regular_members_desc'],
  3107. 'type' => 0,
  3108. 'is_primary' => $context['primary_group'] == 0 ? true : false,
  3109. 'can_be_primary' => true,
  3110. 'can_leave' => 0,
  3111. );
  3112. // No changing primary one unless you have enough groups!
  3113. if (count($context['groups']['member']) < 2)
  3114. $context['can_edit_primary'] = false;
  3115. // In the special case that someone is requesting membership of a group, setup some special context vars.
  3116. if (isset($_REQUEST['request']) && isset($context['groups']['available'][(int) $_REQUEST['request']]) && $context['groups']['available'][(int) $_REQUEST['request']]['type'] == 2)
  3117. $context['group_request'] = $context['groups']['available'][(int) $_REQUEST['request']];
  3118. }
  3119. /**
  3120. * This function actually makes all the group changes
  3121. *
  3122. * @param array $profile_vars
  3123. * @param array $post_errors
  3124. * @param int $memID id_member
  3125. * @return mixed
  3126. */
  3127. function groupMembership2($profile_vars, $post_errors, $memID)
  3128. {
  3129. global $user_info, $sourcedir, $context, $user_profile, $modSettings, $txt, $smcFunc, $scripturl, $language;
  3130. // Let's be extra cautious...
  3131. if (!$context['user']['is_owner'] || empty($modSettings['show_group_membership']))
  3132. isAllowedTo('manage_membergroups');
  3133. if (!isset($_REQUEST['gid']) && !isset($_POST['primary']))
  3134. fatal_lang_error('no_access', false);
  3135. checkSession(isset($_GET['gid']) ? 'get' : 'post');
  3136. $old_profile = &$user_profile[$memID];
  3137. $context['can_manage_membergroups'] = allowedTo('manage_membergroups');
  3138. $context['can_manage_protected'] = allowedTo('admin_forum');
  3139. // By default the new primary is the old one.
  3140. $newPrimary = $old_profile['id_group'];
  3141. $addGroups = array_flip(explode(',', $old_profile['additional_groups']));
  3142. $canChangePrimary = $old_profile['id_group'] == 0 ? 1 : 0;
  3143. $changeType = isset($_POST['primary']) ? 'primary' : (isset($_POST['req']) ? 'request' : 'free');
  3144. // One way or another, we have a target group in mind...
  3145. $group_id = isset($_REQUEST['gid']) ? (int) $_REQUEST['gid'] : (int) $_POST['primary'];
  3146. $foundTarget = $changeType == 'primary' && $group_id == 0 ? true : false;
  3147. // Sanity check!!
  3148. if ($group_id == 1)
  3149. isAllowedTo('admin_forum');
  3150. // Protected groups too!
  3151. else
  3152. {
  3153. $request = $smcFunc['db_query']('', '
  3154. SELECT group_type
  3155. FROM {db_prefix}membergroups
  3156. WHERE id_group = {int:current_group}
  3157. LIMIT {int:limit}',
  3158. array(
  3159. 'current_group' => $group_id,
  3160. 'limit' => 1,
  3161. )
  3162. );
  3163. list ($is_protected) = $smcFunc['db_fetch_row']($request);
  3164. $smcFunc['db_free_result']($request);
  3165. if ($is_protected == 1)
  3166. isAllowedTo('admin_forum');
  3167. }
  3168. // What ever we are doing, we need to determine if changing primary is possible!
  3169. $request = $smcFunc['db_query']('', '
  3170. SELECT id_group, group_type, hidden, group_name
  3171. FROM {db_prefix}membergroups
  3172. WHERE id_group IN ({int:group_list}, {int:current_group})',
  3173. array(
  3174. 'group_list' => $group_id,
  3175. 'current_group' => $old_profile['id_group'],
  3176. )
  3177. );
  3178. while ($row = $smcFunc['db_fetch_assoc']($request))
  3179. {
  3180. // Is this the new group?
  3181. if ($row['id_group'] == $group_id)
  3182. {
  3183. $foundTarget = true;
  3184. $group_name = $row['group_name'];
  3185. // Does the group type match what we're doing - are we trying to request a non-requestable group?
  3186. if ($changeType == 'request' && $row['group_type'] != 2)
  3187. fatal_lang_error('no_access', false);
  3188. // What about leaving a requestable group we are not a member of?
  3189. elseif ($changeType == 'free' && $row['group_type'] == 2 && $old_profile['id_group'] != $row['id_group'] && !isset($addGroups[$row['id_group']]))
  3190. fatal_lang_error('no_access', false);
  3191. elseif ($changeType == 'free' && $row['group_type'] != 3 && $row['group_type'] != 2)
  3192. fatal_lang_error('no_access', false);
  3193. // We can't change the primary group if this is hidden!
  3194. if ($row['hidden'] == 2)
  3195. $canChangePrimary = false;
  3196. }
  3197. // If this is their old primary, can we change it?
  3198. if ($row['id_group'] == $old_profile['id_group'] && ($row['group_type'] > 1 || $context['can_manage_membergroups']) && $canChangePrimary !== false)
  3199. $canChangePrimary = 1;
  3200. // If we are not doing a force primary move, don't do it automatically if current primary is not 0.
  3201. if ($changeType != 'primary' && $old_profile['id_group'] != 0)
  3202. $canChangePrimary = false;
  3203. // If this is the one we are acting on, can we even act?
  3204. if ((!$context['can_manage_protected'] && $row['group_type'] == 1) || (!$context['can_manage_membergroups'] && $row['group_type'] == 0))
  3205. $canChangePrimary = false;
  3206. }
  3207. $smcFunc['db_free_result']($request);
  3208. // Didn't find the target?
  3209. if (!$foundTarget)
  3210. fatal_lang_error('no_access', false);
  3211. // Final security check, don't allow users to promote themselves to admin.
  3212. if ($context['can_manage_membergroups'] && !allowedTo('admin_forum'))
  3213. {
  3214. $request = $smcFunc['db_query']('', '
  3215. SELECT COUNT(permission)
  3216. FROM {db_prefix}permissions
  3217. WHERE id_group = {int:selected_group}
  3218. AND permission = {string:admin_forum}
  3219. AND add_deny = {int:not_denied}',
  3220. array(
  3221. 'selected_group' => $group_id,
  3222. 'not_denied' => 1,
  3223. 'admin_forum' => 'admin_forum',
  3224. )
  3225. );
  3226. list ($disallow) = $smcFunc['db_fetch_row']($request);
  3227. $smcFunc['db_free_result']($request);
  3228. if ($disallow)
  3229. isAllowedTo('admin_forum');
  3230. }
  3231. // If we're requesting, add the note then return.
  3232. if ($changeType == 'request')
  3233. {
  3234. $request = $smcFunc['db_query']('', '
  3235. SELECT id_member
  3236. FROM {db_prefix}log_group_requests
  3237. WHERE id_member = {int:selected_member}
  3238. AND id_group = {int:selected_group}
  3239. AND status = {int:status_open}',
  3240. array(
  3241. 'selected_member' => $memID,
  3242. 'selected_group' => $group_id,
  3243. 'status_open' => 0,
  3244. )
  3245. );
  3246. if ($smcFunc['db_num_rows']($request) != 0)
  3247. fatal_lang_error('profile_error_already_requested_group');
  3248. $smcFunc['db_free_result']($request);
  3249. // Log the request.
  3250. $smcFunc['db_insert']('',
  3251. '{db_prefix}log_group_requests',
  3252. array(
  3253. 'id_member' => 'int', 'id_group' => 'int', 'time_applied' => 'int', 'reason' => 'string-65534',
  3254. 'status' => 'int', 'id_member_acted' => 'int', 'member_name_acted' => 'string', 'time_acted' => 'int', 'act_reason' => 'string',
  3255. ),
  3256. array(
  3257. $memID, $group_id, time(), $_POST['reason'],
  3258. 0, 0, '', 0, '',
  3259. ),
  3260. array('id_request')
  3261. );
  3262. // Set up some data for our background task...
  3263. $data = serialize(array('id_member' => $memID, 'member_name' => $user_info['name'], 'id_group' => $group_id, 'group_name' => $group_name, 'reason' => $_POST['reason'], 'time' => time()));
  3264. // Add a background task to handle notifying people of this request
  3265. $smcFunc['db_insert']('insert', '{db_prefix}background_tasks',
  3266. array('task_file' => 'string-255', 'task_class' => 'string-255', 'task_data' => 'string', 'claimed_time' => 'int'),
  3267. array('$sourcedir/tasks/GroupReq-Notify.php', 'GroupReq_Notify_Background', $data, 0), array()
  3268. );
  3269. return $changeType;
  3270. }
  3271. // Otherwise we are leaving/joining a group.
  3272. elseif ($changeType == 'free')
  3273. {
  3274. // Are we leaving?
  3275. if ($old_profile['id_group'] == $group_id || isset($addGroups[$group_id]))
  3276. {
  3277. if ($old_profile['id_group'] == $group_id)
  3278. $newPrimary = 0;
  3279. else
  3280. unset($addGroups[$group_id]);
  3281. }
  3282. // ... if not, must be joining.
  3283. else
  3284. {
  3285. // Can we change the primary, and do we want to?
  3286. if ($canChangePrimary)
  3287. {
  3288. if ($old_profile['id_group'] != 0)
  3289. $addGroups[$old_profile['id_group']] = -1;
  3290. $newPrimary = $group_id;
  3291. }
  3292. // Otherwise it's an additional group...
  3293. else
  3294. $addGroups[$group_id] = -1;
  3295. }
  3296. }
  3297. // Finally, we must be setting the primary.
  3298. elseif ($canChangePrimary)
  3299. {
  3300. if ($old_profile['id_group'] != 0)
  3301. $addGroups[$old_profile['id_group']] = -1;
  3302. if (isset($addGroups[$group_id]))
  3303. unset($addGroups[$group_id]);
  3304. $newPrimary = $group_id;
  3305. }
  3306. // Finally, we can make the changes!
  3307. foreach ($addGroups as $id => $dummy)
  3308. if (empty($id))
  3309. unset($addGroups[$id]);
  3310. $addGroups = implode(',', array_flip($addGroups));
  3311. // Ensure that we don't cache permissions if the group is changing.
  3312. if ($context['user']['is_owner'])
  3313. $_SESSION['mc']['time'] = 0;
  3314. else
  3315. updateSettings(array('settings_updated' => time()));
  3316. updateMemberData($memID, array('id_group' => $newPrimary, 'additional_groups' => $addGroups));
  3317. return $changeType;
  3318. }
  3319. ?>